Skip to content

Commit 58686c0

Browse files
committed
Fixes #639: Simplify access-permissions-related code:
* Renamed: `access_permissions` -> `permissions` and `access_permissions_t` to `permissions_t`, everwhere * Dropped the boolean enum in favor of parameter-less some named constructor idioms * Dropped the static methods in favor of named constructor idioms * No user code needs to worry about pairs-of-booleans; but they can aggregate-initialize if they really want to
1 parent d023df4 commit 58686c0

File tree

10 files changed

+129
-142
lines changed

10 files changed

+129
-142
lines changed

examples/by_api_module/memory_pools.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,21 @@ void copy_through_pool_allocation(
130130
}
131131
}
132132

133-
cuda::memory::access_permissions_t
133+
cuda::memory::permissions_t
134134
try_forbidding_same_device_access(int device_id, cuda::memory::pool_t &pool)
135135
{
136-
cuda::memory::access_permissions_t permissions;
136+
cuda::memory::permissions_t permissions;
137137
permissions.read = true;
138138
permissions.write = false;
139139
bool got_expected_exception = false;
140140
try {
141-
pool.set_access_permissions(cuda::device::get(device_id), permissions);
141+
pool.set_permissions(cuda::device::get(device_id), permissions);
142142
}
143143
catch(std::invalid_argument&) {
144144
got_expected_exception = true;
145145
}
146146
if (not got_expected_exception) {
147-
die_("Unexpected success in setting a device's access permissions to a pool using"
147+
die_("Unexpected success in setting a device's access get_permissions to a pool using"
148148
"that device; it should have failed");
149149
}
150150
return permissions;
@@ -155,12 +155,12 @@ void try_writing_to_pool_allocation_without_permission(
155155
const cuda::stream_t& stream,
156156
cuda::memory::pool_t& pool,
157157
cuda::memory::region_t& pool_allocated_region,
158-
cuda::memory::access_permissions_t& permissions,
158+
cuda::memory::permissions_t& permissions,
159159
cuda::device_t& peer)
160160
{
161161
permissions.read = false;
162162
permissions.write = false;
163-
pool.set_access_permissions(peer, permissions);
163+
pool.set_permissions(peer, permissions);
164164
std::string str{"hello world"};
165165
stream.synchronize();
166166
auto stream_on_peer = peer.create_stream(cuda::stream::async);
@@ -184,12 +184,12 @@ void try_writing_to_pool_allocation_without_permission(
184184
void try_reading_from_pool_allocation_without_permission(
185185
cuda::memory::pool_t& pool,
186186
cuda::memory::region_t& pool_allocated_region,
187-
cuda::memory::access_permissions_t& permissions,
187+
cuda::memory::permissions_t& permissions,
188188
cuda::device_t& peer)
189189
{
190190
permissions.read = false;
191191
permissions.write = false;
192-
pool.set_access_permissions(peer, permissions);
192+
pool.set_permissions(peer, permissions);
193193
auto host_buffer_uptr = std::unique_ptr<char[]>(new char[region_size]); // replace this with make_unique in C++14
194194
auto host_buffer = cuda::span<char>{host_buffer_uptr.get(), region_size};
195195
std::fill_n(host_buffer.begin(), host_buffer.size()-1, 'a');
@@ -223,7 +223,7 @@ int main(int argc, char** argv)
223223
play_with_attributes(pool, stream);
224224
copy_through_pool_allocation(stream, pool_allocated_region);
225225

226-
cuda::memory::access_permissions_t permissions = try_forbidding_same_device_access(device_id, pool);
226+
cuda::memory::permissions_t permissions = try_forbidding_same_device_access(device_id, pool);
227227

228228
auto maybe_peer_id = maybe_get_p2p_peer_id(device_id);
229229
if (maybe_peer_id) {

examples/modified_cuda_samples/memMapIPCDrv/child.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ std::vector<memory_mapping_t> import_and_map_allocations(
5757
});
5858

5959
// Retain peer access and map all chunks to mapDevice
60-
virtual_mem::set_access_mode(mappings_region, device,
61-
cuda::memory::access_permissions_t::read_and_write());
60+
virtual_mem::set_permissions(mappings_region, device, cuda::memory::permissions::read_and_write());
6261
return mappings;
6362
}
6463

examples/modified_cuda_samples/streamOrderedAllocationIPC/streamOrderedAllocationIPC.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ static void childProcess(int index_in_shared_devices)
146146
auto shared_pool_handle = shared_pool_handles[i];
147147
auto pool_device = cuda::device::get(shm->devices[i]);
148148
auto pool = cuda::memory::pool::ipc::import<shared_handle_kind>(pool_device, shared_pool_handle);
149-
auto permissions = pool.access_permissions(device);
149+
auto permissions = pool.permissions(device);
150150
if (not (permissions.read and permissions.write)) {
151-
pool.set_access_permissions(device, cuda::memory::read_enabled, cuda::memory::write_enabled);
151+
pool.set_permissions(device, cuda::memory::permissions::read_and_write());
152152
}
153153

154154
// Import the allocations from each memory pool

examples/modified_cuda_samples/streamOrderedAllocationP2P/streamOrderedAllocationP2P.cu

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ int memPoolP2PCopy()
171171

172172
auto stream2 = p2pDevices.second.create_stream(cuda::stream::async);
173173
auto output_on_device = cuda::span<int>(stream2.enqueue.allocate(nelem * sizeof(int)));
174-
memPool.set_access_permissions(p2pDevices.second,
175-
cuda::memory::read_enabled, cuda::memory::write_enabled);
174+
memPool.set_permissions(p2pDevices.second, cuda::memory::permissions::read_and_write());
176175

177176
std::cout << "> copyP2PAndScale kernel running ...\n";
178177
auto launch_config = cuda::launch_config_builder()

examples/modified_cuda_samples/vectorAddMMAP/vectorAddMMAP.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,13 @@ setup_virtual_memory(
167167
);
168168

169169
#ifndef _MSC_VER
170-
virtual_mem::set_access_mode(reserved_range.region(), mapping_devices,
171-
cuda::memory::access_permissions_t::read_and_write());
170+
virtual_mem::set_permissions(reserved_range.region(), mapping_devices, cuda::memory::permissions::read_and_write());
172171
#else
173172
// MSVC, at least as of 2019, can't handle template-template parameters with variadcs properly;
174173
// so let's go manual:
175174
for(const auto& mapping_device : mapping_devices) {
176-
virtual_mem::set_access_mode(reserved_range.region(), mapping_device,
177-
cuda::memory::access_permissions_t::read_and_write());
175+
virtual_mem::set_permissions(reserved_range.region(), mapping_device,
176+
cuda::memory::permissions::read_and_write());
178177
}
179178
#endif
180179

src/cuda/api/memory_pool.hpp

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,21 @@ pool_t wrap(cuda::device::id_t device_id, pool::handle_t handle, bool owning) no
159159

160160
namespace detail_ {
161161

162-
inline access_permissions_t access_permissions(cuda::device::id_t device_id, pool::handle_t pool_handle)
162+
inline permissions_t get_permissions(cuda::device::id_t device_id, pool::handle_t pool_handle)
163163
{
164164
CUmemAccess_flags access_flags;
165165
auto mem_location = pool::detail_::create_mem_location(device_id);
166166
auto status = cuMemPoolGetAccess(&access_flags, pool_handle, &mem_location);
167167
throw_if_error_lazy(status,
168168
"Determining access information for " + cuda::device::detail_::identify(device_id)
169169
+ " to " + pool::detail_::identify(pool_handle));
170-
return access_permissions_t::from_access_flags(access_flags);
170+
return permissions::detail_::from_flags(access_flags);
171171
}
172172

173-
inline void set_access_permissions(span<cuda::device::id_t> device_ids, pool::handle_t pool_handle, access_permissions_t permissions)
173+
inline void set_permissions(span<cuda::device::id_t> device_ids, pool::handle_t pool_handle, permissions_t permissions)
174174
{
175175
if (permissions.write and not permissions.read) {
176-
throw ::std::invalid_argument("Memory pool access permissions cannot be write-only");
176+
throw ::std::invalid_argument("Memory pool access get_permissions cannot be write-only");
177177
}
178178

179179
CUmemAccess_flags flags = permissions.read ?
@@ -192,14 +192,14 @@ inline void set_access_permissions(span<cuda::device::id_t> device_ids, pool::ha
192192

193193
auto status = cuMemPoolSetAccess(pool_handle, descriptors.data(), descriptors.size());
194194
throw_if_error_lazy(status,
195-
"Setting access permissions for " + ::std::to_string(descriptors.size())
195+
"Setting access get_permissions for " + ::std::to_string(descriptors.size())
196196
+ " devices to " + pool::detail_::identify(pool_handle));
197197
}
198198

199-
inline void set_access_permissions(cuda::device::id_t device_id, pool::handle_t pool_handle, access_permissions_t permissions)
199+
inline void set_permissions(cuda::device::id_t device_id, pool::handle_t pool_handle, permissions_t permissions)
200200
{
201201
if (permissions.write and not permissions.read) {
202-
throw ::std::invalid_argument("Memory pool access permissions cannot be write-only");
202+
throw ::std::invalid_argument("Memory pool access get_permissions cannot be write-only");
203203
}
204204

205205
CUmemAccessDesc desc;
@@ -212,16 +212,16 @@ inline void set_access_permissions(cuda::device::id_t device_id, pool::handle_t
212212
desc.location = pool::detail_::create_mem_location(device_id);
213213
auto status = cuMemPoolSetAccess(pool_handle, &desc, 1);
214214
throw_if_error_lazy(status,
215-
"Setting access permissions for " + cuda::device::detail_::identify(device_id)
215+
"Setting access get_permissions for " + cuda::device::detail_::identify(device_id)
216216
+ " to " + pool::detail_::identify(pool_handle));
217217
}
218218

219219
} // namespace detail_
220220

221-
access_permissions_t access_permissions(const cuda::device_t& device, const pool_t& pool);
222-
void set_access_permissions(const cuda::device_t& device, const pool_t& pool, access_permissions_t permissions);
221+
permissions_t get_permissions(const cuda::device_t& device, const pool_t& pool);
222+
void set_permissions(const cuda::device_t& device, const pool_t& pool, permissions_t permissions);
223223
template <typename DeviceRange>
224-
void set_access_permissions(DeviceRange devices, const pool_t& pool_handle, access_permissions_t permissions);
224+
void get_permissions(DeviceRange devices, const pool_t& pool_handle, permissions_t permissions);
225225

226226
namespace pool {
227227

@@ -302,48 +302,38 @@ class pool_t {
302302
set_attribute<CU_MEMPOOL_ATTR_RELEASE_THRESHOLD>(threshold);
303303
}
304304

305-
access_permissions_t access_permissions(const cuda::device_t& device)
305+
permissions_t permissions(const cuda::device_t& device)
306306
{
307-
return memory::access_permissions(device, *this);
307+
return memory::get_permissions(device, *this);
308308
}
309309

310310
/**
311-
* Set read and write permissions from a device to the allocations from
311+
* Set read and write get_permissions from a device to the allocations from
312312
* this pool
313313
*
314-
* @param device the device the kernels running on which are governed by these permissions
315-
* @param permissions new read and write permissions to use
314+
* @param device the device the kernels running on which are governed by these get_permissions
315+
* @param permissions new read and write get_permissions to use
316316
*
317317
* @note This affects both future _and past_ allocations from this pool.
318318
*/
319319
///@{
320320
/**
321321
* @param device the device the kernels running on which are governed by this new setting
322-
* @param permissions new read and write permissions to use
322+
* @param permissions new read and write get_permissions to use
323323
*/
324-
void set_access_permissions(const cuda::device_t& device, access_permissions_t permissions)
324+
void set_permissions(const cuda::device_t& device, permissions_t permissions)
325325
{
326-
return memory::set_access_permissions(device, *this, permissions);
327-
}
328-
329-
/**
330-
* @param device the device the kernels running on which are governed by this new setting
331-
* @param read_permission true if kernels are allowed to read from memory allocated by this pool
332-
* @param write_permission true if kernels are allowed to write to memory allocated by this pool
333-
*/
334-
void set_access_permissions(const cuda::device_t& device, bool read_permission, bool write_permission)
335-
{
336-
set_access_permissions(device, access_permissions_t{read_permission, write_permission});
326+
return memory::set_permissions(device, *this, permissions);
337327
}
338328

339329
/**
340330
* @param device the devices the kernels running on which are governed by this new setting
341-
* @param permissions new read and write permissions to use
331+
* @param permissions new read and write get_permissions to use
342332
*/
343333
template <typename DeviceRange>
344-
void set_access_permissions(DeviceRange devices, access_permissions_t permissions)
334+
void set_permissions(DeviceRange devices, permissions_t permissions)
345335
{
346-
return memory::set_access_permissions(devices, *this, permissions);
336+
return memory::set_permissions(devices, *this, permissions);
347337
}
348338
///@}
349339

src/cuda/api/multi_wrapper_impls/memory.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,29 +512,28 @@ inline pool::ipc::imported_ptr_t pool_t::import(const memory::pool::ipc::ptr_han
512512
return pool::ipc::import_ptr(*this, exported_handle);
513513
}
514514

515-
inline access_permissions_t access_permissions(const cuda::device_t& device, const pool_t& pool)
515+
inline permissions_t get_permissions(const cuda::device_t& device, const pool_t& pool)
516516
{
517-
return cuda::memory::detail_::access_permissions(device.id(), pool.handle());
517+
return cuda::memory::detail_::get_permissions(device.id(), pool.handle());
518518
}
519519

520-
inline void set_access_permissions(const cuda::device_t& device, const pool_t& pool, access_permissions_t permissions)
520+
inline void set_permissions(const cuda::device_t& device, const pool_t& pool, permissions_t permissions)
521521
{
522522
if (pool.device_id() == device.id()) {
523-
throw ::std::invalid_argument("Cannot change the access permissions to a pool of the device "
523+
throw ::std::invalid_argument("Cannot change the access get_permissions to a pool of the device "
524524
"on which the pool's memory is allocated (" + cuda::device::detail_::identify(device.id()) + ')');
525525
}
526-
cuda::memory::detail_::set_access_permissions(device.id(), pool.handle(), permissions);
526+
cuda::memory::detail_::set_permissions(device.id(), pool.handle(), permissions);
527527
}
528528

529529
template <typename DeviceRange>
530-
void set_access_permissions(DeviceRange devices, const pool_t& pool, access_permissions_t permissions)
530+
void set_permissions(DeviceRange devices, const pool_t& pool, permissions_t permissions)
531531
{
532532
// Not depending on unique_span here :-(
533533
auto device_ids = ::std::unique_ptr<cuda::device::id_t[]>(new cuda::device::id_t[devices.size()]);
534534
auto device_to_id = [](device_t const& device){ return device.id(); };
535535
::std::transform(::std::begin(devices), ::std::end(devices), device_ids.get(), device_to_id);
536-
cuda::memory::detail_::set_access_permissions(
537-
{ device_ids.get(), devices.size() }, pool.handle(), permissions);
536+
cuda::memory::detail_::set_permissions( { device_ids.get(), devices.size() }, pool.handle(), permissions);
538537
}
539538
#endif // #if CUDA_VERSION >= 11020
540539

0 commit comments

Comments
 (0)