Search code examples
pythonnumpyparallel-processingfortranparallel-python

Execute Fortran subroutine through parallel Python impossible to execute


I am trying to execute a Fortran subroutine in Python 2.7 using parallel python (PP package). But when I execute it using pp.server().submit(...) nothing happens. I might have implemented it wrong, I have used the numpy.f2py.compile() as explained here. Is this correct? And if not, what should I change?

Just to mention that the code is almost surely correct given that it is taken from a doctorate thesis (code, paper).

The subroutine implemented in a Python 2.7 module called "design_operation" is:

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)

Then, I call it this way:

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

which is actually in a loop and should be executed in:

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

I always get the following error:

zero-dimensional arrays cannot be concatenated.

Therefore I suppose that the error comes from the incorrect execution of the tasks, but perhaps am I mistaking.

The stack error is:

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: I supposed there is some unnecessary code and therefore I didn't include it for more clarity + the arguments of func= list_append are correct.


Solution

  • I notice your code doesn't seem to pass the K, L, M and C variables to the Fortran routine. However, K, L, M are used to dimension some arrays and are also used as loop counters. It's quite possible that these values are set to a default value of 0 by the compiler, or maybe more likely as None by Python itself. That would explain your error message `ValueError: zero-dimensional arrays cannot be concatenated'.