MUDA
Loading...
Searching...
No Matches
debug_log.h
1#pragma once
2#include <cstdlib>
3#include <cassert>
4#include <muda/tools/fuzzy.h>
5#include <muda/assert.h>
6#include <muda/print.h>
7#include <muda/muda_config.h>
8#include <muda/muda_def.h>
9
10#ifdef __CUDA_ARCH__
11#define MUDA_KERNEL_PRINT(fmt, ...) \
12 { \
13 if(muda::block_dim().y == 1 && muda::block_dim().z == 1) \
14 { \
15 ::muda::print("(%d|%d)-(%d|%d):" fmt "\n", \
16 muda::block_idx().x, \
17 muda::grid_dim().x, \
18 muda::thread_idx().x, \
19 muda::block_dim().x, \
20 ##__VA_ARGS__); \
21 } \
22 else if(muda::block_dim().z == 1) \
23 { \
24 ::muda::print("(%d|%d,%d|%d)-(%d|%d,%d|%d):" fmt "\n", \
25 muda::block_idx().x, \
26 muda::grid_dim().x, \
27 muda::block_idx().y, \
28 muda::grid_dim().y, \
29 muda::thread_idx().x, \
30 muda::block_dim().x, \
31 muda::thread_idx().y, \
32 muda::block_dim().y, \
33 ##__VA_ARGS__); \
34 } \
35 else \
36 { \
37 ::muda::print("(%d|%d,%d|%d,%d|%d)-(%d|%d,%d|%d,%d|%d):" fmt "\n", \
38 muda::block_idx().x, \
39 muda::grid_dim().x, \
40 muda::block_idx().y, \
41 muda::grid_dim().y, \
42 muda::block_idx().z, \
43 muda::grid_dim().z, \
44 muda::thread_idx().x, \
45 muda::block_dim().x, \
46 muda::thread_idx().y, \
47 muda::block_dim().y, \
48 muda::thread_idx().z, \
49 muda::block_dim().z, \
50 ##__VA_ARGS__); \
51 } \
52 }
53#else
54#define MUDA_KERNEL_PRINT(fmt, ...) \
55 { \
56 ::muda::print("(host):" fmt "\n", ##__VA_ARGS__); \
57 }
58#endif
59
60//when muda::TRAP_ON_ERROR == true trap the device
61#define MUDA_DEBUG_TRAP() \
62 { \
63 if constexpr(::muda::TRAP_ON_ERROR) \
64 ::muda::trap(); \
65 }
66
67// check whether (res == true), if not, print the error info (when muda::TRAP_ON_ERROR == true
68// trap the device)
69#define MUDA_KERNEL_ASSERT(res, fmt, ...) \
70 { \
71 if constexpr(::muda::RUNTIME_CHECK_ON) \
72 { \
73 if(!(res)) \
74 { \
75 MUDA_KERNEL_PRINT("%s(%d): %s:\n <assert> " #res " failed. " fmt, \
76 __FILE__, \
77 __LINE__, \
78 MUDA_FUNCTION_SIG, \
79 ##__VA_ARGS__); \
80 MUDA_DEBUG_TRAP(); \
81 } \
82 } \
83 }
84
85// check whether (res == true), if not, print the error info(never trap the device)
86#define MUDA_KERNEL_CHECK(res, fmt, ...) \
87 { \
88 if constexpr(::muda::RUNTIME_CHECK_ON) \
89 { \
90 if(!(res)) \
91 { \
92 MUDA_KERNEL_PRINT("%s(%d): %s:\n <check> " #res " failed. " fmt, \
93 __FILE__, \
94 __LINE__, \
95 MUDA_FUNCTION_SIG, \
96 ##__VA_ARGS__); \
97 } \
98 } \
99 }
100
101// print error info, and call muda_debug_trap()
102#define MUDA_KERNEL_ERROR(fmt, ...) \
103 { \
104 MUDA_KERNEL_PRINT("<error> " fmt, ##__VA_ARGS__); \
105 MUDA_DEBUG_TRAP(); \
106 }
107
108#define MUDA_KERNEL_ERROR_WITH_LOCATION(fmt, ...) \
109 { \
110 MUDA_KERNEL_PRINT("%s(%d): %s:\n <error> " fmt, __FILE__, __LINE__, MUDA_FUNCTION_SIG, ##__VA_ARGS__); \
111 MUDA_DEBUG_TRAP(); \
112 }
113
114// print warn info
115#define MUDA_KERNEL_WARN(fmt, ...) \
116 { \
117 MUDA_KERNEL_PRINT("<warn>" fmt, ##__VA_ARGS__); \
118 }
119
120#define MUDA_KERNEL_WARN_WITH_LOCATION(fmt, ...) \
121 { \
122 MUDA_KERNEL_PRINT("%s(%d): %s:\n <warn>" fmt, __FILE__, __LINE__, MUDA_FUNCTION_SIG, ##__VA_ARGS__); \
123 }
124
125
126#define MUDA_ASSERT(res, fmt, ...) MUDA_KERNEL_ASSERT(res, fmt, ##__VA_ARGS__)
127
128#define MUDA_ERROR(fmt, ...) MUDA_KERNEL_ERROR(fmt, ##__VA_ARGS__)
129
130#define MUDA_ERROR_WITH_LOCATION(fmt, ...) MUDA_KERNEL_ERROR_WITH_LOCATION(fmt, ##__VA_ARGS__)