Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/openaccel-lab/blob/main/controls/scan_exclusive/kernel.cu # openaccel-lab aien-dev/openaccel-lab Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `openaccel-lab / controls / scan_exclusive / kernel.cu` 66 lines ยท 2097 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // CONTROL (deliberately wrong): scans x shifted right by one, i.e. the EXCLUSIVE prefix sum. 3 // Launch 1: each block of 256 scans its slice in shared memory (Hillis-Steele), writes the 4 // block-local inclusive prefix to y and the block total to bsum[g]. 5 // Launch 2: one block scans bsum in place (chunks of 256 with a running carry). 6 // Launch 3: every element of block g > 0 adds bsum[g - 1]. 7 #include 8 9 #define OAL_B 256 10 11 __global__ void scan_blk(const int *x, int *y, int *bsum, long long n) 12 { 13 __shared__ int s[OAL_B]; 14 int l = threadIdx.x; 15 long long i = (long long)blockIdx.x * OAL_B + l; 16 s[l] = (i >= 1 && i < n) ? x[i - 1] : 0; 17 __syncthreads(); 18 for (int off = 1; off < OAL_B; off <<= 1) { 19 int t = l >= off ? s[l - off] : 0; 20 __syncthreads(); 21 s[l] += t; 22 __syncthreads(); 23 } 24 if (i < n) 25 y[i] = s[l]; 26 if (l == OAL_B - 1) 27 bsum[blockIdx.x] = s[l]; 28 } 29 30 __global__ void scan_sums(int *bsum, long long nb) 31 { 32 __shared__ int s[OAL_B]; 33 int l = threadIdx.x; 34 int carry = 0; 35 for (long long base = 0; base < nb; base += OAL_B) { 36 long long j = base + l; 37 s[l] = j < nb ? bsum[j] : 0; 38 __syncthreads(); 39 for (int off = 1; off < OAL_B; off <<= 1) { 40 int t = l >= off ? s[l - off] : 0; 41 __syncthreads(); 42 s[l] += t; 43 __syncthreads(); 44 } 45 if (j < nb) 46 bsum[j] = s[l] + carry; 47 carry += s[OAL_B - 1]; 48 __syncthreads(); 49 } 50 } 51 52 __global__ void scan_add(int *y, const int *bsum, long long n) 53 { 54 long long i = (long long)blockIdx.x * OAL_B + threadIdx.x; 55 if (blockIdx.x > 0 && i < n) 56 y[i] += bsum[blockIdx.x - 1]; 57 } 58 59 void oal_launch(const long long *p, void *const *buf) 60 { 61 long long n = p[0], nb = p[1]; 62 int *x = (int *)buf[0], *y = (int *)buf[1], *bsum = (int *)buf[2]; 63 scan_blk<<<(unsigned)nb, OAL_B>>>(x, y, bsum, n); 64 scan_sums<<<1, OAL_B>>>(bsum, nb); 65 scan_add<<<(unsigned)nb, OAL_B>>>(y, bsum, n); 66 }