Commit 7c266d3e authored by Matthieu Boileau's avatar Matthieu Boileau

Add argparse to lbm_cl.py

Type ./lbm_cl.py -h to get help
parent cd081a59
...@@ -10,12 +10,15 @@ regular python implementation compared to a pyopencl version ...@@ -10,12 +10,15 @@ regular python implementation compared to a pyopencl version
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import pyopencl as cl import pyopencl as cl
import numpy as np import numpy as np
from matplotlib import rcParams
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import argparse
import time
##################" definition of default values ##################" definition of default values
rcParams['figure.figsize'] = (12.8, 9.6) # figure size in inches
# number of conservative variables # number of conservative variables
_m = 9 _m = 9
...@@ -39,13 +42,8 @@ _Tmax = 2. ...@@ -39,13 +42,8 @@ _Tmax = 2.
############# end of default values ############# end of default values
def solve_ocl(m=_m, n=_n, nx=_nx, ny=_ny, Lx=_Lx, Ly=_Ly, Tmax=_Tmax,
def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, animate=False, precision="single"):
Tmax = _Tmax,
Lx = _Lx, Ly = _Ly,
animate = True,
interactive=True,
precision="single"):
dx = Lx / nx dx = Lx / nx
dy = Ly / ny dy = Ly / ny
...@@ -59,7 +57,7 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, ...@@ -59,7 +57,7 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny,
dt = cfl * dx / vmax dt = cfl * dx / vmax
ff = "_F" ff = "_F"
# load and adjust C program # load and adjust C program
source = open("lbm_kernels.cl", "r").read() source = open("lbm_kernels.cl", "r").read()
source = source.replace("_nx_", "("+str(nx)+")") source = source.replace("_nx_", "("+str(nx)+")")
...@@ -68,12 +66,12 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, ...@@ -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("_dy_", "("+str(dy)+ ff + ")")
source = source.replace("_dt_", "("+str(dt)+ ff + ")") source = source.replace("_dt_", "("+str(dt)+ ff + ")")
source = source.replace("_m_", "("+str(m)+")") 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("_vx_", "("+str(vel[0])+ ff + ")")
source = source.replace("_vy_", "("+str(vel[1])+ ff + ")") source = source.replace("_vy_", "("+str(vel[1])+ ff + ")")
source = source.replace("_lambda_", "("+str(vmax)+ ff + ")") source = source.replace("_lambda_", "("+str(vmax)+ ff + ")")
if precision == "double": if precision == "double":
source = source.replace("_F", "") source = source.replace("_F", "")
source = source.replace("_real_", "double") source = source.replace("_real_", "double")
...@@ -122,13 +120,14 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, ...@@ -122,13 +120,14 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny,
# time loop # time loop
t = 0 t = 0
iter = 0 iter = 0
elapsed = 0.; elapsed = 0.
fn_cpu = np.empty((4 * m * nx * ny, ), dtype = dtype) fn_cpu = np.empty((4 * m * nx * ny, ), dtype=dtype)
if animate: if animate:
fig = plt.gcf() fig = plt.gcf()
fig.show() fig.show()
fig.canvas.draw() fig.canvas.draw()
plot_title = r"$n_x = {}, n_y = {}$".format(nx, ny)
print("start OpenCL computations...") print("start OpenCL computations...")
while t < Tmax: while t < Tmax:
...@@ -141,35 +140,60 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny, ...@@ -141,35 +140,60 @@ def solve_ocl(m = _m, n = _n, nx = _nx, ny = _ny,
elapsed += 1e-9 * (event.profile.end - event.profile.start) elapsed += 1e-9 * (event.profile.end - event.profile.start)
# exchange buffer references for avoiding a copy # exchange buffer references for avoiding a copy
fn_gpu, fnp1_gpu = fnp1_gpu, fn_gpu 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 animate:
if iter % iterplot == 0: if iter % iterplot == 0:
cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait() cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait()
wplot = np.reshape(fn_cpu, (4, m, nx, ny)) wplot = np.reshape(fn_cpu, (4, m, nx, ny))
plt.clf() plt.clf()
#plt.imshow(np.sum(wplot, axis = 0),vmin=0, vmax=1) #plt.imshow(np.sum(wplot, axis = 0),vmin=0, vmax=1)
fig.suptitle(title) fig.suptitle(ite_title)
plt.imshow(np.sum(wplot[:, _ivplot, :, :], axis = 0)) plt.imshow(np.sum(wplot[:, _ivplot, :, :], axis=0))
plt.gca().invert_yaxis() plt.gca().invert_yaxis()
plt.colorbar() plt.colorbar()
fig.canvas.draw() plt.title(plot_title)
if rcParams['backend'] == "nbAgg":
fig.canvas.draw()
else:
plt.pause(1e-6)
else: else:
print(title, end='\r') print(ite_title, end='\r')
iter = iter + 1 iter = iter + 1
# copy OpenCL data to CPU and return the results # copy OpenCL data to CPU and return the results
cl.enqueue_copy(queue, fn_cpu, fn_gpu).wait() 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 return wplot_gpu
if __name__ == '__main__': 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 # gpu solve
wplot_gpu = solve_ocl() wplot_gpu = solve_ocl(**vars(args))
#print(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0)) #print(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0))
plt.clf() plt.clf()
#plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0), vmin=0, vmax=1) #plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0), vmin=0, vmax=1)
plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0)) plt.imshow(np.sum(wplot_gpu[:, _ivplot, :, :],axis=0))
plt.gca().invert_yaxis() plt.gca().invert_yaxis()
...@@ -189,5 +213,3 @@ if __name__ == '__main__': ...@@ -189,5 +213,3 @@ if __name__ == '__main__':
# plt.gca().invert_yaxis() # plt.gca().invert_yaxis()
# plt.colorbar() # plt.colorbar()
# plt.show() # plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment