1 /* Copyright (c) 2007-2019. 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);
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) {
138 return MPI_ERR_COUNT;
139 } else if(blocklen<0){
142 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
146 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
147 if (old_type == MPI_DATATYPE_NULL) {
150 return MPI_ERR_COUNT;
151 } else if(blocklen<0){
154 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
158 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
159 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
162 int PMPI_Type_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
163 if (old_type == MPI_DATATYPE_NULL) {
166 return MPI_ERR_COUNT;
168 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
172 int PMPI_Type_create_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
173 if (old_type == MPI_DATATYPE_NULL) {
176 return MPI_ERR_COUNT;
178 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
182 int PMPI_Type_create_indexed_block(int count, int blocklength, const int* indices, MPI_Datatype old_type,
183 MPI_Datatype* new_type)
185 if (old_type == MPI_DATATYPE_NULL) {
188 return MPI_ERR_COUNT;
190 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
191 for (int i = 0; i < count; i++)
192 blocklens[i]=blocklength;
193 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
199 int PMPI_Type_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
200 MPI_Datatype* new_type)
202 if (old_type == MPI_DATATYPE_NULL) {
205 return MPI_ERR_COUNT;
207 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
211 int PMPI_Type_create_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
212 MPI_Datatype* new_type) {
213 return PMPI_Type_hindexed(count, blocklens, indices, old_type, new_type);
216 int PMPI_Type_create_hindexed_block(int count, int blocklength, const MPI_Aint* indices, MPI_Datatype old_type,
217 MPI_Datatype* new_type) {
218 if (old_type == MPI_DATATYPE_NULL) {
221 return MPI_ERR_COUNT;
223 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
224 for (int i = 0; i < count; i++)
225 blocklens[i] = blocklength;
226 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
232 int PMPI_Type_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
233 MPI_Datatype* new_type)
236 return MPI_ERR_COUNT;
238 for(int i=0; i<count; i++)
239 if(old_types[i]==MPI_DATATYPE_NULL)
241 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
245 int PMPI_Type_create_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
246 MPI_Datatype* new_type) {
247 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
251 int PMPI_Type_create_subarray(int ndims, const int* array_of_sizes,
252 const int* array_of_subsizes, const int* array_of_starts,
253 int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
255 return MPI_ERR_COUNT;
256 } else if (ndims==0){
257 *newtype = MPI_DATATYPE_NULL;
259 } else if (ndims==1){
260 simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
262 } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
264 } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
267 return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
271 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
272 if (oldtype == MPI_DATATYPE_NULL) {
275 return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
279 int PMPI_Type_set_name(MPI_Datatype datatype, const char * name)
281 if (datatype == MPI_DATATYPE_NULL) {
283 } else if (name == nullptr) {
286 datatype->set_name(name);
291 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
293 if (datatype == MPI_DATATYPE_NULL) {
295 } else if (name == nullptr) {
298 datatype->get_name(name, len);
303 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
305 return MPI_DATATYPE_NULL;
306 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
309 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
310 if(datatype==MPI_DATATYPE_NULL)
312 return datatype->c2f();
315 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
317 if (type==MPI_DATATYPE_NULL)
320 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
323 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
325 if (type==MPI_DATATYPE_NULL)
328 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
331 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
333 if (type==MPI_DATATYPE_NULL)
336 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
339 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
342 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr,nullptr,nullptr,nullptr};
343 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr,nullptr,nullptr,nullptr};
344 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
347 int PMPI_Type_free_keyval(int* keyval) {
348 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
351 int PMPI_Unpack(const void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
352 if(incount<0 || outcount < 0){
353 return MPI_ERR_COUNT;
354 } else if (inbuf==nullptr || outbuf==nullptr){
356 } else if (type == MPI_DATATYPE_NULL || not type->is_valid()){
358 } else if(comm==MPI_COMM_NULL){
361 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
365 int PMPI_Pack(const void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
367 return MPI_ERR_COUNT;
368 } else if(inbuf==nullptr || outbuf==nullptr || outcount < 0){
370 } else if (type == MPI_DATATYPE_NULL || not type->is_valid()){
372 } else if(comm==MPI_COMM_NULL){
375 return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
379 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
381 return MPI_ERR_COUNT;
382 } else if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid()){
384 } else if(comm==MPI_COMM_NULL){
387 *size=incount*datatype->size();