-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpara2f.c
More file actions
153 lines (132 loc) · 3.56 KB
/
Copy pathpara2f.c
File metadata and controls
153 lines (132 loc) · 3.56 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
// Algo 3: Matrix-vector decomposition
//Best running on N processes
//#define M 7
//#define N 5
//#define P 3
void printMatrix(int x, int y, int mat[x][y]){
int row, column=0;
for (row=0; row<x; row++){
for(column=0; column<y; column++){
printf("%d ", mat[row][column]);
}
printf("\n");
}
printf("\n");
}
void fillSparseMatrix(int x, int y, int matrix[x][y]){
int i=0, j=0;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
if(i==j){
matrix[i][j]=j+1;
}
else if(i*2==j){
matrix[i][j]=j*2;
}
else if(i/2==j){
matrix[i][j]=j*2;
}
else if(j*3==i){
matrix[i][j]=j*3;
}
else if(j/3==i){
matrix[i][j]=j*3;
}
else{
matrix[i][j]=0;
}
}
}
}
void fillVector(int x, int y, int vector[x][y]){
int i=0, j=0;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
vector[i][j]=j+i+1;
}
}
}
void printVector(int len, int vector[len]){
int i=0;
for(i=0; i<len; i++){
printf("%d ", vector[i]);
}
printf("\n");
}
void transposeMatrix(int x, int y, int mat[x][y], int transpose [y][x]){
int i=0, j=0;
for (i = 0; i < x; i++)
for (j = 0; j < y; j++) {
transpose[j][i] = mat[i][j];
// printf(" %d ", mat[i][j]);
}
}
void singleMultiply(int m, int p, int mymat[m], int myvec[p], int myout[m][p]){
int i=0, j=0;
for(i=0; i<m; i++){
for(j=0; j<p; j++){
myout[i][j] = mymat[i]*myvec[j];
}
}
}
main(int argc, char *argv[]){
int myrank, npes;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &npes);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
int m = M, p = P, n = N;
int mat[M][N], vec[N][P];
int transpose[N][M];
int out[M][P]={0};
int myvec[P]={0};
int mymat[M]={0};
int myout[M][P]={0};
double start = MPI_Wtime();
if (myrank==0){
fillSparseMatrix(m, n, mat);
fillVector(n, p, vec);
transposeMatrix(m, n, mat, transpose);
}
//Setting datatypes for MPI processes
MPI_Datatype col, coltype, row, rowtype;
MPI_Type_vector(m, 1, m, MPI_INT, &col);
MPI_Type_commit(&col);
MPI_Type_create_resized(col, 0, 1*sizeof(int), &coltype);
MPI_Type_commit(&coltype);
MPI_Type_vector(p, 1, p, MPI_INT, &row);
MPI_Type_commit(&row);
MPI_Type_create_resized(row, 0, 1*sizeof(int), &rowtype);
MPI_Type_commit(&rowtype);
//Scatter A and B to all processes
MPI_Scatter(transpose, m, coltype,
mymat, m, coltype,
0, MPI_COMM_WORLD);
MPI_Scatter(vec, p, rowtype,
myvec, p, rowtype,
0, MPI_COMM_WORLD);
//Performing tensor product
int i=0, j=0;
for(i=0; i<m; i++){
for(j=0; j<p; j++){
myout[i][j] = mymat[i]*myvec[j];
}
}
//Sending back to p0
MPI_Reduce(myout, out, m*p, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if (myrank==0){
printf("Algo 3: Matrix-vector decomposition \n \n");
printf("Input matrix \n");
printMatrix(m, n, mat);
printf("Input vector \n");
printMatrix(n, p, vec);
printf("Final output \n");
printMatrix(m, p, out);
double finish = MPI_Wtime();
printf("Finished in %f seconds. \n", finish - start);
}
MPI_Finalize();
}