suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
Distributed.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17
18#ifndef DISTRIBUTED_H
19#define DISTRIBUTED_H
20
21#include <suanPan.h>
22
24 static constexpr int root_rank{0};
25
26 static auto assign_process(const int obj_tag) { return obj_tag % comm_size; }
27
28 const int tag, process_rank;
29
30public:
31 const bool is_local;
32
33 explicit Distributed(const int obj_tag)
34 : tag(obj_tag)
35 , process_rank(assign_process(tag))
36 , is_local(comm_rank == process_rank) {}
37
38#ifdef SUANPAN_DISTRIBUTED
50 template<mpl_data_t DT> std::optional<mpl::irequest> gather(const Mat<DT>& object) {
51 if(root_rank == comm_rank) {
52 if(!is_local) return comm_world.irecv(const_cast<DT*>(object.memptr()), mpl::contiguous_layout<DT>{object.n_elem}, process_rank, mpl::tag_t{tag});
53 }
54 else if(is_local) return comm_world.isend(object.memptr(), mpl::contiguous_layout<DT>{object.n_elem}, root_rank, mpl::tag_t{tag});
55
56 return std::nullopt;
57 }
58#else
59 template<typename T> static auto gather(T&&) {}
60#endif
61};
62
63#endif // DISTRIBUTED_H
Definition Distributed.h:23
static auto gather(T &&)
Definition Distributed.h:59
Distributed(const int obj_tag)
Definition Distributed.h:33
const bool is_local
Definition Distributed.h:31
constexpr auto comm_rank
Definition suanPan.h:235
constexpr auto comm_size
Definition suanPan.h:236