Logo AND Algorithmique Numérique Distribuée

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