# nVidia Optimus and CUDA

## Spiros

I'm planing to buy a Lenovo ThinkPad T420s with Optimus (i.e. in this case integrated Intel graphics and nVidia NVS 4200M). I know that Optimus under Linux is still not really working, or hacky. But I'm not really interested in high performance graphics. Instead, I would really like to be able to develop and run CUDA or OpenCL applications on this discrete graphics device and let everything else run on the Intel chip.

Is this possible? Are there any caveats?

I'm also interested in having a long-life battery. Can I hope that the disrete graphics is "turned off" while normally working on my laptop? I'm sure that the Intel graphics is more than sufficient for me.

----------

## Gusar

 *Spiros wrote:*   

> Can I hope that the disrete graphics is "turned off" while normally working on my laptop?

 

It's not, but you can turn it off manually using either acpi_call or the recently developed bbswitch module.

As for CUDA, no idea really. I'd say it should be possible but I don't know details.

----------

## rmorell

Hi Spiros,

 *Spiros wrote:*   

> I would really like to be able to develop and run CUDA or OpenCL applications on this discrete graphics device and let everything else run on the Intel chip.
> 
> Is this possible? Are there any caveats?

 

Yes, this is possible.  The only sticking point is getting everything installed and set up properly; it's not particularly well-documented right now, unfortunately.

The gentoo x11-drivers/nvidia-drivers ebuild should install everything you need, but will also symlink your system's OpenGL files to nvidia's.  This will prevent the Intel OpenGL from working.  To switch back to the Mesa OpenGL implementation that works on Intel, use:

```
# eselect opengl set xorg-x11
```

after installing nvidia-drivers.

One additional step that may be necessary on Optimus hardware that you might not realize on nvidia-only hardware is that the nvidia kernel module needs to be loaded and the /dev/nvidiactl and /dev/nvidia[0-9]+ device nodes need to be created, which can generally only be done as root.  The nvidia driver components are able to do this themselves, but only when run as root.  (On a system where you run an X server with nvidia's driver, since the X server is a setuid root binary, this is always the case.)  In the absence of an X server running the nvidia driver, you need to perform this manually before being able to run CUDA or OpenCL apps as an unprivileged (non-root) user.

One way to do that is to run something like:

```
# nvidia-xconfig --query-gpu-info
```

as root (but note that you don't want to run nvidia-xconfig without --query-gpu-info since that will cause the intel driver to stop loading by default in your X config).  Another option is to write some udev rules to do this, or add to /etc/conf.d/local.start.

Gentoo's ebuild additionally will create the /dev/nvidia* device nodes with the group "video" and restrict access to only those users in that group.  So you'll need to add any users you'd like to run CUDA or OpenCL apps to the "video" group.

After that, you should be able to use CUDA and OpenCL as any user in the "video" group.

 *Spiros wrote:*   

> I'm also interested in having a long-life battery. Can I hope that the disrete graphics is "turned off" while normally working on my laptop? I'm sure that the Intel graphics is more than sufficient for me.

 

Right now, the discrete GPU is always on in Optimus laptops.  There are hacks to disable it as mentioned in this thread, but I recommend not using them since they can lead to system instability.  nvidia hopes to have a better solution for power (turning off the GPU when not in use) soon.

(note: I work on the NVIDIA Linux graphics device driver team)

----------

## Spiros

Thank you both.

rmorell: thank you very much. Your explanations are just perfect! I will try as soon as possible (i.e. as soon as I get my laptop).

----------

## WuDDjA

I got the module and the bumblebee daemon working a few days ago on my Alienware Optimus chipset and I am about to write an article on the Gentoo Wiki.

At the moment everything is working perfectly on my system.

on boot an initscript I've written starts the bumblebeed

the daemon disables the nvidia card by using the bbswitch module

with "optirun" as a pre command I am able to start applications with nvidia native driver

after closing the application the bumblebee daemon unloads the nvidia driver and disables the nvidia card

The reason for me to spend days to get this software to work was the battery power.

Without disabling the nvidia card my battery life is shrinking by 4 hours

Shall I let you know when the wiki article is finished?

----------

## rmorell

 *WuDDjA wrote:*   

> 
> 
> on boot an initscript I've written starts the bumblebeed
> 
> the daemon disables the nvidia card by using the bbswitch module
> ...

 

Since this thread specifically mentioned CUDA, one warning about bumblebee's "power saving" option: in addition to the system unstability that I mentioned (which may vary from system to system, so maybe you get lucky), bumblebee generally doesn't know about CUDA applications, unlike OpenGL applications.  So if you try to run CUDA applications while bumblebee has disabled the power to the GPU, at best they will fail, and at worst your system will become unresponsive.

It may be possible to run a CUDA application under bumblebee's `optirun` wrapper to get the power turned on (I've never tried this, though, and it might not be sufficient).

As I mentioned in my earlier post, we hope to have a better solution for this soon, so bumblebee doesn't have to go behind the driver's back to lower power usage.

----------

## Spiros

WuDDjA: this seems REALLY interesting! Yes, please let me know if you can!

Anyway, if I can turn on the GPU by just giving some commands, work with CUDA and then shut it down when I'm finished, I could live with that. I'm not scared of ha[/list][/list]ving an unstable system, I can live with some instabilities (I always do, actually).

After Googling a bit I found two interesting references. The first one just says "Everything is working properly (also CUDA)". The second one gives some more explanations.

[list]

[*]  https://lists.launchpad.net/hybrid-graphics-linux/msg01217.html

[*] http://www.ruyk.com/tech/?p=68

[list=]

----------

