2

我的 C 程序中有一个动态声明的二维数组,我想将其内容传输到 CUDA 内核以进行进一步处理。处理后,我想在我的 C 代码中使用 CUDA 处理的数据填充动态声明的 2D 数组。我可以使用静态 2D C 数组来做到这一点,但不能使用动态声明的 C 数组。欢迎任何意见!

我的意思是动态数组的动态数组。我写的测试代码如下。

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

const int nItt = 10;
const int nP = 5;

__device__ int d_nItt = 10;
__device__ int d_nP = 5;


__global__ void arr_chk(float *d_x_k, float *d_w_k, int row_num)
{

int index = (blockIdx.x * blockDim.x) + threadIdx.x;
int index1 = (row_num * d_nP) + index; 
if ( (index1 >= row_num * d_nP) && (index1 < ((row_num +1)*d_nP)))              //Modifying only one row data pertaining to one particular iteration
{

        d_x_k[index1] = row_num * d_nP;
        d_w_k[index1] = index;
}

}

float **mat_create2(int r, int c)
{
float **dynamicArray;
dynamicArray = (float **) malloc (sizeof (float)*r);
for(int i=0; i<r; i++)
    {
    dynamicArray[i] = (float *) malloc (sizeof (float)*c);
        for(int j= 0; j<c;j++)
        {
            dynamicArray[i][j] = 0;
        }
    }
return dynamicArray;
}

/* Freeing memory - here only number of rows are passed*/
void cleanup2d(float **mat_arr, int x)
{
int i;
for(i=0; i<x; i++)
{
    free(mat_arr[i]);
}
free(mat_arr);
}

int main()
{

//float w_k[nItt][nP]; //Static array declaration - works!
//float x_k[nItt][nP];
// if I uncomment this dynamic declaration and comment the static one, it does not work.....
float **w_k = mat_create2(nItt,nP); 
float **x_k = mat_create2(nItt,nP);
float *d_w_k, *d_x_k;       // Device variables for w_k and x_k
int nblocks, blocksize, nthreads;
for(int i=0;i<nItt;i++)
{
    for(int j=0;j<nP;j++)
    {
        x_k[i][j] = (nP*i);
        w_k[i][j] = j;
    }
}

for(int i=0;i<nItt;i++)
{
    for(int j=0;j<nP;j++)
    {
        printf("x_k[%d][%d] = %f\t",i,j,x_k[i][j]);
        printf("w_k[%d][%d] = %f\n",i,j,w_k[i][j]);
    }
}
int size1 = nItt * nP * sizeof(float);
printf("\nThe array size in memory bytes is: %d\n",size1);
cudaMalloc( (void**)&d_x_k, size1 );
cudaMalloc( (void**)&d_w_k, size1 );

if((nP*nItt)<32)    
{
    blocksize = nP*nItt;
    nblocks = 1;
}
else
{
    blocksize = 32;     // Defines the number of threads running per block. Taken equal to warp size
    nthreads = blocksize;
    nblocks =  ceil(float(nP*nItt) / nthreads);     // Calculated total number of blocks thus required
}

for(int i = 0; i< nItt; i++)
{
    cudaMemcpy( d_x_k, x_k, size1,cudaMemcpyHostToDevice ); //copy of x_k to device
    cudaMemcpy( d_w_k, w_k, size1,cudaMemcpyHostToDevice ); //copy of w_k to device
    arr_chk<<<nblocks, blocksize>>>(d_x_k,d_w_k,i);
    cudaMemcpy( x_k, d_x_k, size1, cudaMemcpyDeviceToHost );
    cudaMemcpy( w_k, d_w_k, size1, cudaMemcpyDeviceToHost );
}
printf("\nVerification after return from gpu\n");
for(int i = 0; i<nItt; i++)
{
    for(int j=0;j<nP;j++)
    {
        printf("x_k[%d][%d] = %f\t",i,j,x_k[i][j]);
        printf("w_k[%d][%d] = %f\n",i,j,w_k[i][j]);
    }
}
cudaFree( d_x_k );
cudaFree( d_w_k );
cleanup2d(x_k,nItt);
cleanup2d(w_k,nItt);
getch();
return 0;
4

1 回答 1

5

我的意思是动态数组的动态数组。

嗯,这正是问题所在。动态数组的动态数组由一大堆不相交的内存块组成,数组中的每一行都有一个内存块(从你循环的malloc内部可以清楚地看到)。因此,您不能只调用一次*就将这样的数据结构复制到设备内存中。相反,你必须做format_create2cudaMemcpy

  • 还要在 CUDA 上使用动态数组的动态数组。为此,您必须基本上重新创建您的mat_create2函数,使用cudaMalloc而不是malloc,然后分别复制每一行。

  • 像现在一样在 CUDA 上使用“紧密”的二维数组(这是一件好事,至少在性能方面!)。但是如果你继续在主机内存上使用 dyn-dyn-arrays,你仍然需要单独复制每一行,比如

    for(int i=0; i<r; ++i){
      cudaMemcpy(d_x_k + i*c, x_k[i], c*sizeof(float), cudaMemcpyHostToDevice)
    }
    

您可能想知道“为什么它与静态二维数组一起工作”?好吧, C中的静态二维数组是可以一次性复制的正确、紧凑的数组。使用与 dyn-dyn 数组 ( ) 完全相同的语法对它们进行索引有点令人困惑,因为它实际上完全不同。arr[x][y]

但是您也应该考虑在主机内存上使用紧密数组,也许使用面向对象的包装器,例如

typedef struct {
  float* data;
  int n_rows, n_cols;
} tight2dFloatArray;

#define INDEX_TIGHT2DARRAY(arr, y, x)\
  (arr).data[(y)*(arr).n_cols + (x)]

这种方法当然可以更安全地实现为 C++ 类。


*你也不能只用一个复制它在主内存中memcpy:它只复制指针数组,而不是实际数据。

于 2012-10-13T10:52:05.523 回答