File: anympi.h

package info (click to toggle)
paraview 3.14.1-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 234,468 kB
  • sloc: cpp: 2,166,013; ansic: 801,575; xml: 58,068; tcl: 49,247; python: 43,091; java: 16,625; fortran: 12,224; sh: 11,722; yacc: 5,688; perl: 3,128; makefile: 2,228; lex: 1,311; lisp: 486; asm: 471; pascal: 228
file content (621 lines) | stat: -rw-r--r-- 18,864 bytes parent folder | download | duplicates (2)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#ifndef PyMPI_COMPAT_ANYMPI_H
#define PyMPI_COMPAT_ANYMPI_H

/* ---------------------------------------------------------------- */

#ifdef Py_PYTHON_H

#ifndef PyMPI_MALLOC
#define PyMPI_MALLOC PyMem_Malloc
#endif
#ifndef PyMPI_FREE
#define PyMPI_FREE PyMem_Free
#endif

#else

#include <stdlib.h>

#ifndef PyMPI_MALLOC
#define PyMPI_MALLOC malloc
#endif
#ifndef PyMPI_Free
#define PyMPI_Free free
#endif

#endif

/* ---------------------------------------------------------------- */

/* Version Number */

#ifdef PyMPI_MISSING_MPI_VERSION
#if !defined(MPI_VERSION)
#define MPI_VERSION 1
#endif
#endif

#ifdef PyMPI_MISSING_MPI_SUBVERSION
#if !defined(MPI_SUBVERSION)
#define MPI_SUBVERSION 0
#endif
#endif

#ifdef PyMPI_MISSING_MPI_GET_VERSION
#undef MPI_Get_version
static int PyMPI_Get_version(int *version, int* subversion)
{
  if (!version)    return MPI_ERR_ARG;
  if (!subversion) return MPI_ERR_ARG;
  *version    = MPI_VERSION;
  *subversion = MPI_SUBVERSION;
  return MPI_SUCCESS;
}
#define MPI_Get_version PyMPI_Get_version
#endif

/* ---------------------------------------------------------------- */

/* Threading Support */

#ifdef PyMPI_MISSING_MPI_INIT_THREAD
static int PyMPI_Init_thread(int *argc, char ***argv,
                             int required, int *provided)
{
  int ierr = MPI_SUCCESS;
  if (!provided) return MPI_ERR_ARG;
  ierr = MPI_Init(argc, argv);
  if (ierr != MPI_SUCCESS) return ierr;
  *provided = MPI_THREAD_SINGLE;
  return MPI_SUCCESS;
}
#undef  MPI_Init_thread
#define MPI_Init_thread PyMPI_Init_thread
#endif

#ifdef PyMPI_MISSING_MPI_QUERY_THREAD
static int PyMPI_Query_thread(int *provided)
{
  if (!provided) return MPI_ERR_ARG;
  provided = MPI_THREAD_SINGLE;
  return MPI_SUCCESS;
}
#undef  MPI_Query_thread
#define MPI_Query_thread PyMPI_Query_thread
#endif

#ifdef PyMPI_MISSING_MPI_IS_THREAD_MAIN
static int PyMPI_Is_thread_main(int *flag)
{
  if (!flag) return MPI_ERR_ARG;
  *flag = 1; /* XXX this is completely broken !! */
  return MPI_SUCCESS;
}
#undef  MPI_Is_thread_main
#define MPI_Is_thread_main PyMPI_Is_thread_main
#endif

/* ---------------------------------------------------------------- */

/* Status */

#ifdef PyMPI_MISSING_MPI_STATUS_IGNORE
static MPI_Status PyMPI_STATUS_IGNORE;
#undef MPI_STATUS_IGNORE
#define MPI_STATUS_IGNORE ((MPI_Status*)(&PyMPI_STATUS_IGNORE))
#endif

