带有 CUDA 和 cudaMallocPitch 的二维数组 [英] 2D array with CUDA and cudaMallocPitch

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

问题描述

我一直在阅读有关 2D 数组和 cudaMallocPitch 的 stackoverflow 上的一些线程,并且我尝试将 cudaMallocPitch 与我找到的小文档一起使用.但是我现在面临一个问题.

I have been reading a few threads on stackoverflow about 2D arrays and cudaMallocPitch and I have tried to use cudaMallocPitch with the small documentation I have found. However I'm now facing a problem.

我需要遍历一个数组并做类似的事情:

I need to go through an array and do something similar :

 for(int k=0; k<100; ++k){
     for(i=SID; i<SID+stride; ++i){
        while(-1 < j && Driver[k][j] != Road[i]){
            j = Pilot[j][k];

        }
        ++j;
     }
  }

因此我想知道,我应该如何调整这段代码以使其与音高一起工作,因为我已经读到我必须更新指向行开头的指针.当然,我的内核收到以下内容:

I was thus wondering, how should I adapt this code to make it work with the pitch, because I have read that I had to update the pointer to the beginning of the row. Of course my kernel receives the following :

__global__ void driving(char *Driver, size_t pitch_driver, 
                        char *Road, int *Pilot, size_t pitch_pilot) 

而且我不确定如何使事情发挥作用,我一直在阅读和尝试,但目前似乎不起作用.

And I'm not really sure how to make things working, I've been reading and trying, but it seems not working at the moment.

谢谢.

编辑 1: 我一直在特别阅读此线程:如何在 CUDA 中使用 2D 数组? 遇到了以下问题:

Edit 1: I have been reading this thread in particular :How to use 2D Arrays in CUDA? and came across the lines :

for (int row = 0; row < rowCount; row++)  
 {  
     // update the pointer to point to the beginning of the next row  
    float* rowData = (float*)(((char*)d_array) + (row * pitch));  
    for (int column = 0; column < columnCount; column++)  
     {  
       rowData[column] = 123.0; // make every value in the array 123.0  
       destinationArray[(row*columnCount) + column] = rowData[column];  
      }  
 }  

正在更新下一行的指针,我不确定如何使用我的 2 for 循环以及在工作时(例如在前面的代码中).

Which is updating the pointer of the next row, I'am not sure how to use to make my 2 for loops and while working such as in the previous code.

目前我只能访问数组的一维,而不能访问另一维.

At the moment I can only access one dimension of my array but not the other one.

它返回值 2,但是当我尝试多次比较时,它只返回 0,甚至无法比较两个值.

it returns the value 2, but when I try my multiple comparisons, it only returns 0, or even comparing two values do not work.

推荐答案

在 CUDA 参考手册中说:

In the CUDA Reference Manual it says:

5.8.2.17 cudaError_t cudaMallocPitch (void devPtr, size_t pitch, size_t width, size_t height)

5.8.2.17 cudaError_t cudaMallocPitch (void devPtr, size_t pitch, size_t width, size_t height)

[...]

给定的行和列类型为 T 的数组元素,地址计算如下:

Given the row and column of an array element of type T, the address is computed as:

T* pElement = (T*)((char*)BaseAddress + Row * pitch) + Column;

T* pElement = (T*)((char*)BaseAddress + Row * pitch) + Column;

因此您需要先将指针转换为 char*,进行数学运算,然后将其转换回您的类型.

So you need to cast your pointer first to char*, do the math and then cast it back to your type.

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

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