Skip to content

Commit

Permalink
Add doc and example about dtors under Multiple Devices
Browse files Browse the repository at this point in the history
  • Loading branch information
harrism committed Sep 6, 2023
1 parent 9f5c6db commit bbe46bd
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,32 @@ objects for each device and sets them as the per-device resource for that device
```c++
std::vector<unique_ptr<pool_memory_resource>> per_device_pools;
for(int i = 0; i < N; ++i) {
cudaSetDevice(i); // set device i before creating MR
// Use a vector of unique_ptr to maintain the lifetime of the MRs
per_device_pools.push_back(std::make_unique<pool_memory_resource>());
// Set the per-device resource for device i
set_per_device_resource(cuda_device_id{i}, &per_device_pools.back());
cudaSetDevice(i); // set device i before creating MR
// Use a vector of unique_ptr to maintain the lifetime of the MRs
per_device_pools.push_back(std::make_unique<pool_memory_resource>());
// Set the per-device resource for device i
set_per_device_resource(cuda_device_id{i}, &per_device_pools.back());
}
```

Note that the CUDA device that is current when creating a `device_memory_resource` must also be
current any time that `device_memory_resource` is used to deallocate memory, including in a
destructor. This affects RAII classes like `rmm::device_buffer` and `rmm::device_uvector`. Here's an
(incorrect) example that assumes the above example loop has been run to create a
`pool_memory_resource` for each device. A correct example adds a call to `cudaSetDevice(1)` on the
line of the error comment.

```c++
{
RMM_CUDA_TRY(cudaSetDevice(0));
rmm::device_buffer buf_a(16);

{
RMM_CUDA_TRY(cudaSetDevice(1));
rmm::device_buffer buf_b(16);
}

// Error: when buf_a is destroyed, the current device must be 0, but it is 1
}
```

Expand Down

0 comments on commit bbe46bd

Please sign in to comment.