File: dtypes.c

package info (click to toggle)
mpich 4.3.0%2Breally4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 419,120 kB
  • sloc: ansic: 1,215,557; cpp: 74,755; javascript: 40,763; f90: 20,649; sh: 18,463; xml: 14,418; python: 14,397; perl: 13,772; makefile: 9,279; fortran: 8,063; java: 4,553; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (379 lines) | stat: -rw-r--r-- 15,825 bytes parent folder | download | duplicates (3)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include "mpitestconf.h"
#include "mpitest.h"
#include "dtypes.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

/* This file contains code to generate a variety of MPI datatypes for testing
   the various MPI routines.

   To simplify the test code, this generates an array of datatypes, buffers with
   data and buffers with no data (0 bits) for use in send and receive
   routines of various types.

   In addition, this doesn't even test all of the possibilities.  For example,
   there is currently no test of sending more than one item defined with
   MPI_Type_contiguous .

   Note also that this test assumes that the sending and receive types are
   the same.  MPI requires only that the type signatures match, which is
   a weaker requirement.

   This code was drawn from the MPICH-1 test suite and modified to fit the
   new MPICH test suite.  It provides an alternative set of datatype tests
   to the ones in mtest.c.

 */

/* Change this to test only the basic, predefined types */
static int basic_only = 0;

/*
   Arrays types, inbufs, outbufs, and counts are allocated by the
   CALLER.  n on input is the maximum number; on output, it is the
   number defined.

   See MTestDatatype2Allocate below for a routine to allocate these arrays.

   We may want to add a routine to call to check that the proper data
   has been received.
 */

#define MYNAME_SIZE MPI_MAX_OBJECT_NAME + 50

/*
   Add a predefined MPI type to the tests.  _count instances of the
   type will be sent.
*/
#define SETUPBASICTYPE(_mpitype,_ctype,_count) { \
  int i; _ctype *a;	\
  if (cnt > *n) {*n = cnt; return; }			\
  types[cnt] = _mpitype; \
  inbufs[cnt] = (void *)calloc(_count,sizeof(_ctype)); \
  outbufs[cnt] = (void *)malloc(sizeof(_ctype) * (_count));	\
  a = (_ctype *)inbufs[cnt]; for (i=0; i<(_count); i++) a[i] = i;	\
  a = (_ctype *)outbufs[cnt]; for (i=0; i<(_count); i++) a[i] = 0;	\
  counts[cnt]  = _count; bytesize[cnt] = sizeof(_ctype) * (_count); cnt++; }

/*
   Add a contiguous version of a predefined type.  Send one instance of
   the type which contains _count copies of the predefined type.
 */
#define SETUPCONTIGTYPE(_mpitype,_ctype,_count) { \
  int i; _ctype *a; char*myname; \
  char _basename[MPI_MAX_OBJECT_NAME]; int _basenamelen;\
  if (cnt > *n) {*n = cnt; return; }\
  MPI_Type_contiguous(_count, _mpitype, types + cnt);\
  MPI_Type_commit(types + cnt);\
  inbufs[cnt] = (void *)calloc(_count, sizeof(_ctype)); \
  outbufs[cnt] = (void *)malloc(sizeof(_ctype) * (_count));	\
  a = (_ctype *)inbufs[cnt]; for (i=0; i<(_count); i++) a[i] = i;	\
  a = (_ctype *)outbufs[cnt]; for (i=0; i<(_count); i++) a[i] = 0;	\
  myname = (char *)malloc(MYNAME_SIZE);\
  MPI_Type_get_name(_mpitype, _basename, &_basenamelen); \
  snprintf(myname, MYNAME_SIZE, "Contig type %s", _basename);	\
  MPI_Type_set_name(types[cnt], myname); \
  free(myname); \
  counts[cnt]  = 1;  bytesize[cnt] = sizeof(_ctype) * (_count); cnt++; }

/*
  Create a vector with _count elements, separated by stride _stride,
  of _mpitype.  Each block has a single element.
 */
