I'm trying to write to CUDA host memory (which I created in my main thread) with a worker thread. The code for this is very simple. I create the memory with
unsigned char* _new;
cudaHostAlloc(&_new, _size, cudaHostAllocPortable);
and pass the pointer _new to the other thread. This thread, however, creates a memory violation when trying to write to it with
memcpy(_new, _source, _size);
or
cudaMemcpy(_new, _source, _size, cudaMemcpyHostToHost);
When I use _new = new unsigned char[_size];
or copy the data in the same thread, it works.
Does anybody know why this happens and how I can fix this?
After a long and tiring evening, I finally found out what's wrong with it!
The thread writing to the main thread's memory was started after the cuda allocation call. Therefore, once the thread was started, it wasn't aware that there is some writable memory in this area and this resulted in a memory violation.
To fix this, I moved the allocation call into the run() method of the thread. Like this, the memory gets allocated when both threads are alive. Fixed!