Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add checks for comms and datatypes as well
[simgrid.git] / src / smpi / bindings / smpi_pmpi_topo.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_comm.hpp"
8
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
10
11 /* PMPI User level calls */
12
13 /* The topo part of MPI_COMM_WORLD should always be nullptr. When other topologies will be implemented, not only should we
14  * check if the topology is nullptr, but we should check if it is the good topology type (so we have to add a
15  *  MPIR_Topo_Type field, and replace the MPI_Topology field by an union)*/
16
17 int PMPI_Cart_create(MPI_Comm comm_old, int ndims, int* dims, int* periodic, int reorder, MPI_Comm* comm_cart) {
18   if (comm_old == MPI_COMM_NULL){
19     return MPI_ERR_COMM;
20   } else if (ndims < 0 || (ndims > 0 && (dims == nullptr || periodic == nullptr)) || comm_cart == nullptr) {
21     return MPI_ERR_ARG;
22   } else{
23     simgrid::smpi::Topo_Cart* topo = new simgrid::smpi::Topo_Cart(comm_old, ndims, dims, periodic, reorder, comm_cart);
24     if (*comm_cart == MPI_COMM_NULL) {
25       delete topo;
26     } else {
27       xbt_assert((*comm_cart)->topo() == topo);
28     }
29     return MPI_SUCCESS;
30   }
31 }
32
33 int PMPI_Cart_rank(MPI_Comm comm, int* coords, int* rank) {
34   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
35     return MPI_ERR_TOPOLOGY;
36   }
37   if (coords == nullptr) {
38     return MPI_ERR_ARG;
39   }
40   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
41   if (topo==nullptr) {
42     return MPI_ERR_ARG;
43   }
44   return topo->rank(coords, rank);
45 }
46
47 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
48   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
49     return MPI_ERR_TOPOLOGY;
50   }
51   if (source == nullptr || dest == nullptr || direction < 0 ) {
52     return MPI_ERR_ARG;
53   }
54   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
55   if (topo==nullptr) {
56     return MPI_ERR_ARG;
57   }
58   return topo->shift(direction, displ, source, dest);
59 }
60
61 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
62   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
63     return MPI_ERR_TOPOLOGY;
64   }
65   if (rank < 0 || rank >= comm->size()) {
66     return MPI_ERR_RANK;
67   }
68   if (maxdims <= 0) {
69     return MPI_ERR_ARG;
70   }
71   if(coords == nullptr) {
72     return MPI_ERR_ARG;
73   }
74   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
75   if (topo==nullptr) {
76     return MPI_ERR_ARG;
77   }
78   return topo->coords(rank, maxdims, coords);
79 }
80
81 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
82   if(comm == nullptr || comm->topo() == nullptr) {
83     return MPI_ERR_TOPOLOGY;
84   }
85   if(maxdims <= 0 || dims == nullptr || periods == nullptr || coords == nullptr) {
86     return MPI_ERR_ARG;
87   }
88   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
89   if (topo==nullptr) {
90     return MPI_ERR_ARG;
91   }
92   return topo->get(maxdims, dims, periods, coords);
93 }
94
95 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
96   if (comm == MPI_COMM_NULL || comm->topo() == nullptr) {
97     return MPI_ERR_TOPOLOGY;
98   }
99   if (ndims == nullptr) {
100     return MPI_ERR_ARG;
101   }
102   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
103   if (topo==nullptr) {
104     return MPI_ERR_ARG;
105   }
106   return topo->dim_get(ndims);
107 }
108
109 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
110   if(dims == nullptr) {
111     return MPI_ERR_ARG;
112   }
113   if (ndims < 1 || nnodes < 1) {
114     return MPI_ERR_DIMS;
115   }
116   return simgrid::smpi::Topo_Cart::Dims_create(nnodes, ndims, dims);
117 }
118
119 int PMPI_Cart_sub(MPI_Comm comm, int* remain_dims, MPI_Comm* comm_new) {
120   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
121     return MPI_ERR_TOPOLOGY;
122   }
123   if (comm_new == nullptr) {
124     return MPI_ERR_ARG;
125   }
126   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
127   if (topo==nullptr) {
128     return MPI_ERR_ARG;
129   }
130   MPIR_Cart_Topology cart = topo->sub(remain_dims, comm_new);
131   if(*comm_new==MPI_COMM_NULL)
132       delete cart;
133   if(cart==nullptr)
134     return  MPI_ERR_ARG;
135   return MPI_SUCCESS;
136 }