-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingpong_fault.c
165 lines (132 loc) · 3.99 KB
/
pingpong_fault.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
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_ITERATION 5
#define MPIX_SAFE_CALL(__operation, __predicate, __label) {int code = __operation; if (code != MPI_SUCCESS) {__predicate; goto __label;}}
void inject_fault(int rank);
int main_loop(int argc, char **argv, int epoch, int *done);
void application_checkpoint_read(int epoch, int rank, int *start, int *payload);
void application_checkpoint_write(int epoch, int rank, int start, int payload);
int main(int argc, char **argv)
{
int code = MPI_SUCCESS;
int abort = 0;
int done = 0;
int fault_epoch = 0;
while(!abort && !done)
{
switch(code)
{
case MPI_SUCCESS:
MPI_Init(&argc, &argv);
MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
break;
case MPIX_TRY_RELOAD:
MPIX_Checkpoint_read();
break;
default:
MPI_Abort(MPI_COMM_WORLD, 100);
break;
}
MPIX_Get_fault_epoch(&fault_epoch);
code = main_loop(argc, argv, fault_epoch, &done);
}
MPI_Finalize();
return 0;
}
int main_loop(int argc, char **argv, int epoch, int *done)
{
int size, rank;
int start, msg;
int code = MPI_SUCCESS;
// Randomly choose an iteration to fail between 0 and MAX_ITERATION - 1
srand(time(NULL));
int failed_iteration = rand() % MAX_ITERATION;
MPIX_SAFE_CALL(MPI_Comm_size(MPI_COMM_WORLD, &size), code = MPIX_TRY_RELOAD, fail_return);
MPIX_SAFE_CALL(MPI_Comm_rank(MPI_COMM_WORLD, &rank), code = MPIX_TRY_RELOAD, fail_return);
if(size > 2)
{
MPI_Abort(MPI_COMM_WORLD, 100);
}
if (epoch > 0) {
application_checkpoint_read(epoch - 1, rank, &start, &msg);
// NOTE this disables failing again
failed_iteration = MAX_ITERATION + 1;
start += 1;
}
else {
start = 0;
// initial payload
if(rank == 0)
msg = 0;
else
msg = -1;
}
printf("Hello world from rank %d out of %d processors at epoch %i\n", rank, size, epoch);
// PING PONG
for (int i = start; i < MAX_ITERATION; ++i)
{
if(rank == 0)
{
MPIX_SAFE_CALL(MPI_Send(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD), code = MPIX_TRY_RELOAD, fail_return);
MPIX_SAFE_CALL(MPI_Recv(&msg, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE), code = MPIX_TRY_RELOAD, fail_return);
printf("proc %i recv %i\n", rank, msg);
}
else
{
MPIX_SAFE_CALL(MPI_Recv(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE), code = MPIX_TRY_RELOAD, fail_return);
printf("proc %i recv %i\n", rank, msg);
msg += 1;
// inject fault
if(i == failed_iteration)
{
inject_fault(rank);
}
MPIX_SAFE_CALL(MPI_Send(&msg, 1, MPI_INT, 0, 0, MPI_COMM_WORLD), code = MPIX_TRY_RELOAD, fail_return);
}
// Checkpoint at the end of each iteration
MPIX_SAFE_CALL(MPIX_Get_fault_epoch(&epoch), code = MPIX_TRY_RELOAD, fail_return);
application_checkpoint_write(epoch, rank, i, msg);
MPIX_SAFE_CALL(MPIX_Checkpoint_write(), code = MPIX_TRY_RELOAD, fail_return);
}
printf("%d checksum %i == %i\n", rank, msg, MAX_ITERATION);
MPIX_SAFE_CALL(MPI_Barrier(MPI_COMM_WORLD), code = MPIX_TRY_RELOAD, fail_return);
if (code == MPI_SUCCESS)
{
*done = 1;
}
return code;
fail_return:
return MPIX_TRY_RELOAD;
}
void inject_fault(int rank)
{
printf("proc %i injecting fault, exit now\n", rank);
exit(MPIX_TRY_RELOAD);
}
void application_checkpoint_read(int epoch, int rank, int *start, int *payload) {
FILE *fp;
char buf[10];
sprintf(buf, "check_%d_%d", epoch, rank);
if ((fp = fopen(buf, "rb")) == NULL)
{
printf("ERROR: Failure in Opening File");
}
fread(start, sizeof(int), 1, fp);
fread(payload, sizeof(int), 1, fp);
fclose(fp);
}
void application_checkpoint_write(int epoch, int rank, int start, int payload) {
FILE *fp;
char buf[10];
sprintf(buf, "check_%d_%d", epoch, rank);
if ((fp = fopen(buf, "wb")) == NULL)
{
printf("ERROR: Failure in Opening File\n");
}
fwrite(&start, sizeof(int), 1, fp);
fwrite(&payload, sizeof(int), 1, fp);
fclose(fp);
}