歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> CUDA+Vector測試程序

CUDA+Vector測試程序

日期:2017/3/1 10:24:18   编辑:Linux編程

CUDA+Vector測試程序:

  1. /*
  2. * Copyright 徐洪志(西北農林科技大學.信息工程學院). All rights reserved.
  3. * Data: 2012-4-15
  4. */
  5. //
  6. // 此程序是演示了vector型數據如何拷貝入、出顯存
  7. #include <cutil_inline.h>
  8. #include <iostream>
  9. #include <vector>
  10. using namespace std;
  11. ///////////////////////////////////////////////////////////////////////////////////////////
  12. //
  13. // MAIN
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////////////////
  16. int main(int argc, char** argv)
  17. {
  18. CUT_DEVICE_INIT(argc, argv); // 啟動CUDA
  19. int row, col;
  20. /// Vector-->Device-->Host 1D
  21. cout << "Vector-->Device-->Host 1D" << endl;
  22. vector<int> vec; // Host端vector
  23. int *gpu_data; // Device端data
  24. int *cpu_data; // Host端data
  25. int dataWd = 20;
  26. cpu_data = (int*)calloc(dataWd, sizeof(int)); // 申請內存空間
  27. cutilSafeCall( cudaMalloc((void**) &gpu_data, sizeof(int) * dataWd)); // 申請顯存空間
  28. cutilSafeCall( cudaMemset(gpu_data, 0, sizeof(float) * dataWd));
  29. if((cpu_data == NULL)||(gpu_data == NULL)) // 判斷空間是否申請成功
  30. {
  31. cout << "Alloc Memery Error" << endl;
  32. return -1;
  33. }
  34. for(row = 0; row < dataWd; ++row) // 給Host端的vector初始化
  35. vec.push_back(row);
  36. cutilSafeCall( cudaMemcpy(gpu_data, &vec[0] , sizeof(int) * dataWd, cudaMemcpyHostToDevice)); // 將Host端vector拷貝入Device端data
  37. cutilSafeCall( cudaMemcpy(cpu_data, gpu_data, sizeof(int) * dataWd, cudaMemcpyDeviceToHost)); // 將Device端data拷貝入Host端data
  38. for(row = 0; row < dataWd; ++row) // 打印Host端data
  39. cout << cpu_data[row] << " ";
  40. cout << endl;
  41. cutilSafeCall( cudaFree(gpu_data)); // 釋放顯存空間
  42. free(cpu_data); // 釋放內存空間
  43. /// vector-->Device-->Host 2D
  44. cout << "Vector-->Device-->Host 2D" << endl;
  45. vector< vector<int> > vec2D; // Host端vector
  46. int *cpu_data2D; // Host端data
  47. int *gpu_data2D; // Device端data
  48. size_t pitch; // 字節對齊
  49. int Wd = 10; // 寬度
  50. int Ht = 5; // 高度
  51. cutilSafeCall( cudaMallocPitch((void**) &gpu_data2D, &pitch, sizeof(int) * Wd, Ht)); // 申請顯存空間
  52. cutilSafeCall( cudaMemset2D(gpu_data2D, pitch, 0, sizeof(int)*Wd, Ht)); // 顯存空間初始化
  53. cpu_data2D = (int*)calloc(Wd * Ht, sizeof(int)); // 申請內存空間
  54. if((cpu_data2D == NULL)||(gpu_data2D == NULL)) // 判斷空間是否申請成功
  55. {
  56. cout << "Alloc Memery Error" << endl;
  57. return -1;
  58. }
  59. for(row = 0; row < Ht; ++row) // 初始化Vector
  60. {
  61. vector<int> temp;
  62. for(col = 0; col < Wd; ++col)
  63. {
  64. temp.push_back(row+col);
  65. }
  66. vec2D.push_back(temp);
  67. temp.clear();
  68. }
  69. cout << "Vetor2D" << endl;
  70. for(row = 0; row < Ht; ++row)
  71. {
  72. for(col = 0; col < Wd; ++col)
  73. cout << vec2D[row][col] << " ";
  74. cout << endl;
  75. }
  76. // 將vector中的數據拷貝到Device端data
  77. for(row = 0; row < Ht; ++row)
  78. {
  79. cutilSafeCall( cudaMemcpy(&gpu_data2D[row*(pitch/sizeof(int))], &vec2D[row][0], sizeof(int)*Wd, cudaMemcpyHostToDevice));
  80. }
  81. // 將Device端data拷貝到Host端data
  82. cutilSafeCall( cudaMemcpy2D( cpu_data2D, sizeof(int) * Wd, gpu_data2D, pitch, sizeof(int) * Wd, Ht, cudaMemcpyDeviceToHost));
  83. cout << "cpu_data2D" << endl; // 打印Host端data
  84. for(row = 0; row < Ht; ++row)
  85. {
  86. for(col = 0; col < Wd; ++col)
  87. {
  88. cout << cpu_data2D[row*Wd + col] << " ";
  89. }
  90. cout << endl;
  91. }
  92. cutilSafeCall( cudaFree(gpu_data2D)); // 釋放顯存空間
  93. free(cpu_data2D); // 釋放內存空間
  94. CUT_EXIT(argc, argv); // 退出CUDA
  95. };

相關閱讀:

Ubuntu 11.10 上安裝CUDA開發環境 http://www.linuxidc.com/Linux/2012-04/58913.htm

Ubuntu 11.04 安裝 NVIDIA CUDA 4.0 RC2 http://www.linuxidc.com/Linux/2011-10/46304.htm

Copyright © Linux教程網 All Rights Reserved