-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutilities_internal.cpp
185 lines (158 loc) · 5.19 KB
/
utilities_internal.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @file utilities_internal.cpp
* @author Daniel Nichols
* @version 0.1
* @date 2019-05-21
*
* @copyright Copyright (c) 2019
*/
#include "magmadnn/utilities_internal.h"
namespace magmadnn {
namespace internal {
int debugf(const char *fmt, ...) {
#if defined(DEBUG)
int bytes;
va_list args;
va_start(args, fmt);
bytes = vfprintf(stdout, fmt, args);
va_end(args);
return bytes;
#endif
return 0;
}
void print_vector(const std::vector<unsigned int> &vec, bool debug, char begin, char end, char delim) {
int (*print)(const char *, ...) = (debug) ? debugf : std::printf;
print("%c ", begin);
for (std::vector<unsigned int>::const_iterator it = vec.begin(); it != vec.end(); it++) {
print("%lu", *it);
if (it != vec.end() - 1) print("%c ", delim);
}
print(" %c\n", end);
}
#if defined(_TENSOR_IN_UTIL_)
template <typename T>
void print_tensor(const Tensor<T> &t, bool print_flat, bool debug, const char *begin, const char *end,
const char *delim) {
int (*print)(const char *, ...) = (debug) ? debugf : std::printf;
if (print_flat) {
unsigned int size = t.get_size();
print("%s", begin);
for (unsigned int i = 0; i < size; i++) {
print("%.5g%s", t.get(i), delim);
if (i != size) print(" ");
}
print("%s", end);
} else {
const std::vector<unsigned int> &axes = t.get_shape();
std::vector<unsigned int>::const_iterator vit;
unsigned int axis_size;
/* 4 x 2 x 3 */
/* {
{
{1, 2, 3},
}
}
*/
print("%s", begin);
for (vit = axes.begin(); vit != axes.end(); vit++) {
axis_size = *vit;
print("%llu%s", axis_size, delim);
}
print("%s", end);
}
}
template <typename T>
void print_compute_graph(op::Operation<T> *node, bool debug) {
std::set<op::Operation<T> *> visited;
std::deque<op::Operation<T> *> to_visit;
op::Operation<T> *cur;
int (*print)(const char *, ...);
typename std::vector<op::Operation<T> *>::const_iterator vit;
print = (debug) ? debugf : std::printf;
to_visit.push_back(node);
visited.insert(node);
while (!to_visit.empty()) {
cur = to_visit.front();
to_visit.pop_front();
print("Operation [%s]:\n", cur->to_string().c_str());
print("\tConsumers:");
std::vector<op::Operation<T> *> const &consumers = cur->get_consumers();
for (vit = consumers.begin(); vit != consumers.end(); vit++) {
print(" [%s]", (*vit)->to_string().c_str());
if (visited.find((*vit)) == visited.end()) {
to_visit.push_back(*vit);
visited.insert((*vit));
}
}
print("\n\tInputs:");
std::vector<op::Operation<T> *> const &inputs = cur->get_inputs();
for (vit = inputs.begin(); vit != inputs.end(); vit++) {
print(" [%s]", (*vit)->to_string().c_str());
if (visited.find((*vit)) == visited.end()) {
to_visit.push_back(*vit);
visited.insert((*vit));
}
}
print("\n");
}
}
template void print_compute_graph(op::Operation<int> *node, bool debug);
template void print_compute_graph(op::Operation<float> *node, bool debug);
template void print_compute_graph(op::Operation<double> *node, bool debug);
/** Resets the compute graph.
* @tparam T
* @param node
*/
template <typename T>
void reset_compute_graph(op::Operation<T> *node) {
std::set<op::Operation<T> *> visited;
std::deque<op::Operation<T> *> to_visit;
op::Operation<T> *cur;
typename std::vector<op::Operation<T> *>::const_iterator vit;
to_visit.push_back(node);
visited.insert(node);
while (!to_visit.empty()) {
cur = to_visit.front();
to_visit.pop_front();
/* reset this Operation */
cur->reset();
std::vector<op::Operation<T> *> const &consumers = cur->get_consumers();
for (vit = consumers.begin(); vit != consumers.end(); vit++) {
if (visited.find((*vit)) == visited.end()) {
to_visit.push_back(*vit);
visited.insert((*vit));
}
}
std::vector<op::Operation<T> *> const &inputs = cur->get_inputs();
for (vit = inputs.begin(); vit != inputs.end(); vit++) {
if (visited.find((*vit)) == visited.end()) {
to_visit.push_back(*vit);
visited.insert((*vit));
}
}
}
}
template void reset_compute_graph(op::Operation<int> *node);
template void reset_compute_graph(op::Operation<float> *node);
template void reset_compute_graph(op::Operation<double> *node);
#endif
#if defined(MAGMADNN_HAVE_CUDA)
template <typename T>
cudnnDataType_t get_cudnn_data_type(T v) {
return CUDNN_DATA_FLOAT; /* default */
}
template <>
cudnnDataType_t get_cudnn_data_type(int v) {
return CUDNN_DATA_INT32;
}
template <>
cudnnDataType_t get_cudnn_data_type(float v) {
return CUDNN_DATA_FLOAT;
}
template <>
cudnnDataType_t get_cudnn_data_type(double v) {
return CUDNN_DATA_DOUBLE;
}
#endif
} // namespace internal
} // namespace magmadnn