-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
94 lines (79 loc) · 2.35 KB
/
main.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
#include <mpi.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "mpi_comm.h"
#include "sor.h"
#define true 1
#define false 0
extern MPI_Comm CARTESIAN_COMM;
//arguments: dimention, w, threshold, q
int main(int argc, char* argv[])
{
if( argc != 5)
{
printf("Wrong number of arguments.\n");
printf("Usage: %s <array dimention> <w> <threshold> <q>\n", argv[0]);
return 1;
}
///parse arguments
int matrix_width = atoi(argv[1]);
int matrix_height = atoi(argv[1]);
float threshold = atof(argv[3]);
float h = 1/(float)(matrix_width + 1);
float w = atof(argv[2]);
int q = atoi(argv[4]);
srand(time(NULL));
int proc_num = init_mpi();
int dims[2] = { q, proc_num/q};
///create cartesian topology/comunicator
int rank_id = init_mpi_cart(dims);
MPI_Barrier(CARTESIAN_COMM);
///initialize each process block
sor* myblock = init_sor(rank_id, proc_num, matrix_width, matrix_height, h, w, threshold, q);
int converged = false;
int round = 1;
float redmax = 0;
float blackmax = 0;
float maxdif = 0;
float globalmaxdif = 0;
float prev_glb_mx_dif = 0;
///start timer
double start_time = MPI_Wtime();
compute_red(myblock, true);
compute_black(myblock, true);
do
{
if ( round%20 == 0)
{
sync_ext(myblock);
redmax = compute_red(myblock, true);
sync_ext(myblock);
blackmax = compute_black(myblock, true);
maxdif = redmax > blackmax ? redmax : blackmax;
MPI_Allreduce(&maxdif, &globalmaxdif, 1, MPI_FLOAT, MPI_MAX, CARTESIAN_COMM);
/*if (myblock->rank_id == 0)
printf("globalmaxdif = %f\n", globalmaxdif);*/
if ( globalmaxdif < threshold || fabs(globalmaxdif - prev_glb_mx_dif) < threshold )
converged = true;
prev_glb_mx_dif = globalmaxdif;
}
else
{
sync_ext(myblock);
compute_red(myblock, true);
sync_ext(myblock);
compute_black(myblock, true);
}
round++;
}while(!converged && (round < 10000));
if ( rank_id == 0 )
{
printf("%f,%d\n", MPI_Wtime() - start_time, --round);
}
MPI_Finalize();
return 0;
}