Skip to content

Commit ea8c7f0

Browse files
committed
[window] Consistency for window allocate functions
1 parent dc2e732 commit ea8c7f0

3 files changed

Lines changed: 38 additions & 83 deletions

File tree

src/mpiwcpp17/flag.hpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ MPIWCPP17_BEGIN_NAMESPACE
1212

1313
namespace flag
1414
{
15-
MPIWCPP17_INLINE namespace payload
15+
inline namespace payload
1616
{
1717
/**
1818
* Guarantees that the quantity of message elements is uniform across all
@@ -30,7 +30,7 @@ namespace flag
3030
typedef struct{} varying_t;
3131
}
3232

33-
MPIWCPP17_INLINE namespace functor
33+
inline namespace functor
3434
{
3535
/**
3636
* Indicates that a functor is commutative, allowing MPI to perform optimizations
@@ -39,23 +39,6 @@ namespace flag
3939
*/
4040
typedef struct{} commutative_t;
4141
}
42-
43-
MPIWCPP17_INLINE namespace window
44-
{
45-
/**
46-
* Indicates that the allocated memory for RMA operations is local to each
47-
* process and cannot be accessed directly.
48-
* @since 2.1
49-
*/
50-
typedef struct{} local_t;
51-
52-
/**
53-
* Indicates that the allocated memory for RMA operations is shared between
54-
* processes and can be accessed directly by processes in the same communicator.
55-
* @since 2.1
56-
*/
57-
typedef struct{} shared_t;
58-
}
5942
}
6043

6144
MPIWCPP17_END_NAMESPACE

src/mpiwcpp17/memory.hpp

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,41 @@
1010

1111
#include <cstdint>
1212
#include <utility>
13-
#include <memory>
1413

1514
#include <mpiwcpp17/environment.h>
1615
#include <mpiwcpp17/guard.hpp>
17-
#include <mpiwcpp17/global.hpp>
18-
#include <mpiwcpp17/communicator.hpp>
19-
#include <mpiwcpp17/window.hpp>
2016
#include <mpiwcpp17/info.hpp>
2117

2218
MPIWCPP17_BEGIN_NAMESPACE
2319

