-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduction.hh
37 lines (35 loc) · 1.61 KB
/
reduction.hh
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
#pragma once
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <iostream>
#include <random>
#include <chrono>
#include <cassert>
#include <algorithm>
#include <bitset>
#define CUDA_CHECK(condition) \
do { \
cudaError_t cuda_result = condition; \
if (cuda_result != cudaSuccess) { \
printf("%s on line %i in %s returned: %s(code:%i)\n", #condition, \
__LINE__, __FILE__, cudaGetErrorString(cuda_result), \
cuda_result); \
throw std::runtime_error( \
std::string(#condition) + " in file " + __FILE__ \
+ " on line " + std::to_string(__LINE__) + \
" returned: " + cudaGetErrorString(cuda_result)); \
} \
} while (0)
__inline__ float *dev_prob_generator(){
extern const int num_elem;
float *dev_rand;
float *host_rand = new float[num_elem];
std::srand(std::time(nullptr));
for (int i = 0; i < num_elem; i++) {
host_rand[i] = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}
cudaMalloc(&dev_rand, sizeof(float)*num_elem);
cudaMemcpy(dev_rand, host_rand, sizeof(float)*num_elem, cudaMemcpyHostToDevice);
return dev_rand;
}