-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLU_mpi.c
189 lines (168 loc) · 3.97 KB
/
LU_mpi.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <mpi.h>
#define ln() putchar('\n')
#define GENERIC_TAG (0)
float *gen_mx (size_t dim);
float *gen_row(size_t dim);
float *gen_row_ref (size_t dim, size_t ref);
void print_mx (float *M, size_t dim, size_t sep);
void forw_elim(float **origin, float *master_row, size_t dim);
void U_print (float *M, int dim);
void L_print (float *M, int dim);
int main(int argc, char *argv[])
{
srand(time(NULL));
const int root_p = 0;
int mx_size = 0, p, id;
if (argc < 2) {
printf("Matrix size missing in the arguments\n");
return EXIT_FAILURE;
}
mx_size = atol(argv[1]);
float *A = gen_mx(mx_size);
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &p);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
if (id == root_p) {
#ifdef ALU
printf("[A]\n");
print_mx(A, mx_size * mx_size, mx_size);
ln();
#endif
}
int i, j, tmp_size = mx_size - 1, diag_ref = 0;
double start = MPI_Wtime();
for (i = 0; i < tmp_size; i++, diag_ref++) {
float *diag_row = &A[diag_ref * mx_size + diag_ref];
for (j = diag_ref + 1; j < mx_size; j++) {
if (j % p == id) {
float *save = &A[j * mx_size + diag_ref];
//printf("[%d] ", id);
//print_mx(save, mx_size - diag_ref, mx_size - diag_ref);
forw_elim(&save, diag_row, mx_size - diag_ref);
}
}
//MPI_Barrier(MPI_COMM_WORLD);
for (j = diag_ref + 1; j < mx_size; j++) {
float *save = &A[j * mx_size + diag_ref];
MPI_Bcast(save, mx_size - diag_ref, MPI_FLOAT, j % p, MPI_COMM_WORLD);
}
//MPI_Barrier(MPI_COMM_WORLD);
/*
if (id == root_p) {
#ifdef ALU
printf("(%d)\n", i);
print_mx(A, mx_size * mx_size, mx_size);
ln();
#endif
}
*/
}
double end = MPI_Wtime();
if (id == root_p) {
/*
printf("[LU]\n");
print_mx(A, mx_size * mx_size, mx_size);
*/
#ifdef ALU
printf("\n[L]\n");
L_print(A, mx_size);
printf("\n[U]\n");
U_print(A, mx_size);
#endif
printf("mpi: %f s\n", end - start);
}
free(A);
MPI_Finalize();
return EXIT_SUCCESS;
}
/*
* gen_mx - generate contiguous matrix
*
* @dim dim x dim matrix
* @return matrix
*/
float *gen_mx (size_t dim)
{
int i, j, tot = dim * dim;
float *M = malloc(sizeof(float) * tot);
for (i = 0; i < tot; i++) {
M[i] = rand() % 101 - 50;
}
return M;
}
/*
* mx_print - dumb matrix print function
*
* @M matrix/row
* @dim matrix/row dimension
* @sep where put separator
*/
void print_mx (float *M, size_t dim, size_t sep)
{
int i, j;
for (i = 0; i < dim; i++) {
printf("% *.*f\t", 4, 2, M[i]);
if ((i + 1) % sep == 0) {
ln();
}
}
}
/*
* forw_elim - forward Gauss elimination
*
* @origin row pointer by reference
* @master_row row in which lays diagonal
*/
void forw_elim(float **origin, float *master_row, size_t dim)
{
if (**origin == 0)
return;
float k = **origin / master_row[0];
int i;
for (i = 1; i < dim; i++) {
(*origin)[i] = (*origin)[i] - k * master_row[i];
}
**origin = k;
}
/*
* U_print - dumb U matrix print function
*/
void U_print (float *M, int dim)
{
int i, j;
float z = 0;
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
if (j >= i) {
printf("% *.*f\t", 4, 2, M[i * dim + j]);
} else {
printf("% *.*f\t", 4, 2, z);
}
}
ln();
}
}
/*
* L_print - dumb L matrix print function
*/
void L_print (float *M, int dim)
{
int i, j;
float z = 0, u = 1;
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
if (j > i) {
printf("% *.*f\t", 4, 2, z);
} else if (i == j) {
printf("% *.*f\t", 4, 2, u);
} else {
printf("% *.*f\t", 4, 2, M[i * dim + j]);
}
}
ln();
}
}