#define SETUPVECTORTYPE(_mpitype,_ctype,_count,_stride,_name) { \
  int i; _ctype *a; char *myname;				\
  char _basename[MPI_MAX_OBJECT_NAME]; int _basenamelen;\
  if (cnt > *n) {*n = cnt; return; }\
  MPI_Type_vector(_count, 1, _stride, _mpitype, types + cnt);	\
  MPI_Type_commit(types + cnt);\
  inbufs[cnt] = (void *)calloc(sizeof(_ctype) * (_count) * (_stride),1); \
  outbufs[cnt] = (void *)calloc(sizeof(_ctype) * (_count) * (_stride),1); \
  a = (_ctype *)inbufs[cnt]; for (i=0; i<(_count); i++) a[i*(_stride)] = i; \
  a = (_ctype *)outbufs[cnt]; for (i=0; i<(_count); i++) a[i*(_stride)] = 0; \
  myname = (char *)malloc(MYNAME_SIZE);\
  MPI_Type_get_name(_mpitype, _basename, &_basenamelen); \
  snprintf(myname, MYNAME_SIZE, "Vector type %s", _basename);		\
  MPI_Type_set_name(types[cnt], myname); \
  free(myname); \
  counts[cnt]  = 1; bytesize[cnt] = sizeof(_ctype) * (_count) * (_stride) ;\
  cnt++; }

/* This indexed type is setup like a contiguous type .
   Note that systems may try to convert this to contiguous, so we'll
   eventually need a test that has holes in it */
#define SETUPINDEXTYPE(_mpitype,_ctype,_count,_name) { \
  int i; int *lens, *disp; _ctype *a; char *myname;	\
  char _basename[MPI_MAX_OBJECT_NAME]; int _basenamelen;\
  if (cnt > *n) {*n = cnt; return; }\
  lens = (int *)malloc((_count) * sizeof(int)); \
  disp = (int *)malloc((_count) * sizeof(int)); \
  for (i=0; i<(_count); i++) { lens[i] = 1; disp[i] = i; } \
  MPI_Type_indexed((_count), lens, disp, _mpitype, types + cnt);\
  free(lens); free(disp); \
  MPI_Type_commit(types + cnt);\
  inbufs[cnt] = (void *)calloc((_count), sizeof(_ctype)); \
  outbufs[cnt] = (void *)malloc(sizeof(_ctype) * (_count)); \
  a = (_ctype *)inbufs[cnt]; for (i=0; i<(_count); i++) a[i] = i; \
  a = (_ctype *)outbufs[cnt]; for (i=0; i<(_count); i++) a[i] = 0; \
  myname = (char *)malloc(MYNAME_SIZE);\
  MPI_Type_get_name(_mpitype, _basename, &_basenamelen); \
  snprintf(myname, MYNAME_SIZE, "Index type %s", _basename);		\
  MPI_Type_set_name(types[cnt], myname); \
  free(myname); \
  counts[cnt]  = 1;  bytesize[cnt] = sizeof(_ctype) * (_count); cnt++; }

/* This defines a structure of two basic members; by choosing things like
   (char, double), various packing and alignment tests can be made */
#define SETUPSTRUCT2TYPE(_mpitype1,_ctype1,_mpitype2,_ctype2,_count,_tname) { \
  int i; char *myname;						\
  MPI_Datatype b[2], tmp_raw_type; int cnts[2]; \
  struct name { _ctype1 a1; _ctype2 a2; } *a, samp;	\
  MPI_Aint disp[2];				\
  if (cnt > *n) {*n = cnt; return; }					\
  b[0] = _mpitype1; b[1] = _mpitype2;	\
  cnts[0] = 1; cnts[1] = 1;	\
  MPI_Get_address(&(samp.a2), &disp[1]);		\
  MPI_Get_address(&(samp.a1), &disp[0]);		\
  disp[1] = disp[1] - disp[0]; disp[0] = 0; \
  MPI_Type_create_struct(2, cnts, disp, b, &tmp_raw_type);		\
  MPI_Type_create_resized(tmp_raw_type, 0, sizeof(samp), types + cnt); \
  MPI_Type_free(&tmp_raw_type); \
  MPI_Type_commit(types + cnt);					\
  inbufs[cnt] = (void *)calloc(sizeof(struct name) * (_count),1);	\
  outbufs[cnt] = (void *)calloc(sizeof(struct name) * (_count),1);	\
  a = (struct name *)inbufs[cnt]; for (i=0; i<(_count); i++) { a[i].a1 = i; \
      a[i].a2 = i; }							\
  a = (struct name *)outbufs[cnt]; for (i=0; i<(_count); i++) { a[i].a1 = 0; \
      a[i].a2 = 0; }							\
  myname = (char *)malloc(MYNAME_SIZE);					\
  snprintf(myname, MYNAME_SIZE, "Struct type %s", _tname);		\
  MPI_Type_set_name(types[cnt], myname); \
  free(myname); \
  counts[cnt]  = (_count);  bytesize[cnt] = sizeof(struct name) * (_count);cnt++; }

