-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComprEx.py
More file actions
199 lines (175 loc) · 7.66 KB
/
ComprEx.py
File metadata and controls
199 lines (175 loc) · 7.66 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import ctypes
import os
import numpy as np
import numpy.ctypeslib as npct
script_dir = os.path.dirname(__file__)
lib_dir = os.path.join(script_dir,'./lib/libPyGPI.so')
data_t = ctypes.c_float
np_data_t_p = npct.ndpointer(dtype=np.float32, ndim=1, flags='CONTIGUOUS')
libcomprex = npct.load_library(lib_dir, ".")
######################
# ComprEx
######################
# ComprEx<data_t>* Comprex_new(GaspiEnvironment* gaspi_env, int size, float size_factor)
libcomprex.Comprex_new.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_float]
libcomprex.Comprex_new.restype = ctypes.c_void_p
# void Comprex_del(ComprEx<data_t>* self)
libcomprex.Comprex_del.argtypes = [ctypes.c_void_p]
libcomprex.Comprex_del.restype = ctypes.c_void_p
# void setThreshold(ComprEx<data_t>* self, const ThresholdFunction<data_t>* threshold)
libcomprex.Comprex_setThreshold.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
libcomprex.Comprex_setThreshold.restype = ctypes.c_void_p
# void setCompressor(ComprEx<data_t>* self, const Compressor<data_t>* compressor)
libcomprex.Comprex_setCompressor.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
libcomprex.Comprex_setCompressor.restype = ctypes.c_void_p
# void resetRests(ComprEx<data_t>* self)
libcomprex.Comprex_resetRests.argtypes = [ctypes.c_void_p]
libcomprex.Comprex_resetRests.restype = ctypes.c_void_p
# void flushRests(ComprEx<data_t>* self)
libcomprex.Comprex_flushRests.argtypes = [ctypes.c_void_p]
libcomprex.Comprex_flushRests.restype = ctypes.c_void_p
# data_t* getRests(ComprEx<data_t>* self)
libcomprex.Comprex_getRests.argtypes = [ctypes.c_void_p, np_data_t_p, ctypes.c_int]
libcomprex.Comprex_getRests.restype = ctypes.c_void_p
# void writeRemote(ComprEx<data_t>* self, const data_t* vector, int size)
libcomprex.Comprex_writeRemote.argtypes = [ctypes.c_void_p, np_data_t_p, ctypes.c_int]
libcomprex.Comprex_writeRemote.restype = ctypes.c_void_p
# void readRemote(ComprEx<data_t>* self, data_t* vector, int size)
libcomprex.Comprex_readRemote.argtypes = [ctypes.c_void_p, np_data_t_p, ctypes.c_int]
libcomprex.Comprex_readRemote.restype = ctypes.c_void_p
# void Comprex_connectTo(ComprEx<data_t>* self, int srcRank, int targRank, int tag)
libcomprex.Comprex_connectTo.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int]
libcomprex.Comprex_connectTo.restype = ctypes.c_void_p
# void Comprex_connectTx(ComprEx<data_t>* self, int targRank, int tag)
libcomprex.Comprex_connectTx.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
libcomprex.Comprex_connectTx.restype = ctypes.c_void_p
# void Comprex_connectRx(ComprEx<data_t>* self, int srcRank, int tag)
libcomprex.Comprex_connectRx.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
libcomprex.Comprex_connectRx.restype = ctypes.c_void_p
###########################
# Thresholds
###########################
# ThresholdNone
libcomprex.ThresholdNone_new.argtypes = []
libcomprex.ThresholdNone_new.restype = ctypes.c_void_p
libcomprex.ThresholdNone_del.argtypes = [ctypes.c_void_p]
libcomprex.ThresholdNone_del.restype = ctypes.c_void_p
# ThresholdConst
libcomprex.ThresholdConst_new.argtypes = [data_t]
libcomprex.ThresholdConst_new.restype = ctypes.c_void_p
libcomprex.ThresholdConst_del.argtypes = [ctypes.c_void_p]
libcomprex.ThresholdConst_del.restype = ctypes.c_void_p
libcomprex.ThresholdConst_getThreshold.argtypes = [ctypes.c_void_p]
libcomprex.ThresholdConst_getThreshold.restype = np_data_t_p #ctypes.POINTER(data_t)
# ThresholdTopK
libcomprex.ThresholdTopK_new.argtypes = [ctypes.c_float]
libcomprex.ThresholdTopK_new.restype = ctypes.c_void_p
libcomprex.ThresholdTopK_del.argtypes = [ctypes.c_void_p]
libcomprex.ThresholdTopK_del.restype = ctypes.c_void_p
libcomprex.ThresholdTopK_getThreshold.argtypes = [ctypes.c_void_p]
libcomprex.ThresholdTopK_getThreshold.restype = data_t
###########################
# Compressor
###########################
# CompressorNone
libcomprex.CompressorNone_new.argtypes = []
libcomprex.CompressorNone_new.restype = ctypes.c_void_p
libcomprex.CompressorNone_del.argtypes = [ctypes.c_void_p]
libcomprex.CompressorNone_del.restype = ctypes.c_void_p
# CompressorRLE
libcomprex.CompressorRLE_new.argtypes = []
libcomprex.CompressorRLE_new.restype = ctypes.c_void_p
libcomprex.CompressorRLE_del.argtypes = [ctypes.c_void_p]
libcomprex.CompressorRLE_del.restype = ctypes.c_void_p
# CompressorIndexPairs
libcomprex.CompressorIndexPairs_new.argtypes = []
libcomprex.CompressorIndexPairs_new.restype = ctypes.c_void_p
libcomprex.CompressorIndexPairs_del.argtypes = [ctypes.c_void_p]
libcomprex.CompressorIndexPairs_del.restype = ctypes.c_void_p
class Getable(object):
def get(self):
return self.obj
###########################
# ComprEx
###########################
class Comprex(object):
def __init__(self, gaspi_env, size, size_factor=1.0):
self.size = size
self.obj = libcomprex.Comprex_new(gaspi_env.get(), size, size_factor)
def __del__(self):
if self.obj is not None:
libcomprex.Comprex_del(self.obj)
self.obj=None
def setThreshold(self, threshold):
libcomprex.Comprex_setThreshold(self.obj, threshold.get())
def setCompressor(self, compressor):
libcomprex.Comprex_setCompressor(self.obj, compressor.get())
def getSize(self):
return self.size
def resetRests(self):
libcomprex.Comprex_resetRests(self.obj)
def flushRests(self):
libcomprex.Comprex_flushRests(self.obj)
def getRests(self):
res = np.ndarray([self.size], dtype=np.float32)
try:
libcomprex.Comprex_getRests(self.obj, res, res.size)
except:
raise RuntimeError("Error getting Rests!")
return res
def writeRemote(self, vector):
if vector.dtype is not np.float32:
vec = np.array(vector, dtype=np.float32)
else:
vec = vector
libcomprex.Comprex_writeRemote(self.obj, vec, vec.size)
def readRemote(self):
res = np.ndarray([self.size], dtype=np.float32)
libcomprex.Comprex_readRemote(self.obj, res, res.size)
return res
def connectTo(self, srcRank, targRank, tag):
libcomprex.Comprex_connectTo(self.obj, srcRank, targRank, tag)
def connectTx(self, Rank, tag):
libcomprex.Comprex_connectTx(self.obj, Rank, tag)
def connectRx(self, Rank, tag):
libcomprex.Comprex_connectRx(self.obj, Rank, tag)
###########################
# Thresholds
###########################
class ThresholdNone(Getable):
def __init__(self):
self.obj = libcomprex.ThresholdNone_new()
def __del__(self):
libcomprex.ThresholdNone_del(self.obj)
class ThresholdConst(Getable):
def __init__(self, thresh):
self.obj = libcomprex.ThresholdConst_new(thresh)
def __del__(self):
libcomprex.ThresholdConst_del(self.obj)
def getThreshold(self):
return libcomprex.ThresholdConst_getThreshold(self.obj)
class ThresholdTopK(Getable):
def __init__(self, topK):
self.obj = libcomprex.ThresholdTopK_new(topK)
def __del__(self):
libcomprex.ThresholdTopK_del(self.obj)
def getThreshold(self):
return libcomprex.ThresholdTopK_getThreshold(self.obj)
###########################
# Compressors
###########################
class CompressorNone(Getable):
def __init__(self):
self.obj = libcomprex.CompressorNone_new()
def __del__(self):
libcomprex.CompressorNone_del(self.obj)
class CompressorRLE(Getable):
def __init__(self):
self.obj = libcomprex.CompressorRLE_new()
def __del__(self):
libcomprex.CompressorRLE_del(self.obj)
class CompressorIndexPairs(Getable):
def __init__(self):
self.obj = libcomprex.CompressorIndexPairs_new()
def __del__(self):
libcomprex.CompressorIndexPairs_del(self.obj)