libfaster API Documentation  Development Version
Super fast distributted computing
indexedFddStorage.cpp
1 #include <string>
2 #include <algorithm>
3 #include <numeric>
4 
5 #include "fastCommBuffer.h"
6 #include "indexedFddStorage.h"
7 
8 template <class K, class T>
10  allocSize = 1000;
11  localData = new T[allocSize];
12  localKeys = new K[allocSize];
13  size = 0;
14 }
15 
16 template <class K, class T>
18  if (s > 0){
19  allocSize = s;
20  }else{
21  allocSize = 1000;
22  }
23  localData = new T[allocSize];
24  localKeys = new K[allocSize];
25  size = s;
26 }
27 template <class K, class T>
29  //if (localData != NULL){
30  delete [] localData;
31  delete [] localKeys;
32  //}
33 }
34 
35 template <class K, class T>
37  return localData;
38 }
39 
40 template <class K, class T>
42  return localKeys;
43 }
44 template <class K, class T>
46  return localData[ref];
47 }
48 
49 template <class K, class T>
51  //std::cerr << " SortByKey";
52  std::vector<size_t> p(size,0);
53  std::vector<size_t> rp(size);
54  std::vector<bool> sorted(size, false);
55  size_t i = 0;
56 
57  // Sort
58  iota(p.begin(), p.end(), 0);
59  std::sort(p.begin(), p.end(),
60  [&](size_t i, size_t j){ return localKeys[i] < localKeys[j]; });
61 
62  // Apply in-place
63 
64  // get reverse permutation item>position
65  for (i = 0; i < size; ++i){
66  rp[p[i]] = i;
67  }
68 
69  i = 0;
70  K savedKey;
71  T savedData;
72  while ( i < size){
73  size_t pos = i;
74  // Save This element;
75  if ( ! sorted[pos] ){
76  savedKey = localKeys[p[pos]];
77  savedData = localData[p[pos]];
78  }
79  while ( ! sorted[pos] ){
80  // Hold item to be replaced
81  K holdenKey = localKeys[pos];
82  T holdenData = localData[pos];
83  // Save where it should go
84  size_t holdenPos = rp[pos];
85 
86  // Replace
87  localKeys[pos] = savedKey;
88  localData[pos] = savedData;
89 
90  // Get last item to be the pivot
91  savedKey = holdenKey;
92  savedData = holdenData;
93 
94  // Mark this item as sorted
95  sorted[pos] = true;
96 
97  // Go to the saved item proper location
98  pos = holdenPos;
99  }
100  ++i;
101  }
102 }
103 
104 
105 
106 
107 template <class K, class T>
108 faster::indexedFddStorage<K,T>::indexedFddStorage() : indexedFddStorageCore<K,T>(){}
109 template <class K, class T>
110 faster::indexedFddStorage<K,T*>::indexedFddStorage() : indexedFddStorageCore<K,T*>(){}
111 
112 template <class K, class T>
113 faster::indexedFddStorage<K,T>::indexedFddStorage(size_t s) : indexedFddStorageCore<K,T>(s){
114 }
115 
116 template <class K, class T>
117 faster::indexedFddStorage<K,T*>::indexedFddStorage(size_t s) : indexedFddStorageCore<K,T*>(s){
118  lineSizes = new size_t[s];
119 }
120 
121 
122 template <class K, class T>
123 faster::indexedFddStorage<K,T>::indexedFddStorage(K * keys, T * data, size_t s) : indexedFddStorage<K,T>(s){
124  setData(keys, data, s);
125 }
126 
127 template <class K, class T>
128 faster::indexedFddStorage<K,T*>::indexedFddStorage(K * keys, T ** data, size_t * lineSizes, size_t s) : indexedFddStorage<K,T *>(s){
129  setData(keys, data, lineSizes, s);
130 }
131 
132 
133 
134 
135 template <class K, class T>
137  if (lineSizes != NULL){
138  delete [] lineSizes;
139  }
140 }
141 
142 
143 
144 template <class K, class T>
145 void faster::indexedFddStorage<K,T>::setData(K * keys, T * data, size_t s){
146  grow(s);
147  this->size = s;
148 
149  std::copy(data, data + s, this->localData);
150  std::copy(keys, keys + s, this->localKeys);
151  //this->localData = data;
152  //this->localKeys = keys;
153 }
154 
155 template <class K, class T>
156 void faster::indexedFddStorage<K,T*>::setData( K * keys, T ** data, size_t * ls, size_t s){
157  grow(s);
158  #pragma omp parallel for
159  for ( size_t i = 0; i < s; ++i){
160  lineSizes[i] = ls[i];
161 
162  this->localData[i] = new T [lineSizes[i]];
163  for ( size_t j = 0; j < lineSizes[i]; ++j){
164  this->localData[i][j] = ((T *) data[i])[j];
165  }
166  this->localKeys[i] = keys[i];
167  }
168  this->size = s;
169 }
170 template <class K, class T>
171 void faster::indexedFddStorage<K,T>::setDataRaw(void * keys, void * data, size_t s){
172  fastCommBuffer buffer(0);
173  fastCommBuffer buffer2(0);
174 
175  //grow(s);
176  //this->size = s;
177  buffer.setBuffer(data, s);
178  buffer2.setBuffer(keys, s);
179 
180  //std::cerr << "\nindexedFddStorage setData ";
181  for ( size_t i = 0; i < s; ++i){
182  buffer >> this->localData[i];
183  buffer2 >> this->localKeys[i];
184  }
185 }
186 template <class K, class T>
187 void faster::indexedFddStorage<K,T*>::setDataRaw( void * keys, void * data, size_t * ls, size_t s){
188  fastCommBuffer buffer(0);
189  fastCommBuffer buffer2(0);
190 
191  buffer.setBuffer(data, s);
192  buffer2.setBuffer(keys, s);
193 
194  for ( size_t i = 0; i < s; ++i){
195  lineSizes[i] = ls[i];
196 
197  this->localData[i] = new T [lineSizes[i]];
198 
199  buffer2 >> this->localKeys[i];
200  buffer.read(this->localData[i], ls[i]*sizeof(T));
201  }
202 }
203 
204 template <class K, class T>
206  this->grow(s);
207  this->size = s;
208 }
209 template <class K, class T>
211  this->grow(s);
212  this->size = s;
213 }
214 
215 
216 template <class K, class T>
217 void faster::indexedFddStorage<K,T>::insert(K key, T & item){
218  if ( this->size == this->allocSize )
219  grow( std::max(this->size + 1, size_t(this->allocSize*1.5) ) );
220  this->localKeys[this->size] = std::move(key);
221  this->localData[this->size++] = std::move(item);
222 }
223 
224 template <class K, class T>
225 void faster::indexedFddStorage<K,T*>::insert(K key, T *& item, size_t s){
226  if ( this->size == this->allocSize )
227  grow( std::max(this->size + 1, size_t(this->allocSize*1.5) ) );
228  lineSizes[this->size] = s;
229  this->localKeys[this->size] = key;
230  this->localData[this->size++] = item;
231 
232 }
233 
234 
235 
236 
237 
238 
239 
240 template <class K, class T>
242  return lineSizes;
243 }
244 
245 
246 
247 
248 template <class K, class T>
249 void faster::indexedFddStorage<K,T>::grow(size_t toSize){
250  if (this->allocSize < toSize){
251  //if ((this->allocSize * 2) > toSize){
252  //toSize = this->allocSize * 2;
253  //}
254 
255  T * newStorage = new T [toSize];
256  K * newKeys = new K [toSize];
257 
258  if (this->size > 0) {
259  //#pragma omp parallel
260  {
261  //#pragma omp task
262  std::move(this->localData, this->localData + this->size, newStorage);
263  //std::copy(this->localData, this->localData + this->size, newStorage);
264 
265  //#pragma omp task
266  std::move(this->localKeys, this->localKeys + this->size, newKeys);
267  //std::copy(this->localKeys, this->localKeys + this->size, newKeys);
268  }
269  //#pragma omp parallel for
270  //for ( size_t i = 0; i < this->size; ++i){
271  //newStorage[i] = this->localData[i];
272  //newKeys[i] = this->localKeys[i];
273  //}
274  //memcpy(newStorage, localData, size * sizeof( T ) );
275  }
276 
277  delete [] this->localData;
278  delete [] this->localKeys;
279 
280  this->localData = newStorage;
281  this->localKeys = newKeys;
282  this->allocSize = toSize;
283  }
284 }
285 template <class K, class T>
286 void faster::indexedFddStorage<K,T*>::grow(size_t toSize){
287  if (this->allocSize < toSize){
288  if ((this->allocSize * 2) > toSize){
289  toSize = this->allocSize * 2;
290  }
291 
292  size_t * newLineSizes = new size_t [toSize];
293  T ** newStorage = new T* [toSize];
294  K * newKeys = new K [toSize];
295 
296  if (this->size > 0){
297  //memcpy(newStorage, localData, this->size * sizeof( T* ) );
298  //memcpy(newLineSizes, lineSizes, this->size * sizeof( size_t ) );
299  #pragma omp parallel for
300  for ( size_t i = 0; i < this->size; ++i){
301  newStorage[i] = this->localData[i];
302  newLineSizes[i] = lineSizes[i];
303  newKeys[i] = this->localKeys[i];
304  }
305  }
306 
307  delete [] this->localData;
308  delete [] this->localKeys;
309  delete [] lineSizes;
310 
311  this->localData = newStorage;
312  this->localKeys = newKeys;
313  lineSizes = newLineSizes;
314  this->allocSize = toSize;
315 
316  }
317 }
318 
319 
320 
321 
322 
323 
324 template <class K, class T>
326  if ( (this->size > 0) && (this->allocSize > this->size) ){
327  T * newStorage = new T [this->size];
328  K * newKeys = new K [this->size];
329 
330  for ( size_t i = 0; i < this->size; ++i){
331  newStorage[i] = this->localData[i];
332  newKeys[i] = this->localKeys[i];
333  }
334 
335  delete [] this->localData;
336  delete [] this->localKeys;
337 
338  this->localData = newStorage;
339  this->localKeys = newKeys;
340  this->allocSize = this->size;
341  }
342 }
343 template <class K, class T>
345  if ( (this->size > 0) && (this->allocSize > this->size) ){
346  T ** newStorage = new T* [this->size];
347  K * newKeys = new K [this->size];
348  size_t * newLineSizes = new size_t[this->size];
349 
350  for ( size_t i = 0; i < this->size; ++i){
351  newStorage[i] = this->localData[i];
352  newLineSizes[i] = lineSizes[i];
353  newKeys[i] = this->localKeys[i];
354  }
355  //memcpy(newStorage, localData, size * sizeof( T ) );
356 
357  delete [] this->localData;
358  delete [] this->localKeys;
359  delete [] lineSizes;
360 
361  this->localData = newStorage;
362  lineSizes = newLineSizes;
363  this->localKeys = newKeys;
364  this->allocSize = this->size;
365  }
366 }
367 
384 
401 
418 
435 
452 
469 
470 
487 
504 
521 
538 
555 
572 
573