Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: ManishSahu on October 21, 2018, 01:48:48 PM

Title: Multi GPU enable using python API.
Post by: ManishSahu on October 21, 2018, 01:48:48 PM
I have 2 GTX 1080. I want to enable both GPUs for processing using only python API. Can anyone tell me how to do it?
Title: Re: Multi GPU enable using python API.
Post by: KBL on October 30, 2018, 11:38:56 PM
You need to set the GPU Mask, which I found sort of confusing. The GPU mask is a binary number (converted to an integer) indicating which GPUs you'd like to use. For example, if you had three GPUs, and you wanted to use the first two, the binary would be 110 (yes, yes, no). For two GPUs if you wanted to use both it would be 11 (yes, yes). That binary then needs to be converted to it's decimal equivalent and set under PhotoScan.app.gpu_mask = <mask>.

Common masks:
enable 1 GPU = 1
enable 2 GPUs = 3
enable 3 GPUs = 7
enable 4 GPUs = 15

You can enable two GPUs using the following code:

# Enable two GPUs
PhotoScan.app.gpu_mask = 3

As a general solution to this problem, since I'm working on a script to be run on systems with 0 to 8 GPUs, the following code will determine the GPU mask and enable all of your GPUs automatically:

# Determine GPU binary string
gpuBinary = ""
gpus = PhotoScan.app.enumGPUDevices()
for gpu in gpus:
    gpuBinary += "1"
if gpuBinary == "": gpuBinary = "0"

# Convert binary string to int
gpuMask = int(gpuBinary,2)

# Enable all GPUs
PhotoScan.app.gpu_mask = gpuMask