File: ad_darray.c

package info (click to toggle)
openmpi 5.0.8-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,692 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (310 lines) | stat: -rw-r--r-- 10,933 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "adio.h"
#include "adio_extern.h"

static int MPIOI_Type_block(int *array_of_gsizes, int dim, int ndims, int nprocs,
                            int rank, int darg, int order, MPI_Aint orig_extent,
                            MPI_Datatype type_old, MPI_Datatype * type_new, MPI_Aint * st_offset);
static int MPIOI_Type_cyclic(int *array_of_gsizes, int dim, int ndims, int nprocs,
                             int rank, int darg, int order, MPI_Aint orig_extent,
                             MPI_Datatype type_old, MPI_Datatype * type_new, MPI_Aint * st_offset);


int ADIO_Type_create_darray(int size, int rank, int ndims,
                            int *array_of_gsizes, int *array_of_distribs,
                            int *array_of_dargs, int *array_of_psizes,
                            int order, MPI_Datatype oldtype, MPI_Datatype * newtype)
{
    MPI_Datatype type_old, type_new = MPI_DATATYPE_NULL, types[1];
    int procs, tmp_rank, i, tmp_size, blklens[1], *coords;
    MPI_Aint *st_offsets, lb, ub, orig_extent, disps[1];

    MPI_Type_get_extent(oldtype, &lb, &orig_extent);

/* calculate position in Cartesian grid as MPI would (row-major
   ordering) */
    coords = (int *) ADIOI_Malloc(ndims * sizeof(int));
    procs = size;
    tmp_rank = rank;
    for (i = 0; i < ndims; i++) {
        procs = procs / array_of_psizes[i];
        coords[i] = tmp_rank / procs;
        tmp_rank = tmp_rank % procs;
    }

    st_offsets = (MPI_Aint *) ADIOI_Malloc(ndims * sizeof(MPI_Aint));
    type_old = oldtype;

    if (order == MPI_ORDER_FORTRAN) {
        /* dimension 0 changes fastest */
        for (i = 0; i < ndims; i++) {
            switch (array_of_distribs[i]) {
                case MPI_DISTRIBUTE_BLOCK:
                    MPIOI_Type_block(array_of_gsizes, i, ndims,
                                     array_of_psizes[i],
                                     coords[i], array_of_dargs[i],
                                     order, orig_extent, type_old, &type_new, st_offsets + i);
                    break;
                case MPI_DISTRIBUTE_CYCLIC:
                    MPIOI_Type_cyclic(array_of_gsizes, i, ndims,
                                      array_of_psizes[i], coords[i],
                                      array_of_dargs[i], order,
                                      orig_extent, type_old, &type_new, st_offsets + i);
                    break;
                case MPI_DISTRIBUTE_NONE:
                    /* treat it as a block distribution on 1 process */
                    MPIOI_Type_block(array_of_gsizes, i, ndims, 1, 0,
                                     MPI_DISTRIBUTE_DFLT_DARG, order,
                                     orig_extent, type_old, &type_new, st_offsets + i);
                    break;
            }
            if (i)
                MPI_Type_free(&type_old);
            type_old = type_new;
        }

        /* add displacement and UB */
        disps[0] = st_offsets[0];
        tmp_size = 1;
        for (i = 1; i < ndims; i++) {
            tmp_size *= array_of_gsizes[i - 1];
            disps[0] += (MPI_Aint) tmp_size *st_offsets[i];
        }
        /* rest done below for both Fortran and C order */
    }

    else {      /* order == MPI_ORDER_C */

        /* dimension ndims-1 changes fastest */
        for (i = ndims - 1; i >= 0; i--) {
            switch (array_of_distribs[i]) {
                case MPI_DISTRIBUTE_BLOCK:
                    MPIOI_Type_block(array_of_gsizes, i, ndims, array_of_psizes[i],
                                     coords[i], array_of_dargs[i], order,
                                     orig_extent, type_old, &type_new, st_offsets + i);
                    break;
                case MPI_DISTRIBUTE_CYCLIC:
                    MPIOI_Type_cyclic(array_of_gsizes, i, ndims,
                                      array_of_psizes[i], coords[i],
                                      array_of_dargs[i], order,
                                      orig_extent, type_old, &type_new, st_offsets + i);
                    break;
                case MPI_DISTRIBUTE_NONE:
                    /* treat it as a block distribution on 1 process */
                    MPIOI_Type_block(array_of_gsizes, i, ndims, array_of_psizes[i],
                                     coords[i], MPI_DISTRIBUTE_DFLT_DARG, order, orig_extent,
                                     type_old, &type_new, st_offsets + i);
                    break;
            }
            if (i != ndims - 1)
                MPI_Type_free(&type_old);
            type_old = type_new;
        }

        /* add displacement and UB */
        disps[0] = st_offsets[ndims - 1];
        tmp_size = 1;
        for (i = ndims - 2; i >= 0; i--) {
            tmp_size *= array_of_gsizes[i + 1];
            disps[0] += (MPI_Aint) tmp_size *st_offsets[i];
        }
    }

    disps[0] *= orig_extent;

    lb = 0;
    ub = orig_extent;
    for (i = 0; i < ndims; i++)
        ub *= (MPI_Aint) array_of_gsizes[i];

    blklens[0] = 1;
    types[0] = type_new;

    MPI_Type_create_struct(1, blklens, disps, types, &type_old);
    MPI_Type_create_resized(type_old, lb, ub, newtype);

    MPI_Type_free(&type_old);
    MPI_Type_free(&type_new);

    ADIOI_Free(st_offsets);
    ADIOI_Free(coords);
    return MPI_SUCCESS;
}


