File: sc_test_shmem.c

package info (click to toggle)
p4est 2.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,536 kB
  • sloc: ansic: 87,528; makefile: 855; sh: 635; perl: 272; python: 226; awk: 40; javascript: 23
file content (324 lines) | stat: -rw-r--r-- 9,826 bytes parent folder | download
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
  This file is part of the SC Library.
  The SC Library provides support for parallel scientific applications.

  Copyright (C) 2010 The University of Texas System
  Additional copyright (C) 2011 individual authors

  The SC Library 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 2.1 of the License, or (at your option) any later version.

  The SC Library 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 the SC Library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include <sc_shmem.h>

#define DATA_SIZE 10

/* This struct stores data which we use to test shared
 * memory arrays.
 */
typedef struct
{
  int                 rank;     /*< This entry stores the rank of the creating process */
  double              data[DATA_SIZE];  /*< This field can store arbitrary data */
} data_t;

#if 0
/* For each process print the integer entry of
 * each element in an array of type data_t.
 */
void
test_shmem_print_int (data_t * array, sc_MPI_Comm comm)
{
  int                 i, p;
  MPI_Aint            address;
  int                 mpirank, mpisize, mpiret;
  char                outstring[BUFSIZ];

  mpiret = sc_MPI_Comm_size (comm, &mpisize);
  SC_CHECK_MPI (mpiret);
  mpiret = sc_MPI_Comm_rank (comm, &mpirank);
  SC_CHECK_MPI (mpiret);

  mpiret = MPI_Get_address ((void *) array, &address);
  SC_CHECK_MPI (mpiret);
  outstring[0] = '\0';
  snprintf (outstring + strlen (outstring), BUFSIZ - strlen (outstring),
            "Array at %li:\t", (long) address);
  for (i = 0; i < mpisize; i++)
    /* loop over array entries */
  {
    snprintf (outstring + strlen (outstring), BUFSIZ - strlen (outstring),
              "%i ", array[i].rank);
  }

  for (p = 0; p < mpisize; p++)
    /* loop over procs */
  {
    if (mpirank == p) {
      printf ("[H %i] %s\n", mpirank, outstring);
      outstring[0] = '\0';
      fflush (stdout);
    }
    sc_MPI_Barrier (comm);
  }
}
#endif

/* Check whether a given data item has entries
 * data.rank = i
 * data.data = {0,...,DATA_SIZE-1}
 */
int
test_shmem_correct_data (data_t * data, int i)
{
  int                 j;
  if (data->rank != i) {
    return 0;
  }
  for (j = 0; j < DATA_SIZE; j++) {
    if (data->data[j] != (double) j) {
      return 0;
    }
  }
  return 1;
}

/* Fill the array of one data item with
 * the numbers 0,...,DATA_SIZE -1.
 */
void
test_shmem_fill_data (data_t * data)
{
  int                 i;

  for (i = 0; i < DATA_SIZE; i++) {
    data->data[i] = (double) i;
  }
}

/* allocate a shared memory array and fill the date fields */
data_t *
test_shmem_create_data_array (sc_shmem_type_t type, int mpirank, int mpisize)
{
  data_t              data;
  data_t             *data_array;
  int                 i;

  data.rank = mpirank;
  test_shmem_fill_data (&data);

  sc_shmem_set_type (sc_MPI_COMM_WORLD, type);

  data_array = SC_SHMEM_ALLOC (data_t, mpisize, sc_MPI_COMM_WORLD);
  SC_CHECK_ABORT (data_array != NULL, "Allocation failed");

  sc_shmem_allgather (&data, sizeof (data_t), sc_MPI_BYTE, data_array,
                      sizeof (data_t), sc_MPI_BYTE, sc_MPI_COMM_WORLD);
  /* check whether creation worked */
  for (i = 0; i < mpisize; i++) {
    SC_CHECK_ABORTF (test_shmem_correct_data (&data_array[i], i),
                     "Error in shmem_allgather. Array entry %i is not correct.",
                     i);
  }
  return data_array;
}

