Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
108050c578a0930b93b7d4e23c6b5d44bf11463f
[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, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
200 {
201   if (old_type == MPI_DATATYPE_NULL) {
202     return MPI_ERR_TYPE;
203   } else if (count<0){
204     return MPI_ERR_COUNT;
205   } else {
206     return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
207   }
208 }
209
210 int PMPI_Type_create_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
211                               MPI_Datatype* new_type) {
212   return PMPI_Type_hindexed(count, const_cast<int*>(blocklens),const_cast<MPI_Aint*>(indices),old_type,new_type);
213 }
214
215 int PMPI_Type_create_hindexed_block(int count, int blocklength, const MPI_Aint* indices, MPI_Datatype old_type,
216                                     MPI_Datatype* new_type) {
217   if (old_type == MPI_DATATYPE_NULL) {
218     return MPI_ERR_TYPE;
219   } else if (count<0){
220     return MPI_ERR_COUNT;
221   } else {
222     int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
223     for (int i     = 0; i < count; i++)
224       blocklens[i] = blocklength;
225     int retval     = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
226     xbt_free(blocklens);
227     return retval;
228   }
229 }
230
231 int PMPI_Type_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type) {
232   if (count<0){
233     return MPI_ERR_COUNT;
234   } else {
235     for(int i=0; i<count; i++)
236       if(old_types[i]==MPI_DATATYPE_NULL)
237         return MPI_ERR_TYPE;
238     return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
239   }
240 }
241
242 int PMPI_Type_create_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
243                             MPI_Datatype* new_type) {
244   return PMPI_Type_struct(count, const_cast<int*>(blocklens), const_cast<MPI_Aint*>(indices), const_cast<MPI_Datatype*>(old_types), new_type);
245 }
246
247
248 int PMPI_Type_create_subarray(int ndims, const int* array_of_sizes,
249                              const int* array_of_subsizes, const int* array_of_starts,
250                              int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
251   if (ndims<0){
252     return MPI_ERR_COUNT;
253   } else if (ndims==0){
254     *newtype = MPI_DATATYPE_NULL;
255     return MPI_SUCCESS;
256   } else if (ndims==1){
257     simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
258     return MPI_SUCCESS;
259   } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
260     return MPI_ERR_TYPE;
261   } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
262     return MPI_ERR_ARG;
263   } else {
264     return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
265   }
266 }
267
268 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
269   if (oldtype == MPI_DATATYPE_NULL) {
270     return MPI_ERR_TYPE;
271   }
272   return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
273 }
274
275
276 int PMPI_Type_set_name(MPI_Datatype  datatype, char * name)
277 {
278   if (datatype == MPI_DATATYPE_NULL)  {
279     return MPI_ERR_TYPE;
280   } else if (name == nullptr)  {
281     return MPI_ERR_ARG;
282   } else {
283     datatype->set_name(name);
284     return MPI_SUCCESS;
285   }
286 }
287
288 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
289 {
290   if (datatype == MPI_DATATYPE_NULL)  {
291     return MPI_ERR_TYPE;
292   } else if (name == nullptr)  {
293     return MPI_ERR_ARG;
294   } else {
295     datatype->get_name(name, len);
296     return MPI_SUCCESS;
297   }
298 }
299
300 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
301   return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
302 }
303
304 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
305   return datatype->c2f();
306 }
307
308 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
309 {
310   if (type==MPI_DATATYPE_NULL)
311     return MPI_ERR_TYPE;
312   else
313     return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
314 }
315
316 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
317 {
318   if (type==MPI_DATATYPE_NULL)
319     return MPI_ERR_TYPE;
320   else
321     return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
322 }
323
324 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
325 {
326   if (type==MPI_DATATYPE_NULL)
327     return MPI_ERR_TYPE;
328   else
329     return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
330 }
331
332 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
333                             void* extra_state)
334 {
335   smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr,nullptr,nullptr,nullptr};
336   smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr,nullptr,nullptr,nullptr};
337   return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
338 }
339
340 int PMPI_Type_free_keyval(int* keyval) {
341   return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
342 }
343
344 int PMPI_Unpack(const void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
345   if(incount<0 || outcount < 0){
346     return MPI_ERR_COUNT;
347   } else if (inbuf==nullptr || outbuf==nullptr){
348     return MPI_ERR_ARG;
349   } else if (type == MPI_DATATYPE_NULL || not type->is_valid()){
350     return MPI_ERR_TYPE;
351   } else if(comm==MPI_COMM_NULL){
352     return MPI_ERR_COMM;
353   } else{
354     return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
355   }
356 }
357
358 int PMPI_Pack(const void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
359   if(incount<0){
360     return MPI_ERR_COUNT;
361   } else if(inbuf==nullptr || outbuf==nullptr || outcount < 0){
362     return MPI_ERR_ARG;
363   } else if (type == MPI_DATATYPE_NULL || not type->is_valid()){
364     return MPI_ERR_TYPE;
365   } else if(comm==MPI_COMM_NULL){
366     return MPI_ERR_COMM;
367   } else {
368     return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
369   }
370 }
371
372 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
373   if(incount<0){
374     return MPI_ERR_COUNT;
375   } else if (datatype == MPI_DATATYPE_NULL || not datatype->is_valid()){
376     return MPI_ERR_TYPE;
377   } else if(comm==MPI_COMM_NULL){
378     return MPI_ERR_COMM;
379   } else {
380     *size=incount*datatype->size();
381     return MPI_SUCCESS;
382   }
383 }