-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyMul.c
117 lines (98 loc) · 2.89 KB
/
myMul.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
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define COLS 24
#define ROWS 16
void printMat(int *a,int rows, int cols)
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
printf("%d ", a[i*cols+ j]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
int p, rank;
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// int i;
int a[ROWS*COLS];
int b[ROWS*COLS];
const int NPROWS=2;
const int NPCOLS=4;
const int BLOCKROWS=ROWS/NPROWS;
const int BLOCKCOLS=COLS/NPCOLS;
if(rank == 0)
{
for(int i = 0; i < ROWS * COLS; i++)
{
a[i] = i;
b[i] = i * 2;
}
}
if(p != NPROWS * NPCOLS)
{
fprintf(stderr,"Err: number of PEs: %d is not %d x %d\n", p, NPROWS, NPCOLS);
MPI_Finalize();
exit(-1);
}
int aOut[BLOCKROWS * BLOCKCOLS];
int bOut[BLOCKROWS * BLOCKCOLS];
for (int i = 0; i < BLOCKROWS * BLOCKCOLS; i++)
{
aOut[i] = 0;
bOut[i] = 0;
}
MPI_Datatype blocktypeA;
MPI_Datatype blocktypeA2;
MPI_Datatype blocktypeB;
MPI_Datatype blocktypeB2;
MPI_Type_vector(BLOCKROWS, BLOCKCOLS, COLS, MPI_INT, &blocktypeA2);
MPI_Type_create_resized(blocktypeA2, 0, sizeof(int), &blocktypeA);
MPI_Type_commit(&blocktypeA);
MPI_Type_vector(BLOCKROWS, BLOCKCOLS, COLS, MPI_INT, &blocktypeB2);
MPI_Type_create_resized(blocktypeB2, 0, sizeof(int), &blocktypeB);
MPI_Type_commit(&blocktypeB);
int dispsA[NPROWS * NPCOLS];
int dispsB[NPROWS * NPCOLS];
int countsA[NPROWS * NPCOLS];
int countsB[NPROWS * NPCOLS];
for (int i = 0; i < NPROWS; i++)
{
for(int j = 0; j < NPCOLS; j++)
{
dispsA[i * NPCOLS + j] = i * COLS * BLOCKROWS + j * BLOCKCOLS;
dispsB[i * NPCOLS + j] = i * COLS * BLOCKROWS + j * BLOCKCOLS;
countsA[i * NPCOLS + j] = 1;
countsB[i * NPCOLS + j] = 1;
}
}
MPI_Scatterv(a, countsA, dispsA, blocktypeA, aOut, BLOCKROWS * BLOCKCOLS, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatterv(b, countsB, dispsB, blocktypeB, bOut, BLOCKROWS * BLOCKCOLS, MPI_INT, 0, MPI_COMM_WORLD);
for(int proc = 0; proc < p; proc++)
{
if (proc == rank)
{
printf("Rank: %d\n", rank);
if(rank == 0)
{
printf("Global matrices: \n");
printMat(a,ROWS,COLS);
printMat(b,ROWS,COLS);
}
// printf("Local matrix:\n");
printf("Local A:\n");
printMat(aOut,BLOCKROWS,BLOCKCOLS);
printf("Local B:\n");
printMat(bOut,BLOCKROWS,BLOCKCOLS);
}
MPI_Barrier(MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}