现在有多个一维数组,我想将他们拷贝到设备端修改他们的值,然后拷贝回host端,代码如下:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "helper_cuda.h"
#include <stdio.h>
#include<iostream>
using namespace std;
#define N 10
__global__ void gpuKernel(int **ptr)
{
int tid=threadIdx.x;
int bid=blockIdx.x;
if (tid<N &&bid<N)
{
ptr[bid][tid]=N;
}
}
int main()
{
int **hptr,**hostArray;
int **dptr,**devArray;
hptr=new int *[N];
cudaMalloc(&dptr,N*sizeof(int*));
hostArray=new int *[N];
devArray=new int *[N];
for (int i=0;i<N;i++)
{
hostArray[i]=new int[N];
for (int j=0;j<N;j++)
{
hostArray[i][j]=1;
}
}
for (int i=0;i<N;i++)
{
cudaMemcpy(devArray[i],hostArray[i],N*sizeof(int),cudaMemcpyHostToDevice);
hptr[i]=devArray[i];//保存每个数组的头指针
}
cudaMemcpy(dptr,hptr,N*sizeof(int*),cudaMemcpyHostToDevice);
dim3 dimblock=N;
dim3 dimgrid =N;
gpuKernel<<<dimgrid,dimblock>>>(dptr);
for (int i=0;i<N;i++)
{ cudaMemcpy(hostArray[i],devArray[i],N*sizeof(int),cudaMemcpyDeviceToHost);
}
for (int i=0;i<N;i++)
{
for (int j=0;j<N;j++)
{
cout<<hostArray[i][j]<<",";
}
cout<<endl;