-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.h
51 lines (44 loc) · 1.37 KB
/
misc.h
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
#ifndef MISC_H
#define MISC_H
#include "cublas_v2.h"
#include "cusparse_v2.h"
/*
* misc.h
*
* This file contains miscellaneous helper functions.
*
* @author Simon Schoelly
*/
int divide_and_round_up(int const n, int const d) {
if (n % d == 0) {
return n/d;
}
return (n/d + 1);
}
template<class FT>
void print_device_array(FT const * const array, size_t const num_elements, char const * const array_symbol) {
std::cout << array_symbol << " (" << num_elements << ") elements:";
FT *host_array = new FT[num_elements];
cudaMemcpy(host_array, array, num_elements * sizeof(FT), cudaMemcpyDeviceToHost);
for (int i = 0; i <num_elements; ++i) {
std::cout << " " << host_array[i];
}
std::cout << std::endl;
delete[] host_array;
}
template<class T>
__global__ void device_memset_kernel(T * const devPtr, T const value, size_t const count, size_t offset) {
size_t const tid = threadIdx.x + blockDim.x * blockIdx.x;
if (tid >= count) {
return;
}
devPtr[tid + offset] = value;
}
template<class T>
void device_memset(T * const devPtr, T const value, size_t const count, size_t offset=0) {
if (count <= 0) {
return;
}
device_memset_kernel<T><<<divide_and_round_up(count, 32), 32>>>(devPtr, value, count, offset);
}
#endif