/* This accomplished the same effect as VECTOR, but allow a count of > 1 */
#define SETUPSTRUCTTYPEUB(_mpitype,_ctype,_count,_stride) {	\
  int i; _ctype *a; char *myname;					\
  char _basename[MPI_MAX_OBJECT_NAME]; int _basenamelen;\
  if (cnt > *n) {*n = cnt; return; }					\
  MPI_Type_create_resized(_mpitype, 0, (_stride) * sizeof(_ctype), types + cnt); \
  MPI_Type_commit(types + cnt);					\
  inbufs[cnt] = (void *)calloc(sizeof(_ctype) * (_count) * (_stride),1);\
  outbufs[cnt] = (void *)calloc(sizeof(_ctype) * (_count) * (_stride),1);\
  a = (_ctype *)inbufs[cnt]; for (i=0; i<(_count); i++) a[i*(_stride)] = i;  \
  a = (_ctype *)outbufs[cnt]; for (i=0; i<(_count); i++) a[i*(_stride)] = 0; \
  myname = (char *)malloc(MYNAME_SIZE);					\
  MPI_Type_get_name(_mpitype, _basename, &_basenamelen); \
  snprintf(myname, MYNAME_SIZE, "Struct (MPI_UB) type %s", _basename);	\
  MPI_Type_set_name(types[cnt], myname); \
  free(myname); \
  counts[cnt]  = (_count);  \
  bytesize[cnt] = sizeof(_ctype) * (_count) * (_stride);\
  cnt++; }

/*
 * Set whether only the basic types should be generated
 */
void MTestDatatype2BasicOnly(void)
{
    basic_only = 1;
}

static int nbasic_types = 0;
/* On input, n is the size of the various buffers.  On output,
   it is the number available types
 */
