MPI_Interface.cpp 1.77 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
16
17
18
19
20
21
22
23

/** @file mpi_interface/MPI_Interface.hpp
 *  @brief Implements the MPI interface for the calculations
 *
 *  @author Thomas A. R. Purcell (tpurcell90)
 *  @bug No known bugs.
 *
 *  This class modifies the boost mpi::communicator for easier communication
 */

Thomas Purcell's avatar
Thomas Purcell committed
24
#include "mpi_interface/MPI_Interface.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
25

Thomas Purcell's avatar
Thomas Purcell committed
26
27
28
MPI_Interface::MPI_Interface() : boost::mpi::communicator()
{}

29
std::array<int, 2> MPI_Interface::get_start_end_from_list(const int sz, const int start)
Thomas Purcell's avatar
Thomas Purcell committed
30
31
32
33
34
35
36
37
38
39
{
    int els_per_rank = sz / size();
    int remaineder = sz % size();

    std::array<int, 2> start_end;
    start_end[0] = start + els_per_rank * rank() + std::min(rank(), remaineder);
    start_end[1] = start + els_per_rank * (rank() + 1) + std::min(rank() + 1, remaineder);

    return start_end;
}
Thomas Purcell's avatar
Thomas Purcell committed
40
41
42
43
44
45
46
47
48

std::shared_ptr<MPI_Interface> mpi_setup::comm;

void mpi_setup::init_mpi_env()
{
    if(env == 0)
    {
        #ifdef BOOST_MPI_HAS_NOARG_INITIALIZATION
            env = new boost::mpi::environment();
49
            comm = std::make_shared<MPI_Interface>();
Thomas Purcell's avatar
Thomas Purcell committed
50
51
52
53
54
55
56
57
58
59
        #else
            throw std::runtime_error("MPI cannot be initialized without arguments");
        #endif
    }
}

void mpi_setup::finalize_mpi_env()
{
    delete env;
    env = 0;
60
}