#include #include #include #include "knnMPINonBlockingDeclarations.h" #include "mpi.h" void getArguments(int argc, char** argv){ if (argc != 6) { printf("Usage: %s p d k filename\nwhere:\n", argv[0]); printf("\tp is the the number of points\n"); printf("\td is the number of dimensions of each point\n"); printf("\tk is the number of neighbors to search for\n"); printf("\tdf is the filename of the dataset file\n"); printf("\ttf is the filename of the dataset file\n"); abExit(1); } numberOfPoints = atoi(argv[1]); numberOfDimensions = atoi(argv[2]); numberOfNeighbors = numberOfPoints - 1; k = atoi(argv[3]); if (k >= numberOfPoints){ k = numberOfPoints - 1; } pointsFilename = argv[4]; testFilename = argv[5]; } void init(double ***pointsArray, double ***inBuffer, double ***outBuffer , neighbor ***sortedNeighborsArray, int chunksize, int offset){ //Allocates memory for points array *pointsArray = cMalloc(chunksize, numberOfDimensions); //Allocates memory for the buffer storing points coming from another process *inBuffer = cMalloc(chunksize, numberOfDimensions); //Allocates memory for the buffer storing points going out to another process *outBuffer = cMalloc(chunksize, numberOfDimensions); //Allocates memory for neighbors array if ( (*sortedNeighborsArray = (neighbor**)(malloc((sizeof(neighbor *)) * chunksize))) != NULL ){ for (int row = 0; row < chunksize; ++row){ if ( ( (*sortedNeighborsArray)[row] = (neighbor*)(malloc((sizeof(neighbor)) * numberOfNeighbors)) ) == NULL ){ printf("Error allocating memory\n"); abExit(1); } } } else { printf("Error allocating memory\n"); abExit(1); } //Reads coordinates from the file if (readPointsArrayFromFile(pointsArray, chunksize, offset)){ abExit(1); } //Initializes neighbors array distances and ID's to -1 for (int point=0; point firstPointId){ --insertIndex; } (*sortedNeighborsArray)[firstPointIndex][insertIndex].distance = pointsDistance; (*sortedNeighborsArray)[firstPointIndex][insertIndex].neighborId = secondPointId; } int compare (const void *a, const void *b){ //Casts arguments to pointers to neighbors const neighbor *neighborA = (neighbor*) a; const neighbor *neighborB = (neighbor*) b; if (neighborA->distance == neighborB->distance){ return 0; } else{ return neighborA->distance > neighborB->distance ? 1 : -1; } } void swapPointers(double ***firstArray, double ***secondArray){ double **tempPtr = *firstArray; *firstArray = *secondArray; *secondArray = tempPtr; } int test(neighbor ***sortedNeighborsArray, int chunksize, int offset, char *testFilename){ FILE *testFile = fopen(testFilename, "r"); char *discarded = NULL; size_t n = 0; if (testFile == NULL){ printf("Couldn't open test file.\n"); perror("fopen"); return 1; } if (offset){ for (int line=0; line