Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enabled all combinations of pre and post streaming in native cuda template #251

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ exclude_patterns:
- "**/vendor/"
- "**/*_test.go"
- "**/*.d.ts"


2 changes: 2 additions & 0 deletions lettuce/cuda_native/_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def generate(self):

val['name'] = self.name
val['version'] = self.version
val['enable_pre_streaming'] = '0'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new flags for controling the streaming in the generated kernel

val['enable_post_streaming'] = '1'

t1 = '\n '
t3 = '\n '
Expand Down
102 changes: 54 additions & 48 deletions lettuce/cuda_native/_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def invoke(simulation):
using index_t = int;
using byte_t = unsigned char;

constexpr inline index_t clamp(index_t x, index_t max) {{
[[unlikely]] if (x < 0) return x + max;
[[unlikely]] if (x >= max) return x - max;
return x;
}}

#define d {d}
#define q {q}
#define cs {cs}
Expand All @@ -157,19 +163,52 @@ def invoke(simulation):
#endif

#if d == 1
#define node_coord(x_, y_, z_) (x_)
#define node() (index[0])
#elif d == 2
#define node() (index[0] * dimension[1] + index[1])
#elif d == 3
#define node() ((index[0] * dimension[1] + index[1]) * dimension[2] + index[2])
#endif

#if d == 1
#define distribution(q_) (q_ * dimension[0] + index[0])
#elif d == 2
#define node_coord(x_, y_, z_) (x_ * dimension[1] + y_)
#define distribution(q_) ((q_ * dimension[0] + index[0]) * dimension[1] + index[1])
#elif d == 3
#define node_coord(x_, y_, z_) ((x_ * dimension[1] + y_) * dimension[2] + z_)
#define distribution(q_) (((q_ * dimension[0] + index[0]) * dimension[1] + index[1]) * dimension[2] + index[2])
#endif

#if d == 1
#define dist_coord(q_, x_, y_, z_) (q_ * dimension[0] + x_)
#define neighbour(q_, sign_) (q_ * dimension[0] + clamp(index[0] sign_ e[q_][0], dimension[0]))
#elif d == 2
#define dist_coord(q_, x_, y_, z_) ((q_ * dimension[0] + x_) * dimension[1] + y_)
#define neighbour(q_, sign_) ((q_ * dimension[0] + clamp(index[0] sign_ e[q_][0], dimension[0])) * dimension[1] + clamp(index[1] sign_ e[q_][1], dimension[1]))
#elif d == 3
#define dist_coord(q_, x_, y_, z_) (((q_ * dimension[0] + x_) * dimension[1] + y_) * dimension[2] + z_)
#define neighbour(q_, sign_) (((q_ * dimension[0] + clamp(index[0] sign_ e[q_][0], dimension[0])) * dimension[1] + clamp(index[1] sign_ e[q_][1], dimension[1])) * dimension[2] + clamp(index[2] sign_ e[q_][2], dimension[2]))
#endif

#define read_(q_) f_reg[q_] = f[dist_index[q_]]; ((void)0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new definitions for reading / writing / streaming

#define write_(q_) f_next[dist_index[q_]] = f_reg[q_]; ((void)0)
#define stream_read_(q_) f_reg[q_] = f[neighbour(q_, -)]; ((void)0)
#define stream_write_(q_) f_next[neighbour(q_, +)] = f_reg[q_]; ((void)0)

#if {enable_pre_streaming}
#if {support_no_streaming_mask}
#define read(q_) {{ if (no_streaming_mask[dist_index[q_]]) {{ read_(q_); }} else {{ stream_read_(q_); }} }} ((void)0)
#else
#define read(q_) {{ stream_read_(q_); }} ((void)0)
#endif
#else
#define read(q_) {{ read_(q_); }} ((void)0)
#endif

#if {enable_post_streaming}
#if {support_no_streaming_mask}
#define write(q_) {{ if (no_streaming_mask[dist_index[q_]]) {{ write_(q_); }} else {{ stream_write_(q_); }} }} ((void)0)
#else
#define write(q_) {{ stream_write_(q_); }} ((void)0)
#endif
#else
#define write(q_) {{ write_(q_); }} ((void)0)
#endif

template<typename scalar_t>
Expand All @@ -190,6 +229,8 @@ def invoke(simulation):
#endif
{kernel_parameter})
{{
constexpr index_t e[q][d] = {e};
constexpr scalar_t w[q] = {w};

const index_t index[d] = {{
static_cast<index_t>(blockIdx.x * blockDim.x + threadIdx.x)
Expand All @@ -212,27 +253,18 @@ def invoke(simulation):
}};

#if {support_no_collision_mask}
const index_t node_index = node_coord(index[0], index[1], index[2]);
const index_t node_index = node();
#endif

#if {support_no_streaming_mask}
index_t dist_index[q];
#pragma unroll
for (index_t i = 0; i < q; ++i) {{
dist_index[i] = dist_coord(i, index[0], index[1], index[2]);
}}
scalar_t f_reg[q];
#pragma unroll
for (index_t i = 0; i < q; ++i)
f_reg[i] = f[dist_index[i]];
#else
dist_index[i] = distribution(i);

scalar_t f_reg[q];
#pragma unroll
for (index_t i = 0; i < q; ++i)
f_reg[i] = f[dist_coord(i, index[0], index[1], index[2])];
#endif

constexpr index_t e[q][d] = {e};
constexpr scalar_t w[q] = {w};
read(i);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key change: simplified template body by shifting the streaming logic into a macro definition


scalar_t rho = f_reg[0];
#pragma unroll
Expand Down Expand Up @@ -264,34 +296,8 @@ def invoke(simulation):
{pipeline_buffer}

#pragma unroll
for (index_t i = 0; i < q; ++i) {{

#if {support_no_streaming_mask}
if (no_streaming_mask[dist_index[i]])
f_next[dist_index[i]] = f_reg[i];
else
#endif

{{
index_t neighbor_x = index[0] + e[i][0];
if (neighbor_x < 0) neighbor_x += dimension[0];
else if (neighbor_x >= dimension[0]) neighbor_x -= dimension[0];

#if d > 1
index_t neighbor_y = index[1] + e[i][1];
if (neighbor_y < 0) neighbor_y += dimension[1];
else if (neighbor_y >= dimension[1]) neighbor_y -= dimension[1];
#endif

#if d > 2
index_t neighbor_z = index[2] + e[i][2];
if (neighbor_z < 0) neighbor_z += dimension[2];
else if (neighbor_z >= dimension[2]) neighbor_z -= dimension[2];
#endif

f_next[dist_coord(i, neighbor_x, neighbor_y, neighbor_z)] = f_reg[i];
}}
}}
for (index_t i = 0; i < q; ++i)
write(i);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key change: simplified template body by shifting the streaming logic into a macro definition

}}

void
Expand Down
11 changes: 0 additions & 11 deletions lettuce/native_generator/__init__.py

This file was deleted.

8 changes: 0 additions & 8 deletions lettuce/native_generator/_base.py

This file was deleted.

90 changes: 0 additions & 90 deletions lettuce/native_generator/_collision.py

This file was deleted.

105 changes: 0 additions & 105 deletions lettuce/native_generator/_cuda.py

This file was deleted.

Loading
Loading