Skip to content

Commit

Permalink
Add option to create slice of device memory
Browse files Browse the repository at this point in the history
  • Loading branch information
loostrum committed Jul 19, 2024
1 parent c55a698 commit c8c9296
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/cudawrappers/cu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ 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

0 comments on commit c8c9296

Please sign in to comment.