分配二维数组在CUDA设备内存 [英] Allocate 2D Array on Device Memory in CUDA

查看:577
本文介绍了分配二维数组在CUDA设备内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何分配和转移(和从主机)的二维阵列设备内存中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;
}

这篇关于分配二维数组在CUDA设备内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