implemented spread_evenly() in assignment1

master
Peter Babič 9 years ago
parent 4f3a6d78ac
commit 535a1622f3
  1. 39
      assignment1.c
  2. 51
      assignment2.c
  3. 38
      assignment3.c
  4. 52
      common.c
  5. 4
      common.h

@ -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);
}

@ -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]]);
}

@ -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);

@ -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;
}

@ -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

Loading…
Cancel
Save