|
|
|
@ -3,12 +3,7 @@ |
|
|
|
|
#define INPUTFILE1 "assignment3.in1" |
|
|
|
|
#define INPUTFILE2 "assignment3.in2" |
|
|
|
|
|
|
|
|
|
// TODO: define custom structure for this probably
|
|
|
|
|
#define CODE_SIZE 0 |
|
|
|
|
#define CODE_INDEX 1 |
|
|
|
|
#define NUM_CODES 2 |
|
|
|
|
|
|
|
|
|
int transpose_index(int index, int rows, int cols); |
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) { |
|
|
|
|
int numProcesses, numRank; |
|
|
|
@ -25,90 +20,89 @@ int main(int argc, char *argv[]) { |
|
|
|
|
arrMatrix1 = read_matrix_file(INPUTFILE1, &numCols1, &numCount1); |
|
|
|
|
arrMatrix2 = read_matrix_file(INPUTFILE2, &numCols2, &numCount2); |
|
|
|
|
|
|
|
|
|
checkParallelWorth(numProcesses, numCount1); |
|
|
|
|
checkParallelWorth(numProcesses, numCount2); |
|
|
|
|
|
|
|
|
|
arrIndicesTransp = my_malloc(numCount1 * sizeof(int)); |
|
|
|
|
arrIndicesTransp = my_malloc(numCount2 * sizeof(int)); |
|
|
|
|
|
|
|
|
|
int numBlockIndex = 0; |
|
|
|
|
int numRemainder = numCount1 % numProcesses; |
|
|
|
|
spread_evenly(numProcesses, numCount2, &arrBlockIndices, &arrBlockSizes); |
|
|
|
|
|
|
|
|
|
arrBlockIndices = my_malloc(numProcesses * sizeof(int)); |
|
|
|
|
arrBlockSizes = my_malloc(numProcesses * sizeof(int)); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < numProcesses; i++) { |
|
|
|
|
arrBlockSizes[i] = numCount1 / numProcesses; |
|
|
|
|
if (numRemainder > 0) { |
|
|
|
|
arrBlockSizes[i]++; |
|
|
|
|
numRemainder--; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
arrBlockIndices[i] = numBlockIndex; |
|
|
|
|
numBlockIndex += arrBlockSizes[i]; |
|
|
|
|
|
|
|
|
|
int arrTransp[NUM_CODES]; |
|
|
|
|
arrTransp[CODE_SIZE] = arrBlockSizes[i];
|
|
|
|
|
arrTransp[CODE_INDEX] = arrBlockIndices[i];
|
|
|
|
|
// Send every process starting index of 1D represented matrix and number of
|
|
|
|
|
// succeding indices to calculate transposition
|
|
|
|
|
MPI_Send(arrTransp, NUM_CODES, MPI_INT, i, NUM_CODES, MPI_COMM_WORLD);
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//for (int i = 0; i < numProcesses; i++) {
|
|
|
|
|
//printf("size[%d] = %d\tindex[%d] = %d\n", i, arrBlockSizes[i], i, arrBlockIndices[i]);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
MPI_Bcast(&numCount1, 1, MPI_INT, 0, MPI_COMM_WORLD); |
|
|
|
|
MPI_Bcast(&numCols1, 1, MPI_INT, 0, MPI_COMM_WORLD); |
|
|
|
|
MPI_Bcast(&numCount2, 1, MPI_INT, 0, MPI_COMM_WORLD); |
|
|
|
|
MPI_Bcast(&numCols2, 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 arrTransp[NUM_CODES]; |
|
|
|
|
MPI_Recv(arrTransp, NUM_CODES, MPI_INT, 0, NUM_CODES, MPI_COMM_WORLD, &status); |
|
|
|
|
|
|
|
|
|
//We can calculate numRows * numCols1 = numCount1
|
|
|
|
|
int numRows = numCount1 / numCols1; |
|
|
|
|
int *arrIndicesSub = my_malloc(numCount1 * 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++) { |
|
|
|
|
int arrBlockData[NUM_CODES]; |
|
|
|
|
MPI_Recv(arrBlockData, NUM_CODES, MPI_INT, 0, 1, MPI_COMM_WORLD, &status); |
|
|
|
|
|
|
|
|
|
//We can calculate numRows2 * numCols2 = numCount2
|
|
|
|
|
int numRows2 = numCount2 / numCols2; |
|
|
|
|
int *arrIndicesSub = my_malloc(numCount2 * sizeof(int)); |
|
|
|
|
//for (int i = arrBlockData[CODE_INDEX]; i < (arrBlockData[CODE_SIZE] + arrBlockData[CODE_INDEX]); i++) {
|
|
|
|
|
for (int i = 0; i < arrBlockData[CODE_SIZE]; i++) { |
|
|
|
|
// Only the indices are transposed in parallel
|
|
|
|
|
int index = transpose_index(i + arrTransp[CODE_INDEX], numRows, numCols1); |
|
|
|
|
int index = transpose_index(i + arrBlockData[CODE_INDEX], numRows2, numCols2); |
|
|
|
|
arrIndicesSub[i] = index; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//if (numRank == 0)
|
|
|
|
|
MPI_Gatherv(arrIndicesSub, arrTransp[CODE_SIZE], MPI_INT, arrIndicesTransp, arrBlockSizes, arrBlockIndices,
|
|
|
|
|
MPI_INT, 0, MPI_COMM_WORLD); |
|
|
|
|
|
|
|
|
|
//if (numRank == 0 ) {
|
|
|
|
|
//printf("original matrix: \n");
|
|
|
|
|
//for (int i = 0; i < numCount1; i++) {
|
|
|
|
|
//printf("%d ", arrMatrix1[i]);
|
|
|
|
|
//if ((i + 1) % numCols1 == 0)
|
|
|
|
|
//printf("\n");
|
|
|
|
|
//}
|
|
|
|
|
//printf("\n");
|
|
|
|
|
//}
|
|
|
|
|
MPI_Gatherv(arrIndicesSub, arrBlockData[CODE_SIZE], MPI_INT, arrIndicesTransp, arrBlockSizes, arrBlockIndices,
|
|
|
|
|
MPI_INT, 0, MPI_COMM_WORLD); |
|
|
|
|
|
|
|
|
|
if (numRank == 0) { |
|
|
|
|
printf("transposed matrix:"); |
|
|
|
|
if (numRank == 0 ) { |
|
|
|
|
printf("normal 1st matrix: \n"); |
|
|
|
|
for (int i = 0; i < numCount1; i++) { |
|
|
|
|
if (i % numRows == 0) |
|
|
|
|
printf("%d ", arrMatrix1[i]); |
|
|
|
|
if ((i + 1) % numCols1 == 0) |
|
|
|
|
printf("\n"); |
|
|
|
|
printf("%d ", arrMatrix1[arrIndicesTransp[i]]); |
|
|
|
|
} |
|
|
|
|
printf("\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
printf("fe %d\n", arrMatrix2[0]); |
|
|
|
|
if (numRank == 0) { |
|
|
|
|
printf("transposed 2nd matrix:"); |
|
|
|
|
for (int i = 0; i < numCount2; i++) { |
|
|
|
|
if (i % numRows2 == 0) |
|
|
|
|
printf("\n"); |
|
|
|
|
printf("%d ", arrMatrix2[arrIndicesTransp[i]]); |
|
|
|
|
} |
|
|
|
|
printf("\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
MPI_Finalize(); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int transpose_index(int index, int rows, int cols) { |
|
|
|
|
//return ((rows * index) / (cols * rows)) + ((rows * index) % (cols * rows));
|
|
|
|
|
//return (index / cols) + rows * (index % cols);
|
|
|
|
|
return (index / rows) + cols * (index % rows); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//int numBlockIndex = 0;
|
|
|
|
|
//int numRemainder = numCount1 % numProcesses;
|
|
|
|
|
|
|
|
|
|
//arrBlockIndices = my_malloc(numProcesses * sizeof(int));
|
|
|
|
|
//arrBlockSizes = my_malloc(numProcesses * sizeof(int));
|
|
|
|
|
|
|
|
|
|
//for (int i = 0; i < numProcesses; i++) {
|
|
|
|
|
//arrBlockSizes[i] = numCount1 / numProcesses;
|
|
|
|
|
//if (numRemainder > 0) {
|
|
|
|
|
//arrBlockSizes[i]++;
|
|
|
|
|
//numRemainder--;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//arrBlockIndices[i] = numBlockIndex;
|
|
|
|
|
//numBlockIndex += arrBlockSizes[i];
|
|
|
|
|
//printf("size[%d] = %d\tindex[%d] = %d\n", i, arrBlockSizes[i], i, arrBlockIndices[i]);
|
|
|
|
|
|
|
|
|
|
//int arrBlockData[NUM_CODES];
|
|
|
|
|
//arrBlockData[CODE_SIZE] = arrBlockSizes[i];
|
|
|
|
|
//arrBlockData[CODE_INDEX] = arrBlockIndices[i];
|
|
|
|
|
//// Send every process starting index of 1D represented matrix and number of
|
|
|
|
|
//// succeding indices to calculate transposition
|
|
|
|
|
//MPI_Send(arrBlockData, NUM_CODES, 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]);
|
|
|
|
|
//}
|
|
|
|
|