MPI_Interface.hpp 2.75 KB
Newer Older
1
// Copyright 2021 Thomas A. R. Purcell
2
//
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
14

15
/** @file mpi_interface/MPI_Interface.hpp
16
 *  @brief Defines the MPI interface for the calculations
17
 *
18
 *  @author Thomas A. R. Purcell (tpurcell90)
19
 *  @bug No known bugs.
20
21
 *
 *  This class modifies the boost mpi::communicator for easier communication
22
 */
23

Thomas Purcell's avatar
Thomas Purcell committed
24
25
26
27
28
29
30
31
#ifndef SISSO_MPI_INTERFACE
#define SISSO_MPI_INTERFACE

#include <boost/mpi.hpp>
#include <array>

namespace mpi = boost::mpi;

32
// DocString: cls_mpi_interface
Thomas Purcell's avatar
Thomas Purcell committed
33
/**
34
 * @brief Augment the boost MPI communicator class with some other useful data and other functions
Thomas Purcell's avatar
Thomas Purcell committed
35
36
 *
 */
Thomas Purcell's avatar
Thomas Purcell committed
37
class MPI_Interface : public mpi::communicator
Thomas Purcell's avatar
Thomas Purcell committed
38
39
40
41
42
43
44
45
46
47
{
public:
    /**
     * @brief      Constructor for the MPI_Interface
     */
    MPI_Interface();

    /**
     * @brief      Unique int tag generator
     *
48
49
50
51
     * @param[in]  proc_send   sending process
     * @param[in]  proc_recv   receiving process
     * @param[in]  max_offset  number of different communication processes possible between two processes within the same operation
     * @param[in]  offset     the assigned offset corresponding to a single communication within the same operation
Thomas Purcell's avatar
Thomas Purcell committed
52
53
54
     *
     * @return     A unique tag to send information between two processes
     */
55
    int cantor_tag_gen(const unsigned int proc_send, const unsigned int proc_recv, const unsigned int max_offset, const unsigned int offset)
56
    {
57
        return (int((proc_send + proc_recv) * (proc_send + proc_send +1) / 2) + proc_recv) * max_offset + offset;
58
    }
Thomas Purcell's avatar
Thomas Purcell committed
59

60
61
62
63
64
65
66
67
    /**
     * @brief get the start and end index for splitting up a list across MPI ranks
     *
     * @param sz size of the list to split up
     * @param start the starting point of where to begin the split in the list
     *
     * @return The start and end indexes of what this rank is responsible for
     */
68
    std::array<int, 2> get_start_end_from_list(const int sz, const int start=0);
Thomas Purcell's avatar
Thomas Purcell committed
69
70
71

};

Thomas Purcell's avatar
Thomas Purcell committed
72
73
namespace mpi_setup
{
74
75
    static mpi::environment* env = 0; //!< The MPI environment
    extern std::shared_ptr<MPI_Interface> comm; //!< The MPI communicator
Thomas Purcell's avatar
Thomas Purcell committed
76

77
78
79
80
    /**
     * @brief Initialize MPI enviroment
     *
     */
Thomas Purcell's avatar
Thomas Purcell committed
81
82
    void init_mpi_env();

83
84
85
    /**
     * @brief Finalize the mpi::enviroment at the end of the calculation.
     */
Thomas Purcell's avatar
Thomas Purcell committed
86
87
88
    void finalize_mpi_env();
}

89
#endif