"Since pyfaust 2.9.0 the API has been modified to make the GPU available directly from the python wrapper.\n",
"Indeed, an independent GPU module (aka ``gpu_mod``) has been developed for this purpose. \n",
"\n",
"The first question you might ask is: does it work on my computer? Here is the answer: the loading of this module is quite transparent, if an NVIDIA GPU is available and CUDA is properly installed on your system, you have normally nothing to do except installing pyfaust to get the GPU implementations at your fingertips. We'll see at the end of this notebook how to load manually the module and how to get further information in case of an error. \n",
"The first question you might ask is: does it work on my computer? Here is the answer: the loading of this module is quite transparent, if an NVIDIA GPU is available and CUDA is properly installed on your system, you have normally nothing to do except installing pyfaust to get the GPU implementations at your fingertips. We'll see in the end of this notebook how to load manually the module and how to get further information in case of an error. \n",
"\n",
"It is worthy to note two drawbacks about the pyfaust GPU support:\n",
"- Mac OS X is not supported because NVIDIA has stopped to support this OS.\n",
"- On Windows and Linux, the pyfaust GPU support is currently limited to CUDA 9.2 version.\n",
"- On Windows and Linux, the pyfaust GPU support is currently limited to CUDA 9.2 and 11.x versions.\n",
"\n",
"In addition to these drawbacks, please notice that the GPU module support is still considered in beta status as the code is relatively young and still evolving. However the API shouldn't evolve that much in a near future."
]
...
...
@@ -121,7 +121,7 @@
"\n",
"Many of the functions for generating a Faust object on CPU are available on GPU too. It is always the same, you precise the ``dev`` argument by assigning the ``'gpu'`` value and you'll get a GPU Faust instead of a CPU Faust.\n",
"\n",
"For example, the code below will successively create a random GPU Faust, a Hadamard transform GPU Faust, identity GPU Faust and finally a DFT GPU Faust.\n"
"For example, the code below will successively create a random GPU Faust, a Hadamard transform GPU Faust, a identity GPU Faust and finally a DFT GPU Faust.\n"
]
},
{
...
...
@@ -227,7 +227,7 @@
"source": [
"### Benchmarking your GPU with pyfaust!\n",
"\n",
"Of course when we run some code on GPU rather than on CPU, it is clearly to enhance the performances. So let's try your GPU and find out if it is worth it or not compared to your CPU.\n",
"Of course when we run some code on GPU rather than on CPU, it is clearly to enhance the performance. So let's try your GPU and find out if it is worth it or not compared to your CPU.\n",
"\n",
"First, measure how much time it takes on CPU to compute a Faust norm and the dense array corresponding to the product of its factors:\n",
"\n"
...
...
@@ -381,20 +381,20 @@
" <tr>\n",
" <td>CPU</td>\n",
" <td>Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz</td>\n",
" <td>1241.00</td>\n",
" <td>.129</td>\n",
" <td>616.16</td>\n",
" <td>.130</td>\n",
" </tr>\n",
" <tr>\n",
" <td>GPU</td>\n",
" <td>NVIDIA GTX980</td>\n",
" <td>166.72</td>\n",
" <td>.129</td>\n",
" <td>147.81</td>\n",
" <td>.130</td>\n",
" </tr>\n",
" <tr>\n",
" <td>GPU</td>\n",
" <td>NVIDIA Tesla V100</td>\n",
" <td>49.49</td>\n",
" <td>.129</td>\n",
" <td>RTX 2080</td>\n",
" <td>73.88</td>\n",
" <td>.130</td>\n",
" </tr>\n",
" </table>"
]
...
...
@@ -484,7 +484,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
...
...
@@ -498,7 +498,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.9.2"
}
},
"nbformat": 4,
...
...
%% Cell type:markdown id: tags:
# Using The GPU FAµST API
In this notebook we'll see quickly how to leverage the GPU computing power with pyfaust.
Since pyfaust 2.9.0 the API has been modified to make the GPU available directly from the python wrapper.
Indeed, an independent GPU module (aka ``gpu_mod``) has been developed for this purpose.
The first question you might ask is: does it work on my computer? Here is the answer: the loading of this module is quite transparent, if an NVIDIA GPU is available and CUDA is properly installed on your system, you have normally nothing to do except installing pyfaust to get the GPU implementations at your fingertips. We'll see at the end of this notebook how to load manually the module and how to get further information in case of an error.
The first question you might ask is: does it work on my computer? Here is the answer: the loading of this module is quite transparent, if an NVIDIA GPU is available and CUDA is properly installed on your system, you have normally nothing to do except installing pyfaust to get the GPU implementations at your fingertips. We'll see in the end of this notebook how to load manually the module and how to get further information in case of an error.
It is worthy to note two drawbacks about the pyfaust GPU support:
- Mac OS X is not supported because NVIDIA has stopped to support this OS.
- On Windows and Linux, the pyfaust GPU support is currently limited to CUDA 9.2 version.
- On Windows and Linux, the pyfaust GPU support is currently limited to CUDA 9.2 and 11.x versions.
In addition to these drawbacks, please notice that the GPU module support is still considered in beta status as the code is relatively young and still evolving. However the API shouldn't evolve that much in a near future.
%% Cell type:markdown id: tags:
### Creating a GPU Faust object
Let's start with some basic Faust creations on the GPU. Almost all the ways of creating a Faust object in CPU memory are also available to create a GPU Faust.
First of all, creating a Faust using the constructor works seamlessly on GPU, the only need is to specify the ``dev`` keyword argument, as follows:
%% Cell type:code id: tags:
``` python
frompyfaustimportFaust
fromnumpy.randomimportrand
M,N=rand(10,10),rand(10,15)
gpuF=Faust([M,N],dev='gpu')
gpuF
```
%% Cell type:markdown id: tags:
It's clearly indicated in the output that the Faust object is instantiated in GPU memory (the N and M numpy arrays are copied from the CPU to the GPU memory). However it's also possible to check this programmatically:
%% Cell type:code id: tags:
``` python
gpuF.device
```
%% Cell type:markdown id: tags:
While for a CPU Faust you'll get:
%% Cell type:code id: tags:
``` python
Faust([M,N],dev='cpu').device
```
%% Cell type:markdown id: tags:
In ``gpuF`` the factors are dense matrices but it's totally possible to instantiate sparse matrices on the GPU as you can do on CPU side.
You can also create a GPU Faust by explicitly copying a CPU Faust to the GPU memory. Actually, at anytime you can copy a CPU Faust to GPU and conversely. The ``clone()`` member function is here precisely for this purpose. Below we copy ``gpuF`` to CPU and back again to GPU in the new Faust ``gpuF2``.
%% Cell type:code id: tags:
``` python
cpuF=gpuF.clone('cpu')
gpuF2=cpuF.clone('gpu')
gpuF2
```
%% Cell type:markdown id: tags:
## Generating a GPU Faust
Many of the functions for generating a Faust object on CPU are available on GPU too. It is always the same, you precise the ``dev`` argument by assigning the ``'gpu'`` value and you'll get a GPU Faust instead of a CPU Faust.
For example, the code below will successively create a random GPU Faust, a Hadamard transform GPU Faust, identity GPU Faust and finally a DFT GPU Faust.
For example, the code below will successively create a random GPU Faust, a Hadamard transform GPU Faust, a identity GPU Faust and finally a DFT GPU Faust.
### Manipulating GPU Fausts and CPU interoperability
Once you've created GPU Faust objects, you can perform operations on them staying in GPU world (that is, with no array transfer to CPU memory). That's of course not always possible.
For example, let's consider Faust-scalar multiplication and Faust-matrix product. In the first case the scalar is copied to the GPU memory and likewise in the second case the matrix is copied from CPU to GPU in order to proceed to the computation. However in both cases the Faust factors stay into GPU memory and don't move during the computation.
%% Cell type:code id: tags:
``` python
# Faust-scalar multiplication
2*gpuF
```
%% Cell type:markdown id: tags:
As you see the first factor's address has changed in the result compared to what it was in ``gpuF``. Indeed, when you make a scalar multiplication only one factor is multiplied, the others don't change, they are shared between the Faust being multiplied and the resulting Faust. This is an optimization and to go further in this direction the factor chosen to be multiplied is the smallest in memory (not necessarily the first one).
%% Cell type:code id: tags:
``` python
# Faust-matrix product (the matrix is copied to GPU
# then the multiplication is performed on GPU)
gpuF@rand(gpuF.shape[1],15)
```
%% Cell type:markdown id: tags:
On the contrary, and that matters for optimization, there is no CPU-GPU transfer at all when you create another GPU Faust named for example ``gpuF2`` on the GPU and decide to multiply the two of them like this:
%% Cell type:code id: tags:
``` python
frompyfaustimportrandasfrand
gpuF2=frand(gpuF.shape[1],18,dev='gpu')
gpuF3=gpuF@gpuF2
gpuF3
```
%% Cell type:markdown id: tags:
Besides, it's important to note that ``gpuF3`` factors are not duplicated in memory because they already exist for ``gpuF`` and ``gpuF2``, that's an extra optimization: ``gpuF3`` is just a memory view of the factors of ``gpuF`` and ``gpuF2`` (the same GPU arrays are shared between ``Faust`` objects). That works pretty well the same for CPU ``Faust`` objects.
Finally, please notice that CPU Faust objects are not directly interoperable with GPU Fausts objects. You can try, it'll end up with an error.
%% Cell type:code id: tags:
``` python
cpuF=frand(5,5,5,dev='cpu')
gpuF=frand(5,5,6,dev='gpu')
try:
print("A first try to multiply a CPU Faust with a GPU one...")
cpuF@gpuF
except:
print("it doesn't work, you must either convert cpuF to a GPU Faust or gpuF to a CPU Faust before multiplying.")
print("A second try using conversion as needed...")
print(cpuF.clone('gpu')@gpuF)# this is what you should do
print("Now it works!")
```
%% Cell type:markdown id: tags:
### Benchmarking your GPU with pyfaust!
Of course when we run some code on GPU rather than on CPU, it is clearly to enhance the performances. So let's try your GPU and find out if it is worth it or not compared to your CPU.
Of course when we run some code on GPU rather than on CPU, it is clearly to enhance the performance. So let's try your GPU and find out if it is worth it or not compared to your CPU.
First, measure how much time it takes on CPU to compute a Faust norm and the dense array corresponding to the product of its factors:
9.86 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
13.8 ms ± 39.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
```
%% Cell type:markdown id: tags:
### Running some FAµST algorithms on GPU
Some of the FAµST algorithms implemented in the C++ core are now also available in pure GPU mode.
For example, let's compare the factorization times taken by the hierarchical factorization when launched on CPU and GPU.
When running on GPU, the matrix to factorize is copied in GPU memory and almost all operations executed during the algorithm don't imply the CPU in any manner (the only exception at this stage of development is the proximal operators that only run on CPU).
**Warning: THE COMPUTATION CAN LAST THIRTY MINUTES OR SO ON CPU**
Depending on you GPU card and CPU the results may vary, so below are shown some results obtained on specific hardware.
<tablealign="left">
<tralign="center">
<th>Implementation</th>
<th> Hardware </th>
<th> Time (s) </th>
<th>Error Faust vs MEG matrix </th>
</tr>
<tr>
<td>CPU</td>
<td>Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz</td>
<td>1241.00</td>
<td>.129</td>
<td>616.16</td>
<td>.130</td>
</tr>
<tr>
<td>GPU</td>
<td>NVIDIA GTX980</td>
<td>166.72</td>
<td>.129</td>
<td>147.81</td>
<td>.130</td>
</tr>
<tr>
<td>GPU</td>
<td>NVIDIA Tesla V100</td>
<td>49.49</td>
<td>.129</td>
<td>RTX 2080</td>
<td>73.88</td>
<td>.130</td>
</tr>
</table>
%% Cell type:markdown id: tags:
### Manually loading the pyfaust GPU module
If something goes wrong when trying to use the GPU pyfaust extension, here is how to manually load the module and obtain more information.
The key is the function [enable_gpu_mod](https://faustgrp.gitlabpages.inria.fr/faust/last-doc/html/namespacepyfaust.html#aea03fff2525fc834f2a56e63fd30a54f). This function allows to give another try to ``gpu_mod`` loading with the verbose mode enabled.
%% Cell type:code id: tags:
``` python
importpyfaust
pyfaust.enable_gpu_mod(silent=False,fatal=True)
```
%% Cell type:markdown id: tags:
Afterward you can call ``pyfaust.is_gpu_mod_enabled()`` to verify if it works in your script.
Below I copy outputs that show what it should look like when it doesn't work:
1) If you asked a fatal error using ``enable_gpu_mod(silent=False, fatal=True)`` an exception will be raised and your code won't be able to continue after this call:
WARNING: you must call enable_gpu_mod() before using GPUModHandler singleton.
loading libgm
libcublas.so.9.2: cannot open shared object file: No such file or directory
[...]
Exception: Can't load gpu_mod library, maybe the path (/home/test/venv_pyfaust-2.10.14/lib/python3.7/site-packages/pyfaust/lib/libgm.so) is not correct or the backend (cuda) is not installed or configured properly so the libraries are not found.
```
2) If you just want a warning, you must use ``enable_gpu_mod(silent=False)``, the code will continue after with no gpu_mod enabled but you'll get some information about what is going wrong (here the CUDA toolkit version 9.2 is not installed) :