1 /* Copyright (c) 2007-2018. 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) {
19 simgrid::smpi::Datatype::unref(*datatype);
24 int PMPI_Type_size(MPI_Datatype datatype, int *size)
26 if (datatype == MPI_DATATYPE_NULL) {
28 } else if (size == nullptr) {
31 *size = static_cast<int>(datatype->size());
36 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
38 if (datatype == MPI_DATATYPE_NULL) {
40 } else if (size == nullptr) {
43 *size = static_cast<MPI_Count>(datatype->size());
48 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
50 if (datatype == MPI_DATATYPE_NULL) {
52 } else if (lb == nullptr || extent == nullptr) {
55 return datatype->extent(lb, extent);
59 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
61 return PMPI_Type_get_extent(datatype, lb, extent);
64 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
66 if (datatype == MPI_DATATYPE_NULL) {
68 } else if (extent == nullptr) {
71 *extent = datatype->get_extent();
76 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
78 if (datatype == MPI_DATATYPE_NULL) {
80 } else if (disp == nullptr) {
83 *disp = datatype->lb();
88 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
90 if (datatype == MPI_DATATYPE_NULL) {
92 } else if (disp == nullptr) {
95 *disp = datatype->ub();
100 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
101 int retval = MPI_SUCCESS;
102 if (datatype == MPI_DATATYPE_NULL) {
105 *newtype = new simgrid::smpi::Datatype(datatype, &retval);
106 //error when duplicating, free the new datatype
107 if(retval!=MPI_SUCCESS){
108 simgrid::smpi::Datatype::unref(*newtype);
109 *newtype = MPI_DATATYPE_NULL;
115 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
116 if (old_type == MPI_DATATYPE_NULL) {
119 return MPI_ERR_COUNT;
121 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
125 int PMPI_Type_commit(MPI_Datatype* datatype) {
126 if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
129 (*datatype)->commit();
134 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
135 if (old_type == MPI_DATATYPE_NULL) {
137 } else if (count<0 || blocklen<0){
138 return MPI_ERR_COUNT;
140 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
144 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
145 if (old_type == MPI_DATATYPE_NULL) {
147 } else if (count<0 || blocklen<0){
148 return MPI_ERR_COUNT;
150 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
154 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
155 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
158 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
159 if (old_type == MPI_DATATYPE_NULL) {
162 return MPI_ERR_COUNT;
164 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
168 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
169 if (old_type == MPI_DATATYPE_NULL) {
172 return MPI_ERR_COUNT;
174 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
178 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
179 MPI_Datatype* new_type)
181 if (old_type == MPI_DATATYPE_NULL) {
184 return MPI_ERR_COUNT;
186 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
187 for (int i = 0; i < count; i++)
188 blocklens[i]=blocklength;
189 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
195 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
197 if (old_type == MPI_DATATYPE_NULL) {
200 return MPI_ERR_COUNT;
202 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
206 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
207 MPI_Datatype* new_type) {
208 return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
211 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
212 MPI_Datatype* new_type) {
213 if (old_type == MPI_DATATYPE_NULL) {
216 return MPI_ERR_COUNT;
218 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
219 for (int i = 0; i < count; i++)
220 blocklens[i] = blocklength;
221 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
227 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
229 return MPI_ERR_COUNT;
231 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
235 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
236 MPI_Datatype* new_type) {
237 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
241 int PMPI_Type_create_subarray(int ndims, int* array_of_sizes,
242 int* array_of_subsizes, int* array_of_starts,
243 int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
245 return MPI_ERR_COUNT;
246 } else if (ndims==0){
247 *newtype = MPI_DATATYPE_NULL;
249 } else if (ndims==1){
250 simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
252 } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
254 } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
257 return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
261 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
262 if (oldtype == MPI_DATATYPE_NULL) {
265 return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
269 int PMPI_Type_set_name(MPI_Datatype datatype, char * name)
271 if (datatype == MPI_DATATYPE_NULL) {
273 } else if (name == nullptr) {
276 datatype->set_name(name);
281 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
283 if (datatype == MPI_DATATYPE_NULL) {
285 } else if (name == nullptr) {
288 datatype->get_name(name, len);
293 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
294 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
297 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
298 return datatype->c2f();
301 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
303 if (type==MPI_DATATYPE_NULL)
306 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
309 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
311 if (type==MPI_DATATYPE_NULL)
314 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
317 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
319 if (type==MPI_DATATYPE_NULL)
322 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
325 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
328 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
329 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
330 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
333 int PMPI_Type_free_keyval(int* keyval) {
334 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
337 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
338 if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
340 if (not type->is_valid())
342 if(comm==MPI_COMM_NULL)
344 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
347 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
348 if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
350 if (not type->is_valid())
352 if(comm==MPI_COMM_NULL)
354 return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
357 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
360 if (not datatype->is_valid())
362 if(comm==MPI_COMM_NULL)
365 *size=incount*datatype->size();