Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'depencencies' of https://framagit.org/simgrid/simgrid into depencencies
[simgrid.git] / src / smpi / bindings / smpi_pmpi_type.cpp
1 /* Copyright (c) 2007-2020. 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   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
27   CHECK_NULL(2, MPI_ERR_ARG, size)
28   *size = static_cast<int>(datatype->size());
29   return MPI_SUCCESS;
30 }
31
32 int PMPI_Type_size_x(MPI_Datatype datatype, MPI_Count *size)
33 {
34   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
35   CHECK_NULL(2, MPI_ERR_ARG, size)
36   *size = static_cast<MPI_Count>(datatype->size());
37   return MPI_SUCCESS;
38 }
39
40 int PMPI_Type_get_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
41 {
42   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
43   CHECK_NULL(2, MPI_ERR_ARG, lb)
44   CHECK_NULL(3, MPI_ERR_ARG, extent)
45   return datatype->extent(lb, extent);
46 }
47
48 int PMPI_Type_get_true_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
49 {
50   return PMPI_Type_get_extent(datatype, lb, extent);
51 }
52
53 int PMPI_Type_extent(MPI_Datatype datatype, MPI_Aint * extent)
54 {
55   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
56   CHECK_NULL(2, MPI_ERR_ARG, extent)
57   *extent = datatype->get_extent();
58   return MPI_SUCCESS;
59 }
60
61 int PMPI_Type_lb(MPI_Datatype datatype, MPI_Aint * disp)
62 {
63   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
64   CHECK_NULL(2, MPI_ERR_ARG, disp)
65   *disp = datatype->lb();
66   return MPI_SUCCESS;
67 }
68
69 int PMPI_Type_ub(MPI_Datatype datatype, MPI_Aint * disp)
70 {
71   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
72   CHECK_NULL(2, MPI_ERR_ARG, disp)
73   *disp = datatype->ub();
74   return MPI_SUCCESS;
75 }
76
77 int PMPI_Type_dup(MPI_Datatype datatype, MPI_Datatype *newtype){
78   int retval = MPI_SUCCESS;
79   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
80   *newtype = new simgrid::smpi::Datatype(datatype, &retval);
81   //error when duplicating, free the new datatype
82   if(retval!=MPI_SUCCESS){
83     simgrid::smpi::Datatype::unref(*newtype);
84     *newtype = MPI_DATATYPE_NULL;
85   }
86   return retval;
87 }
88
89 int PMPI_Type_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type) {
90   CHECK_COUNT(1, count)
91   CHECK_MPI_NULL(2, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
92   CHECK_NULL(3, MPI_ERR_ARG, new_type)
93   return simgrid::smpi::Datatype::create_contiguous(count, old_type, 0, new_type);
94 }
95
96 int PMPI_Type_commit(MPI_Datatype* datatype) {
97   CHECK_NULL(1, MPI_ERR_ARG, datatype)
98   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, (*datatype))
99   (*datatype)->commit();
100   return MPI_SUCCESS;
101 }
102
103 int PMPI_Type_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
104   CHECK_COUNT(1, count)
105   CHECK_NEGATIVE(2, MPI_ERR_ARG, blocklen)
106   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
107   return simgrid::smpi::Datatype::create_vector(count, blocklen, stride, old_type, new_type);
108 }
109
110 int PMPI_Type_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
111   CHECK_COUNT(1, count)
112   CHECK_NEGATIVE(2, MPI_ERR_ARG, blocklen)
113   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
114   return simgrid::smpi::Datatype::create_hvector(count, blocklen, stride, old_type, new_type);
115 }
116
117 int PMPI_Type_create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type) {
118   return MPI_Type_hvector(count, blocklen, stride, old_type, new_type);
119 }
120
121 int PMPI_Type_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
122   CHECK_COUNT(1, count)
123   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
124   return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
125 }
126
127 int PMPI_Type_create_indexed(int count, const int* blocklens, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type) {
128   CHECK_COUNT(1, count)
129   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
130   return simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
131 }
132
133 int PMPI_Type_create_indexed_block(int count, int blocklength, const int* indices, MPI_Datatype old_type,
134                                    MPI_Datatype* new_type)
135 {
136   CHECK_COUNT(1, count)
137   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
138   int* blocklens=static_cast<int*>(xbt_malloc(blocklength*count*sizeof(int)));
139   for (int i    = 0; i < count; i++)
140     blocklens[i]=blocklength;
141   int retval    = simgrid::smpi::Datatype::create_indexed(count, blocklens, indices, old_type, new_type);
142   xbt_free(blocklens);
143   return retval;
144 }
145
146 int PMPI_Type_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
147                        MPI_Datatype* new_type)
148 {
149   CHECK_COUNT(1, count)
150   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
151   return simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
152 }
153
154 int PMPI_Type_create_hindexed(int count, const int* blocklens, const MPI_Aint* indices, MPI_Datatype old_type,
155                               MPI_Datatype* new_type) {
156   return PMPI_Type_hindexed(count, blocklens, indices, old_type, new_type);
157 }
158
159 int PMPI_Type_create_hindexed_block(int count, int blocklength, const MPI_Aint* indices, MPI_Datatype old_type,
160                                     MPI_Datatype* new_type) {
161   CHECK_COUNT(1, count)
162   CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_type)
163   int* blocklens=(int*)xbt_malloc(blocklength*count*sizeof(int));
164   for (int i     = 0; i < count; i++)
165     blocklens[i] = blocklength;
166   int retval     = simgrid::smpi::Datatype::create_hindexed(count, blocklens, indices, old_type, new_type);
167   xbt_free(blocklens);
168   return retval;
169 }
170
171 int PMPI_Type_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
172                      MPI_Datatype* new_type)
173 {
174   CHECK_COUNT(1, count)
175   for(int i=0; i<count; i++)
176     CHECK_MPI_NULL(4, MPI_DATATYPE_NULL, MPI_ERR_TYPE, old_types[i])
177   return simgrid::smpi::Datatype::create_struct(count, blocklens, indices, old_types, new_type);
178 }
179
180 int PMPI_Type_create_struct(int count, const int* blocklens, const MPI_Aint* indices, const MPI_Datatype* old_types,
181                             MPI_Datatype* new_type) {
182   return PMPI_Type_struct(count, blocklens, indices, old_types, new_type);
183 }
184
185
186 int PMPI_Type_create_subarray(int ndims, const int* array_of_sizes,
187                              const int* array_of_subsizes, const int* array_of_starts,
188                              int order, MPI_Datatype oldtype, MPI_Datatype *newtype) {
189   CHECK_NEGATIVE(1, MPI_ERR_COUNT, ndims)
190   if (ndims==0){
191     *newtype = MPI_DATATYPE_NULL;
192     return MPI_SUCCESS;
193   } else if (ndims==1){
194     simgrid::smpi::Datatype::create_contiguous( array_of_subsizes[0], oldtype, array_of_starts[0]*oldtype->get_extent(), newtype);
195     return MPI_SUCCESS;
196   } else if (oldtype == MPI_DATATYPE_NULL || not oldtype->is_valid() ) {
197     return MPI_ERR_TYPE;
198   } else if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C){
199     return MPI_ERR_ARG;
200   } else {
201     return simgrid::smpi::Datatype::create_subarray(ndims, array_of_sizes, array_of_subsizes, array_of_starts, order, oldtype, newtype);
202   }
203 }
204
205 int PMPI_Type_create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
206   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, oldtype)
207   return simgrid::smpi::Datatype::create_resized(oldtype, lb, extent, newtype);
208 }
209
210
211 int PMPI_Type_set_name(MPI_Datatype  datatype, const char * name)
212 {
213   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
214   CHECK_NULL(2, MPI_ERR_ARG, name)
215   datatype->set_name(name);
216   return MPI_SUCCESS;
217 }
218
219 int PMPI_Type_get_name(MPI_Datatype  datatype, char * name, int* len)
220 {
221   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, datatype)
222   CHECK_NULL(2, MPI_ERR_ARG, name)
223   datatype->get_name(name, len);
224   return MPI_SUCCESS;
225 }
226
227 MPI_Datatype PMPI_Type_f2c(MPI_Fint datatype){
228   if(datatype==-1)
229     return MPI_DATATYPE_NULL;
230   return static_cast<MPI_Datatype>(simgrid::smpi::F2C::f2c(datatype));
231 }
232
233 MPI_Fint PMPI_Type_c2f(MPI_Datatype datatype){
234   if(datatype==MPI_DATATYPE_NULL)
235     return -1;
236   return datatype->c2f();
237 }
238
239 int PMPI_Type_get_attr (MPI_Datatype type, int type_keyval, void *attribute_val, int* flag)
240 {
241   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
242   return type->attr_get<simgrid::smpi::Datatype>(type_keyval, attribute_val, flag);
243 }
244
245 int PMPI_Type_set_attr (MPI_Datatype type, int type_keyval, void *attribute_val)
246 {
247   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
248   return type->attr_put<simgrid::smpi::Datatype>(type_keyval, attribute_val);
249 }
250
251 int PMPI_Type_delete_attr (MPI_Datatype type, int type_keyval)
252 {
253   CHECK_MPI_NULL(1, MPI_DATATYPE_NULL, MPI_ERR_TYPE, type)
254   return type->attr_delete<simgrid::smpi::Datatype>(type_keyval);
255 }
256
257 int PMPI_Type_create_keyval(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
258                             void* extra_state)
259 {
260   smpi_copy_fn _copy_fn={nullptr,copy_fn,nullptr,nullptr,nullptr,nullptr};
261   smpi_delete_fn _delete_fn={nullptr,delete_fn,nullptr,nullptr,nullptr,nullptr};
262   return simgrid::smpi::Keyval::keyval_create<simgrid::smpi::Datatype>(_copy_fn, _delete_fn, keyval, extra_state);
263 }
264
265 int PMPI_Type_free_keyval(int* keyval) {
266   return simgrid::smpi::Keyval::keyval_free<simgrid::smpi::Datatype>(keyval);
267 }
268
269 int PMPI_Unpack(const void* inbuf, int incount, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm) {
270   CHECK_NEGATIVE(2, MPI_ERR_COUNT, incount)
271   CHECK_NEGATIVE(5, MPI_ERR_COUNT, outcount)
272   CHECK_BUFFER(1, inbuf, incount)
273   CHECK_BUFFER(4, outbuf, outcount)
274   CHECK_TYPE(6, type)
275   CHECK_COMM(7)
276   return type->unpack(inbuf, incount, position, outbuf,outcount, comm);
277 }
278
279 int PMPI_Pack(const void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm) {
280   CHECK_NEGATIVE(2, MPI_ERR_COUNT, incount)
281   CHECK_NEGATIVE(5, MPI_ERR_COUNT, outcount)
282   CHECK_BUFFER(1, inbuf, incount)
283   CHECK_BUFFER(4, outbuf, outcount)
284   CHECK_TYPE(6, type)
285   CHECK_COMM(7)
286   return type->pack(inbuf == MPI_BOTTOM ? nullptr : inbuf, incount, outbuf, outcount, position, comm);
287 }
288
289 int PMPI_Pack_size(int incount, MPI_Datatype datatype, MPI_Comm comm, int* size) {
290   CHECK_NEGATIVE(1, MPI_ERR_COUNT, incount)
291   CHECK_TYPE(2, datatype)
292   CHECK_COMM(3)
293   *size=incount*datatype->size();
294   return MPI_SUCCESS;
295 }