Logo AND Algorithmique Numérique Distribuée

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