分配二维数组在CUDA设备内存数组、分配、内存、设备

2023-09-07 12:12:42 作者:浮夸的拾荒者

我如何分配和转移(和从主机)的二维阵列设备内存中Cuda的?

How do I allocate and transfer(to and from Host) 2D arrays in device memory in Cuda?

推荐答案

我找到了一个解决这个问题。我没有扁平化阵列。

I found a solution to this problem. I didn't have to flatten the array.

内置的使用cud​​aMallocPitch()函数做的工作。我可以在阵列转移到从设备使用 cudaMemcpy2D()的功能。

The inbuilt cudaMallocPitch() function did the job. And I could transfer the array to and from device using cudaMemcpy2D() function.

例如

cudaMallocPitch((void**) &array, &pitch, a*sizeof(float), b);

此产生大小的2D阵列A * B与音调作为传递作为参数

This creates a 2D array of size a*b with the pitch as passed in as parameter.

下面code创建一个二维数组并遍历元素。它编译容易,你可以用它。

The following code creates a 2D array and loops over the elements. It compiles readily, you may use it.

#include<stdio.h>
#include<cuda.h>
#define height 50
#define width 50

// Device code
__global__ void kernel(float* devPtr, int pitch)
{
    for (int r = 0; r < height; ++r) {
        float* row = (float*)((char*)devPtr + r * pitch);
        for (int c = 0; c < width; ++c) {
             float element = row[c];
        }
    }
}

//Host Code
int main()
{

float* devPtr;
size_t pitch;
cudaMallocPitch((void**)&devPtr, &pitch, width * sizeof(float), height);
kernel<<<100, 512>>>(devPtr, pitch);
return 0;
}
 
精彩推荐
图片推荐