Logo AND Algorithmique Numérique Distribuée

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