From 7c266d3e648eae6c885be51089efe724a0475b9a Mon Sep 17 00:00:00 2001 From: Matthieu Boileau Date: Thu, 7 Dec 2017 17:29:39 +0100 Subject: [PATCH] Add argparse to lbm_cl.py Type ./lbm_cl.py -h to get help --- lbm_cl.py | 68 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/lbm_cl.py b/lbm_cl.py index 42a206d..f830a36 100755 --- a/lbm_cl.py +++ b/lbm_cl.py @@ -10,12 +10,15 @@ regular python implementation compared to a pyopencl version from __future__ import absolute_import, print_function import pyopencl as cl import numpy as np +from matplotlib import rcParams import matplotlib.pyplot as plt - -import time +import argparse ##################" definition of default values + +rcParams['figure.figsize'] = (12.8, 9.6) # figure size in inches + # number of conservative variables _m = 9 @@ -39,13 +42,8 @@ _Tmax = 2. ############# end of default values - -def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, - Tmax = _Tmax, - Lx = _Lx, Ly = _Ly, - animate = True, - interactive=True, - precision="single"): +def solve_ocl(m=_m, n=_n, nx=_nx, ny=_ny, Lx=_Lx, Ly=_Ly, Tmax=_Tmax, + animate=False, precision="single"): dx = Lx / nx dy = Ly / ny @@ -59,7 +57,7 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, dt = cfl * dx / vmax ff = "_F" - + # load and adjust C program source = open("lbm_kernels.cl", "r").read() source = source.replace("_nx_", "("+str(nx)+")") @@ -68,12 +66,12 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, source = source.replace("_dy_", "("+str(dy)+ ff + ")") source = source.replace("_dt_", "("+str(dt)+ ff + ")") source = source.replace("_m_", "("+str(m)+")") - source = source.replace("_n_", "("+str(n)+")") + source = source.replace("_n_", "("+str(n)+")") source = source.replace("_vx_", "("+str(vel[0])+ ff + ")") source = source.replace("_vy_", "("+str(vel[1])+ ff + ")") source = source.replace("_lambda_", "("+str(vmax)+ ff + ")") - + if precision == "double": source = source.replace("_F", "") source = source.replace("_real_", "double") @@ -122,13 +120,14 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, # time loop t = 0 iter = 0 - elapsed = 0.; - fn_cpu = np.empty((4 * m * nx * ny, ), dtype = dtype) + elapsed = 0. + fn_cpu = np.empty((4 * m * nx * ny, ), dtype=dtype) if animate: fig = plt.gcf() fig.show() fig.canvas.draw() + plot_title = r"\$n_x = {}, n_y = {}\$".format(nx, ny) print("start OpenCL computations...") while t < Tmax: @@ -141,35 +140,60 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, elapsed += 1e-9 * (event.profile.end - event.profile.start) # exchange buffer references for avoiding a copy fn_gpu, fnp1_gpu = fnp1_gpu, fn_gpu - title = "iter = {}, t = {:f}, elapsed (s) = {:f}".format(iter, t, elapsed) + ite_title = "iter = {}, t = {:f}, elapsed (s) = {:f}".format(iter, + t, + elapsed) if animate: if iter % iterplot == 0: cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait() wplot = np.reshape(fn_cpu, (4, m, nx, ny)) plt.clf() #plt.imshow(np.sum(wplot, axis = 0),vmin=0, vmax=1) - fig.suptitle(title) - plt.imshow(np.sum(wplot[:, _ivplot, :, :], axis = 0)) + fig.suptitle(ite_title) + plt.imshow(np.sum(wplot[:, _ivplot, :, :], axis=0)) plt.gca().invert_yaxis() plt.colorbar() - fig.canvas.draw() + plt.title(plot_title) + if rcParams['backend'] == "nbAgg": + fig.canvas.draw() + else: + plt.pause(1e-6) else: - print(title, end='\r') + print(ite_title, end='\r') iter = iter + 1 # copy OpenCL data to CPU and return the results cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait() - wplot_gpu = np.reshape(fn_cpu,(4, m, nx, ny)) + wplot_gpu = np.reshape(fn_cpu, (4, m, nx, ny)) return wplot_gpu if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Solve orszag-tang using LBM' + ' on PyOpenCL') + parser.add_argument('-a', '--animate', action='store_true', + help='real-time 2D plots') + parser.add_argument('-n', '--resolution', metavar='nx=ny', type=int, + default=256, help='space resolution') + parser.add_argument('-t', '--Tmax', type=float, default=2., + help='final simulation time') + parser.add_argument('-p', '--precision', type=str, default='single', + choices=['single', 'double'], + help='floating point precision') + args = parser.parse_args() + arg_dict = vars(args) + arg_dict['nx'] = arg_dict['ny'] = arg_dict['resolution'] + del(arg_dict['resolution']) + print(arg_dict) + # gpu solve - wplot_gpu = solve_ocl() + wplot_gpu = solve_ocl(**vars(args)) + #print(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0)) plt.clf() + #plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0), vmin=0, vmax=1) plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0)) plt.gca().invert_yaxis() @@ -189,5 +213,3 @@ if __name__ == '__main__': # plt.gca().invert_yaxis() # plt.colorbar() # plt.show() - - -- GitLab