0

我正在尝试构建一个 cuda 程序来进行光线跟踪,我有一些代码如下:

void build_world(World *w, RGBAColor* buffer){  
w->vp = (ViewPlane*) malloc(sizeof(ViewPlane));

w->vp->hres = 512;
w->vp->vres = 512;
w->vp->buffer = buffer;
w->vp->s = 1;

ViewPlane *viewplane;
cudaMalloc(&viewplane,sizeof(ViewPlane)); //return cudaSuccess but pointer still NULL
cudaMemcpy(viewplane,w->vp,sizeof(ViewPlane),cudaMemcpyHostToDevice);
free(w->vp);
w->vp = viewplane;

cudaMalloc(&(w->background_color),sizeof(RGBAColor)); //return cudaSuccess but pointer still NULL
*(w->background_color) = black;  //Memory access error

cudaMalloc(&(w->sphere),sizeof(Sphere));  //return cudaSuccess but pointer still NULL
w->sphere->center = Point3D(0.0,0.0,0.0);
w->sphere->radius = 300;
}

World *w 是一个静态全局指针,它在全局内存中。我的问题是我无法在设备内存中分配内存,所有“cudaMalloc”调用在大多数情况下都不起作用。


我做了@RobertCrovella 在评论中建议的事情,如下所示:

void build_world(World *w, RGBAColor* buffer){

    checkCudaErrors( cudaMalloc(&(w->vp),sizeof(ViewPlane)));
    getLastCudaError("viewplane allocate failed");

    w->vp->hres = 512;  //memory access errors occurs here
    w->vp->vres = 512;
    w->vp->buffer = buffer;
    w->vp->s = 1;       

    checkCudaErrors( cudaMalloc(&(w->background_color),sizeof(RGBAColor)));
    getLastCudaError("background allocate failed");
    *(w->background_color) = black;

    checkCudaErrors( cudaMalloc(&(w->sphere),sizeof(Sphere)));
    getLastCudaError("sphere allocate failed");

    w->sphere->center = Point3D(0.0,0.0,0.0);
    w->sphere->radius = 300;
}

它工作一次...... cudaMalloc API 仍然返回“cudaSuccess”,而不是。

这是结构的定义:

typedef float3 Point3D;
typedef uchar4 RGBAColor;
struct Sphere{
    Point3D center;
    float radius;
};
struct ViewPlane{
public:
    int hres;
    int vres;
    float s;
    //float gamma;
    //float inv_gamma;

    RGBAColor *buffer;

};
struct World{
public:

    ViewPlane *vp;
    RGBAColor *background_color;
    Sphere *sphere;

};

在考虑了@RobertCrovella 在下面的答案中提到的问题之后,这里是 build_world 的第三个版本:

struct World{
public:

    ViewPlane *vp;
    RGBAColor background_color;
    Sphere *sphere;

};
void build_world(World *w, RGBAColor* buffer){  
    World *h_world;
    h_world = (World*)malloc(sizeof(World));

    ViewPlane *h_vp = (ViewPlane*)malloc(sizeof(ViewPlane));
    h_vp->hres = 512;
    h_vp->vres = 512;
    h_vp->buffer = buffer;
    h_vp->s = 1;        
    checkCudaErrors( cudaMalloc(&(h_world->vp),sizeof(ViewPlane)));
    getLastCudaError("viewplane allocate failed");
    checkCudaErrors( cudaMemcpy(h_world->vp,h_vp,sizeof(ViewPlane),cudaMemcpyHostToDevice));
    getLastCudaError("viewplane memory copy failed");

    h_world->background_color = black;

    Sphere *h_sphere = (Sphere*)malloc(sizeof(Sphere));
    h_sphere->center = Point3D(0.0,0.0,0.0);
    h_sphere->radius = 300;
    checkCudaErrors( cudaMalloc(&(h_world->sphere),sizeof(Sphere)));
    getLastCudaError("sphere allocate failed");
    checkCudaErrors( cudaMemcpy(h_world->sphere,h_sphere,sizeof(Sphere),cudaMemcpyHostToDevice));
    getLastCudaError("sphere memory copy failed");

    checkCudaErrors( cudaMalloc( &w , sizeof(World)));
    getLastCudaError( "world allocate failed" );
    checkCudaErrors( cudaMemcpy(w,h_world,sizeof(World),cudaMemcpyHostToDevice));
    getLastCudaError("world memory copy failed");

    free(h_world);free(h_vp);free(h_sphere);    
}

这一次,所有cudaMemcpy的调用都不起作用:运行到这个函数结束时,h_vpand的值h_sphere是好的;h_world->vp并且h_world->sphere确实指向设备内存区域但包含错误值;w没有正确的值,它包含的所有指针都是 0x00000000 ...

4

1 回答 1

0

