UniformGridGPU.py 4.1 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
Martin Bauer's avatar
Martin Bauer committed
4
5
from lbmpy.creationfunctions import create_lb_method, create_lb_update_rule
from lbmpy.boundaries import NoSlip, UBB
6
from lbmpy.fieldaccess import StreamPullTwoFieldsAccessor, StreamPushTwoFieldsAccessor
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
15
16
17
18
omega = sp.symbols("omega")
# sweep_block_size = (128, 1, 1)
sweep_block_size = (TypedSymbol("cudaBlockSize0", np.int32),
                    TypedSymbol("cudaBlockSize1", np.int32),
                    1)
Martin Bauer's avatar
Martin Bauer committed
19

Martin Bauer's avatar
Martin Bauer committed
20
21
sweep_params = {'block_size': sweep_block_size}

22
options_dict = {
23
    'srt': {
Martin Bauer's avatar
Martin Bauer committed
24
25
        'method': 'srt',
        'stencil': 'D3Q19',
26
        'relaxation_rate': omega,
Martin Bauer's avatar
Martin Bauer committed
27
        'compressible': False,
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    },
    'trt': {
        'method': 'trt',
        'stencil': 'D3Q19',
        'relaxation_rate': omega,
    },
    'mrt': {
        'method': 'mrt',
        'stencil': 'D3Q19',
        'relaxation_rates': [0, omega, 1.3, 1.4, omega, 1.2, 1.1],
    },
    'entropic': {
        'method': 'mrt3',
        'stencil': 'D3Q19',
        'compressible': True,
        'relaxation_rates': [omega, omega, sp.Symbol("omega_free")],
        'entropic': True,
    },
    'smagorinsky': {
        'method': 'srt',
        'stencil': 'D3Q19',
        'smagorinsky': True,
        'relaxation_rate': omega,
    }
}

with CodeGeneration() as ctx:
55
56
57
    accessor = StreamPullTwoFieldsAccessor()
    #accessor = StreamPushTwoFieldsAccessor()
    assert not accessor.is_inplace, "This app does not work for inplace accessors"
58
59
60

    common_options = {
        'field_name': 'pdfs',
Martin Bauer's avatar
Martin Bauer committed
61
        'temporary_field_name': 'pdfs_tmp',
62
        'kernel_type': accessor,
Martin Bauer's avatar
Martin Bauer committed
63
        'optimization': {'cse_global': True,
64
                         'cse_pdfs': False}
Martin Bauer's avatar
Martin Bauer committed
65
    }
66
    options = options_dict.get(ctx.config, options_dict['srt'])
67
68
    options.update(common_options)

69
70
71
72
73
    stencil_str = options['stencil']
    q = int(stencil_str[stencil_str.find('Q')+1:])
    pdfs, velocity_field = ps.fields("pdfs({q}), velocity(3) : double[3D]".format(q=q), layout='fzyx')
    options['optimization']['symbolic_field'] = pdfs

74
75
76
77
    vp = [
        ('int32_t', 'cudaBlockSize0'),
        ('int32_t', 'cudaBlockSize1')
    ]
Martin Bauer's avatar
Martin Bauer committed
78
79
80
    lb_method = create_lb_method(**options)
    update_rule = create_lb_update_rule(lb_method=lb_method, **options)

81
82
83
    update_rule = insert_fast_divisions(update_rule)
    update_rule = insert_fast_sqrts(update_rule)

Martin Bauer's avatar
Martin Bauer committed
84
    # CPU lattice model - required for macroscopic value computation, VTK output etc.
85
86
87
    options_without_opt = options.copy()
    del options_without_opt['optimization']
    generate_lattice_model(ctx, 'UniformGridGPU_LatticeModel', lb_method, update_rule_params=options_without_opt)
Martin Bauer's avatar
Martin Bauer committed
88
89

    # gpu LB sweep & boundaries
90
91
92
93
94
    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
95
96
97
    generate_boundary(ctx, 'UniformGridGPU_NoSlip', NoSlip(), lb_method, target='gpu')
    generate_boundary(ctx, 'UniformGridGPU_UBB', UBB([0.05, 0, 0]), lb_method, target='gpu')

98
99
100
101
102
103
104
105
    # getter & setter
    setter_assignments = macroscopic_values_setter(lb_method, velocity=velocity_field.center_vector,
                                                   pdfs=pdfs.center_vector, density=1)
    getter_assignments = macroscopic_values_getter(lb_method, velocity=velocity_field.center_vector,
                                                   pdfs=pdfs.center_vector,  density=None)
    generate_sweep(ctx, 'UniformGridGPU_MacroSetter', setter_assignments)
    generate_sweep(ctx, 'UniformGridGPU_MacroGetter', getter_assignments)

Martin Bauer's avatar
Martin Bauer committed
106
107
    # communication
    generate_pack_info_from_kernel(ctx, 'UniformGridGPU_PackInfo', update_rule, target='gpu')