19 #include "skepu/src/malloc_allocator.h"
43 static void matrix_filter_uneven_func(
void *father_interface,
void *child_interface,
struct starpu_data_filter *f,
unsigned id,
unsigned nchunks)
45 struct starpu_matrix_interface *matrix_father = (
struct starpu_matrix_interface *) father_interface;
46 struct starpu_matrix_interface *matrix_child = (
struct starpu_matrix_interface *) child_interface;
48 uint32_t nx = matrix_father->nx;
49 uint32_t ny = matrix_father->ny;
50 size_t elemsize = matrix_father->elemsize;
53 assert(nchunks <= ny && nchunks==SKEPU_PARTDIM);
55 assert(nchunks <= ny && nchunks==2);
58 uint32_t chunk_size = SKEPU_PARTS[id];
61 for(
int i=0; i<(id); ++i)
63 offset += SKEPU_PARTS[i] * (matrix_father->ld) * elemsize;
66 std::cerr <<
"chunk_size: " << chunk_size <<
", offset: " << offset <<
"\n";
68 uint32_t child_ny = chunk_size;
70 matrix_child->ld = matrix_father->ld;
71 matrix_child->nx = nx;
72 matrix_child->ny = child_ny;
73 matrix_child->elemsize = elemsize;
75 if (matrix_father->ptr)
76 matrix_child->ptr = matrix_father->ptr + offset;
78 if (matrix_father->dev_handle)
80 matrix_child->dev_handle = matrix_father->dev_handle;
81 matrix_child->offset = matrix_father->offset + offset;
108 void unpartitionMatrix()
const
110 DEBUG_TEXT_LEVEL1(
"***** MATRIX UNPARTIOTIINING xparts: "<< xparts <<
", yparts: "<< yparts <<
" *****\n")
111 if(xparts>1 && yparts >1)
112 starpu_data_unpartition(matrix_handle, 0);
113 else if(xparts>1 || yparts >1)
114 starpu_data_unpartition(matrix_handle, 0);
122 void release_acquire()
126 if(xparts>1 || yparts>1)
128 DEBUG_TEXT_LEVEL1(
"***** MATRIX RELEASE **** xparts: "<< xparts <<
", yparts: "<< yparts <<
"\n")
130 if(xparts > 1 && yparts > 1)
132 for (
int y = 0; y < yparts; y++)
133 for (
int x = 0; x < xparts; x++)
135 starpu_data_release(starpu_data_get_sub_data(matrix_handle, 2, x, y));
140 int parts = (xparts>1) ? xparts : yparts;
142 assert (parts == starpu_data_get_nb_children(matrix_handle));
143 for (
int x = 0; x < parts; x++)
145 starpu_data_release(starpu_data_get_sub_data(matrix_handle, 1, x));
151 DEBUG_TEXT_LEVEL1(
"***** MATRIX RELEASE ****\n")
152 starpu_data_release(matrix_handle);
162 starpu_data_handle_t matrix_handle;
163 mutable bool isOnStarPU;
164 mutable bool isReadBack;
165 mutable bool isWriteBack;
169 mutable bool isAcquire;
170 mutable enum starpu_data_access_mode mode;
172 struct starpu_data_filter matrix_filter_x;
173 struct starpu_data_filter matrix_filter_y;
185 starpu_matrix_data_register(&matrix_handle, 0, (uintptr_t)&m_data[0], m_cols, m_cols, m_rows,
sizeof(m_data[0]));
187 DEBUG_TEXT_LEVEL1(
"***** MATRIX REGISTERING **** rows: "<< m_rows <<
" cols: "<< m_cols <<
" \n")
189 else if(xparts>1 || yparts >1)
193 return matrix_handle;
205 if(xparts>1 || yparts >1)
211 starpu_data_unregister(matrix_handle);
214 starpu_data_unregister_no_coherency(matrix_handle);
230 starpu_matrix_data_register(&matrix_handle, 0, (uintptr_t)&m_data[0], m_cols, m_cols, m_rows,
sizeof(m_data[0]));
232 DEBUG_TEXT_LEVEL1(
"***** MATRIX REGISTERING **** rows: "<< m_rows <<
" cols: "<< m_cols <<
" \n")
235 if(_xparts == xparts && _yparts==yparts)
236 return matrix_handle;
237 if(xparts>1 || yparts>1)
241 if(_xparts>1 && _yparts>1)
243 matrix_filter_x.nchildren = _xparts;
244 matrix_filter_y.nchildren = _yparts;
245 starpu_data_map_filters(matrix_handle, 2, &matrix_filter_x, &matrix_filter_y);
246 DEBUG_TEXT_LEVEL1(
"***** MATRIX PARTIOTIINING xparts: "<< _xparts <<
", yparts: "<< _yparts <<
" *****\n")
252 matrix_filter_x.nchildren = _xparts;
253 starpu_data_partition(matrix_handle, &matrix_filter_x);
254 DEBUG_TEXT_LEVEL1(
"***** MATRIX PARTIOTIINING xparts: "<< _xparts <<
" *****\n")
259 matrix_filter_y.nchildren = _yparts;
260 starpu_data_partition(matrix_handle, &matrix_filter_y);
261 DEBUG_TEXT_LEVEL1(
"***** MATRIX PARTIOTIINING yparts: "<< _yparts <<
" *****\n")
265 return matrix_handle;
271 #ifdef USE_PINNED_MEMORY
272 typedef std::vector<T, malloc_allocator<T> > container_type;
273 typedef typename std::vector<T, malloc_allocator<T> >::size_type size_type;
274 typedef typename std::vector<T, malloc_allocator<T> >::value_type value_type;
275 typedef typename std::vector<T, malloc_allocator<T> >::difference_type difference_type;
276 typedef typename std::vector<T, malloc_allocator<T> >::pointer pointer;
277 typedef typename std::vector<T, malloc_allocator<T> >::reference reference;
278 typedef typename std::vector<T, malloc_allocator<T> >::const_reference const_reference;
279 typedef typename std::vector<T, malloc_allocator<T> >::const_iterator const_iterator;
280 typedef typename std::vector<T, malloc_allocator<T> >::const_reverse_iterator const_reverse_iterator;
282 typedef std::vector<T> container_type;
283 typedef typename std::vector<T>::size_type size_type;
284 typedef typename std::vector<T>::value_type value_type;
285 typedef typename std::vector<T>::difference_type difference_type;
286 typedef typename std::vector<T>::pointer pointer;
287 typedef typename std::vector<T>::reference reference;
288 typedef typename std::vector<T>::const_reference const_reference;
289 typedef typename std::vector<T>::const_iterator const_iterator;
290 typedef typename std::vector<T>::const_reverse_iterator const_reverse_iterator;
308 friend std::ostream& operator<<(std::ostream &os, Matrix<T>& matrix)
310 matrix.acquireRead();
312 os <<
"Matrix: ("<< matrix.total_rows() <<
" X "<<matrix.total_cols()<<
")\n";
313 for(
int i=0;i<matrix.size();i++)
315 os<<(matrix(i))<<
" ";
316 if((i+1)%(matrix.total_cols())==0)
337 for(
unsigned int i = 0; i <
size(); i++)
339 m_data.at(i) = (T)( rand() % max + min);
351 void save(
const std::string& filename)
353 std::ofstream file(filename.c_str());
357 for(
unsigned int i = 0; i < m_data.size(); ++i)
359 file<<m_data.at(i) <<
" ";
365 std::cout<<
"Unable to open file\n";
379 void load(
const std::string& filename,
int rowWidth,
int numRows = 0)
381 std::ifstream file(filename.c_str());
387 std::istringstream ss(line);
402 for(
int i = 0; i < (numRows*rowWidth); ++i)
410 m_rows = (
size()/rowWidth);
416 std::cout<<
"Unable to open file\n";
431 DEBUG_TEXT_LEVEL1(
"***** MATRIX DESTRUCTOR **** rows: "<<
total_rows()<<
", cols: "<<
total_cols() <<
"\n")
436 if(xparts>1 || yparts>1)
439 starpu_data_unregister(matrix_handle);
449 Matrix(size_type _rows, size_type _cols): m_rows(_rows), m_cols(_cols), m_data(m_rows * m_cols), transpose_matrix(0)
456 memset(&matrix_filter_x, 0,
sizeof(matrix_filter_x));
459 matrix_filter_x.filter_func=matrix_filter_uneven_func;
461 matrix_filter_x.filter_func=starpu_vertical_block_filter_func;
464 memset(&matrix_filter_y, 0,
sizeof(matrix_filter_y));
465 matrix_filter_y.filter_func=starpu_block_filter_func;
474 Matrix(size_type _rows, size_type _cols,
const T& val): m_rows(_rows), m_cols(_cols),m_data(m_rows * m_cols, val), transpose_matrix(0)
480 memset(&matrix_filter_x, 0,
sizeof(matrix_filter_x));
483 matrix_filter_x.filter_func=matrix_filter_uneven_func;
485 matrix_filter_x.filter_func=starpu_vertical_block_filter_func;
488 memset(&matrix_filter_y, 0,
sizeof(matrix_filter_y));
489 matrix_filter_y.filter_func=starpu_block_filter_func;
505 this->m_data= copy.m_data;
506 this->transpose_matrix = copy.transpose_matrix;
508 isOnStarPU = copy.isOnStarPU;
509 isAcquire = copy.isAcquire;
511 matrix_filter_x = copy.matrix_filter_x;
512 matrix_filter_y = copy.matrix_filter_y;
524 size_type
size()
const {
return m_data.size();}
552 size_type m_rows, m_cols;
555 #ifdef USE_PINNED_MEMORY
556 mutable std::vector<T, malloc_allocator<T> > m_data;
558 mutable std::vector<T> m_data;
565 template<
typename Type>
566 void item_swap(Type &t1, Type &t2);
583 void resize(size_type _rows, size_type _cols, T val = T());
590 bool operator<(const Matrix<T>& c1);
592 bool operator<=(const Matrix<T>& c1);
595 Matrix<T>&
subsection(size_type row, size_type col, size_type rowWidth, size_type colWidth);
601 const_iterator
begin()
const;
602 iterator
begin(
unsigned row);
603 const_iterator
begin(
unsigned row)
const;
606 const_iterator
end()
const;
607 iterator
end(
unsigned row);
608 const_iterator
end(
unsigned row)
const;
631 proxy_elem at(size_type row, size_type col);
632 const T& at(size_type row, size_type col)
const;
634 size_type row_back(size_type row);
635 const T& row_back(size_type row)
const;
637 size_type row_front(size_type row);
638 const T& row_front(size_type row)
const;
640 proxy_elem col_back(size_type col);
641 const T& col_back(size_type col)
const;
643 proxy_elem col_front(size_type col);
644 const T& col_front(size_type col)
const;
648 iterator erase( iterator loc );
649 iterator erase( iterator start, iterator
end );
659 const T& operator()(
const size_type row,
const size_type col)
const;
661 T& operator()(
const size_type row,
const size_type col);
664 T& operator()(
const size_type index);
682 Matrix<T>& get_transpose_matrix(
bool updated =
false)
684 if(transpose_matrix==0 || updated)
691 (*transpose_matrix)(j,i) = x;
694 return (*transpose_matrix);
711 const Matrix<T>&
operator+=(
const Matrix<T>& rhs);
714 const Matrix<T>&
operator-=(
const Matrix<T>& rhs);
717 const Matrix<T>&
operator*=(
const Matrix<T>& rhs);
720 const Matrix<T>&
operator/=(
const Matrix<T>& rhs);
723 const Matrix<T>&
operator%=(
const Matrix<T>& rhs);
729 #include "src/matrix_iterator.inl"
731 #include "src/matrix_col_iterator.inl"
733 #include "src/matrix_proxy.inl"
734 #include "src/matrix.inl"
T & operator[](const size_type index)
Definition: matrix.inl:947
void acquireReadWrite()
Definition: matrix.inl:335
iterator begin()
Definition: matrix.inl:421
size_type capacity() const
Definition: matrix.inl:567
void unregisterMatrix(bool update=true)
Definition: matrix.h:199
void clear()
Definition: matrix.inl:794
bool operator>(const Matrix< T > &c1)
Definition: matrix.inl:1012
Matrix(size_type _rows, size_type _cols, const T &val)
Definition: matrix.h:474
Matrix< T > & subsection(size_type row, size_type col, size_type rowWidth, size_type colWidth)
Definition: matrix.inl:636
A matrix container class (2D matrix), internally uses 1D container (std::vector). ...
Definition: matrix.h:103
std::pair< const_iterator, const_iterator > col_iterator_range()
Definition: matrix.inl:472
size_type total_cols() const
Definition: matrix.h:536
const Matrix< T > & operator-=(const Matrix< T > &rhs)
Definition: matrix.inl:126
bool operator!=(const Matrix< T > &c1)
Definition: matrix.inl:985
void save(const std::string &filename)
Saves content of Matrix to a file.
Definition: matrix.h:351
col_iterator col_end()
Definition: matrix.inl:552
T * GetArrayRep()
Definition: matrix.h:298
const Matrix< T > & operator%=(const Matrix< T > &rhs)
Definition: matrix.inl:242
void acquireRead() const
Definition: matrix.inl:290
col_iterator col_begin()
Definition: matrix.inl:487
starpu_data_handle_t registerPartitions(int _xparts=1, int _yparts=1)
To register Matrix to StarPU. This method can create partitions of the matrix.
Definition: matrix.h:224
bool empty() const
Definition: matrix.inl:578
iterator end()
Definition: matrix.inl:503
const Matrix< T > & operator/=(const Matrix< T > &rhs)
Definition: matrix.inl:203
size_type size() const
Definition: matrix.h:524
const Matrix< T > & operator+=(const Matrix< T > &rhs)
Definition: matrix.inl:87
AccessType
Can be used to specify whether the access is row-wise or column-wise.
Definition: matrix.h:33
void load(const std::string &filename, int rowWidth, int numRows=0)
Loads the Matrix from a file.
Definition: matrix.h:379
void flush()
Definition: matrix.inl:857
void resize(size_type _rows, size_type _cols, T val=T())
Definition: matrix.inl:51
starpu_data_handle_t registerMatrix()
To register Matrix with StarPU. Does not create partitions of Matrix.
Definition: matrix.h:179
Contains a class declaration for Environment class.
Matrix(size_type _rows, size_type _cols)
Definition: matrix.h:449
const Matrix< T > & operator*=(const Matrix< T > &rhs)
Definition: matrix.inl:165
Matrix< T > & operator=(Matrix< T > &other)
Definition: matrix.inl:35
void change_layout()
Definition: matrix.h:542
bool operator>=(const Matrix< T > &c1)
Definition: matrix.inl:1039
void randomize(int min=0, int max=RAND_MAX)
Randomizes the Matrix.
Definition: matrix.h:333
size_type total_rows() const
Definition: matrix.h:530
void swap(Matrix< T > &from)
Definition: matrix.inl:806
bool operator==(const Matrix< T > &c1)
Definition: matrix.inl:972
Matrix(const Matrix< T > ©)
Definition: matrix.h:499
~Matrix()
Definition: matrix.h:429