Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rendering in Blender on a machine with multiple GPUs

Rendering in Blender on a machine with multiple GPUs

So here's the premise: For scenes that take around a minute or less to render, performance is actually worse if you render on all of the cards with a single instance of Blender. This is because (AFAIK) there's a bit of additional time necessary to collect the render results from each card and stitch them together. That time is a fixed short duration, so it's negligible on larger/longer render jobs. However, on shorter render jobs, the 'stitch time' has a much more significant impact.

I ran into this with a machine I render on that has 4 Quadro K6000s in it. To render animations, I ended up writing a few little scripts to facilitate launching 4 separate instances of Blender, each one tied to one GPU. Overall rendertime was much shorter with that setup than one instance of Blender using all 4 GPUs.

The setup works basically like this... I have a the following Python script (it can be anywhere on your hard drive, so long as you remember the path to it). I call it

import sys
import bpy

#XXX Assumes sys.argv[1] is the CUDA device

argv = sys.argv[sys.argv.index("--") + 1:]

if len(argv) > 0:
    dev = int(argv[0])
    C = bpy.context
    cycles_prefs = C.user_preferences.addons['cycles'].preferences
    C.scene.render.use_overwrite = False
    C.scene.render.use_placeholder = True
    cycles_prefs.compute_device_type = "CUDA"

    for device in cycles_prefs.devices:
        device.use = False
    cycles_prefs.devices[dev].use = True

This script takes a single compute device (an integer number) as an input argument (argv). The script disables all compute devices except for the one specified. By doing that, only one GPU is used for this running instance of Blender. The use_overwrite and use_placeholder settings are also critical to have in there. Otherwise, all of my Blender instances will be rendering the same frames.

With the script created and accessible, I just need a script to launch 4 instances of Blender. As I'm in Linux, I did it in bash. However, you could just as easily write a Windows BAT script or even a Python script. The key is that you need a mechanism that lauches Blender and lets that instance run in the background while you launch another instance of Blender. My bash script looks like this:


if [ $# = 0 ]; then
    echo "You forgot to mention the .blend file you want to render"
    for i in `seq 0 3`;
        blender -b $1 --python /path/to/ -a -- $i &

In short, this script launches 4 instances of Blender (seq 0 3), and each instance uses a different CUDA device. The ampersand (&) at the end is what lets each instance of Blender run in the background. I'm not sure what the correct flag is in Windows. It does exist, though. I'd looked it up at one point, but have since forgotten it.

And that's basically how I do it. Now, I know that this could certainly stand to be refined. In the meantime, however, this certainly does the job for me.

Copy link

sbrl commented Mar 3, 2022

For Nvidia GPUs, see also the CUDA_VISIBLE_DEVICES environment variable, which takes a comma-separated of integers that specify the GPUs which should be made visible to the running application. Said integers are the indexes you can find in the nvidia-smi command.

Copy link

robomartion commented Apr 1, 2022

Using this could you basically combine the VRAM of multiple cards without NVLink? So a 3840x2160 frame of a scene that requires 48GB of VRAM is split into four 960x2160 frames, 12GB each allowing for four RTX 3080Tis to render it as if they were two RTX 3090s?

Copy link

Fweeb commented Apr 1, 2022

@robomartion I haven't tested for that particular use-case, but hypothetically it could be used that way. Final render resolution isn't necessarily the only determiner for how much VRAM a scene uses. Objects off-camera contribute to lighting, so the size of the assembled BVH is likely to play a part as well (and may not actually be smaller if you render just a crop of your intended final image).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment