| 1 |
/*! \file PlainMatrix.hpp |
| 2 |
\brief Common interface for plain matrices (square, rectangulars,...) |
| 3 |
\author Xavier Juvigny, ONERA |
| 4 |
\date Jan. 25th 2005 |
| 5 |
\author Atsushi Suzuki, Laboratoire Jacques-Louis Lions |
| 6 |
\date Jun. 11th 2013 |
| 7 |
\date Jul. 12th 2015 |
| 8 |
\date Nov. 30th 2016 |
| 9 |
*/ |
| 10 |
|
| 11 |
// This file is part of Dissection |
| 12 |
// |
| 13 |
// Dissection is free software: you can redistribute it and/or modify |
| 14 |
// it under the terms of the GNU General Public License as published by |
| 15 |
// the Free Software Foundation, either version 3 of the License, or |
| 16 |
// (at your option) any later version. |
| 17 |
// |
| 18 |
// Linking Dissection statically or dynamically with other modules is making |
| 19 |
// a combined work based on Disssection. Thus, the terms and conditions of |
| 20 |
// the GNU General Public License cover the whole combination. |
| 21 |
// |
| 22 |
// As a special exception, the copyright holders of Dissection give you |
| 23 |
// permission to combine Dissection program with free software programs or |
| 24 |
// libraries that are released under the GNU LGPL and with independent modules |
| 25 |
// that communicate with Dissection solely through the Dissection-fortran |
| 26 |
// interface. You may copy and distribute such a system following the terms of |
| 27 |
// the GNU GPL for Dissection and the licenses of the other code concerned, |
| 28 |
// provided that you include the source code of that other code when and as |
| 29 |
// the GNU GPL requires distribution of source code and provided that you do |
| 30 |
// not modify the Dissection-fortran interface. |
| 31 |
// |
| 32 |
// Note that people who make modified versions of Dissection are not obligated |
| 33 |
// to grant this special exception for their modified versions; it is their |
| 34 |
// choice whether to do so. The GNU General Public License gives permission to |
| 35 |
// release a modified version without this exception; this exception also makes |
| 36 |
// it possible to release a modified version which carries forward this |
| 37 |
// exception. If you modify the Dissection-fortran interface, this exception |
| 38 |
// does not apply to your modified version of Dissection, and you must remove |
| 39 |
// this exception when you distribute your modified version. |
| 40 |
// |
| 41 |
// This exception is an additional permission under section 7 of the GNU |
| 42 |
// General Public License, version 3 ("GPLv3") |
| 43 |
// |
| 44 |
// Dissection is distributed in the hope that it will be useful, |
| 45 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 46 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 47 |
// GNU General Public License for more details. |
| 48 |
// |
| 49 |
// You should have received a copy of the GNU General Public License |
| 50 |
// along with Dissection. If not, see <http://www.gnu.org/licenses/>. |
| 51 |
// |
| 52 |
|
| 53 |
#ifndef _ALGEBRA_PLAINMATRIX_ |
| 54 |
# define _ALGEBRA_PLAINMATRIX_ |
| 55 |
#include <cstdio> |
| 56 |
#include <cstring> |
| 57 |
#include <new> |
| 58 |
//#define DEBUG_MEMORY_PLAINMATRIX |
| 59 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 60 |
# include <cstdlib> |
| 61 |
#include <stdint.h> |
| 62 |
#endif |
| 63 |
|
| 64 |
template<typename T> |
| 65 |
class PlainMatrix |
| 66 |
{ |
| 67 |
public: |
| 68 |
|
| 69 |
PlainMatrix(bool isOwner = true) |
| 70 |
{ |
| 71 |
_isOwner = isOwner; |
| 72 |
_n = (-1); // not yet allocated |
| 73 |
_coefs = new T*; |
| 74 |
_coefs_alloc_status = true; |
| 75 |
} |
| 76 |
|
| 77 |
PlainMatrix(int nbElts) |
| 78 |
{ |
| 79 |
_isOwner = true; |
| 80 |
_n = nbElts; |
| 81 |
_coefs = new T*; |
| 82 |
_coefs_alloc_status = true; |
| 83 |
try { |
| 84 |
*_coefs = new T[nbElts]; |
| 85 |
} catch (const std::bad_alloc& e) { |
| 86 |
fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__, |
| 87 |
e.what()); |
| 88 |
} |
| 89 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 90 |
mem_get(); |
| 91 |
fprintf(stderr, "@@PlainMatrix constructor : %.6e %.6e @%x %d\n", |
| 92 |
(double)_mem_vrt * 4.0 / (1024.0 *1024.0), |
| 93 |
(double)_mem_res * 4.0 / (1024.0 *1024.0), |
| 94 |
*_coefs, (sizeof(T) * nbElts)); |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
virtual ~PlainMatrix() { |
| 99 |
free(); |
| 100 |
if (_coefs_alloc_status) { |
| 101 |
delete _coefs; // same as destructor |
| 102 |
_coefs_alloc_status = false; |
| 103 |
} |
| 104 |
} |
| 105 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 106 |
void mem_get() |
| 107 |
{ |
| 108 |
int pid = (int)getpid(); |
| 109 |
// cerr << "pid = " << pid << endl; |
| 110 |
char buf[256]; |
| 111 |
sprintf(buf, "/proc/%d/statm", pid); |
| 112 |
ifstream fin(buf); |
| 113 |
fin >> _mem_vrt >> _mem_res; |
| 114 |
fin.close(); |
| 115 |
} |
| 116 |
#endif |
| 117 |
int size() const { return _n; } |
| 118 |
|
| 119 |
inline T& operator [] (int i) { return (*_coefs)[i]; } |
| 120 |
inline const T& operator [] (int i) const { return (*_coefs)[i]; } |
| 121 |
|
| 122 |
virtual T& operator () (int i, int j) = 0; |
| 123 |
|
| 124 |
virtual const T& operator () (int i, int j) const = 0; |
| 125 |
|
| 126 |
T** addrCoefs_pt() { return _coefs; } |
| 127 |
|
| 128 |
const T** addrCoefs_pt() const { return _coefs; } |
| 129 |
|
| 130 |
inline T* addrCoefs() { return *_coefs; } |
| 131 |
|
| 132 |
inline const T* addrCoefs() const { return *_coefs; } |
| 133 |
|
| 134 |
protected: |
| 135 |
void init(int nbElts) |
| 136 |
{ |
| 137 |
_isOwner = true; |
| 138 |
if (_n == (-1)) { |
| 139 |
_n = nbElts; |
| 140 |
if (_n > 0) { |
| 141 |
try { |
| 142 |
*_coefs = new T[nbElts]; |
| 143 |
} catch (const std::bad_alloc& e) { |
| 144 |
fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__, |
| 145 |
e.what()); |
| 146 |
} |
| 147 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 148 |
mem_get(); |
| 149 |
fprintf(stderr, "@@PlainMatrix init : %.6e %.6e %d\n", |
| 150 |
(double)_mem_vrt * 4.0 / (1024.0 *1024.0), |
| 151 |
(double)_mem_res * 4.0 / (1024.0 *1024.0), |
| 152 |
(sizeof(T) * nbElts)); |
| 153 |
#endif |
| 154 |
} |
| 155 |
} // _n == (-1) |
| 156 |
else { |
| 157 |
#if 0 |
| 158 |
if (_n > nbElts) { |
| 159 |
fprintf(stderr, |
| 160 |
"%s %d : PlanMatrix::init(%d) : %d better deallocate %d\n", |
| 161 |
__FILE__, __LINE__, nbElts, _n, (_n - nbElts)); |
| 162 |
} |
| 163 |
#endif |
| 164 |
delete [] *_coefs; |
| 165 |
try { |
| 166 |
*_coefs = new T[nbElts]; |
| 167 |
} catch (const std::bad_alloc& e) { |
| 168 |
fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__, |
| 169 |
e.what()); |
| 170 |
} |
| 171 |
_n = nbElts; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void init(int nbElts, T* coefs, bool isOwner) |
| 176 |
{ |
| 177 |
_isOwner = isOwner; |
| 178 |
_n = nbElts; |
| 179 |
*_coefs = coefs; |
| 180 |
} |
| 181 |
|
| 182 |
void free() |
| 183 |
{ |
| 184 |
if (_n > 0 && _isOwner) { |
| 185 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 186 |
mem_get(); |
| 187 |
fprintf(stderr, "@@PlainMatrix free : %.6e %.6e @%x\n", |
| 188 |
(double)_mem_vrt * 4.0 / (1024.0 *1024.0), |
| 189 |
(double)_mem_res * 4.0 / (1024.0 *1024.0), |
| 190 |
*_coefs); |
| 191 |
#endif |
| 192 |
delete [] *_coefs; |
| 193 |
} |
| 194 |
_n = (-1); |
| 195 |
} |
| 196 |
|
| 197 |
T* coefs() |
| 198 |
{ |
| 199 |
return *_coefs; |
| 200 |
} |
| 201 |
|
| 202 |
const T* coefs() const |
| 203 |
{ |
| 204 |
return *_coefs; |
| 205 |
} |
| 206 |
|
| 207 |
void ZeroClear() |
| 208 |
{ |
| 209 |
#if 0 |
| 210 |
const T zero(0.0); |
| 211 |
for (int i = 0; i < _n; i++) { |
| 212 |
(*_coefs)[i] = zero; // memset() |
| 213 |
} |
| 214 |
#else |
| 215 |
memset((void *)*_coefs, 0, sizeof(T) * _n); |
| 216 |
#endif |
| 217 |
} |
| 218 |
|
| 219 |
virtual PlainMatrix<T>* clone() const = 0; |
| 220 |
|
| 221 |
void copy(const PlainMatrix<T> &M) |
| 222 |
{ |
| 223 |
if (_n <= 0) { |
| 224 |
try { |
| 225 |
*_coefs = new T[_n]; |
| 226 |
} catch (const std::bad_alloc& e) { |
| 227 |
fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__, |
| 228 |
e.what()); |
| 229 |
} |
| 230 |
} |
| 231 |
_isOwner = M._isOwner; |
| 232 |
_n = M._n; |
| 233 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 234 |
mem_get(); |
| 235 |
fprintf(stderr, "@@PlainMatrix copy : %.6e %.6e @%x %d\n", |
| 236 |
(double)_mem_vrt * 4.0 / (1024.0 *1024.0), |
| 237 |
(double)_mem_res * 4.0 / (1024.0 *1024.0), |
| 238 |
*_coefs, (sizeof(T) * _n)); |
| 239 |
#endif |
| 240 |
#if 0 |
| 241 |
for (int i = 0; i < _n; i++) { |
| 242 |
(*_coefs)[i] = (*(M._coefs))[i]; // memcopy |
| 243 |
} |
| 244 |
#else |
| 245 |
memcpy((void *)*_coefs, (void *)*(M._coefs), _n * sizeof(T)); |
| 246 |
#endif |
| 247 |
} |
| 248 |
|
| 249 |
PlainMatrix(const PlainMatrix& A) |
| 250 |
{ |
| 251 |
_isOwner = A._isOwner; |
| 252 |
_n = A._n; |
| 253 |
_coefs = A._coefs; |
| 254 |
} |
| 255 |
PlainMatrix& operator = (const PlainMatrix& A) |
| 256 |
{ |
| 257 |
if (this != &A) { |
| 258 |
_isOwner = A._isOwner; |
| 259 |
_n = A._n; |
| 260 |
_coefs = A._coefs; |
| 261 |
} |
| 262 |
return *this; |
| 263 |
} |
| 264 |
|
| 265 |
private: |
| 266 |
bool _isOwner; |
| 267 |
bool _coefs_alloc_status; |
| 268 |
T** _coefs; |
| 269 |
int _n; |
| 270 |
#ifdef DEBUG_MEMORY_PLAINMATRIX |
| 271 |
uint64_t _mem_vrt, _mem_res; |
| 272 |
#endif |
| 273 |
}; // end class PlainMatrix |
| 274 |
|
| 275 |
#endif |