libfaster API Documentation  Development Version
Super fast distributted computing
fddStorage.cpp
1 #include <string>
2 #include <iostream>
3 
4 #include "fastCommBuffer.h"
5 #include "fddStorage.h"
6 
7 template <class T>
9  allocSize = 1000;
10  localData = new T[allocSize];
11  size = 0;
12 }
13 
14 template <class T>
16  if (s > 0)
17  allocSize = s;
18  else
19  allocSize = 1000;
20  localData = new T[allocSize];
21  size = s;
22 }
23 template <class T>
25  delete [] localData;
26 }
27 
28 template <class T>
30  return localData;
31 }
32 
33 template <class T>
35  return localData[ref];
36 }
37 
38 
39 
40 
41 template class faster::fddStorageCore<char>;
42 template class faster::fddStorageCore<int>;
44 template class faster::fddStorageCore<float>;
45 template class faster::fddStorageCore<double>;
46 template class faster::fddStorageCore<char *>;
47 template class faster::fddStorageCore<int *>;
49 template class faster::fddStorageCore<float *>;
57 
58 
59 
60 
61 template <class T>
62 faster::fddStorage<T>::fddStorage() : fddStorageCore<T>(){}
63 template <class T>
64 faster::fddStorage<T*>::fddStorage() : fddStorageCore<T*>(){
65 }
66 
67 template <class T>
68 faster::fddStorage<T*>::fddStorage(size_t s):fddStorageCore<T *>(s){
69  lineSizes = new size_t[s];
70 }
71 
72 template <class T>
73 faster::fddStorage<T>::fddStorage(T * data, size_t s) : fddStorage<T>(s){
74  setData(data, s);
75 }
76 
77 template <class T>
78 faster::fddStorage<T*>::fddStorage(T ** data, size_t * lineSizes, size_t s) : fddStorage(s){
79  setData( data, lineSizes, s);
80 }
81 
82 
83 
84 
85 template <class T>
87  if (lineSizes != NULL){
88  delete [] lineSizes;
89  }
90 }
91 
92 
93 
94 
95 template <class T>
96 void faster::fddStorage<T>::setData( T * data, size_t s){
97  grow(s);
98  this->size = s;
99 
100  for ( size_t i = 0; i < s; ++i){
101  this->localData[i] = data[i];
102  }
103 }
104 // Works for primitive and Containers
105 
106 template <class T>
107 void faster::fddStorage<T*>::setData( T ** data, size_t * ls, size_t s){
108  grow(s);
109  #pragma omp parallel for
110  for ( size_t i = 0; i < s; ++i){
111  lineSizes[i] = ls[i];
112 
113  this->localData[i] = new T [lineSizes[i]];
114  for ( size_t j = 0; j < lineSizes[i]; ++j){
115  this->localData[i][j] = data[i][j];
116  }
117  }
118  this->size = s;
119 }
120 template <class T>
121 void faster::fddStorage<T>::setDataRaw(void * data, size_t s){
122  fastCommBuffer buffer(0);
123 
124  //grow(s);
125  //this->size = s;
126  buffer.setBuffer(data, s);
127 
128  //std::cerr << "\nfddStorage setData ";
129  for ( size_t i = 0; i < this->size; ++i){
130  //std::cerr << ((int *) data)[i] << " ";
131  buffer >> this->localData[i];
132  }
133  //std::cerr << "\n";
134 }
135 template <class T>
136 void faster::fddStorage<T*>::setDataRaw( void * data, size_t * ls, size_t s){
137  fastCommBuffer buffer(0);
138 
139  buffer.setBuffer(data, s);
140 
141  for ( size_t i = 0; i < s; ++i){
142  lineSizes[i] = ls[i];
143 
144  this->localData[i] = new T [lineSizes[i]];
145 
146  buffer.read(this->localData[i], ls[i]*sizeof(T));
147  }
148 }
149 
150 template <class T>
151 void faster::fddStorage<T>::setSize(size_t s){
152  this->grow(s);
153  this->size = s;
154 }
155 template <class T>
156 void faster::fddStorage<T*>::setSize(size_t s){
157  this->grow(s);
158  this->size = s;
159 }
160 
161 template <class T>
162 void faster::fddStorage<T>::insert(T & item){
163  if ( this->size == this->allocSize )
164  grow( std::max(this->size + 1, size_t(this->allocSize*1.5) ) );
165  this->localData[this->size++] = std::move(item);
166 }
167 
168 template <class T>
169 void faster::fddStorage<T*>::insert(T *& item, size_t s){
170  if ( this->size == this->allocSize )
171  grow( std::max(this->size + 1, size_t(this->allocSize*1.5) ) );
172  lineSizes[this->size] = s;
173  this->localData[this->size++] = item;
174 
175 }
176 
177 
178 
179 
180 
181 
182 template <class T>
184  return lineSizes;
185 }
186 
187 
188 
189 
190 template <class T>
191 void faster::fddStorage<T>::grow(size_t toSize){
192  if (this->allocSize < toSize){
193  if ((this->allocSize * 2) > toSize){
194  toSize = this->allocSize * 2;
195  }
196 
197  T * newStorage = new T [toSize];
198 
199  if (this->size > 0)
200  std::move(this->localData, this->localData + this->size, newStorage);
201  //memcpy(newStorage, localData, size * sizeof( T ) );
202 
203  delete [] this->localData;
204 
205  this->localData = newStorage;
206  this->allocSize = toSize;
207  }
208 }
209 template <class T>
210 void faster::fddStorage<T*>::grow(size_t toSize){
211  if (this->allocSize < toSize){
212  if ((this->allocSize * 2) > toSize){
213  toSize = this->allocSize * 2;
214  }
215 
216  size_t * newLineSizes = new size_t [toSize];
217  T ** newStorage = new T* [toSize];
218 
219  if (this->size > 0){
220  std::move(this->localData, this->localData + this->size, newStorage);
221  std::move(this->lineSizes, this->lineSizes + this->size, newLineSizes);
222  //memcpy(newStorage, localData, this->size * sizeof( T* ) );
223  //memcpy(newLineSizes, lineSizes, this->size * sizeof( size_t ) );
224  }
225 
226  delete [] this->localData;
227  delete [] lineSizes;
228 
229  this->localData = newStorage;
230  lineSizes = newLineSizes;
231  this->allocSize = toSize;
232 
233  }
234 }
235 
236 
237 
238 
239 
240 
241 template <class T>
243  if ( (this->size > 0) && (this->allocSize > this->size) ){
244  T * newStorage = new T [this->size];
245 
246  std::move(this->localData, this->localData + this->size, newStorage);
247 
248  delete [] this->localData;
249 
250  this->localData = newStorage;
251  this->allocSize = this->size;
252  }
253 }
254 template <class T>
256  if ( (this->size > 0) && (this->allocSize > this->size) ){
257  T ** newStorage = new T* [this->size];
258  size_t * newLineSizes = new size_t[this->size];
259 
260  std::move(this->localData, this->localData + this->size, newStorage);
261  std::move(this->lineSizes, this->lineSizes + this->size, newLineSizes);
262  //memcpy(newStorage, localData, size * sizeof( T ) );
263 
264  delete [] this->localData;
265  delete [] lineSizes;
266 
267  this->localData = newStorage;
268  lineSizes = newLineSizes;
269  this->allocSize = this->size;
270  }
271 }
272 
273 
274 
275 
276 template class faster::fddStorage<char>;
277 template class faster::fddStorage<int>;
278 template class faster::fddStorage<long int>;
279 template class faster::fddStorage<float>;
280 template class faster::fddStorage<double>;
281 template class faster::fddStorage<char *>;
282 template class faster::fddStorage<int *>;
283 template class faster::fddStorage<long int *>;
284 template class faster::fddStorage<float *>;
285 template class faster::fddStorage<double *>;
286 template class faster::fddStorage<std::string>;