The CUDA API has types that require create() and destroy() calls analogous to memory allocation new and delete. In the spirit of RAII, and rather than having to call cudaEventCreate( &event) and cudaEventDestory( event ), I wrote the following wrapper for cudaEvent_t.
My Question: Is this acceptable code without any obvious errors?
It builds for me and I've yet to discover a problem. But I particularly do not like the reinterpret_cast<> trickery used to get the cudaEvent_t variable through the custom Allocater and Deleter for the shared_ptr.
Some related posts:
CUDA: Wrapping device memory allocation in C++
Is there a better/cleaner/more elegant way to malloc and free in cuda?
class CudaEvent {
private:
struct Deleter {
void operator()(cudaEvent_t * ptr) const {
checkCudaErrors( cudaEventDestroy( reinterpret_cast<cudaEvent_t>(ptr) ));
}
};
shared_ptr<cudaEvent_t> Allocate( ){
cudaEvent_t event;
checkCudaErrors( cudaEventCreate( &event ) );
shared_ptr<cudaEvent_t> p( reinterpret_cast<cudaEvent_t*>(event), Deleter() );
return p;
}
shared_ptr<cudaEvent_t> ps;
public:
cudaEvent_t event;
CudaEvent( )
: ps( Allocate( ) ),
event( *(ps.get()) )
{ }
};