/* Returns MPI_SUCCESS on success, an MPI error code on failure.  Code above
 * needs to call MPIO_Err_return_xxx.
 */
static int MPIOI_Type_block(int *array_of_gsizes, int dim, int ndims, int nprocs,
                            int rank, int darg, int order, MPI_Aint orig_extent,
                            MPI_Datatype type_old, MPI_Datatype * type_new, MPI_Aint * st_offset)
{
/* nprocs = no. of processes in dimension dim of grid
   rank = coordinate of this process in dimension dim */
    int blksize, global_size, mysize, i, j;
    MPI_Aint stride;

    global_size = array_of_gsizes[dim];

    if (darg == MPI_DISTRIBUTE_DFLT_DARG)
        blksize = (global_size + nprocs - 1) / nprocs;
    else {
        blksize = darg;

        /* --BEGIN ERROR HANDLING-- */
        if (blksize <= 0) {
            return MPI_ERR_ARG;
        }

        if (blksize * nprocs < global_size) {
            return MPI_ERR_ARG;
        }
        /* --END ERROR HANDLING-- */
    }

    j = global_size - blksize * rank;
    mysize = MPL_MIN(blksize, j);
    if (mysize < 0)
        mysize = 0;

    stride = orig_extent;
    if (order == MPI_ORDER_FORTRAN) {
        if (dim == 0)
            MPI_Type_contiguous(mysize, type_old, type_new);
        else {
            for (i = 0; i < dim; i++)
                stride *= (MPI_Aint) array_of_gsizes[i];
            MPI_Type_create_hvector(mysize, 1, stride, type_old, type_new);
        }
    } else {
        if (dim == ndims - 1)
            MPI_Type_contiguous(mysize, type_old, type_new);
        else {
            for (i = ndims - 1; i > dim; i--)
                stride *= (MPI_Aint) array_of_gsizes[i];
            MPI_Type_create_hvector(mysize, 1, stride, type_old, type_new);
        }

    }

    *st_offset = (MPI_Aint) blksize *(MPI_Aint) rank;
    /* in terms of no. of elements of type oldtype in this dimension */
    if (mysize == 0)
        *st_offset = 0;

    MPI_Aint lb, ex;
    MPI_Type_get_extent(type_old, &lb, &ex);
    MPI_Datatype type_tmp;
    MPI_Type_create_resized(*type_new, 0, array_of_gsizes[dim] * ex, &type_tmp);
    MPI_Type_free(type_new);
    *type_new = type_tmp;

    return MPI_SUCCESS;
}


