This source file includes following definitions.
- block
- cyclic
- ompi_datatype_create_darray
   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 #include "ompi_config.h"
  27 
  28 #include <stddef.h>
  29 
  30 #include "ompi/datatype/ompi_datatype.h"
  31 
  32 static int
  33 block(const int *gsize_array, int dim, int ndims, int nprocs,
  34       int rank, int darg, int order, ptrdiff_t orig_extent,
  35       ompi_datatype_t *type_old, ompi_datatype_t **type_new,
  36       ptrdiff_t *st_offset)
  37 {
  38     int blksize, global_size, mysize, i, j, rc, start_loop, step;
  39     ptrdiff_t stride, disps[2];
  40 
  41     global_size = gsize_array[dim];
  42 
  43     if (darg == MPI_DISTRIBUTE_DFLT_DARG)
  44         blksize = (global_size + nprocs - 1) / nprocs;
  45     else {
  46         blksize = darg;
  47     }
  48 
  49     j = global_size - blksize*rank;
  50     mysize = blksize < j ? blksize : j;
  51     if (mysize < 0) mysize = 0;
  52 
  53     if (MPI_ORDER_C == order) {
  54         start_loop = ndims - 1 ; step = -1;
  55     } else {
  56         start_loop = 0 ; step = 1;
  57     }
  58 
  59     stride = orig_extent;
  60     if (dim == start_loop) {
  61         rc = ompi_datatype_create_contiguous(mysize, type_old, type_new);
  62         if (OMPI_SUCCESS != rc) return rc;
  63     } else {
  64         for (i = start_loop ; i != dim ; i += step) {
  65             stride *= gsize_array[i];
  66         }
  67         rc = ompi_datatype_create_hvector(mysize, 1, stride, type_old, type_new);
  68         if (OMPI_SUCCESS != rc) return rc;
  69     }
  70 
  71     *st_offset = blksize * rank;
  72     
  73     if (mysize == 0) *st_offset = 0;
  74 
  75     
  76     disps[0] = 0;         disps[1] = orig_extent;
  77     if (order == MPI_ORDER_FORTRAN) {
  78         for(i=0; i<=dim; i++) {
  79             disps[1] *= gsize_array[i];
  80         }
  81     } else {
  82         for(i=ndims-1; i>=dim; i--) {
  83             disps[1] *= gsize_array[i];
  84         }
  85     }
  86     rc = opal_datatype_resize( &(*type_new)->super, disps[0], disps[1] );
  87     if (OMPI_SUCCESS != rc) return rc;
  88 
  89     return OMPI_SUCCESS;
  90 }
  91 
  92 
  93 static int
  94 cyclic(const int *gsize_array, int dim, int ndims, int nprocs,
  95        int rank, int darg, int order, ptrdiff_t orig_extent,
  96        ompi_datatype_t* type_old, ompi_datatype_t **type_new,
  97        ptrdiff_t *st_offset)
  98 {
  99     int blksize, i, blklens[2], st_index, end_index, local_size, rem, count, rc;
 100     ptrdiff_t stride, disps[2];
 101     ompi_datatype_t *type_tmp, *types[2];
 102 
 103     if (darg == MPI_DISTRIBUTE_DFLT_DARG) {
 104         blksize = 1;
 105     } else {
 106         blksize = darg;
 107     }
 108 
 109     st_index = rank * blksize;
 110     end_index = gsize_array[dim] - 1;
 111 
 112     if (end_index < st_index) {
 113         local_size = 0;
 114     } else {
 115         local_size = ((end_index - st_index + 1)/(nprocs*blksize))*blksize;
 116         rem = (end_index - st_index + 1) % (nprocs*blksize);
 117         local_size += rem < blksize ? rem : blksize;
 118     }
 119 
 120     count = local_size / blksize;
 121     rem = local_size % blksize;
 122 
 123     stride = nprocs*blksize*orig_extent;
 124     if (order == MPI_ORDER_FORTRAN) {
 125         for (i=0; i<dim; i++) {
 126             stride *= gsize_array[i];
 127         }
 128     } else {
 129         for (i=ndims-1; i>dim; i--) {
 130             stride *= gsize_array[i];
 131         }
 132     }
 133 
 134     rc = ompi_datatype_create_hvector(count, blksize, stride, type_old, type_new);
 135     if (OMPI_SUCCESS != rc) return rc;
 136 
 137     if (rem) {
 138         
 139 
 140 
 141         types  [0] = *type_new; types  [1] = type_old;
 142         disps  [0] = 0;         disps  [1] = count*stride;
 143         blklens[0] = 1;         blklens[1] = rem;
 144 
 145         rc = ompi_datatype_create_struct(2, blklens, disps, types, &type_tmp);
 146         ompi_datatype_destroy(type_new);
 147         
 148 
 149         if (OMPI_SUCCESS != rc) return rc;
 150         *type_new = type_tmp;
 151     }
 152 
 153     
 154     disps[0] = 0;         disps[1] = orig_extent;
 155     if (order == MPI_ORDER_FORTRAN) {
 156         for(i=0; i<=dim; i++) {
 157             disps[1] *= gsize_array[i];
 158         }
 159     } else {
 160         for(i=ndims-1; i>=dim; i--) {
 161             disps[1] *= gsize_array[i];
 162         }
 163     }
 164     rc = opal_datatype_resize( &(*type_new)->super, disps[0], disps[1] );
 165     if (OMPI_SUCCESS != rc) return rc;
 166 
 167     *st_offset = rank * blksize;
 168     
 169     if (local_size == 0) *st_offset = 0;
 170 
 171     return OMPI_SUCCESS;
 172 }
 173 
 174 int32_t ompi_datatype_create_darray(int size,
 175                                     int rank,
 176                                     int ndims,
 177                                     int const* gsize_array,
 178                                     int const* distrib_array,
 179                                     int const* darg_array,
 180                                     int const* psize_array,
 181                                     int order,
 182                                     const ompi_datatype_t* oldtype,
 183                                     ompi_datatype_t** newtype)
 184 {
 185     ompi_datatype_t *lastType;
 186     ptrdiff_t orig_extent, *st_offsets = NULL;
 187     int i, start_loop, end_loop, step;
 188     int *coords = NULL, rc = OMPI_SUCCESS;
 189     ptrdiff_t displs[2], tmp_size = 1;
 190 
 191     
 192     if (ndims < 1) {
 193         
 194 
 195         return ompi_datatype_duplicate( &ompi_mpi_datatype_null.dt, newtype);
 196     }
 197 
 198     rc = ompi_datatype_type_extent(oldtype, &orig_extent);
 199     if (MPI_SUCCESS != rc) goto cleanup;
 200 
 201     
 202     {
 203         int tmp_rank = rank, procs = size;
 204 
 205         coords = (int *) malloc(ndims * sizeof(int));
 206         displs[1] = orig_extent;
 207         for (i = 0 ; i < ndims ; i++) {
 208             procs = procs / psize_array[i];
 209             coords[i] = tmp_rank / procs;
 210             tmp_rank = tmp_rank % procs;
 211             
 212             displs[1] *= gsize_array[i];
 213         }
 214     }
 215 
 216     st_offsets = (ptrdiff_t *) malloc(ndims * sizeof(ptrdiff_t));
 217 
 218     
 219 
 220 
 221     rc = ompi_datatype_duplicate(oldtype, &lastType);
 222     if (OMPI_SUCCESS != rc) goto cleanup;
 223 
 224     
 225     if (MPI_ORDER_C == order) {
 226         start_loop = ndims - 1 ; step = -1; end_loop = -1;
 227     } else {
 228         start_loop = 0 ; step = 1; end_loop = ndims;
 229     }
 230 
 231     
 232     for (i = start_loop; i != end_loop; i += step) {
 233         int nprocs, tmp_rank;
 234 
 235         switch(distrib_array[i]) {
 236         case MPI_DISTRIBUTE_BLOCK:
 237             rc = block(gsize_array, i, ndims, psize_array[i], coords[i],
 238                        darg_array[i], order, orig_extent,
 239                        lastType, newtype, st_offsets+i);
 240             break;
 241         case MPI_DISTRIBUTE_CYCLIC:
 242             rc = cyclic(gsize_array, i, ndims, psize_array[i], coords[i],
 243                         darg_array[i], order, orig_extent,
 244                         lastType, newtype, st_offsets+i);
 245             break;
 246         case MPI_DISTRIBUTE_NONE:
 247             
 248             if (order == MPI_ORDER_C) {
 249                 nprocs = psize_array[i]; tmp_rank = coords[i];
 250             } else {
 251                 nprocs = 1; tmp_rank = 0;
 252             }
 253 
 254             rc = block(gsize_array, i, ndims, nprocs, tmp_rank,
 255                        MPI_DISTRIBUTE_DFLT_DARG, order, orig_extent,
 256                        lastType, newtype, st_offsets+i);
 257             break;
 258         default:
 259             rc = MPI_ERR_ARG;
 260         }
 261         ompi_datatype_destroy(&lastType);
 262         
 263 
 264         if (MPI_SUCCESS != rc) goto cleanup;
 265         lastType = *newtype;
 266     }
 267 
 268     
 269 
 270 
 271 
 272 
 273 
 274 
 275 
 276     displs[0] = st_offsets[start_loop];
 277     for (i = start_loop + step; i != end_loop; i += step) {
 278         tmp_size *= gsize_array[i - step];
 279         displs[0] += tmp_size * st_offsets[i];
 280     }
 281     displs[0] *= orig_extent;
 282 
 283     *newtype = ompi_datatype_create(lastType->super.desc.used);
 284     rc = ompi_datatype_add(*newtype, lastType, 1, displs[0], displs[1]);
 285     ompi_datatype_destroy(&lastType);
 286     
 287 
 288     if (MPI_SUCCESS != rc) {
 289         ompi_datatype_destroy (newtype);
 290     } else {
 291         (void) opal_datatype_resize( &(*newtype)->super, 0, displs[1]);
 292     }
 293 
 294  cleanup:
 295     free(st_offsets);
 296     free(coords);
 297     return rc;
 298 }