forked from parmes/solfec-1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
put.c
266 lines (201 loc) · 5.8 KB
/
put.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* put.c
* Copyright (C) 2009, Tomasz Koziara (t.koziara AT gmail.com)
* ---------------------------------------------------------------
* parallel utilities
*/
/* This file is part of Solfec.
* Solfec is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Solfec is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Solfec. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <limits.h>
#include <mpi.h>
#include "err.h"
#include "alg.h"
#include "put.h"
/* get statistics on a vector of integer variables */
void PUT_int_stats (int n, int *val, int *sum, int *min, int *avg, int *max)
{
int ncpu, *all, i, j, va, su, mi, av, ma;
MPI_Comm_size (MPI_COMM_WORLD, &ncpu);
ERRMEM (all = malloc (sizeof (int [n * ncpu])));
MPI_Allgather (val, n, MPI_INT, all, n, MPI_INT, MPI_COMM_WORLD);
for (j = 0; j < n; j ++)
{
for (su = i = 0, ma = INT_MIN, mi = INT_MAX; i < ncpu; i ++)
{
va = all [i*n + j];
su += va, ma = MAX (ma, va), mi = MIN (mi, va);
}
av = su / ncpu;
if (sum) sum [j] = su;
if (min) min [j] = mi;
if (avg) avg [j] = av;
if (max) max [j] = ma;
}
free (all);
}
/* get statistics on a vector of integer variables; return 1 for rank 0 and 0 for others */
int PUT_root_int_stats (int n, int *val, int *sum, int *min, int *avg, int *max)
{
int rank, ncpu, *all;
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
MPI_Comm_size (MPI_COMM_WORLD, &ncpu);
if (rank == 0) { ERRMEM (all = malloc (sizeof (int [n * ncpu]))); }
else all = NULL;
MPI_Gather (val, n, MPI_INT, all, n, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0)
{
int i, j, va, su, mi, av, ma;
for (j = 0; j < n; j ++)
{
for (su = i = 0, ma = INT_MIN, mi = INT_MAX; i < ncpu; i ++)
{
va = all [i*n + j];
su += va, ma = MAX (ma, va), mi = MIN (mi, va);
}
av = su / ncpu;
if (sum) sum [j] = su;
if (min) min [j] = mi;
if (avg) avg [j] = av;
if (max) max [j] = ma;
}
free (all);
return 1;
}
return 0;
}
/* get statistics on a vector of double variables; return 1 for rank 0 and 0 for others */
int PUT_root_double_stats (int n, double *val, double *sum, double *min, double *avg, double *max)
{
int rank, ncpu;
double *all;
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
MPI_Comm_size (MPI_COMM_WORLD, &ncpu);
if (rank == 0) { ERRMEM (all = malloc (sizeof (double [n * ncpu]))); }
else all = NULL;
MPI_Gather (val, n, MPI_DOUBLE, all, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (rank == 0)
{
double va, su, mi, av, ma;
int i, j;
for (j = 0; j < n; j ++)
{
for (su = i = 0, ma = INT_MIN, mi = INT_MAX; i < ncpu; i ++)
{
va = all [i*n + j];
su += va, ma = MAX (ma, va), mi = MIN (mi, va);
}
av = su / (double) ncpu;
if (sum) sum [j] = su;
if (min) min [j] = mi;
if (avg) avg [j] = av;
if (max) max [j] = ma;
}
free (all);
return 1;
}
return 0;
}
/* parallel timer end: get maximum of all calls; return 1 for rank 0 and 0 for others */
int PUT_root_timerend (TIMING *t, double *time)
{
double local, timing;
int rank;
local = timerend (t);
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
if (!time) time = &timing;
MPI_Reduce (&local, time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
if (rank == 0) return 1;
else return 0;
}
/* parallel timer end: return maximum of all calls */
double PUT_timerend (TIMING *t)
{
double local, time;
local = timerend (t);
MPI_Allreduce (&local, &time, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
return time;
}
/* return sum of all calls */
int PUT_int_sum (int val)
{
int ret;
MPI_Allreduce (&val, &ret, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
return ret;
}
/* return maximum of all calls */
int PUT_int_max (int val)
{
int ret;
MPI_Allreduce (&val, &ret, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
return ret;
}
/* return minimum of all calls */
int PUT_int_min (int val)
{
int ret;
MPI_Allreduce (&val, &ret, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
return ret;
}
/* MPI operator callback used to find minimal value rank */
static void int_min_rank (int *in, int *inout, int *len, MPI_Datatype *type)
{
int i;
for (i = 0; i < *len; i ++)
{
if (in [0] < inout [0]) /* smaller value */
{
inout [0] = in [0];
inout [1] = in [1];
}
else if (in [0] == inout [0] && in [1] < inout [1]) /* same values => compare ranks */
{
inout [0] = in [0];
inout [1] = in [1];
}
inout += 2;
in += 2;
}
}
/* return minimum of all calls and its rank */
int PUT_int_min_rank (int val, int *rank)
{
int in [2], out [2];
MPI_Datatype type;
MPI_Op op;
in [0] = val;
MPI_Comm_rank (MPI_COMM_WORLD, &in [1]);
MPI_Type_contiguous (2, MPI_INT, &type);
MPI_Type_commit (&type);
MPI_Op_create ((MPI_User_function*)int_min_rank, 1, &op);
MPI_Allreduce (in, out, 1, type, op, MPI_COMM_WORLD); /* compute (min(val), rank(min(val))) in out */
MPI_Type_free (&type);
MPI_Op_free (&op);
if (rank) *rank = out [1];
return out [0];
}
/* return minimum of all calls */
double PUT_double_min (double val)
{
double ret;
MPI_Allreduce (&val, &ret, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
return ret;
}
/* return maximum of all calls */
double PUT_double_max (double val)
{
double ret;
MPI_Allreduce (&val, &ret, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
return ret;
}