interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

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 <cstdio>
3#include <cuda_runtime.h>
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
10int 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}