SkePU  1.2
 All Classes Namespaces Files Functions Variables 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 <algorithm>
15 #include <stdexcept>
16 
17 #include <cstddef>
18 
19 #include <map>
20 
21 #include "skepu/matrix.h"
22 #include "skepu/src/malloc_allocator.h"
23 #include "skepu/src/environment.h"
24 
25 #ifdef SKEPU_OPENCL
26 #ifdef USE_MAC_OPENCL
27 #include <OpenCL/opencl.h>
28 #else
29 #include <CL/cl.h>
30 #endif
32 #endif
33 
34 #ifdef SKEPU_CUDA
36 #endif
37 
38 
39 namespace skepu
40 {
41 
60 template <typename T>
61 class Vector
62 {
63 
64 public:
65 
66  typedef typename std::vector<T>::size_type size_type;
67  typedef T value_type;
68  typedef ptrdiff_t difference_type;
69  typedef T* pointer;
70  typedef T& reference;
71  typedef T const & const_reference;
72 
73  //-- For Testing --//
74 
80  friend std::ostream& operator<< (std::ostream& output, Vector<T>& vec)
81  {
82  for(size_type i = 0; i < vec.size(); ++i)
83  {
84  output<<vec.at(i) <<" ";
85  }
86 
87  return output;
88  }
89 
90 public: //-- For Testing --//
91 
101  void randomize(int min = 0, int max = RAND_MAX)
102  {
104 
105  for(size_type i = 0; i < size(); i++)
106  {
107  m_data[i] = (T)( rand() % max + min );
108  }
109  }
110 
119  void save(const std::string& filename)
120  {
121  std::ofstream file(filename.c_str());
122 
123  if (file.is_open())
124  {
125  for(size_type i = 0; i < size(); ++i)
126  {
127  file<<at(i) <<" ";
128  }
129  file.close();
130  }
131  else
132  {
133  std::cout<<"Unable to open file\n";
134  }
135  }
136 
146  void load(const std::string& filename, size_type numElements = 0)
147  {
148  std::ifstream file(filename.c_str());
149 
150  if (file.is_open())
151  {
152  std::string line;
153  getline (file,line);
154  std::istringstream ss(line);
155  T num;
156  clear();
157 
158  //Load all elements
159  if(numElements == 0)
160  {
161  while(ss >> num)
162  {
163  push_back(num);
164  }
165  }
166  // Load only numElements elements
167  else
168  {
169  for(size_type i = 0; i < numElements; ++i)
170  {
171  ss >> num;
172  push_back(num);
173  }
174  }
175 
176  file.close();
177  }
178  else
179  {
180  std::cout<<"Unable to open file\n";
181  }
182  }
183 
184 public: //-- Typedefs --//
185 
186 
187 #ifdef SKEPU_CUDA
188  typedef DeviceMemPointer_CU<T>* device_pointer_type_cu;
189 #endif
190 
191 #ifdef SKEPU_OPENCL
192  typedef DeviceMemPointer_CL<T>* device_pointer_type_cl;
193 #endif
194 
195 
196 
197 
198 public: //-- Constructors & Destructor --//
199 
200  Vector();
201 
202  Vector(const Vector& vec);
203 
204  explicit Vector(size_type num, const T& val = T());
205 
206  Vector(T * const ptr, size_type size, bool deallocEnabled = true);
207 
208  ~Vector();
209 
210 public: //-- Member classes --//
211 
212  class iterator;
213 
214  class proxy_elem;
215 
216 public: //-- Operators --//
217 
218  proxy_elem operator[](const size_type index);
219 
220  const T& operator[](const size_type index) const;
221 
222  Vector<T>& operator=(const Vector<T>& other);
223 
224  bool operator==(const Vector<T>& c1);
225  bool operator!=(const Vector<T>& c1);
226 
227  bool operator<(const Vector<T>& c1);
228  bool operator>(const Vector<T>& c1);
229  bool operator<=(const Vector<T>& c1);
230  bool operator>=(const Vector<T>& c1);
231 
232 public: //-- STL vector regular interface --//
233 
234  //Iterators
235  iterator begin();
236 
237  iterator end();
238 
239  //Capacity
240  size_type capacity() const;
241 
242  size_type size() const;
243 
244  size_type max_size() const;
245 
246  void resize(size_type num, T val = T());
247 
248  bool empty() const;
249 
250  void reserve(size_type size);
251 
252 
253  //Element access
254  proxy_elem at(size_type loc);
255  const T& at(size_type loc) const;
256 
257  proxy_elem back();
258  const T& back() const;
259 
260  proxy_elem front();
261  const T& front() const;
262 
263 
264  //Modifiers
265  void assign( size_type num, const T& val );
266 
267  template <typename input_iterator>
268  void assign( input_iterator start, input_iterator end );
269 
270  void clear();
271 
272  iterator erase( iterator loc );
273  iterator erase( iterator start, iterator end );
274 
275  iterator insert( iterator loc, const T& val );
276 
277  void insert( iterator loc, size_type num, const T& val );
278 
279  void pop_back();
280 
281  void push_back(const T& val);
282 
283  void swap(Vector<T>& from);
284 
285  T *getAddress()
286  {
287  return m_data;
288  }
289 
290 public: //-- Additions to interface --//
291 
292 
293 
294 #ifdef SKEPU_OPENCL
295  device_pointer_type_cl updateDevice_CL(T* start, size_type numElements, Device_CL* device, bool copy);
296  void flush_CL();
297  bool isVectorOnDevice_CL(Device_CL* device, bool multi=false);
298 #endif
299 
300 #ifdef SKEPU_CUDA
301  void copyDataToAnInvalidDeviceCopy(DeviceMemPointer_CU<T> *copy, unsigned int deviceID, unsigned int streamID = 0);
302  device_pointer_type_cu updateDevice_CU(T* start, size_type numElements, unsigned int deviceID, bool copy, bool writeAccess, bool markOnlyLocalCopiesInvalid = false, unsigned int streamID = 0);
303  void flush_CU();
304  bool isVectorOnDevice_CU(unsigned int deviceID);
305  bool isModified_CU(unsigned int deviceID);
306 #endif
307 
308  void flush();
309 
310  // Does not care about device data, use with care
311  T& operator()(const size_type index);
312 
313  const T& operator()(const size_type index) const;
314 
315  // To be able to explicitly force updates without flushing entire vector.
316  // Could be used with operator () above to avoid unneccesary function calls
317  // due to implicit synch.
318  void updateHost() const;
319  void invalidateDeviceData();
323 
324 // #if SKEPU_DEBUG>0
325  std::string m_nameVerbose; // for debugging useful
326 // #endif
327 
328  void setValidFlag(bool val)
329  {
330  m_valid = val;
331  }
332 
333 private: //-- Data --//
334  T *m_data;
335  mutable bool m_valid;
336  size_type m_capacity;
337  size_type m_size;
338  bool m_deallocEnabled;
339  bool m_noValidDeviceCopy;
340 
341 
342 #ifdef SKEPU_OPENCL
343  std::map<std::pair< cl_device_id, T* >, device_pointer_type_cl > m_deviceMemPointers_CL;
344 #endif
345 
346 #ifdef SKEPU_CUDA
347 // std::map<std::pair< int, std::pair< T*, size_type > >, device_pointer_type_cu > m_deviceMemPointers_CU;
348  std::map<std::pair< T*, size_type >, device_pointer_type_cu > m_deviceMemPointers_CU[MAX_GPU_DEVICES];
349 
351  mutable std::map<std::pair< T*, size_type >, device_pointer_type_cu > m_deviceMemPointers_Modified_CU[MAX_GPU_DEVICES];
352 #endif
353 
354 //-- Private helpers --//
355 
356 #ifdef SKEPU_OPENCL
357  void updateHost_CL() const;
358  void invalidateDeviceData_CL();
359  void releaseDeviceAllocations_CL();
360 #endif
361 
362 #ifdef SKEPU_CUDA
363  void updateHost_CU(int deviceID = -1) const;
364  void invalidateDeviceData_CU(int deviceID = -1);
365  void releaseDeviceAllocations_CU(int deviceID = -1);
366 #endif
367 
368 
369 
370 };
371 
372 }
373 
374 #include "src/vector_iterator.inl"
375 #include "src/vector_proxy.inl"
376 #include "src/vector.inl"
377 
378 
379 
380 #ifdef SKEPU_OPENCL
381 #include "src/vector_cl.inl"
382 #endif
383 
384 #ifdef SKEPU_CUDA
385 #include "src/vector_cu.inl"
386 #endif
387 
388 
389 
390 #endif
391 
392 
T & operator()(const size_type index)
Definition: vector.inl:639
Contains a class declaration for an object which represents an OpenCL device memory allocation for co...
Contains a class declaration for an object which represents an CUDA device memory allocation for Vect...
void save(const std::string &filename)
Saves content of vector to a file.
Definition: vector.h:119
void pop_back()
Definition: vector.inl:571
iterator begin()
Definition: vector.inl:278
bool empty() const
Definition: vector.inl:349
#define MAX_GPU_DEVICES
Definition: globals.h:43
void load(const std::string &filename, size_type numElements=0)
Loads the vector from a file.
Definition: vector.h:146
~Vector()
Definition: vector.inl:92
size_type size() const
Definition: vector.inl:308
void randomize(int min=0, int max=RAND_MAX)
Randomizes the vector.
Definition: vector.h:101
A class representing an OpenCL device memory allocation for container.
Definition: device_mem_pointer_cl.h:38
void assign(size_type num, const T &val)
Definition: vector.inl:455
void updateHostAndReleaseDeviceAllocations()
Definition: vector.inl:187
proxy_elem back()
Definition: vector.inl:410
T min(T a, T b)
Definition: mapoverlap_convol_kernels.h:212
bool operator>(const Vector< T > &c1)
Definition: vector.inl:722
bool operator>=(const Vector< T > &c1)
Definition: vector.inl:760
bool operator==(const Vector< T > &c1)
Definition: vector.inl:657
void resize(size_type num, T val=T())
Definition: vector.inl:326
iterator end()
Definition: vector.inl:288
proxy_elem operator[](const size_type index)
Definition: vector.inl:213
device_pointer_type_cu updateDevice_CU(T *start, size_type numElements, unsigned int deviceID, bool copy, bool writeAccess, bool markOnlyLocalCopiesInvalid=false, unsigned int streamID=0)
Update device with vector content.
Definition: vector_cu.inl:162
void releaseDeviceAllocations()
Definition: vector.inl:170
proxy_elem at(size_type loc)
Definition: vector.inl:387
void flush_CL()
Flushes the vector.
Definition: vector_cl.inl:77
T max(T a, T b)
Definition: mapoverlap_convol_kernels.h:203
Contains a class declaration for the Matrix container.
void flush()
Definition: vector.inl:619
A vector container class, implemented as a wrapper for std::vector.
Definition: vector.h:61
bool isModified_CU(unsigned int deviceID)
Definition: vector_cu.inl:437
A class representing a CUDA device memory allocation for container.
Definition: device_mem_pointer_cu.h:58
void invalidateDeviceData()
Definition: vector.inl:137
size_type max_size() const
Definition: vector.inl:317
Vector()
Definition: vector.inl:13
Contains a class declaration for Environment class.
Vector< T > & operator=(const Vector< T > &other)
Definition: vector.inl:234
void push_back(const T &val)
Definition: vector.inl:582
bool isVectorOnDevice_CU(unsigned int deviceID)
Definition: vector_cu.inl:344
size_type capacity() const
Definition: vector.inl:299
void reserve(size_type size)
Definition: vector.inl:358
device_pointer_type_cl updateDevice_CL(T *start, size_type numElements, Device_CL *device, bool copy)
Update device with vector content.
Definition: vector_cl.inl:20
void copyDataToAnInvalidDeviceCopy(DeviceMemPointer_CU< T > *copy, unsigned int deviceID, unsigned int streamID=0)
Used by updateDevice_CU function to copy data to a device copy.. the device copy could be a new one (...
Definition: vector_cu.inl:16
bool operator!=(const Vector< T > &c1)
Definition: vector.inl:680
void updateHostAndInvalidateDevice()
Definition: vector.inl:160
void flush_CU()
Flushes the vector.
Definition: vector_cu.inl:359
void updateHost() const
Definition: vector.inl:116
void swap(Vector< T > &from)
Definition: vector.inl:596
void clear()
Definition: vector.inl:495
proxy_elem front()
Definition: vector.inl:433