/* Returns MPI_SUCCESS on success, an MPI error code on failure.  Code above
 * needs to call MPIO_Err_return_xxx.
 */
static int MPIOI_Type_cyclic(int *array_of_gsizes, int dim, int ndims, int nprocs,
                             int rank, int darg, int order, MPI_Aint orig_extent,
                             MPI_Datatype type_old, MPI_Datatype * type_new, MPI_Aint * st_offset)
{
/* nprocs = no. of processes in dimension dim of grid
   rank = coordinate of this process in dimension dim */
    int blksize, i, blklens[3], st_index, end_index, local_size, rem, count;
    MPI_Aint stride, disps[3];
    MPI_Datatype type_tmp, types[3];

    if (darg == MPI_DISTRIBUTE_DFLT_DARG)
        blksize = 1;
    else
        blksize = darg;

    /* --BEGIN ERROR HANDLING-- */
    if (blksize <= 0) {
        return MPI_ERR_ARG;
    }
    /* --END ERROR HANDLING-- */

    st_index = rank * blksize;
    end_index = array_of_gsizes[dim] - 1;

    if (end_index < st_index)
        local_size = 0;
    else {
        local_size = ((end_index - st_index + 1) / (nprocs * blksize)) * blksize;
        rem = (end_index - st_index + 1) % (nprocs * blksize);
        local_size += MPL_MIN(rem, blksize);
    }

    count = local_size / blksize;
    rem = local_size % blksize;

    stride = (MPI_Aint) nprocs *(MPI_Aint) blksize *orig_extent;
    if (order == MPI_ORDER_FORTRAN)
        for (i = 0; i < dim; i++)
            stride *= (MPI_Aint) array_of_gsizes[i];
    else
        for (i = ndims - 1; i > dim; i--)
            stride *= (MPI_Aint) array_of_gsizes[i];

    MPI_Type_create_hvector(count, blksize, stride, type_old, type_new);

    if (rem) {
        /* if the last block is of size less than blksize, include
         * it separately using MPI_Type_create_struct */

        types[0] = *type_new;
        types[1] = type_old;
        disps[0] = 0;
        disps[1] = (MPI_Aint) count *stride;
        blklens[0] = 1;
        blklens[1] = rem;

        MPI_Type_create_struct(2, blklens, disps, types, &type_tmp);

        MPI_Type_free(type_new);
        *type_new = type_tmp;
    }

    /* In the first iteration, we need to set the displacement in that
     * dimension correctly. */
    if (((order == MPI_ORDER_FORTRAN) && (dim == 0)) ||
        ((order == MPI_ORDER_C) && (dim == ndims - 1))) {
        MPI_Datatype tmp;
        MPI_Aint lb, ub;
        types[0] = *type_new;
        disps[0] = (MPI_Aint) rank *(MPI_Aint) blksize *orig_extent;
        lb = 0;
        ub = orig_extent * (MPI_Aint) array_of_gsizes[dim];
        blklens[0] = 1;
        MPI_Type_create_struct(1, blklens, disps, types, &tmp);
        MPI_Type_create_resized(tmp, lb, ub, &type_tmp);
        MPI_Type_free(&tmp);
        MPI_Type_free(type_new);
        *type_new = type_tmp;

        *st_offset = 0; /* set it to 0 because it is taken care of in
                         * the struct above */
    } else {
        *st_offset = (MPI_Aint) rank *(MPI_Aint) blksize;
        /* st_offset is in terms of no. of elements of type oldtype in
         * this dimension */
    }

    if (local_size == 0)
        *st_offset = 0;

    MPI_Aint lb, ex;
    MPI_Type_get_extent(type_old, &lb, &ex);
    MPI_Type_create_resized(*type_new, 0, array_of_gsizes[dim] * ex, &type_tmp);
    MPI_Type_free(type_new);
    *type_new = type_tmp;

    return MPI_SUCCESS;
}