使用np.vstack()时遇OSError: [WinError 87]参数错误的问题求助
曼德博集合渲染问题:大尺寸多进程拼接错误与交互渲染显示修复
问题详情
- 使用CPU多进程加速渲染32768×32768尺寸的曼德博集合,执行
np.vstack(results)拼接多进程计算结果时,触发OSError: [WinError 87] The parameter is incorrect错误;16384×16384及以下尺寸可正常运行。 - 改用交互渲染方案后,最初存在显示异常,通过在
update_plot函数中添加ax.cla()解决,但仍不清楚大尺寸拼接错误的根源。
相关代码
多进程大尺寸渲染代码
import numpy as np import matplotlib.pyplot as plt import multiprocessing as mp import warnings import time start = time.time() #to time program warnings.filterwarnings("ignore") #For the overflow and invalid exponent runtime warnings b = 8192*4 #Length and Width of the image if __name__ == "__main__": num_cores = mp.cpu_count() Y, X = np.mgrid[-1 * (b // 2):b // 2, -1 * (b // 2):b // 2] #include all four quadrants about (0,0), not just Quadrant I chunk_size = b // num_cores chunks = [] for i in range(num_cores): #create the chunks start_row = i * chunk_size end_row = (i + 1) * chunk_size if i < num_cores - 1 else b chunks.append((Y, X, start_row, end_row)) pool = mp.Pool(processes=num_cores) results = pool.imap(calculate_mandelbrot_chunk, chunks, 3) #this is the error line for b > 2^14 pool.close() pool.join() ####vvvvvvvvvvvvvvvvvvvvvv################## a = np.vstack(results)
交互渲染代码
import matplotlib.pyplot as plt from mpl_interactions import ioff, panhandler, zoom_factory import warnings import time import numpy as np warnings.filterwarnings("ignore") # For the overflow and invalid exponent runtime warnings start = time.time() # to time program b = 512 # Length and Width of the image xlims = (-512,512) ylims = (-512,512) #scroll code def on_ylims_change(event_ax): global ylims ylims = event_ax.get_ylim() def on_xlims_change(event_ax): global xlims xlims = event_ax.get_xlim() print("Updated xlims:", xlims) update_plot(xlims, ylims) #calc mandelbrot set def calculate_mandelbrot(xmin, xmax, ymin, ymax, size=512): Y, X = np.mgrid[ymin:ymax, xmin:xmax] c = (X + Y * 1j) * (4 / size) # Complex numbers array c = c.astype(np.complex64) # Save memory a = np.copy(c) * 0 for k in range(64): a = a ** 2 + c a[a < 100] = 1 # Filter divergents out return np.real(a) # Type casting def update_plot(xlims, ylims): ax.cla() # Added this line to fix display issues a_zoomed = calculate_mandelbrot(xlims[0], xlims[1], ylims[0], ylims[1], size=b) ax.imshow(a_zoomed, cmap="gray", extent=(xlims[0], xlims[1], ylims[0], ylims[1])) def blank(xlims, ylims): a_zoomed = np.zeros([32, 32]) ax.imshow(a_zoomed, cmap="gray", extent=(xlims[0], xlims[1], ylims[0], ylims[1])) a = calculate_mandelbrot(xlims[0], xlims[1], ylims[0], ylims[1], size=b) end = time.time() print(end - start) #### scroll wheel zoom #### with plt.ioff(): fig, ax = plt.subplots() ax.imshow(a, cmap="gray", extent=(-512,512,-512,512)) disconnect_zoom = zoom_factory(ax) ax.callbacks.connect('ylim_changed', on_ylims_change) ax.callbacks.connect('xlim_changed', on_xlims_change) pan_handler = panhandler(fig) plt.show()
注:在Intel i9设备上,b=512时每次重绘仅需0.3055903911590576 seconds,添加ax.cla()后解决了交互渲染时的图像叠加显示问题。
内容的提问来源于stack exchange,提问作者Aarush Abrol
相关产品推荐
相关产品推荐

