1 /*
2 * Copyright (C) by Argonne National Laboratory
3 * See COPYRIGHT in top-level directory
4 */
5
6 #include <mpiimpl.h>
7 #include <stdlib.h>
8 #include "datatype.h"
9
10 #undef MPID_TYPE_ALLOC_DEBUG
11
12 /*@
13 MPIR_Type_blockindexed - create a block indexed datatype
14
15 Input Parameters:
16 + count - number of blocks in type
17 . blocklength - number of elements in each block
18 . displacement_array - offsets of blocks from start of type (see next
19 parameter for units)
20 . dispinbytes - if nonzero, then displacements are in bytes, otherwise
21 they in terms of extent of oldtype
22 - oldtype - type (using handle) of datatype on which new type is based
23
24 Output Parameters:
25 . newtype - handle of new block indexed datatype
26
27 Return Value:
28 MPI_SUCCESS on success, MPI error on failure.
29 @*/
MPIR_Type_blockindexed(int count,int blocklength,const void * displacement_array,int dispinbytes,MPI_Datatype oldtype,MPI_Datatype * newtype)30 int MPIR_Type_blockindexed(int count,
31 int blocklength,
32 const void *displacement_array,
33 int dispinbytes, MPI_Datatype oldtype, MPI_Datatype * newtype)
34 {
35 int mpi_errno = MPI_SUCCESS, i;
36 int old_is_contig;
37 MPI_Aint contig_count;
38 MPI_Aint old_lb, old_ub, old_extent, old_true_lb, old_true_ub;
39 MPI_Aint min_lb = 0, max_ub = 0, eff_disp;
40
41 MPIR_Datatype *new_dtp;
42
43 if (count == 0)
44 return MPII_Type_zerolen(newtype);
45
46 /* allocate new datatype object and handle */
47 new_dtp = (MPIR_Datatype *) MPIR_Handle_obj_alloc(&MPIR_Datatype_mem);
48 /* --BEGIN ERROR HANDLING-- */
49 if (!new_dtp) {
50 mpi_errno = MPIR_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
51 "MPIR_Type_vector", __LINE__, MPI_ERR_OTHER, "**nomem", 0);
52 return mpi_errno;
53 }
54 /* --END ERROR HANDLING-- */
55
56 /* handle is filled in by MPIR_Handle_obj_alloc() */
57 MPIR_Object_set_ref(new_dtp, 1);
58 new_dtp->is_committed = 0;
59 new_dtp->attributes = NULL;
60 new_dtp->name[0] = 0;
61 new_dtp->contents = NULL;
62 new_dtp->flattened = NULL;
63
64 new_dtp->typerep.handle = NULL;
65
66 if (HANDLE_IS_BUILTIN(oldtype)) {
67 MPI_Aint el_sz = (MPI_Aint) MPIR_Datatype_get_basic_size(oldtype);
68
69 old_lb = 0;
70 old_true_lb = 0;
71 old_ub = el_sz;
72 old_true_ub = el_sz;
73 old_extent = el_sz;
74 old_is_contig = 1;
75
76 new_dtp->size = (MPI_Aint) count *(MPI_Aint) blocklength *el_sz;
77
78 new_dtp->alignsize = el_sz; /* ??? */
79 new_dtp->n_builtin_elements = count * blocklength;
80 new_dtp->builtin_element_size = el_sz;
81 new_dtp->basic_type = oldtype;
82 } else {
83 /* user-defined base type (oldtype) */
84 MPIR_Datatype *old_dtp;
85
86 MPIR_Datatype_get_ptr(oldtype, old_dtp);
87
88 old_lb = old_dtp->lb;
89 old_true_lb = old_dtp->true_lb;
90 old_ub = old_dtp->ub;
91 old_true_ub = old_dtp->true_ub;
92 old_extent = old_dtp->extent;
93 MPIR_Datatype_is_contig(oldtype, &old_is_contig);
94
95 new_dtp->size = (MPI_Aint) count *(MPI_Aint) blocklength *(MPI_Aint) old_dtp->size;
96
97 new_dtp->alignsize = old_dtp->alignsize;
98 new_dtp->n_builtin_elements = count * blocklength * old_dtp->n_builtin_elements;
99 new_dtp->builtin_element_size = old_dtp->builtin_element_size;
100 new_dtp->basic_type = old_dtp->basic_type;
101 }
102
103 /* priming for loop */
104 eff_disp = (dispinbytes) ? ((MPI_Aint *) displacement_array)[0] :
105 (((MPI_Aint) ((int *) displacement_array)[0]) * old_extent);
106 MPII_DATATYPE_BLOCK_LB_UB((MPI_Aint) blocklength,
107 eff_disp, old_lb, old_ub, old_extent, min_lb, max_ub);
108
109 /* determine new min lb and max ub */
110 for (i = 1; i < count; i++) {
111 MPI_Aint tmp_lb, tmp_ub;
112
113 eff_disp = (dispinbytes) ? ((MPI_Aint *) displacement_array)[i] :
114 (((MPI_Aint) ((int *) displacement_array)[i]) * old_extent);
115 MPII_DATATYPE_BLOCK_LB_UB((MPI_Aint) blocklength,
116 eff_disp, old_lb, old_ub, old_extent, tmp_lb, tmp_ub);
117
118 if (tmp_lb < min_lb)
119 min_lb = tmp_lb;
120 if (tmp_ub > max_ub)
121 max_ub = tmp_ub;
122 }
123
124 new_dtp->lb = min_lb;
125 new_dtp->ub = max_ub;
126 new_dtp->true_lb = min_lb + (old_true_lb - old_lb);
127 new_dtp->true_ub = max_ub + (old_true_ub - old_ub);
128 new_dtp->extent = max_ub - min_lb;
129
130 /* new type is contig for N types if it is all one big block,
131 * its size and extent are the same, and the old type was also
132 * contiguous.
133 */
134 new_dtp->is_contig = 0;
135 if (old_is_contig) {
136 contig_count = MPII_Datatype_blockindexed_count_contig(count,
137 blocklength,
138 displacement_array,
139 dispinbytes, old_extent);
140 if ((contig_count == 1) && ((MPI_Aint) new_dtp->size == new_dtp->extent)) {
141 new_dtp->is_contig = 1;
142 }
143 }
144
145 if (dispinbytes) {
146 mpi_errno = MPIR_Typerep_create_hindexed_block(count, blocklength, displacement_array,
147 oldtype, new_dtp);
148 MPIR_ERR_CHECK(mpi_errno);
149 } else {
150 mpi_errno = MPIR_Typerep_create_indexed_block(count, blocklength, displacement_array,
151 oldtype, new_dtp);
152 MPIR_ERR_CHECK(mpi_errno);
153 }
154
155 *newtype = new_dtp->handle;
156
157 fn_exit:
158 return mpi_errno;
159 fn_fail:
160 goto fn_exit;
161 }
162