UniformGridGPU.py 6.23 KB
Newer Older
Martin Bauer's avatar
Martin Bauer committed
1
import sympy as sp
2
import numpy as np
3
import pystencils as ps
4
from lbmpy.creationfunctions import create_lb_method, create_lb_update_rule, create_lb_collision_rule
Martin Bauer's avatar
Martin Bauer committed
5
from lbmpy.boundaries import NoSlip, UBB
6
from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor
Martin Bauer's avatar
Martin Bauer committed
7
8
9
from pystencils_walberla import generate_pack_info_from_kernel
from lbmpy_walberla import generate_lattice_model, generate_boundary
from pystencils_walberla import CodeGeneration, generate_sweep
10
11
from pystencils.data_types import TypedSymbol
from pystencils.fast_approximation import insert_fast_sqrts, insert_fast_divisions
12
from lbmpy.macroscopic_value_kernels import macroscopic_values_getter, macroscopic_values_setter
Martin Bauer's avatar
Martin Bauer committed
13

14
omega = sp.symbols("omega")
15
omega_free = sp.Symbol("omega_free")
16
omega_fill = sp.symbols("omega_:10")
17
18
19
20
21
22
23
24
compile_time_block_size = False

if compile_time_block_size:
    sweep_block_size = (128, 1, 1)
else:
    sweep_block_size = (TypedSymbol("cudaBlockSize0", np.int32),
                        TypedSymbol("cudaBlockSize1", np.int32),
                        1)
Martin Bauer's avatar
Martin Bauer committed
25

Martin Bauer's avatar
Martin Bauer committed
26
27
sweep_params = {'block_size': sweep_block_size}

28
options_dict = {
29
    'srt': {
Martin Bauer's avatar
Martin Bauer committed
30
31
        'method': 'srt',
        'stencil': 'D3Q19',
32
        'relaxation_rate': omega,
Martin Bauer's avatar
Martin Bauer committed
33
        'compressible': False,
34
35
36
37
38
39
40
41
42
    },
    'trt': {
        'method': 'trt',
        'stencil': 'D3Q19',
        'relaxation_rate': omega,
    },
    'mrt': {
        'method': 'mrt',
        'stencil': 'D3Q19',
43
        'relaxation_rates': [omega, 1.3, 1.4, 1.2, 1.1, 1.15, 1.234, 1.4235],
44
    },
45
46
47
    'mrt_full': {
        'method': 'mrt',
        'stencil': 'D3Q19',
48
49
        'relaxation_rates': [omega_fill[0], omega, omega_fill[1], omega_fill[2],
                             omega_fill[3], omega_fill[4], omega_fill[5]],
50
    },
51
    'entropic': {
52
        'method': 'mrt',
53
54
        'stencil': 'D3Q19',
        'compressible': True,
55
        'relaxation_rates': [omega, omega, omega_free, omega_free, omega_free, omega_free],
56
57
        'entropic': True,
    },
58
59
60
61
    'entropic_kbc_n4': {
        'method': 'trt-kbc-n4',
        'stencil': 'D3Q27',
        'compressible': True,
62
        'relaxation_rates': [omega, omega_free],
63
64
        'entropic': True,
    },
65
66
67
68
69
    'smagorinsky': {
        'method': 'srt',
        'stencil': 'D3Q19',
        'smagorinsky': True,
        'relaxation_rate': omega,
Martin Bauer's avatar
Martin Bauer committed
70
71
    },
    'cumulant': {
Markus Holzer's avatar
Markus Holzer committed
72
        'method': 'cumulant',
Martin Bauer's avatar
Martin Bauer committed
73
74
        'stencil': 'D3Q19',
        'compressible': True,
Markus Holzer's avatar
Markus Holzer committed
75
        'relaxation_rate': omega,
Martin Bauer's avatar
Martin Bauer committed
76
    },
77
78
}

79
info_header = """
80
#include "stencil/D3Q{q}.h"\nusing Stencil_T = walberla::stencil::D3Q{q};
81
82
83
84
85
86
87
const char * infoStencil = "{stencil}";
const char * infoConfigName = "{configName}";
const bool infoCseGlobal = {cse_global};
const bool infoCsePdfs = {cse_pdfs};
"""


88
with CodeGeneration() as ctx:
89
    accessor = StreamPullTwoFieldsAccessor()
90
    # accessor = StreamPushTwoFieldsAccessor()
91
    assert not accessor.is_inplace, "This app does not work for inplace accessors"
