Logo AND Algorithmique Numérique Distribuée

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