我正在 cuda 中做一个项目。我在设备内存中分配了指针,将主机指针复制到了设备指针并调用了全局函数。我正在尝试将设备指针复制回主机指针并显示结果。
但似乎 cudaMemcpy() 不起作用,因为我打算复制到主机指针的设备指针据说没有被复制。在实际代码中,我试图打印在全局函数中分配的数组的索引和内容。我花了一整天的时间试图修复它,但我仍然无法。请在这里帮助我。任何形式的帮助将不胜感激。
#include<cuda.h>
#include<cuda_runtime.h>
#include<iostream>
#include<device_launch_parameters.h>
#define number 4
#define PI 3.141592654
using namespace std;
struct vertex
{
float x,y,z,h;
vertex():x(0),y(0),z(0),h(1){};
vertex(float x, float y, float z, float h=0):x(x),y(y),z(z),h(h){};
};
struct triangle
{
vertex v1,v2,v3;
triangle(){
}
};
__global__ void check(float mat[4][4],vertex *a,float *re,int *index)
{
int idx = blockIdx.x+ threadIdx.x;
re[idx] = mat[idx][0];
re[idx] = mat[idx][1];
re[idx] = mat[idx][2];
re[idx] = mat[idx][3];
}
int main()
{
triangle t1;
t1.v1.x = 2;
t1.v1.y = 1.33512;
t1.v1.z = 5.849567;
t1.v2.x = 2;
t1.v2.y = -1.33512;
t1.v2.z = 5.849567;
t1.v3.x = 2;
t1.v3.y = 0;
t1.v3.z = 5;
int index_h[4]={0,0,0,0};
int index_d[4];
vertex *check_d;
vertex *check_h;
float check_re_d[4];
float check_re_h[4]={0,0,0,0};
float translation_check_d[4][4];
float translation_check_h[number][number] = {{1, 0, 0, -t1.v1.x},
{0, 1, 0, -t1.v1.y},
{0, 0, 1, -t1.v1.z},
{0 ,0 ,0, 1}};
check_h = new vertex(1,-4,3);
cudaMalloc((void**)&index_d,4*sizeof(int));
cudaMalloc((void**)&check_d, sizeof(vertex));
cudaMalloc((void**)&check_re_d, 4*sizeof(float));
cudaMemcpy(index_d,index_h,4*sizeof(int),cudaMemcpyHostToDevice);
cudaMemcpy(check_d,check_h,sizeof(vertex),cudaMemcpyHostToDevice);
cudaMemcpy(check_re_d,check_re_h,4*sizeof(float),cudaMemcpyHostToDevice);
size_t dPitch;
cudaMallocPitch((void**)&translation_check_d,&dPitch,4*sizeof(float),4);
cudaMemcpy2D(translation_check_d,4*sizeof(float),translation_check_h,4*sizeof(float),4*sizeof(float),4,cudaMemcpyHostToDevice);
check<<<4,1>>>(translation_check_d,check_d,check_re_d,index_d);
cudaMemcpy(check_re_h,check_re_d,4*sizeof(float),cudaMemcpyDeviceToHost);
cudaMemcpy(index_h,index_d,4*sizeof(int),cudaMemcpyDeviceToHost);
std::cout<<"These are the value"<<"INDEX: "<<index_h[0]<<" x: "<<check_re_h[0]<<"\n";
std::cout<<"These are the value"<<"INDEX: "<<index_h[1]<<" x: "<<check_re_h[1]<<"\n";
std::cout<<"These are the value"<<"INDEX: "<<index_h[2]<<" x: "<<check_re_h[2]<<"\n";
std::cout<<"These are the value"<<"INDEX: "<<index_h[2]<<" x: "<<check_re_h[3]<<"\n";
cudaFree(check_d);
cudaFree(check_re_d);
cudaFree(index_d);
int a;
cin>>a;
return 0;
}