将函数参数间隔拆分为单独部分时出错。并行Python

将函数参数间隔拆分为单独部分时出错。并行Python,python,eof,parallel-python,Python,Eof,Parallel Python,首先,我为所有的代码感到抱歉。我很难制定一个具体的问题。我已经修补了一段时间,只是无法让它工作。这个错误消息没有太大帮助。如果能给我一点帮助,我将不胜感激 我想为不同的参数范围并行执行函数“EMatCreatorrx(umin、umax、vmin、vmax、stepsize、pstep、alphamax、A、rot):”的三个实例,以加快计算速度 umin、umax和vmin、vmax都定义了分别在u和v方向上计算的值范围。我想通过将u范围分成三个较小的范围来划分函数 我已经通过运行以下程序演示

首先,我为所有的代码感到抱歉。我很难制定一个具体的问题。我已经修补了一段时间,只是无法让它工作。这个错误消息没有太大帮助。如果能给我一点帮助,我将不胜感激

我想为不同的参数范围并行执行函数“EMatCreatorrx(umin、umax、vmin、vmax、stepsize、pstep、alphamax、A、rot):”的三个实例,以加快计算速度

umin、umax和vmin、vmax都定义了分别在u和v方向上计算的值范围。我想通过将u范围分成三个较小的范围来划分函数

我已经通过运行以下程序演示了将函数拼接成三个独立部分然后重新连接的代码的正确性:

import pp
import sys

import numpy as N
import scipy as sp
from scipy import integrate as Int
from scipy import special as S
import math

cos=sp.cos
sin=sp.sin
exp=sp.exp
sqrt=sp.sqrt
j=S.jn
pi=N.pi
floor=N.floor

