small refactorings and cleaning

master
Peter Babič 9 years ago
parent 87ad365293
commit 8933b8b785
  1. 130
      zadanie2.c
  2. 2
      zadanie2.in

@ -5,8 +5,6 @@
// TODO: define custom structure for this probably
#define CODE_SIZE 0
#define CODE_INDEX 1
//#define CODE_COLS 0
//#define CODE_COUNT 1
#define NUM_CODES 2
int transpose_index(int index, int rows, int cols);
@ -19,15 +17,15 @@ int main(int argc, char *argv[]) {
MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
MPI_Comm_rank(MPI_COMM_WORLD, &numRank);
int *arrNumbers, *arrBlockSizes, *arrBlockIndices;
int cols = 0, numCount = 0;
int *arrMatrixOriginal, *arrIndicesTransp, *arrBlockSizes, *arrBlockIndices;
int numCols, numCount = 0;
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
arrNumbers = my_malloc(sizeBufferTotal * sizeof(int));
arrMatrixOriginal = my_malloc(sizeBufferTotal * sizeof(int));
// Open the file with input data
FILE *ptrFile = my_fopen(INPUTFILE, "r");
@ -37,22 +35,24 @@ int main(int argc, char *argv[]) {
while (fgets(strBuffer, sizeof strBuffer, ptrFile) != 0) {
// Store the pointer
ptrBuffer = strBuffer;
cols = 0;
numCols = 0;
int numValue;
// Read untill newline
while (*ptrBuffer != '\n') {
// Grow buffer when needed
arrNumbers = buffer_grow(arrNumbers, numCount, &sizeBufferTotal);
arrMatrixOriginal = buffer_grow(arrMatrixOriginal, numCount, &sizeBufferTotal);
// Convert string to base 10
numValue = strtol(ptrBuffer, &ptrBuffer, 10);
// Store the number read in memory
arrNumbers[numCount++] = numValue;
cols++;
arrMatrixOriginal[numCount++] = numValue;
numCols++;
}
}
checkParallelWorth(numProcesses, numCount);
arrIndicesTransp = my_malloc(numCount * sizeof(int));
int numBlockIndex = 0;
int numRemainder = numCount % numProcesses;
@ -83,67 +83,43 @@ int main(int argc, char *argv[]) {
}
//int packet[NUM_CODES];
//MPI_Scatter(arrPackets, NUM_CODES, MPI_INT, packet, NUM_CODES, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&numCount, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&cols, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&numCols, 1, MPI_INT, 0, MPI_COMM_WORLD);
//printf("size %2d, index %2d, buffer %2d, rank %d\n", packet[0], packet[1], packet[CODE_COUNT], numRank);
//int *arrSlice = my_malloc(sizeBufferTotal * sizeof(int));
//MPI_Scatterv(arrNumbers, arrPackets, arrPackets, MPI_INT, arrSlice, sizeBufferTotal, MPI_INT, 0, MPI_COMM_WORLD);
//if (numRank == 0 ) {
//printf("original matrix: \n");
//for (int i = 0; i < numCount; i++) {
//printf("%d ", arrNumbers[i]);
//if ((i + 1) % cols == 0)
//printf("\n");
//}
//printf("\n");
//}
int arrTransp[NUM_CODES];
MPI_Recv(arrTransp, NUM_CODES, MPI_INT, 0, NUM_CODES, MPI_COMM_WORLD, &status);
//for (int i = 0; i < numCount; i++) {
//printf("rank %d: m[%d] = %d\n", numRank, i, arrSlice[i]);
//}
//We can calculate rows * cols = numCount
int rows = numCount / cols;
int *arrIndices = my_malloc(numCount * sizeof(int));
//We can calculate numRows * numCols = numCount
int numRows = numCount / numCols;
int *arrIndicesSub = my_malloc(numCount * sizeof(int));
//for (int i = arrTransp[CODE_INDEX]; i < (arrTransp[CODE_SIZE] + arrTransp[CODE_INDEX]); i++) {
for (int i = 0; i < arrTransp[CODE_SIZE]; i++) {
//if (i % rows == 0)
//printf("\n");
// Only the indices are transposed
int index = transpose_index(i + arrTransp[CODE_INDEX], rows, cols);
arrIndices[i] = index;
//printf("%2d[%2d](%d)\n", i, index, numRank);
//arrIndices[index] = i;
//printf("%2d[%2d](%d)\n", index, i, numRank);
// Only the indices are transposed in parallel
int index = transpose_index(i + arrTransp[CODE_INDEX], numRows, numCols);
arrIndicesSub[i] = index;
}
//printf("\n");
//printf("rank %d, rows %d, cols %d, count %d, index %2d, size %2d\n", numRank, rows, cols, numCount,
//arrTransp[CODE_INDEX], arrTransp[CODE_SIZE]);
int *arrMatrix;
if (numRank == 0)
arrMatrix = my_malloc(numCount * sizeof(int));
MPI_Gatherv(arrIndices, arrTransp[CODE_SIZE], MPI_INT, arrMatrix, arrBlockSizes, arrBlockIndices, MPI_INT, 0, MPI_COMM_WORLD);
//if (numRank == 0)
MPI_Gatherv(arrIndicesSub, arrTransp[CODE_SIZE], MPI_INT, arrIndicesTransp, arrBlockSizes, arrBlockIndices,
MPI_INT, 0, MPI_COMM_WORLD);
if (numRank == 0) {
//if (numRank == 0 ) {
//printf("original matrix: \n");
//for (int i = 0; i < numCount; i++) {
//printf("arrMatrix[%2d] = %2d\n", i, arrMatrix[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 % rows == 0)
if (i % numRows == 0)
printf("\n");
printf("%d ", arrNumbers[arrMatrix[i]]);
printf("%d ", arrMatrixOriginal[arrIndicesTransp[i]]);
}
printf("\n");
}
@ -153,54 +129,8 @@ int main(int argc, char *argv[]) {
}
int transpose_index(int index, int rows, int cols) {
//return ((rows * index) / (cols * rows)) + ((rows * index) % (colss * rows));
//return ((rows * index) / (cols * rows)) + ((rows * index) % (cols * rows));
//return (index / cols) + rows * (index % cols);
return (index / rows) + cols * (index % rows);
}
//printf("trasnposed matrix:");
//for (int i = 0; i < numCount; i++) {
//if (i % rows == 0)
//printf("\n");
//printf("%d ", arrTrans[i]);
//}
/* for (int i = 0; i < numCount; i++) {*/
//if (i % rows == 0)
//printf("\n");
//printf("%2d[%2d]\t", i, arrIndices[i]);
/*}*/
//printf("rows: %zu, cols: %d\n", numCount / cols, cols);
/*
int numBlockIndex = 0;
int numRemainder = numCount % numProcesses;
arrBlockIndices = my_malloc(numProcesses * sizeof(int));
arrBlockSizes = my_malloc(numProcesses * sizeof(int));
int numPackets = numProcesses * NUM_CODES;
arrPackets = my_malloc(numPackets * sizeof(int));
for (int i = 0; i < numPackets; i += NUM_CODES) {
arrBlockSizes[i] = arrPackets[i + CODE_SIZE] = numCount / numProcesses;
if (numRemainder > 0) {
arrPackets[i]++;
numRemainder--;
}
arrBlockIndices[i] = arrPackets[i + CODE_INDEX] = numBlockIndex;
numBlockIndex += arrPackets[i];
arrPackets[i + CODE_COLS] = cols;
arrPackets[i + CODE_COUNT] = numCount;
}*/
//for (int i = 0; i < numPackets; i += 2) {
//printf("size[%d] = %d\tindex[%d] = %d\n", i, arrPackets[i], i + 1, arrPackets[i + 1]);
//}
//for (int i = 0; i < numProcesses; i++) {
//printf("size[%d] = %d\tindex[%d] = %d\n", i, arrPackets[i], i, arrPackets[i + 1]);
//}
//for (int i = 0; i < numProcesses; i++) {
//MPI_Send(&arrPackets[i], 1, MPI_INT, i, 1, MPI_COMM_WORLD);
//}

@ -5,3 +5,5 @@
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9

Loading…
Cancel
Save