cuda - Why does nvcc fail allow casting a pointer of type T * to void *? -
with next trivial deleter
struct cudadeleter{ void operator()(void * ptr) { cudafree( ptr ); } };
i next errors when using deleter in code compiled nvcc. same deleter works fine vs2012 compiler
warning : "std::unique_ptr<_ty, _dx>::unique_ptr( const std::unique_ptr<_ty, _dx>::_myt &) [with _ty=const int, _dx=cuda::cudadeleter]" error : function "cuda::cudadeleter::operator()" cannot called given argument list warning : "std::unique_ptr<_ty, _dx>::unique_ptr( const std::unique_ptr<_ty, _dx>::_myt &) [with _ty=float, _dx=cuda::cudadeleter]"
@talonmies: smart pointers constructed function only
template <typename t> std::unique_ptr<t, cudadeleter> make_unique(size_t size) { void * pmemory = nullptr; check( cudamalloc(&pmemory, size) ); homecoming std::unique_ptr<t, cudadeleter>( static_cast<t*>(pmemory) ); }
the next works me. seek standalone code below, if works need identify difference code, if not, there's different setup.
#include <iostream> #include <memory> struct cudadeleter { void operator()(void *p) { std::cout << "free..." << std::endl; cudaerror_t res = cudafree(p); if (res != cudasuccess) { std::cout << "error freeing: " << cudageterrorstring(res) << std::endl; } } }; template <typename t> std::unique_ptr<t, cudadeleter> make_unique(size_t size) { void *pmemory = nullptr; std::cout << "allocate..." << std::endl; cudaerror_t res = cudamalloc(&pmemory, size); if (res != cudasuccess) { std::cout << "error allocating pmemory: " << cudageterrorstring(res) << std::endl; throw; } homecoming std::unique_ptr<t, cudadeleter>(static_cast<t*>(pmemory)); } int main(void) { { std::cout << "create..." << std::endl; std::unique_ptr<float, cudadeleter> x = make_unique<float>(100*sizeof(float)); std::cout << "destroy..." << std::endl; } std::cout << "done." << std::endl; }
cuda nvcc
No comments:
Post a Comment