SkePU(integratedwithStarPU)  0.8.1
 All Classes Namespaces Files Functions Enumerations Friends Macros Groups Pages
vector.h
Go to the documentation of this file.
1 
5 #ifndef VECTOR_H
6 #define VECTOR_H
7 
8 #include <fstream>
9 #include <sstream>
10 
11 #include <iostream>
12 #include <cstdlib>
13 #include <vector>
14 #include <assert.h>
15 #include <algorithm>
16 
17 #include <map>
18 
19 #include <starpu.h>
20 
21 #include "skepu/src/debug.h"
22 #include "skepu/src/malloc_allocator.h"
23 
24 
25 
26 namespace skepu
27 {
39 template <typename T>
40 class Vector
41 {
42  //-- For Testing --//
43 
49  friend std::ostream& operator<< (std::ostream& output, Vector<T>& vec)
50  {
51  vec.acquireRead();
52 
53  for(unsigned int i = 0; i < vec.size(); ++i)
54  {
55  output<<vec.at(i) <<" ";
56  }
57 
58  return output;
59  }
60 
61 public: //-- For Testing --//
62 
72  void randomize(int min = 0, int max = RAND_MAX)
73  {
74  unregisterVector(false);
75 
76  for(unsigned int i = 0; i < m_data.size(); i++)
77  {
78  m_data.at(i) = (T)( rand() % max + min );
79  }
80  }
81 
90  void save(const std::string& filename)
91  {
92  std::ofstream file(filename.c_str());
93 
94  if (file.is_open())
95  {
96  for(unsigned int i = 0; i < size(); ++i)
97  {
98  file<<at(i) <<" ";
99  }
100  file.close();
101  }
102  else
103  {
104  std::cout<<"Unable to open file\n";
105  }
106  }
107 
117  void load(const std::string& filename, int numElements = 0)
118  {
119  std::ifstream file(filename.c_str());
120 
121  if (file.is_open())
122  {
123  std::string line;
124  getline (file,line);
125  std::istringstream ss(line);
126  T num;
127  clear();
128 
129  //Load all elements
130  if(numElements == 0)
131  {
132  while(ss >> num)
133  {
134  push_back(num);
135  }
136  }
137  // Load only numElements elements
138  else
139  {
140  for(int i = 0; i < numElements; ++i)
141  {
142  ss >> num;
143  push_back(num);
144  }
145  }
146 
147  file.close();
148  }
149  else
150  {
151  std::cout<<"Unable to open file\n";
152  }
153  }
154 
155 private: // Helper methods
156 
160  void release_acquire()
161  {
162  if(isAcquire)
163  {
164  if(parts>1) // partitioning
165  {
166  DEBUG_TEXT_LEVEL1("***** VECTOR RELEASE **** parts: "<< parts <<"\n")
167 // int parts = starpu_data_get_nb_children(vector_handle);
168 
169  // parts should be equal to what parts variable points to
170  assert (parts == starpu_data_get_nb_children(vector_handle));
171  for (int x = 0; x < parts; x++)
172  {
173  starpu_data_release(starpu_data_get_sub_data(vector_handle, 1, x));
174  }
175  }
176  else // no partitioning
177  {
178  DEBUG_TEXT_LEVEL1("***** VECTOR RELEASE ****\n")
179  starpu_data_release(vector_handle);
180  }
181  isAcquire = false;
182  }
183  }
184 
185 public:
186 
187 // StaPU specific data handler and other information
188 starpu_data_handle_t vector_handle;
189 mutable int parts;
190 mutable bool isOnStarPU;
191 mutable bool isAcquire;
192 mutable bool isReadBack;
193 mutable bool isWriteBack;
194 struct starpu_data_filter vector_filter;
195 mutable enum starpu_data_access_mode mode;
196 
197 
198 //enum starpu_data_access_mode accessMode;
199 
200 /*
201 * \brief To register Vector with StarPU runtime. Does not create partitions
202 */
203 starpu_data_handle_t registerVector()
204 {
205  // release acquire before giving vector handle
206  release_acquire();
207 
208  if(!isOnStarPU)
209  {
210  starpu_vector_data_register(&vector_handle, 0, (uintptr_t)&m_data[0], m_data.size(), sizeof(m_data[0]));
211  isOnStarPU=true;
212  DEBUG_TEXT_LEVEL1("***** VECTOR REGISTERING **** "<< m_data.size()<<"\n")
213  }
214  if(parts>1)
215  {
216  starpu_data_unpartition(vector_handle, 0); // unpartitioning vector.
217  DEBUG_TEXT_LEVEL1("***** VECTOR UN-PARTITIONING **** parts: "<< parts <<"\n")
218  parts=1;
219  }
220  return vector_handle;
221 }
222 
223 
224 
228 void unregisterVector(bool update=true)
229 {
230  if(isOnStarPU)
231  {
232  release_acquire(); // Should we release before unregister? Yes!
233 
234  if(parts>1) // do we need to unpartition before unregistering? Yes!
235  {
236  starpu_data_unpartition(vector_handle, 0); // first unpartition the partitioning.
237  parts = 1;
238  }
239 
240  if(update)
241  starpu_data_unregister(vector_handle);
242 
243  else // dont need to get updated data back
244  starpu_data_unregister_no_coherency(vector_handle);
245 
246  isOnStarPU = false;
247  isAcquire = false;
248  }
249 }
250 
255 starpu_data_handle_t registerPartitions(int _parts)
256 {
257  release_acquire();
258 
259  if(!isOnStarPU) // if not registered, register it first
260  {
261  starpu_vector_data_register(&vector_handle, 0, (uintptr_t)&m_data[0], m_data.size(), sizeof(m_data[0]));
262  isOnStarPU=true;
263  DEBUG_TEXT_LEVEL1("***** VECTOR REGISTERING **** "<< m_data.size()<<"\n")
264  parts=1;
265  }
266 
267  if(_parts == parts)
268  return vector_handle;
269 
270  if(parts>1)
271  {
272  starpu_data_unpartition(vector_handle, 0); // first unpartition previous partitioning.
273  DEBUG_TEXT_LEVEL1("***** VECTOR UNPARTITIONING **** parts: "<< parts <<"\n")
274  parts = 1;
275  }
276  if(_parts>1)
277  {
278  vector_filter.nchildren = _parts;
279  starpu_data_partition(vector_handle, &vector_filter);
280  DEBUG_TEXT_LEVEL1("***** VECTOR PARTITIONING **** parts: "<< _parts <<"\n")
281  parts = _parts;
282  }
283 
284  return vector_handle;
285 }
286 
287 
288  //-- Typedefs --//
289 #ifdef USE_PINNED_MEMORY
290  typedef typename std::vector<T, malloc_allocator<T> >::size_type size_type;
291  typedef typename std::vector<T, malloc_allocator<T> >::value_type value_type;
292  typedef typename std::vector<T, malloc_allocator<T> >::difference_type difference_type;
293  typedef typename std::vector<T, malloc_allocator<T> >::pointer pointer;
294  typedef typename std::vector<T, malloc_allocator<T> >::reference reference;
295  typedef typename std::vector<T, malloc_allocator<T> >::const_reference const_reference;
296  typedef typename std::vector<T, malloc_allocator<T> >::const_iterator const_iterator;
297  typedef typename std::vector<T, malloc_allocator<T> >::const_reverse_iterator const_reverse_iterator;
298 #else
299  typedef typename std::vector<T>::size_type size_type;
300  typedef typename std::vector<T>::value_type value_type;
301  typedef typename std::vector<T>::difference_type difference_type;
302  typedef typename std::vector<T>::pointer pointer;
303  typedef typename std::vector<T>::reference reference;
304  typedef typename std::vector<T>::const_reference const_reference;
305  typedef typename std::vector<T>::const_iterator const_iterator;
306  typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
307 #endif
308 
309 
310 public: //-- Constructors & Destructor --//
311 
312  Vector();
313 
314  Vector(const Vector& vec);
315 
316  explicit Vector(size_type num, const T& val = T(), bool redVec = false);
317 
318  template<typename input_iterator>
319  Vector(input_iterator start, input_iterator end);
320 
321  ~Vector();
322 
323 public: //-- Member classes --//
324 
325  class iterator;
326 
327  //Use STL reverse iterator
328  typedef std::reverse_iterator<iterator> reverse_iterator;
329 
330  class proxy_elem;
331 
332 public: //-- Operators --//
333 
334  proxy_elem operator[](const size_type index);
335  const T& operator[](const size_type index) const;
336  Vector<T>& operator=(const Vector<T>& other);
337 
338  bool operator==(const Vector<T>& c1);
339  bool operator!=(const Vector<T>& c1);
340  bool operator<(const Vector<T>& c1);
341  bool operator>(const Vector<T>& c1);
342  bool operator<=(const Vector<T>& c1);
343  bool operator>=(const Vector<T>& c1);
344 
345 public: //-- STL vector regular interface --//
346 
347  T *getRawType() { return &m_data[0]; }
348 
349 
350  //Iterators
351  iterator begin();
352  const_iterator begin() const;
353 
354  iterator end();
355  const_iterator end() const;
356 
357  reverse_iterator rbegin();
358  const_reverse_iterator rbegin() const;
359 
360  reverse_iterator rend();
361  const_reverse_iterator rend() const;
362 
363 
364  //Capacity
365  size_type capacity() const;
366 
367  size_type size() const;
368 
369  size_type max_size() const;
370 
371  void resize(size_type num, T val = T());
372 
373  bool empty() const;
374 
375  void reserve(size_type size);
376 
377 
378  //Element access
379  proxy_elem at(size_type loc);
380  const T& at(size_type loc) const;
381 
382  proxy_elem back();
383  const T& back() const;
384 
385  proxy_elem front();
386  const T& front() const;
387 
388 
389  //Modifiers
390  void assign( size_type num, const T& val );
391 
392  template <typename input_iterator>
393  void assign( input_iterator start, input_iterator end );
394 
395  void clear();
396 
397  iterator erase( iterator loc );
398  iterator erase( iterator start, iterator end );
399 
400  iterator insert( iterator loc, const T& val );
401  void insert( iterator loc, size_type num, const T& val );
402 
403  template <typename input_iterator>
404  void insert( iterator loc, input_iterator start, input_iterator end );
405 
406  void pop_back();
407 
408  void push_back(const T& val);
409 
410  void swap(Vector<T>& from);
411 
412 public: //-- Additions to interface --//
413 
414  void flush();
415 
416  // Does not care about device data, use with care
417  T& operator()(const size_type index);
418 
419  // To be able to explicitly force updates without flushing entire vector.
420  // Could be used with operator () above to avoid unneccesary function calls
421  // due to implicit synch.
422  void acquireRead() const;
423  void acquireReadWrite();
424 
425 private: //-- Data --//
426 
427 // A custom memory allocator is written to support pinned memory allocation for CUDA. Enabled by defining USE_PINNED_MEMORY macro
428 #ifdef USE_PINNED_MEMORY
429  mutable std::vector<T, malloc_allocator<T> > m_data;
430 #else
431  mutable std::vector<T> m_data;
432 #endif
433 
434 };
435 
436 }
437 
438 #include "src/vector_iterator.inl"
439 #include "src/vector_proxy.inl"
440 #include "src/vector.inl"
441 
442 
443 #endif
444 
reverse_iterator rend()
Definition: vector.inl:370
T & operator()(const size_type index)
Definition: vector.inl:696
Defines a few macros that can be used to output text when debugging. The macros use std::cerr...
void save(const std::string &filename)
Saves content of vector to a file.
Definition: vector.h:90
void pop_back()
Definition: vector.inl:632
iterator begin()
Definition: vector.inl:316
bool empty() const
Definition: vector.inl:430
~Vector()
Definition: vector.inl:145
void load(const std::string &filename, int numElements=0)
Loads the vector from a file.
Definition: vector.h:117
size_type size() const
Definition: vector.inl:398
void randomize(int min=0, int max=RAND_MAX)
Randomizes the vector.
Definition: vector.h:72
void assign(size_type num, const T &val)
Definition: vector.inl:521
proxy_elem back()
Definition: vector.inl:476
bool operator>(const Vector< T > &c1)
Definition: vector.inl:750
bool operator>=(const Vector< T > &c1)
Definition: vector.inl:774
bool operator==(const Vector< T > &c1)
Definition: vector.inl:714
void resize(size_type num, T val=T())
Definition: vector.inl:416
reverse_iterator rbegin()
Definition: vector.inl:352
iterator end()
Definition: vector.inl:334
proxy_elem operator[](const size_type index)
Definition: vector.inl:271
proxy_elem at(size_type loc)
Definition: vector.inl:453
void flush()
Definition: vector.inl:682
A vector container class, implemented as a wrapper for std::vector. It is configured to use StarPU DS...
Definition: vector.h:40
void acquireReadWrite()
Ensure that data is available for reading and writing purpose on CPU First updates the vector from it...
Definition: vector.inl:206
starpu_data_handle_t registerPartitions(int _parts)
To register and partition Vector with StarPU runtime.
Definition: vector.h:255
size_type max_size() const
Definition: vector.inl:407
Vector()
Definition: vector.inl:55
Vector< T > & operator=(const Vector< T > &other)
Definition: vector.inl:291
void push_back(const T &val)
Definition: vector.inl:646
size_type capacity() const
Definition: vector.inl:389
void reserve(size_type size)
Definition: vector.inl:439
void unregisterVector(bool update=true)
Definition: vector.h:228
bool operator!=(const Vector< T > &c1)
Definition: vector.inl:726
void acquireRead() const
Ensure that data is most updated for reading purpose on CPU.
Definition: vector.inl:173
void swap(Vector< T > &from)
Definition: vector.inl:660
void clear()
Definition: vector.inl:548
proxy_elem front()
Definition: vector.inl:499