Skip to content

Commit

Permalink
Merge pull request #333 from ahehn-nv/ahehn/device_memory_allocation_…
Browse files Browse the repository at this point in the history
…exception

[common] Added a device_memory_allocation_exception
  • Loading branch information
ahehn-nv authored Mar 24, 2020
2 parents 510e46d + 5291351 commit 13edb63
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
26 changes: 22 additions & 4 deletions common/utils/include/claragenomics/utils/allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <claragenomics/utils/device_preallocated_allocator.cuh>

#include <claragenomics/utils/cudautils.hpp>
#include <claragenomics/utils/exceptions.hpp>

namespace claragenomics
{
Expand Down Expand Up @@ -105,8 +106,20 @@ class CudaMallocAllocator
pointer allocate(std::size_t n, cudaStream_t stream = 0)
{
static_cast<void>(stream);
void* ptr = 0;
CGA_CU_CHECK_ERR(cudaMalloc(&ptr, n * sizeof(T)));
void* ptr = nullptr;
cudaError_t err = cudaMalloc(&ptr, n * sizeof(T));
if (err == cudaErrorMemoryAllocation)
{
// Clear the error from the runtime...
err = cudaGetLastError();
// Did a different (async) error happen in the meantime?
if (err != cudaErrorMemoryAllocation)
{
CGA_CU_CHECK_ERR(err);
}
throw device_memory_allocation_exception();
}
CGA_CU_CHECK_ERR(err);
return static_cast<pointer>(ptr);
}

Expand Down Expand Up @@ -213,8 +226,13 @@ class CachingDeviceAllocator
/// @return pointer to allocated array
pointer allocate(std::size_t n, cudaStream_t stream = 0)
{
void* ptr = 0;
CGA_CU_CHECK_ERR(memory_resource_->DeviceAllocate(&ptr, n * sizeof(T), stream));
void* ptr = nullptr;
cudaError_t err = memory_resource_->DeviceAllocate(&ptr, n * sizeof(T), stream);
if (err == cudaErrorMemoryAllocation)
{
throw device_memory_allocation_exception();
}
CGA_CU_CHECK_ERR(err);
return static_cast<pointer>(ptr);
}

Expand Down
43 changes: 43 additions & 0 deletions common/utils/include/claragenomics/utils/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/

#pragma once

#include <exception>

namespace claragenomics
{

/// @brief Exception class for out-of-(device-)memory errors.
///
/// Exceptions of this class are thrown if a memory allocation fails on the device.
class device_memory_allocation_exception : public std::exception
{
public:
device_memory_allocation_exception() = default;
/// Copy constructor
device_memory_allocation_exception(device_memory_allocation_exception const&) = default;
/// Move constructor
device_memory_allocation_exception(device_memory_allocation_exception&&) = default;
/// Assignment
device_memory_allocation_exception& operator=(device_memory_allocation_exception const&) = default;
/// Move-Assignment
device_memory_allocation_exception& operator=(device_memory_allocation_exception&&) = default;
/// Destructor
virtual ~device_memory_allocation_exception() = default;

/// Returns the error message of the exception
virtual const char* what() const noexcept
{
return "Could not allocate device memory!";
}
};

} // namespace claragenomics

0 comments on commit 13edb63

Please sign in to comment.