二维显存的使用

来源:互联网 发布:linux 启动oracle实例 编辑:程序博客网 时间:2024/05/17 09:28

维数组动态申请内存空间,及其与显存数据相互拷贝的两种方式如下:

[cpp] view plain copy
 print?
  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. using namespace std;  
  11.   
  12. int main(int argc, char **argv)  
  13. {  
  14.     CUT_DEVICE_INIT(argc, argv);  // 启动 CUDA  
  15. #if 1  
  16.     // 方法1.逐行拷贝  
  17.     float **CPU_ORIGN, **CPU_RET;  // host端原数据、拷贝回数据  
  18.     float **GPU;                   // device端数据  
  19.     int width = 5, height = 3;  // 数组的宽度和高度  
  20.     size_t size = sizeof(float)*width; // 数据的宽度in bytes  
  21.     int row, col;  
  22.   
  23.     // 申请内存空间, 并初始化  
  24.     CPU_ORIGN = new float*[height];  
  25.     CPU_RET = new float*[height];  
  26.     for(row = 0; row < height; ++row)  
  27.     {  
  28.         CPU_ORIGN[row] = new float[width];  
  29.         CPU_RET[row] = new float[width];  
  30.         // 初始化数据  
  31.         for(col = 0; col < width; ++col)  
  32.         {  
  33.             CPU_ORIGN[row][col] = (float)(row + col);  
  34.             CPU_RET[row][col] = 0.0f;  
  35.         }  
  36.     }  
  37.   
  38.     // 申请显存空间并初始化  
  39.     GPU = new float*[height];  
  40.     for(row = 0; row < height; ++row)  
  41.     {  
  42.         cutilSafeCall( cudaMalloc((void**)&GPU[row], size));  
  43.         cutilSafeCall( cudaMemset(GPU[row], 0, size));  
  44.     }  
  45.   
  46.     // 将host端原数据拷贝到device端  
  47.     for(row = 0; row < height; ++row)  
  48.         cutilSafeCall(cudaMemcpy(GPU[row], CPU_ORIGN[row], size, cudaMemcpyHostToDevice));  
  49.       
  50.     // 将device端数据拷贝到host端返回数据  
  51.     for(row = 0; row < height; ++row)  
  52.         cutilSafeCall(cudaMemcpy(CPU_RET[row], GPU[row], size, cudaMemcpyDeviceToHost));  
  53.   
  54.     // 打印host端返回数据  
  55.     for(row = 0; row < height; ++row)  
  56.     {  
  57.         for(col = 0; col < width; ++col)  
  58.             cout << CPU_RET[row][col] << " ";  
  59.         cout << endl;  
  60.     }  
  61.     // 释放内存和显存空间  
  62.     free(CPU_ORIGN);  
  63.     free(CPU_RET);  
  64.     for(row = 0; row < height; ++row)  
  65.         cutilSafeCall(cudaFree(GPU[row]));  
  66. #endif  
  67. #if 0  
  68.     // 方法2.整体拷贝  
  69.     float **CPU_ORIGN, **CPU_RET;  // host端原数据、拷贝回数据  
  70.     float **GPU;                   // device端数据  
  71.     int width = 5, height = 3;         // 数组的宽度和高度  
  72.     size_t size = sizeof(float)*width; // 数据的宽度in bytes  
  73.     size_t pitch;  
  74.     int row, col;  
  75.   
  76.     // 申请内存空间, 并初始化  
  77.     CPU_ORIGN = new float*[height];  
  78.     CPU_RET = new float*[height];  
  79.     for(row = 0; row < height; ++row)  
  80.     {  
  81.         CPU_ORIGN[row] = new float[width];  
  82.         CPU_RET[row] = new float[width];  
  83.         // 初始化数据  
  84.         for(col = 0; col < width; ++col)  
  85.         {  
  86.             CPU_ORIGN[row][col] = (float)(row + col);  
  87.             CPU_RET[row][col] = 0.0f;  
  88.         }  
  89.     }  
  90.   
  91.     // 申请显存空间并初始化  
  92.     cutilSafeCall(cudaMallocPitch((void**)&GPU, &pitch, size, height));  
  93.     cutilSafeCall(cudaMemset2D(GPU, pitch, 0, size, height));  
  94.   
  95.     // 将host端原数据拷贝到device端  
  96.     cutilSafeCall(cudaMemcpy2D(GPU, pitch, CPU_ORIGN, size, size, height, cudaMemcpyHostToDevice));  
  97.   
  98.     // 将device端数据拷贝到host端返回数据  
  99.     cutilSafeCall(cudaMemcpy2D(CPU_RET, size, GPU, pitch, size, height, cudaMemcpyDeviceToHost));  
  100.   
  101.     // 打印host端返回数据  
  102.     for(row = 0; row < height; ++row)  
  103.     {  
  104.         for(col = 0; col < width; ++col)  
  105.             cout << CPU_RET[row][col] << " ";  
  106.         cout << endl;  
  107.     }  
  108.     // 释放内存和显存空间  
  109.     free(CPU_ORIGN);  
  110.     free(CPU_RET);  
  111.     cutilSafeCall(cudaFree(GPU));  
  112. #endif  
  113.     CUT_EXIT(argc, argv);  // 退出CUDA  
  114. }