# cudaSetDevice bug?

**URL:** <https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762>\
**Category:** CUDA Programming and Performance\
**Created:** [November 15, 2010, 9:12pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762 "2010-11-15T21:12:35Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 15, 2010, 9:12pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/1 "2010-11-15T21:12:35Z")

</div>

Win 64 Intel Core I7  
Cuda 3.2  
GTX480 and GT9800

I have a 2 gpu system: a GTX480 for computation and a GT9800 for video.  
I have code that runs perfectly on the default device (0) which in my system is the GTX480.

I’ve discovered by chance that if I explicitly set the device using cudaSetDevice(0), I get what appears to be a memory leak. I can run the code once, but if I try to run it again I get a cudaMalloc error. The problem comes and goes merely by commenting out/or not the cudaSetDevice(0) statement.

And yes, I’ve verified using cudaGetDevice(…) that I’m always using device 0.

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 15, 2010, 9:12pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/2 "2010-11-15T21:12:35Z")

</div>

Win 64 Intel Core I7  
Cuda 3.2  
GTX480 and GT9800

I have a 2 gpu system: a GTX480 for computation and a GT9800 for video.  
I have code that runs perfectly on the default device (0) which in my system is the GTX480.

I’ve discovered by chance that if I explicitly set the device using cudaSetDevice(0), I get what appears to be a memory leak. I can run the code once, but if I try to run it again I get a cudaMalloc error. The problem comes and goes merely by commenting out/or not the cudaSetDevice(0) statement.

And yes, I’ve verified using cudaGetDevice(…) that I’m always using device 0.

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 15, 2010, 9:28pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/3 "2010-11-15T21:28:15Z")

</div>

Likely unrelated but there is another quirk:

The nvidia GPU Computing SDK code sample called Device Query correctly shows my GTX 480 as being device 0, and the 9800 GT as being device 1.

However, the nvidia System Performance Monitor Tool calls my GTX480 - GPU2, and the 9800 GT - GPU1.

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 15, 2010, 9:28pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/4 "2010-11-15T21:28:15Z")

</div>

Likely unrelated but there is another quirk:

The nvidia GPU Computing SDK code sample called Device Query correctly shows my GTX 480 as being device 0, and the 9800 GT as being device 1.

However, the nvidia System Performance Monitor Tool calls my GTX480 - GPU2, and the 9800 GT - GPU1.

---

<div class="post-metadata">

