Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / smpi / bindings / smpi_pmpi_type.cpp
1 /* Copyright (c) 2007-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "private.hpp"
7 #include "smpi_datatype_derived.hpp"
8 #include "smpi_comm.hpp"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
11
12 /* PMPI User level calls */
13
14 int PMPI_Type_free(MPI_Datatype * datatype)
15 {
16   /* Free a predefined datatype is an error according to the standard, and should be checked for */
17   if (*datatype == MPI_DATATYPE_NULL || (*datatype)->flags() & DT_FLAG_PREDEFINED) {
18     return MPI_ERR_TYPE;
19   } else {
20     simgrid::smpi::Datatype::unref(*datatype);
21     *datatype=MPI_DATATYPE_NULL;
22     return MPI_SUCCESS;
23   }
24 }
25
26 int PMPI_Type_size(MPI_Datatype datatype, int *size)
27 {
28   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
29   CHECK_NULL(2, MPI_ERR_ARG, size)
30   *size = static_cast<int>(datatype->size());
31   return MPI_SUCCESS;
32 }
33
34 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
35 {
36   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
37   CHECK_NULL(2, MPI_ERR_ARG, size)
38   *size = static_cast<MPI_Count>(datatype->size());
39   return MPI_SUCCESS;
40 }
41
42 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
43 {
44   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
45   CHECK_NULL(2, MPI_ERR_ARG, lb)
46   CHECK_NULL(3, MPI_ERR_ARG, extent)
47   return datatype->extent(lb, extent);
48 }
49
50 int PMPI_Type_get_extent_x(MPI_Datatype datatype, MPI_Count * lb, MPI_Count * extent)
51 {
52   MPI_Aint tmplb;
53   MPI_Aint tmpext;
54   int ret = PMPI_Type_get_extent(datatype, &tmplb, &tmpext);
55   if(ret == MPI_SUCCESS){
56     *lb = static_cast<MPI_Count>(tmplb);
57     *extent = static_cast<MPI_Count>(tmpext);
58   }
59   return ret;
60 }
61
62 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
63 {
64   return PMPI_Type_get_extent(datatype, lb, extent);
65 }
66
67 int PMPI_Type_get_true_extent_x(MPI_Datatype datatype, MPI_Count * lb, MPI_Count * extent)
68 {
69   return PMPI_Type_get_extent_x(datatype, lb, extent);
70 }
71
72 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
73 {
74   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
75   CHECK_NULL(2, MPI_ERR_ARG, extent)
76   *extent = datatype->get_extent();
77   return MPI_SUCCESS;
78 }
79
80 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
81 {
82   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
83   CHECK_NULL(2, MPI_ERR_ARG, disp)
84   *disp = datatype->lb();
85   return MPI_SUCCESS;
86 }
87
88 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
89 {
90   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
91   CHECK_NULL(2, MPI_ERR_ARG, disp)
92   *disp = datatype->ub();
93   return MPI_SUCCESS;
94 }
95
96 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
97   int retval = MPI_SUCCESS;
98   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
99   retval = datatype->clone(newtype);
100   //error when duplicating, free the new datatype
101   if(retval!=MPI_SUCCESS){
102     simgrid::smpi::Datatype::unref(*newtype);
103     *newtype = MPI_DATATYPE_NULL;
104   }
105   return retval;
106 }
107
108 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
109   CHECK_COUNT(1, count)
110   CHECK_MPI_NULL(2, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
111   CHECK_NULL(3, MPI_ERR_ARG, new_type)
112   return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
113 }
114
115 int PMPI_Type_commit(MPI_Datatype* datatype) {
116   CHECK_NULL(1, MPI_ERR_ARG, datatype)
117   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, (*datatype))
118   (*datatype)->commit();
119   return MPI_SUCCESS;
120 }
121
122 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
123   CHECK_COUNT(1, count)
124   CHECK_NEGATIVE(2, MPI_ERR_ARG, blocklen)
125   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
126   return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
127 }
128
129 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
130   CHECK_COUNT(1, count)
131   CHECK_NEGATIVE(2, MPI_ERR_ARG, blocklen)
132   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
133   return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
134 }
135
136 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
137   return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
138 }
139
140 int PMPI_Type_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
141   CHECK_COUNT(1, count)
142   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
143   return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
144 }
145
146 int PMPI_Type_create_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
147   CHECK_COUNT(1, count)
148   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
149   return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
150 }
151
152 int PMPI_Type_create_indexed_block(int count, int blocklength, const int* indices, MPI_Datatype old_type,
153                                    MPI_Datatype* new_type)
154 {
155   CHECK_COUNT(1, count)
156   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
157   auto* blocklens = static_cast<int*>(xbt_malloc(blocklength * count * sizeof(int)));
158   for (int i    = 0; i < count; i++)
159     blocklens[i]=blocklength;
160   int retval    = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
161   xbt_free(blocklens);
162   return retval;
163 }
164
165 int PMPI_Type_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
166                        MPI_Datatype* new_type)
167 {
168   CHECK_COUNT(1, count)
169   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
170   return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
171 }
172
173 int PMPI_Type_create_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
174                               MPI_Datatype* new_type) {
175   return PMPI_Type_hindexed(count, blocklens, indices, old_type, new_type);
176 }
177
178 int PMPI_Type_create_hindexed_block(int count, int blocklength, const MPI_Aint* indices, MPI_Datatype old_type,
179                                     MPI_Datatype* new_type) {
180   CHECK_COUNT(1, count)
181   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
182   auto* blocklens = static_cast<int*>(xbt_malloc(blocklength * count * sizeof(int)));
183   for (int i     = 0; i < count; i++)
184     blocklens[i] = blocklength;
185   int retval     = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
186   xbt_free(blocklens);
187   return retval;
188 }
189
190 int PMPI_Type_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
191                      MPI_Datatype* new_type)
192 {
193   CHECK_COUNT(1, count)
194   for(int i=0; i<count; i++)
195     CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_types[i])
196   return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
197 }
198
199 int PMPI_Type_create_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
200                             MPI_Datatype* new_type) {
201   return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
202 }
203
204
205 int PMPI_Type_create_subarray(int ndims, const int* array_of_sizes,
206                              const int* array_of_subsizes, const int* array_of_starts,
207                              int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
208   CHECK_NEGATIVE(1, MPI_ERR_COUNT, ndims)
209   if (ndims==0){
210     *newtype = MPI_DATATYPE_NULL;
211     return MPI_SUCCESS;
212   } else if (ndims==1){
213     simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
214     return MPI_SUCCESS;
215   } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
216     return MPI_ERR_TYPE;
217   } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
218     return MPI_ERR_ARG;
219   } else {
220     return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
221   }
222 }
223
224 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
225   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, oldtype)
226   return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
227 }
228
229
230 int PMPI_Type_set_name(MPI_Datatype  datatype, const char * name)
231 {
232   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
233   CHECK_NULL(2, MPI_ERR_ARG, name)
234   datatype->set_name(name);
235   return MPI_SUCCESS;
236 }
237
238 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
239 {
240   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
241   CHECK_NULL(2, MPI_ERR_ARG, name)
242   datatype->get_name(name, len);
243   return MPI_SUCCESS;
244 }
245
246 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
247   if(datatype==-1)
248     return MPI_DATATYPE_NULL;
249   return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
250 }
251
252 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
253   if(datatype==MPI_DATATYPE_NULL)
254     return -1;
255   return datatype->c2f();
256 }
257
258 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
259 {
260   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
261   return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
262 }
263
264 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
265 {
266   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
267   return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
268 }
269
270 int PMPI_Type_get_contents (MPI_Datatype type, int max_integers, int max_addresses, 
271                             int max_datatypes, int* array_of_integers, MPI_Aint* array_of_addresses, 
272                             MPI_Datatype *array_of_datatypes)
273 {
274   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
275   CHECK_NEGATIVE(2, MPI_ERR_COUNT, max_integers)
276   CHECK_NEGATIVE(3, MPI_ERR_COUNT, max_addresses)
277   CHECK_NEGATIVE(4, MPI_ERR_COUNT, max_datatypes)
278   if(max_integers>0)
279     CHECK_NULL(5, MPI_ERR_ARG, array_of_integers)
280   if(max_addresses!=0)
281     CHECK_NULL(6, MPI_ERR_ARG, array_of_addresses)
282   if(max_datatypes!=0)
283     CHECK_NULL(7, MPI_ERR_ARG, array_of_datatypes)
284   return type->get_contents(max_integers, max_addresses, max_datatypes,
285                             array_of_integers, array_of_addresses, array_of_datatypes);
286 }
287
288 int PMPI_Type_get_envelope (MPI_Datatype type, int *num_integers, int *num_addresses, 
289                             int *num_datatypes, int *combiner)
290 {
291   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
292   CHECK_NULL(2, MPI_ERR_ARG, num_integers)
293   CHECK_NULL(3, MPI_ERR_ARG, num_addresses)
294   CHECK_NULL(4, MPI_ERR_ARG, num_datatypes)
295   CHECK_NULL(5, MPI_ERR_ARG, combiner)
296   return type->get_envelope(num_integers, num_addresses, num_datatypes, combiner);
297 }
298
299 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
300 {
301   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
302   return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
303 }
304
305 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
306                             void* extra_state)
307 {
308   smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr,nullptr,nullptr,nullptr};
309   smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr,nullptr,nullptr,nullptr};
310   return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
311 }
312
313 int PMPI_Type_free_keyval(int* keyval) {
314   return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
315 }
316
317 int PMPI_Unpack(const void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
318   CHECK_NEGATIVE(2, MPI_ERR_COUNT, incount)
319   CHECK_NEGATIVE(5, MPI_ERR_COUNT, outcount)
320   CHECK_BUFFER(1, inbuf, incount)
321   CHECK_BUFFER(4, outbuf, outcount)
322   CHECK_TYPE(6, type)
323   CHECK_COMM(7)
324   return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
325 }
326
327 int PMPI_Pack(const void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
328   CHECK_NEGATIVE(2, MPI_ERR_COUNT, incount)
329   CHECK_NEGATIVE(5, MPI_ERR_COUNT, outcount)
330   CHECK_BUFFER(1, inbuf, incount)
331   CHECK_BUFFER(4, outbuf, outcount)
332   CHECK_TYPE(6, type)
333   CHECK_COMM(7)
334   return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
335 }
336
337 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
338   CHECK_NEGATIVE(1, MPI_ERR_COUNT, incount)
339   CHECK_TYPE(2, datatype)
340   CHECK_COMM(3)
341   *size = incount * std::max<long>(datatype->size(), datatype->get_extent());
342   return MPI_SUCCESS;
343 }