10#ifndef TLAPACK_STARPU_MATRIX_HH
11#define TLAPACK_STARPU_MATRIX_HH
39 template <
class T,
bool TisConstType>
46 virtual idx_t nrows()
const noexcept = 0;
47 virtual idx_t ncols()
const noexcept = 0;
48 virtual MatrixEntry<T> map_to_entry(idx_t i, idx_t j)
noexcept = 0;
61 return T(
A.map_to_entry(i, j));
73 assert((nrows() <= 1 || ncols() <= 1) &&
74 "Matrix is not a vector");
75 return (nrows() > 1) ? (*this)(i, 0) : (*this)(0, i);
83 virtual idx_t nrows()
const noexcept = 0;
84 virtual idx_t ncols()
const noexcept = 0;
85 virtual MatrixEntry<T> map_to_entry(idx_t i, idx_t j)
noexcept = 0;
103 return map_to_entry(i, j);
115 assert((nrows() <= 1 || ncols() <= 1) &&
116 "Matrix is not a vector");
117 return (nrows() > 1) ? (*this)(i, 0) : (*this)(0, i);
158 assert(m > 0 && n > 0 &&
"Invalid matrix size");
159 assert(
mt <= m &&
nt <= n &&
"Invalid tile size");
161 nx = (
mt == 0) ? 1 : ((m %
mt == 0) ? m /
mt : m /
mt + 1);
162 ny = (
nt == 0) ? 1 : ((n %
nt == 0) ? n /
nt : n /
nt + 1);
183 constexpr Matrix(
const std::shared_ptr<starpu_data_handle_t>& pHandle,
202 assert(ix >= 0 && iy >= 0 &&
"Invalid tile position");
203 assert(nx > 0 && ny > 0 &&
"Invalid tile size");
204 assert(row0 >= 0 && col0 >= 0 &&
"Invalid tile offset");
205 assert(lastRows >= 0 && lastCols >= 0 &&
"Invalid tile size");
237 assert(ix >= 0 && iy >= 0 && ix < nx && iy < ny &&
238 "Invalid tile index");
241 *pHandle, 2, ix + this->ix, iy + this->iy);
255 if (ix == nx - 1) m = lastRows;
256 if (iy == ny - 1) n = lastCols;
269 if (nx == 1)
return lastRows;
270 if (nx == 2)
return (
mb - row0) + lastRows;
271 return (
mb - row0) + (nx - 2) *
mb + lastRows;
282 if (ny <= 1)
return lastCols;
283 if (ny <= 2)
return (nb - col0) + lastCols;
284 return (nb - col0) + (ny - 2) * nb + lastCols;
301 const auto [row0, col0, lastRows, lastCols] =
302 _get_tiles_info(ix, iy, nx, ny);
307 return Matrix<T>(pHandle, this->ix + ix, this->iy + iy, nx, ny,
308 row0, col0, lastRows, lastCols);
324 const auto [ix, iy, nx, ny, row0, col0, lastRows, lastCols] =
327 return Matrix<T>(pHandle, this->ix + ix, this->iy + iy, nx, ny,
328 row0, col0, lastRows, lastCols);
344 idx_t ny)
const noexcept
346 const auto [row0, col0, lastRows, lastCols] =
347 _get_tiles_info(ix, iy, nx, ny);
353 ny, row0, col0, lastRows, lastCols);
367 idx_t
colEnd)
const noexcept
369 const auto [ix, iy, nx, ny, row0, col0, lastRows, lastCols] =
373 ny, row0, col0, lastRows, lastCols);
388 out <<
"starpu::Matrix<" <<
typeid(T).
name()
389 <<
">( nrows = " <<
A.nrows() <<
", ncols = " <<
A.ncols()
391 if (
A.ncols() <= 10) {
392 out << std::scientific << std::setprecision(2) <<
"\n";
393 for (idx_t i = 0; i <
A.nrows(); ++i) {
394 for (idx_t j = 0; j <
A.ncols(); ++j) {
406 std::shared_ptr<starpu_data_handle_t> pHandle;
431 void create_grid(idx_t
mt, idx_t
nt)
noexcept
435 .filter_func =
filter_rows, .nchildren = nx, .filter_arg =
mt};
439 .filter_func = filter_cols, .nchildren = ny, .filter_arg =
nt};
441 starpu_data_map_filters(*pHandle, 2, &row_split, &col_split);
452 MatrixEntry<T> map_to_entry(idx_t i, idx_t j)
noexcept override
457 assert((i >= 0 && i <
nrows()) &&
"Row index out of bounds");
458 assert((j >= 0 && j <
ncols()) &&
"Column index out of bounds");
460 const idx_t ix = (i + row0) / mb;
461 const idx_t iy = (j + col0) / nb;
462 const idx_t row = (i + row0) % mb;
463 const idx_t col = (j + col0) % nb;
465 const idx_t pos[2] = {row, col};
467 return MatrixEntry<T>(
468 starpu_data_get_sub_data(*pHandle, 2, ix + this->ix,
485 std::array<idx_t, 4> _get_tiles_info(idx_t ix,
488 idx_t ny)
const noexcept
490 assert(ix >= 0 && iy >= 0 && ix + nx <= this->nx &&
491 iy + ny <= this->ny &&
"Invalid tile indices");
492 assert(nx >= 0 && ny >= 0 &&
"Invalid number of tiles");
497 const idx_t row0 = (ix == 0) ? this->row0 : 0;
498 const idx_t col0 = (iy == 0) ? this->col0 : 0;
503 else if (ix + nx == this->nx)
504 lastRows = this->lastRows;
505 else if (ix + nx == 1)
506 lastRows = mb - row0;
513 else if (iy + ny == this->ny)
514 lastCols = this->lastCols;
515 else if (iy + ny == 1)
516 lastCols = nb - col0;
520 return {row0, col0, lastRows, lastCols};
534 std::array<idx_t, 8> _map_to_tiles(idx_t rowStart,
537 idx_t colEnd)
const noexcept
541 const idx_t
nrows = rowEnd - rowStart;
542 const idx_t
ncols = colEnd - colStart;
544 assert(rowStart >= 0 && colStart >= 0 &&
545 "Submatrix starts before the beginning of the matrix");
546 assert(rowEnd >= rowStart && colEnd >= colStart &&
547 "Submatrix has negative dimensions");
548 assert(rowEnd <= this->
nrows() && colEnd <= this->
ncols() &&
549 "Submatrix ends after the end of the matrix");
551 const idx_t ix = (rowStart + this->row0) / mb;
552 const idx_t iy = (colStart + this->col0) / nb;
553 const idx_t row0 = (rowStart + this->row0) % mb;
554 const idx_t col0 = (colStart + this->col0) % nb;
556 const idx_t nx = (
nrows == 0) ? 1 : (row0 +
nrows - 1) / mb + 1;
557 const idx_t ny = (
ncols == 0) ? 1 : (col0 +
ncols - 1) / nb + 1;
559 const idx_t lastRows =
560 (nx == 1) ?
nrows : (row0 +
nrows - 1) % mb + 1;
561 const idx_t lastCols =
562 (ny == 1) ?
ncols : (col0 +
ncols - 1) % nb + 1;
564 return {ix, iy, nx, ny, row0, col0, lastRows, lastCols};
Class for representing a matrix in StarPU that is split into tiles.
Definition Matrix.hpp:134
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:151
constexpr idx_t get_ny() const noexcept
Get number of tiles in y direction.
Definition Matrix.hpp:218
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:319
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:178
constexpr idx_t ncols() const noexcept override
Get the number of columns in the matrix.
Definition Matrix.hpp:279
constexpr idx_t get_nx() const noexcept
Get number of tiles in x direction.
Definition Matrix.hpp:215
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:341
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:299
constexpr idx_t nblockcols() const noexcept
Get the maximum number of columns of a tile.
Definition Matrix.hpp:227
constexpr idx_t nrows() const noexcept override
Get the number of rows in the matrix.
Definition Matrix.hpp:266
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:364
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:235
friend std::ostream & operator<<(std::ostream &out, const starpu::Matrix< T > &A)
Display matrix in output stream.
Definition Matrix.hpp:385
constexpr idx_t nblockrows() const noexcept
Get the maximum number of rows of a tile.
Definition Matrix.hpp:221
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:183
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:101
constexpr MatrixEntry< T > operator[](idx_t i) noexcept
Returns a reference to an element of the vector.
Definition Matrix.hpp:113
constexpr T operator()(idx_t i, idx_t j) const noexcept
Returns an element of the matrix.
Definition Matrix.hpp:58
constexpr T operator[](idx_t i) const noexcept
Returns an element of the vector.
Definition Matrix.hpp:71
Class for accessing the elements of a tlapack::starpu::Matrix.
Definition Matrix.hpp:40