Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to create slice of device memory #293

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

- Added compatibility with HIP
- Added `cu::Device::getArch()`
- Added `cu::DeviceMemory` constructor to create non-owning slice of another
`cu::DeviceMemory` object

### Changed

Expand Down
9 changes: 9 additions & 0 deletions include/cudawrappers/cu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,15 @@ class DeviceMemory : public Wrapper<CUdeviceptr> {
checkCudaCall(cuMemHostGetDevicePointer(&_obj, hostMemory, 0));
}

explicit DeviceMemory(const DeviceMemory &other, size_t offset, size_t size)
: _size(size) {
if (size + offset > other.size()) {
throw Error(CUDA_ERROR_INVALID_VALUE);
}
_obj = reinterpret_cast<CUdeviceptr>(reinterpret_cast<char *>(other._obj) +
offset);
}

void zero(size_t size) { checkCudaCall(cuMemsetD8(_obj, 0, size)); }

const void *parameter()
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ TEST_CASE("Test zeroing cu::DeviceMemory", "[zero]") {
CHECK_THROWS(
cu::DeviceMemory(size, CU_MEMORYTYPE_DEVICE, CU_MEM_ATTACH_HOST));
}

SECTION("Test cu::DeviceMemory offset") {
const size_t size = 1024;
const size_t offset = 512;
const size_t slice_size = 512;
cu::DeviceMemory mem(size);
CHECK_NOTHROW(cu::DeviceMemory(mem, offset, slice_size));
}

SECTION("Test cu::DeviceMemory invalid offset") {
const size_t size = 1024;
const size_t offset = 512;
const size_t slice_size = 1024;
cu::DeviceMemory mem(size);
CHECK_THROWS(cu::DeviceMemory(mem, offset, slice_size));
}
}

TEST_CASE("Test cu::Stream", "[stream]") {
Expand Down
Loading