-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexample_common.h
More file actions
109 lines (93 loc) · 3.15 KB
/
example_common.h
File metadata and controls
109 lines (93 loc) · 3.15 KB
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
#pragma once
#include <iostream>
#include <fstream>
#define example_desc(...) \
example example_guard__(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
constexpr const char* white = "\033[1,37m";
constexpr const char* yellow = "\033[1;33m";
constexpr const char* green = "\033[1;32m";
constexpr const char* blue = "\033[1;34m";
constexpr const char* cyan = "\033[1;36m";
constexpr const char* red = "\033[1;31m";
constexpr const char* magenta = "\033[1;35m";
constexpr const char* black = "\033[1;30m";
constexpr const char* darkwhite = "\033[0;37m";
constexpr const char* darkyellow = "\033[0;33m";
constexpr const char* darkgreen = "\033[0;32m";
constexpr const char* darkblue = "\033[0;34m";
constexpr const char* darkcyan = "\033[0;36m";
constexpr const char* darkred = "\033[0;31m";
constexpr const char* darkmagenta = "\033[0;35m";
constexpr const char* darkblack = "\033[0;30m";
constexpr const char* off = "\033[0;0m";
struct color
{
color(const char* c) { std::cout << c; }
~color() { std::cout << off; }
};
struct example
{
example(const std::string& n,
const std::string& file,
int line,
const std::string& description = "",
bool show_code = false)
{
std::cout << std::dec;
//get console width
std::string name = "muda example: " + n;
int w = 79;
std::cout << std::string(w, '=') << std::endl;
std::cout << "muda example: ";
{
color c(blue);
std::cout << n << std::endl;
}
{
color c(darkyellow);
std::cout << "> " << file << "(" << line << ")" << std::endl;
}
std::cout << "description: \n" << description << std::endl;
if(show_code)
{
std::cout << std::string(w, '-') << std::endl;
std::ifstream ifs(file);
if(ifs.is_open())
std::cout << ifs.rdbuf() << std::endl;
}
std::cout << std::string(w, '-') << std::endl;
{
color c(green);
std::cout << "output:" << std::endl;
}
}
~example()
{
std::cout << std::endl;
//std::cout << "\033[1;32m";
//std::cout << "==============================================================================="
// << std::endl;
//std::cout << "\033[0m";
}
};
// a dummy function, just let the kernel wait some clock cycles
// to make the kernel execution time long enough.
inline __device__ void some_work(size_t clock_cycles = 1e9)
{
clock_t start = clock();
clock_t now;
while(true)
{
now = clock();
clock_t cycles = now > start ? now - start : now + (0xffffffff - start);
if(cycles >= clock_cycles)
break;
}
}
inline void make_progress_bar(float progress, int bar = 77)
{
int w = std::round(progress * bar);
std::string done(w, '>');
std::string undone(bar - w, '=');
std::cout << "[" << done << undone << "]\r";
}