summaryrefslogtreecommitdiffstats
path: root/functionTest.cu
blob: eac1ab6d67144465f11f35d9ffd918a5edb71e25 (plain)
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
#include <cuda.h>
#include <stdio.h>
#include <stdlib.h>

template <int nthreads>
__device__ void f2(int idx, float x_s[nthreads])
{
    __shared__ float y_s[nthreads];
    int kk = 1;
    y_s[idx] = x_s[idx];
    __syncthreads();
    if(idx>0) {
        printf("%i %i %.2e %.2e %.2e %.2e\n", kk, idx, x_s[idx], x_s[idx-1], y_s[idx], y_s[idx-1]);
    } else {
        printf("%i %i %.2e %.2e\n", kk, idx, x_s[idx], y_s[idx]);
    }
}

template <int nthreads>
__global__ void f1(float *x_d)
{   
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    __shared__ float x_s[nthreads];
    x_s[idx] = x_d[idx];
    __syncthreads();
    //printf("%i %.2e %.2e\n", idx, x_d[idx], x_s[idx]);
    f2 <nthreads> (idx, x_s);
}

int main()
{
    float *x_h, *x_d;
    int nx = 4;

    // allocate, copy
    cudaSetDevice(0);
    x_h = (float *) malloc(sizeof(float)*nx);
    for(int ii=0; ii<nx; ii++) {
        x_h[ii] = ii;
        printf("%i %.2e\n", ii, x_h[ii]);
    }
    printf("\n");
    cudaMalloc((void**) &x_d, sizeof(float)*nx);
    cudaMemcpy(x_d, x_h, sizeof(float)*nx, cudaMemcpyHostToDevice);

    // run kernel
    f1 <4> <<< 1, 4 >>> (x_d);

    // free
    cudaFree(x_d);
    free(x_h);
}