Arcane  4.1.11.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestCSRMatrixView.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/*---------------------------------------------------------------------------*/
9/*
10 * This file is based on the work on AMGCL library (version march 2026)
11 * which can be found at https://github.com/ddemidov/amgcl.
12 *
13 * Copyright (c) 2012-2022 Denis Demidov <dennis.demidov@gmail.com>
14 * SPDX-License-Identifier: MIT
15 */
16/*---------------------------------------------------------------------------*/
17/*---------------------------------------------------------------------------*/
18
19#include <gtest/gtest.h>
20
21#include "arccore/base/PlatformUtils.h"
22
23#include "arccore/alina/Adapters.h"
24#include "arccore/alina/BuiltinBackend.h"
25
26#include "arccore/alina/CSRMatrixView.h"
27#include "arccore/alina/CSRMatrix.h"
28
29#include "SampleProblemCommon.h"
30
31using namespace Arcane;
32using namespace Arcane::Alina;
33
34namespace
35{
36template <class Val, class Col, class Ptr>
39{
40 return CSRMatrixView<Val,Col,Ptr>(matrix.nbRow(), matrix.nbNonZero(),
41 matrix.ptr.data(), matrix.col.data(), matrix.val.data());
42}
43
45template <class Val, class Col, class Ptr, class T> void
46doTestScale1(CSRMatrix<Val, Col, Ptr>& A, T s, Int32 nb_loop)
47{
48 const ptrdiff_t nb_row = backend::nbRow(A);
49
50 std::cout << "DO_TEST_SCALE1 nb_row=" << nb_row
51 << " nb_non_zero=" << A.nbNonZero() << "\n";
52 Ptr* row_ptr = &A.ptr[0];
53 Real t1 = Platform::getRealTime();
54 for (Int32 z = 0; z < nb_loop; ++z) {
55 for (Int32 i = 0; i < nb_row; ++i) {
56 for (ptrdiff_t j = row_ptr[i], e = row_ptr[i + 1]; j < e; ++j)
57 A.val[j] *= s;
58 }
59 }
60 Real t2 = Platform::getRealTime();
61 std::cout << "Scale1_Time=" << (t2 - t1) << "\n";
62}
64template <class Val, class Col, class Ptr, class T> void
65doTestScale2(CSRMatrixView<Val, Col, Ptr> A, T s, Int32 nb_loop)
66{
67 const ptrdiff_t nb_row = A.nbRow();
68
69 std::cout << "DO_TEST_SCALE2 nb_row=" << nb_row
70 << " nb_non_zero=" << A.nbNonZero() << "\n";
71 const bool is_verbose = false;
72 if (is_verbose) {
73 for (Int32 i = 0; i < 5; ++i) {
74 for (auto ci : A.rowRange(i))
75 std::cout << "X=" << i << " j=" << A.column(ci) << " v=" << A.value(ci) << "\n";
76 }
77 }
78 Real t1 = Platform::getRealTime();
79 auto ptrs = A.rowIndexes();
80 auto values = A.values(); //.data();
81 for (Int32 z = 0; z < nb_loop; ++z) {
82 for (Int32 i = 0; i < nb_row; ++i) {
83 for (Int32 j = ptrs[i], e = ptrs[i + 1]; j < e; ++j)
84 values[j] *= s;
85 }
86 }
87 Real t2 = Platform::getRealTime();
88 std::cout << "Scale2_Time=" << (t2 - t1) << "\n";
89
90 for (Int32 z = 0; z < nb_loop; ++z) {
91 for (auto row : A.rows()) {
92 for (auto ci : row)
93 A.value(ci) *= s;
94 }
95 }
96 Real t3 = Platform::getRealTime();
97 std::cout << "Scale3_Time=" << (t3 - t2) << "\n";
98
99 {
100 Int32 nb_done = 0;
101 for (auto row : A.rows()) {
102 for ([[maybe_unused]] auto ci : row) {
103 ++nb_done;
104 }
105 }
106 ASSERT_EQ(nb_done, A.nbNonZero());
107 }
108
109 if (is_verbose) {
110 for (Int32 i = 0; i < 5; ++i) {
111 for (auto ci : A.rowRange(i))
112 std::cout << "I=" << i << " v=" << A.value(ci) << "\n";
113 }
114 }
115}
116} // namespace
117
118TEST(alina_test_csr_matrix_view, basic)
119{
120 std::vector<Int32> ptr;
121 std::vector<Int32> col;
122 std::vector<double> val;
123 std::vector<double> rhs;
124
125 Int32 nb_square = 64;
126 size_t n = sample_problem(nb_square, val, col, ptr, rhs);
127 std::cout << "TEST_VIEW_BASIC\n";
128 auto A = Alina::adapter::zero_copy(n, ptr.data(), col.data(), val.data());
129 auto matrix_view = matrixView(*A);
130
131 ASSERT_EQ(A->nbRow(),matrix_view.nbRow());
132 ASSERT_EQ(A->nbNonZero(),matrix_view.nbNonZero());
133
134 Int32 nb_loop = 50;
135 if (arccoreIsDebug())
136 nb_loop = 5;
137 doTestScale1(*A, 2.0, nb_loop);
138 doTestScale2(matrixView(*A), 2.0, nb_loop);
139}
Sparse matrix stored in CSR (Compressed Sparse Row) format.
constexpr col_type column(CSRRowColumnIndex< RowIndexType > rc_index) const
Value of the matrix for the given RowColumnIndex rc_index.
constexpr CSRRowRange< RowIndexType > rows() const
Range of all rows of the matrix.
constexpr val_type & value(CSRRowColumnIndex< RowIndexType > rc_index) const
Value of the matrix for the given RowColumnIndex rc_index.
constexpr Int32 nbRow() const noexcept
Number of row.
constexpr RowIndexType nbNonZero() const noexcept
Number of non-zero in the matrix.
Real getRealTime()
Temps Real utilisé en secondes.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
bool arccoreIsDebug()
Vrai si la macro ARCCORE_DEBUG est définie.
Sparse matrix stored in CSR (Compressed Sparse Row) format.
Definition CSRMatrix.h:98