拷贝时间测试=cudamelloc+cudahostalloc

  1 /*
  2 * Copyright 1993-2010 NVIDIA Corporation.  All rights reserved.
  3 *
  4 * NVIDIA Corporation and its licensors retain all intellectual property and
  5 * proprietary rights in and to this software and related documentation.
  6 * Any use, reproduction, disclosure, or distribution of this software
  7 * and related documentation without an express license agreement from
  8 * NVIDIA Corporation is strictly prohibited.
  9 *
 10 * Please refer to the applicable NVIDIA end user license agreement (EULA)
 11 * associated with this source code for terms and conditions that govern
 12 * your use of this NVIDIA software.
 13 *
 14 */
 15
 16
 17 #include "../common/book.h"
 18 #include <cuda.h>
 19 #include "cuda_runtime.h"
 20 #include "device_launch_parameters.h"
 21 #define SIZE    (64*1024*1024)
 22
 23
 24 float cuda_malloc_test(int size, bool up) {
 25     cudaEvent_t     start, stop;
 26     int             *a, *dev_a;
 27     float           elapsedTime;
 28
 29     HANDLE_ERROR(cudaEventCreate(&start));
 30     HANDLE_ERROR(cudaEventCreate(&stop));
 31
 32     a = (int*)malloc(size * sizeof(*a));
 33     HANDLE_NULL(a);
 34     HANDLE_ERROR(cudaMalloc((void**)&dev_a,
 35         size * sizeof(*dev_a)));
 36
 37     HANDLE_ERROR(cudaEventRecord(start, 0));
 38     for (int i = 0; i<100; i++) {
 39         if (up)
 40             HANDLE_ERROR(cudaMemcpy(dev_a, a,
 41             size * sizeof(*dev_a),
 42             cudaMemcpyHostToDevice));
 43         else
 44             HANDLE_ERROR(cudaMemcpy(a, dev_a,
 45             size * sizeof(*dev_a),
 46             cudaMemcpyDeviceToHost));
 47     }
 48     HANDLE_ERROR(cudaEventRecord(stop, 0));
 49     HANDLE_ERROR(cudaEventSynchronize(stop));
 50     HANDLE_ERROR(cudaEventElapsedTime(&elapsedTime,
 51         start, stop));
 52
 53     free(a);
 54     HANDLE_ERROR(cudaFree(dev_a));
 55     HANDLE_ERROR(cudaEventDestroy(start));
 56     HANDLE_ERROR(cudaEventDestroy(stop));
 57
 58     return elapsedTime;
 59 }
 60
 61
 62 float cuda_host_alloc_test(int size, bool up) {
 63     cudaEvent_t     start, stop;
 64     int             *a, *dev_a;
 65     float           elapsedTime;
 66
 67     HANDLE_ERROR(cudaEventCreate(&start));
 68     HANDLE_ERROR(cudaEventCreate(&stop));
 69
 70     HANDLE_ERROR(cudaHostAlloc((void**)&a,
 71         size * sizeof(*a),
 72         cudaHostAllocDefault));
 73     HANDLE_ERROR(cudaMalloc((void**)&dev_a,
 74         size * sizeof(*dev_a)));
 75
 76     HANDLE_ERROR(cudaEventRecord(start, 0));
 77     for (int i = 0; i<100; i++) {
 78         if (up)
 79             HANDLE_ERROR(cudaMemcpy(dev_a, a,
 80             size * sizeof(*a),
 81             cudaMemcpyHostToDevice));
 82         else
 83             HANDLE_ERROR(cudaMemcpy(a, dev_a,
 84             size * sizeof(*a),
 85             cudaMemcpyDeviceToHost));
 86     }
 87     HANDLE_ERROR(cudaEventRecord(stop, 0));
 88     HANDLE_ERROR(cudaEventSynchronize(stop));
 89     HANDLE_ERROR(cudaEventElapsedTime(&elapsedTime,
 90         start, stop));
 91
 92     HANDLE_ERROR(cudaFreeHost(a));
 93     HANDLE_ERROR(cudaFree(dev_a));
 94     HANDLE_ERROR(cudaEventDestroy(start));
 95     HANDLE_ERROR(cudaEventDestroy(stop));
 96
 97     return elapsedTime;
 98 }
 99
