/*
* 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
};