Dependecies:
- numpy
- gfortran
The function vectormt
contain fortran code and openmp calls and will be compiled with gfortran.
A similar compilations could be done using hpc-sdk or OpenMP (targets) for GPU support.
File: vectormt.f90
subroutine vectormt (c,n)
USE OMP_LIB
implicit none
integer, intent(in) :: n
real*8, intent(out), dimension(n) :: c
PRINT *, "Hello max threads :", OMP_GET_MAX_THREADS()
PRINT *, "Hello from process:", OMP_GET_THREAD_NUM()
PRINT *, "vectormt.f90: First element of vector C ", c(1)
end subroutine vectormt
The helper function simple
is just an intermediary between python and vectormt
and will be compiled by f2py,
this is useful when vectormt
is too complex to be compiled by f2py, or when the fortran needed by vectormt
is not
supported by nummpy.f2py
.
File: simple.f90
implicit none
real*8, allocatable :: c(:)
real*8 :: alpha
integer :: n
integer :: i
n = 1000
allocate(c(n))
do i = 1, n
c(i) = 1.0
enddo
print *, "simple.f90: First element of vector C ", c(1)
call vectormt (c,n)
if (allocated(c)) then
deallocate(c)
end if
end subroutine simple
The follow is just a Makefile that summarizes the steps needed to compile the both simple.f90
and vectormt.f90
,
this will produce the objects, libraries and python modules for this example.
File: Makefile
simpler.so: simple.f90
f2py -c --fcompiler=gnu95 -m simpler simple.f90 -L. -lvectormt -lgomp
ln -s simpler.cpython-36m-x86_64-linux-gnu.so simpler.so
libvectormt.so: vectormt.o
ld -o libvectormt.so.1 -dy -G -h libvectormt.so.1 vectormt.o
ln -s libvectormt.so.1 libvectormt.so
vectormt.o: vectormt.f90
gfortran -shared -fPIC -c -o vectormt.o vectormt.f90
clean:
rm *.o *.so
testpython: simpler.so
python -c "import simpler;simpler.simple()"
With all this files in place the compilations is done calling make simpler.so
or with the test case make testpython
.
This is should be the output:
simple.f90: First element of vector C : 1
Hello max threads : 3
Hello from process: 0
vectormt.f90: First element of vector C : 1
NOTES:
In case you get :
ImportError: libvectormt.so.1: cannot open shared object file: No such file or directory
just activate the library to your LD_LIBRARY_PATH path as:
export LD_LIBRARY_PATH=
pwd:$LD_LIBRARY_PATH