Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the OOP of kernel::profile
[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) {
17     return MPI_ERR_ARG;
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 || blocklen<0){
138     return MPI_ERR_COUNT;
139   } else {
140     return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
141   }
142 }
143
144 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
145   if (old_type == MPI_DATATYPE_NULL) {
146     return MPI_ERR_TYPE;
147   } else if (count<0 || blocklen<0){
148     return MPI_ERR_COUNT;
149   } else {
150     return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
151   }
152 }
153
154 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
155   return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
156 }
157
158 int PMPI_Type_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
159   if (old_type == MPI_DATATYPE_NULL) {
160     return MPI_ERR_TYPE;
161   } else if (count<0){
162     return MPI_ERR_COUNT;
163   } else {
164     return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
165   }
166 }
167
168 int PMPI_Type_create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
169   if (old_type == MPI_DATATYPE_NULL) {
170     return MPI_ERR_TYPE;
171   } else if (count<0){
172     return MPI_ERR_COUNT;
173   } else {
174     return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
175   }
176 }
177
178 int PMPI_Type_create_indexed_block(int count, int blocklength, int* indices, MPI_Datatype old_type,
179                                    MPI_Datatype* new_type)
180 {
181   if (old_type == MPI_DATATYPE_NULL) {
182     return MPI_ERR_TYPE;
183   } else if (count<0){
184     return MPI_ERR_COUNT;
185   } else {
186     int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
187     for (int i    = 0; i < count; i++)
188       blocklens[i]=blocklength;
189     int retval    = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
190     xbt_free(blocklens);
191     return retval;
192   }
193 }
194
195 int PMPI_Type_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
196 {
197   if (old_type == MPI_DATATYPE_NULL) {
198     return MPI_ERR_TYPE;
199   } else if (count<0){
200     return MPI_ERR_COUNT;
201   } else {
202     return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
203   }
204 }
205
206 int PMPI_Type_create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type,
207                               MPI_Datatype* new_type) {
208   return PMPI_Type_hindexed(count, blocklens,indices,old_type,new_type);
209 }
210
211 int PMPI_Type_create_hindexed_block(int count, int blocklength, MPI_Aint* indices, MPI_Datatype old_type,
212                                     MPI_Datatype* new_type) {
213   if (old_type == MPI_DATATYPE_NULL) {
214     return MPI_ERR_TYPE;
215   } else if (count<0){
216     return MPI_ERR_COUNT;
217   } else {
218     int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
219     for (int i     = 0; i < count; i++)
220       blocklens[i] = blocklength;
221     int retval     = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
222     xbt_free(blocklens);
223     return retval;
224   }
225 }
226
227 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
228   if (count<0){
229     return MPI_ERR_COUNT;
230   } else {
231     return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
232   }
233 }
234
235 int PMPI_Type_create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types,
236                             MPI_Datatype* new_type) {
237   return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
238 }
239
240
241 int PMPI_Type_create_subarray(int ndims, int* array_of_sizes,
242                              int* array_of_subsizes, int* array_of_starts,
243                              int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
244   if (ndims<0){
245     return MPI_ERR_COUNT;
246   } else if (ndims==0){
247     *newtype = MPI_DATATYPE_NULL;
248     return MPI_SUCCESS;
249   } else if (ndims==1){
250     simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
251     return MPI_SUCCESS;
252   } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
253     return MPI_ERR_TYPE;
254   } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
255     return MPI_ERR_ARG;
256   } else {
257     return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
258   }
259 }
260
261 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
262   if (oldtype == MPI_DATATYPE_NULL) {
263     return MPI_ERR_TYPE;
264   }
265   return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
266 }
267
268
269 int PMPI_Type_set_name(MPI_Datatype  datatype, char * name)
270 {
271   if (datatype == MPI_DATATYPE_NULL)  {
272     return MPI_ERR_TYPE;
273   } else if (name == nullptr)  {
274     return MPI_ERR_ARG;
275   } else {
276     datatype->set_name(name);
277     return MPI_SUCCESS;
278   }
279 }
280
281 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
282 {
283   if (datatype == MPI_DATATYPE_NULL)  {
284     return MPI_ERR_TYPE;
285   } else if (name == nullptr)  {
286     return MPI_ERR_ARG;
287   } else {
288     datatype->get_name(name, len);
289     return MPI_SUCCESS;
290   }
291 }
292
293 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
294   return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
295 }
296
297 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
298   return datatype->c2f();
299 }
300
301 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
302 {
303   if (type==MPI_DATATYPE_NULL)
304     return MPI_ERR_TYPE;
305   else
306     return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
307 }
308
309 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
310 {
311   if (type==MPI_DATATYPE_NULL)
312     return MPI_ERR_TYPE;
313   else
314     return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
315 }
316
317 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
318 {
319   if (type==MPI_DATATYPE_NULL)
320     return MPI_ERR_TYPE;
321   else
322     return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
323 }
324
325 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
326                             void* extra_state)
327 {
328   smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr,nullptr,nullptr,nullptr};
329   smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr,nullptr,nullptr,nullptr};
330   return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
331 }
332
333 int PMPI_Type_free_keyval(int* keyval) {
334   return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
335 }
336
337 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
338   if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
339     return MPI_ERR_ARG;
340   if (not type->is_valid())
341     return MPI_ERR_TYPE;
342   if(comm==MPI_COMM_NULL)
343     return MPI_ERR_COMM;
344   return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
345 }
346
347 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
348   if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
349     return MPI_ERR_ARG;
350   if (not type->is_valid())
351     return MPI_ERR_TYPE;
352   if(comm==MPI_COMM_NULL)
353     return MPI_ERR_COMM;
354   return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
355 }
356
357 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
358   if(incount<0)
359     return MPI_ERR_ARG;
360   if (not datatype->is_valid())
361     return MPI_ERR_TYPE;
362   if(comm==MPI_COMM_NULL)
363     return MPI_ERR_COMM;
364
365   *size=incount*datatype->size();
366
367   return MPI_SUCCESS;
368 }