1 /* Copyright (c) 2007-2017. 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 */
12 extern "C" { // Obviously, the C MPI interface should use the C linkage
14 int PMPI_Type_free(MPI_Datatype * datatype)
16 /* Free a predefined datatype is an error according to the standard, and should be checked for */
17 if (*datatype == MPI_DATATYPE_NULL) {
20 simgrid::smpi::Datatype::unref(*datatype);
25 int PMPI_Type_size(MPI_Datatype datatype, int *size)
27 if (datatype == MPI_DATATYPE_NULL) {
29 } else if (size == nullptr) {
32 *size = static_cast<int>(datatype->size());
37 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
39 if (datatype == MPI_DATATYPE_NULL) {
41 } else if (size == nullptr) {
44 *size = static_cast<MPI_Count>(datatype->size());
49 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
51 if (datatype == MPI_DATATYPE_NULL) {
53 } else if (lb == nullptr || extent == nullptr) {
56 return datatype->extent(lb, extent);
60 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
62 return PMPI_Type_get_extent(datatype, lb, extent);
65 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
67 if (datatype == MPI_DATATYPE_NULL) {
69 } else if (extent == nullptr) {
72 *extent = datatype->get_extent();
77 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
79 if (datatype == MPI_DATATYPE_NULL) {
81 } else if (disp == nullptr) {
84 *disp = datatype->lb();
89 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
91 if (datatype == MPI_DATATYPE_NULL) {
93 } else if (disp == nullptr) {
96 *disp = datatype->ub();
101 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
102 int retval = MPI_SUCCESS;
103 if (datatype == MPI_DATATYPE_NULL) {
106 *newtype = new simgrid::smpi::Datatype(datatype, &retval);
107 //error when duplicating, free the new datatype
108 if(retval!=MPI_SUCCESS){
109 simgrid::smpi::Datatype::unref(*newtype);
110 *newtype = MPI_DATATYPE_NULL;
116 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
117 if (old_type == MPI_DATATYPE_NULL) {
120 return MPI_ERR_COUNT;
122 return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
126 int PMPI_Type_commit(MPI_Datatype* datatype) {
127 if (datatype == nullptr || *datatype == MPI_DATATYPE_NULL) {
130 (*datatype)->commit();
135 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
136 if (old_type == MPI_DATATYPE_NULL) {
138 } else if (count<0 || blocklen<0){
139 return MPI_ERR_COUNT;
141 return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
145 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
146 if (old_type == MPI_DATATYPE_NULL) {
148 } else if (count<0 || blocklen<0){
149 return MPI_ERR_COUNT;
151 return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
155 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
156 return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
159 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
160 if (old_type == MPI_DATATYPE_NULL) {
163 return MPI_ERR_COUNT;
165 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
169 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
170 if (old_type == MPI_DATATYPE_NULL) {
173 return MPI_ERR_COUNT;
175 return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
179 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
180 MPI_Datatype* new_type)
182 if (old_type == MPI_DATATYPE_NULL) {
185 return MPI_ERR_COUNT;
187 int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
188 for (int i = 0; i < count; i++)
189 blocklens[i]=blocklength;
190 int retval = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
196 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
198 if (old_type == MPI_DATATYPE_NULL) {
201 return MPI_ERR_COUNT;
203 return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
207 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
208 MPI_Datatype* new_type) {
209 return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
212 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
213 MPI_Datatype* new_type) {
214 if (old_type == MPI_DATATYPE_NULL) {
217 return MPI_ERR_COUNT;
219 int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
220 for (int i = 0; i < count; i++)
221 blocklens[i] = blocklength;
222 int retval = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
228 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
230 return MPI_ERR_COUNT;
232 return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
236 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
237 MPI_Datatype* new_type) {
238 return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
241 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
242 if (oldtype == MPI_DATATYPE_NULL) {
245 int blocks[3] = {1, 1, 1};
246 MPI_Aint disps[3] = {lb, 0, lb + extent};
247 MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
249 *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
251 (*newtype)->addflag(~DT_FLAG_COMMITED);
256 int PMPI_Type_set_name(MPI_Datatype datatype, char * name)
258 if (datatype == MPI_DATATYPE_NULL) {
260 } else if (name == nullptr) {
263 datatype->set_name(name);
268 int PMPI_Type_get_name(MPI_Datatype datatype, char * name, int* len)
270 if (datatype == MPI_DATATYPE_NULL) {
272 } else if (name == nullptr) {
275 datatype->get_name(name, len);
280 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
281 return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
284 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
285 return datatype->c2f();
288 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
290 if (type==MPI_DATATYPE_NULL)
293 return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
296 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
298 if (type==MPI_DATATYPE_NULL)
301 return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
304 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
306 if (type==MPI_DATATYPE_NULL)
309 return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
312 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
315 smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
316 smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
317 return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
320 int PMPI_Type_free_keyval(int* keyval) {
321 return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
324 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
325 if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
327 if (not type->is_valid())
329 if(comm==MPI_COMM_NULL)
331 return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
334 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
335 if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
337 if (not type->is_valid())
339 if(comm==MPI_COMM_NULL)
341 return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
344 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
347 if (not datatype->is_valid())
349 if(comm==MPI_COMM_NULL)
352 *size=incount*datatype->size();