bodono / scs-python

Python interface for SCS
MIT License
41 stars 33 forks source link

Strategy of solving many SOCPs with GPU #31

Open yifei-liu-stat opened 3 years ago

yifei-liu-stat commented 3 years ago

Hi,

Suppose I want to solve a sequence (a lot) of SOCPs of small or moderate size, would you recommend utilizing GPU? I can solve a single SOCP with SCS solver (called from cvxpy) with option "gpu=True", but running with GPU seems slower than running with CPU in my case (small-size SOCP). Maybe the cost of transferring data to GPU prevails. Now if I want to solve a sequence of them, then the cost of running with GPU would be much higher than the cost of running with CPU, do you have any suggestions on that?

Thank you

bodono commented 3 years ago

The first thing is to make sure you have the latest GPU version which includes this commit which provides about 10x speedup. The other tricks would be to use warm-starting if the problems are closely related and to use the ability of cvxpy to parse the problem once and reuse it so you only pay cvxpy's cost once.

yifei-liu-stat commented 3 years ago

I have tried to parse the problem with cvxpy.parameter() and matrix-form constraints, and they indeed provided efficiency!

On checking whether I have the latest GPU version installed, what is the difference between using setup.py and the commit you provided? I followed the gpu-version installation on scs-python:

git clone --recursive https://github.com/bodono/scs-python.git
cd scs-python
python setup.py install --scs --gpu

If there is no difference, how should I check whether I have the latest GPU version? If there is a difference, how should I use the file private.c to install the newest gpu-version scs (kind of confused on using that file)? Thanks for your help!

bodono commented 3 years ago

OK in the scs-python directory there is an scs directory that contains the C code. cd into that directory and do a git pull, that will bring you up to the latest commit (you can make sure you see that commit in the logs by doing git log and looking for it). Then cd back up to scs-python and run the usual python setup.py install --scs --gpu.

Let me know if that makes it faster for you, hopefully should be a big speedup!

yifei-liu-stat commented 3 years ago

Yeah, it helped me to some extent. I was wondering why git clone didn't incorporate the latest commit on that file you mentioned. Also, do you have an available test demo where GPU can accelerate a lot? In my case, It only improved a bit, and I want to check my installation with a suitable demo. Thank you!

bodono commented 3 years ago

The speedup depends a lot on the data structure and sparsity. You can use nvidia-smi to see how much your GPU is being used, if it's mostly idle then either it's not installed correctly or the data pattern isn't yielding much speedup.

h-vetinari commented 3 years ago

The first thing is to make sure you have the latest GPU version which includes this commit which provides about 10x speedup.

Hey, I'm (co-)maintaining the conda-forge feedstock for scs, and I didn't know that CUDA is supported. That's awesome, I'll try to add GPU builds to conda-forge as well.

Looking at your quote, it sounds like it'd make a lot of sense to have a new release that includes the commit you mention? Do you have any plans to cut a new release, or would you be able to do that?

bodono commented 3 years ago

Oh awesome, yes it would be great to get the gpu build into the conda package, thanks!

I will cut a new github release, do you also need a new pypi release?

h-vetinari commented 3 years ago

I will cut a new github release, do you also need a new pypi release?

Cool thanks! Either is fine.

Are there some GPU-specific tests? Feel free to chime in in https://github.com/conda-forge/scs-feedstock/pull/21 as well. 🙃

bodono commented 3 years ago

Ok, I cut a new SCS github release and a new Pypi release (v2.1.3).

h-vetinari commented 3 years ago

@h-vetinari: Are there some GPU-specific tests?

@bodono: just checking in quickly - I'd like to make sure we're not pushing out packages that don't work, but cannot find anything in this repo that would explicitly exercise the GPU paths.

The CI in conda-forge/scs-feedstock#21 runs through without having any GPUs (but I could test the produced artefacts locally with a GPU), so presumably the SCS-code switches behaviour based on whether a GPU is available?

bodono commented 3 years ago

Hi @h-vetinari , thanks again for doing all this work, and sorry for the delay in responding.

This file checks if the gpu path can be imported, and if it can it will test the gpu code.

I think that's all the tests I have for gpus unfortunately. I can probably turn that into a real test, but it won't be for a little while I'm afraid.

h-vetinari commented 3 years ago

Hi @bodono, thanks for the hint; took me a while to get the code to a point where I could verify it runs as desired, and the answer is still: "unsure" - I've added my current progress in #37, please take a look.

h-vetinari commented 3 years ago

There's now a separate issue for GPU tests: #40