# Managing multiple GPUs from a single host thread

**URL:** <https://forums.developer.nvidia.com/t/managing-multiple-gpus-from-a-single-host-thread/19151>\
**Category:** CUDA Programming and Performance\
**Created:** [October 10, 2010, 8:05am UTC](https://forums.developer.nvidia.com/t/managing-multiple-gpus-from-a-single-host-thread/19151 "2010-10-10T08:05:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_habanero](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@m\_habanero](https://forums.developer.nvidia.com/u/m_habanero)\
**Post date:** [October 10, 2010, 8:05am UTC](https://forums.developer.nvidia.com/t/managing-multiple-gpus-from-a-single-host-thread/19151/1 "2010-10-10T08:05:20Z")

</div>

From what I understand, there normally must be a one-to-one mapping of host threads to devices. I’m wondering if it is possible to manage multiple GPUs from a single host thread. According to this post: [url=“[The Official NVIDIA Forums | NVIDIA](http://forums.nvidia.com/index.php?showtopic=91739&pid=984126&mode=threaded&start=#entry984126)”][http://forums.nvidia.com/index.php?showtop...rt=#entry984126[/url]](http://forums.nvidia.com/index.php?showtop...rt=#entry984126%5B/url%5D) it seems like this should be possible. Has anyone tried this? I’m thinking it would look something like:

CUcontext ctxs[NUM\_DEVICES];  
CUdevice \*dev;

for(i=0; i \< NUM\_DEVICES; i++) {  
cudaSetDevice(i);  
dev = (CUdevice \*)malloc(sizeof(CUdevice));  
cuCtxGetDevice(dev);  
cuCtxCreate(&ctxs[i],0, dev);  
cuCtxPopCurrent(&ctxs[i]);  
}

And then you could use cuCtxPushCurrent and cuCtxPopCurrent to switch between contexts. Has anyone tried this or see any basic problems with it (I’m not familiar with how CUDA contexts work)? Would switching contexts possible introduce a lot of overhead?

Any input is appreciated, Thanks very much in advance.

---

<div class="post-metadata">

**Author:** ![m\_habanero](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@m\_habanero](https://forums.developer.nvidia.com/u/m_habanero)\
**Post date:** [October 10, 2010, 8:05am UTC](https://forums.developer.nvidia.com/t/managing-multiple-gpus-from-a-single-host-thread/19151/2 "2010-10-10T08:05:20Z")

</div>

From what I understand, there normally must be a one-to-one mapping of host threads to devices. I’m wondering if it is possible to manage multiple GPUs from a single host thread. According to this post: [url=“[The Official NVIDIA Forums | NVIDIA](http://forums.nvidia.com/index.php?showtopic=91739&pid=984126&mode=threaded&start=#entry984126)”][The Official NVIDIA Forums | NVIDIA](http://forums.nvidia.com/index.php?showtop...rt=#entry984126%5B/url%5D) it seems like this should be possible. Has anyone tried this? I’m thinking it would look something like:

CUcontext ctxs[NUM\_DEVICES];  
CUdevice \*dev;

for(i=0; i \< NUM\_DEVICES; i++) {  
cudaSetDevice(i);  
dev = (CUdevice \*)malloc(sizeof(CUdevice));  
cuCtxGetDevice(dev);  
cuCtxCreate(&ctxs[i],0, dev);  
cuCtxPopCurrent(&ctxs[i]);  
}

And then you could use cuCtxPushCurrent and cuCtxPopCurrent to switch between contexts. Has anyone tried this or see any basic problems with it (I’m not familiar with how CUDA contexts work)? Would switching contexts possible introduce a lot of overhead?

Any input is appreciated, Thanks very much in advance.
