Skip to content
Open
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
5 changes: 3 additions & 2 deletions include/nvexec/detail/memory.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,21 @@ namespace nv::execution::_strm
if (status = STDEXEC_LOG_CUDA_API(::cudaMalloc(reinterpret_cast<void**>(&ptr), sizeof(Type)));
status == cudaSuccess)
{
device_ptr_t<Type> result{ptr};

STDEXEC_TRY
{
Type h(static_cast<Args&&>(args)...);
status = STDEXEC_LOG_CUDA_API(
::cudaMemcpy(ptr, &h, sizeof(Type), cudaMemcpyHostToDevice));
if (status == cudaSuccess)
{
return device_ptr_t<Type>(ptr);
return result;
}
}
STDEXEC_CATCH_ALL
{
status = cudaErrorUnknown;
STDEXEC_ASSERT_CUDA_API(::cudaFree(ptr));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/nvexec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

set(nvexec_test_sources
continues_on.cpp
device_allocate.cpp
bulk.cpp
ensure_started.cpp
start_detached.cpp
Expand Down
43 changes: 43 additions & 0 deletions test/nvexec/device_allocate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <cstddef>
#include <cuda_runtime_api.h>

static int test_device_allocate_free_calls{};

static cudaError_t
test_device_allocate_cudaMemcpy(void*, void const *, std::size_t, cudaMemcpyKind) noexcept
{
return cudaErrorInvalidValue;
}

static cudaError_t test_device_allocate_cudaFree(void* ptr) noexcept
{
++test_device_allocate_free_calls;
return ::cudaFree(ptr);
}

#define cudaMemcpy test_device_allocate_cudaMemcpy
#define cudaFree test_device_allocate_cudaFree
#include "nvexec/detail/memory.cuh"
#undef cudaFree
#undef cudaMemcpy

#include <test_common/catch2.hpp>

namespace
{
TEST_CASE("device allocation frees storage when cudaMemcpy fails",
"[cuda][stream][memory][device_allocate]")
{
test_device_allocate_free_calls = 0;
cudaError_t status = cudaSuccess;

{
auto ptr = nvexec::_strm::device_allocate<int>(status, 42);

REQUIRE(status == cudaErrorInvalidValue);
REQUIRE(ptr == nullptr);
}

REQUIRE(test_device_allocate_free_calls == 1);
}
} // namespace