forked from leobago/fti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncIntv.c
192 lines (171 loc) · 5.37 KB
/
syncIntv.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
190
191
192
/**
* @file syncIntv.c
* @author Leonardo A. Bautista Gomez (../../exaples/heatdis.c) & Karol Sierocinski
* @date March, 2017
* @brief Heat distribution code to test FTI.
*
* Program tests FTI_Snapshot.
* Slight modification of heatdis.c to make one iterations slower than others
* and remove fail/recovery since it's not needed in this test. This Program
* generates necessary output for syncIntvtest.sh.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fti.h>
#include <unistd.h>
//do not change this defines (static verifying)
#define PRECISION 0.005
#define ITER_TIMES 5000
#define ITER_OUT 500
#define ITER_SLOW 2000
#define WORKTAG 50
#define REDUCE 5
#define WORK_DONE 0
#define WORK_STOPED 1
#define SNAPSHOT_FAILED 2
#define VERIFY_SUCCESS 0
#define VERIFY_FAILED 1
void initData(int nbLines, int M, int rank, double *h)
{
int i, j;
for (i = 0; i < nbLines; i++) {
for (j = 0; j < M; j++) {
h[(i*M)+j] = 0;
}
}
if (rank == 0) {
for (j = (M*0.1); j < (M*0.9); j++) {
h[j] = 100;
}
}
}
double doWork(int numprocs, int rank, int M, int nbLines, double *g, double *h)
{
int i,j;
MPI_Request req1[2], req2[2];
MPI_Status status1[2], status2[2];
double localerror;
localerror = 0;
for (i = 0; i < nbLines; i++) {
for (j = 0; j < M; j++) {
h[(i*M)+j] = g[(i*M)+j];
}
}
if (rank > 0) {
MPI_Isend(g+M, M, MPI_DOUBLE, rank-1, WORKTAG, FTI_COMM_WORLD, &req1[0]);
MPI_Irecv(h, M, MPI_DOUBLE, rank-1, WORKTAG, FTI_COMM_WORLD, &req1[1]);
}
if (rank < numprocs-1) {
MPI_Isend(g+((nbLines-2)*M), M, MPI_DOUBLE, rank+1, WORKTAG, FTI_COMM_WORLD, &req2[0]);
MPI_Irecv(h+((nbLines-1)*M), M, MPI_DOUBLE, rank+1, WORKTAG, FTI_COMM_WORLD, &req2[1]);
}
if (rank > 0) {
MPI_Waitall(2,req1,status1);
}
if (rank < numprocs-1) {
MPI_Waitall(2,req2,status2);
}
for (i = 1; i < (nbLines-1); i++) {
for (j = 0; j < M; j++) {
g[(i*M)+j] = 0.25*(h[((i-1)*M)+j]+h[((i+1)*M)+j]+h[(i*M)+j-1]+h[(i*M)+j+1]);
if(localerror < fabs(g[(i*M)+j] - h[(i*M)+j])) {
localerror = fabs(g[(i*M)+j] - h[(i*M)+j]);
}
}
}
if (rank == (numprocs-1)) {
for (j = 0; j < M; j++) {
g[((nbLines-1)*M)+j] = g[((nbLines-2)*M)+j];
}
}
return localerror;
}
int init(char** argv)
{
int rtn = 0; //return value
if (argv[1] == NULL) {
printf("Missing first parameter (config file).\n");
rtn = 1;
}
return rtn;
}
int verify (double globalerror, int rank) {
if (fabs(globalerror - 0.004998) <= 0.000001) {
return 0;
}
printf("%d: globalerror = %f, should be 0.004998\n", rank, globalerror);
return 1;
}
/*-------------------------------------------------------------------------*/
/**
@return integer 0 if successful, 1 otherwise
**/
/*-------------------------------------------------------------------------*/
int main(int argc, char** argv)
{
int rank, nbProcs, nbLines, i, M, arg;
double wtime, *h, *g, memSize, localerror, globalerror = 1;
if (init(argv)) {
return 0; //verify args
}
MPI_Init(&argc, &argv);
FTI_Init(argv[1], MPI_COMM_WORLD);
MPI_Comm_size(FTI_COMM_WORLD, &nbProcs);
MPI_Comm_rank(FTI_COMM_WORLD, &rank);
arg = 4;
M = (int)sqrt((double)(arg * 1024.0 * 512.0 * nbProcs)/sizeof(double));
nbLines = (M / nbProcs)+3;
h = (double *) malloc(sizeof(double *) * M * nbLines);
g = (double *) malloc(sizeof(double *) * M * nbLines);
initData(nbLines, M, rank, g);
memSize = M * nbLines * 2 * sizeof(double) / (1024 * 1024);
if (rank == 0) {
printf("Local data size is %d x %d = %f MB (%d).\n", M, nbLines, memSize, arg);
printf("Target precision : %f \n", PRECISION);
printf("Maximum number of iterations : %d \n", ITER_TIMES);
}
//adding variables to protect
FTI_Protect(0, &i, 1, FTI_INTG);
FTI_Protect(1, h, M*nbLines, FTI_DBLE);
FTI_Protect(2, g, M*nbLines, FTI_DBLE);
int iTmp = 0;
wtime = MPI_Wtime();
for (i = 0; i < ITER_TIMES; i++) {
iTmp = i;
int checkpointed = FTI_Snapshot();
if (!(checkpointed != FTI_SCES || checkpointed != FTI_DONE)) {
printf("%d: Snapshot failed! Returned %d.\n", rank, checkpointed);
free(h);
free(g);
FTI_Finalize();
MPI_Finalize();
return 1;
}
else if (rank == 0 && checkpointed == FTI_DONE) {
printf("Checkpoint made i = %d\n", i);
}
localerror = doWork(nbProcs, rank, M, nbLines, g, h);
if (((i%ITER_OUT) == 0) && (rank == 0)) {
printf("Step : %d, error = %f\n", i, globalerror);
}
if ((i%REDUCE) == 0) {
MPI_Allreduce(&localerror, &globalerror, 1, MPI_DOUBLE, MPI_MAX, FTI_COMM_WORLD);
}
if (globalerror < PRECISION) {
break;
}
if (i >= ITER_TIMES - ITER_SLOW) {
usleep(100000);
}
}
if (rank == 0) {
printf("Execution finished in %lf seconds. Error = %f\n", MPI_Wtime() - wtime, globalerror);
}
int rtn = verify(globalerror, rank);
free(h);
free(g);
FTI_Finalize();
MPI_Finalize();
return rtn;
}