| 1 |
|
|---|
| 2 | #ifndef GROUP_SIZE
|
|---|
| 3 | #define GROUP_SIZE (64)
|
|---|
| 4 | #endif
|
|---|
| 5 |
|
|---|
| 6 | #ifndef OPERATIONS
|
|---|
| 7 | #define OPERATIONS (1)
|
|---|
| 8 | #endif
|
|---|
| 9 |
|
|---|
| 10 | ////////////////////////////////////////////////////////////////////////////////////////////////////
|
|---|
| 11 |
|
|---|
| 12 | #define LOAD_GLOBAL_I1(s, i) \
|
|---|
| 13 | ((__global const int*)(s))[(size_t)(i)]
|
|---|
| 14 |
|
|---|
| 15 | #define STORE_GLOBAL_I1(s, i, v) \
|
|---|
| 16 | ((__global int*)(s))[(size_t)(i)] = (v)
|
|---|
| 17 |
|
|---|
| 18 | ////////////////////////////////////////////////////////////////////////////////////////////////////
|
|---|
| 19 |
|
|---|
| 20 | #define LOAD_LOCAL_I1(s, i) \
|
|---|
| 21 | ((__local int*)(s))[(size_t)(i)]
|
|---|
| 22 |
|
|---|
| 23 | #define STORE_LOCAL_I1(s, i, v) \
|
|---|
| 24 | ((__local int*)(s))[(size_t)(i)] = (v)
|
|---|
| 25 |
|
|---|
| 26 | #define ACCUM_LOCAL_I1(s, i, j) \
|
|---|
| 27 | { \
|
|---|
| 28 | int x = ((__local int*)(s))[(size_t)(i)]; \
|
|---|
| 29 | int y = ((__local int*)(s))[(size_t)(j)]; \
|
|---|
| 30 | ((__local int*)(s))[(size_t)(i)] = (x + y); \
|
|---|
| 31 | }
|
|---|
| 32 | ////////////////////////////////////////////////////////////////////////////////////////////////////
|
|---|
| 33 | /*
|
|---|
| 34 | __global int *output,
|
|---|
| 35 | __global const int *input,
|
|---|
| 36 | __local int *shared,
|
|---|
| 37 | const unsigned int n)
|
|---|
| 38 | */
|
|---|
| 39 | void reduce(
|
|---|
| 40 | int *output,
|
|---|
| 41 | const int *input,
|
|---|
| 42 | int *shared,
|
|---|
| 43 | const unsigned int n)
|
|---|
| 44 | {
|
|---|
| 45 | const int zero = 0.0f;
|
|---|
| 46 | const unsigned int group_id = get_global_id(0) / get_local_size(0);
|
|---|
| 47 | const unsigned int group_size = GROUP_SIZE;
|
|---|
| 48 | const unsigned int group_stride = 2 * group_size;
|
|---|
| 49 | const size_t local_stride = group_stride * group_size;
|
|---|
| 50 |
|
|---|
| 51 | unsigned int op = 0;
|
|---|
| 52 | unsigned int last = OPERATIONS - 1;
|
|---|
| 53 | for(op = 0; op < OPERATIONS; op++)
|
|---|
| 54 | {
|
|---|
| 55 | const unsigned int offset = (last - op);
|
|---|
| 56 | const size_t local_id = get_local_id(0) + offset;
|
|---|
| 57 |
|
|---|
| 58 | STORE_LOCAL_I1(shared, local_id, zero);
|
|---|
| 59 |
|
|---|
| 60 | size_t i = group_id * group_stride + local_id;
|
|---|
| 61 | while (i < n)
|
|---|
| 62 | {
|
|---|
| 63 | int a = LOAD_GLOBAL_I1(input, i);
|
|---|
| 64 | int b = LOAD_GLOBAL_I1(input, i + group_size);
|
|---|
| 65 | int s = LOAD_LOCAL_I1(shared, local_id);
|
|---|
| 66 | STORE_LOCAL_I1(shared, local_id, (a + b + s));
|
|---|
| 67 | i += local_stride;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | #if (GROUP_SIZE >= 2)
|
|---|
| 71 | if (local_id < 1) { ACCUM_LOCAL_I1(shared, local_id, local_id + 1); }
|
|---|
| 72 | #endif
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | if (get_local_id(0) == 0)
|
|---|
| 78 | {
|
|---|
| 79 | int v = LOAD_LOCAL_I1(shared, 0);
|
|---|
| 80 | STORE_GLOBAL_I1(output, group_id, v);
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|