网站建设好么,医疗网站备案要怎么做 需要准备什么材料,无限容量网站,郑州营销型网站建设工作室前言Python是机器学习领域不断增长的通用语言。拥有一些非常棒的工具包#xff0c;比如scikit-learn#xff0c;tensorflow和pytorch。气候模式通常是使用Fortran实现的。那么我们应该将基于Python的机器学习迁移到Fortran模型中吗#xff1f;数据科学领域可能会利用HTTP AP…前言Python是机器学习领域不断增长的通用语言。拥有一些非常棒的工具包比如scikit-learntensorflow和pytorch。气候模式通常是使用Fortran实现的。那么我们应该将基于Python的机器学习迁移到Fortran模型中吗数据科学领域可能会利用HTTP API(比如Flask)封装机器学习方法但是HTTP在紧密耦合的系统(比如气候模式)中效率太低。因此可以选择直接从Fortran中调用Python直接通过RAM传递气候模式的状态而不是通过高延迟的通信层比如HTTP。1、实现方法有很多方法可以实现通过Python调用Fortran但是从Fortran调用Python的方法却很少。从Fortran调用Python可以看作是将Python代码嵌入到Fortran但是Python的设计并不是像嵌入式语言Lua。可以通过以下三种方法实现从Fortran调用PythonPython的C语言API。这是最常用的方式但需要实现大量的C封装代码。基于Cython。Cython用于从Python中调用C语言但也可以实现从C调用Python。基于CFFI。CFFI提供了非常方便的方法可以嵌入Python代码。无论选择哪种方法用户都需要将可执行Fortran文件链接到系统Python库比如通过添加-lpython3.6到Fortran模式的Makefile文件。2、实例演示下面通过Hello World示例演示如何通过Fortran调用Python。Fortran代码保存在test.f90文件如下! test.f90
program call_pythonuse, intrinsic :: iso_c_bindingimplicit noneinterfacesubroutine hello_world() bind (c)end subroutine hello_worldend interfacecall hello_world()end program call_python首先导入Fortran 2003内部定义的和C语言类型互通的模块iso_c_binding。但使用CFFI时我们不需要写任何C代码CFFI会生成C类型的打包接口。下一行则定义了一个C函数hello_world接口这可以在C语言中实现但是这里我们使用Python和CFFI。最后调用hello_world。为了使用hello_world我们需要构建CFFI标注并保存在builder.py中此代码用于创建可以链接Fortran程序的动态库import cffi
ffibuilder cffi.FFI()header
extern void hello_world(void);
module
from my_plugin import ffi
import numpy as npffi.def_extern()
def hello_world():print(Hello World!)
with open(plugin.h, w) as f:f.write(header)ffibuilder.embedding_api(header)
ffibuilder.set_source(my_plugin, r#include plugin.h
)ffibuilder.embedding_init_code(module)
ffibuilder.compile(targetlibplugin.dylib, verboseTrue)首先我们导入cffi包并且声明了外部函数接口(FFI)对象。这看起来似乎比较奇怪这只是CFFI实现这种目的的方式。下一步header字符串中包含了需要调用的函数接口的定义。module字符串中包含了真正需要执行的Python程序。装饰器ffi.def_extern用于标记hello_world函数。my_plugin用于获取ffi对象。def_extern装饰器用于处理C类型指针等。然后ffibuilder.embedding_api(header)定义了APIembedding_init_code定义了Python代码。看起来比较奇怪的是在字符串中定义Python代码但CFFI需要以这种方式将Python代码构建为共享库对象。ffibuilder.set_source来设置源代码信息。然后执行以下语句创建共享库libplugin.dylibpython builder.py然后使用下列命令编译Fortran程序:gfortran -o test -L./ -lplugin test.f90以上是在Mac OSX上创建的共享库如果在Linux上共享库应该以.so结尾。如果一切没有问题那么就可以执行文件了:3、常见的一些问题1必须将所有Python代码写入header字符串吗不需要这样。你可以直接在不同的Python模块中定义Python代码(比如my_module.py)然后在module字符串的开头导入即可。比如builder.py可以改为如下形式...module
from my_plugin import ffi
import my_moduleffi.def_extern()
def hello_world():my_module.some_function()
...这将在Python中使用可导入的形式使用Python程序。在添加到Fortran中之前你也可以通过python -c import my_module测试一下。如果失败了你可能需要将包含my_module模块的路径添加到Python的sys.path变量中。2如何传递Fortran数组给Python下面是一个示例将代码定义在一个模块文件中比如my_module.py# my_module.py# Create the dictionary mapping ctypes to np dtypes.
ctype2dtype {}# Integer types
for prefix in (int, uint):for log_bytes in range(4):ctype %s%d_t % (prefix, 8 * (2**log_bytes))dtype %s%d % (prefix[0], 2**log_bytes)# print( ctype )# print( dtype )ctype2dtype[ctype] np.dtype(dtype)# Floating point types
ctype2dtype[float] np.dtype(f4)
ctype2dtype[double] np.dtype(f8)def asarray(ffi, ptr, shape, **kwargs):length np.prod(shape)# Get the canonical C type of the elements of ptr as a string.T ffi.getctype(ffi.typeof(ptr).item)# print( T )# print( ffi.sizeof( T ) )if T not in ctype2dtype:raise RuntimeError(Cannot create an array for element type: %s % T)a np.frombuffer(ffi.buffer(ptr, length * ffi.sizeof(T)), ctype2dtype[T])\.reshape(shape, **kwargs)return aasarray函数使用CFFI的ffi对象转换指针ptr为给定形状的numpy数组。可以使用如下形式在builder.py中的module字符串中调用module
import my_moduleffi.def_extern()
def add_one(a_ptr)a my_module.asarray(a)a[:] 1
add_one也可以定义在my_module.py中。最后我们需要定义与函数相关的头文件信息并且添加到builder.py的header字符串中header
extern void add_one (double *);
最后在Fortran中以如下形式调用program call_pythonuse, intrinsic :: iso_c_bindingimplicit noneinterfacesubroutine add_one(x_c, n) bind (c)use iso_c_bindinginteger(c_int) :: nreal(c_double) :: x_c(n)end subroutine add_oneend interfacereal(c_double) :: x(10)print *, xcall add_one(x, size(x))print *, xend program call_python这一部分我们介绍了如何在Fortran中嵌入Python代码块以及如何传递数组给Fortran或从Fortran传递数组给Python。然后有些方面还是不太方便。3必须要在三个不同的区域定义python函数签名吗任何要传递给Fortran的Python函数都必须要要在三个区域进行定义。首先必须在header.h中进行C头文件声明然后执行函数必须要在builder.py的module字符串中或一个外部模块中最后Fortran代码中必须包含定义子程序的interface块(接口块)这对于改变Python函数来说就显得有些麻烦。比如我们写了一个Python函数defcompute_precipitation(T,Q):...必须要在所有区域进行声明。如果我们想添加一个垂直涡度W作为输入参数我们必须要修改builder.py以及调用Fortran的程序。显而易见对于大的工程来说这就变得极为麻烦。对于一般通信而言采用了一小部分fortran/python代码封装器。主要依赖于一个外部python模块# module.py
import impSTATE {}def get(key, c_ptr, shape, ffi):Copy the numpy array stored in STATE[key] to a pointer# wrap pointer in numpy arrayfortran_arr asarray(ffi, c_ptr, shape)# update the numpy array in placefortran_arr[:] STATE[key]def set(key, c_ptr, shape, ffi):Call pythonSTATE[key] asarray(ffi, c_ptr, shape).copy()def call_function(module_name, function_name):# import the python moduleimport importlibmod importlib.import_module(module_name)# the function we want to callfun getattr(mod, function_name)# call the function# this function can edit STATE inplacefun(STATE)全局变量STATE是一个包含了函数需要的所有数据的Python字典。get和set函数的功能主要就是将Fortran数组传递给STATA或者从STATE中取出Fortran数组。如果这些函数使用了Fortran/CFFI封装器那么可以使用如下方式从Fortran中调用Python函数cumulus.compute_precipitation(state_dict)call set(Q, q)
call set(T, temperature)
call set(ahother_arg, 1)call call_function(cumulus, compute_precipitation)call get(prec, prec)如果需要传递更多的数据给compute_precipitation那么需要添加更多的call set命令这可能会改变Python函数的执行。我们就不需要改变builder.py中的任何代码。4、结论上面描述了如何传递Fortran数据给Python函数然后再获取计算输出。为了解决频繁更改接口的问题我们将fortran数据放到了Python模块的字典中。通过调用给定的名称来获取数据并且将计算结果也存储到相同的字段中然后Fortran代码通过索引字典中正确的关键词来获取结果。Cython中使用了类似的架构但CFFI更为方便。最重要的是从C语言中调用Cython需要导入Python.h头文件还要运行Py_initialize和init_my_cython_module函数。然而CFFI会在后台完成这些操作。