Visual studio 2010 cuda计划的问题

Visual studio 2010 cuda计划的问题,visual-studio-2010,cuda,Visual Studio 2010,Cuda,我正在编写一个简单的cuda程序,在这个程序中,我在设备中创建一个2D数组,然后在内核函数中执行非常基本的操作,操作完成后,我将它复制回主机的2D数组。我在遵循stackoverlow和cuda论坛的几个线程之后编写了这段代码。我遵循了建议,但是我得到的代码输出是0,而我期望数组的所有成员的输出都是10。我在下面发布我的代码: __global__ void test_kernel(int *dev_ptr[]) { int tidx = threadIdx.x; int tid

我正在编写一个简单的cuda程序,在这个程序中,我在设备中创建一个2D数组,然后在内核函数中执行非常基本的操作,操作完成后,我将它复制回主机的2D数组。我在遵循stackoverlow和cuda论坛的几个线程之后编写了这段代码。我遵循了建议,但是我得到的代码输出是0,而我期望数组的所有成员的输出都是10。我在下面发布我的代码:

__global__ void test_kernel(int *dev_ptr[])
{
    int tidx = threadIdx.x;
    int tidy = threadIdx.y;

    dev_ptr[tidx][tidy] = dev_ptr[tidx][tidy] +10;
}

int main(int argc,char *argv[])
{

    int env_end =50;
    int **h_ptr ;
    int **d_ptr ;
    int **env_t;
    int i,k,j;
    /************************************************************************/
    /* cpu
    /************************************************************************/
    env_t =(int **) malloc(env_end * sizeof *env_t);
    for(k=0;k<env_end;k++)
    {env_t[k] = (int *)malloc(env_end* env_end* sizeof *env_t[0]);                                                                                                                             
    }

    for (k = 1; k < env_end; ++k)
        env_t[k] = env_t[k - 1] + env_end;

    memset(*env_t, 0, env_end * env_end* sizeof **env_t);

    for (i=0;i<env_end;i++)
    {  for(j=0;j<env_end;j++)
    {printf("%d\t",env_t[i][j]);        }
    if (j==env_end-1)
    {printf("\n");  }
    }

    /************************************************************************/
    /* gpu
    /************************************************************************/

    h_ptr = (int **)malloc(env_end*sizeof(int *));
    for (i=0;i<env_end;i++)
    {  cudaMalloc((void **)&h_ptr[i],env_end*sizeof(int));
        cudaMemcpy(h_ptr[i],&env_t[i][0],env_end*sizeof(int),cudaMemcpyHostToDevice);
    }

    cudaMalloc((void ***)d_ptr,env_end*sizeof(int));
    cudaMemcpy(d_ptr,h_ptr,env_end*sizeof(int),cudaMemcpyHostToDevice);


    /************************************************************************/
    /* kernel function and declaration
    /************************************************************************/

          dim3 blockDim(env_end,env_end,1);

          test_kernel<<<1,blockDim>>>(d_ptr);


    /************************************************************************/
    /* Copying data back to host
    ************************************************************************/
          for (i=0;i<env_end;i++)
          {cudaMemcpy(env_t[i],h_ptr[i],env_end*sizeof(int),cudaMemcpyDeviceToHost);
          }

          for (i=0;i<env_end;i++)
          {  for(j=0;j<env_end;j++)
          {printf("%d\t",env_t[i][j]);      }
          if (j==env_end-1)
          {printf("\n");    }
          }

    /************************************************************************/
    /* Freeing the memory locations
    /************************************************************************/
          for (i=0;i<env_end;i++)
          {cudaFree(h_ptr[i]);
          }

          cudaFree(d_ptr);
          free(h_ptr);

          for (i=0;i<env_end;i++)
          { free(env_t[i]);
            }
          free(env_t);

}
\uuuuu全局\uuuuuu无效测试\u内核(int*dev\u ptr[])
{
int tidx=threadIdx.x;
int-tidy=threadIdx.y;
dev_ptr[tidx][tidy]=dev_ptr[tidx][tidy]+10;
}
int main(int argc,char*argv[])
{
int env_end=50;
int**h_ptr;
国际**d_ptr;
国际**环境;
int i,k,j;
/************************************************************************/
/*中央处理器
/************************************************************************/
环境温度=(int**)malloc(环境温度*环境温度*sizeof*环境温度);

对于(k=0;k),此代码存在一些问题,包括:

  • h_ptr
    env_t
    d_ptr
    之间的大小不匹配
  • 对于
    cudamaloc
    ,请使用
    &
    而不是
    (***无效)
  • 不要通过
    cudamaloc
    分配主机内存
  • 优化:2D内存在全局内存中分配。 分配1D内存并将其称为2D
以下是完整的代码:

#include <stdio.h>
#define SIZE 10
#define INDEX(i,j,k) i*k+j

__global__ void test_kernel(int *dev_ptr, int row_size)
{
    int tidx = threadIdx.x;
    int tidy = threadIdx.y;

    dev_ptr[INDEX(tidx,tidy,row_size)] = dev_ptr[INDEX(tidx,tidy,row_size)] +10;
}

int main(int argc,char *argv[])
{

    int env_end =SIZE;
    int *d_ptr=NULL;
    int *env_t;
    int i,j;

    /************************************************************************/
    // cpu
    /************************************************************************/
    env_t =(int *) malloc(env_end * env_end * sizeof(int));
    memset(env_t, 0, env_end * env_end* sizeof(int));

    printf("Input Array:\n");
    for (i=0;i<env_end;i++)
    {   for(j=0;j<env_end;j++)
        {printf("%d\t",env_t[INDEX(i,j,env_end)]);        }
        printf("\n");
    }
    printf("\n");


    /************************************************************************/
    // gpu
    /************************************************************************/
    cudaMalloc(&d_ptr,env_end*env_end*sizeof(int));
    cudaMemcpy(d_ptr,env_t,env_end*env_end*sizeof(int),cudaMemcpyHostToDevice);

    /************************************************************************/
    // kernel function and declaration
    /************************************************************************/
    dim3 blckDim(env_end,env_end,1);
    test_kernel<<<1,blckDim>>>(d_ptr, env_end);


    /************************************************************************/
    // Copying data back to host
    /************************************************************************/
    cudaMemcpy(env_t,d_ptr,env_end*env_end*sizeof(int),cudaMemcpyDeviceToHost);

    printf("Output Array:\n");
    for (i=0;i<env_end;i++)
    {  for(j=0;j<env_end;j++)
        {printf("%d\t",env_t[INDEX(i,j,env_end)]);      }
        printf("\n");
    }
    printf("\n");

    /************************************************************************/
    // Freeing the memory locations
    /************************************************************************/

    cudaFree(d_ptr);
    free(env_t);

}
#包括
#定义尺寸10
#定义索引(i,j,k)i*k+j
__全局无效测试内核(int*dev\u ptr,int行大小)
{
int tidx=threadIdx.x;
int-tidy=threadIdx.y;
dev_ptr[索引(tidx,tidy,row_size)]=dev_ptr[索引(tidx,tidy,row_size)]+10;
}
int main(int argc,char*argv[])
{
int env_end=尺寸;
int*d_ptr=NULL;
国际*环境;
int i,j;
/************************************************************************/
//中央处理器
/************************************************************************/
env_t=(int*)malloc(env_end*env_end*sizeof(int));
memset(env_t,0,env_end*env_end*sizeof(int));
printf(“输入数组:\n”);

对于(i=0;我非常感谢您的回复。我将深入研究您的代码,但在设备全局内存中声明2D数组背后有一个原因,我不确定是否能够将2D到1D配置为我在更大项目的一小部分中提供的代码。我仍然会尝试每分钟检查一次您的代码nd会像你提到的那样尝试配置我的程序,但我不确定我是否能够做到。实际上我遵循了和中提到的代码。