92
93
94

    common_options = {
        'field_name': 'pdfs',
Martin Bauer's avatar
Martin Bauer committed
95
        'temporary_field_name': 'pdfs_tmp',
96
        'kernel_type': accessor,
Martin Bauer's avatar
Martin Bauer committed
97
        'optimization': {'cse_global': True,
98
                         'cse_pdfs': False}
Martin Bauer's avatar
Martin Bauer committed
99
    }
100
101
    config_name = ctx.config
    noopt = False
102
    d3q27 = False
103
104
105
    if config_name.endswith("_noopt"):
        noopt = True
        config_name = config_name[:-len("_noopt")]
106
107
108
    if config_name.endswith("_d3q27"):
        d3q27 = True
        config_name = config_name[:-len("_d3q27")]
109
110

    options = options_dict[config_name]
111
    options.update(common_options)
112
113
114
115
116
    options = options.copy()

    if noopt:
        options['optimization']['cse_global'] = False
        options['optimization']['cse_pdfs'] = False
117
118
    if d3q27:
        options['stencil'] = 'D3Q27'
119

120
    stencil_str = options['stencil']
121
    q = int(stencil_str[stencil_str.find('Q') + 1:])
122
123
124
    pdfs, velocity_field = ps.fields("pdfs({q}), velocity(3) : double[3D]".format(q=q), layout='fzyx')
    options['optimization']['symbolic_field'] = pdfs

125
    vp = [
126
127
128
129
130
131
132
        ('double', 'omega_0'),
        ('double', 'omega_1'),
        ('double', 'omega_2'),
        ('double', 'omega_3'),
        ('double', 'omega_4'),
        ('double', 'omega_5'),
        ('double', 'omega_6'),
133
        ('int32_t', 'cudaBlockSize0'),
134
        ('int32_t', 'cudaBlockSize1'),
135
    ]
Martin Bauer's avatar
Martin Bauer committed
136
137
    lb_method = create_lb_method(**options)
    update_rule = create_lb_update_rule(lb_method=lb_method, **options)
Markus Holzer's avatar
Markus Holzer committed
138
 
139
140
141
    if not noopt:
        update_rule = insert_fast_divisions(update_rule)
        update_rule = insert_fast_sqrts(update_rule)
142

Martin Bauer's avatar
Martin Bauer committed
143
    # CPU lattice model - required for macroscopic value computation, VTK output etc.
144
145
    options_without_opt = options.copy()
    del options_without_opt['optimization']
146
147
    generate_lattice_model(ctx, 'UniformGridGPU_LatticeModel', create_lb_collision_rule(lb_method=lb_method,
                                                                                        **options_without_opt))
Martin Bauer's avatar
Martin Bauer committed
148
149

    # gpu LB sweep & boundaries
150
151
152
153
154
    generate_sweep(ctx, 'UniformGridGPU_LbKernel', update_rule,
                   field_swaps=[('pdfs', 'pdfs_tmp')],
                   inner_outer_split=True, target='gpu', gpu_indexing_params=sweep_params,
                   varying_parameters=vp)

Martin Bauer's avatar
Martin Bauer committed
155
156
157
    generate_boundary(ctx, 'UniformGridGPU_NoSlip', NoSlip(), lb_method, target='gpu')
    generate_boundary(ctx, 'UniformGridGPU_UBB', UBB([0.05, 0, 0]), lb_method, target='gpu')

158
159
    # getter & setter
    setter_assignments = macroscopic_values_setter(lb_method, velocity=velocity_field.center_vector,
160
                                                   pdfs=pdfs.center_vector, density=1.0)
161
    getter_assignments = macroscopic_values_getter(lb_method, velocity=velocity_field.center_vector,
162
                                                   pdfs=pdfs.center_vector, density=None)
163
164
165
    generate_sweep(ctx, 'UniformGridGPU_MacroSetter', setter_assignments)
    generate_sweep(ctx, 'UniformGridGPU_MacroGetter', getter_assignments)

Martin Bauer's avatar
Martin Bauer committed
166
167
    # communication
    generate_pack_info_from_kernel(ctx, 'UniformGridGPU_PackInfo', update_rule, target='gpu')
168
169
170
171
172
173
174
175
176

    infoHeaderParams = {
        'stencil': stencil_str,
        'q': q,
        'configName': ctx.config,
        'cse_global': int(options['optimization']['cse_global']),
        'cse_pdfs': int(options['optimization']['cse_pdfs']),
    }
    ctx.write_file("UniformGridGPU_Defines.h", info_header.format(**infoHeaderParams))