#include #include #include #include "knnMPIBlockingDeclarations.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 = atoi(argv[3]); if (numberOfNeighbors >= numberOfPoints) { numberOfNeighbors = 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 pointsDistance){ //Distance at the this index is greater than the distance being inserted //Shifts right all non empty columns holding distances lower than pointsDistance for (int moveColumn=numberOfNeighbors-2; moveColumn>=arrayIndex; --moveColumn){ double tempDistance = (*sortedNeighborsArray)[firstPointId][moveColumn].distance; if (tempDistance == -1){ //Skips empty columns continue; } (*sortedNeighborsArray)[firstPointId][moveColumn+1].distance = tempDistance; (*sortedNeighborsArray)[firstPointId][moveColumn+1].neighborId = (*sortedNeighborsArray)[firstPointId][moveColumn].neighborId; } //Inserts pointsDistance in the space created after shifting (*sortedNeighborsArray)[firstPointId][arrayIndex].distance = pointsDistance; (*sortedNeighborsArray)[firstPointId][arrayIndex].neighborId = secondPointId; break; } } } 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