Debian Codegraph @

3rdparty/dissection/src/Algebra/RectBlockMatrix.cpp

raw
1 /*! \file RectBlockMatrix.cpp
2 \brief Block storage for Rectangular 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 #include "Algebra/RectBlockMatrix.hpp"
52 #include "Compiler/blas.hpp"
53 #include <cstdio>
54
55 template<typename T>
56 void RectBlockMatrix<T>::init(int dim_r, int dim_c, int block_size, int first_block)
57 {
58 int itmp;
59
60 _dim_r = dim_r;
61 _dim_c = dim_c;
62 _block_size = block_size;
63
64 _block_size2 = _block_size * _block_size;
65
66 if ((dim_r * dim_c) == 0) {
67 _isdecomposed_c = false;
68 _num_blocks_r = 0;
69 _num_blocks_c = 0;
70 _block_size_last_r = 0;
71 _block_size_last_c = 0;
72 _coefs = NULL;
73 _allocation_status = NULL;
74 _deallocation_status = false;
75 return;
76 }
77 if (first_block > 0) {
78 _isdecomposed_c = true;
79 _dim0_c = first_block;
80 _dim1_c = _dim_c - _dim0_c;
81 _num_blocks_r = (_dim_r + _block_size - 1) / _block_size;
82 _num_blocks0_c = (_dim0_c + _block_size - 1) / _block_size;
83 _num_blocks1_c = (_dim1_c + _block_size - 1) / _block_size;
84 _num_blocks_c = _num_blocks0_c + _num_blocks1_c;
85 _block_sizes_r.resize(_num_blocks_r);
86 _block_sizes_c.resize(_num_blocks_c);
87 _indexblock_r.resize(_num_blocks_r + 1);
88 _indexblock_c.resize(_num_blocks_c + 1);
89
90 itmp = _dim_r % _block_size;
91 _block_size_last_r = (itmp == 0) ? _block_size : itmp;
92 int ktmp;
93 ktmp = 0;
94 if (_num_blocks_r > 0) {
95 for (int i = 0; i < (_num_blocks_r - 1); i++, ktmp += _block_size) {
96 _block_sizes_r[i] = _block_size;
97 _indexblock_r[i] = ktmp;
98 }
99 _block_sizes_r[_num_blocks_r - 1] = _block_size_last_r;
100 _indexblock_r[_num_blocks_r - 1] = ktmp;
101 _indexblock_r[_num_blocks_r] = _dim_r;
102 }
103 itmp = _dim0_c % _block_size;
104 _block_size_last0_c = (itmp == 0) ? _block_size : itmp;
105 ktmp = 0;
106 if (_num_blocks0_c > 0) {
107 for (int i = 0; i < (_num_blocks0_c - 1); i++, ktmp += _block_size) {
108 _block_sizes_c[i] = _block_size;
109 _indexblock_c[i] = ktmp;
110 }
111 _block_sizes_c[_num_blocks0_c - 1] = _block_size_last0_c;
112 _indexblock_c[_num_blocks0_c - 1] = ktmp;
113 }
114 itmp = _dim1_c % _block_size;
115 _block_size_last1_c = (itmp == 0) ? _block_size : itmp;
116 ktmp = _dim0_c;
117 if (_num_blocks1_c > 0) {
118 for (int i = _num_blocks0_c; i < (_num_blocks_c - 1);
119 i++, ktmp += _block_size) {
120 _block_sizes_c[i] = _block_size;
121 _indexblock_c[i] = ktmp;
122 }
123 _block_sizes_c[_num_blocks_c - 1] = _block_size_last1_c;
124 _indexblock_c[_num_blocks_c - 1] = ktmp;
125 }
126 _indexblock_c[_num_blocks_c] = _dim_c;
127 }
128 else {
129 _isdecomposed_c = false;
130 _num_blocks_r = (_dim_r + _block_size - 1) / _block_size;
131 _num_blocks_c = (_dim_c + _block_size - 1) / _block_size;
132 _block_sizes_r.resize(_num_blocks_r);
133 _block_sizes_c.resize(_num_blocks_c);
134 _indexblock_r.resize(_num_blocks_r + 1);
135 _indexblock_c.resize(_num_blocks_c + 1);
136
137 itmp = _dim_r % _block_size;
138 _block_size_last_r = (itmp == 0) ? _block_size : itmp;
139 int ktmp;
140 ktmp = 0;
141 if (_num_blocks_r > 0) {
142 for (int i = 0; i < (_num_blocks_r - 1); i++, ktmp += _block_size) {
143 _block_sizes_r[i] = _block_size;
144 _indexblock_r[i] = ktmp;
145 }
146 _block_sizes_r[_num_blocks_r - 1] = _block_size_last_r;
147 _indexblock_r[_num_blocks_r - 1] = ktmp;
148 _indexblock_r[_num_blocks_r] = _dim_r;
149 }
150 itmp = _dim_c % _block_size;
151 ktmp = 0;
152 if (_num_blocks_c > 0) {
153 _block_size_last_c = (itmp == 0) ? _block_size : itmp;
154 for (int i = 0; i < (_num_blocks_c - 1); i++, ktmp += _block_size) {
155 _block_sizes_c[i] = _block_size;
156 _indexblock_c[i] = ktmp;
157 }
158 _block_sizes_c[_num_blocks_c - 1] = _block_size_last_c;
159 _indexblock_c[_num_blocks_c - 1] = ktmp;
160 _indexblock_c[_num_blocks_c] = _dim_c;
161 }
162 }
163 if (_num_blocks_r * _num_blocks_c == 0) {
164 _coefs = NULL; // new T*[1]; // dummy
165 _allocation_status = NULL; // new bool[1]; // dummy
166 _deallocation_status = false;
167 // _deallocation_status = true;
168 }
169 else {
170 try {
171 _coefs = new T*[_num_blocks_r * _num_blocks_c]; // need to be managed
172 } catch (const std::bad_alloc& e) {
173 fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__,
174 e.what());
175 }
176 try {
177 _allocation_status = new bool[_num_blocks_r * _num_blocks_c];
178 } catch (const std::bad_alloc& e) {
179 fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__,
180 e.what());
181 }
182 for (int i = 0; i < (_num_blocks_r * _num_blocks_c); i++) {
183 _allocation_status[i] = false;
184 }
185 _deallocation_status = false;
186 }
187 }
188
189 template
190 void RectBlockMatrix<double>::init(int dim_r, int dim_c, int block_size,
191 int first_block);
192 template
193 void RectBlockMatrix<quadruple>::init(int dim_r, int dim_c, int block_size,
194 int first_block);
195 template
196 void RectBlockMatrix<complex<double> >::init(int dim_r, int dim_c,
197 int block_size, int first_block);
198 template
199 void RectBlockMatrix<complex<quadruple> >::init(int dim_r, int dim_c,
200 int block_size,
201 int first_block);
202
203 template
204 void RectBlockMatrix<float>::init(int dim_r, int dim_c, int block_size,
205 int first_block);
206 template
207 void RectBlockMatrix<complex<float> >::init(int dim_r, int dim_c,
208 int block_size, int first_block);
209
210 //
211
212 template<typename T>
213 void RectBlockMatrix<T>::allocateBlock(int i, int j)
214 {
215 const int nrow = _block_sizes_r[i];
216 const int ncol = _block_sizes_c[j];
217 if (!_allocation_status[i + j * _num_blocks_r]) { //
218 try {
219 _coefs[i + j * _num_blocks_r] = new T[nrow * ncol];
220 } catch (const std::bad_alloc& e) {
221 fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__,
222 e.what());
223 }
224 }
225 _allocation_status[i + j * _num_blocks_r] = true;
226 }
227
228 template
229 void RectBlockMatrix<double>::allocateBlock(int i, int j);
230 template
231 void RectBlockMatrix<quadruple>::allocateBlock(int i, int j);
232 template
233 void RectBlockMatrix<complex<double> >::allocateBlock(int i, int j);
234 template
235 void RectBlockMatrix<complex<quadruple> >::allocateBlock(int i, int j);
236 template
237 void RectBlockMatrix<float>::allocateBlock(int i, int j);
238 template
239 void RectBlockMatrix<complex<float> >::allocateBlock(int i, int j);
240
241 //
242
243 template<typename T>
244 void RectBlockMatrix<T>::allocate()
245 {
246 for (int i = 0; i < _num_blocks_r; i++) {
247 for (int j = 0; j < _num_blocks_c; j++) {
248 const int nrow = _block_sizes_r[i];
249 const int ncol = _block_sizes_c[j];
250 if (!_allocation_status[i + j * _num_blocks_r]) {
251 try {
252 _coefs[i + j * _num_blocks_r] = new T[nrow * ncol];
253 } catch (const std::bad_alloc& e) {
254 fprintf(stderr, "%s %d : allocation failed : %s", __FILE__, __LINE__,
255 e.what());
256 }
257 }
258 _allocation_status[i + j * _num_blocks_r] = true;
259 }
260 }
261 _deallocation_status = false;
262 }
263
264 template
265 void RectBlockMatrix<double>::allocate();
266 template
267 void RectBlockMatrix<quadruple>::allocate();
268 template
269 void RectBlockMatrix<complex<double> >::allocate();
270 template
271 void RectBlockMatrix<complex<quadruple> >::allocate();
272 template
273 void RectBlockMatrix<float>::allocate();
274 template
275 void RectBlockMatrix<complex<float> >::allocate();
276
277 //
278
279 template<typename T>
280 void RectBlockMatrix<T>::free(int i, int j)
281 {
282 if (_allocation_status[i + j * _num_blocks_r]) {
283 delete[] _coefs[i + j * _num_blocks_r]; // need to be mananged
284 }
285 _allocation_status[i + j * _num_blocks_r] = false;
286 }
287
288 template
289 void RectBlockMatrix<double>::free(int i, int j);
290 template
291 void RectBlockMatrix<quadruple>::free(int i, int j);
292 template
293 void RectBlockMatrix<complex<double> >::free(int i, int j);
294 template
295 void RectBlockMatrix<complex<quadruple> >::free(int i, int j);
296 template
297 void RectBlockMatrix<float>::free(int i, int j);
298 template
299 void RectBlockMatrix<complex<float> >::free(int i, int j);
300 //
301
302 template<typename T>
303 void RectBlockMatrix<T>::free()
304 {
305 for (int i = 0; i < _num_blocks_r; i++) {
306 for (int j = 0; j < _num_blocks_c; j++) {
307 if (_allocation_status[i + j * _num_blocks_r]) {
308 delete[] _coefs[i + j * _num_blocks_r]; // need to be managed
309 _allocation_status[i + j * _num_blocks_r] = false;
310 }
311 }
312 }
313 }
314
315 template
316 void RectBlockMatrix<double>::free();
317 template
318 void RectBlockMatrix<quadruple>::free();
319 template
320 void RectBlockMatrix<complex<double> >::free();
321 template
322 void RectBlockMatrix<complex<quadruple> >::free();
323 template
324 void RectBlockMatrix<float>::free();
325 template
326 void RectBlockMatrix<complex<float> >::free();
327 //
328
329 template<typename T>
330 T* RectBlockMatrix<T>::addrCoefBlock(int i, int j)
331 {
332 return _coefs[i + j * _num_blocks_r];
333 }
334
335 template
336 double* RectBlockMatrix<double>::addrCoefBlock(int i, int j);
337
338 template
339 quadruple* RectBlockMatrix<quadruple>::addrCoefBlock(int i, int j);
340
341 template
342 complex<double>* RectBlockMatrix<complex<double> >::
343 addrCoefBlock(int i, int j);
344
345 template
346 complex<quadruple>* RectBlockMatrix<complex<quadruple> >::
347 addrCoefBlock(int i, int j);
348
349 template
350 float* RectBlockMatrix<float>::addrCoefBlock(int i, int j);
351
352 template
353 complex<float>* RectBlockMatrix<complex<float> >::
354 addrCoefBlock(int i, int j);
355 //
356
357 template<typename T>
358 void RectBlockMatrix<T>::ZeroClear()
359 {
360 const T zero(0.0);
361 for (int i = 0; i < _num_blocks_r; i++) {
362 for (int j = 0; j < _num_blocks_c; j++) {
363 const int nrow = _block_sizes_r[i];
364 const int ncol = _block_sizes_c[j];
365 const int nsize = nrow * ncol;
366 const int itmp = i + j * _num_blocks_r;
367 for (int k = 0; k < nsize; k++) {
368 _coefs[itmp][k] = zero; // need to be replaced by memcpy
369 }
370 }
371 }
372 }
373
374 template
375 void RectBlockMatrix<double>::ZeroClear();
376
377 template
378 void RectBlockMatrix<quadruple>::ZeroClear();
379
380 template
381 void RectBlockMatrix<complex<double> >::ZeroClear();
382
383 template
384 void RectBlockMatrix<complex<quadruple> >::ZeroClear();
385
386 template
387 void RectBlockMatrix<float>::ZeroClear();
388
389 template
390 void RectBlockMatrix<complex<float> >::ZeroClear();
391 //
392
393 template<typename T>
394 T& RectBlockMatrix<T>::operator () (int i, int j)
395 {
396 int i0, i1, j0, j1;
397
398 if (_isdecomposed_c) {
399 i0 = i / _block_size;
400 i1 = i % _block_size;
401 if (j < _dim0_c) {
402 j0 = j / _block_size;
403 j1 = j % _block_size;
404 }
405 else {
406 j0 = (j - _dim0_c) / _block_size + _num_blocks0_c;
407 j1 = (j - _dim0_c) % _block_size;
408 }
409 }
410 else {
411 i0 = i / _block_size;
412 j0 = j / _block_size;
413 i1 = i % _block_size;
414 j1 = j % _block_size;
415 }
416 return _coefs[i0 + j0 * _num_blocks_r][i1 + j1 * _block_sizes_r[i0]];
417 }
418
419 template
420 double& RectBlockMatrix<double>::operator () (int i, int j);
421
422 template
423 quadruple& RectBlockMatrix<quadruple>::operator () (int i, int j);
424
425 template
426 complex<double> & RectBlockMatrix<complex<double> >::
427 operator () (int i, int j);
428
429 template
430 complex<quadruple> & RectBlockMatrix<complex<quadruple> >::
431 operator () (int i, int j);
432
433 template
434 float& RectBlockMatrix<float>::operator () (int i, int j);
435
436 template
437 complex<float> & RectBlockMatrix<complex<float> >::
438 operator () (int i, int j);
439
440 //
441
442 template<typename T>
443 const T& RectBlockMatrix<T>::operator () (int i, int j) const
444 {
445 int i0, i1, j0, j1;
446
447 i0 = i / _block_size;
448 j0 = j / _block_size;
449 i1 = i % _block_size;
450 j1 = j % _block_size;
451 return _coefs[i0 + j0 * _num_blocks_r][i1 + j1 * _block_sizes_r[i0]];
452 }
453
454 template
455 const double& RectBlockMatrix<double>::operator () (int i, int j) const;
456
457 template
458 const quadruple& RectBlockMatrix<quadruple>::operator () (int i, int j) const;
459
460 template
461 const complex<double>& RectBlockMatrix<complex<double> >::
462 operator () (int i, int j) const;
463
464 template
465 const complex<quadruple>& RectBlockMatrix<complex<quadruple> >::
466 operator () (int i, int j) const;
467
468 template
469 const float& RectBlockMatrix<float>::operator () (int i, int j) const;
470
471 template
472 const complex<float>& RectBlockMatrix<complex<float> >::
473 operator () (int i, int j) const;
474 //