diff --git a/include/malloc_unmanaged.hpp b/include/malloc_unmanaged.hpp index 63ad26b..59b8c54 100644 --- a/include/malloc_unmanaged.hpp +++ b/include/malloc_unmanaged.hpp @@ -5,8 +5,46 @@ #ifndef RAIISAFECUDA_MALLOC_UNMANAGED_HPP #define RAIISAFECUDA_MALLOC_UNMANAGED_HPP -namespace safe_cuda { +#include +#include +#include +#include +namespace safe_cuda::unmanaged { + using deviceMallocDestroyType = decltype(&cudaFree); + using hostMallocDestroyType = decltype(&cudaFreeHost); + + template + concept CudaArrayDestroyer = std::is_same_v || std::is_same_v; + + template requires std::integral || std::floating_point + using safePtrType = std::unique_ptr; + + /** + * \brief It allocates unmanaged memory with cuda runtime API. + * + * 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. + * \param byteDataSize + * \return + */ + template + std::variant, cudaError_t> cuda_malloc(const std::size_t byteDataSize) noexcept { + T *ptr_tmp = nullptr; + if constexpr (std::is_same_v) { + const cudaError_t error = cudaMalloc(&ptr_tmp, byteDataSize); + if (error != cudaSuccess) { + return error; + } + } else if constexpr (std::is_same_v) { + const cudaError_t error = cudaMallocHost(&ptr_tmp, byteDataSize); + if (error != cudaSuccess) { + return error; + } + } + return safePtrType{ ptr_tmp, cudaFree }; + } } #endif //RAIISAFECUDA_MALLOC_UNMANAGED_HPP