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/aienos.com/blob/main/om/lab-page/axpy.cu # aienos.com Source of www.aienos.com, served from my own hardware 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 `aienos.com / om / lab-page / axpy.cu` 24 lines ยท 735 bytes Raw Blame History 1 // The same kernel in CUDA C: y[i] = a*x[i] + y[i], one element per thread. 2 #include 3 #include 4 5 __global__ void axpy(long long a, const long long *x, long long *y, long long n) { 6 long long i = (long long)blockIdx.x * blockDim.x + threadIdx.x; 7 if (i < n) y[i] = a * x[i] + y[i]; 8 } 9 10 int main() { 11 const long long n = 1 << 20, a = 3; 12 long long *x, *y; 13 cudaMallocManaged(&x, n * sizeof *x); 14 cudaMallocManaged(&y, n * sizeof *y); 15 for (long long i = 0; i < n; i++) { x[i] = i; y[i] = 2 * i + 1; } 16 axpy<<<(n + 255) / 256, 256>>>(a, x, y, n); 17 cudaDeviceSynchronize(); 18 long long s = 0; 19 for (long long i = 0; i < n; i++) s += y[i]; 20 printf("%lld\n", s); 21 cudaFree(x); 22 cudaFree(y); 23 return 0; 24 }