Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0e64a85fe145bd2fffe31bba8a940283a15cc93b
[simgrid.git] / src / smpi / bindings / smpi_pmpi_comm.cpp
1 /* Copyright (c) 2007-2021. 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 <climits>
7
8 #include "private.hpp"
9 #include "smpi_comm.hpp"
10 #include "smpi_info.hpp"
11 #include "smpi_errhandler.hpp"
12 #include "src/smpi/include/smpi_actor.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
15
16 /* PMPI User level calls */
17
18 int PMPI_Comm_rank(MPI_Comm comm, int *rank)
19 {
20   CHECK_COMM(1)
21   CHECK_NULL(2, MPI_ERR_ARG, rank)
22   *rank = comm->rank();
23   return MPI_SUCCESS;
24 }
25
26 int PMPI_Comm_size(MPI_Comm comm, int *size)
27 {
28   CHECK_COMM(1)
29   CHECK_NULL(2, MPI_ERR_ARG, size)
30   *size = comm->size();
31   return MPI_SUCCESS;
32 }
33
34 int PMPI_Comm_get_name (MPI_Comm comm, char* name, int* len)
35 {
36   CHECK_COMM(1)
37   CHECK_NULL(2, MPI_ERR_ARG, name)
38   CHECK_NULL(3, MPI_ERR_ARG, len)
39   comm->get_name(name, len);
40   return MPI_SUCCESS;
41 }
42
43 int PMPI_Comm_set_name (MPI_Comm comm, const char* name)
44 {
45   CHECK_COMM(1)
46   CHECK_NULL(2, MPI_ERR_ARG, name)
47   comm->set_name(name);
48   return MPI_SUCCESS;
49 }
50
51 int PMPI_Comm_group(MPI_Comm comm, MPI_Group * group)
52 {
53   CHECK_COMM(1)
54   CHECK_NULL(2, MPI_ERR_ARG, group)
55   *group = comm->group();
56   if (*group != MPI_COMM_WORLD->group() && *group != MPI_GROUP_NULL && *group != MPI_GROUP_EMPTY)
57     (*group)->ref();
58   return MPI_SUCCESS;
59 }
60
61 int PMPI_Comm_compare(MPI_Comm comm1, MPI_Comm comm2, int *result)
62 {
63   CHECK_COMM2(1, comm1)
64   CHECK_COMM2(2, comm2)
65   CHECK_NULL(3, MPI_ERR_ARG, result)
66   if (comm1 == comm2) {       /* Same communicators means same groups */
67     *result = MPI_IDENT;
68   } else {
69     *result = comm1->group()->compare(comm2->group());
70     if (*result == MPI_IDENT) {
71       *result = MPI_CONGRUENT;
72     }
73   }
74   return MPI_SUCCESS;
75 }
76
77 int PMPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm)
78 {
79   CHECK_COMM(1)
80   CHECK_NULL(2, MPI_ERR_ARG, newcomm)
81   return comm->dup(newcomm);
82 }
83
84 int PMPI_Comm_dup_with_info(MPI_Comm comm, MPI_Info info, MPI_Comm * newcomm)
85 {
86   CHECK_COMM(1)
87   CHECK_NULL(2, MPI_ERR_ARG, newcomm)
88   comm->dup_with_info(info, newcomm);
89   return MPI_SUCCESS;
90 }
91
92 int PMPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm)
93 {
94   CHECK_COMM(1)
95   CHECK_GROUP(2, group)
96   CHECK_NULL(3, MPI_ERR_ARG, newcomm)
97   if (group->rank(simgrid::s4u::this_actor::get_pid()) == MPI_UNDEFINED) {
98     *newcomm= MPI_COMM_NULL;
99     return MPI_SUCCESS;
100   }else{
101     group->ref();
102     *newcomm = new simgrid::smpi::Comm(group, nullptr);
103     return MPI_SUCCESS;
104   }
105 }
106
107 int PMPI_Comm_free(MPI_Comm * comm)
108 {
109   CHECK_NULL(1, MPI_ERR_ARG, comm)
110   CHECK_COMM2(1, *comm)
111   CHECK_MPI_NULL(1, MPI_COMM_WORLD, MPI_ERR_COMM, *comm)
112   simgrid::smpi::Comm::destroy(*comm);
113   *comm = MPI_COMM_NULL;
114   return MPI_SUCCESS;
115 }
116
117 int PMPI_Comm_disconnect(MPI_Comm * comm)
118 {
119   /* TODO: wait until all communication in comm are done */
120   CHECK_NULL(1, MPI_ERR_ARG, comm)
121   CHECK_COMM2(1, *comm)
122   simgrid::smpi::Comm::destroy(*comm);
123   *comm = MPI_COMM_NULL;
124   return MPI_SUCCESS;
125 }
126
127 int PMPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out)
128 {
129   CHECK_NULL(4, MPI_ERR_ARG, comm_out)
130   CHECK_COMM2(1, comm)
131   if( color != MPI_UNDEFINED)//we use a negative value for MPI_UNDEFINED
132     CHECK_NEGATIVE(3, MPI_ERR_ARG, color)
133   smpi_bench_end();
134   *comm_out = comm->split(color, key);
135   smpi_bench_begin();
136   return MPI_SUCCESS;
137 }
138
139 int PMPI_Comm_split_type(MPI_Comm comm, int split_type, int key, MPI_Info info, MPI_Comm *newcomm)
140 {
141   CHECK_COMM(1)
142   CHECK_NULL(5, MPI_ERR_ARG, newcomm)
143   smpi_bench_end();
144   *newcomm = comm->split_type(split_type, key, info);
145   smpi_bench_begin();
146   return MPI_SUCCESS;
147 }
148
149 int PMPI_Comm_create_group(MPI_Comm comm, MPI_Group group, int, MPI_Comm* comm_out)
150 {
151   CHECK_COMM(1)
152   CHECK_GROUP(2, group)
153   CHECK_NULL(5, MPI_ERR_ARG, comm_out)
154   smpi_bench_end();
155   int retval = MPI_Comm_create(comm, group, comm_out);
156   smpi_bench_begin();
157   return retval;
158 }
159
160 MPI_Comm PMPI_Comm_f2c(MPI_Fint comm){
161   if(comm==-1)
162     return MPI_COMM_NULL;
163   return simgrid::smpi::Comm::f2c(comm);
164 }
165
166 MPI_Fint PMPI_Comm_c2f(MPI_Comm comm){
167   if(comm==MPI_COMM_NULL)
168     return -1;
169   return comm->c2f();
170 }
171
172 int PMPI_Comm_get_attr (MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
173 {
174   return PMPI_Attr_get(comm, comm_keyval, attribute_val,flag);
175 }
176
177 int PMPI_Comm_set_attr (MPI_Comm comm, int comm_keyval, void *attribute_val)
178 {
179   return PMPI_Attr_put(comm, comm_keyval, attribute_val);
180 }
181
182 int PMPI_Comm_get_info(MPI_Comm comm, MPI_Info* info)
183 {
184   CHECK_COMM(1)
185   CHECK_NULL(2, MPI_ERR_ARG, info)
186   *info = comm->info();
187   return MPI_SUCCESS;
188 }
189
190 int PMPI_Comm_set_info(MPI_Comm  comm, MPI_Info info)
191 {
192   CHECK_COMM(1)
193   comm->set_info(info);
194   return MPI_SUCCESS;
195 }
196
197 int PMPI_Comm_delete_attr (MPI_Comm comm, int comm_keyval)
198 {
199   return PMPI_Attr_delete(comm, comm_keyval);
200 }
201
202 int PMPI_Comm_create_keyval(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
203                             void* extra_state)
204 {
205   return PMPI_Keyval_create(copy_fn, delete_fn, keyval, extra_state);
206 }
207
208 int PMPI_Comm_free_keyval(int* keyval) {
209   return PMPI_Keyval_free(keyval);
210 }
211
212 int PMPI_Comm_test_inter(MPI_Comm comm, int* flag){
213   CHECK_COMM(1)
214
215   if(flag == nullptr)
216     return MPI_ERR_ARG;
217   *flag=false;
218   return MPI_SUCCESS;
219 }
220
221 int PMPI_Attr_delete(MPI_Comm comm, int keyval) {
222   CHECK_COMM(1)
223   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
224        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
225     return MPI_ERR_ARG;
226   else
227     return comm->attr_delete<simgrid::smpi::Comm>(keyval);
228 }
229
230 int PMPI_Attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag) {
231   static int one = 1;
232   static int zero = 0;
233   static int tag_ub = INT_MAX;
234   static int last_used_code = MPI_ERR_LASTCODE;
235   static int universe_size;
236
237   CHECK_NULL(4, MPI_ERR_ARG, flag)
238   *flag = 0;
239   CHECK_COMM(1)
240
241   switch (keyval) {
242   case MPI_HOST:
243   case MPI_IO:
244   case MPI_APPNUM:
245     *flag = 1;
246     *static_cast<int**>(attr_value) = &zero;
247     return MPI_SUCCESS;
248   case MPI_UNIVERSE_SIZE:
249     *flag = 1;
250     universe_size                   = smpi_get_universe_size();
251     *static_cast<int**>(attr_value) = &universe_size;
252     return MPI_SUCCESS;
253   case MPI_LASTUSEDCODE:
254     *flag = 1;
255     *static_cast<int**>(attr_value) = &last_used_code;
256     return MPI_SUCCESS;
257   case MPI_TAG_UB:
258     *flag=1;
259     *static_cast<int**>(attr_value) = &tag_ub;
260     return MPI_SUCCESS;
261   case MPI_WTIME_IS_GLOBAL:
262     *flag = 1;
263     *static_cast<int**>(attr_value) = &one;
264     return MPI_SUCCESS;
265   default:
266     return comm->attr_get<simgrid::smpi::Comm>(keyval, attr_value, flag);
267   }
268 }
269
270 int PMPI_Attr_put(MPI_Comm comm, int keyval, void* attr_value) {
271   CHECK_COMM(1)
272   if(keyval == MPI_TAG_UB||keyval == MPI_HOST||keyval == MPI_IO ||keyval == MPI_WTIME_IS_GLOBAL||keyval == MPI_APPNUM
273        ||keyval == MPI_UNIVERSE_SIZE||keyval == MPI_LASTUSEDCODE)
274     return MPI_ERR_ARG;
275   else
276     return comm->attr_put<simgrid::smpi::Comm>(keyval, attr_value);
277 }
278
279 int PMPI_Errhandler_free(MPI_Errhandler* errhandler){
280   CHECK_NULL(1, MPI_ERR_ARG, errhandler)
281   CHECK_MPI_NULL(1, MPI_ERRHANDLER_NULL, MPI_ERR_ARG, *errhandler)
282   simgrid::smpi::Errhandler::unref(*errhandler);
283   *errhandler = MPI_ERRHANDLER_NULL;
284   return MPI_SUCCESS;
285 }
286
287 int PMPI_Errhandler_create(MPI_Handler_function* function, MPI_Errhandler* errhandler){
288   CHECK_NULL(2, MPI_ERR_ARG, errhandler)
289   *errhandler=new simgrid::smpi::Errhandler(function);
290   (*errhandler)->add_f();
291   return MPI_SUCCESS;
292 }
293
294 int PMPI_Errhandler_get(MPI_Comm comm, MPI_Errhandler* errhandler){
295   CHECK_COMM(1)
296   CHECK_NULL(1, MPI_ERR_ARG, errhandler)
297   *errhandler=comm->errhandler();
298   return MPI_SUCCESS;
299 }
300
301 int PMPI_Errhandler_set(MPI_Comm comm, MPI_Errhandler errhandler){
302   CHECK_COMM(1)
303   CHECK_NULL(1, MPI_ERR_ARG, errhandler)
304   comm->set_errhandler(errhandler);
305   return MPI_SUCCESS;
306 }
307
308 int PMPI_Comm_call_errhandler(MPI_Comm comm,int errorcode){
309   CHECK_COMM(1)
310   MPI_Errhandler err = comm->errhandler();
311   err->call(comm, errorcode);
312   simgrid::smpi::Errhandler::unref(err);
313   return MPI_SUCCESS;
314 }
315
316 MPI_Errhandler PMPI_Errhandler_f2c(MPI_Fint errhan){
317   if(errhan==-1)
318     return MPI_ERRHANDLER_NULL;
319   return simgrid::smpi::Errhandler::f2c(errhan);
320 }
321
322 MPI_Fint PMPI_Errhandler_c2f(MPI_Errhandler errhan){
323   if(errhan==MPI_ERRHANDLER_NULL)
324     return -1;
325   return errhan->c2f();
326 }
327
328 int PMPI_Comm_create_errhandler( MPI_Comm_errhandler_fn *function, MPI_Errhandler *errhandler){
329   return MPI_Errhandler_create(function, errhandler);
330 }
331 int PMPI_Comm_get_errhandler(MPI_Comm comm, MPI_Errhandler* errhandler){
332   return PMPI_Errhandler_get(comm, errhandler);
333 }
334 int PMPI_Comm_set_errhandler(MPI_Comm comm, MPI_Errhandler errhandler){
335   return PMPI_Errhandler_set(comm, errhandler);
336 }