This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
forked from bejonwe/MPIHelloWorld
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpi_uebung_2_muster.c
104 lines (82 loc) · 3 KB
/
mpi_uebung_2_muster.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv) {
int rank, size;
double time;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Rank %d starting\n", rank);
time = MPI_Wtime();
// ----------------------------------------------------
int matrixA[4][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{0, 1, 2}};
int matrixB[3][3] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}};
int heightA = (sizeof matrixA) / (sizeof matrixA[0]);
int widthA = (sizeof matrixA[0]) / (sizeof matrixA[0][0]);
int heightB = (sizeof matrixB) / (sizeof matrixB[0]);
int widthB = (sizeof matrixB[0]) / (sizeof matrixB[0][0]);
if (widthA != heightB) {
printf("the matrices cannot be multiplied\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
int heightC = heightA;
int widthC = widthB;
// ---------------------------------------------------- scatter
int rowsPerProcess = heightC / size;
int rest = heightC % size;
if (rest) {
printf("the number of processes must be a divisor of the number of lines\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
int partMatrixA[rowsPerProcess][widthA];
int partMatrixC[rowsPerProcess][widthC];
MPI_Scatter(matrixA, rowsPerProcess * widthA, MPI_INT, partMatrixA, rowsPerProcess * widthA, MPI_INT, 0, MPI_COMM_WORLD);
//* DEBUG print partMatrixA
for (int y = 0; y < rowsPerProcess; y++) {
for (int x = 0; x < widthA; x++) {
if (rank == 0) printf("Rank %d partMatrixA[%d][%d] = %d\n", rank, y, x, partMatrixA[y][x]);
}
}
// ---------------------------------------------------- calculate
for (int y = 0; y < widthC; y++) {
for (int r = 0; r < rowsPerProcess; r++) {
partMatrixC[r][y] = 0;
for (int x = 0; x < widthA; x++) {
partMatrixC[r][y] += partMatrixA[r][x] * matrixB[x][y];
}
}
}
//* DEBUG print partMatrixC
for (int y = 0; y < rowsPerProcess; y++) {
for (int x = 0; x < widthC; x++) {
if (rank == 0) printf("Rank %d partMatrixC[%d][%d] = %d\n", rank, y, x, partMatrixC[y][x]);
}
}
// ---------------------------------------------------- gather
int matrixC[heightC][widthC];
MPI_Gather(partMatrixC, rowsPerProcess * widthC, MPI_INT, matrixC, rowsPerProcess * widthC, MPI_INT, 0, MPI_COMM_WORLD);
// ---------------------------------------------------- print result
if (rank == 0) {
printf("\nRank 0 result matrix:\n");
for (int y = 0; y < heightC; y++) {
for (int x = 0; x < widthC; x++) {
printf("%d ", matrixC[y][x]);
}
printf("\n");
}
}
// ----------------------------------------------------
time = MPI_Wtime() - time;
printf("Rank %d time = %g\n", rank, time);
MPI_Finalize();
return 0;
}