void MTestDatatype2Generate(MPI_Datatype * types, void **inbufs, void **outbufs,
                            int *counts, int *bytesize, int *n)
{
    int cnt = 0;                /* Number of defined types */
    int typecnt = 10;           /* Number of instances to send in most cases */
    int stride = 9;             /* Number of elements in vector to stride */

    /* First, generate an element of each basic type */
    SETUPBASICTYPE(MPI_CHAR, char, typecnt);
    SETUPBASICTYPE(MPI_SHORT, short, typecnt);
    SETUPBASICTYPE(MPI_INT, int, typecnt);
    SETUPBASICTYPE(MPI_LONG, long, typecnt);
    SETUPBASICTYPE(MPI_UNSIGNED_CHAR, unsigned char, typecnt);
    SETUPBASICTYPE(MPI_UNSIGNED_SHORT, unsigned short, typecnt);
    SETUPBASICTYPE(MPI_UNSIGNED, unsigned, typecnt);
    SETUPBASICTYPE(MPI_UNSIGNED_LONG, unsigned long, typecnt);
    SETUPBASICTYPE(MPI_FLOAT, float, typecnt);
    SETUPBASICTYPE(MPI_DOUBLE, double, typecnt);
    SETUPBASICTYPE(MPI_BYTE, char, typecnt);
#ifdef HAVE_LONG_LONG_INT
    SETUPBASICTYPE(MPI_LONG_LONG_INT, long long, typecnt);
#endif
#ifdef HAVE_LONG_DOUBLE
    SETUPBASICTYPE(MPI_LONG_DOUBLE, long double, typecnt);
#endif
    nbasic_types = cnt;

    if (basic_only) {
        *n = cnt;
        return;
    }
    /* Generate contiguous data items */
    SETUPCONTIGTYPE(MPI_CHAR, char, typecnt);
    SETUPCONTIGTYPE(MPI_SHORT, short, typecnt);
    SETUPCONTIGTYPE(MPI_INT, int, typecnt);
    SETUPCONTIGTYPE(MPI_LONG, long, typecnt);
    SETUPCONTIGTYPE(MPI_UNSIGNED_CHAR, unsigned char, typecnt);
    SETUPCONTIGTYPE(MPI_UNSIGNED_SHORT, unsigned short, typecnt);
    SETUPCONTIGTYPE(MPI_UNSIGNED, unsigned, typecnt);
    SETUPCONTIGTYPE(MPI_UNSIGNED_LONG, unsigned long, typecnt);
    SETUPCONTIGTYPE(MPI_FLOAT, float, typecnt);
    SETUPCONTIGTYPE(MPI_DOUBLE, double, typecnt);
    SETUPCONTIGTYPE(MPI_BYTE, char, typecnt);
#ifdef HAVE_LONG_LONG_INT
    SETUPCONTIGTYPE(MPI_LONG_LONG_INT, long long, typecnt);
#endif
#ifdef HAVE_LONG_DOUBLE
    SETUPCONTIGTYPE(MPI_LONG_DOUBLE, long double, typecnt);
#endif

    /* Generate vector items */
    SETUPVECTORTYPE(MPI_CHAR, char, typecnt, stride, "MPI_CHAR");
    SETUPVECTORTYPE(MPI_SHORT, short, typecnt, stride, "MPI_SHORT");
    SETUPVECTORTYPE(MPI_INT, int, typecnt, stride, "MPI_INT");
    SETUPVECTORTYPE(MPI_LONG, long, typecnt, stride, "MPI_LONG");
    SETUPVECTORTYPE(MPI_UNSIGNED_CHAR, unsigned char, typecnt, stride, "MPI_UNSIGNED_CHAR");
    SETUPVECTORTYPE(MPI_UNSIGNED_SHORT, unsigned short, typecnt, stride, "MPI_UNSIGNED_SHORT");
    SETUPVECTORTYPE(MPI_UNSIGNED, unsigned, typecnt, stride, "MPI_UNSIGNED");
    SETUPVECTORTYPE(MPI_UNSIGNED_LONG, unsigned long, typecnt, stride, "MPI_UNSIGNED_LONG");
    SETUPVECTORTYPE(MPI_FLOAT, float, typecnt, stride, "MPI_FLOAT");
    SETUPVECTORTYPE(MPI_DOUBLE, double, typecnt, stride, "MPI_DOUBLE");
    SETUPVECTORTYPE(MPI_BYTE, char, typecnt, stride, "MPI_BYTE");
#ifdef HAVE_LONG_LONG_INT
    SETUPVECTORTYPE(MPI_LONG_LONG_INT, long long, typecnt, stride, "MPI_LONG_LONG_INT");
#endif
#ifdef HAVE_LONG_DOUBLE
    SETUPVECTORTYPE(MPI_LONG_DOUBLE, long double, typecnt, stride, "MPI_LONG_DOUBLE");
#endif

    /* Generate indexed items */
    SETUPINDEXTYPE(MPI_CHAR, char, typecnt, "MPI_CHAR");
    SETUPINDEXTYPE(MPI_SHORT, short, typecnt, "MPI_SHORT");
    SETUPINDEXTYPE(MPI_INT, int, typecnt, "MPI_INT");
    SETUPINDEXTYPE(MPI_LONG, long, typecnt, "MPI_LONG");
    SETUPINDEXTYPE(MPI_UNSIGNED_CHAR, unsigned char, typecnt, "MPI_UNSIGNED_CHAR");
    SETUPINDEXTYPE(MPI_UNSIGNED_SHORT, unsigned short, typecnt, "MPI_UNSIGNED_SHORT");
    SETUPINDEXTYPE(MPI_UNSIGNED, unsigned, typecnt, "MPI_UNSIGNED");
    SETUPINDEXTYPE(MPI_UNSIGNED_LONG, unsigned long, typecnt, "MPI_UNSIGNED_LONG");
    SETUPINDEXTYPE(MPI_FLOAT, float, typecnt, "MPI_FLOAT");
    SETUPINDEXTYPE(MPI_DOUBLE, double, typecnt, "MPI_DOUBLE");
    SETUPINDEXTYPE(MPI_BYTE, char, typecnt, "MPI_BYTE");
#ifdef HAVE_LONG_LONG_INT
    SETUPINDEXTYPE(MPI_LONG_LONG_INT, long long, typecnt, "MPI_LONG_LONG_INT");
#endif
#ifdef HAVE_LONG_DOUBLE
    SETUPINDEXTYPE(MPI_LONG_DOUBLE, long double, typecnt, "MPI_LONG_DOUBLE");
#endif

    /* Generate struct items */
    SETUPSTRUCT2TYPE(MPI_CHAR, char, MPI_DOUBLE, double, typecnt, "char-double");
    SETUPSTRUCT2TYPE(MPI_DOUBLE, double, MPI_CHAR, char, typecnt, "double-char");
    SETUPSTRUCT2TYPE(MPI_UNSIGNED, unsigned, MPI_DOUBLE, double, typecnt, "unsigned-double");
    SETUPSTRUCT2TYPE(MPI_FLOAT, float, MPI_LONG, long, typecnt, "float-long");
    SETUPSTRUCT2TYPE(MPI_UNSIGNED_CHAR, unsigned char, MPI_CHAR, char, typecnt,
                     "unsigned char-char");
    SETUPSTRUCT2TYPE(MPI_UNSIGNED_SHORT, unsigned short, MPI_DOUBLE, double,
                     typecnt, "unsigned short-double");

    /* Generate struct using MPI_UB */
    SETUPSTRUCTTYPEUB(MPI_CHAR, char, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_SHORT, short, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_INT, int, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_LONG, long, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_UNSIGNED_CHAR, unsigned char, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_UNSIGNED_SHORT, unsigned short, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_UNSIGNED, unsigned, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_UNSIGNED_LONG, unsigned long, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_FLOAT, float, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_DOUBLE, double, typecnt, stride);
    SETUPSTRUCTTYPEUB(MPI_BYTE, char, typecnt, stride);

    /* 60 different entries to this point + 4 for long long and
     * 4 for long double */
    *n = cnt;
}

