Skip to content

Commit c74eb1a

Browse files
committed
Add decomposition factory to decouple from mpi
1 parent d38c37f commit c74eb1a

File tree

5 files changed

+68
-46
lines changed

5 files changed

+68
-46
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ add_library(OpenPFC
102102
src/openpfc/core/world.cpp
103103
src/openpfc/core/box3d.cpp
104104
src/openpfc/core/decomposition.cpp
105+
src/openpfc/factory/decomposition_factory.cpp
105106
# Add more .cpp files as you go
106107
)
107108

include/openpfc/core/decomposition.hpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,41 @@
77
#include "openpfc/core/world.hpp"
88
#include <array>
99
#include <heffte.h>
10-
#include <mpi.h>
1110
#include <ostream>
1211
#include <stdexcept>
1312
#include <vector>
1413

1514
namespace pfc {
1615

1716
/**
18-
* @brief Class representing the domain decomposition for parallel Fast Fourier
19-
* Transform.
17+
* @brief Represents a partitioning of the global simulation domain into local
18+
* subdomains.
2019
*
21-
* The Decomposition class defines the domain decomposition for parallel FFT. It
22-
* splits the domain into multiple parts based on the number of processors and
23-
* the grid setup. It provides information about the local domain and
24-
* communication patterns.
20+
* The Decomposition class describes how the World is split among multiple
21+
* processes. Each instance provides access to the local subdomain owned by the
22+
* current process, including size, offset, and basic mappings between global
23+
* and local indices.
24+
*
25+
*
26+
* ## Responsibilities
27+
*
28+
* - Split the World into non-overlapping local boxes.
29+
* - Provide access to local box information.
30+
* - Support global-local coordinate mapping.
31+
*
32+
*
33+
* ## Design Justification
34+
*
35+
* - Keeps World (global domain) and local process view separate.
36+
* - Decouples decomposition logic from communication (MPI, FFT backends).
37+
* - Enables different decomposition strategies if needed (block, slab, pencil).
38+
*
39+
*
40+
* ## Relations to Other Components
41+
*
42+
* - Used by Fields and Arrays to allocate local data.
43+
* - Used by Communication components to know where data lives.
44+
* - Passed to FFT backends as a description of local domains.
2545
*/
2646
class Decomposition {
2747
private:
@@ -33,20 +53,6 @@ class Decomposition {
3353
const std::array<int, 3> proc_grid; ///< Processor grid dimensions.
3454
const std::vector<heffte::box3d<int>> real_boxes, complex_boxes; ///< Local domain boxes.
3555

36-
/**
37-
* @brief Get the rank of the current MPI communicator.
38-
* @param comm The MPI communicator.
39-
* @return The rank of the current process.
40-
*/
41-
int get_comm_rank(MPI_Comm comm);
42-
43-
/**
44-
* @brief Get the size of the current MPI communicator.
45-
* @param comm The MPI communicator.
46-
* @return The size of the communicator (total number of processes).
47-
*/
48-
int get_comm_size(MPI_Comm comm);
49-
5056
public:
5157
const heffte::box3d<int> inbox, outbox; ///< Local communication boxes.
5258
const int r2c_direction = 0; ///< Real-to-complex symmetry direction.
@@ -64,15 +70,6 @@ class Decomposition {
6470
*/
6571
Decomposition(const World &world, int rank, int num_domains);
6672

67-
/**
68-
* @brief Construct a new Decomposition object using MPI communicator. In this
69-
* case, the total number of domains equals the communicator size.
70-
*
71-
* @param world Reference to the World object.
72-
* @param comm The MPI communicator (default: MPI_COMM_WORLD).
73-
*/
74-
Decomposition(const World &world, MPI_Comm comm = MPI_COMM_WORLD);
75-
7673
/**
7774
* @brief Get the size of the inbox.
7875
*
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2+
// SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
#ifndef PFC_DECOMPOSITION_FACTORY_HPP
5+
#define PFC_DECOMPOSITION_FACTORY_HPP
6+
7+
#include "openpfc/core/decomposition.hpp"
8+
#include "openpfc/core/world.hpp"
9+
#include <mpi.h>
10+
11+
namespace pfc {
12+
13+
/**
14+
* @brief Factory function to create a Decomposition from MPI communicator.
15+
*
16+
* @param world The World object.
17+
* @param comm The MPI communicator (defaults to MPI_COMM_WORLD).
18+
* @return Decomposition
19+
*/
20+
Decomposition make_decomposition(const World &world, MPI_Comm comm = MPI_COMM_WORLD);
21+
22+
} // namespace pfc
23+
24+
#endif // PFC_DECOMPOSITION_FACTORY_HPP

src/openpfc/core/decomposition.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,6 @@
99

1010
namespace pfc {
1111

12-
int Decomposition::get_comm_rank(MPI_Comm comm) {
13-
int rank;
14-
MPI_Comm_rank(comm, &rank);
15-
return rank;
16-
}
17-
18-
int Decomposition::get_comm_size(MPI_Comm comm) {
19-
int size;
20-
MPI_Comm_size(comm, &size);
21-
return size;
22-
}
23-
2412
Decomposition::Decomposition(const World &world, int rank, int num_domains)
2513
: m_world(world),
2614
m_rank(rank < num_domains ? rank
@@ -35,10 +23,6 @@ Decomposition::Decomposition(const World &world, int rank, int num_domains)
3523
assert(real_indexes.r2c(r2c_direction) == complex_indexes);
3624
}
3725

38-
Decomposition::Decomposition(const World &world, MPI_Comm comm)
39-
: Decomposition(world, get_comm_rank(comm), get_comm_size(comm)) {
40-
}
41-
4226
const std::array<int, 3> &Decomposition::get_inbox_size() const {
4327
return inbox.size;
4428
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2+
// SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
#include "openpfc/factory/decomposition_factory.hpp"
5+
6+
namespace pfc {
7+
8+
Decomposition make_decomposition(const World &world, MPI_Comm comm) {
9+
int rank;
10+
int size;
11+
MPI_Comm_rank(comm, &rank);
12+
MPI_Comm_size(comm, &size);
13+
return Decomposition(world, rank, size);
14+
}
15+
16+
} // namespace pfc

0 commit comments

Comments
 (0)