| 1 |
/*! \file SquareBlockMatrix.hpp |
| 2 |
\brief Block storage for symmetric/unsymmetric Square matrix |
| 3 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 4 |
\date Jun. 11th 2013 |
| 5 |
\date Jul. 12th 2015 |
| 6 |
\date Nov. 30th 2016 |
| 7 |
*/ |
| 8 |
|
| 9 |
// This file is part of Dissection |
| 10 |
// |
| 11 |
// Dissection is free software: you can redistribute it and/or modify |
| 12 |
// it under the terms of the GNU General Public License as published by |
| 13 |
// the Free Software Foundation, either version 3 of the License, or |
| 14 |
// (at your option) any later version. |
| 15 |
// |
| 16 |
// Linking Dissection statically or dynamically with other modules is making |
| 17 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 18 |
// the GNU General Public License cover the whole combination. |
| 19 |
// |
| 20 |
// As a special exception, the copyright holders of Dissection give you |
| 21 |
// permission to combine Dissection program with free software programs or |
| 22 |
// libraries that are released under the GNU LGPL and with independent modules |
| 23 |
// that communicate with Dissection solely through the Dissection-fortran |
| 24 |
// interface. You may copy and distribute such a system following the terms of |
| 25 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 26 |
// provided that you include the source code of that other code when and as |
| 27 |
// the GNU GPL requires distribution of source code and provided that you do |
| 28 |
// not modify the Dissection-fortran interface. |
| 29 |
// |
| 30 |
// Note that people who make modified versions of Dissection are not obligated |
| 31 |
// to grant this special exception for their modified versions; it is their |
| 32 |
// choice whether to do so. The GNU General Public License gives permission to |
| 33 |
// release a modified version without this exception; this exception also makes |
| 34 |
// it possible to release a modified version which carries forward this |
| 35 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 36 |
// does not apply to your modified version of Dissection, and you must remove |
| 37 |
// this exception when you distribute your modified version. |
| 38 |
// |
| 39 |
// This exception is an additional permission under section 7 of the GNU |
| 40 |
// General Public License, version 3 ("GPLv3") |
| 41 |
// |
| 42 |
// Dissection is distributed in the hope that it will be useful, |
| 43 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 44 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 45 |
// GNU General Public License for more details. |
| 46 |
// |
| 47 |
// You should have received a copy of the GNU General Public License |
| 48 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 49 |
// |
| 50 |
|
| 51 |
#ifndef _ALGEBRA_SQUAREBLOCKMATRIX_HPP |
| 52 |
#define _ALGEBRA_SQUAREBLOCKMATRIX_HPP |
| 53 |
#include <cstdio> |
| 54 |
#include <cmath> |
| 55 |
#include <vector> |
| 56 |
|
| 57 |
using std::vector; |
| 58 |
|
| 59 |
template<typename T> |
| 60 |
class SquareBlockMatrix |
| 61 |
{ |
| 62 |
public: |
| 63 |
|
| 64 |
SquareBlockMatrix() : _dim(0), _size(0), _block_size(0), _block_size2(0), |
| 65 |
_block_size_last(0), _isSym(true) |
| 66 |
{ |
| 67 |
_isblocked = false; |
| 68 |
_isdecomposed = false; |
| 69 |
_num_blocks = 0; |
| 70 |
_num_blocks0 = 0; |
| 71 |
_num_blocks1 = 0; |
| 72 |
_dim0 = 0; |
| 73 |
_dim1 = 0; |
| 74 |
_block_size_last0 = 0; |
| 75 |
_block_size_last1 = 0; |
| 76 |
_lower_allocated = false; |
| 77 |
_coefs_alloc_status = false; |
| 78 |
_pivrelaxed = false; |
| 79 |
} |
| 80 |
|
| 81 |
SquareBlockMatrix(int dim, int block_size, bool isSym) |
| 82 |
{ |
| 83 |
#ifdef DEBUG_SQUAREBLOCKMATRIX |
| 84 |
fprintf(stderr, "%s %d : constructor with args %d %d\n", |
| 85 |
__FILE__, __LINE__, dim, block_size); |
| 86 |
#endif |
| 87 |
if (dim > 0) { |
| 88 |
init(dim, block_size, isSym, 0); |
| 89 |
allocate(); |
| 90 |
_permute.resize(dim); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void init(int dim, int block_size, bool isSym, int first_block = 0); |
| 95 |
void allocateBlock(int i, int j); |
| 96 |
void allocate(); |
| 97 |
void free(int i, int j); |
| 98 |
void free(); |
| 99 |
|
| 100 |
~SquareBlockMatrix() { |
| 101 |
free(); |
| 102 |
if (_coefs_alloc_status) { |
| 103 |
delete [] _coefs; |
| 104 |
delete [] _allocation_status; |
| 105 |
_coefs_alloc_status = false; |
| 106 |
} |
| 107 |
_block_sizes.clear(); |
| 108 |
_nsing_block.clear(); |
| 109 |
_num_blocks = 0; // ? to avoid double free |
| 110 |
_dim = 0; |
| 111 |
_permute.clear(); |
| 112 |
_singIdx.clear(); |
| 113 |
_singIdx0.clear(); |
| 114 |
} |
| 115 |
|
| 116 |
int dimension() const { return (int)_dim; } |
| 117 |
int dimension0() const { return (int)_dim0; } |
| 118 |
int dimension1() const { return (int)_dim1; } |
| 119 |
int storage_size() const { return (int)_size; } |
| 120 |
int block_size() const { return (int)_block_size; } |
| 121 |
int num_blocks() const { return _num_blocks; } |
| 122 |
int block_size_last() const { return (int)_block_size_last; } |
| 123 |
bool isSym() const { return _isSym; } |
| 124 |
bool isDecomposed() const { return _isdecomposed; } |
| 125 |
int num_blocks0() const { return _num_blocks0; } |
| 126 |
int num_blocks1() const { return _num_blocks1; } |
| 127 |
int block_size_last0() const { return (int)_block_size_last0; } |
| 128 |
int block_size_last1() const { return (int)_block_size_last1; } |
| 129 |
int nrowBlock(int i) const { return _block_sizes[i]; } |
| 130 |
|
| 131 |
// int offsetBlock(int i, int j) const; |
| 132 |
|
| 133 |
T** coefs() { return _coefs; } |
| 134 |
T* addrCoefBlock(int i, int j); |
| 135 |
|
| 136 |
void allocateLowerBlocks() { |
| 137 |
if ((!_lower_allocated) && _isSym) { |
| 138 |
for (int i = 0; i < _num_blocks; i++) { |
| 139 |
for (int j = 0; j < i; j++) { |
| 140 |
const int nrow = _block_sizes[i]; |
| 141 |
const int ncol = _block_sizes[j]; |
| 142 |
const int itmp = i + j * _num_blocks; |
| 143 |
_coefs[itmp] = new T[nrow * ncol]; |
| 144 |
_allocation_status[itmp] = true; |
| 145 |
} |
| 146 |
} |
| 147 |
_lower_allocated = true; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void freeLowerBlocks() { |
| 152 |
#ifdef ALLOCATE_LOWER_BLOCKS |
| 153 |
if (_lower_allocated && _isSym) { |
| 154 |
#else |
| 155 |
if (_isSym) { |
| 156 |
#endif |
| 157 |
for (int i = 0; i < _num_blocks; i++) { |
| 158 |
for (int j = 0; j < i; j++) { |
| 159 |
const int itmp = i + j * _num_blocks; |
| 160 |
if (_allocation_status[itmp]) { |
| 161 |
delete [] _coefs[itmp]; |
| 162 |
} |
| 163 |
_allocation_status[itmp] = false; |
| 164 |
} |
| 165 |
} |
| 166 |
_lower_allocated = false; |
| 167 |
} |
| 168 |
else { |
| 169 |
fprintf(stderr, "double free _lower %s %d\n", __FILE__, __LINE__); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
int nrowBlock(int i, int j) const |
| 174 |
{ |
| 175 |
return (i <= j ? _block_sizes[i] : _block_sizes[j]); |
| 176 |
} |
| 177 |
|
| 178 |
int ncolBlock(int i, int j) const |
| 179 |
{ |
| 180 |
return (i <= j ? _block_sizes[j] : _block_sizes[i]); |
| 181 |
} |
| 182 |
|
| 183 |
bool isTransposed(int i, int j); |
| 184 |
|
| 185 |
int BlockIndex(int i) const { |
| 186 |
int iblock; |
| 187 |
if (_isdecomposed) { |
| 188 |
if (i < _dim0) { |
| 189 |
iblock = i / _block_size; |
| 190 |
} |
| 191 |
else { |
| 192 |
iblock = (i - _dim0) / _block_size + _num_blocks0; |
| 193 |
} |
| 194 |
} |
| 195 |
else { |
| 196 |
iblock = i / _block_size; |
| 197 |
} |
| 198 |
return iblock; |
| 199 |
} |
| 200 |
|
| 201 |
int IndexBlock(int iblock) { |
| 202 |
int index; |
| 203 |
if (_isdecomposed) { |
| 204 |
if (iblock < _num_blocks0) { |
| 205 |
index = iblock * _block_size; |
| 206 |
} |
| 207 |
else { |
| 208 |
index = _dim0 + (iblock - _num_blocks0) * _block_size; |
| 209 |
} |
| 210 |
} |
| 211 |
else { |
| 212 |
index = iblock * _block_size; |
| 213 |
} |
| 214 |
return index; |
| 215 |
} |
| 216 |
|
| 217 |
int BlockOffset(int i) const { |
| 218 |
int ires; |
| 219 |
if (_isdecomposed) { |
| 220 |
if (i < _dim0) { |
| 221 |
ires = i % _block_size; |
| 222 |
} |
| 223 |
else { |
| 224 |
ires = (i - _dim0) % _block_size; |
| 225 |
} |
| 226 |
} |
| 227 |
else { |
| 228 |
ires = i % _block_size; |
| 229 |
} |
| 230 |
return ires; |
| 231 |
} |
| 232 |
void copyFromArrayPermute(const T *a, int dim, int *permute); |
| 233 |
void copyFromArray(const T *a, int dim); |
| 234 |
void copyToArrayFull(const T *a,int dim); |
| 235 |
void copyBlockToArray(int i, int j, const T *a,int dim); |
| 236 |
|
| 237 |
void ZeroClear(); |
| 238 |
|
| 239 |
T& diag(int i); |
| 240 |
|
| 241 |
const T& diag(int i) const; |
| 242 |
|
| 243 |
T& operator () (int i, int j); |
| 244 |
|
| 245 |
const T& operator () (int i, int j) const; |
| 246 |
|
| 247 |
SquareBlockMatrix<T>* clone() const |
| 248 |
{ |
| 249 |
SquareBlockMatrix<T> *ret = new SquareBlockMatrix<T>; |
| 250 |
ret->copy(*this); |
| 251 |
return(ret); |
| 252 |
} |
| 253 |
|
| 254 |
void copy ( const SquareBlockMatrix& A ) |
| 255 |
{ |
| 256 |
// PlainMatrix<T>::copy(A); //_loc2glob=A._loc2glob; |
| 257 |
_block_size = A._block_size; |
| 258 |
_block_size2 = A._block_size2; |
| 259 |
_num_blocks = A._num_blocks; |
| 260 |
_block_size_last = A._block_size_last; |
| 261 |
_isSym = A._isSym; |
| 262 |
_rank = A._rank; |
| 263 |
_nsing = A._nsing; |
| 264 |
_nsing_block = A._nsing_block; |
| 265 |
_dim = A._dim; |
| 266 |
_size = A._size; |
| 267 |
_permute = A._permute; |
| 268 |
_kernelDetected = A._kernelDetected; |
| 269 |
_singIdx = A._singIdx; |
| 270 |
_singIdx0 = A._singIdx0; |
| 271 |
_isblocked = A._isblocked; |
| 272 |
} |
| 273 |
|
| 274 |
bool isBlocked() |
| 275 |
{ |
| 276 |
return _isblocked; |
| 277 |
} |
| 278 |
void unsetBlocked() |
| 279 |
{ |
| 280 |
_isblocked = false; |
| 281 |
} |
| 282 |
void setBlocked() |
| 283 |
{ |
| 284 |
_isblocked = true; |
| 285 |
} |
| 286 |
|
| 287 |
void set_rank(int rank) { |
| 288 |
_rank = rank; |
| 289 |
_nsing = _dim - _rank; |
| 290 |
} |
| 291 |
|
| 292 |
void set_KernelDetected(int state) { |
| 293 |
_kernelDetected = state; |
| 294 |
} |
| 295 |
|
| 296 |
int KernelDetected() const { |
| 297 |
return _kernelDetected; |
| 298 |
} |
| 299 |
|
| 300 |
vector<int> &getNsingBlock() { |
| 301 |
return _nsing_block; |
| 302 |
} |
| 303 |
|
| 304 |
vector<int> &getSingIdx() { |
| 305 |
return _singIdx; |
| 306 |
} |
| 307 |
|
| 308 |
vector<int> &getSingIdx0() { |
| 309 |
return _singIdx0; |
| 310 |
} |
| 311 |
|
| 312 |
int rank() const |
| 313 |
{ |
| 314 |
return _rank; |
| 315 |
} |
| 316 |
|
| 317 |
int dim_kern() const |
| 318 |
{ |
| 319 |
return _nsing; |
| 320 |
} |
| 321 |
void set_dim_kern(int nsing) |
| 322 |
{ _nsing = nsing; } |
| 323 |
|
| 324 |
int dim_kern_block(int k) const |
| 325 |
{ |
| 326 |
return _nsing_block[k]; |
| 327 |
} |
| 328 |
void set_dim_kern_block(int k, int nsing) |
| 329 |
{ |
| 330 |
_nsing_block[k] = nsing; |
| 331 |
} |
| 332 |
|
| 333 |
vector<int>& getPermute() |
| 334 |
{ |
| 335 |
return _permute; |
| 336 |
} |
| 337 |
const vector<int>& getPermute() const |
| 338 |
{ |
| 339 |
return _permute; |
| 340 |
} |
| 341 |
|
| 342 |
void set_lastPivot(double lastpiv) |
| 343 |
{ |
| 344 |
_lastpiv = lastpiv; |
| 345 |
} |
| 346 |
|
| 347 |
double lastPivot() const |
| 348 |
{ |
| 349 |
# ifdef DISDEBUG |
| 350 |
assert( isFactorized() ); |
| 351 |
# endif |
| 352 |
return _lastpiv; |
| 353 |
} |
| 354 |
|
| 355 |
void unset_pivrelaxed() { _pivrelaxed = false; } |
| 356 |
void set_pivrelaxed() { _pivrelaxed = true; } |
| 357 |
bool is_pivrelaxed() { return _pivrelaxed; } |
| 358 |
private: |
| 359 |
int _dim; //<! Dimension of the matrix |
| 360 |
int _size; |
| 361 |
int _block_size; |
| 362 |
int _block_size2; |
| 363 |
int _num_blocks; |
| 364 |
int _block_size_last; |
| 365 |
bool _isSym; |
| 366 |
vector<int> _block_sizes; |
| 367 |
int _dim0; |
| 368 |
int _dim1; |
| 369 |
int _block_size_last0; |
| 370 |
int _block_size_last1; |
| 371 |
int _num_blocks0; |
| 372 |
int _num_blocks1; |
| 373 |
bool _isdecomposed; |
| 374 |
// |
| 375 |
bool _coefs_alloc_status; |
| 376 |
bool *_allocation_status; |
| 377 |
bool _lower_allocated; |
| 378 |
// |
| 379 |
int _rank; //!< Rank of the matrix (initialized after factorization) |
| 380 |
int _nsing; |
| 381 |
double _lastpiv; // in case of quadruple ? |
| 382 |
T** _coefs; |
| 383 |
// |
| 384 |
bool _isblocked; |
| 385 |
vector<int> _nsing_block; |
| 386 |
vector<int> _permute; |
| 387 |
bool _kernelDetected; // true : done, false : unknown |
| 388 |
vector<int> _singIdx; // with permutation |
| 389 |
vector<int> _singIdx0; // without permutation |
| 390 |
bool _pivrelaxed; |
| 391 |
// |
| 392 |
}; // End class SquareMatrix |
| 393 |
|
| 394 |
#endif |