1 /* 2 * Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved. 3 * Data: 2012-4-22 4 */ 5 // 6 // 此程序是演示了二维动态数组空间申请和与显存数据相互拷贝的两种方法 7 #include <stdio.h> 8 //#include <cutil_inline.h> 9 #include <iostream> 10 #include <cuda_runtime.h> 11 //#include <cutil.h> 12 using namespace std; 13 14 int main(int argc, char **argv) 15 { 16 17 //CUT_DEVICE_INIT(argc, argv); // 启动 CUDA 18 #if 1 19 // 方法1.逐行拷贝 20 float **CPU_ORIGN, **CPU_RET; // host端原数据、拷贝回数据 21 float **GPU; // device端数据 22 int width = 5, height = 3; // 数组的宽度和高度 23 size_t size = sizeof(float)*width; // 数据的宽度in bytes 24 int row, col; 25 26 // 申请内存空间, 并初始化 27 CPU_ORIGN = new float*[height]; 28 CPU_RET = new float*[height]; 29 for(row = 0; row < height; ++row) 30 { 31 CPU_ORIGN[row] = new float[width]; 32 CPU_RET[row] = new float[width]; 33 // 初始化数据 34 for(col = 0; col < width; ++col) 35 { 36 CPU_ORIGN[row][col] = (float)(row + col); 37 CPU_RET[row][col] = 0.0f; 38 } 39 } 40 41 // 申请显存空间并初始化 42 GPU = new float*[height]; 43 for(row = 0; row < height; ++row) 44 { 45 ( cudaMalloc((void**)&GPU[row], size)); 46 ( cudaMemset(GPU[row], 0, size)); 47 } 48 49 // 将host端原数据拷贝到device端 50 for(row = 0; row < height; ++row) 51 (cudaMemcpy(GPU[row], CPU_ORIGN[row], size, cudaMemcpyHostToDevice)); 52 53 // 将device端数据拷贝到host端返回数据 54 for(row = 0; row < height; ++row) 55 (cudaMemcpy(CPU_RET[row], GPU[row], size, cudaMemcpyDeviceToHost)); 56 57 // 打印host端返回数据 58 for(row = 0; row < height; ++row) 59 { 60 for(col = 0; col < width; ++col) 61 cout << CPU_RET[row][col] << " "; 62 cout << endl; 63 } 64 // 释放内存和显存空间 65 free(CPU_ORIGN); 66 free(CPU_RET); 67 for(row = 0; row < height; ++row) 68 (cudaFree(GPU[row])); 69 #endif 70 71 72 #if 0 73 // 方法2.整体拷贝 74 float **CPU_ORIGN, **CPU_RET; // host端原数据、拷贝回数据 75 float **GPU; // device端数据 76 int width = 5, height = 3; // 数组的宽度和高度 77 size_t size = sizeof(float)*width; // 数据的宽度in bytes 78 size_t pitch; 79 int row, col; 80 81 // 申请内存空间, 并初始化 82 CPU_ORIGN = new float*[height]; 83 CPU_RET = new float*[height]; 84 for(row = 0; row < height; ++row) 85 { 86 CPU_ORIGN[row] = new float[width]; 87 CPU_RET[row] = new float[width]; 88 // 初始化数据 89 for(col = 0; col < width; ++col) 90 { 91 CPU_ORIGN[row][col] = (float)(row + col); 92 CPU_RET[row][col] = 0.0f; 93 } 94 } 95 96 // 申请显存空间并初始化 97 (cudaMallocPitch((void**)&GPU, &pitch, size, height)); 98 (cudaMemset2D(GPU, pitch, 0, size, height)); 99 100 // 将host端原数据拷贝到device端 101 (cudaMemcpy2D(GPU, pitch, CPU_ORIGN, size, size, height, cudaMemcpyHostToDevice)); 102 103 // 将device端数据拷贝到host端返回数据 104 (cudaMemcpy2D(CPU_RET, size, GPU, pitch, size, height, cudaMemcpyDeviceToHost)); 105 106 // 打印host端返回数据 107 for(row = 0; row < height; ++row) 108 { 109 for(col = 0; col < width; ++col) 110 cout << CPU_RET[row][col] << " "; 111 cout << endl; 112 } 113 // 释放内存和显存空间 114 free(CPU_ORIGN); 115 free(CPU_RET); 116 (cudaFree(GPU)); 117 #endif 118 system("pause"); 119 //CUT_EXIT(argc, argv); // 退出CUDA 120 }
http://blog.csdn.net/xhz1234/article/details/7487767
时间: 2024-10-13 10:59:17