/*
   MAX_TEST should be 1 + actual max (allows us to check that it was,
   indeed, large enough)
 */
#define MAX_TEST 70
void MTestDatatype2Allocate(MPI_Datatype ** types, void ***inbufs,
                            void ***outbufs, int **counts, int **bytesize, int *n)
{
    *types = (MPI_Datatype *) malloc(MAX_TEST * sizeof(MPI_Datatype));
    *inbufs = (void **) malloc(MAX_TEST * sizeof(void *));
    *outbufs = (void **) malloc(MAX_TEST * sizeof(void *));
    *counts = (int *) malloc(MAX_TEST * sizeof(int));
    *bytesize = (int *) malloc(MAX_TEST * sizeof(int));
    *n = MAX_TEST;
}

int MTestDatatype2Check(void *inbuf, void *outbuf, int size_bytes)
{
    char *in = (char *) inbuf, *out = (char *) outbuf;
    int i;
    for (i = 0; i < size_bytes; i++) {
        if (in[i] != out[i]) {
            return i + 1;
        }
    }
    return 0;
}

/*
 * This is a version of CheckData that prints error messages
 */
int MTestDatatype2CheckAndPrint(void *inbuf, void *outbuf, int size_bytes,
                                char *typename, int typenum)
{
    int errloc, world_rank;

    if ((errloc = MTestDatatype2Check(inbuf, outbuf, size_bytes))) {
        char *p1, *p2;
        MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
        fprintf(stderr,
                "Error in data with type %s (type %d on %d) at byte %d of %d\n",
                typename, typenum, world_rank, errloc - 1, size_bytes);
        p1 = (char *) inbuf;
        p2 = (char *) outbuf;
        fprintf(stderr, "Got %x expected %x\n", p2[errloc - 1], p1[errloc - 1]);
    }
    return errloc;
}

void MTestDatatype2Free(MPI_Datatype * types, void **inbufs, void **outbufs,
                        int *counts, int *bytesize, int n)
{
    int i;
    for (i = 0; i < n; i++) {
        if (inbufs[i])
            free(inbufs[i]);
        if (outbufs[i])
            free(outbufs[i]);
        /* Only if not basic ... */
        if (i >= nbasic_types)
            MPI_Type_free(types + i);
    }
    free(types);
    free(inbufs);
    free(outbufs);
    free(counts);
    free(bytesize);
}