libfaster API Documentation  Development Version
Super fast distributted computing
workerFddWrapper.cpp
1 #include <dlfcn.h>
2 #include <iostream>
3 #include <unordered_map>
4 #include <unordered_map>
5 #include <chrono>
6 
7 #include "workerFdd.h"
8 #include "workerFddModule.h"
9 
10 using std::chrono::system_clock;
11 using std::chrono::duration_cast;
12 using std::chrono::milliseconds;
13 
14 void * faster::workerFdd::dLHandler[3][7] = {};
15 
16 // Key Type
17 std::unordered_map<faster::fddType, int> faster::workerFdd::khAssign = std::unordered_map<fddType, int> ({
18  {0,0},{Char,1}, {Int,2}, {LongInt,3}, {Float,4}, {Double,5},{String,6}
19  });
20 // Data Type
21 std::unordered_map<faster::fddType, int> faster::workerFdd::hAssign = std::unordered_map<fddType, int>({
22  {Char,0}, {Int,0}, {LongInt,0}, {Float,0}, {Double,0},
23  {CharP,1},{IntP,1},{LongIntP,1},{FloatP,1},{DoubleP,1},{Custom,1},
24  {CharV,2},{IntV,2},{LongIntV,2},{FloatV,2},{DoubleV,2},{String,2},
25  });
26 
27 std::unordered_map<char, void *> faster::workerFdd::funcTable[3][7];
28 
29 
30 void * faster::workerFdd::load(const std::string libraryName){
31  //std::cerr << "[Loading " << libraryName << " ";
32  //auto start = system_clock::now();
33 
34  void * hdlr = dlopen(libraryName.data(), RTLD_LAZY);
35  //void * hdlr = dlopen(libraryName.data(), RTLD_NOW);
36 
37  if(hdlr == NULL){
38  std::cerr << "\n\033[5m\033[91mERROR! \033[0m\033[38;5;202m"<< dlerror() << "\033[0m " << std::endl;
39  exit(-1);
40  }
41  //std::cerr << duration_cast<milliseconds>(system_clock::now() - start).count() << "ms] ";
42 
43  return hdlr;
44 }
45 
46 void faster::workerFdd::loadSym(dFuncName funcName, const std::string symbolName){
47  void * symbl = dlsym(dLHandler[hAssign[type]][khAssign[keyType]], symbolName.data());
48 
49  if(symbl == NULL){
50  std::cerr << "\n\033[5m\033[91mERROR! \033[0m\033[38;5;202m"<< dlerror() << "\033[0m " << std::endl;
51  exit(-1);
52  }
53  funcTable[hAssign[type]] [khAssign[keyType]] [funcName] = symbl;
54 }
55 
56 void faster::workerFdd::loadLib(){
57  if ( dLHandler[hAssign[type]][0] )
58  return;
59 
60  if (type & POINTER){
61  dLHandler[hAssign[type]][0] = load("libfaster/libfasterWorkerPFdd.so");
62  }else{
63  if (type & (VECTOR | String))
64  dLHandler[hAssign[type]][0] = load("libfaster/libfasterWorkerCFdd.so");
65  else
66  dLHandler[hAssign[type]][0] = load("libfaster/libfasterWorkerSFdd.so");
67  }
68 
69 }
70 
71 void faster::workerFdd::loadLibI(){
72  if ( dLHandler[hAssign[type]][khAssign[keyType]] )
73  return;
74 
75 
76  if (type & POINTER){
77  switch (keyType){
78  case Char: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIPFddInstance0.so"); break;
79  case Int: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIPFddInstance1.so"); break;
80  case LongInt: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIPFddInstance2.so"); break;
81  case Float: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIPFddInstance3.so"); break;
82  case Double: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIPFddInstance4.so"); break;
83  case String: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIPFddInstance5.so"); break;
84  }
85  }else{
86  if (type & (VECTOR | String))
87  switch (keyType){
88  case Char: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance6.so"); break;
89  case Int: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance7.so"); break;
90  case LongInt: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance8.so"); break;
91  case Float: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance9.so"); break;
92  case Double: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance10.so"); break;
93  case String: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance11.so"); break;
94  }
95  else
96  switch (keyType){
97  case Char: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance0.so"); break;
98  case Int: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance1.so"); break;
99  case LongInt: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance2.so"); break;
100  case Float: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance3.so"); break;
101  case Double: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance4.so"); break;
102  case String: dLHandler[hAssign[type]][khAssign[keyType]] = load("libfaster/libfasterWorkerIFddInstance5.so"); break;
103  }
104  }
105 
106 }
107 
108 
109 void faster::workerFdd::loadSymbols(){
110  //funcTable[hAssign[t]] = std::unordered_map<char, void *>();
111  if (funcTable[hAssign[type]] [khAssign[keyType]] .size() != 0)
112  return;
113  //auto start = system_clock::now();
114  //std::cerr << "[Locating Symbols (" << hAssign[type] << "," << khAssign[keyType] << ") ";
115 
116  //std::cerr.flush();
117  loadSym(NewWorkerSDL , "newWorkerSDL");
118  loadSym(DiscardWorkerDL , "discardWorkerDL");
119 
120  loadSym(GetTypeDL , "getTypeDL");
121  loadSym(GetKeyTypeDL , "getKeyTypeDL");
122 
123 
124  loadSym(SetDataDL , "setDataDL");
125  loadSym(SetDataRawDL , "setDataRawDL");
126  //std::cerr << ".";
127 
128  loadSym(GetLineSizesDL , "getLineSizesDL");
129 
130  loadSym(GetFddItemDL , "getFddItemDL");
131  loadSym(GetKeysDL , "getKeysDL");
132  loadSym(GetDataDL , "getDataDL");
133  loadSym(GetSizeDL , "getSizeDL");
134  loadSym(ItemSizeDL , "itemSizeDL");
135  loadSym(BaseSizeDL , "baseSizeDL");
136  loadSym(SetSizeDL , "setSizeDL");
137 
138  loadSym(DeleteItemDL , "deleteItemDL");
139  loadSym(ShrinkDL , "shrinkDL");
140  //std::cerr << ".";
141 
142 
143  loadSym(InsertDL , "insertDL");
144  loadSym(InsertListDL , "insertListDL");
145 
146  loadSym(PreapplyDL , "preapplyDL");
147 
148  loadSym(CollectDL , "collectDL");
149  loadSym(ExchangeDataByKeyDL, "exchangeDataByKeyDL");
150  loadSym(GetKeyLocationDL, "getKeyLocationsDL");
151  loadSym(GetUKeysDL , "getUKeysDL");
152  loadSym(SetUKeysDL , "setUKeysDL");
153  loadSym(GetKeyMapDL , "getKeyMapDL");
154  loadSym(SetKeyMapDL , "setKeyMapDL");
155  loadSym(WriteToFileDL , "writeToFileDL");
156 
157  //std::cerr << duration_cast<milliseconds>(system_clock::now() - start).count() << "ms] ";
158 }
159 
160 
161 // Create Indexed FDDs
162 faster::workerFdd::workerFdd(fddType t){
163  type = t;
164  keyType = 0;
165  loadLib();
166  loadSymbols();
167 }
168 faster::workerFdd::workerFdd(unsigned long int id, fddType t, size_t size) : workerFdd(t){
169  this->id = id;
170  void * funcP = funcTable[hAssign[t]] [0] [NewWorkerSDL];
171  _fdd = ((workerFddBase * (*)(unsigned long int, fddType, size_t)) funcP)(id, t, size);
172 }
173 faster::workerFdd::workerFdd(unsigned long int id, fddType t) : workerFdd(id, t, size_t(0)){
174 }
175 
176 
177 // Create Indexed FDDs
178 faster::workerFdd::workerFdd(fddType kt, fddType t){
179  type = t;
180  keyType = kt;
181  loadLibI();
182  loadSymbols();
183 }
184 faster::workerFdd::workerFdd(unsigned long int id, fddType kt, fddType t, size_t size) : workerFdd(kt, t){
185  this->id = id;
186  void * funcP = funcTable[hAssign[t]] [khAssign[kt]] [NewWorkerSDL];
187  _fdd = ((workerFddBase * (*)(unsigned long int, fddType, size_t)) funcP)(id, t, size);
188 }
189 faster::workerFdd::workerFdd(unsigned long int id, fddType kt, fddType t) : workerFdd(id, kt, t, 0){
190 }
191 
192 
193 faster::workerFdd::~workerFdd(){
194  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [DiscardWorkerDL];
195  ((void (*) (workerFddBase *)) funcP)(_fdd);
196 }
197 
198 
199 faster::fddType faster::workerFdd::getType(){
200  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetTypeDL];
201  return ((fddType (*)(workerFddBase *)) funcP)(_fdd);
202 }
203 
204 faster::fddType faster::workerFdd::getKeyType(){
205  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetKeyTypeDL];
206  return ((fddType (*)(workerFddBase *)) funcP)(_fdd);
207 }
208 
209 
210 //T & faster::workerFdd::operator[](size_t address){
211 void * faster::workerFdd::getItem(size_t address){
212  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetFddItemDL];
213  return ((void * (*)(workerFddBase *, size_t)) funcP)(_fdd, address);
214 }
215 
216 void * faster::workerFdd::getKeys(){
217  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetKeysDL];
218  return ((void * (*)(workerFddBase * fdd)) funcP)(_fdd);
219 }
220 
221 void * faster::workerFdd::getData(){
222  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetDataDL];
223  return ((void * (*)(workerFddBase * fdd)) funcP)(_fdd);
224 }
225 
226 size_t faster::workerFdd::getSize(){
227  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetSizeDL];
228  return ((size_t (*)(workerFddBase *)) funcP)(_fdd);
229 }
230 
231 size_t faster::workerFdd::itemSize(){
232  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [ItemSizeDL];
233  return ((size_t (*)(workerFddBase *)) funcP)(_fdd);
234 }
235 
236 size_t faster::workerFdd::baseSize(){
237  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [BaseSizeDL];
238  return ((size_t (*)(workerFddBase *)) funcP)(_fdd);
239 }
240 
241 void faster::workerFdd::setSize(size_t s){
242  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetSizeDL];
243  return ((void (*)(workerFddBase *, size_t s)) funcP)(_fdd, s);
244 }
245 
246 
247 void faster::workerFdd::deleteItem(void * item){
248  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [DeleteItemDL];
249  ((void (*)(workerFddBase *, void*)) funcP)(_fdd, item);
250 }
251 
252 void faster::workerFdd::shrink(){
253  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [ShrinkDL];
254  ((void (*)(workerFddBase *)) funcP)(_fdd);
255 }
256 
257 // For known types
258 void faster::workerFdd::setData(void * data, size_t size) {
259  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataDL];
260  ((void (*)(workerFddBase *, void*, void*, size_t*, size_t)) funcP)(_fdd, NULL, data, NULL, size);
261 }
262 
263 void faster::workerFdd::setData(void * data, size_t * lineSizes, size_t size) {
264  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataDL];
265  ((void (*)(workerFddBase *, void*, void*, size_t*, size_t)) funcP)(_fdd, NULL, data, lineSizes, size);
266 }
267 void faster::workerFdd::setData(void * keys, void * data, size_t size) {
268  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataDL];
269  ((void (*)(workerFddBase *, void*, void*, size_t*, size_t)) funcP)(_fdd, keys, data, NULL, size);
270 }
271 
272 void faster::workerFdd::setData(void * keys, void * data, size_t * lineSizes, size_t size) {
273  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataDL];
274  ((void (*)(workerFddBase *, void*, void*, size_t*, size_t)) funcP)(_fdd, keys, data, lineSizes, size);
275 }
276 
277 // For anonymous types
278 
279 void faster::workerFdd::setDataRaw(void * data, size_t size){
280  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataRawDL];
281  ((void (*)(workerFddBase*, void*, void*, size_t*, size_t)) funcP)(_fdd, NULL, data, NULL, size);
282 }
283 
284 void faster::workerFdd::setDataRaw(void * data, size_t *lineSizes, size_t size){
285  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataRawDL];
286  ((void (*)(workerFddBase*, void*, void*, size_t*, size_t)) funcP)(_fdd, NULL, data, lineSizes, size);
287 }
288 void faster::workerFdd::setDataRaw(void * keys, void * data, size_t size){
289  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataRawDL];
290  ((void (*)(workerFddBase*, void*, void*, size_t*, size_t)) funcP)(_fdd, keys, data, NULL, size);
291 }
292 
293 void faster::workerFdd::setDataRaw(void * keys, void * data, size_t *lineSizes, size_t size){
294  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetDataRawDL];
295  ((void (*)(workerFddBase*, void*, void*, size_t*, size_t)) funcP)(_fdd, keys, data, lineSizes, size);
296 }
297 
298 
299 size_t * faster::workerFdd::getLineSizes(){
300  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetLineSizesDL];
301  return ((size_t * (*)(workerFddBase *)) funcP)(_fdd);
302 }
303 
304 
305 
306 void faster::workerFdd::insert(void * k, void * v, size_t s){
307  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [InsertDL];
308  ((void (*)(workerFddBase *, void *, void *, size_t)) funcP)(_fdd, k, v, s);
309 }
310 
311 void faster::workerFdd::insertl(void * v){
312  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [InsertListDL];
313  ((void (*)(workerFddBase *, void *)) funcP)(_fdd, v);
314 }
315 
316 
317 
318 // Apply task functions to FDDs
319 void faster::workerFdd::preapply(unsigned long int id, void * func, fddOpType op, workerFddBase * dest, fastComm * comm){
320  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [PreapplyDL];
321  ((void (*)(workerFddBase*, unsigned long int id, void*, fddOpType, workerFddBase*, fastComm *)) funcP)(_fdd, id, func, op, dest, comm);
322 }
323 
324 
325 void faster::workerFdd::collect(fastComm * comm){
326  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [CollectDL];
327  ((void (*)(workerFddBase *, fastComm *)) funcP)(_fdd, comm);
328 }
329 void faster::workerFdd::exchangeDataByKey(fastComm *comm){
330  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [ExchangeDataByKeyDL];
331  ((void (*)(workerFddBase *, fastComm *)) funcP)(_fdd, comm);
332 }
333 std::vector< std::vector<void*> > * faster::workerFdd::getKeyLocations() {
334  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetKeyLocationDL];
335  return (std::vector< std::vector<void*> > *) (( void * (*) (workerFddBase *)) funcP)(_fdd);
336 }
337 
338 void * faster::workerFdd::getUKeys() {
339  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetUKeysDL];
340  return (( void * (*) (workerFddBase *)) funcP)(_fdd);
341 }
342 void faster::workerFdd::setUKeys(void * uk) {
343  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetUKeysDL];
344  (( void (*) (workerFddBase *, void*)) funcP)(_fdd, uk);
345 }
346 void * faster::workerFdd::getKeyMap() {
347  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [GetKeyMapDL];
348  return (( void * (*) (workerFddBase *)) funcP)(_fdd);
349 }
350 void faster::workerFdd::setKeyMap(void * km) {
351  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [SetKeyMapDL];
352  (( void (*) (workerFddBase *, void*)) funcP)(_fdd, km);
353 }
354 
355 void faster::workerFdd::writeToFile(void * path, size_t procId, void * sufix){
356  void * funcP = funcTable[hAssign[type]] [khAssign[keyType]] [WriteToFileDL];
357  (( void (*) (workerFddBase *, void*, size_t procId, void*)) funcP)(_fdd, path, procId, sufix);
358 }
359 
360 
unsigned int fddOpType
Dataset operation type.
Definition: definitions.h:41
unsigned int fddType
Dataset type.
Definition: definitions.h:16