Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
warp_reduce.h
Go to the documentation of this file.
1#pragma once
2
3#include "vecs.h"
4
5#if AC_USE_HIP
6 const char* shuffle_instruction = "rocprim__warp_shuffle(val,target_tid)";
7#else
8 const char* shuffle_instruction = "__shfl_sync(AC_INTERNAL_active_threads,val,target_tid)";
9#endif
10
11void
12print_shuffle_down(FILE* stream, const int offset)
13{
14#if AC_USE_HIP
15 fprintf(stream,"rocprim__warp_shuffle_down(val,%d)",offset);
16#else
17 fprintf(stream,"__shfl_down_sync(AC_INTERNAL_active_threads,val,%d)",offset);
18#endif
19}
20
21void
22print_shuffle_iteration(FILE* stream, const char* tmp_type, const int offset, const char* op_instruction, const bool check_activeness)
23{
24 char shuffle_down[1000];
25 memset(shuffle_down,0,sizeof(shuffle_down));
26 {
27 FILE* instruction_stream = fmemopen(shuffle_down, sizeof(shuffle_down), "w");
28 print_shuffle_down(instruction_stream,offset);
29 fclose(instruction_stream);
30 }
31 char guard[1000];
32 memset(guard,0,sizeof(guard));
33 {
34 FILE* guard_stream = fmemopen(guard, sizeof(guard), "w");
35 fprintf(guard_stream,"if(AC_INTERNAL_active_threads >> (lane_id + %d))",offset);
36 fclose(guard_stream);
37 }
38 fprintf(stream,
39 "%s shuffle_tmp = %s;"
40 "%s %s;"
41 ,tmp_type
42 ,shuffle_down
43 ,check_activeness ? guard : ""
44 ,op_instruction
45 );
46}
47
48void
49print_warp_reduction(FILE* stream, const int warp_size,const char* op_instruction, const bool check_activeness)
50{
51 print_shuffle_iteration(stream,"auto",warp_size/2,op_instruction,check_activeness);
52 for(int offset = warp_size/4; offset >= 1; offset /= 2)
53 print_shuffle_iteration(stream,"",offset,op_instruction,check_activeness);
54}
55const char*
57{
58 return
59 op == REDUCE_SUM ? "val += shuffle_tmp" :
60 op == REDUCE_MIN ? "val = val > shuffle_tmp ? shuffle_tmp : val" :
61 op == REDUCE_MAX ? "val = val > shuffle_tmp ? val : shuffle_tmp" :
62 NULL;
63}
64const char*
66{
67 return
68 op == REDUCE_SUM ? "sum" :
69 op == REDUCE_MIN ? "min" :
70 op == REDUCE_MAX ? "max" :
71 NULL;
72}
static const Stream stream
Definition astaroth_device.h:187
ReduceOp
Definition vecs.h:8
@ REDUCE_MAX
Definition vecs.h:11
@ REDUCE_SUM
Definition vecs.h:12
@ REDUCE_MIN
Definition vecs.h:10
const char * get_op_instruction(const ReduceOp op)
Definition warp_reduce.h:56
const char * shuffle_instruction
Definition warp_reduce.h:8
void print_shuffle_down(FILE *stream, const int offset)
Definition warp_reduce.h:12
void print_warp_reduction(FILE *stream, const int warp_size, const char *op_instruction, const bool check_activeness)
Definition warp_reduce.h:49
const char * reduce_op_to_name(const ReduceOp op)
Definition warp_reduce.h:65
void print_shuffle_iteration(FILE *stream, const char *tmp_type, const int offset, const char *op_instruction, const bool check_activeness)
Definition warp_reduce.h:22