C中手动深层复制到设备

时间:2016-02-27 22:40:04

标签: c openacc pgi

我正在尝试并行化一个使用openACC进行图像处理的程序。作为此处理的一部分,我有一个定义类似于:

的自定义结构
typedef struct {
  RGB *image;
  double property;
} Deep;

我在数组Deep *structPointer中访问的内容。

我遇到过一些文档,用于手动将structPointer的全部内容复制到GPU,这给我留下了以下代码。

  Deep *structPointer = (Deep*)
    malloc(total_size*sizeof(Deep));
  assert(structPointer);

  int i;

  for (i = 0; i < total_size; i++)
  {
    structPointer[i].image = randomImage(width, height, max);
  }

    dP = acc_copyin( stuctPointer, sizeof( Deep )*total_size ); 

  for ( i=0; i < total_size; i++ ) {
   dA = acc_copyin( structPointer[i].image, sizeof(RGB)*width*height );     //device address in dA
   acc_memcpy_to_device( &dP[i].image, &dA,  sizeof(RGB*) );
  }

这一切都运行正常,直到我尝试运行访问structPointer的并行for循环,并根据property的内容修改数组成员的RGB *image属性。

伪代码:

#pragma acc parallel loop copyin(inputImage[0:width*height], width, height)
for (i = 0; i < total_size; i++) {
  computeProperty(input_image, structPointer+i, width, height)
}

inline void compProperty (const RGB *A, Deep *B, int width, int height)
{
   B->property = 10;
}

我明白了:

  

调用cuStreamSynchronize返回错误700:非法地址期间   内核执行

cuda-memcheck的输出是:

> ========= CUDA-MEMCHECK image2.ppm is a PPM file 256 x 256 image, max value= 255
> ========= Program hit CUDA_ERROR_INVALID_CONTEXT (error 201) due to "invalid device context" on CUDA API call to cuCtxAttach.
> =========     Saved host backtrace up to driver entry point at error
> =========     Host Frame:/usr/lib64/libcuda.so (cuCtxAttach + 0x156) [0x13fc36]
> =========     Host Frame:./genimg_acc [0x13639]
> =========
> ========= Program hit CUDA_ERROR_ILLEGAL_ADDRESS (error 700) due to "an illegal memory access was encountered" on CUDA API call to
> cuStreamSynchronize. call to cuStreamSynchronize returned error 700:
> Illegal address during kernel execution
> =========     Saved host backtrace up to driver entry point at error
> =========     Host Frame:/usr/lib64/libcuda.so (cuStreamSynchronize + 0x13d) [0x149a9d]
> =========     Host Frame:./genimg_acc [0x15856]
> =========
> ========= Program hit CUDA_ERROR_ILLEGAL_ADDRESS (error 700) due to "an illegal memory access was encountered" on CUDA API call to
> cuCtxSynchronize.
> =========     Saved host backtrace up to driver entry point at error
> =========     Host Frame:/usr/lib64/libcuda.so (cuCtxSynchronize + 0x127) [0x13ee37]

请注意,程序在没有openACC的情况下编译时运行,并且在单个线程中运行时将正确处理。

1 个答案:

答案 0 :(得分:1)

好的,我找到了OpenACC Deep Copying的引用,这可能是您已根据Deep名称查看的内容。查看第7页的图9 ,它们给出了一个在包含标量和指针的结构上进行深层复制的示例。

必须使用acc_copyin返回的指针来访问并行化代码中的结构数组 - 即dP而不是structPointer。以下代码应该可以解决问题。

#pragma acc parallel loop copyin(inputImage[0:width*height], width, height)
for (i = 0; i < total_size; i++) {
  computeProperty(input_image, dP+i, width, height)
}