is it valid to free device allocated memory from a host function? I'm writing some C++ class that should be used on host and device. My constructor and destructor are something like this:
class myClass {
public:
__host__ __device__ myClass() {
#if defined(__CUDA_ARCH__)
data = (char*)malloc(DATA_SIZE);
#else
cudaMalloc(&data,DATA_SIZE);
#endif
}
__host__ __device__ ~myClass() {
#if defined(__CUDA_ARCH__)
free(data);
#else
cudaFree(data);
#endif
}
private:
char* data;
}
The above code compiles and i didn't get an error if i construct a class on the device and free it on the host. But this case is not documented in the CUDA developer papers.
__CUDA_ARCH__
is defined, wouldn't you then want to use thecuda
functions? – Jonathan Grynspan__CUDA_ARCH__
is defined, the code is compiled for the device. On the device, i have to usemalloc
andfree
. Only on the host, there is need to usecudaMalloc
andcudaFree
– Thomas Berger