#include<stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_CITIES 5
device double sqr(double x)
{
return x*x;
}
device double distance(int i,int j,int *a_d, int *b_d)
{
return (sqrt(sqr(a_d[i]-a_d[j])+sqr(b_d[i]-b_d[j])));
}
global void calc_dist(double *dev_dist,int *a_d, int *b_d)
{
int i,j;
for(i=0;i<MAX_CITIES;i++)
for(j=0;j<MAX_CITIES;j++)
dev_dist[i*MAX_CITIES+j]=distance(i,j,a_d,b_d);
}
int main()
{
int c1[MAX_CITIES];
int c2[MAX_CITIES];
double *host_dist,*dev_dist;
int *a_h,*b_h,*a_d,*b_d;
FILE *f1;
int n=0;
int a,b;
f1=fopen(“co.txt”,“r”);
while(!feof(f1))
{
fscanf(f1,“%d”,&c1[n]);
fscanf(f1,“%d”,&c2[n]);
n++;
}
fclose(f1);
for(n=0;n<MAX_CITIES;n++)
printf(“\n%d %d”,c1[n],c2[n]);
a_h = (int *)malloc(sizeof(int)*MAX_CITIES);
b_h = (int *)malloc(sizeof(int)*MAX_CITIES);
for (a=0; a<MAX_CITIES; a++)
{
a_h[a] = c1[a];
b_h[a] = c2[a];
printf(“\n hai%d%d”,a_h[a],b_h[a]);
}
cudaMalloc((void **) &a_d, sizeof(int)*MAX_CITIES);
cudaMalloc((void **) &b_d, sizeof(int)*MAX_CITIES);
cudaMemcpy(a_d, a_h, sizeof(int)*MAX_CITIES, cudaMemcpyHostToDevice);
cudaMemcpy(b_d, b_h, sizeof(int)*MAX_CITIES, cudaMemcpyHostToDevice);
host_dist=(double )malloc(sizeof(double)MAX_CITIESMAX_CITIES);
cudaMalloc((void **) &dev_dist, sizeof(double)MAX_CITIESMAX_CITIES);
calc_dist<<<1,1>>>(dev_dist,a_d,b_d);
cudaMemcpy(host_dist,dev_dist,sizeof(double)MAX_CITIESMAX_CITIES,cudaMemcpyDeviceToHost);
for(b=0;b<(MAX_CITIESMAX_CITIES);b++)
printf("%lf\ndist ",host_dist[b]);
free(a_h);free(b_h);free(c_h);
cudaFree(a_d);cudaFree(b_d);
cudaFree(dev_dist);
return 0;
}