pascalwhoop
pascalwhoop

Reputation: 3101

Run python scripts in virtual environment using optirun

How may one run a python tool that uses a virtual environment and an library like click if the tool requires the dedicated GPU which is only available via bumblebee or optirun?

Example:

#normally (with nvidia driver being the default)
agent --run-with-tf-gpu train

the agent script triggers the click based application which then uses the venv/bin/python binary.

I have a solution (see answer below) but I am happy to award a better one with the "right answer"

Upvotes: 1

Views: 289

Answers (1)

pascalwhoop
pascalwhoop

Reputation: 3101

Option 1: replace binary with wrapper

p=`pwd`
cd venv/bin
rm python #soft link to python3.6
echo 'optirun $p/venv/bin/python3.6 "$*"' > python
chmod +x python

Option 2: create aliases

only works if python is not called with an absolute path.

Insert these three lines at the end of the venv/bin/activate file:

#setting optirun to be used when running python in venv
alias python="optirun python"
alias python3="optirun python3"

Option 3: turn card on and off before and after

according to the arch wiki the following will also enable the card globally so one may easily use IDEs that otherwise perform some complex call with absolute paths etc:

tee /proc/acpi/bbswitch <<< ON

Now when you start a CUDA application it is going to automatically load all the necessary modules.

To turn off the nvidia card after using CUDA do:

rmmod nvidia_uvm
rmmod nvidia
tee /proc/acpi/bbswitch <<< OFF

Upvotes: 0

Related Questions