Logo AND Algorithmique Numérique Distribuée

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