Does CudaFreeHost care about which device is active when cudaMallocHost is used to allocate memory?

I am using Cuda SDK 4.0 and am facing a problem that took me 2 days to destroy the following code.

#include <cuda.h> #include <cuda_runtime.h> void main (int argc, char ** argv) { int* test; cudaError_t err; err = cudaSetDevice( 1 ); err = cudaMallocHost(&test, 1024*sizeof(int)); err = cudaSetDevice( 0 ); err = cudaFreeHost(test); } 

This causes the following error when calling cudaFreeHost:

 First-chance exception at 0x000007fefd96aa7d in Test.exe: Microsoft C++ exception: cudaError_enum at memory location 0x0022f958.. 

Error value cudaErrorInvalidValue

The same error occurs with this change:

 err = cudaSetDevice( 0 ); err = cudaMallocHost(&test, 1024*sizeof(int)); err = cudaSetDevice( 1 ); err = cudaFreeHost(test); 

The following options do not throw an error:

 err = cudaSetDevice( 0 ); err = cudaMallocHost(&test, 1024*sizeof(int)); err = cudaSetDevice( 0 ); err = cudaFreeHost(test); 

and

 err = cudaSetDevice( 1 ); err = cudaMallocHost(&test, 1024*sizeof(int)); err = cudaSetDevice( 1 ); err = cudaFreeHost(test); 

It seemed to me that you only need to call cudaSetDevice if you want to allocate memory on a specific GPU. In the above example, I only allocate fixed memory on the CPU.

Is this a mistake or am I missing something in the manual?

+7
source share
1 answer

I found a problem. cudaHostAlloc and cudaMallocHost are NOT AVAILABLE ONLY .

For those facing this problem, the solution is to use

 cudaHostAlloc(&test, 1024*sizeof(int),cudaHostAllocPortable); 

instead

 cudaMallocHost(&test, 1024*sizeof(int)); 
+7
source

All Articles