# Reporting a problem with CUDA memory access in multiple OS threads

**URL:** <https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645>\
**Category:** CUDA Programming and Performance\
**Created:** [April 28, 2007, 5:21am UTC](https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645 "2007-04-28T05:21:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![svd2cn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@svd2cn](https://forums.developer.nvidia.com/u/svd2cn)\
**Post date:** [April 28, 2007, 5:21am UTC](https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645/1 "2007-04-28T05:21:52Z")

</div>

Platform specification: WinXP, VS .net 2003, CUDA SDK 0.81, Core Duo 2 E6600 (2.4G/4M/1066) + 2G DDR 667 (1G\*2) + NVIDIA G80.

I have tried to allocate memory (cudaMalloc()) in one OS thread, and access to it in another thread, but always fails.

The sample code is as the following. Who can try such code and tell me your results? Is there some suggestions on how to solve this problem? thank you in advance.

#define \_MT //for thread related APIs.

#include \<windows.h\>  
#include \<process.h\>

float\* h\_idata;  
float\* d\_idata;

unsigned int \_\_stdcall test\_func(void\* pArguments)  
{  
//both memory copy calls fail…  
CUDA\_SAFE\_CALL( cudaMemcpy( d\_idata, h\_idata, 1024,cudaMemcpyHostToDevice) );  
CUDA\_SAFE\_CALL( cudaMemcpy( h\_idata, d\_idata, 1024,cudaMemcpyDeviceToHost) );

\_endthreadex(0);  
return 0;  
}

int main( int argc, char\*\* argv)  
{  
h\_idata = (float\*) malloc( 1024);  
CUDA\_SAFE\_CALL( cudaMalloc( (void\*\*) &d\_idata, 1024));

```
unsigned threadID;
HANDLE hThread = (HANDLE)_beginthreadex( NULL, 0, &test_func, NULL, 0, &threadID);

WaitForSingleObject( hThread, INFINITE );
CloseHandle( hThread );

```

}

The run-time message is as follows when I debug to the cudaMemcpy() line:  
First-chance exception at 0x7c812a5b in template.exe: Microsoft C++ exception: bool @ 0x03a2ff0f.  
First-chance exception at 0x7c812a5b in template.exe: Microsoft C++ exception: [rethrow] @ 0x000000000.  
Unhandled exception at 0x7c812a5b in template.exe: Microsoft C++ exception: bool @ 0x03a2ff0f.

Note that the execution works well if cudaMalloc call is directly before cudaMemcpy() call (i.e. in the same thread).

---

<div class="post-metadata">

**Author:** ![mimichw](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mimichw](https://forums.developer.nvidia.com/u/mimichw)\
**Post date:** [April 28, 2007, 5:54am UTC](https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645/2 "2007-04-28T05:54:09Z")

</div>

What’s failed message did you get?

---

<div class="post-metadata">

**Author:** ![svd2cn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@svd2cn](https://forums.developer.nvidia.com/u/svd2cn)\
**Post date:** [April 29, 2007, 2:24am UTC](https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645/3 "2007-04-29T02:24:18Z")

</div>

> [@](#):
>
> What’s failed message did you get?
> 
> [snapback]190494[/snapback]

the description is much more clarified in the original text. Thanks.

---

<div class="post-metadata">

**Author:** ![osiris1](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@osiris1](https://forums.developer.nvidia.com/u/osiris1)\
**Post date:** [April 29, 2007, 5:26am UTC](https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645/4 "2007-04-29T05:26:47Z")

</div>

Yes you can’t do that! See section 4.5.3.3 - there is a 1:1 correspondence between contexts and host threads. Perhaps a few words could be added to the manual to emphasise this goes both ways.  
Cheers, Eric

---

<div class="post-metadata">

**Author:** ![svd2cn](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@svd2cn](https://forums.developer.nvidia.com/u/svd2cn)\
**Post date:** [April 30, 2007, 9:29am UTC](https://forums.developer.nvidia.com/t/reporting-a-problem-with-cuda-memory-access-in-multiple-os-threads/645/5 "2007-04-30T09:29:04Z")

</div>

> [@](#):
>
> Yes you can’t do that!&nbsp; See section 4.5.3.3 - there is a 1:1 correspondence between contexts and host threads. Perhaps a few words could be added to the manual to emphasise this goes both ways.
> 
> Cheers, Eric
> 
> [snapback]190823[/snapback]

I see. thank you very much. In fact I have not read that part since I only used runtime API…
