-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlambda.cpp
66 lines (54 loc) · 1.74 KB
/
lambda.cpp
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
#include <stdio.h>
#include <math.h>
#include <hip/hip_runtime.h>
/* Blocksize is small because we are printing from all threads */
#define BLOCKSIZE 4
/* CPU loop execution */
template <typename Lambda>
void cpuKernel(Lambda lambda, const int loop_size) {
for(int i = 0; i < loop_size; i++){
lambda(i);
}
}
/* GPU loop execution */
template <typename Lambda>
__global__ void gpuKernel(Lambda lambda, const int loop_size)
{
const int i = blockIdx.x * blockDim.x + threadIdx.x;
if(i < loop_size)
{
lambda(i);
}
}
/* Check if this function is running on CPU or GPU */
__host__ __device__ void helloFromThread(const int i) {
#ifdef __HIP_DEVICE_COMPILE__ // If running on GPU
printf("Hello from GPU! I'm thread number %d\n", i);
#else // If running on CPU
printf("Hello from CPU! I'm thread number %d\n", i);
#endif
}
/* The main function */
int main()
{
// Set the problem dimensions
const int loop_size = BLOCKSIZE;
const int blocksize = BLOCKSIZE;
const int gridsize = (loop_size - 1 + blocksize) / blocksize;
// Define lambda1 function with 1 integer argument,
// the lamba must call helloFromThread with that argument
# error put the first lambda funtion definition here
// Run lambda1 on the CPU device
cpuKernel(lambda1, loop_size);
// Run lambda1 on the GPU device
gpuKernel<<<gridsize, blocksize>>>(lambda1, loop_size);
hipStreamSynchronize(0);
// Store value of pi in pi
double pi = M_PI;
// Define lambda2 that captures pi (use [=] to capture by value),
// and prints out the results for i * pi from each thread
# error put the second lambda funtion definition here
// Run lambda2 on the GPU device
gpuKernel<<<gridsize, blocksize>>>(lambda2, loop_size);
hipStreamSynchronize(0);
}