Commit 0ba606a5 authored by Franck Thollard's avatar Franck Thollard
Browse files

Merge branch 'master' of gricad-gitlab.univ-grenoble-alpes.fr:python-uga/training-hpc

parents ee47b450 ab5a9f67
......@@ -824,7 +824,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.7.2"
}
},
"nbformat": 4,
......
......@@ -27,10 +27,36 @@
%% Cell type:markdown id: tags:
## Different parallel strategies
%% Cell type:markdown id: tags:
### IO bounded: one process + `async`/`await`
<p class="small"><br></p>
Cooperative concurrency
<p class="small"><br></p>
Functions able to pause
<p class="small"><br></p>
`asyncio`, [`trio`](https://trio.readthedocs.io)
%% Cell type:raw id: tags:
<div align="middle">
<img src="./fig/logo_trio.png" style="width: 15%">
</div>
%% Cell type:markdown id: tags:
## Different parallel strategies
### One process split in light subprocesses called threads 👩🏼‍🔧 👨🏼‍🔧👩🏼‍🔧 👨🏼‍🔧
- handled by the OS
- share memory and can use at the same time different CPU cores
......
%% Cell type:markdown id: tags:
# Port Python 2 code to Python 3
%% Cell type:markdown id: tags:
Once upon a time, it was very difficult to:
- convert code written in Python 2
- write code compatible for py2 and py3
Dependencies + strong language incompatibilities (between Python 2.6 and 3.3).
Now, it much simpler!
- main packages all work in Python 3
- smaller language incompatibility (between Python 2.7 and >3.4) (for example `from __future__ import print_function, division`)
- good tools to write compatible code ([six](https://pypi.org/project/six/) and [future](https://python-future.org))
But... still some differences... For example for us in sciences:
- division `int / int`
- str are unicode in Python 3 but not in Python 2
%% Cell type:markdown id: tags:
## Steps to port Python 2 code to Python 3
- Solidify the Python 2 code (write tests!)
- Use [pyupgrade](https://github.com/asottile/pyupgrade) to modernize the code to Python 2.7 ➡️ test!
- Use the automatic tool `futurize -w mymodule.py` (provided by [future](https://python-future.org)) ➡️ test!
Today, it can make sense to stop supporting Python 2.7 (and even Python 3.5). See https://python3statement.org/.
Main advantage: One can use nice modern features, for example:
- f-strings
- `from pathlib import Path`
It can be done automatically with pyupgrade:
`find -name "*.py" | xargs pyupgrade --py36-plus`
%% Cell type:markdown id: tags:
# Developer tools
## IDE (open-source)
- Spyder
- Visual Studio Code
- ...
## Checker and formatter
- [pylint](https://www.pylint.org/): check Python code
- [black](https://github.com/python/black): The uncompromising Python code formatter
- [mypy](http://mypy-lang.org/): Optional Static Typing for Python
%% Cell type:markdown id: tags:
# Port a Matlab code to Python
⚠️ ⚠️ Difficult !! ⚠️ ⚠️
### Converter (with runtime dependence)
- [Smop](https://github.com/victorlei/smop), Small Matlab to Python compiler: convert Matlab code to Python code
See https://stackoverflow.com/a/17535694
### Other approach
- fluidmat2py, provided by [fluiddyn](https://fluiddyn.readthedocs.io)
```
usage: fluidmat2py [-h] [-c] path
Utility to produce a strange code which is no longer Matlab and not yet Python.
positional arguments:
path str indicating which file or directory has to be used.
optional arguments:
-h, --help show this help message and exit
-c, --clean Only clean the Matlab code.
```
%% Cell type:markdown id: tags:
......@@ -30,8 +30,9 @@ module dtw_cort
end do
! Fill the dtw_matrix
do i = 2, len_s1
do j = 2, len_s2
do j = 2, len_s2
do i = 2, len_s1
dist = dabs(s1(i) - s2(j))
dtw_mat(i, j) = dist + dmin1(dtw_mat(i - 1, j), &
dtw_mat(i, j - 1), &
......
all:
python3 -m numpy.f2py -c "dtw_cort.f90" -m distances_fort
python3 -m numpy.f2py --opt="-O3 -march=native" -c "dtw_cort.f90" -m distances_fort
clean:
rm -f distances_fort.*.so *.mod
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment