1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "smpi_datatype_derived.hpp"
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
11 /* PMPI User level calls */
13 int PMPI_Type_free(MPI_Datatype * datatype)
15 /* Free a predefined datatype is an error according to the standard, and should be checked for */
16 if (*datatype == MPI_DATATYPE_NULL || (*datatype)->flags() & DT_FLAG_PREDEFINED) {
19 simgrid::smpi::Datatype::unref(*datatype);
20 *datatype=MPI_DATATYPE_NULL;
25 int PMPI_Type_size(MPI_Datatype datatype, int *size)
27 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
28 CHECK_NULL(2, MPI_ERR_ARG, size)
29 *size = static_cast<int>(datatype->size());
33 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
35 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
36 CHECK_NULL(2, MPI_ERR_ARG, size)
37 *size = static_cast<MPI_Count>(datatype->size());
41 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
43 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
44 CHECK_NULL(2, MPI_ERR_ARG, lb)
45 CHECK_NULL(3, MPI_ERR_ARG, extent)
46 return datatype->extent(lb, extent);
49 int PMPI_Type_get_extent_x(MPI_Datatype datatype, MPI_Count * lb, MPI_Count * extent)
53 int ret = PMPI_Type_get_extent(datatype, &tmplb, &tmpext);
54 if(ret == MPI_SUCCESS){
55 *lb = static_cast<MPI_Count>(tmplb);
56 *extent = static_cast<MPI_Count>(tmpext);
61 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
63 return PMPI_Type_get_extent(datatype, lb, extent);
66 int PMPI_Type_get_true_extent_x(MPI_Datatype datatype, MPI_Count * lb, MPI_Count * extent)
68 return PMPI_Type_get_extent_x(datatype, lb, extent);
71 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
73 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
74 CHECK_NULL(2, MPI_ERR_ARG, extent)
75 *extent = datatype->get_extent();
79 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
81 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
82 CHECK_NULL(2, MPI_ERR_ARG, disp)
83 *disp = datatype->lb();
87 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
89 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
90 CHECK_NULL(2, MPI_ERR_ARG, disp)
91 *disp = datatype->ub();
95 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
96 int retval = MPI_SUCCESS;
97 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
98 retval = datatype->clone(newtype);
99 //error when duplicating, free the new datatype
100 if(retval!=MPI_SUCCESS){
101 simgrid::smpi::Datatype::unref(*newtype);
102 *newtype = MPI_DATATYPE_NULL;
107 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
108 CHECK_COUNT(1, count)
109 CHECK_MPI_NULL(2, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
110 CHECK_NULL(3, MPI_ERR_ARG, new_type)
111 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
114 int PMPI_Type_commit(MPI_Datatype* datatype) {
115 CHECK_NULL(1, MPI_ERR_ARG, datatype)
116 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, (*datatype))
117 (*datatype)->commit();
121 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
122 CHECK_COUNT(1, count)
123 CHECK_NEGATIVE(2, MPI_ERR_ARG, blocklen)
124 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
125 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
128 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
129 CHECK_COUNT(1, count)
130 CHECK_NEGATIVE(2, MPI_ERR_ARG, blocklen)
131 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
132 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
135 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
136 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
139 int PMPI_Type_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
140 CHECK_COUNT(1, count)
141 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
142 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
145 int PMPI_Type_create_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
146 CHECK_COUNT(1, count)
147 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
148 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
151 int PMPI_Type_create_indexed_block(int count, int blocklength, const int* indices, MPI_Datatype old_type,
152 MPI_Datatype* new_type)
154 CHECK_COUNT(1, count)
155 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
156 auto* blocklens = static_cast<int*>(xbt_malloc(blocklength * count * sizeof(int)));
157 for (int i = 0; i < count; i++)
158 blocklens[i]=blocklength;
159 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
164 int PMPI_Type_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
165 MPI_Datatype* new_type)
167 CHECK_COUNT(1, count)
168 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
169 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
172 int PMPI_Type_create_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
173 MPI_Datatype* new_type) {
174 return PMPI_Type_hindexed(count, blocklens, indices, old_type, new_type);
177 int PMPI_Type_create_hindexed_block(int count, int blocklength, const MPI_Aint* indices, MPI_Datatype old_type,
178 MPI_Datatype* new_type) {
179 CHECK_COUNT(1, count)
180 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
181 auto* blocklens = static_cast<int*>(xbt_malloc(blocklength * count * sizeof(int)));
182 for (int i = 0; i < count; i++)
183 blocklens[i] = blocklength;
184 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
189 int PMPI_Type_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
190 MPI_Datatype* new_type)
192 CHECK_COUNT(1, count)
193 for(int i=0; i<count; i++)
194 CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_types[i])
195 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
198 int PMPI_Type_create_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
199 MPI_Datatype* new_type) {
200 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
204 int PMPI_Type_create_subarray(int ndims, const int* array_of_sizes,
205 const int* array_of_subsizes, const int* array_of_starts,
206 int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
207 CHECK_NEGATIVE(1, MPI_ERR_COUNT, ndims)
209 *newtype = MPI_DATATYPE_NULL;
211 } else if (ndims==1){
212 simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
214 } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
216 } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
219 return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
223 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
224 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, oldtype)
225 return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
229 int PMPI_Type_set_name(MPI_Datatype datatype, const char * name)
231 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
232 CHECK_NULL(2, MPI_ERR_ARG, name)
233 datatype->set_name(name);
237 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
239 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
240 CHECK_NULL(2, MPI_ERR_ARG, name)
241 datatype->get_name(name, len);
245 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
247 return MPI_DATATYPE_NULL;
248 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
251 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
252 if(datatype==MPI_DATATYPE_NULL)
254 return datatype->c2f();
257 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
259 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
260 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
263 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
265 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
266 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
269 int PMPI_Type_get_contents (MPI_Datatype type, int max_integers, int max_addresses,
270 int max_datatypes, int* array_of_integers, MPI_Aint* array_of_addresses,
271 MPI_Datatype *array_of_datatypes)
273 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
274 CHECK_NEGATIVE(2, MPI_ERR_COUNT, max_integers)
275 CHECK_NEGATIVE(3, MPI_ERR_COUNT, max_addresses)
276 CHECK_NEGATIVE(4, MPI_ERR_COUNT, max_datatypes)
278 CHECK_NULL(5, MPI_ERR_ARG, array_of_integers)
280 CHECK_NULL(6, MPI_ERR_ARG, array_of_addresses)
282 CHECK_NULL(7, MPI_ERR_ARG, array_of_datatypes)
283 return type->get_contents(max_integers, max_addresses, max_datatypes,
284 array_of_integers, array_of_addresses, array_of_datatypes);
287 int PMPI_Type_get_envelope (MPI_Datatype type, int *num_integers, int *num_addresses,
288 int *num_datatypes, int *combiner)
290 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
291 CHECK_NULL(2, MPI_ERR_ARG, num_integers)
292 CHECK_NULL(3, MPI_ERR_ARG, num_addresses)
293 CHECK_NULL(4, MPI_ERR_ARG, num_datatypes)
294 CHECK_NULL(5, MPI_ERR_ARG, combiner)
295 return type->get_envelope(num_integers, num_addresses, num_datatypes, combiner);
298 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
300 CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
301 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
304 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
307 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr,nullptr,nullptr,nullptr};
308 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr,nullptr,nullptr,nullptr};
309 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
312 int PMPI_Type_free_keyval(int* keyval) {
313 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
316 int PMPI_Unpack(const void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
317 CHECK_NEGATIVE(2, MPI_ERR_COUNT, incount)
318 CHECK_NEGATIVE(5, MPI_ERR_COUNT, outcount)
319 CHECK_BUFFER(1, inbuf, incount)
320 CHECK_BUFFER(4, outbuf, outcount)
323 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
326 int PMPI_Pack(const void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
327 CHECK_NEGATIVE(2, MPI_ERR_COUNT, incount)
328 CHECK_NEGATIVE(5, MPI_ERR_COUNT, outcount)
329 CHECK_BUFFER(1, inbuf, incount)
330 CHECK_BUFFER(4, outbuf, outcount)
333 return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
336 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
337 CHECK_NEGATIVE(1, MPI_ERR_COUNT, incount)
338 CHECK_TYPE(2, datatype)
340 *size=incount*datatype->size();