From 535a1622f369f894d5330ce43ba66d4101c9bd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Mon, 2 Nov 2015 18:57:49 +0100 Subject: [PATCH] implemented spread_evenly() in assignment1 --- assignment1.c | 39 ++++++++++++++++++++++---------------- assignment2.c | 51 +++++++++++++------------------------------------- assignment3.c | 38 ++----------------------------------- common.c | 52 ++++++++++++++++++++++++++++++++++++++++++++------- common.h | 4 +++- 5 files changed, 86 insertions(+), 98 deletions(-) diff --git a/assignment1.c b/assignment1.c index d35ba1c..6847fcd 100644 --- a/assignment1.c +++ b/assignment1.c @@ -3,6 +3,7 @@ #define INPUTFILE "assignment1.in" int maximum(int n, int *arrNumbers); +void send_block_size(const void *blockSize, int toProcess); int main(int argc, char *argv[]) { int numProcesses, numRank; @@ -36,25 +37,27 @@ int main(int argc, char *argv[]) { fclose(ptrFile); checkParallelWorth(numProcesses, sizeBufferUsed); + + spread_evenly(numProcesses, sizeBufferUsed, &arrBlockIndices, &arrBlockSizes, &send_block_size); - int numBlockIndex = 0; - int numRemainder = sizeBufferUsed % numProcesses; + //int numBlockIndex = 0; + //int numRemainder = sizeBufferUsed % numProcesses; - arrBlockIndices = my_malloc(numProcesses * sizeof(int)); - arrBlockSizes = my_malloc(numProcesses * sizeof(int)); + //arrBlockIndices = my_malloc(numProcesses * sizeof(int)); + //arrBlockSizes = my_malloc(numProcesses * sizeof(int)); - for (int i = 0; i < numProcesses; i++) { - arrBlockSizes[i] = sizeBufferUsed / numProcesses; - if (numRemainder > 0) { - arrBlockSizes[i]++; - numRemainder--; - } - - arrBlockIndices[i] = numBlockIndex; - numBlockIndex += arrBlockSizes[i]; - // Send block size to every process - MPI_Send(&arrBlockSizes[i], 1, MPI_INT, i, 1, MPI_COMM_WORLD); - } + //for (int i = 0; i < numProcesses; i++) { + //arrBlockSizes[i] = sizeBufferUsed / numProcesses; + //if (numRemainder > 0) { + //arrBlockSizes[i]++; + //numRemainder--; + //} + + //arrBlockIndices[i] = numBlockIndex; + //numBlockIndex += arrBlockSizes[i]; + //// Send block size to every process + //MPI_Send(&arrBlockSizes[i], 1, MPI_INT, i, 1, MPI_COMM_WORLD); + //} //for (int i = 0; i < numProcesses; i++) { //printf("size[%d] = %d\tindex[%d] = %d\n", i, arrBlockSizes[i], i, arrBlockIndices[i]); @@ -103,4 +106,8 @@ int maximum(int n, int *arrNumbers) { return value; } +//void send_block_size(int blockSize, int toProcess) { +void send_block_size(const void *blockSize, int toProcess) { + MPI_Send(blockSize, 1, MPI_INT, toProcess, 1, MPI_COMM_WORLD); +} diff --git a/assignment2.c b/assignment2.c index bbc66db..1670ce9 100644 --- a/assignment2.c +++ b/assignment2.c @@ -22,32 +22,7 @@ int main(int argc, char *argv[]) { if (numRank == 0) { - // To avoid aggresive buffer growth at the beggining of the sequence - int sizeBufferTotal = INIT_BUFFER_SIZE; - // Allocate enough memory for initial buffer - arrMatrixOriginal = my_malloc(sizeBufferTotal * sizeof(int)); - // Open the file with input data - FILE *ptrFile = my_fopen(INPUTFILE, "r"); - - // Read line by line until the end of file - // TODO: remove the MAX_LINE_LENGTH limitation of strBuffer - char *ptrBuffer, strBuffer[MAX_LINE_LENGTH]; - while (fgets(strBuffer, sizeof strBuffer, ptrFile) != 0) { - // Store the pointer - ptrBuffer = strBuffer; - numCols = 0; - int numValue; - // Read untill newline - while (*ptrBuffer != '\n') { - // Grow buffer when needed - arrMatrixOriginal = buffer_grow(arrMatrixOriginal, numCount, &sizeBufferTotal); - // Convert string to base 10 - numValue = strtol(ptrBuffer, &ptrBuffer, 10); - // Store the number read in memory - arrMatrixOriginal[numCount++] = numValue; - numCols++; - } - } + arrMatrixOriginal = read_matrix_file(INPUTFILE, &numCols, &numCount); checkParallelWorth(numProcesses, numCount); @@ -68,7 +43,7 @@ int main(int argc, char *argv[]) { arrBlockIndices[i] = numBlockIndex; numBlockIndex += arrBlockSizes[i]; - + int arrTransp[NUM_CODES]; arrTransp[CODE_SIZE] = arrBlockSizes[i]; arrTransp[CODE_INDEX] = arrBlockIndices[i]; @@ -78,7 +53,7 @@ int main(int argc, char *argv[]) { } //for (int i = 0; i < numProcesses; i++) { - //printf("size[%d] = %d\tindex[%d] = %d\n", i, arrBlockSizes[i], i, arrBlockIndices[i]); + //printf("size[%d] = %d\tindex[%d] = %d\n", i, arrBlockSizes[i], i, arrBlockIndices[i]); //} } @@ -89,7 +64,7 @@ int main(int argc, char *argv[]) { int arrTransp[NUM_CODES]; MPI_Recv(arrTransp, NUM_CODES, MPI_INT, 0, NUM_CODES, MPI_COMM_WORLD, &status); - + //We can calculate numRows * numCols = numCount int numRows = numCount / numCols; int *arrIndicesSub = my_malloc(numCount * sizeof(int)); @@ -102,22 +77,22 @@ int main(int argc, char *argv[]) { //if (numRank == 0) MPI_Gatherv(arrIndicesSub, arrTransp[CODE_SIZE], MPI_INT, arrIndicesTransp, arrBlockSizes, arrBlockIndices, - MPI_INT, 0, MPI_COMM_WORLD); + MPI_INT, 0, MPI_COMM_WORLD); //if (numRank == 0 ) { - //printf("original matrix: \n"); - //for (int i = 0; i < numCount; i++) { - //printf("%d ", arrMatrixOriginal[i]); - //if ((i + 1) % numCols == 0) - //printf("\n"); - //} - //printf("\n"); + //printf("original matrix: \n"); + //for (int i = 0; i < numCount; i++) { + //printf("%d ", arrMatrixOriginal[i]); + //if ((i + 1) % numCols == 0) + //printf("\n"); + //} + //printf("\n"); //} if (numRank == 0) { printf("transposed matrix:"); for (int i = 0; i < numCount; i++) { - if (i % numRows == 0) + if (i % numRows == 0) printf("\n"); printf("%d ", arrMatrixOriginal[arrIndicesTransp[i]]); } diff --git a/assignment3.c b/assignment3.c index 3df7678..9248ad9 100644 --- a/assignment3.c +++ b/assignment3.c @@ -10,40 +10,6 @@ int transpose_index(int index, int rows, int cols); -void *read_datafile(const char *filename, int *numCols, int *numCount) { - // To avoid aggresive buffer growth at the beggining of the sequence - int sizeBufferTotal = INIT_BUFFER_SIZE; - // Allocate enough memory for initial buffer - int *ptr = my_malloc(sizeBufferTotal * sizeof(int)); - // Open the file with input data - FILE *ptrFile = my_fopen(filename, "r"); - - // Read line by line until the end of file - // TODO: remove the MAX_LINE_LENGTH limitation of strBuffer - char *ptrBuffer, strBuffer[MAX_LINE_LENGTH]; - while (fgets(strBuffer, sizeof strBuffer, ptrFile) != 0) { - // Store the pointer - ptrBuffer = strBuffer; - *numCols = 0; - int numValue; - // Read untill newline - while (*ptrBuffer != '\n') { - // Grow buffer when needed - ptr = buffer_grow(ptr, *numCount, &sizeBufferTotal); - // Convert string to base 10 - numValue = strtol(ptrBuffer, &ptrBuffer, 10); - // Store the number read in memory - ptr[*numCount] = numValue; - *numCount = *numCount + 1; - *numCols = *numCols + 1; - } - } - - fclose(ptrFile); - - return ptr; -} - int main(int argc, char *argv[]) { int numProcesses, numRank; MPI_Status status; @@ -56,8 +22,8 @@ int main(int argc, char *argv[]) { int numCols1, numCount1 = 0, numCols2, numCount2 = 0; if (numRank == 0) { - arrMatrix1 = read_datafile(INPUTFILE1, &numCols1, &numCount1); - arrMatrix2 = read_datafile(INPUTFILE2, &numCols2, &numCount2); + arrMatrix1 = read_matrix_file(INPUTFILE1, &numCols1, &numCount1); + arrMatrix2 = read_matrix_file(INPUTFILE2, &numCols2, &numCount2); checkParallelWorth(numProcesses, numCount1); diff --git a/common.c b/common.c index 28c65e9..30e53f9 100644 --- a/common.c +++ b/common.c @@ -57,7 +57,8 @@ bool checkParallelWorth(int numProcesses, int sizeBufferUsed) { return true; } -void spread_evenly(int numProcesses, int sizeBufferUsed, int **arrBlockIndices, int **arrBlockSizes) { +void spread_evenly(int numProcesses, int sizeBufferUsed, + int **arrBlockIndices, int **arrBlockSizes, void (*ptrFciLoopEnd)(const void*, int)) { int numBlockIndex = 0; int numRemainder = sizeBufferUsed % numProcesses; @@ -65,17 +66,54 @@ void spread_evenly(int numProcesses, int sizeBufferUsed, int **arrBlockIndices, *arrBlockSizes = my_malloc(numProcesses * sizeof(int)); for (int i = 0; i < numProcesses; i++) { - (*arrBlockSizes)[i] = sizeBufferUsed / numProcesses; + int numBlockSize = sizeBufferUsed / numProcesses; + (*arrBlockSizes)[i] = numBlockSize; if (numRemainder > 0) { - (*arrBlockSizes)[i]++; + (*arrBlockSizes)[i] = ++numBlockSize; numRemainder--; } (*arrBlockIndices)[i] = numBlockIndex; - numBlockIndex += *arrBlockSizes[i]; + numBlockIndex += numBlockSize; + //printf("size[%d] = %d\tindex[%d] = %d\n", i, (*arrBlockSizes)[i], i, (*arrBlockIndices)[i]); + + (*ptrFciLoopEnd)(&numBlockSize, i); + //MPI_Send(&arrBlockSizes[i], 1, MPI_INT, i, 1, MPI_COMM_WORLD); } +} + +void *read_matrix_file(const char *filename, int *numCols, int *numCount) { + // To avoid aggresive buffer growth at the beggining of the sequence + int sizeBufferTotal = INIT_BUFFER_SIZE; + // Allocate enough memory for initial buffer + int *ptr = my_malloc(sizeBufferTotal * sizeof(int)); + // Open the file with input data + FILE *ptrFile = my_fopen(filename, "r"); - //for (int i = 0; i < numProcesses; i++) { - //printf("size[%d] = %d\tindex[%d] = %d\n", i, arrBlockSizes[i], i, arrBlockIndices[i]); - //} + // Read line by line until the end of file + // TODO: remove the MAX_LINE_LENGTH limitation of strBuffer + char *ptrBuffer, strBuffer[MAX_LINE_LENGTH]; + while (fgets(strBuffer, sizeof strBuffer, ptrFile) != 0) { + // Store the pointer + ptrBuffer = strBuffer; + *numCols = 0; + int numValue; + // Read untill newline + while (*ptrBuffer != '\n') { + // Grow buffer when needed + ptr = buffer_grow(ptr, *numCount, &sizeBufferTotal); + // Convert string to base 10 + numValue = strtol(ptrBuffer, &ptrBuffer, 10); + // Store the number read in memory + ptr[*numCount] = numValue; + *numCount = *numCount + 1; + *numCols = *numCols + 1; + } + } + + fclose(ptrFile); + + return ptr; } + + diff --git a/common.h b/common.h index 7d3202d..7e01b5c 100644 --- a/common.h +++ b/common.h @@ -20,6 +20,8 @@ void *my_realloc(void *ptr, size_t size); FILE *my_fopen(const char *filename, const char *mode); void *buffer_grow(void *ptr, int sizeBufferUsed, int *sizeBufferTotal); bool checkParallelWorth(int numProcesses, int sizeBufferUsed); -void spread_evenly(int numProcesses, int sizeBufferUsed, int **arrBlockIndices, int **arrBlockSizes); +void spread_evenly(int numProcesses, int sizeBufferUsed, + int **arrBlockIndices, int **arrBlockSizes, void (*ptrFciLoopEnd)(const void*, int)); +void *read_matrix_file(const char *filename, int *numCols, int *numCount); #endif