首页 > 代码库 > 拷贝时间测试=cudamelloc+cudahostalloc

拷贝时间测试=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 cudaMalloc107     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 cudaHostAlloc120     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 }

 项目打包下载

拷贝时间测试=cudamelloc+cudahostalloc