SWIG and Ctypes#
Interfacing Fortran code through C interface
I found this reference with good examples.
Problem: Collatz conjecture#
Choose \(u_0\)
If \(u_k\) even, \(u_{k+1} \rightarrow \frac{u_k}{2}\) ;
If \(u_k\) odd, \(u_{k+1} \rightarrow 3 u_k+1\)
The Collatz conjecture is: For all \(u_0>0\) , the process will eventually reach \(u_k=1\).
The programs below compute number of steps (named flight) to reach \(f(u_0)\) for \(1\leq u_0 \leq N\), \(N\) given.
C program#
%%file syracuse.c
#include <stdlib.h>
#include <stdio.h>
long syracuse(long n) {
long count = 0L ;
while (n > 1) {
if ((n&1)==0)
n /= 2;
else
n = 3*n+1;
count++;
}
return count ;
}
int main() {
const long N = 1000000;
double t1, t2;
long i , *flights ;
flights = (long*)malloc(N*sizeof(long));
for (i = 0; i <N; i++) flights[i] = syracuse(i+1);
return EXIT_SUCCESS;
}
Writing syracuse.c
%%bash
gcc -O3 syracuse.c
time ./a.out
real 0m0.001s
user 0m0.001s
sys 0m0.000s
Python program#
%%time
from itertools import count
def syracuse(n):
x = n
for steps in count() :
if x & 1 :
x = 3*x+1
else:
x = x // 2
if x == 1:
return steps
N = 1000000
flights = [syracuse(i) for i in range(1,N+1)]
CPU times: user 9.44 s, sys: 6.5 ms, total: 9.45 s
Wall time: 9.45 s
Performances#
The python syntax is simpler.
100 times slower
Solution : call the C function from python.
Ctypes#
This is the C function we will call from python
%%file syrac.c
long syracuse(long n)
{
long count = 0L ;
while (n > 1)
{
if ((n&1)==0)
n /= 2;
else
n = 3*n+1;
count++;
}
return count ;
}
Writing syrac.c
Build the shared library
%%bash
gcc -fPIC -shared -O3 \
-o syrac.so syrac.c
%%time
import time
from ctypes import *
syracDLL = CDLL("./syrac.so")
syracuse = syracDLL.syracuse
flights = [syracuse(i) for i in range(1,N+1)]
CPU times: user 364 ms, sys: 1 µs, total: 364 ms
Wall time: 364 ms
Ctypes with Fortran module#
If you change the fortran file you have to restart the kernel
%%file syrac.F90
module syrac_f90
use iso_c_binding
implicit none
contains
function f_syrac(n) bind(c, name='c_syrac') result(f)
integer(c_long) :: f
integer(c_long), intent(in), value :: n
integer(c_long) :: x
x = n
f = 0_8
do while(x>1)
if (iand(x,1_8) == 0) then
x = x / 2
else
x = 3*x+1
end if
f = f + 1_8
end do
end function f_syrac
end module syrac_f90
Writing syrac.F90
%%bash
rm -f *.o *.so *.dylib
gfortran -fPIC -shared -O3 -o syrac.dylib syrac.F90
from ctypes import *
syrac_f90 = CDLL('./syrac.dylib')
syrac_f90.c_syrac.restype = c_long
syrac_f90.c_syrac(1000)
111
%%time
N = 1000000
flights = [syrac_f90.c_syrac(i) for i in range(1,N+1)]
CPU times: user 397 ms, sys: 0 ns, total: 397 ms
Wall time: 397 ms
Faster than pure Python
We can call function from DLL windows libraries.
Unfortunately you need to adapt the syntax to the operating system.
SWIG#
Interface file syrac.i for C function in syrac.c
%%file syrac.i
%module syracuseC
%{
extern long syracuse(long n);
%}
extern long syracuse(long n);
Writing syrac.i
%%bash
swig -python syrac.i
Build the python module#
Using command line
swig -python syrac.i
gcc `python3-config --cflags` -fPIC \
-shared -O3 -o _syracuseC.so syrac_wrap.c syrac.c `python3-config --ldflags`
With distutils
%%file setup.py
from numpy.distutils.core import Extension, setup
module_swig = Extension('_syracuseC', sources=['syrac_wrap.c', 'syrac.c'])
setup( name='Syracuse',
version = '0.1.0',
author = "Pierre Navaro",
description = """Simple C Fortran interface example """,
ext_modules = [module_swig],
)
Writing setup.py
import sys, os
if sys.platform == "darwin":
os.environ["CC"] = "gcc-10"
!{sys.executable} setup.py build_ext --inplace --quiet
/home/runner/work/python-fortran/python-fortran/notebooks/setup.py:1: DeprecationWarning:
`numpy.distutils` is deprecated since NumPy 1.23.0, as a result
of the deprecation of `distutils` itself. It will be removed for
Python >= 3.12. For older Python versions it will remain present.
It is recommended to use `setuptools < 60.0` for those Python versions.
For more details, see:
https://numpy.org/devdocs/reference/distutils_status_migration.html
from numpy.distutils.core import Extension, setup
running build_ext
running build_src
INFO: build_src
INFO: building extension "_syracuseC" sources
INFO: build_src: building npy-pkg config files
/usr/share/miniconda/envs/python-fortran/lib/python3.9/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.
!!
********************************************************************************
Please avoid running ``setup.py`` directly.
Instead, use pypa/build, pypa/installer or other
standards-based tools.
See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
********************************************************************************
!!
self.initialize_options()
INFO: customize UnixCCompiler
INFO: customize UnixCCompiler using build_ext
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-march=native)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
creating /tmp/tmp8i1jwcjd/usr/share/miniconda/envs/python-fortran/lib/python3.9/site-packages/numpy/distutils/checks
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-march=native'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-O3)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-O3'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-Werror)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-Werror'
INFO: CCompilerOpt.__init__[1795] : check requested baseline
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-msse)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-msse2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse2'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'SSE' with flags (-msse -msse2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'SSE2' with flags (-msse -msse2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-msse3)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse3'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'SSE3' with flags (-msse -msse2 -msse3)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -Werror'
INFO: CCompilerOpt.__init__[1804] : check requested dispatch-able features
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mssse3)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mssse3'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-msse4.1)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse4.1'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'SSE41' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mpopcnt)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mpopcnt'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'POPCNT' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-msse4.2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse4.2'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'SSE42' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'SSSE3' with flags (-msse -msse2 -msse3 -mssse3)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mf16c)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mf16c'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'F16C' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx2'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX2' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mavx2)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mavx2 -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mfma)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mfma'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512f -mno-mmx)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512f -mno-mmx'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512cd)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512cd'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512vl -mavx512bw -mavx512dq)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512vl -mavx512bw -mavx512dq'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512vnni)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512vnni'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_CLX' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512vnni)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512vnni -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512F' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512ifma -mavx512vbmi)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512ifma -mavx512vbmi'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdq)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdq'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_ICL' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512vnni -mavx512ifma -mavx512vbmi -mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdq)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512vnni -mavx512ifma -mavx512vbmi -mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdq -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_CNL' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512ifma -mavx512vbmi)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512ifma -mavx512vbmi -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'FMA3' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_SKX' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -Werror'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512CD' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512fp16)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512fp16'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_SPR' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512vnni -mavx512ifma -mavx512vbmi -mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdq -mavx512fp16)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -mavx512vnni -mavx512ifma -mavx512vbmi -mavx512vbmi2 -mavx512bitalg -mavx512vpopcntdq -mavx512fp16 -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx512er -mavx512pf)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx512er -mavx512pf'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_KNL' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512er -mavx512pf)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512er -mavx512pf -Werror'
INFO: CCompilerOpt.cc_test_flags[1086] : testing flags (-mavx5124fmaps -mavx5124vnniw -mavx512vpopcntdq)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-mavx5124fmaps -mavx5124vnniw -mavx512vpopcntdq'
INFO: CCompilerOpt.feature_test[1560] : testing feature 'AVX512_KNM' with flags (-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512er -mavx512pf -mavx5124fmaps -mavx5124vnniw -mavx512vpopcntdq)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512er -mavx512pf -mavx5124fmaps -mavx5124vnniw -mavx512vpopcntdq -Werror'
INFO: CCompilerOpt.__init__[1816] : skip features (SSE2 SSE SSE3) since its part of baseline
INFO: CCompilerOpt.__init__[1820] : initialize targets groups
INFO: CCompilerOpt.__init__[1822] : parse target group simd_test
INFO: CCompilerOpt._parse_target_tokens[2033] : skip targets (VXE2 XOP NEON VXE VX VSX3 FMA4 VSX VSX4 VSX2 ASIMD) not part of baseline or dispatch-able features
INFO: CCompilerOpt._parse_policy_not_keepbase[2145] : skip baseline features (SSE2)
INFO: CCompilerOpt.generate_dispatch_header[2366] : generate CPU dispatch header: (build/src.linux-x86_64-3.9/numpy/distutils/include/npy_cpu_dispatch_config.h)
WARN: CCompilerOpt.generate_dispatch_header[2375] : dispatch header dir build/src.linux-x86_64-3.9/numpy/distutils/include does not exist, creating it
INFO: CCompilerOpt.feature_extra_checks[1640] : Testing extra checks for feature 'AVX512F' (AVX512F_REDUCE)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -Werror'
INFO: CCompilerOpt.feature_extra_checks[1640] : Testing extra checks for feature 'AVX512_SKX' (AVX512BW_MASK AVX512DQ_MASK)
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -Werror'
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3 -mssse3 -msse4.1 -mpopcnt -msse4.2 -mavx -mf16c -mfma -mavx2 -mavx512f -mno-mmx -mavx512cd -mavx512vl -mavx512bw -mavx512dq -Werror'
INFO: building '_syracuseC' extension
INFO: compiling C sources
INFO: C compiler: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC -O2 -isystem /usr/share/miniconda/envs/python-fortran/include -fPIC
INFO: compile options: '-I/usr/share/miniconda/envs/python-fortran/lib/python3.9/site-packages/numpy/_core/include -Ibuild/src.linux-x86_64-3.9/numpy/distutils/include -I/usr/share/miniconda/envs/python-fortran/include/python3.9 -c'
extra options: '-msse -msse2 -msse3'
INFO: gcc: syrac_wrap.c
INFO: gcc: syrac.c
INFO: gcc -pthread -B /usr/share/miniconda/envs/python-fortran/compiler_compat -shared -Wl,--allow-shlib-undefined -Wl,-rpath,/usr/share/miniconda/envs/python-fortran/lib -Wl,-rpath-link,/usr/share/miniconda/envs/python-fortran/lib -L/usr/share/miniconda/envs/python-fortran/lib -Wl,--allow-shlib-undefined -Wl,-rpath,/usr/share/miniconda/envs/python-fortran/lib -Wl,-rpath-link,/usr/share/miniconda/envs/python-fortran/lib -L/usr/share/miniconda/envs/python-fortran/lib build/temp.linux-x86_64-cpython-39/syrac_wrap.o build/temp.linux-x86_64-cpython-39/syrac.o -o _syracuseC.cpython-39-x86_64-linux-gnu.so
INFO:
########### EXT COMPILER OPTIMIZATION ###########
INFO: Platform :
Architecture: x64
Compiler : gcc
CPU baseline :
Requested : 'min'
Enabled : SSE SSE2 SSE3
Flags : -msse -msse2 -msse3
Extra checks: none
CPU dispatch :
Requested : 'max -xop -fma4'
Enabled : SSSE3 SSE41 POPCNT SSE42 AVX F16C FMA3 AVX2 AVX512F AVX512CD AVX512_KNL AVX512_KNM AVX512_SKX AVX512_CLX AVX512_CNL AVX512_ICL AVX512_SPR
Generated : none
INFO: CCompilerOpt.cache_flush[864] : write cache to path -> /home/runner/work/python-fortran/python-fortran/notebooks/build/temp.linux-x86_64-cpython-39/ccompiler_opt_cache_ext.py
import _syracuseC
syracuse = _syracuseC.syracuse
syracuse(1000)
111
%%time
N=1000000
flights = [syracuse(i) for i in range(1,N+1)]
CPU times: user 206 ms, sys: 17 µs, total: 206 ms
Wall time: 206 ms
References#
Python Scripting for Computational Science de Hans Petter Langtangen chez Springer