From 2e07d5fac4d3fedcd2cc9ccbab27f50c69f65983 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 23 Oct 2019 22:03:46 +0200 Subject: Some cleanup, add `collect_moments` to C++ template --- boltzgen/kernel/template/basic.cl.mako | 101 ++++++++++++++++++++++++++++ boltzgen/kernel/template/basic.cpp.mako | 56 +++++++++++----- boltzgen/kernel/template/basic.opencl.mako | 104 ----------------------------- 3 files changed, 140 insertions(+), 121 deletions(-) create mode 100644 boltzgen/kernel/template/basic.cl.mako delete mode 100644 boltzgen/kernel/template/basic.opencl.mako diff --git a/boltzgen/kernel/template/basic.cl.mako b/boltzgen/kernel/template/basic.cl.mako new file mode 100644 index 0000000..1b02c63 --- /dev/null +++ b/boltzgen/kernel/template/basic.cl.mako @@ -0,0 +1,101 @@ +<% +def gid(): + return { + 2: 'get_global_id(1)*%d + get_global_id(0)' % geometry.size_x, + 3: 'get_global_id(2)*%d + get_global_id(1)*%d + get_global_id(0)' % (geometry.size_x*geometry.size_y, geometry.size_x) + }.get(descriptor.d) + +def pop_offset(i): + return i * geometry.volume + +def neighbor_offset(c_i): + return { + 2: lambda: c_i[1]*geometry.size_x + c_i[0], + 3: lambda: c_i[2]*geometry.size_x*geometry.size_y + c_i[1]*geometry.size_x + c_i[0] + }.get(descriptor.d)() +%> + +% if float_type == 'double': +#if defined(cl_khr_fp64) +#pragma OPENCL EXTENSION cl_khr_fp64 : enable +#elif defined(cl_amd_fp64) +#pragma OPENCL EXTENSION cl_amd_fp64 : enable +#endif +% endif + +__kernel void equilibrilize(__global ${float_type}* f_next, + __global ${float_type}* f_prev) +{ + const unsigned int gid = ${gid()}; + + __global ${float_type}* preshifted_f_next = f_next + gid; + __global ${float_type}* preshifted_f_prev = f_prev + gid; + +% for i, w_i in enumerate(descriptor.w): + preshifted_f_next[${pop_offset(i)}] = ${w_i}.f; + preshifted_f_prev[${pop_offset(i)}] = ${w_i}.f; +% endfor +} + +__kernel void collide_and_stream(__global ${float_type}* f_next, + __global ${float_type}* f_prev, + __global int* material, + unsigned int time) +{ + const unsigned int gid = ${gid()}; + + const int m = material[gid]; + + if ( m == 0 ) { + return; + } + + __global ${float_type}* preshifted_f_next = f_next + gid; + __global ${float_type}* preshifted_f_prev = f_prev + gid; + +% for i, c_i in enumerate(descriptor.c): + const ${float_type} f_curr_${i} = preshifted_f_prev[${pop_offset(i) + neighbor_offset(-c_i)}]; +% endfor + +% for i, expr in enumerate(moments_subexpr): + const ${float_type} ${expr[0]} = ${ccode(expr[1])}; +% endfor + +% for i, expr in enumerate(moments_assignment): + ${float_type} ${ccode(expr)} +% endfor + + ${boundary_src} + +% for i, expr in enumerate(collision_subexpr): + const ${float_type} ${expr[0]} = ${ccode(expr[1])}; +% endfor + +% for i, expr in enumerate(collision_assignment): + const ${float_type} ${ccode(expr)} +% endfor + +% for i in range(0,descriptor.q): + preshifted_f_next[${pop_offset(i)}] = f_next_${i}; +% endfor +} + +__kernel void collect_moments(__global ${float_type}* f, + __global ${float_type}* moments) +{ + const unsigned int gid = ${gid()}; + + __global ${float_type}* preshifted_f = f + gid; + +% for i in range(0,descriptor.q): + const ${float_type} f_curr_${i} = preshifted_f[${pop_offset(i)}]; +% endfor + +% for i, expr in enumerate(moments_subexpr): + const ${float_type} ${expr[0]} = ${ccode(expr[1])}; +% endfor + +% for i, expr in enumerate(moments_assignment): + moments[${pop_offset(i)} + gid] = ${ccode(expr.rhs)}; +% endfor +} diff --git a/boltzgen/kernel/template/basic.cpp.mako b/boltzgen/kernel/template/basic.cpp.mako index ef3486f..d284a1c 100644 --- a/boltzgen/kernel/template/basic.cpp.mako +++ b/boltzgen/kernel/template/basic.cpp.mako @@ -1,22 +1,7 @@ <% def pop_offset(i): return i * geometry.volume -%> - -void equilibrilize(${float_type}* f_next, - ${float_type}* f_prev, - const std::size_t gid) -{ - ${float_type}* preshifted_f_next = f_next + gid; - ${float_type}* preshifted_f_prev = f_prev + gid; -% for i, w_i in enumerate(descriptor.w): - preshifted_f_next[${pop_offset(i)}] = ${w_i.evalf()}; - preshifted_f_prev[${pop_offset(i)}] = ${w_i.evalf()}; -% endfor -} - -<% def neighbor_offset(c_i): return { 2: lambda: c_i[1]*geometry.size_x + c_i[0], @@ -30,10 +15,23 @@ def padding(): }.get(descriptor.d)() %> +void equilibrilize(${float_type}* f_next, + ${float_type}* f_prev, + std::size_t gid) +{ + ${float_type}* preshifted_f_next = f_next + gid; + ${float_type}* preshifted_f_prev = f_prev + gid; + +% for i, w_i in enumerate(descriptor.w): + preshifted_f_next[${pop_offset(i)}] = ${w_i.evalf()}; + preshifted_f_prev[${pop_offset(i)}] = ${w_i.evalf()}; +% endfor +} + void collide_and_stream( ${float_type}* f_next, const ${float_type}* f_prev, - const int* material, - const std::size_t gid) + const int* material, + std::size_t gid) { const int m = material[gid]; @@ -64,3 +62,27 @@ void collide_and_stream( ${float_type}* f_next, preshifted_f_next[${pop_offset(i)}] = m*f_next_${i} + (1.0-m)*${descriptor.w[i].evalf()}; % endfor } + +void collect_moments(const ${float_type}* f, + std::size_t gid, + ${float_type}& rho, + ${float_type} u[${descriptor.d}]) +{ + const ${float_type}* preshifted_f = f + gid; + +% for i in range(0,descriptor.q): + const ${float_type} f_curr_${i} = preshifted_f[${pop_offset(i)}]; +% endfor + +% for i, expr in enumerate(moments_subexpr): + const ${float_type} ${expr[0]} = ${ccode(expr[1])}; +% endfor + +% for i, expr in enumerate(moments_assignment): +% if i == 0: + rho = ${ccode(expr.rhs)}; +% else: + u[${i-1}] = ${ccode(expr.rhs)}; +% endif +% endfor +} diff --git a/boltzgen/kernel/template/basic.opencl.mako b/boltzgen/kernel/template/basic.opencl.mako deleted file mode 100644 index 3ecb16c..0000000 --- a/boltzgen/kernel/template/basic.opencl.mako +++ /dev/null @@ -1,104 +0,0 @@ -% if float_type == 'double': -#if defined(cl_khr_fp64) -#pragma OPENCL EXTENSION cl_khr_fp64 : enable -#elif defined(cl_amd_fp64) -#pragma OPENCL EXTENSION cl_amd_fp64 : enable -#endif -% endif - -<% -def gid(): - return { - 2: 'get_global_id(1)*%d + get_global_id(0)' % geometry.size_x, - 3: 'get_global_id(2)*%d + get_global_id(1)*%d + get_global_id(0)' % (geometry.size_x*geometry.size_y, geometry.size_x) - }.get(descriptor.d) - -def pop_offset(i): - return i * geometry.volume -%> - -__kernel void equilibrilize(__global ${float_type}* f_next, - __global ${float_type}* f_prev) -{ - const unsigned int gid = ${gid()}; - - __global ${float_type}* preshifted_f_next = f_next + gid; - __global ${float_type}* preshifted_f_prev = f_prev + gid; - -% for i, w_i in enumerate(descriptor.w): - preshifted_f_next[${pop_offset(i)}] = ${w_i}.f; - preshifted_f_prev[${pop_offset(i)}] = ${w_i}.f; -% endfor -} - -<% -def neighbor_offset(c_i): - return { - 2: lambda: c_i[1]*geometry.size_x + c_i[0], - 3: lambda: c_i[2]*geometry.size_x*geometry.size_y + c_i[1]*geometry.size_x + c_i[0] - }.get(descriptor.d)() - -%> - -__kernel void collide_and_stream(__global ${float_type}* f_next, - __global ${float_type}* f_prev, - __global int* material, - unsigned int time) -{ - const unsigned int gid = ${gid()}; - - const int m = material[gid]; - - if ( m == 0 ) { - return; - } - - __global ${float_type}* preshifted_f_next = f_next + gid; - __global ${float_type}* preshifted_f_prev = f_prev + gid; - -% for i, c_i in enumerate(descriptor.c): - const ${float_type} f_curr_${i} = preshifted_f_prev[${pop_offset(i) + neighbor_offset(-c_i)}]; -% endfor - -% for i, expr in enumerate(moments_subexpr): - const ${float_type} ${expr[0]} = ${ccode(expr[1])}; -% endfor - -% for i, expr in enumerate(moments_assignment): - ${float_type} ${ccode(expr)} -% endfor - - ${boundary_src} - -% for i, expr in enumerate(collision_subexpr): - const ${float_type} ${expr[0]} = ${ccode(expr[1])}; -% endfor - -% for i, expr in enumerate(collision_assignment): - const ${float_type} ${ccode(expr)} -% endfor - -% for i in range(0,descriptor.q): - preshifted_f_next[${pop_offset(i)}] = f_next_${i}; -% endfor -} - -__kernel void collect_moments(__global ${float_type}* f, - __global ${float_type}* moments) -{ - const unsigned int gid = ${gid()}; - - __global ${float_type}* preshifted_f = f + gid; - -% for i in range(0,descriptor.q): - const ${float_type} f_curr_${i} = preshifted_f[${pop_offset(i)}]; -% endfor - -% for i, expr in enumerate(moments_subexpr): - const ${float_type} ${expr[0]} = ${ccode(expr[1])}; -% endfor - -% for i, expr in enumerate(moments_assignment): - moments[${pop_offset(i)} + gid] = ${ccode(expr.rhs)}; -% endfor -} -- cgit v1.2.3