#ifdef PyMPI_MISSING_MPI_STATUSES_IGNORE
#ifndef PyMPI_MPI_STATUSES_IGNORE_SIZE
#define PyMPI_MPI_STATUSES_IGNORE_SIZE 4096
#warning "MPI_STATUSES_IGNORE will use static storage of size 4096"
#endif
static MPI_Status PyMPI_STATUSES_IGNORE[PyMPI_MPI_STATUSES_IGNORE_SIZE];
#undef  MPI_STATUSES_IGNORE
#define MPI_STATUSES_IGNORE ((MPI_Status*)(PyMPI_STATUSES_IGNORE))
#endif

/* ---------------------------------------------------------------- */

/* Datatypes */

#ifdef PyMPI_MISSING_MPI_TYPE_GET_EXTENT
static int PyMPI_Type_get_extent(MPI_Datatype datatype,
                                 MPI_Aint *lb, MPI_Aint *extent)
{
  int ierr = MPI_SUCCESS;
  ierr = MPI_Type_lb(datatype, lb);
  if (ierr != MPI_SUCCESS) return ierr;
  ierr = MPI_Type_extent(datatype, extent);
  if (ierr != MPI_SUCCESS) return ierr;
  return MPI_SUCCESS;
}
#undef  MPI_Type_get_extent
#define MPI_Type_get_extent PyMPI_Type_get_extent
#endif

#ifdef PyMPI_MISSING_MPI_TYPE_DUP
static int PyMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype)
{
  int ierr = MPI_SUCCESS;
  ierr = MPI_Type_contiguous(1, datatype, newtype);
  if (ierr != MPI_SUCCESS) return ierr;
  ierr = MPI_Type_commit(newtype); /* the safe way  ... */
  if (ierr != MPI_SUCCESS) return ierr;
  return MPI_SUCCESS;
}
#undef  MPI_Type_dup
#define MPI_Type_dup PyMPI_Type_dup
#endif

#ifdef PyMPI_MISSING_MPI_TYPE_CREATE_INDEXED_BLOCK
static int PyMPI_Type_create_indexed_block(int count,
                                           int blocklength,
                                           int displacements[],
                                           MPI_Datatype oldtype,
                                           MPI_Datatype *newtype)
{
  int i, *blocklengths = 0;
  int ierr = MPI_SUCCESS;
  if (count > 0) {
  blocklengths = (int *) PyMPI_MALLOC(count*sizeof(int));
  if (!blocklengths) return MPI_ERR_NO_MEM;
  }
  for (i=0; i<count; i++) blocklengths[i] = blocklength;
  ierr = MPI_Type_indexed(count,blocklengths,displacements,oldtype,newtype);
  if (blocklengths) PyMPI_FREE(blocklengths);
  return ierr;
}
#undef  MPI_Type_create_indexed_block
#define MPI_Type_create_indexed_block PyMPI_Type_create_indexed_block
#endif

/*
 * Adapted from the implementation in MPICH2 sources,
 * mpich2-1.0.7/src/mpi/datatype/type_create_subarray.c
 *
 */
#ifdef PyMPI_MISSING_MPI_TYPE_CREATE_SUBARRAY

