大学のプロジェクトでは、Windows7x64でvs2010を使用しています。CUDAツールキットv4.0を使用しています。簡単なgpu-vs-cpuテストを実行したいのですが、ほとんどのテストは実行されていますが、cudaテストで結果が返されることはありません。デバッガーでメモリを確認しましたが、デバイスのメモリには必要なものがすべて含まれており、メモリのコピーだけが失敗しました。
host_vector<int> addWithCuda(host_vector<int> h_a, host_vector<int> h_b)
{
int size = h_a.size();
host_vector<int> h_c(size);
// Choose which GPU to run on, change this on a multi-GPU system.
cudaError_t cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
return h_c;
}
else{
// Allocate GPU buffers for three vectors (two input, one output).
// Copy input vectors from host memory to GPU buffers.
device_vector<int> d_c=h_c;
device_vector<int> d_a=h_a;
device_vector<int> d_b=h_b;
int*d_a_ptr = raw_pointer_cast(&d_a[0]);
int*d_b_ptr = raw_pointer_cast(&d_b[0]);
int*d_c_ptr = raw_pointer_cast(&d_c[0]);
int*h_c_ptr = raw_pointer_cast(&h_c[0]);
// Launch a kernel on the GPU with one thread for each element.
addKernel<<<1, size>>>(d_c_ptr, d_a_ptr, d_b_ptr);
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
return h_c;
}
thrust::device_vector<int>::iterator d_it;
thrust::host_vector<int>::iterator h_it;
// Copy output vector from GPU buffer to host memory.
h_c=d_c;
printf("||Debug h_c[0]=%d\td_c[0]=%d\n",h_c[0],d_c[0]);
}
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
}
return h_c;
}
コード行「h_c=d_c;」に注意してください。推力では、これはデータをd_c(デバイスベクトル)からh_c(ホストベクトル)にコピーすることになっています。この行は失敗しませんが、正しく実行されません。h_cはずっと0のままです。
私は次のような他のいくつかの方法を試しました
thrust::copy(d_c.begin(),d_c.end(),h_c.begin());
また
cudaMemcpy(h_c_ptr,d_c_ptr,size*sizeof(int),cudaMemcpyDeviceToHost);
あるいは
for(int i=0;i < size;++i)h_c[i]=d_c[i];
何も機能しませんでした。私はここで迷子になっています。
誰か似たようなものがありましたか?すべての助けが高く評価されました。