100
101 int main(void) {
102     float           elapsedTime;
103     float           MB = (float)100 * SIZE*sizeof(int) / 1024 / 1024;
104
105
106     // try it with cudaMalloc
107     elapsedTime = cuda_malloc_test(SIZE, true);
108     printf("Time using cudaMalloc:  %3.1f ms\n",
109         elapsedTime);
110     printf("\tMB/s during copy up:  %3.1f\n",
111         MB / (elapsedTime / 1000));
112
113     elapsedTime = cuda_malloc_test(SIZE, false);
114     printf("Time using cudaMalloc:  %3.1f ms\n",
115         elapsedTime);
116     printf("\tMB/s during copy down:  %3.1f\n",
117         MB / (elapsedTime / 1000));
118
119     // now try it with cudaHostAlloc
120     elapsedTime = cuda_host_alloc_test(SIZE, true);
121     printf("Time using cudaHostAlloc:  %3.1f ms\n",
122         elapsedTime);
123     printf("\tMB/s during copy up:  %3.1f\n",
124         MB / (elapsedTime / 1000));
125
126     elapsedTime = cuda_host_alloc_test(SIZE, false);
127     printf("Time using cudaHostAlloc:  %3.1f ms\n",
128         elapsedTime);
129     printf("\tMB/s during copy down:  %3.1f\n",
130         MB / (elapsedTime / 1000));
131 }

项目打包下载

时间: 2024-10-25 21:03:09

拷贝时间测试=cudamelloc+cudahostalloc的相关文章

算法时间测试

1 //算法时间测试 基准法(定量分析) 大O法(定性分析) 2 //以下是基准法 3 #include "iostream" 4 #include "cstdio" 5 #include "ctime" 6 #include "windows.h" 7 using namespace std; 8 int main() 9 { 10 //法一 11 clock_t start1,end1;//clock_t 需头文件#inc

使用拷贝文件测试(BufferedInputStream,FileInputStream)

package com.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileDemo {

关于Java中的HashMap的深浅拷贝的测试与几点思考

0.前言 工作忙起来后,许久不看算法,竟然DFA敏感词算法都要看好一阵才能理解...真是和三阶魔方还原手法一样,田园将芜,非常可惜啊. 在DFA算法中,第一步是需要理解它的数据结构,在此基础上,涉及到一些Hashmap的赋值.这里的赋值非常有趣,三个Hashmap翻来覆去赋值,就解决了敏感词表的初始化. 里面都是属于下文中的Hashmap"浅拷贝",那么究竟Java中的Hashmap有哪些拷贝方法呢? 1.测试代码 HashMap hm_source = new HashMap();

R︱Rstudio 1.0版本尝鲜(R notebook、下载链接、sparkR、代码时间测试profile)

2016年11月1日,RStudio 1.0版本正式发布!这是自2011年2月RStudio发布以来的第10个主要版本,也是更新最大的一次.主要亮点如下: 1.R Notebooks 的辅助工具(实时反馈结果,这个RMD做不来) 2.对sparklyr包的集成支持(R与Spark连接) 3.profvis包进行性能分析(代码运行步骤.代码运行时间) 4.基于readr/readxl/haven,优化数据读取(任意修改载入行名.列名以及一些字符串处理) 5.R Markdown的辅助工具(webs

C#下的时间测试(用于计算方法执行时间)

1 public class Timing 2 { 3 private TimeSpan m_StartTime; 4 5 private TimeSpan duringTime; 6 7 public Timing() //构造函数用于初始化开始时间和持续时间 8 { 9 this.m_StartTime = new TimeSpan(0); //初始化的时候设置时间为00:00:00 10 this.duringTime = new TimeSpan(0); 11 } 12 13 publi

python多进程并行执行和顺序执行的时间测试

#_*_coding:utf-8_*_ import time from  multiprocessing import Pool from threading import Thread def func1(fn):     time.sleep(1)     return fn * fn if __name__ == "__main__":     a = [1,2,3,4,5,6]     print "顺序执行的方式开始..."     s = time.t

单线程和多线程在pc上执行花费时间测试

1 package Thread; 2 3 import junit.framework.Assert; 4 import org.junit.Test; 5 6 import java.util.concurrent.CountDownLatch; 7 import java.util.concurrent.Executor; 8 import java.util.concurrent.Executors; 9 10 /** 11 * Created by csophys on 15/7/11

Unity-Animator深入系列---StateMachineBehaviour初始化时间测试

回到 Animator深入系列总目录 结果有点出乎意料. MyClass public class MyClass { public MyClass() { Debug.Log("Init!"); } } TestSMB public class TestSMB : StateMachineBehaviour { public MyClass obj = new MyClass(); public override void OnStateMachineEnter(Animator a

USACO1.5Superprime Rid[附带关于素数算法时间测试]

题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数. 7331 被叫做长度 4 的特殊质数.写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数.数字1不被看作一个质数. 输