#undef  PyMPI_CHKARG
#define PyMPI_CHKARG(EXPR) if (!(EXPR)) return MPI_ERR_ARG
#undef  PyMPI_CHKERR
#define PyMPI_CHKERR(IERR) if (IERR) return IERR
static int PyMPI_Type_create_subarray(int ndims,
                                      int sizes[],
                                      int subsizes[],
                                      int starts[],
                                      int order,
                                      MPI_Datatype oldtype,
                                      MPI_Datatype *newtype)
{
  int i = 0;
  MPI_Aint size = 0, extent = 0, disps[3];
  int blklens[3];
  MPI_Datatype tmp1, tmp2, types[3];
  int ierr = MPI_SUCCESS;
  tmp1 = tmp2 = types[0] = types[1] = types[2] = MPI_DATATYPE_NULL;

  PyMPI_CHKARG( ndims > 0 );
  PyMPI_CHKARG( sizes );
  PyMPI_CHKARG( subsizes );
  PyMPI_CHKARG( starts );
  PyMPI_CHKARG( newtype );
  for (i=0; i<ndims; i++) {
  PyMPI_CHKARG( sizes[i] >  0 );
  PyMPI_CHKARG( subsizes[i] >  0 );
  PyMPI_CHKARG( starts[i] >= 0 );
  PyMPI_CHKARG( sizes[i] >= subsizes[i] );
  PyMPI_CHKARG( starts[i] <= (sizes[i] - subsizes[i]) );
  }
  PyMPI_CHKARG( (order==MPI_ORDER_C) || (order==MPI_ORDER_FORTRAN) );

  ierr = MPI_Type_extent(oldtype, &extent); PyMPI_CHKERR(ierr);

  if (order == MPI_ORDER_FORTRAN) {
  if (ndims == 1)
    ierr = MPI_Type_contiguous(subsizes[0], oldtype, &tmp1); PyMPI_CHKERR(ierr);
  else {
  ierr = MPI_Type_vector(subsizes[1], subsizes[0], sizes[0],
                         oldtype, &tmp1); PyMPI_CHKERR(ierr);
  size = sizes[0]*extent;
  for (i=2; i<ndims; i++) {
  size *= sizes[i-1];
  ierr = MPI_Type_hvector(subsizes[i], 1, size,
                          tmp1, &tmp2); PyMPI_CHKERR(ierr);
  ierr = MPI_Type_free(&tmp1); PyMPI_CHKERR(ierr);
  tmp1 = tmp2;
  }
  }
  /* add displacement and UB */
  disps[1] = starts[0];
  size = 1;
  for (i=1; i<ndims; i++) {
  size *= sizes[i-1];
  disps[1] += size*starts[i];
  }
  } else /* MPI_ORDER_C */ {
  /* dimension ndims-1 changes fastest */
  if (ndims == 1) {
  ierr = MPI_Type_contiguous(subsizes[0], oldtype, &tmp1); PyMPI_CHKERR(ierr);
  } else {
  ierr = MPI_Type_vector(subsizes[ndims-2], subsizes[ndims-1], sizes[ndims-1],
                         oldtype, &tmp1); PyMPI_CHKERR(ierr);
  size = sizes[ndims-1]*extent;
  for (i=ndims-3; i>=0; i--) {
  size *= sizes[i+1];
  ierr = MPI_Type_hvector(subsizes[i], 1, size,
                          tmp1, &tmp2); PyMPI_CHKERR(ierr);
  ierr = MPI_Type_free(&tmp1); PyMPI_CHKERR(ierr);
  tmp1 = tmp2;
  }
  }
  /* add displacement and UB */
  disps[1] = starts[ndims-1];
  size = 1;
  for (i=ndims-2; i>=0; i--) {
  size *= sizes[i+1];
  disps[1] += size*starts[i];
  }
  }

  disps[1] *= extent;
  disps[2] = extent;
  for (i=0; i<ndims; i++) disps[2] *= sizes[i];
  disps[0] = 0;
  blklens[0] = blklens[1] = blklens[2] = 1;
  types[0] = MPI_LB;
  types[1] = tmp1;
  types[2] = MPI_UB;

  ierr = MPI_Type_struct(3,  blklens, disps, types, newtype); PyMPI_CHKERR(ierr);
  ierr = MPI_Type_free(&tmp1); PyMPI_CHKERR(ierr);

  return MPI_SUCCESS;
}
#undef PyMPI_CHKARG
#undef PyMPI_CHKERR

#undef  MPI_Type_create_subarray
#define MPI_Type_create_subarray PyMPI_Type_create_subarray
#endif

/*
 * Adapted from the implementation in MPICH2 sources,
 * mpich2-1.0.7/src/mpi/datatype/type_create_darray.c
 *
 */
#ifdef PyMPI_MISSING_MPI_TYPE_CREATE_DARRAY

