# CUDA Context and host multi-threading

**URL:** <https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508>\
**Category:** CUDA Programming and Performance\
**Created:** [July 10, 2008, 11:14am UTC](https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508 "2008-07-10T11:14:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmetz\_nv](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mmetz\_nv](https://forums.developer.nvidia.com/u/mmetz_nv)\
**Post date:** [July 10, 2008, 11:14am UTC](https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508/1 "2008-07-10T11:14:10Z")

</div>

Hi,  
I thought it must be possible to reuse a floating CUDA context in a host-multithreading application several times ?  
I had a look at the example “threadMigration” of CUDA2.0beta2. This made me think that something like the following should be possible (very simplistic):

1. host thread: create a context ‘ctx’ cuCtxCreate, cuCtxPopCurrent(NULL);
2. start thread 1; cuCtxPushCurrent( ctx); do something; cuCtxPopCurrent(NULL);
3. host thread: wait for thread 1 to finish
4. start thread 2; cuCtxPushCurrent( ctx); do something; cuCtxPopCurrent(NULL);
5. host thread: wait for thread 2 to finish

But when I try this, I get a CUDA\_ERROR\_INVALID\_VALUE return value on the second call to cuCtxPushCurrent( ctx).  
I did not call cuCtxDestroy or cuCtxDetach. (I don’t know whther this might be important, but in “so something” I use the cuFFT + my own code) … Any ideas ???

---

<div class="post-metadata">

**Author:** ![Reimar](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Reimar](https://forums.developer.nvidia.com/u/Reimar)\
**Post date:** [July 10, 2008, 11:33am UTC](https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508/2 "2008-07-10T11:33:52Z")

</div>

> [@](#):
>
> I thought it must be possible to reuse a floating CUDA context in a host-multithreading application several times ?
> 
> I had a look at the example “threadMigration” of CUDA2.0beta2. This made me think that something like the following should be possible (very simplistic):
> 
> 1. host thread: create a context ‘ctx’ cuCtxCreate, cuCtxPopCurrent(NULL);
> 
> 2. start thread 1; cuCtxPushCurrent( ctx); do something; cuCtxPopCurrent(NULL);
> 
> 3. host thread: wait for thread 1 to finish
> 
> 4. start thread 2; cuCtxPushCurrent( ctx); do something; cuCtxPopCurrent(NULL);
> 
> 5. host thread: wait for thread 2 to finish
> 
> But when I try this, I get a CUDA\_ERROR\_INVALID\_VALUE return value on the second call to cuCtxPushCurrent( ctx).
> 
> I did not call cuCtxDestroy or cuCtxDetach. (I don’t know whther this might be important, but in “so something” I use the cuFFT + my own code) … Any ideas ???
> 
> [snapback]408469[/snapback]

Reading the documentation I think that you must pass the context pointer returned by cuCtxPopCurrent to cuCtxPushCurrent, or did you verify that it is the same as returned by cuCtxCreate?

---

<div class="post-metadata">

**Author:** ![mmetz\_nv](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mmetz\_nv](https://forums.developer.nvidia.com/u/mmetz_nv)\
**Post date:** [July 10, 2008, 12:05pm UTC](https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508/3 "2008-07-10T12:05:22Z")

</div>

> [@](#):
>
> Reading the documentation I think that you must pass the context pointer returned by cuCtxPopCurrent to cuCtxPushCurrent, or did you verify that it is the same as returned by cuCtxCreate?
> 
> [snapback]408479[/snapback]

Hm,

it’s always the same context. Nevertheless, I checked what happens in the threadMigration example by replacing

```auto
cuCtxPopCurrent( NULL );

```

by

```auto
CUcontext pctx = 0;

cuCtxPopCurrent( &pctx );

printf( " cuCtxPopCurrent, context is %p\n", pctx);

```

at the end of the ThreadProc…

Output is (nil) ?

---

<div class="post-metadata">

**Author:** ![podonok](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@podonok](https://forums.developer.nvidia.com/u/podonok)\
**Post date:** [February 19, 2009, 12:38pm UTC](https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508/4 "2009-02-19T12:38:12Z")

</div>

The same situation… I checked the function ptr. They are correct. Do you have any idea? The algorithm was taken from documentation

---

<div class="post-metadata">

**Author:** ![muzungu](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@muzungu](https://forums.developer.nvidia.com/u/muzungu)\
**Post date:** [June 9, 2010, 10:11am UTC](https://forums.developer.nvidia.com/t/cuda-context-and-host-multi-threading/4508/5 "2010-06-09T10:11:40Z")

</div>

I have the same problem, please help