**Author:** ![srhines](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@srhines](https://forums.developer.nvidia.com/u/srhines)\
**Post date:** [November 16, 2010, 3:10am UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/5 "2010-11-16T03:10:12Z")

</div>

> [@](#):
>
> Win 64 Intel Core I7
> 
> Cuda 3.2
> 
> GTX480 and GT9800
> 
> I have a 2 gpu system: a GTX480 for computation and a GT9800 for video.
> 
> I have code that runs perfectly on the default device (0) which in my system is the GTX480.
> 
> I’ve discovered by chance that if I explicitly set the device using cudaSetDevice(0), I get what appears to be a memory leak. I can run the code once, but if I try to run it again I get a cudaMalloc error. The problem comes and goes merely by commenting out/or not the cudaSetDevice(0) statement.
> 
> And yes, I’ve verified using cudaGetDevice(…) that I’m always using device 0.

Did you call cudaThreadExit() before reinvoking all of your code? cudaSetDevice() can only be used if you haven’t already kicked off actual work on the GPU. Once you do, calling it is an error (which you can check from the error code returned from cudaMalloc).

As far as device ordering, there is no guarantee that Cuda’s ordering is the same as the PCI or system ordering.

---

<div class="post-metadata">

**Author:** ![srhines](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@srhines](https://forums.developer.nvidia.com/u/srhines)\
**Post date:** [November 16, 2010, 3:10am UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/6 "2010-11-16T03:10:12Z")

</div>

> [@](#):
>
> Win 64 Intel Core I7
> 
> Cuda 3.2
> 
> GTX480 and GT9800
> 
> I have a 2 gpu system: a GTX480 for computation and a GT9800 for video.
> 
> I have code that runs perfectly on the default device (0) which in my system is the GTX480.
> 
> I’ve discovered by chance that if I explicitly set the device using cudaSetDevice(0), I get what appears to be a memory leak. I can run the code once, but if I try to run it again I get a cudaMalloc error. The problem comes and goes merely by commenting out/or not the cudaSetDevice(0) statement.
> 
> And yes, I’ve verified using cudaGetDevice(…) that I’m always using device 0.

Did you call cudaThreadExit() before reinvoking all of your code? cudaSetDevice() can only be used if you haven’t already kicked off actual work on the GPU. Once you do, calling it is an error (which you can check from the error code returned from cudaMalloc).

As far as device ordering, there is no guarantee that Cuda’s ordering is the same as the PCI or system ordering.

---

<div class="post-metadata">

**Author:** ![MichaA\_Skrzypkowski](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MichaA\_Skrzypkowski](https://forums.developer.nvidia.com/u/MichaA_Skrzypkowski)\
**Post date:** [November 16, 2010, 1:51pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/7 "2010-11-16T13:51:13Z")

</div>

> [@](#):
>
> I’ve discovered by chance that if I explicitly set the device using cudaSetDevice(0), I get what appears to be a memory leak. I can run the code once, but if I try to run it again I get a cudaMalloc error. The problem comes and goes merely by commenting out/or not the cudaSetDevice(0) statement.

Just as _srhines_ said. Check the cudaMalloc error.

You have the error handling reference here

So you could try something like this:

```auto
printf("Error: %s\n", cudaGetErrorString(cudaGetLastError()));

```

Here you have error types with definitions.

---

<div class="post-metadata">

**Author:** ![MichaA\_Skrzypkowski](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@MichaA\_Skrzypkowski](https://forums.developer.nvidia.com/u/MichaA_Skrzypkowski)\
**Post date:** [November 16, 2010, 1:51pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/8 "2010-11-16T13:51:13Z")

</div>

> [@](#):
>
> I’ve discovered by chance that if I explicitly set the device using cudaSetDevice(0), I get what appears to be a memory leak. I can run the code once, but if I try to run it again I get a cudaMalloc error. The problem comes and goes merely by commenting out/or not the cudaSetDevice(0) statement.

Just as _srhines_ said. Check the cudaMalloc error.

You have the error handling reference here

So you could try something like this:

```auto
printf("Error: %s\n", cudaGetErrorString(cudaGetLastError()));

```

Here you have error types with definitions.

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 16, 2010, 3:04pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/9 "2010-11-16T15:04:13Z")

</div>

Thanks. Calling cudaThreadExit() did the job. I was lazy and didn’t put an error catch on the cudaSetDevice(0).  
I’m curious though. When I said the problem appeared when I ran the code a second time, I meant that literally. I didn’t mean within a loop. I was under the impression that cuda threads were cleaned up when they were all finished and the code terminated.

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 16, 2010, 3:04pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/10 "2010-11-16T15:04:13Z")

</div>

Thanks. Calling cudaThreadExit() did the job. I was lazy and didn’t put an error catch on the cudaSetDevice(0).  
I’m curious though. When I said the problem appeared when I ran the code a second time, I meant that literally. I didn’t mean within a loop. I was under the impression that cuda threads were cleaned up when they were all finished and the code terminated.

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 16, 2010, 3:06pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/11 "2010-11-16T15:06:34Z")

</div>

In fact, the documentation says

“cudaThreadExit() is implicitly called on host thread exit”

---

<div class="post-metadata">

**Author:** ![Dittoaway](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Dittoaway](https://forums.developer.nvidia.com/u/Dittoaway)\
**Post date:** [November 16, 2010, 3:06pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/12 "2010-11-16T15:06:34Z")

</div>

In fact, the documentation says

“cudaThreadExit() is implicitly called on host thread exit”

---

<div class="post-metadata">

**Author:** ![eelsen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@eelsen](https://forums.developer.nvidia.com/u/eelsen)\
**Post date:** [November 16, 2010, 6:39pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/13 "2010-11-16T18:39:06Z")

</div>

> [@](#):
>
> In fact, the documentation says
> 
> “cudaThreadExit() is implicitly called on host thread exit”

Yeah, but in the release notes for every single CUDA version, the following appears:

o It is a known issue that cudaThreadExit() may not be called implicitly on

host thread exit. Due to this, developers are recommended to explicitly

call cudaThreadExit() while the issue is being resolved

I don’t know when they are planning on actually resolving this…

---

<div class="post-metadata">

**Author:** ![eelsen](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@eelsen](https://forums.developer.nvidia.com/u/eelsen)\
**Post date:** [November 16, 2010, 6:39pm UTC](https://forums.developer.nvidia.com/t/cudasetdevice-bug/19762/14 "2010-11-16T18:39:06Z")

</div>

> [@](#):
>
> In fact, the documentation says
> 
> “cudaThreadExit() is implicitly called on host thread exit”

Yeah, but in the release notes for every single CUDA version, the following appears:

o It is a known issue that cudaThreadExit() may not be called implicitly on

host thread exit. Due to this, developers are recommended to explicitly

call cudaThreadExit() while the issue is being resolved

I don’t know when they are planning on actually resolving this…
