CUDA+Vector測試程序:
- /*
- * Copyright 徐洪志(西北農林科技大學.信息工程學院). All rights reserved.
- * Data: 2012-4-15
- */
- //
- // 此程序是演示了vector型數據如何拷貝入、出顯存
- #include <cutil_inline.h>
-
- #include <iostream>
- #include <vector>
- using namespace std;
-
-
- ///////////////////////////////////////////////////////////////////////////////////////////
- //
- // MAIN
- //
- ///////////////////////////////////////////////////////////////////////////////////////////
- int main(int argc, char** argv)
- {
- CUT_DEVICE_INIT(argc, argv); // 啟動CUDA
- int row, col;
- /// Vector-->Device-->Host 1D
- cout << "Vector-->Device-->Host 1D" << endl;
- vector<int> vec; // Host端vector
- int *gpu_data; // Device端data
- int *cpu_data; // Host端data
- int dataWd = 20;
- cpu_data = (int*)calloc(dataWd, sizeof(int)); // 申請內存空間
- cutilSafeCall( cudaMalloc((void**) &gpu_data, sizeof(int) * dataWd)); // 申請顯存空間
- cutilSafeCall( cudaMemset(gpu_data, 0, sizeof(float) * dataWd));
- if((cpu_data == NULL)||(gpu_data == NULL)) // 判斷空間是否申請成功
- {
- cout << "Alloc Memery Error" << endl;
- return -1;
- }
- for(row = 0; row < dataWd; ++row) // 給Host端的vector初始化
- vec.push_back(row);
- cutilSafeCall( cudaMemcpy(gpu_data, &vec[0] , sizeof(int) * dataWd, cudaMemcpyHostToDevice)); // 將Host端vector拷貝入Device端data
- cutilSafeCall( cudaMemcpy(cpu_data, gpu_data, sizeof(int) * dataWd, cudaMemcpyDeviceToHost)); // 將Device端data拷貝入Host端data
-
- for(row = 0; row < dataWd; ++row) // 打印Host端data
- cout << cpu_data[row] << " ";
- cout << endl;
-
- cutilSafeCall( cudaFree(gpu_data)); // 釋放顯存空間
- free(cpu_data); // 釋放內存空間
-
- /// vector-->Device-->Host 2D
- cout << "Vector-->Device-->Host 2D" << endl;
- vector< vector<int> > vec2D; // Host端vector
- int *cpu_data2D; // Host端data
- int *gpu_data2D; // Device端data
- size_t pitch; // 字節對齊
- int Wd = 10; // 寬度
- int Ht = 5; // 高度
-
- cutilSafeCall( cudaMallocPitch((void**) &gpu_data2D, &pitch, sizeof(int) * Wd, Ht)); // 申請顯存空間
- cutilSafeCall( cudaMemset2D(gpu_data2D, pitch, 0, sizeof(int)*Wd, Ht)); // 顯存空間初始化
- cpu_data2D = (int*)calloc(Wd * Ht, sizeof(int)); // 申請內存空間
- if((cpu_data2D == NULL)||(gpu_data2D == NULL)) // 判斷空間是否申請成功
- {
- cout << "Alloc Memery Error" << endl;
- return -1;
- }
- for(row = 0; row < Ht; ++row) // 初始化Vector
- {
- vector<int> temp;
- for(col = 0; col < Wd; ++col)
- {
- temp.push_back(row+col);
- }
- vec2D.push_back(temp);
- temp.clear();
- }
- cout << "Vetor2D" << endl;
- for(row = 0; row < Ht; ++row)
- {
- for(col = 0; col < Wd; ++col)
- cout << vec2D[row][col] << " ";
- cout << endl;
- }
- // 將vector中的數據拷貝到Device端data
- for(row = 0; row < Ht; ++row)
- {
- cutilSafeCall( cudaMemcpy(&gpu_data2D[row*(pitch/sizeof(int))], &vec2D[row][0], sizeof(int)*Wd, cudaMemcpyHostToDevice));
- }
- // 將Device端data拷貝到Host端data
- cutilSafeCall( cudaMemcpy2D( cpu_data2D, sizeof(int) * Wd, gpu_data2D, pitch, sizeof(int) * Wd, Ht, cudaMemcpyDeviceToHost));
- cout << "cpu_data2D" << endl; // 打印Host端data
- for(row = 0; row < Ht; ++row)
- {
- for(col = 0; col < Wd; ++col)
- {
- cout << cpu_data2D[row*Wd + col] << " ";
- }
- cout << endl;
- }
- cutilSafeCall( cudaFree(gpu_data2D)); // 釋放顯存空間
- free(cpu_data2D); // 釋放內存空間
- CUT_EXIT(argc, argv); // 退出CUDA
- };
相關閱讀:
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