def EMatCreatorrx(umin,umax,vmin,vmax,stepsize,pstep,alphamax,A,rot): #pstep needs to be pi/n for n=0,1,2,3,... This is so there will be an odd number of samples when calculating the PSF. This is necessary to increase the accuracy of Simpson's method.   
    mnum=N.int(((umax-umin)/stepsize)+1)
    nnum=N.int(((vmax-vmin)/stepsize)+1)
    pnum=N.int((2*pi/pstep)+1)
    gridshape=(mnum,nnum,pnum)
    I0=N.zeros(gridshape,dtype=complex)
    I1=N.zeros(gridshape,dtype=complex)
    I2=N.zeros(gridshape,dtype=complex)
    E01=N.zeros(gridshape,dtype=complex) 
    E02=N.zeros(gridshape,dtype=complex) 
    E03=N.zeros(gridshape,dtype=complex)
    for m,u in enumerate(N.linspace(umin,umax,num=mnum)):
        for n,v in enumerate(N.linspace(vmin,vmax,num=nnum)):
            for i,p in enumerate(N.linspace(0,2*pi,num=pnum)):             
                vp = sqrt((v*cos(p))**2 + (v*sin(p)*cos(rot)-u*sin(rot))**2)
                up = (v*cos(p)*sin(rot) + u*cos(rot))
                pp= N.arctan2(v*sin(p)*cos(rot) - u*sin(rot), v*cos(p))  
                I0real=lambda theta: N.real((sqrt(cos(theta))*sin(theta))*(1+cos(theta))*  (j(0,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I0imaginary=lambda theta: N.imag((sqrt(cos(theta))*sin(theta))*(1+cos(theta))*(j(0,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I0real_integral= Int.quad(I0real, 0, alphamax)
                I0imag_integral= Int.quad(I0imaginary, 0, alphamax)
                I1real=lambda theta: N.real(sqrt(cos(theta))*((sin(theta))**2)*j(1,vp*sin(theta)/(sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I1imaginary=lambda theta: N.imag(sqrt(cos(theta))*((sin(theta))**2)*j(1,vp*sin(theta)/(sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I1real_integral= Int.quad(I1real, 0, alphamax)
                I1imag_integral= Int.quad(I1imaginary, 0, alphamax)
                I2real=lambda theta: N.real((sqrt(cos(theta))*sin(theta))*(1-cos(theta))*(j(2,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I2imaginary=lambda theta: N.imag((sqrt(cos(theta))*sin(theta))*(1-cos(theta))*(j(2,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))   
                I2real_integral= Int.quad(I2real, 0, alphamax)
                I2imag_integral= Int.quad(I2imaginary, 0, alphamax)
                I0.real[m,n,i]=I0real_integral[0]
                I0.imag[m,n,i]=I0imag_integral[0]
                I1.real[m,n,i]=I1real_integral[0]
                I1.imag[m,n,i]=I1imag_integral[0]
                I2.real[m,n,i]=I2real_integral[0]
                I2.imag[m,n,i]=I2imag_integral[0]
                E01[m,n,i]=-A*(1j*(I0[m,n,i]+I2[m,n,i]*cos(2*pp)))
                E02[m,n,i]=-A*(cos(rot)*1j*I2[m,n,i]*sin(2*pp) - sin(rot)*2*I1[m,n,i]*cos(pp))
                E03[m,n,i]=-A*(sin(rot)*1j*I2[m,n,i]*sin(2*pp) + cos(rot)*2*I1[m,n,i]*cos(pp))
    return E01,E02,E03,pstep


#EMatCreatorrx(umin,umax,vmin,vmax,stepsize,pstep,alphamax,A,rot): #Enter parameters below in tuple "params."

params=-2,2,-2,2,.2,N.pi/10,1,1,0
##############################################################################################################

mnum=N.int(((params[1]-params[0])/params[4])+1)
nnum=N.int(((params[3]-params[2])/params[4])+1)
pnum=N.int((2*N.pi/params[5])+1)
phold=N.zeros((mnum,nnum,pnum), dtype=complex),N.zeros((mnum,nnum,pnum), dtype=complex),N.zeros((mnum,nnum,pnum), dtype=complex), params[5]

uindarr = [(m, u) for m,u in enumerate(N.linspace(params[0],params[1],num=mnum))]

ind_end1=floor(len(uindarr)/3)
spa_end1=uindarr[int(ind_end1)][1]

ind_beg2=ind_end1+1
spa_beg2=uindarr[int(ind_beg2)][1]
ind_end2=2*floor(len(uindarr)/3)
spa_end2=uindarr[int(ind_end2)][1]

ind_beg3=ind_end2+1
spa_beg3=uindarr[int(ind_beg3)][1]

job1 = EMatCreatorrx(params[0],spa_end1,params[2],params[3],params[4],params[5],params[6],params[7],params[8]) 
job2 = EMatCreatorrx(spa_beg2,spa_end2,params[2],params[3],params[4],params[5],params[6],params[7],params[8])
job3 = EMatCreatorrx(spa_beg3,params[1],params[2],params[3],params[4],params[5],params[6],params[7],params[8])

phold[0][:ind_end1+1,:,:]=job1[0]
phold[0][ind_beg2:ind_end2+1,:,:]=job2[0]
phold[0][ind_beg3:,:,:]=job3[0]

phold[1][:ind_end1+1,:,:]=job1[1]
phold[1][ind_beg2:ind_end2+1,:,:]=job2[1]
phold[1][ind_beg3:,:,:]=job3[1]

phold[2][:ind_end1+1,:,:]=job1[2]
phold[2][ind_beg2:ind_end2+1,:,:]=job2[2]
phold[2][ind_beg3:,:,:]=job3[2]
工作完成后,我尝试实现并行python,以便使用以下代码并行计算三个切片:

import pp
import sys

import numpy as N
import scipy as sp
from scipy import integrate as Int
from scipy import special as S
import math

cos=sp.cos
sin=sp.sin
exp=sp.exp
sqrt=sp.sqrt
j=S.jn
pi=sp.pi
floor=N.floor

def EMatCreatorrx(umin,umax,vmin,vmax,stepsize,pstep,alphamax,A,rot): #pstep needs to be pi/n for n=0,1,2,3,... This is so there will be an odd number of samples when calculating the PSF. This is necessary to increase the accuracy of Simpson's method.   
    cos=sp.cos
    sin=sp.sin
    exp=sp.exp
    sqrt=sp.sqrt
    j=S.jn
    pi=sp.pi  
    mnum=N.int(((umax-umin)/stepsize)+1)
    nnum=N.int(((vmax-vmin)/stepsize)+1)
    pnum=N.int((2*pi/pstep)+1)
    gridshape=(mnum,nnum,pnum)
    I0=N.zeros(gridshape,dtype=complex)
    I1=N.zeros(gridshape,dtype=complex)
    I2=N.zeros(gridshape,dtype=complex)
    E01=N.zeros(gridshape,dtype=complex) 
    E02=N.zeros(gridshape,dtype=complex) 
    E03=N.zeros(gridshape,dtype=complex)
    for m,u in enumerate(N.linspace(umin,umax,num=mnum)):
        for n,v in enumerate(N.linspace(vmin,vmax,num=nnum)):
            for i,p in enumerate(N.linspace(0,2*pi,num=pnum)):             
                vp = sqrt((v*cos(p))**2 + (v*sin(p)*cos(rot)-u*sin(rot))**2)
                up = (v*cos(p)*sin(rot) + u*cos(rot))
                pp= N.arctan2(v*sin(p)*cos(rot) - u*sin(rot), v*cos(p))  
                I0real=lambda theta: N.real((sqrt(cos(theta))*sin(theta))*(1+cos(theta))*(j(0,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I0imaginary=lambda theta: N.imag((sqrt(cos(theta))*sin(theta))*(1+cos(theta))*(j(0,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I0real_integral= Int.quad(I0real, 0, alphamax)
                I0imag_integral= Int.quad(I0imaginary, 0, alphamax)
                I1real=lambda theta: N.real(sqrt(cos(theta))*((sin(theta))**2)*j(1,vp*sin(theta)/(sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I1imaginary=lambda theta: N.imag(sqrt(cos(theta))*((sin(theta))**2)*j(1,vp*sin(theta)/(sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I1real_integral= Int.quad(I1real, 0, alphamax)
                I1imag_integral= Int.quad(I1imaginary, 0, alphamax)
                I2real=lambda theta: N.real((sqrt(cos(theta))*sin(theta))*(1-cos(theta))*(j(2,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))
                I2imaginary=lambda theta: N.imag((sqrt(cos(theta))*sin(theta))*(1-cos(theta))*(j(2,vp*sin(theta)/sin(alphamax)))*exp((1j*up*cos(theta))/((sin(alphamax))**2)))   
                I2real_integral= Int.quad(I2real, 0, alphamax)
                I2imag_integral= Int.quad(I2imaginary, 0, alphamax)
                I0.real[m,n,i]=I0real_integral[0]
                I0.imag[m,n,i]=I0imag_integral[0]
                I1.real[m,n,i]=I1real_integral[0]
                I1.imag[m,n,i]=I1imag_integral[0]
                I2.real[m,n,i]=I2real_integral[0]
                I2.imag[m,n,i]=I2imag_integral[0]
                E01[m,n,i]=-A*(1j*(I0[m,n,i]+I2[m,n,i]*cos(2*pp)))
                E02[m,n,i]=-A*(cos(rot)*1j*I2[m,n,i]*sin(2*pp) - sin(rot)*2*I1[m,n,i]*cos(pp))
                E03[m,n,i]=-A*(sin(rot)*1j*I2[m,n,i]*sin(2*pp) + cos(rot)*2*I1[m,n,i]*cos(pp))
    return E01,E02,E03,pstep






#EMatCreatorrx(umin,umax,vmin,vmax,stepsize,pstep,alphamax,A,rot): #Enter parameters below in tuple "params."

params=-2,2,-2,2,.2,N.pi/10,1,1,0
##############################################################################################################

mnum=N.int(((params[1]-params[0])/params[4])+1)
nnum=N.int(((params[3]-params[2])/params[4])+1)
pnum=N.int((2*N.pi/params[5])+1)
phold=N.zeros((mnum,nnum,pnum), dtype=complex),N.zeros((mnum,nnum,pnum), dtype=complex),N.zeros((mnum,nnum,pnum), dtype=complex), params[5]

uindarr = [(m, u) for m,u in enumerate(N.linspace(params[0],params[1],num=mnum))]

ind_end1=floor(len(uindarr)/3)
spa_end1=uindarr[int(ind_end1)][1]

ind_beg2=ind_end1+1
spa_beg2=uindarr[int(ind_beg2)][1]
ind_end2=2*floor(len(uindarr)/3)
spa_end2=uindarr[int(ind_end2)][1]

ind_beg3=ind_end2+1
spa_beg3=uindarr[int(ind_beg3)][1]

ppservers = ()
job_server = pp.Server()
fn = pp.Template(job_server, EMatCreatorrx, (), ("scipy as sp", "numpy as N", "scipy.special as S", "scipy.integrate as Int",))
job1 = fn.submit(params[0],spa_end1,params[2],params[3],params[4],params[5],params[6],params[7],params[8]) 
job2 = fn.submit(spa_beg2,spa_end2,params[2],params[3],params[4],params[5],params[6],params[7],params[8])
job3 = fn.submit(spa_beg3,params[1],params[2],params[3],params[4],params[5],params[6],params[7],params[8])

phold[0][:ind_end1+1,:,:]=job1[0]
phold[0][ind_beg2:ind_end2+1,:,:]=job2[0]
phold[0][ind_beg3:,:,:]=job3[0]

phold[1][:ind_end1+1,:,:]=job1[1]
phold[1][ind_beg2:ind_end2+1,:,:]=job2[1]
phold[1][ind_beg3:,:,:]=job3[1]

phold[2][:ind_end1+1,:,:]=job1[2]
phold[2][ind_beg2:ind_end2+1,:,:]=job2[2]
phold[2][ind_beg3:,:,:]=job3[2]


print "computation complete"
当我尝试运行上述代码时,会出现以下文件结尾错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\tph89\Desktop\Python Path\parallel python\parallel2.py", line 104, in <module>
    job1 = fn.submit((params[0],spa_end1,params[2],params[3],params[4],params[5],params[6],params[7],params[8])) 
  File "C:\Users\tph89\Desktop\Python Path\parallel python\pp.py", line 270, in submit
    self.group, self.globals)
  File "C:\Users\tph89\Desktop\Python Path\parallel python\pp.py", line 459, in submit
    sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)
  File "C:\Users\tph89\Desktop\Python Path\parallel python\pp.py", line 637, in __dumpsfunc
    sources = [self.__get_source(func) for func in funcs]
  File "C:\Users\tph89\Desktop\Python Path\parallel python\pp.py", line 704, in __get_source
    sourcelines = inspect.getsourcelines(func)[0]
  File "C:\Users\tph89\Python27\lib\inspect.py", line 693, in getsourcelines
    else: return getblock(lines[lnum:]), lnum + 1
  File "C:\Users\tph89\Python27\lib\inspect.py", line 677, in getblock
    tokenize.tokenize(iter(lines).next, blockfinder.tokeneater)
  File "C:\Users\tph89\Python27\lib\tokenize.py", line 169, in tokenize
    tokenize_loop(readline, tokeneater)
  File "C:\Users\tph89\Python27\lib\tokenize.py", line 175, in tokenize_loop
    for token_info in generate_tokens(readline):
  File "C:\Users\tph89\Python27\lib\tokenize.py", line 296, in generate_tokens
    raise TokenError, ("EOF in multi-line string", strstart)
tokenize.TokenError: ('EOF in multi-line string', (2, 0))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Users\tph89\Desktop\Python Path\parallel Python\parallel2.py”,第104行,在
job1=fn.submit((参数[0],spa_end1,参数[2],参数[3],参数[4],参数[5],参数[6],参数[7],参数[8]))
文件“C:\Users\tph89\Desktop\Python Path\parallel Python\pp.py”,第270行,提交
self.group、self.globals)
文件“C:\Users\tph89\Desktop\Python Path\parallel Python\pp.py”,第459行,提交
sfunc=self.\u dumpsfunc((func,)+depfuncs,modules)
文件“C:\Users\tph89\Desktop\Python Path\parallel Python\pp.py”,第637行,在\uu dumpsfunc中
sources=[self.\uuuu获取\u func中func的源(func)]
文件“C:\Users\tph89\Desktop\Python Path\parallel Python\pp.py”,第704行,在\uuuu get\u source中
sourcelines=inspect.getsourcelines(func)[0]
文件“C:\Users\tph89\Python27\lib\inspect.py”,第693行,位于getsourcelines中
else:返回getblock(行[lnum:]),lnum+1
文件“C:\Users\tph89\Python27\lib\inspect.py”,第677行,在getblock中
tokenize.tokenize(iter(行).next,blockfinder.tokeneater)
文件“C:\Users\tph89\Python27\lib\tokenize.py”,第169行,在tokenize中
标记化_循环(readline、tokeneater)
文件“C:\Users\tph89\Python27\lib\tokenize.py”,第175行,在tokenize\u循环中
对于生成令牌中的令牌信息(readline):
文件“C:\Users\tph89\Python27\lib\tokenize.py”,第296行,在generate\u tokens中
引发令牌错误(“多行字符串中的EOF”,strstart)
tokenize.TokenError:(“多行字符串中的EOF”(2,0))

在这一点上,我有点不知所措。你们中有人以前遇到过这个错误吗?如果是,问题是什么?非常感谢您的任何意见。谢谢

我在使用并行Python时也遇到了这个问题,这也是由于标记化错误造成的。在我的例子中,我的一个助手函数具有双缩进(由于复制粘贴现象,八个空格,而函数声明处于零空格的正确缩进级别)-Spyder的主解释器刚刚处理了这个问题,但tokenize显然没有


在OP的情况下:根据上面代码的格式,以及此处顶部的注释,可能发生错误,因为空行上没有空格;这将返回“”注释中称其为EOF。

此处的代码太多。该错误通常意味着您有一个未关闭的