Created
May 24, 2021 12:49
-
-
Save yudhastyawan/dc9aba18a64823500d7dc1110380d5be to your computer and use it in GitHub Desktop.
embed Fortran in Python using Cython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module penjumlahan_interface | |
use iso_c_binding, only: c_double | |
use penjumlahan_module, only: penjumlahan | |
implicit none | |
contains | |
subroutine c_penjumlahan(a, b, c) bind(c) | |
real(c_double), intent(in) :: a, b | |
real(c_double), intent(out) :: c | |
call penjumlahan(a, b, c) | |
end subroutine c_penjumlahan | |
end module penjumlahan_interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern void c_penjumlahan(double* a, double* b, double* c); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module perkalian_interface | |
use iso_c_binding, only: c_double | |
use perkalian_module, only: perkalian | |
implicit none | |
contains | |
subroutine c_perkalian(a, b, c) bind(c) | |
real(c_double), intent(in) :: a, b | |
real(c_double), intent(out) :: c | |
call perkalian(a, b, c) | |
end subroutine c_perkalian | |
end module perkalian_interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern void c_perkalian(double* a, double* b, double* c); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module penjumlahan_module | |
implicit none | |
contains | |
subroutine penjumlahan(a, b, c) | |
double precision, intent(in) :: a, b | |
double precision, intent(out) :: c | |
c = a + b | |
end subroutine penjumlahan | |
end module penjumlahan_module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module perkalian_module | |
implicit none | |
contains | |
subroutine perkalian(a, b, c) | |
double precision, intent(in) :: a, b | |
double precision, intent(out) :: c | |
c = a * b | |
end subroutine perkalian | |
end module perkalian_module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Distutils import build_ext | |
# This line only needed if building with NumPy in Cython file. | |
from numpy import get_include | |
from os import system | |
# HOW TO RUN SETUP.PY | |
# python setup.py build_ext -inplace --plat-name=win-amd64 | |
# python setup.py install | |
def fortran_compiler(statement): | |
print(statement) | |
system(statement) | |
# compile the fortran modules without linking | |
# compile fortran modules | |
fortran_compiler('gfortran penjumlahan.f90 -c -o penjumlahan.o -O3 -fPIC') | |
fortran_compiler('gfortran perkalian.f90 -c -o perkalian.o -O3 -fPIC') | |
# compile fortran shared modules | |
fortran_compiler('gfortran c_penjumlahan.f90 -c -o c_penjumlahan.o -O3 -fPIC') | |
fortran_compiler('gfortran c_perkalian.f90 -c -o c_perkalian.o -O3 -fPIC') | |
# editable components | |
module_name = "kalkulator" | |
source_file = "source_file.pyx" | |
compiled_modules = ['penjumlahan.o', 'perkalian.o', 'c_penjumlahan.o', 'c_perkalian.o'] | |
# setup | |
ext_modules = [Extension(# module name: | |
module_name, | |
# source file: | |
[source_file], | |
# other compile args for gcc | |
extra_compile_args=['-fPIC', '-O3'], | |
# other files to link to | |
extra_link_args=compiled_modules)] | |
for e in ext_modules: | |
e.cython_directives = {'language_level': "3"} #all are Python-3 | |
setup(name = module_name, | |
cmdclass = {'build_ext': build_ext}, | |
# Needed if building with NumPy. | |
# This includes the NumPy headers when compiling. | |
include_dirs = [get_include()], | |
ext_modules = ext_modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdef extern from "c_penjumlahan.h": | |
void c_penjumlahan(double* a, double* b, double* c) | |
cdef extern from "c_perkalian.h": | |
void c_perkalian(double* a, double* b, double* c) | |
def penjumlahan(double a, double b): | |
cdef: | |
double c | |
c_penjumlahan(&a, &b, &c) | |
return c | |
def perkalian(double a, double b): | |
cdef: | |
double c | |
c_perkalian(&a, &b, &c) | |
return c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Example