-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhamr_cuda_print_impl.h
65 lines (57 loc) · 1.66 KB
/
hamr_cuda_print_impl.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef hamr_cuda_print_impl_h
#define hamr_cuda_print_impl_h
#include "hamr_config.h"
#include "hamr_env.h"
#include "hamr_stream.h"
#if defined(HAMR_ENABLE_CUDA)
#include "hamr_cuda_kernels.h"
#include "hamr_cuda_launch.h"
#include <cuda.h>
#include <cuda_runtime.h>
#endif
#include <iostream>
/// heterogeneous accelerator memory resource
namespace hamr
{
/** prints an array on the GPU
* @param[in] vals an array of n elements accessible in CUDA
* @param[in] n_elem the length of the array
* @returns 0 if there were no errors
*/
template <typename T>
int cuda_print(const hamr::stream &strm, T *vals, size_t n_elem)
{
#if !defined(HAMR_ENABLE_CUDA)
(void) vals;
(void) n_elem;
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" print_cuda failed because CUDA is not enabled." << std::endl;
return -1;
#else
// get launch parameters
int device_id = -1;
dim3 block_grid;
int n_blocks = 0;
dim3 thread_grid = 0;
if (hamr::partition_thread_blocks(device_id, n_elem, 8, block_grid,
n_blocks, thread_grid))
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to determine launch properties." << std::endl;
return -1;
}
// invoke the print kernel
cudaError_t ierr = cudaSuccess;
hamr::cuda_kernels::print<<<block_grid, thread_grid, 0, strm>>>(vals, n_elem);
if ((ierr = cudaGetLastError()) != cudaSuccess)
{
std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] ERROR:"
" Failed to launch the print kernel. "
<< cudaGetErrorString(ierr) << std::endl;
return -1;
}
return 0;
#endif
}
}
#endif