Exercise 2 for the course "Parallel and distributed systems" of THMMY in AUTH university.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.8 KiB

6 years ago
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "knnSerialDeclarations.h"
void getArguments(int argc, char** argv){
if (argc != 6) {
printf("Usage: %s p d k filename tfilename\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("\tfilename is the filename of the dataset file\n");
printf("\tfilename is the filename of the test file\n");
exit(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, neighbor ***sortedNeighborsArray){
//Allocates continuous memory for points array
*pointsArray = cMalloc(numberOfPoints, numberOfDimensions);
//Allocates memory for neighbors array
if ( (*sortedNeighborsArray = (neighbor**)(malloc((sizeof(neighbor *)) * numberOfPoints)) )
!= NULL){
for (int row = 0; row < numberOfPoints; ++row){
if ( ((*sortedNeighborsArray)[row]
= (neighbor*)(malloc((sizeof(neighbor)) * numberOfNeighbors)) ) == NULL){
printf("Error allocating memory\n");
exit(1);
}
}
} else {
printf("Error allocating memory\n");
exit(1);
}
//Reads coordinates from the file
if (readPointsArrayFromFile(pointsArray)){
printf("Press any key to exit.\n");
getchar();
exit(1);
}
//Initializes neighbors array distances and ID's to -1
for (int point=0; point<numberOfPoints; ++point){
for(int neighbor=0; neighbor<numberOfNeighbors; ++neighbor){
(*sortedNeighborsArray)[point][neighbor].distance = -1;
(*sortedNeighborsArray)[point][neighbor].neighborId = -1;
}
}
}
double **cMalloc(int rows, int columns){
double *tempArray = (double *)malloc(rows * columns * sizeof(double));
double **array;
if ((array = ((double**)(malloc((sizeof(double *)) * rows)))) != NULL){
for (int row = 0; row < rows; ++row){
array[row] = &(tempArray[columns * row]);
}
} else {
printf("Error allocating memory\n");
exit(1);
}
return array;
}
int readPointsArrayFromFile(double ***array){
FILE *pointsBinaryFile = fopen(pointsFilename, "rb");
if (pointsBinaryFile == NULL){
printf("Couldn't open points file.\n");
return 1;
}
for (int point=0; point<numberOfPoints; ++point){
if (fread((*array)[point], sizeof(double), numberOfDimensions, pointsBinaryFile)
!= numberOfDimensions) {
if(feof(pointsBinaryFile)){
printf("Premature end of file reached.\n");
} else{
printf("Error reading points file.");
}
fclose(pointsBinaryFile);
return 1;
}
}
fclose(pointsBinaryFile);
return 0;
}
void printArray(int rows, int columns, double ***array){
for (int row=0; row<rows; ++row){
for (int column=0; column<columns; ++column){
printf("[%d][%d] = %f\n", row, column, (*array)[row][column]);
}
printf("\n");
}
}
void calculateDistances(double ***firstPointsSet, double ***secondPointsSet
, neighbor ***sortedNeighborsArray){
for (int firstPoint=0; firstPoint<numberOfPoints; ++firstPoint){
for (int secondPoint=0; secondPoint<numberOfPoints; ++secondPoint){
if (firstPoint == secondPoint){
continue;
}
double distance = 0;
for (int dimensionIndex=0; dimensionIndex<numberOfDimensions; ++dimensionIndex){
distance += pow((*firstPointsSet)[firstPoint][dimensionIndex]
- (*secondPointsSet)[secondPoint][dimensionIndex] ,2);
}
addDistanceAndShift(sortedNeighborsArray, firstPoint, secondPoint, distance);
}
}
}
void addDistanceAndShift(neighbor ***sortedNeighborsArray, int firstPointId, int secondPointId
, double pointsDistance){
for (int arrayIndex=0; arrayIndex<numberOfNeighbors; ++arrayIndex){
//Gets distance stored in the array for this index
double thisNeighborDistance = (*sortedNeighborsArray)[firstPointId][arrayIndex].distance;
if (thisNeighborDistance == -1){ //Loop reached the end of the array
(*sortedNeighborsArray)[firstPointId][arrayIndex].distance = pointsDistance;
(*sortedNeighborsArray)[firstPointId][arrayIndex].neighborId = secondPointId;
break;
} else if (thisNeighborDistance > pointsDistance){
//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;
}
}
}
int test(neighbor ***sortedNeighborsArray, char *testFilename){
FILE *testFile = fopen(testFilename, "r");
if (testFile == NULL){
printf("Couldn't open test file.\n");
perror("fopen");
return 1;
}
for (int point=0; point<numberOfPoints; ++point){
for (int i=0; i<numberOfNeighbors; ++i){
int tempID = 0;
if (fscanf(testFile, "%d,", &tempID) == EOF){
printf("Premature end of file reached.\n");
fclose(testFile);
return 1;
}
if ((*sortedNeighborsArray)[point][i].neighborId != tempID - 1){
fclose(testFile);
return 1;
}
}
}
fclose(testFile);
return 0;
}
void cleanUp(double ***pointsArray, neighbor ***sortedNeighborsArray){
free((*pointsArray)[0]);
free(*pointsArray);
for (int row=0; row<numberOfPoints; ++row){
free((*sortedNeighborsArray)[row]);
}
free(*sortedNeighborsArray);
}