1 #ifdef COMPILATION_INSTRUCTIONS 2 (echo '#include "'$0'"'>$0.cpp)&&c++ -std=c++11 -Wall -Wextra -Wpedantic -Wfatal-errors -D_TEST_MULTI_MEMORY_ADAPTOR_CUDA_MANAGED_MALLOC $0.cpp -lcudart -o $0x &&$0x&& rm $0x $0.cpp; exit 3 #endif 4 5 #ifndef MULTI_MEMORY_ADAPTOR_CUDA_MANAGED_CLIB_HPP 6 #define MULTI_MEMORY_ADAPTOR_CUDA_MANAGED_CLIB_HPP 7 8 #include<cuda_runtime.h> // cudaMallocManaged 9 10 #include "../../../adaptors/cuda/clib.hpp" // Cuda::free 11 #include "../../../adaptors/cuda/error.hpp" 12 13 namespace Cuda{ 14 namespace Managed{ 15 error Malloc(void** p, size_t bytes){return static_cast<error>(cudaMallocManaged(p, bytes/*, cudaMemAttachGlobal*/));} 16 void* malloc(size_t bytes){ 17 void* ret; 18 switch(auto e = Malloc(&ret, bytes)){ 19 case success : return ret; 20 case memory_allocation : return nullptr; 21 default : 22 throw std::system_error{e, "cannot allocate "+std::to_string(bytes)+" bytes in '"+__PRETTY_FUNCTION__+"'"}; 23 } 24 } 25 void free(void* p){return Cuda::free(p);} 26 } 27 } 28 29 30 #ifdef _TEST_MULTI_MEMORY_ADAPTOR_CUDA_MANAGED_MALLOC 31 32 #include "../../cuda/managed/ptr.hpp" 33 34 #include<iostream> 35 36 namespace multi = boost::multi; 37 namespace cuda = multi::memory::cuda; 38 39 using std::cout; 40 41 int main(){ 42 void* p = Cuda::Managed::malloc(100); 43 Cuda::Managed::free(p); 44 } 45 #endif 46 #endif 47 48