#include #include #include #include #include #include #include #define SIMD_SIZE 8 typedef struct { double x; double y; } point; double sqr(double x) { return x*x; } double dist(point cities[], int i, int j) { return sqrt(sqr(cities[i].x-cities[j].x)+ sqr(cities[i].y-cities[j].y)); } double DistSqrd(point cities[], int i, int j) { return (sqr(cities[i].x-cities[j].x)+ sqr(cities[i].y-cities[j].y)); } void swap(point *p, point *q) { point tmp=*p; *p = *q; *q = tmp; } void swapd(double *p, double *q) { double tmp=*p; *p = *q; *q = tmp; } void tsp(point cities[], point tour[], int ncities) { size_t i,j; size_t ClosePt=0; double CloseDist; double *x, *y; void *x1, *y1; int err; size_t size=sizeof(double)*(ncities+2*SIMD_SIZE)&~(sizeof(double)*SIMD_SIZE-1); if ((err=posix_memalign(&x1,sizeof(double)*SIMD_SIZE,size))) error(1,err,"tsp"); x = x1+size-sizeof(double)*ncities; if ((err=posix_memalign(&y1,sizeof(double)*SIMD_SIZE,size))) error(1,err,"tsp"); y = y1+size-sizeof(double)*ncities; for (i=1; i=0; k--) if (ThisDist[k] %ld\n",i,ClosePt); swapd(&x[i],&x[ClosePt]); swapd(&y[i],&y[ClosePt]); tour[i].x = x[i]; tour[i].y = y[i]; } free(x1); free(y1); } int main(int argc, char *argv[]) { int i, ncities; point *cities; point *tour; FILE *psfile; double sumdist = 0.0; if (argc!=2) { fprintf(stderr, "usage: %s ", argv[0]); exit(1); } ncities = atoi(argv[1]); cities = malloc(ncities*sizeof(point)); tour = malloc(ncities*sizeof(point)); for (i=0; i