-
Notifications
You must be signed in to change notification settings - Fork 6
/
DeviceViewable.h
159 lines (137 loc) · 3.55 KB
/
DeviceViewable.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
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
#ifndef _DeviceViewable_h
#define _DeviceViewable_h
#include <string>
#include <vector>
#include <cstdint>
#include <memory.h>
typedef std::vector<char> ViewBuf;
// root class of all device-viewable objects
class DeviceViewable
{
public:
DeviceViewable(){}
virtual ~DeviceViewable(){}
virtual ViewBuf view() const = 0;
const std::string& name_view_cls() const { return m_name_view_cls; }
protected:
std::string m_name_view_cls;
};
struct CapturedDeviceViewable
{
const char* obj_name;
const DeviceViewable* obj;
};
class SomeDeviceViewable : public DeviceViewable
{
public:
SomeDeviceViewable(const char* name_view_cls, const void* data_view = "", size_t size_view = 1)
{
m_name_view_cls = name_view_cls;
m_view_buf.resize(size_view);
memcpy(m_view_buf.data(), data_view, size_view);
}
virtual ViewBuf view() const
{
return m_view_buf;
}
private:
ViewBuf m_view_buf;
};
#define DECLAR_DV_BASIC(clsname, type)\
class clsname : public SomeDeviceViewable\
{\
public:\
clsname(type in) : SomeDeviceViewable(#type, &in, sizeof(type)) {}\
};
DECLAR_DV_BASIC(DVChar, char)
DECLAR_DV_BASIC(DVSChar, signed char)
DECLAR_DV_BASIC(DVUChar, unsigned char)
DECLAR_DV_BASIC(DVShort, short)
DECLAR_DV_BASIC(DVUShort, unsigned short)
DECLAR_DV_BASIC(DVInt, int)
DECLAR_DV_BASIC(DVUInt, unsigned int)
DECLAR_DV_BASIC(DVLong, long)
DECLAR_DV_BASIC(DVULong, unsigned long)
DECLAR_DV_BASIC(DVLongLong, long long)
DECLAR_DV_BASIC(DVULongLong, unsigned long long)
DECLAR_DV_BASIC(DVFloat, float)
DECLAR_DV_BASIC(DVDouble, double)
DECLAR_DV_BASIC(DVBool, bool)
DECLAR_DV_BASIC(DVInt8, int8_t)
DECLAR_DV_BASIC(DVUInt8, uint8_t)
DECLAR_DV_BASIC(DVInt16, int16_t)
DECLAR_DV_BASIC(DVUInt16, uint16_t)
DECLAR_DV_BASIC(DVInt32, int32_t)
DECLAR_DV_BASIC(DVUInt32, uint32_t)
DECLAR_DV_BASIC(DVInt64, int64_t)
DECLAR_DV_BASIC(DVUInt64, uint64_t)
DECLAR_DV_BASIC(DVSizeT, size_t)
class DVComplex64 : public DeviceViewable
{
public:
DVComplex64(float real, float imag)
{
m_name_view_cls = "cuFloatComplex";
m_real = real;
m_imag = imag;
}
virtual ViewBuf view() const
{
ViewBuf buf(sizeof(float) * 2);
*(float*)&buf[0] = m_real;
*(float*)&buf[sizeof(float)] = m_imag;
return buf;
}
private:
float m_real;
float m_imag;
};
class DVComplex128 : public DeviceViewable
{
public:
DVComplex128(double real, double imag)
{
m_name_view_cls = "cuDoubleComplex";
m_real = real;
m_imag = imag;
}
virtual ViewBuf view() const
{
ViewBuf buf(sizeof(double) * 2);
*(double*)&buf[0] = m_real;
*(double*)&buf[sizeof(double)] = m_imag;
return buf;
}
private:
double m_real;
double m_imag;
};
inline DeviceViewable* dv_from_viewbuf(const ViewBuf& buf, const char* type)
{
std::string s_type = type;
if (s_type == "int8_t")
return new DVInt8(*(int8_t*)buf.data());
else if (s_type == "uint8_t")
return new DVUInt8(*(uint8_t*)buf.data());
else if (s_type == "int16_t")
return new DVInt16(*(int16_t*)buf.data());
else if (s_type == "uint16_t")
return new DVUInt16(*(uint16_t*)buf.data());
else if (s_type == "int32_t")
return new DVInt32(*(int32_t*)buf.data());
else if (s_type == "uint32_t")
return new DVUInt32(*(uint32_t*)buf.data());
else if (s_type == "int64_t")
return new DVInt64(*(int64_t*)buf.data());
else if (s_type == "uint64_t")
return new DVUInt64(*(uint64_t*)buf.data());
else if (s_type == "float")
return new DVFloat(*(float*)buf.data());
else if (s_type == "double")
return new DVDouble(*(double*)buf.data());
else if (s_type == "bool")
return new DVBool(*(bool*)buf.data());
else
return new SomeDeviceViewable(type, buf.data(), buf.size());
}
#endif