Logo AND Algorithmique Numérique Distribuée

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