2420
namespace memory
2521
{
2622
/**
27-
* Allocates speciallized memory for RMA operations. The allocated memory is
28-
* not shared and thus can not be accessed directly by other processes.
23+
* Allocates speciallized memory for RMA operations.
24+
* The allocated memory is not shared and must be manually freed by the user.
2925
* @tparam T The type of the elements to store in the allocated memory region.
3026
* @param count The number of elements or bytes to allocate memory for each process.
3127
* @param info The key-value information instance to attach to allocated memory.
3228
* @return The smart pointer to the allocated region.
3329
*/
3430
template <typename T = void>
35-
MPIWCPP17_INLINE auto allocate(size_t count = 1, const info_t& info = info::null)
31+
MPIWCPP17_INLINE T* allocate(size_t count = 1, const info_t& info = info::null)
3632
{
37-
using element_t = std::conditional_t<std::is_void_v<T>, uint8_t, T>;
38-
using region_t = std::conditional_t<std::is_void_v<T>, void, element_t[]>;
39-
constexpr size_t size = sizeof(element_t);
40-
41-
auto ptr = MPIWCPP17_GUARD_CALL(T*, MPI_Alloc_mem(count * size, info, &_));
42-
auto fn = [](T* ptr) -> void { guard(MPI_Free_mem(ptr)); };
43-
44-
return std::unique_ptr<region_t, decltype(fn)>(ptr, fn);
33+
constexpr size_t size = sizeof(std::conditional_t<std::is_void_v<T>, uint8_t, T>);
34+
return MPIWCPP17_GUARD_CALL(T*, MPI_Alloc_mem(count * size, info, &_));
4535
}
4636

4737
/**
48-
* Allocates contiguous memory for processes in a shared-memory communicator.
49-
* The allocated memory region can be accessed directly by address from processes
50-
* within the same physical node. Therefore, relative addesses can be calculated
51-
* from variable known by each process individually.
52-
* @tparam T The type of the elements to store in the allocated memory region.
53-
* @param count The number of elements or bytes to allocate memory for each process.
54-
* @param comm Communicator allowed for RMA operations with allocated memory.
55-
* @param info The key-value information instance to attach to allocated memory.
56-
* @return The smart pointer to the beginning of the shared memory region.
57-
* @see mpi::window::allocate_shared
38+
* Frees a manually allocated RMA-specialized memory region.
39+
* Destructors must be manually called before freeing the memory, if necessary.
40+
* @tparam T The type of the elements to stored the given memory region.
41+
* @param ptr The pointer to the memory region to be released.
42+
* @see mpi::memory::allocate
5843
*/
5944
template <typename T = void>
60-
MPIWCPP17_INLINE auto allocate_shared(
61-
size_t count = 1
62-
, const communicator_t& comm = world
63-
, const info_t& info = info::null
64-
) {
65-
using element_t = std::conditional_t<std::is_void_v<T>, uint8_t, T>;
66-
using region_t = std::conditional_t<std::is_void_v<T>, void, element_t[]>;
67-
68-
auto [base, w] = window::allocate_shared<T>(count, comm, info);
69-
auto [gptr, _] = window::query_shared<T>(w, process::root);
70-
auto fn = [w = std::move(w)](T*) -> void {};
71-
72-
return std::unique_ptr<region_t, decltype(fn)>(gptr, std::move(fn));
45+
MPIWCPP17_INLINE void free(T *ptr)
46+
{
47+
return MPIWCPP17_GUARD_EVAL(MPI_Free_mem(ptr));
7348
}
7449
}
7550

src/mpiwcpp17/window.hpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <utility>
1414

1515
#include <mpiwcpp17/environment.h>
16-
#include <mpiwcpp17/flag.hpp>
1716
#include <mpiwcpp17/guard.hpp>
1817
#include <mpiwcpp17/global.hpp>
1918
#include <mpiwcpp17/process.hpp>
@@ -51,23 +50,23 @@ struct window_t : MPIWCPP17_INHERIT_HANDLE(MPI_Win, MPI_Win_free);
5150
* @param B The call block to be wrapped.
5251
*/
5352
#define MPIWCPP17_WIN_RAII(x) window_t((x), true)
54-
#define MPIWCPP17_WIN_CALL(B) MPIWCPP17_WIN_RAII(MPIWCPP17_GUARD_CALL(window_t, B))
53+
#define MPIWCPP17_WIN_CALL(B) MPIWCPP17_WIN_RAII(MPIWCPP17_GUARD_CALL(MPI_Win, B))
5554

5655
namespace window
5756
{
5857
/**
5958
* The invalid or empty window instance.
6059
* This can be used to verify whether a window is not in a valid state or to
6160
* denote an empty or uninitialized window.
62-
* @since 3.0
61+
* @since 2.1
6362
*/
6463
MPIWCPP17_INLINE const window_t null = MPI_WIN_NULL;
6564

6665
/**
6766
* Declares window attribute namespace and corresponding functions.
6867
* Attributes are identified by keys that can be used to attach and retrieve
6968
* generic data from window instances.
70-
* @since 3.0
69+
* @since 2.1
7170
*/
7271
MPIWCPP17_ATTRIBUTE_DECLARE(
7372
window_t
@@ -80,66 +79,66 @@ namespace window
8079
* The window synchronization modes. These modes can be used to specify the type
8180
* of synchronization to be performed on a window when calling the corresponding
8281
* synchronization functions.
83-
* @since 3.0
82+
* @since 2.1
8483
*/
8584
enum mode_t : int
8685
{
8786
/**
8887
* No special synchronization mode. This is the default synchronization mode
8988
* for all synchronization functions, no guarantees given whatsoever.
90-
* @since 3.0
89+
* @since 2.1
9190
*/
9291
none = 0
9392

9493
/**
9594
* The mode for no preceding RMA operations. This mode indicates that no
9695
* RMA operations will be issued before synchronization.
97-
* @since 3.0
96+
* @since 2.1
9897
*/
9998
, no_precede = MPI_MODE_NOPRECEDE
10099

101100
/**
102101
* The mode for no preceding local stores or get calls. This mode indicates
103102
* that no local stores or get calls will be issued before synchronization.
104-
* @since 3.0
103+
* @since 2.1
105104
*/
106105
, no_store = MPI_MODE_NOSTORE
107106

108107
/**
109108
* The mode for no updates by any put or accumulate calls. This mode indicates
110109
* that no put or accumulate calls will be performed until the next synchronization.
111-
* @since 3.0
110+
* @since 2.1
112111
*/
113112
, no_put = MPI_MODE_NOPUT
114113

115114
/**
116115
* The mode for no succeeding RMA operations. This mode indicates that no
117116
* RMA operations will be issued after synchronization.
118-
* @since 3.0
117+
* @since 2.1
119118
*/
120119
, no_succeed = MPI_MODE_NOSUCCEED
121120
};
122121

123122
/**
124123
* The window lock types. These types can be used to specify the type of lock
125124
* to be acquired on a window when calling the corresponding lock functions.
126-
* @since 3.0
125+
* @since 2.1
127126
*/
128127
enum lock_t : int
129128
{
130129
/**
131130
* The exclusive lock type. This lock type indicates that the calling process
132131
* will acquire an exclusive lock on the window, which prevents any other
133132
* process from performing access operations on the window.
134-
* @since 3.0
133+
* @since 2.1
135134
*/
136135
exclusive = MPI_LOCK_EXCLUSIVE
137136

138137
/**
139138
* The shared lock type. This lock type indicates that the calling process
140139
* will acquire a shared lock on the window, which allows other processes
141140
* to perform access operations as long as they also acquire a shared lock.
142-
* @since 3.0
141+
* @since 2.1
143142
*/
144143
, shared = MPI_LOCK_SHARED
145144
};
@@ -149,28 +148,22 @@ namespace window
149148
* instance enabling RMA communication. The amount of memory allocated may differ
150149
* between processes and is determined individually by its own parameters.
151150
* @tparam T The type of the elements to store in the allocated memory region.
152-
* @tparam G The flag to determine the type of allocated memory.
153151
* @param count The number of elements or bytes to allocate for each process.
154152
* @param comm Communicator allowed for RMA operations with allocated memory.
155153
* @param info The key-value information instance to attach to allocated memory.
156154
* @return The allocated memory pointer and new window instance.
157155
*/
158-
template <
159-
typename T = void
160-
, typename G = flag::window::local_t>
156+
template <typename T = void>
161157
MPIWCPP17_INLINE std::pair<T*, window_t> allocate(
162158
size_t count = 1
163159
, const communicator_t& comm = world
164160
, const info_t& info = info::null
165-
, G = {}
166161
) {
167-
T *ptr;
168162
constexpr size_t size = sizeof(std::conditional_t<std::is_void_v<T>, uint8_t, T>);
169-
auto w = MPIWCPP17_WIN_CALL((
170-
std::is_same_v<flag::window::shared_t, G>
171-
? MPI_Win_allocate_shared(count * size, size, info, comm, &ptr, &_)
172-
: MPI_Win_allocate(count * size, size, info, comm, &ptr, &_)));
173-
return std::make_pair(ptr, std::move(w));
163+
auto result = MPIWCPP17_GUARD_CALL(
164+
struct { T *ptr; window_t::raw_t w; }
165+
, MPI_Win_allocate(count * size, size, info, comm, &_.ptr, &_.w));
166+
return std::make_pair(result.ptr, MPIWCPP17_WIN_RAII(result.w));
174167
}
175168

176169
/**
@@ -190,7 +183,11 @@ namespace window
190183
, const communicator_t& comm = world
191184
, const info_t& info = info::null
192185
) {
193-
return allocate<T, flag::window::shared_t>(count, comm, info);
186+
constexpr size_t size = sizeof(std::conditional_t<std::is_void_v<T>, uint8_t, T>);
187+
auto result = MPIWCPP17_GUARD_CALL(
188+
struct { T *ptr; window_t::raw_t w; }
189+
, MPI_Win_allocate_shared(count * size, size, info, comm, &_.ptr, &_.w));
190+
return std::make_pair(result.ptr, MPIWCPP17_WIN_RAII(result.w));
194191
}
195192

196193
/**
@@ -236,7 +233,7 @@ namespace window
236233
* @param info The key-value information instance to attach to the new window.
237234
* @return The new window instance that manages a memory regions attached dynamically.
238235
*/
239-
MPIWCPP17_INLINE window_t create_dynamic(
236+
MPIWCPP17_INLINE window_t create(
240237
const communicator_t& comm = world
241238
, const info_t& info = info::null
242239
) {

0 commit comments

Comments
 (0)