File: hellocuda.cu

package info (click to toggle)
sleef 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,564 kB
  • sloc: ansic: 49,154; cpp: 6,095; makefile: 38
file content (45 lines) | stat: -rw-r--r-- 945 bytes parent folder | download | duplicates (3)
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
#include <iostream>
#include <math.h>

#include "sleefinline_cuda.h"

// Based on the tutorial code at https://developer.nvidia.com/blog/even-easier-introduction-cuda/

__global__ void pow_gpu(int n, double *r, double *x, double *y)
{
  int index = threadIdx.x, stride = blockDim.x;

  for (int i = index; i < n; i += stride)
    r[i] = Sleef_powd1_u10cuda(x[i], y[i]);
}

int main(void)
{
  int N = 1 << 20;

  double *r, *x, *y;
  cudaMallocManaged(&r, N*sizeof(double));
  cudaMallocManaged(&x, N*sizeof(double));
  cudaMallocManaged(&y, N*sizeof(double));

  for (int i = 0; i < N; i++) {
    r[i] = 0.0;
    x[i] = 1.00001;
    y[i] = i;
  }

  pow_gpu<<<1, 256>>>(N, r, x, y);

  cudaDeviceSynchronize();

  double maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = fmax(maxError, fabs(r[i]-pow(x[i], y[i])));
  std::cout << "Max error: " << maxError << std::endl;

  cudaFree(y);
  cudaFree(x);
  cudaFree(r);
  
  return 0;
}