#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;
}