about summary refs log tree commit diff
path: root/pkgs/development/cuda-modules/saxpy/saxpy.cu
blob: 912a6d1647b145ca1d7891b4e383cff00ccb6bac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <vector>

#include <stdio.h>

static inline void check(cudaError_t err, const char *context) {
  if (err != cudaSuccess) {
    fprintf(stderr, "CUDA error at %s: %s\n", context, cudaGetErrorString(err));
    std::exit(EXIT_FAILURE);
  }
}

#define CHECK(x) check(x, #x)

__global__ void saxpy(int n, float a, float *x, float *y) {
  int i = blockIdx.x * blockDim.x + threadIdx.x;
  if (i < n)
    y[i] = a * x[i] + y[i];
}

int main(void) {
  setbuf(stderr, NULL);
  fprintf(stderr, "Start\n");

  int rtVersion, driverVersion;
  CHECK(cudaRuntimeGetVersion(&rtVersion));
  CHECK(cudaDriverGetVersion(&driverVersion));

  fprintf(stderr, "Runtime version: %d\n", rtVersion);
  fprintf(stderr, "Driver version: %d\n", driverVersion);

  constexpr int N = 1 << 10;

  std::vector<float> xHost(N), yHost(N);
  for (int i = 0; i < N; i++) {
    xHost[i] = 1.0f;
    yHost[i] = 2.0f;
  }

  fprintf(stderr, "Host memory initialized, copying to the device\n");
  fflush(stderr);

  float *xDevice, *yDevice;
  CHECK(cudaMalloc(&xDevice, N * sizeof(float)));
  CHECK(cudaMalloc(&yDevice, N * sizeof(float)));

  CHECK(cudaMemcpy(xDevice, xHost.data(), N * sizeof(float),
                   cudaMemcpyHostToDevice));
  CHECK(cudaMemcpy(yDevice, yHost.data(), N * sizeof(float),
                   cudaMemcpyHostToDevice));
  fprintf(stderr, "Scheduled a cudaMemcpy, calling the kernel\n");

  saxpy<<<(N + 255) / 256, 256>>>(N, 2.0f, xDevice, yDevice);
  fprintf(stderr, "Scheduled a kernel call\n");
  CHECK(cudaGetLastError());

  CHECK(cudaMemcpy(yHost.data(), yDevice, N * sizeof(float),
                   cudaMemcpyDeviceToHost));

  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = max(maxError, abs(yHost[i] - 4.0f));
  fprintf(stderr, "Max error: %f\n", maxError);

  CHECK(cudaFree(xDevice));
  CHECK(cudaFree(yDevice));
}