/* For a given shmem type, allocate a shared array
 * and fill it with data via a call to shmem_allgather.
 * We check whether all data was gathered correctly and
 * free the array.
 */
void
test_shmem_allgather (sc_shmem_type_t type)
{
  data_t             *data_array;
  int                 mpirank, mpisize, mpiret;
  int                 i;

  SC_GLOBAL_ESSENTIALF ("Testing allgather with type %s.\n",
                        sc_shmem_type_to_string[type]);
  mpiret = sc_MPI_Comm_size (sc_MPI_COMM_WORLD, &mpisize);
  SC_CHECK_MPI (mpiret);
  mpiret = sc_MPI_Comm_rank (sc_MPI_COMM_WORLD, &mpirank);
  SC_CHECK_MPI (mpiret);

  data_array = test_shmem_create_data_array (type, mpirank, mpisize);

  for (i = 0; i < mpisize; i++) {
    SC_CHECK_ABORTF (test_shmem_correct_data (&data_array[i], i),
                     "Error in shmem_allgather. Array entry %i is not correct.",
                     i);
  }
  SC_SHMEM_FREE (data_array, sc_MPI_COMM_WORLD);

  SC_GLOBAL_ESSENTIALF ("Testing type %s successful.\n",
                        sc_shmem_type_to_string[type]);
}

/* create a shmem array, copy it and check whether the
 * copy is the same as the original
 */
void
test_shmem_copy (sc_shmem_type_t type)
{
  data_t             *data_array, *copy_array;
  int                 mpirank, mpisize, mpiret;
  int                 i;

  SC_GLOBAL_ESSENTIALF ("Testing copy with type %s.\n",
                        sc_shmem_type_to_string[type]);
  mpiret = sc_MPI_Comm_size (sc_MPI_COMM_WORLD, &mpisize);
  SC_CHECK_MPI (mpiret);
  mpiret = sc_MPI_Comm_rank (sc_MPI_COMM_WORLD, &mpirank);
  SC_CHECK_MPI (mpiret);

  data_array = test_shmem_create_data_array (type, mpirank, mpisize);
  copy_array = SC_SHMEM_ALLOC (data_t, mpisize, sc_MPI_COMM_WORLD);

  sc_shmem_memcpy ((void *) copy_array, (void *) data_array,
                   mpisize * sizeof (data_t), sc_MPI_COMM_WORLD);
  /* Check whether the copy worked */
  for (i = 0; i < mpisize; i++) {
    SC_CHECK_ABORTF (!memcmp
                     (&data_array[i], &copy_array[i], sizeof (data_t)),
                     "Error in shmem_copy. Array entries at %i do not match",
                     i);
  }

  SC_SHMEM_FREE (data_array, sc_MPI_COMM_WORLD);
  SC_SHMEM_FREE (copy_array, sc_MPI_COMM_WORLD);

  SC_GLOBAL_ESSENTIALF ("Testing type %s successful.\n",
                        sc_shmem_type_to_string[type]);
}

void
test_shmem_write (sc_shmem_type_t type)
{
  data_t             *data_array;
  int                 mpirank, mpisize, mpiret;
  int                 i;

  SC_GLOBAL_ESSENTIALF ("Testing shmem_write with type %s.\n",
                        sc_shmem_type_to_string[type]);
  mpiret = sc_MPI_Comm_size (sc_MPI_COMM_WORLD, &mpisize);
  SC_CHECK_MPI (mpiret);
  mpiret = sc_MPI_Comm_rank (sc_MPI_COMM_WORLD, &mpirank);
  SC_CHECK_MPI (mpiret);

  data_array = SC_SHMEM_ALLOC (data_t, mpisize, sc_MPI_COMM_WORLD);
  /* The process that first gets write access to the array writes the data */
  if (sc_shmem_write_start (data_array, sc_MPI_COMM_WORLD)) {
    for (i = 0; i < mpisize; i++) {
      data_array[i].rank = i;
      test_shmem_fill_data (&data_array[i]);
    }
  }

  sc_shmem_write_end (data_array, sc_MPI_COMM_WORLD);
  mpiret = sc_MPI_Barrier (sc_MPI_COMM_WORLD);
  SC_CHECK_MPI (mpiret);

  /* All processes check whether writing worked */
  for (i = 0; i < mpisize; i++) {
    SC_CHECK_ABORTF (test_shmem_correct_data (&data_array[i], i),
                     "Error in shmem_copy. Array entries at %i do not match",
                     i);
  }

  SC_SHMEM_FREE (data_array, sc_MPI_COMM_WORLD);
  SC_GLOBAL_ESSENTIALF ("Testing type %s successful.\n",
                        sc_shmem_type_to_string[type]);
}

