This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
CudaDebugUtils.cuh
145 lines (128 loc) · 7.11 KB
/
CudaDebugUtils.cuh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <stdio.h> // for printf
namespace facebook { namespace cuda {
// Collection of CUDA debugging utilities
//
// PRINT_DEBUG
// These are macros since we need to concatenate fmt strings and only
// each printf() is atomic, and doing work in device land to
// concatenate strings is too much
//
#ifdef ENABLE_CUDA_DEBUG
#define PRINT_DEBUG(FMT, ...) { \
if (gridDim.y == 1 && gridDim.z == 1) { \
if (blockDim.y == 1 && blockDim.z == 1) { \
printf("(b%d t%d) " FMT "\n", \
blockIdx.x, \
threadIdx.x, __VA_ARGS__); \
} else if (blockDim.z == 1) { \
printf("(b%d t%d/%d) " FMT "\n", \
blockIdx.x, \
threadIdx.x, threadIdx.y, __VA_ARGS__); \
} else { \
printf("(b%d t%d/%d/%d) " FMT "\n", \
blockIdx.x, \
threadIdx.x, threadIdx.y, threadIdx.z, __VA_ARGS__); \
} \
} else if (gridDim.z == 1) { \
if (blockDim.y == 1 && blockDim.z == 1) { \
printf("(b%d/%d t%d) " FMT "\n", \
blockIdx.x, blockIdx.y, \
threadIdx.x, __VA_ARGS__); \
} else if (blockDim.z == 1) { \
printf("(b%d/%d t%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, \
threadIdx.x, threadIdx.y, __VA_ARGS__); \
} else { \
printf("(b%d/%d t%d/%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, \
threadIdx.x, threadIdx.y, threadIdx.z, __VA_ARGS__); \
} \
} else { \
if (blockDim.y == 1 && blockDim.z == 1) { \
printf("(b%d/%d/%d t%d) " FMT "\n", \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x, __VA_ARGS__); \
} else if (blockDim.z == 1) { \
printf("(b%d/%d/%d t%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x, threadIdx.y, __VA_ARGS__); \
} else { \
printf("(b%d/%d/%d t%d/%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x, threadIdx.y, threadIdx.z, __VA_ARGS__); \
} \
} \
}
#define PRINT_DEBUG0(FMT) { \
if (gridDim.y == 1 && gridDim.z == 1) { \
if (blockDim.y == 1 && blockDim.z == 1) { \
printf("(b%d t%d) " FMT "\n", \
blockIdx.x, \
threadIdx.x); \
} else if (blockDim.z == 1) { \
printf("(b%d t%d/%d) " FMT "\n", \
blockIdx.x, \
threadIdx.x, threadIdx.y); \
} else { \
printf("(b%d t%d/%d/%d) " FMT "\n", \
blockIdx.x, \
threadIdx.x, threadIdx.y, threadIdx.z); \
} \
} else if (gridDim.z == 1) { \
if (blockDim.y == 1 && blockDim.z == 1) { \
printf("(b%d/%d t%d) " FMT "\n", \
blockIdx.x, blockIdx.y, \
threadIdx.x); \
} else if (blockDim.z == 1) { \
printf("(b%d/%d t%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, \
threadIdx.x, threadIdx.y); \
} else { \
printf("(b%d/%d t%d/%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, \
threadIdx.x, threadIdx.y, threadIdx.z); \
} \
} else { \
if (blockDim.y == 1 && blockDim.z == 1) { \
printf("(b%d/%d/%d t%d) " FMT "\n", \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x); \
} else if (blockDim.z == 1) { \
printf("(b%d/%d/%d t%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x, threadIdx.y); \
} else { \
printf("(b%d/%d/%d t%d/%d/%d) " FMT "\n", \
blockIdx.x, blockIdx.y, blockIdx.z, \
threadIdx.x, threadIdx.y, threadIdx.z); \
} \
} \
}
//
// PRINT_UNIQUE_DEBUG
// Only prints once, on the first thread in the first block
//
__device__ __forceinline__
bool isFirstThread() {
return (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0 &&
blockIdx.x == 0 && blockIdx.y == 0 && blockIdx.z == 0);
}
#define PRINT_UNIQUE_DEBUG(FMT, ...) { \
if (isFirstThread()) { \
PRINT_DEBUG(FMT, __VA_ARGS__); \
} \
}
#define PRINT_UNIQUE_DEBUG0(FMT) { \
if (isFirstThread()) { \
PRINT_DEBUG0(FMT); \
} \
}
#else
#define PRINT_DEBUG(FMT, ...)
#define PRINT_DEBUG0(FMT)
#define PRINT_UNIQUE_DEBUG(FMT, ...)
#define PRINT_UNIQUE_DEBUG0(FMT)
#endif // ENABLE_CUDA_DEBUG
} } // namespace