通过并行执行 Fortran 子例程 Python 无法执行

Execute Fortran subroutine through parallel Python impossible to execute

我正在尝试使用并行 python(PP 包)在 Python 2.7 中执行 Fortran 子例程。但是当我使用 pp.server().submit(...) 执行它时,什么也没有发生。我可能实施错了,我按照 here 的解释使用了 numpy.f2py.compile()。这个对吗?如果没有,我应该改变什么?

只需提及代码几乎肯定是正确的,因为它来自博士论文 (code, paper)。

在名为 "design_operation" 的 Python 2.7 模块中实现的子例程是:

import numpy.f2py
fsource = '''
subroutine matrix_op(grid_x,grid_t,eval_grid,pas,K,L,M,C)
  COMPLEX :: i=(0.0,1.0)
  INTEGER , intent(in) :: K,L,M
  REAL , intent(in) :: pas
  INTEGER :: u,v,w
  REAL , dimension(1:M) , intent(in) :: grid_x
  REAL , dimension(1:K) , intent(in) :: grid_t
  REAL , dimension(1:L) , intent(in) :: eval_grid
  COMPLEX, dimension(1:L,1:M) , intent(out) :: C

  do u=1,L
     do v=1,M
        do w=1,K
           C(u,v) = C(u,v) - i*pas*grid_t(w)*grid_x(v)*exp(-i*grid_t(w)*grid_x(v)*eval_grid(u))
        end do
     end do
  end do
  end subroutine matrix_op
  '''
numpy.f2py.compile(fsource, modulename='design_operation', verbose=0)

那么,我这样称呼它:

job_server.submit(func=list_append,
                                  args=(grid_x, grid_t, sub_eval[k,:], pas_f,),
                                  modules=('numpy as np','design_operation as fdp',)

实际上在一个循环中,应该在:

中执行
job_server = pp.Server()
thread_number = job_server.get_ncpus()
...some unimportant code ...
jobs = []
for k in range(thread_number): 
    jobs.append(job_server.submit(func=list_append,
                                  args=(grid_x, grid_t, sub_eval[k,:], pas_f,),
                                  modules=('numpy as np','design_operation as fdp',)))

for i,job in enumerate(jobs):
    if i == 0:
        dM = job() 
    else:
        dM = np.concatenate((dM, job()))

job_server.destroy()
return dM

我总是收到以下错误:

zero-dimensional arrays cannot be concatenated.

所以我想错误是由于任务执行不正确造成的,但也许我弄错了。

堆栈错误为:

An error has occured during the function execution
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\project\lib\site-packages\ppworker.py", line 90, in run
    __result = __f(*__args)
  File "<string>", line 2, in list_append
AttributeError: 'module' object has no attribute 'matrix_op'
An error has occured during the function execution
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\project\lib\site-packages\ppworker.py", line 90, in run
    __result = __f(*__args)
  File "<string>", line 2, in list_append
AttributeError: 'module' object has no attribute 'matrix_op'
Traceback (most recent call last):

  File "<ipython-input-89-6cb5b50fd813>", line 5, in <module>
    dM = np.concatenate((dM, job()))#

ValueError: zero-dimensional arrays cannot be concatenated

PS:我认为有一些不必要的代码,因此为了更清楚起见我没有包含它 + func= list_append 的参数是正确的。

我注意到您的代码似乎没有将 K, L, MC 变量传递给 Fortran 例程。但是,K, L, M 用于对一些数组进行维数,也用作循环计数器。这些值很可能被编译器设置为默认值 0,或者更有可能被 Python 本身设置为 None。这将解释您的错误消息“ValueError:无法连接零维数组”。