这个问题已正式成为“一团糟”,因为您发布了两个截然不同的版本build_world,除了我要求您添加的错误检查之外,它们在重要方面有所不同。我会尝试解决我看到的一些问题,但是我的理解因您发布的混乱而蒙上阴影。

  • 如果*w您传递给build_world的指针已经是一个设备指针(即分配给cudaMalloc),这似乎是您所说的,那么这些都不起作用。在设备上创建数据结构,其中还包含指向也在设备上的其他数据结构的指针,这是一个有点不直观的过程。您不能将指针传递给已经存在于设备上的指针cudaMalloc(即已经是使用cudaMalloc.cudaMalloc在设备数据结构中,使用 cudaMemcpy。要查看我所指的另一个示例,请查看此处
  • 您不能取消引用主机代码中的设备指针。例如:

    w->vp->hres = 512;
    

    如果worw->vp是用 设置的指针cudaMalloc,则上述操作无效。相反,有必要在主机上创建一个并行数据结构,在那里设置值,然后cudaMemcpy从主机到设备:

    h_vp->hres = 512;
    cudaMemcpy(d_vp, h_vp, sizeof(vp_struct), cudaMemcpyHostToDevice);
    

    请注意,在这个简化的描述中,我忽略了我在上面第一点中提到的问题。

  • 如果您build_world一遍又一遍地调用,则需要确保cudaFree在传递相同*w指针的情况下正确使用。

编辑:作为对第三版的附加发布的回应,build_world我选择创建一个示例代码,该代码应该修复剩余的问题:

#include <stdio.h>
#include <vector_functions.h>

#define black make_uchar4(4,3,2,1)
#define white make_uchar4(0,1,2,3)

#define cudaCheckErrors(msg) \
    do { \
        cudaError_t __err = cudaGetLastError(); \
        if (__err != cudaSuccess) { \
            fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
                msg, cudaGetErrorString(__err), \
                __FILE__, __LINE__); \
            fprintf(stderr, "*** FAILED - ABORTING\n"); \
            exit(1); \
        } \
    } while (0)

typedef float3 Point3D;
typedef uchar4 RGBAColor;
struct Sphere{
    Point3D center;
    float radius;
};
struct ViewPlane{
public:
    int hres;
    int vres;
    float s;
    //float gamma;
    //float inv_gamma;

    RGBAColor *buffer;

};

struct World{
public:

    ViewPlane *vp;
    RGBAColor background_color;
    Sphere *sphere;

};

__global__ void my_kernel(World *w){

  printf("w->vp->hres = %d\n", w->vp->hres);
  printf("w->background_color.y = %d\n", w->background_color.y);
  printf("w->sphere->radius = %f\n", w->sphere->radius);
  printf("w->vp->buffer->y = %d\n", w->vp->buffer->y);

}


void build_world(World **w, RGBAColor* buffer){
    World *h_world;
    h_world = (World*)malloc(sizeof(World));

    ViewPlane *h_vp = (ViewPlane*)malloc(sizeof(ViewPlane));
    h_vp->hres = 512;
    h_vp->vres = 512;
    h_vp->s = 1;
    cudaMalloc((void **)&(h_vp->buffer), sizeof(RGBAColor));
    cudaCheckErrors("viewplane RGBAColor allocate failed");
    cudaMemcpy(h_vp->buffer, buffer, sizeof(RGBAColor), cudaMemcpyHostToDevice);
    cudaCheckErrors("viewplane RGBAColor copy failed");

    cudaMalloc((void **)&(h_world->vp),sizeof(ViewPlane));
    cudaCheckErrors("viewplane allocate failed");
    cudaMemcpy(h_world->vp,h_vp,sizeof(ViewPlane),cudaMemcpyHostToDevice);
    cudaCheckErrors("viewplane memory copy failed");

    h_world->background_color = black;

    Sphere *h_sphere = (Sphere*)malloc(sizeof(Sphere));
    h_sphere->center = (Point3D) make_float3(0.0,0.0,0.0);
    h_sphere->radius = 300;
    cudaMalloc((void **)&(h_world->sphere),sizeof(Sphere));
    cudaCheckErrors("sphere allocate failed");
    cudaMemcpy(h_world->sphere,h_sphere,sizeof(Sphere),cudaMemcpyHostToDevice);
    cudaCheckErrors("sphere memory copy failed");

    cudaMalloc((void **)w , sizeof(World));
    cudaCheckErrors( "world allocate failed" );
    cudaMemcpy(*w,h_world,sizeof(World),cudaMemcpyHostToDevice);
    cudaCheckErrors("world memory copy failed");

    free(h_world);free(h_vp);free(h_sphere);
}



int main(){

   World *d_w;
   RGBAColor my_buffer = white;

   build_world(&d_w, &my_buffer);
   my_kernel<<<1,1>>>(d_w);
   cudaDeviceSynchronize();
   cudaCheckErrors("kernel fail");
   return 0;
}

您可以使用以下代码编译此代码nvcc -arch=sm_20 -o t98 t98.cu

当我编译并运行这段代码时,我没有得到任何错误和以下输出:

$ ./t98
w->vp->hres = 512
w->background_color.y = 3
w->sphere->radius = 300.000000
w->vp->buffer->y = 1
$ 
于 2013-03-23T15:31:41.040 回答