Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9b0779bcb58c19164379919d44f5c43a7bb0ae06
[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, const int* dims, const 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     for (int i = 0; i < ndims; i++)
24       if (dims[i] < 0)
25         return MPI_ERR_ARG;
26
27     simgrid::smpi::Topo_Cart* topo = new simgrid::smpi::Topo_Cart(comm_old, ndims, dims, periodic, reorder, comm_cart);
28     if (*comm_cart == MPI_COMM_NULL) {
29       delete topo;
30     } else {
31       xbt_assert((*comm_cart)->topo() == topo);
32     }
33     return MPI_SUCCESS;
34   }
35 }
36
37 int PMPI_Cart_rank(MPI_Comm comm, const int* coords, int* rank) {
38   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
39     return MPI_ERR_TOPOLOGY;
40   }
41   if (coords == nullptr) {
42     return MPI_SUCCESS;
43   }
44   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
45   if (topo==nullptr) {
46     return MPI_ERR_ARG;
47   }
48   return topo->rank(coords, rank);
49 }
50
51 int PMPI_Cart_shift(MPI_Comm comm, int direction, int displ, int* source, int* dest) {
52   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
53     return MPI_ERR_TOPOLOGY;
54   }
55   if (source == nullptr || dest == nullptr || direction < 0 ) {
56     return MPI_ERR_ARG;
57   }
58   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
59   if (topo==nullptr) {
60     return MPI_ERR_ARG;
61   }
62   return topo->shift(direction, displ, source, dest);
63 }
64
65 int PMPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int* coords) {
66   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
67     return MPI_ERR_TOPOLOGY;
68   }
69   if (rank < 0 || rank >= comm->size()) {
70     return MPI_ERR_RANK;
71   }
72   if (maxdims < 0) {
73     return MPI_ERR_ARG;
74   }
75   if(maxdims==0 || coords == nullptr) {
76     return MPI_SUCCESS;
77   }
78   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
79   if (topo==nullptr) {
80     return MPI_ERR_ARG;
81   }
82   return topo->coords(rank, maxdims, coords);
83 }
84
85 int PMPI_Cart_get(MPI_Comm comm, int maxdims, int* dims, int* periods, int* coords) {
86   if(dims == nullptr || periods == nullptr || coords == nullptr){
87     return MPI_SUCCESS;
88   }
89   if(comm == nullptr || comm->topo() == nullptr) {
90     return MPI_ERR_TOPOLOGY;
91   }
92   if(maxdims <= 0) {
93     return MPI_ERR_ARG;
94   }
95   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
96   if (topo==nullptr) {
97     return MPI_ERR_ARG;
98   }
99   return topo->get(maxdims, dims, periods, coords);
100 }
101
102 int PMPI_Cartdim_get(MPI_Comm comm, int* ndims) {
103   if (comm == MPI_COMM_NULL || comm->topo() == nullptr) {
104     return MPI_ERR_TOPOLOGY;
105   }
106   if (ndims == nullptr) {
107     return MPI_ERR_ARG;
108   }
109   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
110   if (topo==nullptr) {
111     return MPI_ERR_ARG;
112   }
113   return topo->dim_get(ndims);
114 }
115
116 int PMPI_Dims_create(int nnodes, int ndims, int* dims) {
117   if(dims == nullptr) {
118     return MPI_SUCCESS;
119   }
120   if (ndims < 1 || nnodes < 1) {
121     return MPI_ERR_DIMS;
122   }
123   return simgrid::smpi::Topo_Cart::Dims_create(nnodes, ndims, dims);
124 }
125
126 int PMPI_Cart_sub(MPI_Comm comm, const int* remain_dims, MPI_Comm* comm_new) {
127   if(comm == MPI_COMM_NULL || comm->topo() == nullptr) {
128     return MPI_ERR_TOPOLOGY;
129   }
130   if (comm_new == nullptr) {
131     return MPI_ERR_ARG;
132   }
133   MPIR_Cart_Topology topo = static_cast<MPIR_Cart_Topology>(comm->topo());
134   if (topo==nullptr) {
135     return MPI_ERR_ARG;
136   }
137   MPIR_Cart_Topology cart = topo->sub(remain_dims, comm_new);
138   if(*comm_new==MPI_COMM_NULL)
139       delete cart;
140   if(cart==nullptr)
141     return  MPI_ERR_ARG;
142   return MPI_SUCCESS;
143 }