10#ifndef TLAPACK_STARPU_MATRIX_HH
11#define TLAPACK_STARPU_MATRIX_HH
38 template <
class T,
bool TisConstType>
45 virtual idx_t nrows()
const noexcept = 0;
46 virtual idx_t ncols()
const noexcept = 0;
47 virtual MatrixEntry<T> map_to_entry(idx_t i, idx_t j)
noexcept = 0;
60 return T(
A.map_to_entry(i, j));
72 assert((nrows() <= 1 || ncols() <= 1) &&
73 "Matrix is not a vector");
74 return (nrows() > 1) ? (*this)(i, 0) : (*this)(0, i);
82 virtual idx_t nrows()
const noexcept = 0;
83 virtual idx_t ncols()
const noexcept = 0;
84 virtual MatrixEntry<T> map_to_entry(idx_t i, idx_t j)
noexcept = 0;
102 return map_to_entry(i, j);
114 assert((nrows() <= 1 || ncols() <= 1) &&
115 "Matrix is not a vector");
116 return (nrows() > 1) ? (*this)(i, 0) : (*this)(0, i);
157 assert(m > 0 && n > 0 &&
"Invalid matrix size");
158 assert(
mt <= m &&
nt <= n &&
"Invalid tile size");
160 nx = (
mt == 0) ? 1 : ((m %
mt == 0) ? m /
mt : m /
mt + 1);
161 ny = (
nt == 0) ? 1 : ((n %
nt == 0) ? n /
nt : n /
nt + 1);
182 constexpr Matrix(
const std::shared_ptr<starpu_data_handle_t>& pHandle,
201 assert(ix >= 0 && iy >= 0 &&
"Invalid tile position");
202 assert(nx > 0 && ny > 0 &&
"Invalid tile size");
203 assert(row0 >= 0 && col0 >= 0 &&
"Invalid tile offset");
204 assert(lastRows >= 0 && lastCols >= 0 &&
"Invalid tile size");
236 assert(ix >= 0 && iy >= 0 && ix < nx && iy < ny &&
237 "Invalid tile index");
240 *pHandle, 2, ix + this->ix, iy + this->iy);
254 if (ix == nx - 1) m = lastRows;
255 if (iy == ny - 1) n = lastCols;
268 if (nx == 1)
return lastRows;
269 if (nx == 2)
return (
mb - row0) + lastRows;
270 return (
mb - row0) + (nx - 2) *
mb + lastRows;
281 if (ny <= 1)
return lastCols;
282 if (ny <= 2)
return (nb - col0) + lastCols;
283 return (nb - col0) + (ny - 2) * nb + lastCols;
300 const auto [row0, col0, lastRows, lastCols] =
301 _get_tiles_info(ix, iy, nx, ny);
306 return Matrix<T>(pHandle, this->ix + ix, this->iy + iy, nx, ny,
307 row0, col0, lastRows, lastCols);
323 const auto [ix, iy, nx, ny, row0, col0, lastRows, lastCols] =
326 return Matrix<T>(pHandle, this->ix + ix, this->iy + iy, nx, ny,
327 row0, col0, lastRows, lastCols);
343 idx_t ny)
const noexcept
345 const auto [row0, col0, lastRows, lastCols] =
346 _get_tiles_info(ix, iy, nx, ny);
352 ny, row0, col0, lastRows, lastCols);
366 idx_t
colEnd)
const noexcept
368 const auto [ix, iy, nx, ny, row0, col0, lastRows, lastCols] =
372 ny, row0, col0, lastRows, lastCols);
387 out <<
"starpu::Matrix<" <<
typeid(T).
name()
388 <<
">( nrows = " <<
A.nrows() <<
", ncols = " <<
A.ncols()
390 if (
A.ncols() <= 10) {
391 out << std::scientific << std::setprecision(2) <<
"\n";
392 for (idx_t i = 0; i <
A.nrows(); ++i) {
393 for (idx_t j = 0; j <
A.ncols(); ++j) {
405 std::shared_ptr<starpu_data_handle_t> pHandle;
430 void create_grid(idx_t
mt, idx_t
nt)
noexcept
434 .filter_func =
filter_rows, .nchildren = nx, .filter_arg =
mt};
438 .filter_func = filter_cols, .nchildren = ny, .filter_arg =
nt};
440 starpu_data_map_filters(*pHandle, 2, &row_split, &col_split);
451 MatrixEntry<T> map_to_entry(idx_t i, idx_t j)
noexcept override
456 assert((i >= 0 && i <
nrows()) &&
"Row index out of bounds");
457 assert((j >= 0 && j <
ncols()) &&
"Column index out of bounds");
459 const idx_t ix = (i + row0) / mb;
460 const idx_t iy = (j + col0) / nb;
461 const idx_t row = (i + row0) % mb;
462 const idx_t col = (j + col0) % nb;
464 const idx_t pos[2] = {row, col};
466 return MatrixEntry<T>(
467 starpu_data_get_sub_data(*pHandle, 2, ix + this->ix,
484 std::array<idx_t, 4> _get_tiles_info(idx_t ix,
487 idx_t ny)
const noexcept
489 assert(ix >= 0 && iy >= 0 && ix + nx <= this->nx &&
490 iy + ny <= this->ny &&
"Invalid tile indices");
491 assert(nx >= 0 && ny >= 0 &&
"Invalid number of tiles");
496 const idx_t row0 = (ix == 0) ? this->row0 : 0;
497 const idx_t col0 = (iy == 0) ? this->col0 : 0;
502 else if (ix + nx == this->nx)
503 lastRows = this->lastRows;
504 else if (ix + nx == 1)
505 lastRows = mb - row0;
512 else if (iy + ny == this->ny)
513 lastCols = this->lastCols;
514 else if (iy + ny == 1)
515 lastCols = nb - col0;
519 return {row0, col0, lastRows, lastCols};
533 std::array<idx_t, 8> _map_to_tiles(idx_t rowStart,
536 idx_t colEnd)
const noexcept
540 const idx_t
nrows = rowEnd - rowStart;
541 const idx_t
ncols = colEnd - colStart;
543 assert(rowStart >= 0 && colStart >= 0 &&
544 "Submatrix starts before the beginning of the matrix");
545 assert(rowEnd >= rowStart && colEnd >= colStart &&
546 "Submatrix has negative dimensions");
547 assert(rowEnd <= this->
nrows() && colEnd <= this->
ncols() &&
548 "Submatrix ends after the end of the matrix");
550 const idx_t ix = (rowStart + this->row0) / mb;
551 const idx_t iy = (colStart + this->col0) / nb;
552 const idx_t row0 = (rowStart + this->row0) % mb;
553 const idx_t col0 = (colStart + this->col0) % nb;
555 const idx_t nx = (
nrows == 0) ? 1 : (row0 +
nrows - 1) / mb + 1;
556 const idx_t ny = (
ncols == 0) ? 1 : (col0 +
ncols - 1) / nb + 1;
558 const idx_t lastRows =
559 (nx == 1) ?
nrows : (row0 +
nrows - 1) % mb + 1;
560 const idx_t lastCols =
561 (ny == 1) ?
ncols : (col0 +
ncols - 1) % nb + 1;
563 return {ix, iy, nx, ny, row0, col0, lastRows, lastCols};
Class for representing a matrix in StarPU that is split into tiles.
Definition Matrix.hpp:133
Matrix(T *ptr, idx_t m, idx_t n, idx_t ld, idx_t mt, idx_t nt) noexcept
Create a matrix of size m-by-n from a pointer in main memory.
Definition Matrix.hpp:150
constexpr idx_t get_ny() const noexcept
Get number of tiles in y direction.
Definition Matrix.hpp:217
Matrix< T > map_to_tiles(idx_t rowStart, idx_t rowEnd, idx_t colStart, idx_t colEnd) noexcept
Create a submatrix from starting and ending indices.
Definition Matrix.hpp:318
constexpr Matrix(T *ptr, idx_t m, idx_t n, idx_t mt, idx_t nt) noexcept
Create a matrix of size m-by-n from contiguous data in main memory.
Definition Matrix.hpp:177
constexpr idx_t ncols() const noexcept override
Get the number of columns in the matrix.
Definition Matrix.hpp:278
constexpr idx_t get_nx() const noexcept
Get number of tiles in x direction.
Definition Matrix.hpp:214
Matrix< const T > get_const_tiles(idx_t ix, idx_t iy, idx_t nx, idx_t ny) const noexcept
Create a const submatrix from a list of tiles.
Definition Matrix.hpp:340
Matrix< T > get_tiles(idx_t ix, idx_t iy, idx_t nx, idx_t ny) noexcept
Create a submatrix from a list of tiles.
Definition Matrix.hpp:298
constexpr idx_t nblockcols() const noexcept
Get the maximum number of columns of a tile.
Definition Matrix.hpp:226
constexpr idx_t nrows() const noexcept override
Get the number of rows in the matrix.
Definition Matrix.hpp:265
Matrix< const T > map_to_const_tiles(idx_t rowStart, idx_t rowEnd, idx_t colStart, idx_t colEnd) const noexcept
Create a const submatrix from starting and ending indices.
Definition Matrix.hpp:363
Tile tile(idx_t ix, idx_t iy) noexcept
Get the data handle of a tile in the matrix or the data handle of the matrix if it is not partitioned...
Definition Matrix.hpp:234
friend std::ostream & operator<<(std::ostream &out, const starpu::Matrix< T > &A)
Display matrix in output stream.
Definition Matrix.hpp:384
constexpr idx_t nblockrows() const noexcept
Get the maximum number of rows of a tile.
Definition Matrix.hpp:220
constexpr Matrix(const std::shared_ptr< starpu_data_handle_t > &pHandle, idx_t ix, idx_t iy, idx_t nx, idx_t ny, idx_t row0, idx_t col0, idx_t lastRows, idx_t lastCols) noexcept
Create a submatrix from a handle and a grid.
Definition Matrix.hpp:182
Filters for StarPU data interfaces.
void filter_rows(void *father_interface, void *child_interface, STARPU_ATTRIBUTE_UNUSED struct starpu_data_filter *f, unsigned id, unsigned nparts) noexcept
StarPU filter to partition a matrix along the x (row) dimension.
Definition filters.hpp:105
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
Class for representing a tile of a matrix.
Definition Tile.hpp:25
constexpr MatrixEntry< T > operator()(idx_t i, idx_t j) noexcept
Returns a reference to an element of the matrix.
Definition Matrix.hpp:100
constexpr MatrixEntry< T > operator[](idx_t i) noexcept
Returns a reference to an element of the vector.
Definition Matrix.hpp:112
constexpr T operator()(idx_t i, idx_t j) const noexcept
Returns an element of the matrix.
Definition Matrix.hpp:57
constexpr T operator[](idx_t i) const noexcept
Returns an element of the vector.
Definition Matrix.hpp:70
Class for accessing the elements of a tlapack::starpu::Matrix.
Definition Matrix.hpp:39