#undef  PyMPI_MIN
#define PyMPI_MIN(__a, __b) (((__a) < (__b)) ? (__a) : (__b))
#undef  PyMPI_CHKARG
#define PyMPI_CHKARG(EXPR) if (!(EXPR)) return MPI_ERR_ARG
#undef  PyMPI_CHKERR
#define PyMPI_CHKERR(IERR) if (IERR) goto fn_exit
static int PyMPI_Type_block(int *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 *offset)
{
  int ierr, blksize, global_size, mysize, i, j;
  MPI_Aint stride;

  global_size = gsizes[dim];

  if (darg == MPI_DISTRIBUTE_DFLT_DARG)
    blksize = (global_size + nprocs - 1)/nprocs;
  else {
  blksize = darg;
  PyMPI_CHKARG(blksize > 0);
  PyMPI_CHKARG(blksize * nprocs >= global_size);
  }

  j = global_size - blksize*rank;
  mysize = PyMPI_MIN(blksize, j);
  if (mysize < 0) mysize = 0;
  stride = orig_extent;
  if (order == MPI_ORDER_FORTRAN) {
  if (dim == 0) {
  ierr = MPI_Type_contiguous(mysize, type_old, type_new); PyMPI_CHKERR(ierr);
  } else {
  for (i=0; i<dim; i++) stride *= gsizes[i];
  ierr = MPI_Type_hvector(mysize, 1, stride, type_old, type_new); PyMPI_CHKERR(ierr);
  }
  } else { /* order == MPI_ORDER_C */
  if (dim == ndims-1) {
  ierr = MPI_Type_contiguous(mysize, type_old, type_new); PyMPI_CHKERR(ierr);
  } else {
  for (i=ndims-1; i>dim; i--) stride *= gsizes[i];
  ierr = MPI_Type_hvector(mysize, 1, stride, type_old, type_new); PyMPI_CHKERR(ierr);
  }
  }

  *offset = blksize * rank;
  if (mysize == 0) *offset = 0;

  ierr = MPI_SUCCESS;
 fn_exit:
  return ierr;
}
static int PyMPI_Type_cyclic(int *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 *offset)
{
  int ierr, blksize, i, blklens[3], st_index, end_index,
    local_size, rem, count;
  MPI_Aint stride, disps[3];
  MPI_Datatype type_tmp, types[3];

  type_tmp = MPI_DATATYPE_NULL;
  types[0] = types[1] = types[2] = MPI_DATATYPE_NULL;

  if (darg == MPI_DISTRIBUTE_DFLT_DARG)
    blksize = 1;
  else
    blksize = darg;
  PyMPI_CHKARG(blksize > 0);

  st_index = rank*blksize;
  end_index = 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 += PyMPI_MIN(rem, blksize);
  }

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

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

  ierr = MPI_Type_hvector(count, blksize, stride, type_old, type_new);
  /* if the last block is of size less than blksize,
     include it separately using MPI_Type_struct */
  if (rem) {
  types[0] = *type_new;
  types[1] = type_old;
  disps[0] = 0;
  disps[1] = count*stride;
  blklens[0] = 1;
  blklens[1] = rem;
  ierr = MPI_Type_struct(2, blklens, disps, types, &type_tmp); PyMPI_CHKERR(ierr);
  ierr = MPI_Type_free(type_new); PyMPI_CHKERR(ierr);
  *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)) )
    {
    types[0] = MPI_LB;
    disps[0] = 0;
    types[1] = *type_new;
    disps[1] = rank * blksize * orig_extent;
    types[2] = MPI_UB;
    disps[2] = orig_extent * gsizes[dim];
    blklens[0] = blklens[1] = blklens[2] = 1;
    ierr = MPI_Type_struct(3, blklens, disps, types, &type_tmp); PyMPI_CHKERR(ierr);
    ierr = MPI_Type_free(type_new); PyMPI_CHKERR(ierr);
    *type_new = type_tmp;
    *offset = 0;
    } else {
  *offset = rank * blksize;
  }

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

  ierr = MPI_SUCCESS;
 fn_exit:
  return ierr;
}
static int PyMPI_Type_create_darray(int size,
                                    int rank,
                                    int ndims,
                                    int gsizes[],
                                    int distribs[],
                                    int dargs[],
                                    int psizes[],
                                    int order,
                                    MPI_Datatype oldtype,
                                    MPI_Datatype *newtype)
{
  int ierr = MPI_SUCCESS, i;
  int procs, tmp_rank, tmp_size, blklens[3];
  MPI_Aint orig_extent, disps[3];
  MPI_Datatype type_old, type_new, types[3];

  int      *coords  = 0;
  MPI_Aint *offsets = 0;

  orig_extent=0;
  type_old = type_new = MPI_DATATYPE_NULL;;
  types[0] = types[1] = types[2] = MPI_DATATYPE_NULL;

  ierr = MPI_Type_extent(oldtype, &orig_extent); PyMPI_CHKERR(ierr);

  PyMPI_CHKARG(rank     >= 0);
  PyMPI_CHKARG(size     >  0);
  PyMPI_CHKARG(ndims    >  0);
  PyMPI_CHKARG(gsizes   != 0);
  PyMPI_CHKARG(distribs != 0);
  PyMPI_CHKARG(dargs    != 0);
  PyMPI_CHKARG(psizes   != 0);
  PyMPI_CHKARG((order == MPI_ORDER_C) ||
               (order == MPI_ORDER_FORTRAN));
  for (i=0; ierr == MPI_SUCCESS && i < ndims; i++) {
  PyMPI_CHKARG(gsizes[1] >  0);
  PyMPI_CHKARG(psizes[1] >  0);
  PyMPI_CHKARG((distribs[i] == MPI_DISTRIBUTE_NONE)  ||
               (distribs[i] == MPI_DISTRIBUTE_BLOCK) ||
               (distribs[i] == MPI_DISTRIBUTE_CYCLIC));
  PyMPI_CHKARG((dargs[i] == MPI_DISTRIBUTE_DFLT_DARG) ||
               (dargs[i] > 0));
  PyMPI_CHKARG (!((distribs[i] == MPI_DISTRIBUTE_NONE) &&
                  (psizes[i] != 1)));
  }

  /* calculate position in Cartesian grid
     as MPI would (row-major ordering) */
  coords  = (int *) PyMPI_MALLOC(ndims*sizeof(int));
  if (coords  == 0) { ierr = MPI_ERR_INTERN; PyMPI_CHKERR(ierr); }
  offsets = (MPI_Aint *) PyMPI_MALLOC(ndims*sizeof(MPI_Aint));
  if (offsets == 0) { ierr = MPI_ERR_INTERN; PyMPI_CHKERR(ierr); }

  procs = size;
  tmp_rank = rank;
  for (i=0; i<ndims; i++) {
  procs = procs/psizes[i];
  coords[i] = tmp_rank/procs;
  tmp_rank = tmp_rank % procs;
  }

  type_old = oldtype;

  if (order == MPI_ORDER_FORTRAN) {
  /* dimension 0 changes fastest */
  for (i=0; i<ndims; i++) {
  if (distribs[i] == MPI_DISTRIBUTE_BLOCK) {
  ierr = PyMPI_Type_block(gsizes, i, ndims,
                          psizes[i], coords[i], dargs[i],
                          order, orig_extent,
                          type_old, &type_new, offsets+i); PyMPI_CHKERR(ierr);
  } else if (distribs[i] == MPI_DISTRIBUTE_CYCLIC) {
  ierr = PyMPI_Type_cyclic(gsizes, i, ndims,
                           psizes[i], coords[i], dargs[i],
                           order, orig_extent,
                           type_old, &type_new, offsets+i); PyMPI_CHKERR(ierr);
  } else if (distribs[i] == MPI_DISTRIBUTE_NONE) {
  /* treat it as a block distribution on 1 process */
  ierr = PyMPI_Type_block(gsizes, i, ndims,
                          1, 0,  MPI_DISTRIBUTE_DFLT_DARG,
                          order, orig_extent,
                          type_old, &type_new, offsets+i); PyMPI_CHKERR(ierr);
  }
  if (i) { ierr = MPI_Type_free(&type_old); PyMPI_CHKERR(ierr); }
  type_old = type_new;
  }
  /* add displacement and UB */
  disps[1] = offsets[0];
  tmp_size = 1;
  for (i=1; i<ndims; i++) {
  tmp_size *= gsizes[i-1];
  disps[1] += tmp_size*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--) {
  if (distribs[i] == MPI_DISTRIBUTE_BLOCK) {
  ierr = PyMPI_Type_block(gsizes, i, ndims,
                          psizes[i], coords[i], dargs[i],
                          order, orig_extent,
                          type_old, &type_new, offsets+i); PyMPI_CHKERR(ierr);
  } else if (distribs[i] == MPI_DISTRIBUTE_CYCLIC) {
  ierr = PyMPI_Type_cyclic(gsizes, i, ndims,
                           psizes[i], coords[i], dargs[i],
                           order,  orig_extent,
                           type_old, &type_new, offsets+i); PyMPI_CHKERR(ierr);
  } else if (distribs[i] == MPI_DISTRIBUTE_NONE) {
  /* treat it as a block distribution on 1 process */
  ierr = PyMPI_Type_block(gsizes, i, ndims,
                          psizes[i], coords[i], MPI_DISTRIBUTE_DFLT_DARG,
                          order, orig_extent,
                          type_old, &type_new, offsets+i); PyMPI_CHKERR(ierr);
  }
  if (i != ndims-1) { ierr = MPI_Type_free(&type_old); PyMPI_CHKERR(ierr); }
  type_old = type_new;
  }
  /* add displacement and UB */
  disps[1] = offsets[ndims-1];
  tmp_size = 1;
  for (i=ndims-2; i>=0; i--) {
  tmp_size *= gsizes[i+1];
  disps[1] += tmp_size*offsets[i];
  }
  /* rest done below for both Fortran and C order */
  }

  disps[1] *= orig_extent;
  disps[2] = orig_extent;
  for (i=0; i<ndims; i++) disps[2] *= gsizes[i];
  disps[0] = 0;
  blklens[0] = blklens[1] = blklens[2] = 1;
  types[0] = MPI_LB;
  types[1] = type_new;
  types[2] = MPI_UB;
  ierr = MPI_Type_struct(3, blklens, disps, types, newtype);PyMPI_CHKERR(ierr);
  ierr = MPI_Type_free(&type_new);PyMPI_CHKERR(ierr);

  ierr = MPI_SUCCESS;
 fn_exit:
  if (coords  != 0) PyMPI_FREE(coords);
  if (offsets != 0) PyMPI_FREE(offsets);
  return ierr;
}
#undef PyMPI_MIN
#undef PyMPI_CHKARG
#undef PyMPI_CHKERR

