Logo AND Algorithmique Numérique Distribuée

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