aboutsummaryrefslogtreecommitdiff
path: root/template/kernel.mako
blob: 01c5b33d0e4eb1ffb14e0b92bc723fe3bcd99f08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<%
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)
%>

__kernel void equilibrilize(__global __write_only float* f_next,
                            __global __write_only float* f_prev)
{
    const unsigned int gid = ${gid()};

    __global __write_only float* preshifted_f_next = f_next + gid;
    __global __write_only float* preshifted_f_prev = f_prev + gid;

% if pop_eq_src == '':
%     for i, w_i in enumerate(descriptor.w):
    preshifted_f_next[${i*geometry.volume}] = ${w_i}.f;
    preshifted_f_prev[${i*geometry.volume}] = ${w_i}.f;
%     endfor
% else:
    ${pop_eq_src}
% endif
}

<%
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 __write_only float* f_next,
                                 __global __read_only  float* f_prev,
                                 __global __read_only  int* material)
{
    const unsigned int gid = ${gid()};

    const int m = material[gid];

    if ( m == 0 ) {
        return;
    }

    __global __write_only float* preshifted_f_next = f_next + gid;
    __global __read_only  float* preshifted_f_prev = f_prev + gid;

% for i, c_i in enumerate(descriptor.c):
    const float f_curr_${i} = preshifted_f_prev[${i*geometry.volume + neighbor_offset(-c_i)}];
% endfor

% for i, expr in enumerate(moments_subexpr):
    const float ${expr[0]} = ${ccode(expr[1])};
% endfor

% for i, expr in enumerate(moments_assignment):
    float ${ccode(expr)}
% endfor

  ${boundary_src}

% for i, expr in enumerate(collide_subexpr):
    const float ${expr[0]} = ${ccode(expr[1])};
% endfor

% for i, expr in enumerate(collide_assignment):
    const float ${ccode(expr)}
% endfor

% for i in range(0,descriptor.q):
    preshifted_f_next[${i*geometry.volume}] = f_next_${i};
% endfor
}

__kernel void collect_moments(__global __read_only  float* f,
                              __global __write_only float* moments)
{
    const unsigned int gid = ${gid()};

    __global __read_only float* preshifted_f = f + gid;

% for i in range(0,descriptor.q):
    const float f_curr_${i} = preshifted_f[${i*geometry.volume}];
% endfor

% for i, expr in enumerate(moments_subexpr):
    const float ${expr[0]} = ${ccode(expr[1])};
% endfor

% for i, expr in enumerate(moments_assignment):
    moments[${i*geometry.volume} + gid] = ${ccode(expr.rhs)};
% endfor
}