Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
File smpi/include/private.h cannot be compiled in C mode either. Merge with private...
[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 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
242   if (oldtype == MPI_DATATYPE_NULL) {
243     return MPI_ERR_TYPE;
244   }
245   int blocks[3]         = {1, 1, 1};
246   MPI_Aint disps[3]     = {lb, 0, lb + extent};
247   MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
248
249   *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
250
251   (*newtype)->addflag(~DT_FLAG_COMMITED);
252   return MPI_SUCCESS;
253 }
254
255
256 int PMPI_Type_set_name(MPI_Datatype  datatype, char * name)
257 {
258   if (datatype == MPI_DATATYPE_NULL)  {
259     return MPI_ERR_TYPE;
260   } else if (name == nullptr)  {
261     return MPI_ERR_ARG;
262   } else {
263     datatype->set_name(name);
264     return MPI_SUCCESS;
265   }
266 }
267
268 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
269 {
270   if (datatype == MPI_DATATYPE_NULL)  {
271     return MPI_ERR_TYPE;
272   } else if (name == nullptr)  {
273     return MPI_ERR_ARG;
274   } else {
275     datatype->get_name(name, len);
276     return MPI_SUCCESS;
277   }
278 }
279
280 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
281   return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
282 }
283
284 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
285   return datatype->c2f();
286 }
287
288 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
289 {
290   if (type==MPI_DATATYPE_NULL)
291     return MPI_ERR_TYPE;
292   else
293     return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
294 }
295
296 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
297 {
298   if (type==MPI_DATATYPE_NULL)
299     return MPI_ERR_TYPE;
300   else
301     return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
302 }
303
304 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
305 {
306   if (type==MPI_DATATYPE_NULL)
307     return MPI_ERR_TYPE;
308   else
309     return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
310 }
311
312 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
313                             void* extra_state)
314 {
315   smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr};
316   smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr};
317   return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
318 }
319
320 int PMPI_Type_free_keyval(int* keyval) {
321   return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
322 }
323
324 int PMPI_Unpack(void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
325   if(incount<0 || outcount < 0 || inbuf==nullptr || outbuf==nullptr)
326     return MPI_ERR_ARG;
327   if (not type->is_valid())
328     return MPI_ERR_TYPE;
329   if(comm==MPI_COMM_NULL)
330     return MPI_ERR_COMM;
331   return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
332 }
333
334 int PMPI_Pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
335   if(incount<0 || outcount < 0|| inbuf==nullptr || outbuf==nullptr)
336     return MPI_ERR_ARG;
337   if (not type->is_valid())
338     return MPI_ERR_TYPE;
339   if(comm==MPI_COMM_NULL)
340     return MPI_ERR_COMM;
341   return type->pack(inbuf, incount, outbuf,outcount,position, comm);
342 }
343
344 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
345   if(incount<0)
346     return MPI_ERR_ARG;
347   if (not datatype->is_valid())
348     return MPI_ERR_TYPE;
349   if(comm==MPI_COMM_NULL)
350     return MPI_ERR_COMM;
351
352   *size=incount*datatype->size();
353
354   return MPI_SUCCESS;
355 }
356
357 }