-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDist_V1.cu
More file actions
36 lines (28 loc) · 761 Bytes
/
Dist_V1.cu
File metadata and controls
36 lines (28 loc) · 761 Bytes
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
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <math.h>
#define N 64
#define TPB 32
__device__ float scale(int i, int n) {
return ((float)i) / (n - 1);
}
__device__ float distance(float x1, float x2) {
return sqrt((x2 - x1)*(x2 - x1));
}
__global__ void distanceKernel(float *d_out, float ref, int len)
{
const int i = blockIdx.x*blockDim.x + threadIdx.x;
const float x = scale(i, len);
d_out[i] = distance(x, ref);
printf("i=%2d: dist from %f to %f is %f.\n", i, ref, x, d_out[i]);
}
int main()
{
const float ref = 0.5f;
float *d_out = 0;
cudaMalloc(&d_out, N * sizeof(float));
distanceKernel << <N / TPB, TPB >> > (d_out, ref, N);
cudaFree(d_out);
return 0;
}