3

我正在尝试并行化一个使用 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*) );
  }

这一切都运行良好,直到我尝试运行一个并行 for 循环,该structPointer循环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 的情况下编译时运行,并且在单线程中运行时将正确处理。

4

1 回答 1

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)
}
于 2016-02-27T23:05:38.617 回答