void
test_shmem_prefix (sc_shmem_type_t type)
{
  int                *data_array;
  int                 mpirank, mpisize, mpiret;
  int                 i;

  SC_GLOBAL_ESSENTIALF ("Testing prefix with type %s.\n",
                        sc_shmem_type_to_string[type]);
  mpiret = sc_MPI_Comm_size (sc_MPI_COMM_WORLD, &mpisize);
  SC_CHECK_MPI (mpiret);
  mpiret = sc_MPI_Comm_rank (sc_MPI_COMM_WORLD, &mpirank);
  SC_CHECK_MPI (mpiret);

  data_array = SC_SHMEM_ALLOC (int, mpisize + 1, sc_MPI_COMM_WORLD);

  sc_shmem_prefix (&mpirank, data_array, 1, sc_MPI_INT, sc_MPI_SUM,
                   sc_MPI_COMM_WORLD);

  for (i = 0; i <= mpisize; i++) {
    SC_CHECK_ABORTF (data_array[i] == i * (i - 1) / 2,
                     "Error in shmem prefix."
                     "Array entry at %i is not correct.\n", i);
  }

  SC_SHMEM_FREE (data_array, sc_MPI_COMM_WORLD);
  SC_GLOBAL_ESSENTIALF ("Testing type %s successful.\n",
                        sc_shmem_type_to_string[type]);
}

void
test_shmem_test1 ()
{
  int                 type;

  SC_GLOBAL_ESSENTIAL ("Testing sc_shmem_allgather.\n");
  sc_log_indent_push ();
  for (type = (int) SC_SHMEM_BASIC; type < (int) SC_SHMEM_NUM_TYPES; type++) {
    test_shmem_allgather ((sc_shmem_type_t) type);
  }
  sc_log_indent_pop ();
  SC_GLOBAL_ESSENTIAL ("Testing sc_shmem_copy.\n");
  sc_log_indent_push ();
  for (type = (int) SC_SHMEM_BASIC; type < (int) SC_SHMEM_NUM_TYPES; type++) {
    test_shmem_copy ((sc_shmem_type_t) type);
  }
  sc_log_indent_pop ();
  SC_GLOBAL_ESSENTIAL ("Testing sc_shmem_write.\n");
  sc_log_indent_push ();
  for (type = (int) SC_SHMEM_BASIC; type < (int) SC_SHMEM_NUM_TYPES; type++) {
    test_shmem_write ((sc_shmem_type_t) type);
  }
  sc_log_indent_pop ();
  SC_GLOBAL_ESSENTIAL ("Testing sc_shmem_prefix.\n");
  sc_log_indent_push ();
  for (type = (int) SC_SHMEM_BASIC; type < (int) SC_SHMEM_NUM_TYPES; type++) {
    test_shmem_prefix ((sc_shmem_type_t) type);
  }
  sc_log_indent_pop ();
}

int
main (int argc, char *argv[])
{
  int                 mpiret;

  mpiret = sc_MPI_Init (&argc, &argv);
  SC_CHECK_MPI (mpiret);

  sc_init (sc_MPI_COMM_WORLD, 1, 1, NULL, SC_LP_ESSENTIAL);

  test_shmem_test1 ();

  sc_finalize ();

  mpiret = sc_MPI_Finalize ();
  SC_CHECK_MPI (mpiret);
  return 0;
}