def norm(self,x):
""" norm = sum(x*x) """
nbduplicates = (self.np0*self.mp0)/(self.np*self.mp)
# computenorm is done in Fortran
self.typenorm='l2'
t0 = time()
# MPI.COMM_WORLD.Barrier()
if self.typenorm=='l2':
local_sum = computenorm(self.msk,x,self.nh)
t1 = time()
self.time['norm']+=t1-t0
self.ncalls['norm']+=1
z=MPI.COMM_WORLD.allreduce(local_sum,op=MPI.SUM)/ nbduplicates
z=sqrt(z)
t0 = time()
self.time['reduce']+=t0-t1
self.ncalls['reduce']+=1
if self.typenorm=='inf':
local_z= computemax(self.msk,x,self.nh)
t1 = time()
self.time['norm']+=t1-t0
self.ncalls['norm']+=1
z=MPI.COMM_WORLD.allreduce(local_z,op=MPI.MAX)
t0 = time()
self.time['reduce']+=t0-t1
self.ncalls['reduce']+=1
return z
python类MAX的实例源码
def mpi_max(value):
global_max = np.zeros(1, dtype='float64')
local_max = np.max(value).astype('float64')
MPI.COMM_WORLD.Reduce(local_max, global_max, op=MPI.MAX)
return global_max[0]
def set_msk_laplacian(self,prevlev=0,mskf=0):
""" set the good mask and the laplacian using previous level """
if prevlev==0:
self.msk=mskf
self.compute_A_atfinest()
else:
mskf=prevlev.msk*1.0
coef=ones((self.mv,self.nv))
finetocoarse(prevlev,self,mskf,coef)
self.msk[coef<=0.5]=0
np=self.np
mp=self.mp
myrank=self.myrank
iloc=myrank%np
jloc=(myrank//np)%mp
nh=self.nh
self.compute_A_atcoarser(prevlev,self.msk)
# # coefficient for the Jacobi iteration, A[:,:,4] is the main diagonal
# val=abs(self.A[:,:,4]).max()
# self.coef = MPI.COMM_WORLD.allreduce(val, op=MPI.MAX)
# if self.coef!=0.:
# self.coef=1./self.coef
# else:
# if self.myrank==0:
# print('matrix diagonal is zero')
# print('fix something!')
# exit()
# buffer for 'smoothertwice' the Fortran subroutine
self.yo=zeros((3,self.nv))
#----------------------------------------
def max(self, x_local):
return self._reduce(x_local, MPI.MAX)
def max_at_root(self, x_local):
return self._reduce_at_root(x_local, MPI.MAX)
def _sync_i_max(self):
sendbuf = numpy.array(self._i_max, dtype='i')
recvbuf = numpy.empty(1, dtype='i')
log_debug(logger, self._log_prefix + "Entering allreduce with maximum index %i" % (self._i_max))
self.comm.Allreduce([sendbuf, MPI.INT], [recvbuf, MPI.INT], op=MPI.MAX)
self._i_max = recvbuf[0]
log_debug(logger, self._log_prefix + "After reduce: i_max = %i" % self._i_max)