Compare commits
4 commits
24a0a78ec6
...
a53e5f87be
| Author | SHA1 | Date | |
|---|---|---|---|
|
a53e5f87be |
|||
|
81d04d6332 |
|||
|
1ebcaf6f97 |
|||
|
22f09c47fd |
4 changed files with 74 additions and 26 deletions
|
|
@ -6,18 +6,42 @@
|
|||
#define RAIISAFECUDA_MALLOC_UNMANAGED_HPP
|
||||
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
#include <cuda_runtime_api.h>
|
||||
|
||||
namespace safe_cuda::unmanaged {
|
||||
using deviceMallocDestroyType = decltype(&cudaFree);
|
||||
using hostMallocDestroyType = decltype(&cudaFreeHost);
|
||||
|
||||
template<typename D>
|
||||
concept CudaArrayDestroyer = std::is_same_v<D, deviceMallocDestroyType> || std::is_same_v<D, hostMallocDestroyType>;
|
||||
namespace safe_cuda {
|
||||
enum class allocType : std::uint8_t {
|
||||
Unmanaged = 0,
|
||||
Managed = 1,
|
||||
Host = 2,
|
||||
};
|
||||
|
||||
template<typename T, CudaArrayDestroyer D> requires std::integral<T> || std::floating_point<T>
|
||||
template<typename T, allocType alloc_type = allocType::Managed>
|
||||
struct destroyType {
|
||||
void operator()(T *ptr) const noexcept {
|
||||
(void) cudaFree(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct destroyType<T, allocType::Unmanaged> {
|
||||
void operator()(T *ptr) const noexcept {
|
||||
(void) cudaFree(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct destroyType<T, allocType::Host> {
|
||||
void operator()(T *ptr) const noexcept {
|
||||
(void) cudaFreeHost(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, allocType alloc_type, typename D = destroyType<T, alloc_type> >
|
||||
requires std::integral<T> || std::floating_point<T>
|
||||
using safePtrType = std::unique_ptr<T, D>;
|
||||
|
||||
/**
|
||||
|
|
@ -25,25 +49,26 @@ namespace safe_cuda::unmanaged {
|
|||
*
|
||||
* It can allocate unmanaged memory on device and on Host for pinned memory.
|
||||
* \tparam T bare and built-in type.
|
||||
* \tparam D destroyer type. It determines if it allocates on device or pinned host.
|
||||
* \tparam alloc_type Type of allocation: Managed (default), Unmanage, Host.
|
||||
* \param byteDataSize
|
||||
* \return
|
||||
*/
|
||||
template<typename T, CudaArrayDestroyer D>
|
||||
std::variant<safePtrType<T, D>, cudaError_t> cuda_malloc(const std::size_t byteDataSize) noexcept {
|
||||
template<typename T, allocType alloc_type>
|
||||
std::pair<safePtrType<T, alloc_type>, cudaError_t>
|
||||
cuda_malloc(const std::size_t byteDataSize) noexcept {
|
||||
T *ptr_tmp = nullptr;
|
||||
if constexpr (std::is_same_v<D, deviceMallocDestroyType>) {
|
||||
const cudaError_t error = cudaMalloc(&ptr_tmp, byteDataSize);
|
||||
if (error != cudaSuccess) {
|
||||
return error;
|
||||
}
|
||||
} else if constexpr (std::is_same_v<D, hostMallocDestroyType>) {
|
||||
const cudaError_t error = cudaMallocHost(&ptr_tmp, byteDataSize);
|
||||
if (error != cudaSuccess) {
|
||||
return error;
|
||||
}
|
||||
cudaError_t error = cudaSuccess;
|
||||
switch (alloc_type) {
|
||||
case allocType::Unmanaged:
|
||||
error = cudaMalloc(reinterpret_cast<void **>(&ptr_tmp), byteDataSize);
|
||||
return { safePtrType<T, alloc_type>{ ptr_tmp, destroyType<T, alloc_type>{} }, error };
|
||||
case allocType::Host:
|
||||
error = cudaMallocHost(reinterpret_cast<void **>(&ptr_tmp), byteDataSize);
|
||||
return { safePtrType<T, alloc_type>{ ptr_tmp, destroyType<T, alloc_type>{} }, error };
|
||||
case allocType::Managed:
|
||||
error = cudaMallocManaged(reinterpret_cast<void **>(&ptr_tmp), byteDataSize);
|
||||
return { safePtrType<T, alloc_type>{ ptr_tmp, destroyType<T, alloc_type>{} }, error };
|
||||
}
|
||||
return safePtrType<T, D>{ ptr_tmp, cudaFree };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@
|
|||
namespace safe_cuda {
|
||||
std::variant<std::unique_ptr<CUstream_st, streamDestroyType>, cudaError_t> create_stream() noexcept {
|
||||
cudaStream_t stream = nullptr;
|
||||
const cudaError_t error = cudaStreamCreate(&stream);
|
||||
if (error != cudaSuccess) {
|
||||
if (const cudaError_t error = cudaStreamCreate(&stream); error != cudaSuccess) {
|
||||
return error;
|
||||
}
|
||||
return std::unique_ptr<CUstream_st, streamDestroyType>{ stream, cudaStreamDestroy };
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ if (NOT Catch2_FOUND)
|
|||
fetchcontent_declare(
|
||||
Catch2
|
||||
GIT_REPOSITORY https://github.com/catchorg/Catch2
|
||||
GIT_TAG v3.5.3
|
||||
GIT_TAG v3.11.0
|
||||
EXCLUDE_FROM_ALL
|
||||
OVERRIDE_FIND_PACKAGE
|
||||
)
|
||||
|
|
@ -20,7 +20,15 @@ enable_testing()
|
|||
include(Catch)
|
||||
include(CatchAddTests)
|
||||
|
||||
add_executable(tests tests_stream.cpp)
|
||||
target_link_libraries(tests Catch2::Catch2WithMain)
|
||||
add_executable(tests
|
||||
tests_stream.cpp
|
||||
tests_safe_allocation.cpp
|
||||
)
|
||||
target_link_libraries(tests Catch2::Catch2WithMain raiiSafeCuda)
|
||||
set_target_properties(tests PROPERTIES
|
||||
CXX_STANDARD 20
|
||||
CXX_EXTENSIONS OFF
|
||||
INTERPROCEDURAL_OPTIMIZATION ON
|
||||
)
|
||||
|
||||
catch_discover_tests(tests WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
|
|
|||
16
tests/tests_safe_allocation.cpp
Normal file
16
tests/tests_safe_allocation.cpp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
//
|
||||
// Created by postaron on 13/12/2025.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include "../include/malloc_unmanaged.hpp"
|
||||
|
||||
TEST_CASE("Managed allocation", "[safe_alloc][0]") {
|
||||
std::cout << "Safely allocates memory in CUDA" << std::endl;
|
||||
const auto [safe_ptr, error] = safe_cuda::cuda_malloc<int, safe_cuda::allocType::Managed>(sizeof(int));
|
||||
REQUIRE(safe_ptr != nullptr);
|
||||
REQUIRE(error == cudaSuccess);
|
||||
std::cout << "Safely deallocates memory in CUDA" << std::endl;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue