1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*! \file */
/* ************************************************************************
* Copyright (C) 2021-2022 Advanced Micro Devices, Inc. All rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ************************************************************************ */
#pragma once
#ifndef ROCSPARSE_MATRIX_GEBSX_HPP
#define ROCSPARSE_MATRIX_GEBSX_HPP
#include "rocsparse_vector.hpp"
template <memory_mode::value_t MODE,
rocsparse_direction direction_,
typename T,
typename I,
typename J>
struct gebsx_matrix
{
template <typename S>
using array_t = typename memory_traits<MODE>::template array_t<S>;
static constexpr rocsparse_direction dir = direction_;
J mb{};
J nb{};
I nnzb{};
rocsparse_direction block_direction{};
J row_block_dim{};
J col_block_dim{};
rocsparse_index_base base{};
rocsparse_storage_mode storage_mode{rocsparse_storage_mode_sorted};
array_t<I> ptr{};
array_t<J> ind{};
array_t<T> val{};
gebsx_matrix(){};
~gebsx_matrix(){};
gebsx_matrix(rocsparse_direction block_dir_,
J mb_,
J nb_,
I nnzb_,
J row_block_dim_,
J col_block_dim_,
rocsparse_index_base base_)
: mb(mb_)
, nb(nb_)
, nnzb(nnzb_)
, block_direction(block_dir_)
, row_block_dim(row_block_dim_)
, col_block_dim(col_block_dim_)
, base(base_)
, ptr((rocsparse_direction_row == direction_) ? ((mb > 0) ? (mb + 1) : 0)
: ((nb > 0) ? (nb + 1) : 0))
, ind(nnzb)
, val(size_t(nnzb) * row_block_dim * col_block_dim){};
template <memory_mode::value_t THAT_MODE>
explicit gebsx_matrix(const gebsx_matrix<THAT_MODE, direction_, T, I, J>& that_,
bool transfer = true)
: gebsx_matrix<MODE, direction_, T, I, J>(that_.block_direction,
that_.mb,
that_.nb,
that_.nnzb,
that_.row_block_dim,
that_.col_block_dim,
that_.base)
{
if(transfer)
{
this->transfer_from(that_);
}
}
explicit gebsx_matrix(const gebsx_matrix<MODE, direction_, T, I, J>& that_,
bool transfer = true)
: gebsx_matrix<MODE, direction_, T, I, J>(that_.block_direction,
that_.mb,
that_.nb,
that_.nnzb,
that_.row_block_dim,
that_.col_block_dim,
that_.base)
{
if(transfer)
{
this->transfer_from(that_);
}
}
void unit_check(gebsx_matrix<memory_mode::host, direction_, T, I, J>& that,
bool check_values = true)
{
unit_check_enum(this->block_direction, that.block_direction);
unit_check_scalar<J>(this->mb, that.mb);
unit_check_scalar<J>(this->nb, that.nb);
unit_check_scalar<I>(this->nnzb, that.nnzb);
unit_check_scalar<J>(this->row_block_dim, that.row_block_dim);
unit_check_scalar<J>(this->col_block_dim, that.col_block_dim);
switch(direction_)
{
case rocsparse_direction_row:
{
if(this->mb > 0)
{
this->ptr.unit_check(that.ptr);
}
break;
}
case rocsparse_direction_column:
{
if(this->nb > 0)
{
this->ptr.unit_check(that.ptr);
}
break;
}
}
if(this->nnzb > 0)
{
this->ind.unit_check(that.ind);
}
if(check_values)
{
if(this->nnzb > 0)
{
this->val.unit_check(that.val);
}
}
}
void info() const
{
std::cout << "INFO GEBSX " << std::endl;
std::cout << " dir : " << direction_ << std::endl;
std::cout << " mb : " << this->mb << std::endl;
std::cout << " nb : " << this->nb << std::endl;
std::cout << " nnzb : " << this->nnzb << std::endl;
std::cout << " dirb : " << this->block_direction << std::endl;
std::cout << " row_block_dim : " << this->row_block_dim << std::endl;
std::cout << " col_block_dim : " << this->col_block_dim << std::endl;
std::cout << " base : " << this->base << std::endl;
}
bool is_invalid() const
{
if(this->mb < 0)
return true;
if(this->nb < 0)
return true;
if(this->nnzb < 0)
return true;
if(this->nnzb > this->mb * this->nb)
return true;
if(this->row_block_dim <= 0)
return true;
if(this->col_block_dim <= 0)
return true;
switch(direction_)
{
case rocsparse_direction_row:
{
if(this->mb > 0)
{
if(this->ptr.size() != (this->mb + 1))
{
return true;
}
}
break;
}
case rocsparse_direction_column:
{
if(this->nb > 0)
{
if(this->ptr.size() != (this->nb + 1))
{
return true;
}
}
break;
}
}
return false;
};
void define(rocsparse_direction block_dir_,
J mb_,
J nb_,
I nnzb_,
J row_block_dim_,
J col_block_dim_,
rocsparse_index_base base_)
{
if(block_dir_ != this->block_direction)
{
this->block_direction = block_dir_;
}
if(row_block_dim_ != this->row_block_dim)
{
this->row_block_dim = row_block_dim_;
}
if(col_block_dim_ != this->col_block_dim)
{
this->col_block_dim = col_block_dim_;
}
if(mb_ != this->mb)
{
this->mb = mb_;
if(direction_ == rocsparse_direction_row)
{
this->ptr.resize((this->mb > 0) ? (this->mb + 1) : 0);
}
}
if(nb_ != this->nb)
{
this->nb = nb_;
if(direction_ == rocsparse_direction_column)
{
this->ptr.resize((this->nb > 0) ? (this->nb + 1) : 0);
}
}
if(nnzb_ != this->nnzb)
{
this->nnzb = nnzb_;
this->ind.resize(this->nnzb);
this->val.resize(size_t(this->nnzb) * this->col_block_dim * this->row_block_dim);
}
if(base_ != this->base)
{
this->base = base_;
}
};
template <memory_mode::value_t THAT_MODE>
void transfer_from(const gebsx_matrix<THAT_MODE, direction_, T, I, J>& that)
{
CHECK_HIP_THROW_ERROR((this->mb == that.mb && this->nb == that.nb && this->nnzb == that.nnzb
&& this->block_direction == that.block_direction
&& this->row_block_dim == that.row_block_dim
&& this->col_block_dim == that.col_block_dim
&& this->base == that.base)
? hipSuccess
: hipErrorInvalidValue);
if(this->mb > 0)
{
this->ptr.transfer_from(that.ptr);
}
if(this->nnzb > 0)
{
this->ind.transfer_from(that.ind);
this->val.transfer_from(that.val);
}
};
template <memory_mode::value_t THAT_MODE>
gebsx_matrix<MODE, direction_, T, I, J>&
operator()(const gebsx_matrix<THAT_MODE, direction_, T, I, J>& that, bool transfer = true)
{
this->define(that.block_direction,
that.mb,
that.nb,
that.nnzb,
that.row_block_dim,
that.col_block_dim,
that.base);
if(transfer)
{
this->transfer_from(that);
}
return *this;
};
};
template <rocsparse_direction direction_, typename T, typename I, typename J>
using host_gebsx_matrix = gebsx_matrix<memory_mode::host, direction_, T, I, J>;
template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
using host_gebsr_matrix = host_gebsx_matrix<rocsparse_direction_row, T, I, J>;
template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
using host_gebsc_matrix = host_gebsx_matrix<rocsparse_direction_column, T, I, J>;
template <rocsparse_direction direction_, typename T, typename I, typename J>
using device_gebsx_matrix = gebsx_matrix<memory_mode::device, direction_, T, I, J>;
template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
using device_gebsr_matrix = device_gebsx_matrix<rocsparse_direction_row, T, I, J>;
template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
using device_gebsc_matrix = device_gebsx_matrix<rocsparse_direction_column, T, I, J>;
template <rocsparse_direction direction_, typename T, typename I, typename J>
using managed_gebsx_matrix = gebsx_matrix<memory_mode::managed, direction_, T, I, J>;
template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
using managed_gebsr_matrix = managed_gebsx_matrix<rocsparse_direction_row, T, I, J>;
template <typename T, typename I = rocsparse_int, typename J = rocsparse_int>
using managed_gebsc_matrix = managed_gebsx_matrix<rocsparse_direction_column, T, I, J>;
#endif // ROCSPARSE_MATRIX_GEBSX_HPP
|