def main(command, unit='s'): with timeme(unit=unit) as t: info_lines, err_lines = exec_cmd(command) color.print_info('\n'.join(info_lines)) color.print_err('\n'.join(err_lines)) color.print_info(t)
def draw_mandelbrot(C=0, power=1 + 5j, N=800, cx=0, cy=0, d=2.5, escape_radius=2, iter_num=100): """ 绘制点(cx, cy)附近正负d的范围的Mandelbrot """ with timeme() as t: x0, x1, y0, y1 = cx - d, cx + d, cy - d, cy + d y, x = np.ogrid[y0:y1:N * 1j, x0:x1:N * 1j] z0 = x + y * 1j def smooth_iter_point(z0, c): z = z0 # 赋初值 for i in range(1, iter_num): if abs(z) > escape_radius: break z = z ** power + c # **运算符是幂运算 # 下面是重新计算迭代次数,可以获取连续的迭代次数(即正规化) absz = abs(z) # 复数的模 if absz > 2.0: mu = iter_num - log(log(abs(z), 2), 2) else: mu = iter_num return mu # 返回正规化的迭代次数 smooth_mand = np.frompyfunc(smooth_iter_point, 2, 1)(z0, C).astype(np.float) print(t) pl.gca().set_axis_off() pl.imshow(smooth_mand, extent=[x0, x1, y1, y0]) pl.show()
def main(method_name,N=int(10e5)): func=None N=int(ast.literal_eval(str(N))) if method_name in ['rectangle','rect']: func=pi.random_sampling elif method_name in ['trapezoidals','traps']: func=pi.using_trapezoidal else: raise Exception('{0} not match with any method'.format(method_name)) with timeme.timeme() as t: if comm_rank==0: n=[N//comm_size]*comm_size else: n=None local_n=comm.scatter(n,root=0) local_pi=func(local_n) all_sum=comm.reduce(local_pi,root=0,op=MPI.SUM) if comm_rank==0: global_pi=all_sum/comm_size if comm_rank==0: print('test times: ',print_num(N,need_print=False)) print('using ranctangle mpich: ',global_pi) print('time: ',t)
def main(method_name,N=int(10e5)): func=None N=int(ast.literal_eval(str(N))) if method_name in ['rectangle','rect']: func=pi.random_sampling elif method_name in ['trapezoidals','traps']: func=pi.using_trapezoidal else: raise Exception('{0} not match with any method'.format(method_name)) with timeme.timeme() as t: pi_value=func(N) print('test times: ',print_num(N,need_print=False)) print('using ranctangle: ',pi_value) print('time: ',t)
def bcast_3(): import numpy as np from minghu6.algs.timeme import timeme with timeme() as t: #y2 = np.ones(800, dtype='i') #x1=np.ones(800,dtype='i')*-2.2 if comm_rank == 0: #y, x = np.ogrid[-5:5:800*1j, -5:5:800*1j] y2 = np.ones(800, dtype='i') x1=np.ones(800,dtype='i')*-2.2 else: y2=None x1=None y2=comm.bcast(y2,root=0) x1=comm.bcast(x1,root=0) #print(x1) slice_size=y2.size//comm_size extent_size=y2.size-comm_size*slice_size if comm_rank==0: slice_m=y2[:slice_size+extent_size] slice_m*=-1 else: start=slice_size*comm_rank+extent_size end=start+slice_size slice_m=y2[start:end] slice_m*=comm_rank recv=comm.allgather(slice_m) #result=np.vstack(recv) if comm_rank==0: #print(type(recv),recv) pass print(t)
def random_sampling(N=int(10e4)): return using_rectangles(N) def using_trapezoidal(N=int(10e4)): """ :return: """ def sub_sum(): for j in range(1, N): x = -1 + j * 2 / N yield (1 - x**2)**0.5 return 4 * sum(sub_sum()) / N if __name__ == '__main__': from minghu6.algs import timeme with timeme.timeme() as tm1: pi1 = using_rectangles(int(10e5)) print('using_rectangles', pi1) with timeme.timeme() as tm2: pi2 = using_trapezoidal(int(10e5)) print('using_trapezoidal', pi2) print('tm1: ', tm1) print('tm2: ', tm2)
max_line=[] for (title,key) in [('kbytes',0),('lines',1)]: if not inner: print('\nBy {0:s}...'.format(title)) if title=='lines' and quick:continue else:allsizes.sort(key=lambda x:-x[key]) if title=='kbytes': max_size=[s[2] for s in allsizes[:topnum]] elif title=='lines': max_line=[s[2] for s in allsizes[:topnum]] if not inner: pprint.pprint(allsizes[:topnum]) return (max_size,max_line) if __name__=='__main__': from minghu6.algs.timeme import timeme with timeme() as t: args_dict=shell_interactive() file_search(**args_dict) print('total',t.total,'s')
def draw_mandelbrot_2(cx, cy, d, degree=2, N=800): """ 与draw_mandelbrot相比,是不同的实现方法,搞不太懂,也能绘图,但不好定制 绘制点(cx, cy)附近正负d的范围的Mandelbrot """ with timeme() as t0: global mandelbrot x0, x1, y0, y1 = cx - d, cx + d, cy - d, cy + d y, x = np.ogrid[y0:y1:N * 1j, x0:x1:N * 1j] c = x + y * 1j # 创建X,Y轴的坐标数组 ix, iy = np.mgrid[0:N, 0:N] # 创建保存mandelbrot图的二维数组,缺省值为最大迭代次数 mandelbrot = np.ones(c.shape, dtype=np.int) * 100 # 将数组都变成一维的 ix.shape = -1 iy.shape = -1 c.shape = -1 z = c.copy() # 从c开始迭代,因此开始的迭代次数为1 start = timeme.clock() with timeme() as t1: t1_1 = 0 t1_2 = 0 t1_3 = 0 for i in range(1, 100): # 进行一次迭代 with timeme() as t: z = z ** degree + c t1_1 += t.total # 找到所有结果逃逸了的点 with timeme() as t: tmp = np.abs(z) > 2.0 # 将这些逃逸点的迭代次数赋值给mandelbrot图 mandelbrot[ix[tmp], iy[tmp]] = i t1_2 += t.total with timeme() as t: # 找到所有没有逃逸的点 np.logical_not(tmp, tmp) # 更新ix, iy, c, z只包含没有逃逸的点 ix, iy, c, z = ix[tmp], iy[tmp], c[tmp], z[tmp] len_z = len(z) t1_3 += t.total if len_z == 0: break with timeme() as t2: print("time=", timeme.clock() - start) pl.imshow(mandelbrot, cmap=cm.Blues_r, extent=[x0, x1, y1, y0]) pl.gca().set_axis_off() all_t = t0.total + t1.total + t2.total print('t0:', t0, t0.total / all_t) print('t1:', t1, t1.total / all_t) print('t1_1', t1_1) print('t1_2', t1_2) print('t1_3', t1_3) print('t2:', t2, t2.total / all_t) pl.show()
def draw_mandelbrot_mpich(C=0, power=1 + 5j, N=800, cx=0, cy=0, d=2.5, escape_radius=2, iter_num=100): """ Use MPICH Parallel Architecture :param C: :param power: :param N: :param cx: :param cy: :param d: :param escape_radius: :param iter_num: :return: """ comm_rank = comm.Get_rank() comm_size = comm.Get_size() with timeme() as t: if comm_rank == 0: x0, x1, y0, y1 = cx - d, cx + d, cy - d, cy + d y, x = np.ogrid[y0:y1:N * 1j, x0:x1:N * 1j] z0 = x + y * 1j # z0=z0.reshape(z0.size) else: z0 = None z0 = comm.bcast(z0, root=0) slice_size = N // comm_size extent_size = N - comm_size * slice_size if comm_rank == 0: z0_self = z0[:slice_size + extent_size] else: start = slice_size * comm_rank + extent_size end = start + slice_size z0_self = z0[start:end] def smooth_iter_point(z0, c): z = z0 # 赋初值 for i in range(1, iter_num): if abs(z) > escape_radius: break z = z ** power + c # **运算符是幂运算 # 下面是重新计算迭代次数,可以获取连续的迭代次数(即正规化) absz = abs(z) # 复数的模 if absz > 2.0: mu = i - log(log(abs(z), 2), 2) else: mu = i return mu # 返回正规化的迭代次数 smooth_mand_self = np.frompyfunc(smooth_iter_point, 2, 1)(z0_self, C).astype(np.float) smooth_mand_list = comm.allgather(smooth_mand_self) # print(type(smooth_mand_list),comm_rank) smooth_mand = np.vstack(smooth_mand_list) if comm_rank == 0: pl.gca().set_axis_off() print(t) pl.imshow(smooth_mand, extent=[x0, x1, y1, y0]) pl.show() pass
def using_trapezoidal(N=int(10e4)): """ :return: """ def sub_sum(): for j in range(1,N): x=-1+j*2/N yield (1-x**2)**0.5 return 4*sum(sub_sum())/N if __name__=='__main__': from minghu6.algs import timeme with timeme.timeme() as tm1: pi1=using_rectangles(int(10e5)) print('using_rectangles',pi1) with timeme.timeme() as tm2: pi2=using_trapezoidal(int(10e5)) print('using_trapezoidal',pi2) print('tm1: ',tm1) print('tm2: ',tm2)
else: allsizes.sort(key=lambda x: -x[key]) if title == 'kbytes': max_size = [s[2] for s in allsizes[:topnum]] elif title == 'lines': max_line = [s[2] for s in allsizes[:topnum]] if not inner: for item in allsizes[:topnum]: item[0] = print_num(item[0], need_print=False, split_char=',') pprint.pprint(allsizes[:topnum]) return (max_size, max_line) def cli(): args_dict = shell_interactive() file_search(**args_dict) if __name__ == '__main__': from minghu6.algs.timeme import timeme with timeme() as t: args_dict = shell_interactive() file_search(**args_dict) print('total', t.total, 's')