I need to copy a 2d array from device to host. I have attached the code below can any one reply and

#include<stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_CITIES 5
#include<cuda.h>
#include<curand_kernel.h>
#include<curand.h>
#define gpuErrchk(ans) { gpuAssert((ans), FILE, LINE); }

inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{

if (code != cudaSuccess)
{
fprintf(stderr,“GPUassert: %s %s %d\n”, cudaGetErrorString(code), file, line);
if (abort) { exit(code); }
}
}

device int dev_dist[MAX_CITIES][MAX_CITIES];
global void initialize()
{
dev_dist[0][0]=5;
dev_dist[0][1]=2;
}
int main()
{
size_t pitch;
int i,j,b;

int **host_dist = (int **)malloc(MAX_CITIES * sizeof(int *));
for (b=0; b<MAX_CITIES; b++)
host_dist[b] = (int *)malloc(MAX_CITIES * sizeof(int));

gpuErrchk(cudaMallocPitch((int **)&dev_dist,(size_t)&pitch,MAX_CITIES*sizeof(int),MAX_CITIES));

//gpuErrchk(cudaMemcpy2D(dev_dist,pitch,host_dist,MAX_CITIESsizeof(int),MAX_CITIESsizeof(int),MAX_CITIES,cudaMemcpyHostToDevice));

initialize<<<1,1>>>();

gpuErrchk(cudaMemcpy(host_dist,dev_dist,(sizeof(int)MAX_CITIESMAX_CITIES),cudaMemcpyDeviceToHost));
gpuErrchk(cudaMemcpy2D(dev_dist,pitch,host_dist,MAX_CITIESsizeof(int),MAX_CITIESsizeof(int),MAX_CITIES,cudaMemcpyDeviceToHost));
//(cudaMemcpy2D(host_dist,MAX_CITIESsizeof(int),dev_dist,pitch,MAX_CITIESsizeof(int),MAX_CITIES,cudaMemcpyDeviceToHost));
for(i=0;i<MAX_CITIES;i++)
{printf("\n");
for(j=0;j<MAX_CITIES;j++)
printf("%d",host_dist[i][j]);}
return 0;
}

Your method for attempting to use a 2D (i.e. doubly-subscripted) array on the device will not work and has a variety of errors in it.

The general recommendation for beginners is that you flatten such arrays and handle them as 1D, and use pointer/index arithmetic to simulate 2D access if desired.

I have tried for index conversion it was working good but i need to perform some calculation for that 2d array please suggest any other techniques