#undef  MPI_Type_create_darray
#define MPI_Type_create_darray PyMPI_Type_create_darray
#endif

/* ---------------------------------------------------------------- */

/* Memory Allocation */

#if (defined(PyMPI_MISSING_MPI_ALLOC_MEM) ||    \
     defined(PyMPI_MISSING_MPI_FREE_MEM))

static int PyMPI_Alloc_mem(MPI_Aint size, MPI_Info info, void *baseptr)
{
  char *buf = 0, **basebuf = 0;
  if (size < 0) return MPI_ERR_ARG;
  if (!baseptr) return MPI_ERR_ARG;
  if (size == 0) size = 1;
  buf = (char *) PyMPI_MALLOC(size);
  if (!buf) return MPI_ERR_NO_MEM;
  basebuf = (char **) baseptr;
  *basebuf = buf;
  return MPI_SUCCESS;
}
#undef  MPI_Alloc_mem
#define MPI_Alloc_mem PyMPI_Alloc_mem

static int PyMPI_Free_mem(void *baseptr)
{
  if (!baseptr) return MPI_ERR_ARG;
  PyMPI_FREE(baseptr);
  return MPI_SUCCESS;
}
#undef  MPI_Free_mem
#define MPI_Free_mem PyMPI_Free_mem

#endif

/* ---------------------------------------------------------------- */

#endif /* !PyMPI_COMPAT_ANYMPI_H */