Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AlephCudaMatrix.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*
8 * CNC: Concurrent Number Cruncher
9 * Copyright (C) 2008 GOCAD/ASGA, INRIA/ALICE
10 *
11 * This program 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 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * If you modify this software, you should include a notice giving the
26 * name of the person performing the modification, the date of modification,
27 * and the reason for such modification.
28 *
29 * Contact: Luc Buatois
30 *
31 * buatois@gocad.org
32 *
33 * ASGA-INPL Bt. G
34 * Rue du Doyen Marcel Roubault - BP 40
35 * 54501 VANDOEUVRE LES NANCY
36 * FRANCE
37 *
38 * Note that the GNU General Public License does not permit incorporating
39 * the Software into proprietary programs.
40 */
41
42#include "arcane/aleph/AlephArcane.h"
43#include "arcane/aleph/cuda/AlephCuda.h"
44
45//---------------------------------------------------------------------------//
46//---------------------------------------------------------------------------//
47
48namespace Arcane
49{
50
51//---------------------------------------------------------------------------//
52//---------------------------------------------------------------------------//
53
54//---------------------------------------------------------------------------//
55
56CNC_Matrix::CNC_Matrix(long m, long n, Storage storage)
57{
58 storage_ = NONE;
59 allocate(m, n, storage, false);
60}
61
62//---------------------------------------------------------------------------//
63
64CNC_Matrix::CNC_Matrix(long n, Storage storage, bool symmetric_storage)
65{
66 storage_ = NONE;
67 allocate(n, n, storage, symmetric_storage);
68}
69
70//---------------------------------------------------------------------------//
71
73{
74 m_ = 0;
75 n_ = 0;
76 diag_size_ = 0;
77
78 row_ = NULL;
79 column_ = NULL;
80 diag_ = NULL;
81
82 storage_ = ROWS;
83 allocate(n, n, storage_, false);
84}
85
86//---------------------------------------------------------------------------//
87
88CNC_Matrix::~CNC_Matrix()
89{
90 deallocate();
91}
92
93//---------------------------------------------------------------------------//
94
96{
97 m_ = 0;
98 n_ = 0;
99 diag_size_ = 0;
100
101 row_ = NULL;
102 column_ = NULL;
103 diag_ = NULL;
104
105 storage_ = NONE;
106 rows_are_stored_ = false;
107 columns_are_stored_ = false;
108 symmetric_storage_ = false;
109 symmetric_tag_ = false;
110}
111
112//---------------------------------------------------------------------------//
113
114long CNC_Matrix::m() const
115{
116 return m_;
117}
118
119long CNC_Matrix::n() const
120{
121 return n_;
122}
123
124long CNC_Matrix::diag_size() const
125{
126 return diag_size_;
127}
128
129long CNC_Matrix::nnz() const
130{
131 long result = 0;
132 if (rows_are_stored()) {
133 for (long i = 0; i < m(); i++) {
134 result += row(i).nb_coeffs();
135 }
136 }
137 else if (columns_are_stored()) {
138 for (long j = 0; j < n(); j++) {
139 result += column(j).nb_coeffs();
140 }
141 }
142 else {
143 }
144 return result;
145}
146
147bool CNC_Matrix::rows_are_stored() const
148{
149 return rows_are_stored_;
150}
151
152bool CNC_Matrix::columns_are_stored() const
153{
154 return columns_are_stored_;
155}
156
157CNC_Matrix::Storage CNC_Matrix::storage() const
158{
159 return storage_;
160}
161
162bool CNC_Matrix::has_symmetric_storage() const
163{
164 return symmetric_storage_;
165}
166
167bool CNC_Matrix::is_square() const
168{
169 return (m_ == n_);
170}
171
172bool CNC_Matrix::is_symmetric() const
173{
174 return (symmetric_storage_ || symmetric_tag_);
175}
176
182{
183 symmetric_tag_ = x;
184}
185
191{
192 return row_[i];
193}
194
200{
201 return row_[i];
202}
203
209{
210 return column_[j];
211}
212
218{
219 return column_[j];
220}
221
225double CNC_Matrix::diag(long i) const
226{
227 return diag_[i];
228}
229
233void CNC_Matrix::add(long i, long j, double val)
234{
235 if (symmetric_storage_ && j > i) {
236 return;
237 }
238 if (i == j) {
239 diag_[i] += val;
240 }
241 if (rows_are_stored_) {
242 row(i).add(j, val);
243 }
244 if (columns_are_stored_) {
245 column(j).add(i, val);
246 }
247}
248
249//---------------------------------------------------------------------------//
250
252{
253 if (rows_are_stored_) {
254 for (long i = 0; i < m_; i++) {
255 row(i).sort();
256 }
257 }
258 if (columns_are_stored_) {
259 for (long j = 0; j < n_; j++) {
260 column(j).sort();
261 }
262 }
263}
264
265//---------------------------------------------------------------------------//
266
268{
269 if (rows_are_stored_) {
270 for (long i = 0; i < m_; i++) {
271 row(i).zero();
272 }
273 }
274 if (columns_are_stored_) {
275 for (long j = 0; j < n_; j++) {
276 column(j).zero();
277 }
278 }
279 for (long i = 0; i < diag_size_; i++) {
280 diag_[i] = 0.0;
281 }
282}
283
284//---------------------------------------------------------------------------//
285
287{
288 if (rows_are_stored_) {
289 for (long i = 0; i < m_; i++) {
290 row(i).clear();
291 }
292 }
293 if (columns_are_stored_) {
294 for (long j = 0; j < n_; j++) {
295 column(j).clear();
296 }
297 }
298 for (long i = 0; i < diag_size_; i++) {
299 diag_[i] = 0.0;
300 }
301}
302
303//---------------------------------------------------------------------------//
304
305void CNC_Matrix::deallocate()
306{
307 m_ = 0;
308 n_ = 0;
309 diag_size_ = 0;
310
311 if (row_ != NULL)
312 delete[] row_;
313 if (column_ != NULL)
314 delete[] column_;
315 if (diag_ != NULL)
316 delete[] diag_;
317 row_ = NULL;
318 column_ = NULL;
319 diag_ = NULL;
320
321 storage_ = NONE;
322 rows_are_stored_ = false;
323 columns_are_stored_ = false;
324 symmetric_storage_ = false;
325}
326
327//---------------------------------------------------------------------------//
328
329void CNC_Matrix::allocate(long m, long n, Storage storage, bool symmetric_storage)
330{
331 m_ = m;
332 n_ = n;
333 diag_size_ = (m < n) ? (m) : (n);
334 symmetric_storage_ = symmetric_storage;
335 symmetric_tag_ = false;
336 storage_ = storage;
337 switch (storage) {
338 case NONE:
339 break;
340 case ROWS:
341 rows_are_stored_ = true;
342 columns_are_stored_ = false;
343 break;
344 case COLUMNS:
345 rows_are_stored_ = false;
346 columns_are_stored_ = true;
347 break;
348 case ROWS_AND_COLUMNS:
349 rows_are_stored_ = true;
350 columns_are_stored_ = true;
351 break;
352 }
353 diag_ = new double[diag_size_];
354 for (long i = 0; i < diag_size_; i++) {
355 diag_[i] = 0.0;
356 }
357
358 if (rows_are_stored_) {
359 row_ = new CNCSparseRowColumn[m];
360 }
361 else {
362 row_ = NULL;
363 }
364
365 if (columns_are_stored_) {
366 column_ = new CNCSparseRowColumn[n];
367 }
368 else {
369 column_ = NULL;
370 }
371}
372
373//---------------------------------------------------------------------------//
374//---------------------------------------------------------------------------//
375
376} // namespace Arcane
377
378//---------------------------------------------------------------------------//
379//---------------------------------------------------------------------------//
void add(long index, double val)
CNC_Matrix(long m, long n, Storage storage=ROWS)
void add(long i, long j, double val)
void set_symmetric_tag(bool x)
CNCSparseRowColumn & row(long i)
double diag(long i) const
CNCSparseRowColumn & column(long j)
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --