Difference between revisions of "Programming/Python"

From HPC
Jump to: navigation , search
Line 67: Line 67:
 
* module add python/anaconda/4.0/3.5
 
* module add python/anaconda/4.0/3.5
 
* module add python/anaconda/4.1.1/2.7
 
* module add python/anaconda/4.1.1/2.7
 +
 +
and miniconda is provided by
 +
 
* module add python/anaconda/4.3.31/3.6-VE
 
* module add python/anaconda/4.3.31/3.6-VE
 
+
* module add python/anaconda/4.6/miniconda/3.7
 +
* module add python/anaconda/202111/3.9
 +
* module add python/anaconda/20220712/3.9
  
 
==== Compilation ====
 
==== Compilation ====
Line 101: Line 106:
  
 
module purge
 
module purge
module add anaconda/4.0/3.5
+
module add python/anaconda/20220712/3.9
 
module add openmpi/gcc/1.10.2
 
module add openmpi/gcc/1.10.2
 
export I_MPI_DEBUG=5
 
export I_MPI_FABRICS=shm:tmi
 
export I_MPI_FALLBACK=no
 
  
 
mpirun python broadcast.py
 
mpirun python broadcast.py
Line 181: Line 182:
 
* See [http://docs.cython.org/en/latest/src/userguide/parallelism.html Cython Parallelism] documentation for more details here.
 
* See [http://docs.cython.org/en/latest/src/userguide/parallelism.html Cython Parallelism] documentation for more details here.
  
==Further Information==
+
== Next Steps ==
  
 
* [https://en.wikipedia.org/wiki/Python_(programming_language) https://en.wikipedia.org/wiki/Python_(programming_language)]
 
* [https://en.wikipedia.org/wiki/Python_(programming_language) https://en.wikipedia.org/wiki/Python_(programming_language)]

Revision as of 12:06, 2 November 2022

Programming Details

Python is a widely used high-level programming language used for general-purpose programming.

An interpreted language, Python has a design philosophy that emphasizes code readability (notably using white-space indentation to delimit code blocks rather than curly braces or keywords), and a syntax that allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or JAVA

Icon pencil.png When programming with Python in a HPC environment you will need to change the first line as shown below

 #!/usr/bin/python

To



 #!/usr/bin/env python

Python example


#!/usr/bin/env python

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
   data = {'key1' : [7, 2.72, 2+3j],
           'key2' : ( 'abc', 'xyz')}
else:
   data = None

data = comm.bcast(data, root=0)

if rank != 0:
        print ("data is %s and %d" % (data,rank))
else:
        print ("I am master\n")
Icon exclam3.png Due to the limited of the Python interpreter (GIL) there is no point to use threads for CPU intensive tasks in Python. You need either multiprocessing or use C extensions that release GIL during computations e.g., some of numpy functions, example.

Modules Available

The following modules are available:

Python provided by the Python Software foundation

  • module add python/2.7.11
  • module add python/3.5.1

Anaconda python Anaconda is the open data science platform powered by Continuum

  • module add python/anaconda/4.0/2.7
  • module add python/anaconda/4.0/3.5
  • module add python/anaconda/4.1.1/2.7

and miniconda is provided by

  • module add python/anaconda/4.3.31/3.6-VE
  • module add python/anaconda/4.6/miniconda/3.7
  • module add python/anaconda/202111/3.9
  • module add python/anaconda/20220712/3.9

Compilation

Python is byte-compiled at runtime by typing for example


[username@login01 ~]$  python myPython.py


Usage Examples

Batch example

 
#!/bin/bash
#SBATCH -J compute-single-node
#SBATCH -N 1
#SBATCH --ntasks-per-node 20
#SBATCH -o %N.%j.%a.out
#SBATCH -e %N.%j.%a.err
#SBATCH -p compute
#SBATCH --exclusive
#SBATCH --mail-user= your email address here

echo $SLURM_JOB_NODELIST

module purge
module add python/anaconda/20220712/3.9
module add openmpi/gcc/1.10.2

mpirun python broadcast.py


[username@login01 ~]$ sbatch python-demo.job
Submitted batch job 289572

Python and OpenMP

The Python interpreter uses a GIL (Global Interpreter Lock) which makes multi-threading almost impossible. Although there are ways around this and one of the most common methods is to use Cython.

Cython supports native parallelism through the cython.parallel module. To use this kind of parallelism, the GIL must be released (see Releasing the GIL). It currently supports OpenMP

  • Example with a reduction (on sum):

#!/usr/bin/env python
from cython.parallel import prange

cdef int i
cdef int n = 30
cdef int sum = 0

for i in prange(n, nogil=True):
    sum += i

print(sum)
  • Example with a typed memoryview (e.g. a NumPy array):

#!/usr/bin/env python
from cython.parallel import prange

def func(double[:] x, double alpha):
    cdef Py_ssize_t i

    for i in prange(x.shape[0]):
        x[i] = alpha * x[i]

To actually use the OpenMP support, you need to tell the C or C++ compiler to enable OpenMP. For gcc this can be done as follows in a setup.py:


#!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

ext_modules = [
    Extension(
        "hello",
        ["hello.pyx"],
        extra_compile_args=['-fopenmp'],
        extra_link_args=['-fopenmp'],
    )
]

setup(
    name='hello-parallel-world',
    ext_modules=cythonize(ext_modules),
)

Next Steps

Navigation