Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit c96b3c1

Browse files
authored
add Matrix expressions (#177)
* add Matrix expressions * clang-format * clang-format * clang-format * mv norm2(matrix) from Algebra to AlgebraExpr
1 parent b9c6dea commit c96b3c1

15 files changed

+315
-16
lines changed

_clang-format

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Language: Cpp
22
# BasedOnStyle: WebKit
33
AccessModifierOffset: -1
44
AlignAfterOpenBracket: true
5-
AlignConsecutiveAssignments: None
5+
AlignConsecutiveAssignments: false
66
AlignEscapedNewlines: DontAlign
77
AlignOperands: false
88
AlignTrailingComments: false
@@ -17,7 +17,7 @@ AlwaysBreakBeforeMultilineStrings: false
1717
AlwaysBreakTemplateDeclarations: No
1818
BinPackArguments: true
1919
BinPackParameters: true
20-
BreakBeforeBinaryOperators: None
20+
BreakBeforeBinaryOperators: false
2121

2222
BreakBeforeBraces: Custom
2323
BraceWrapping:

src/core/alien/core/backend/IInternalLinearAlgebraExprT.h

+31
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class IInternalLinearAlgebraExpr
8383
*/
8484
virtual Real norm2(const Vector& x) const = 0;
8585

86+
/*!
87+
* \brief Compute L2 (Frobenous) norm of a matrix
88+
* \param[in] x The matrix on which norm2 is computed
89+
* \returns The norm2 of the matrix
90+
*/
91+
virtual Real norm2(const Matrix& x) const = 0;
92+
8693
/*!
8794
* \brief Compute a matrix vector product
8895
*
@@ -125,6 +132,23 @@ class IInternalLinearAlgebraExpr
125132
*/
126133
virtual void copy(const Vector& x, Vector& r) const = 0;
127134

135+
/*!
136+
* \brief Copy a matrix in another one
137+
*
138+
* \param[in] x The vector to copy
139+
* \param[in,out] r The copied vector
140+
*/
141+
virtual void copy(const Matrix& a, Matrix& r) const = 0;
142+
143+
/*!
144+
* \brief Add a matrix to another one
145+
*
146+
* \param[in] a The matrix to copy
147+
*
148+
* \param[in,out] r The copied vector
149+
*/
150+
virtual void add(const Matrix& a, Matrix& r) const = 0;
151+
128152
/*!
129153
* \brief Compute the dot product of two vectors
130154
* \param[in] x The first vector
@@ -140,6 +164,13 @@ class IInternalLinearAlgebraExpr
140164
*/
141165
virtual void scal(Real alpha, Vector& x) const = 0;
142166

167+
/*!
168+
* \brief Scale a matrix by a factor
169+
* \param[in] alpha The real value to scale with
170+
* \param[in,out] x The vector to be scaled
171+
*/
172+
virtual void scal(Real alpha, Matrix& a) const = 0;
173+
143174
/*!
144175
* \brief Extract the diagonal of a matrix in a vector
145176
* \param[in] a The matrix to extract the diagonal

src/core/alien/core/backend/IInternalLinearAlgebraT.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class IInternalLinearAlgebra
7777

7878
/*!
7979
* \brief Compute L2 norm of a vector
80-
* \param[in] x The vector on which norm0 is computed
80+
* \param[in] x The vector on which norm2 is computed
8181
* \returns The norm2 of the vector
8282
*/
8383
virtual Real norm2(const Vector& x) const = 0;

src/core/alien/core/backend/LinearAlgebra.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class LinearAlgebra : public ILinearAlgebra
9393

9494
/*!
9595
* \brief Compute L2 norm of a vector
96-
* \param[in] x The vector on which norm0 is computed
96+
* \param[in] x The vector on which norm2 is computed
9797
* \returns The norm2 of the vector
9898
*/
9999
Real norm2(const IVector& x) const;

src/core/alien/core/backend/LinearAlgebraExpr.h

+32
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ class LinearAlgebraExpr
9797
*/
9898
Real norm2(const IVector& x) const;
9999

100+
/*!
101+
* \brief Compute L2 (Frobenous) norm of a matrix
102+
* \param[in] x The matrix on which norm2 is computed
103+
* \returns The norm2 of the matrix
104+
*/
105+
Real norm2(const IMatrix& x) const;
106+
100107
/*!
101108
* \brief Compute a matrix vector product
102109
*
@@ -139,6 +146,24 @@ class LinearAlgebraExpr
139146
*/
140147
void copy(const IVector& x, IVector& r) const;
141148

149+
/*!
150+
* \brief Copy a matrix in another one
151+
*
152+
* \param[in] x The matrix to copy
153+
* \param[in,out] r The copied matrix
154+
*/
155+
void copy(const IMatrix& x, IMatrix& r) const;
156+
157+
/*!
158+
* \brief Add two matrices A and B
159+
*
160+
*
161+
* \param[in] a The matrix A
162+
* \param[in] b The matrix B
163+
* \param[in,out] c The resulting matrix
164+
*/
165+
void add(const IMatrix& a, IMatrix& b) const;
166+
142167
/*!
143168
* \brief Compute the dot product of two vectors
144169
* \param[in] x The first vector
@@ -154,6 +179,13 @@ class LinearAlgebraExpr
154179
*/
155180
void scal(Real alpha, IVector& x) const;
156181

182+
/*!
183+
* \brief Scale a matrix by a factor
184+
* \param[in] alpha The real value to scale with
185+
* \param[in,out] x The vector to be scaled
186+
*/
187+
void scal(Real alpha, IMatrix& a) const;
188+
157189
/*!
158190
* \brief Extract the diagonal of a matrix in a vector
159191
* \param[in] a The matrix to extract the diagonal

src/core/alien/core/backend/LinearAlgebraExprT.h

+36
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ LinearAlgebraExpr<Tag, TagV>::norm2(const IVector& x) const
7777
/*---------------------------------------------------------------------------*/
7878
/*---------------------------------------------------------------------------*/
7979

80+
template <class Tag, class TagV>
81+
Arccore::Real
82+
LinearAlgebraExpr<Tag, TagV>::norm2(const IMatrix& x) const
83+
{
84+
const auto& mx = x.impl()->get<Tag>();
85+
return m_algebra->norm2(mx);
86+
}
87+
88+
/*---------------------------------------------------------------------------*/
89+
/*---------------------------------------------------------------------------*/
90+
8091
template <class Tag, class TagV>
8192
void LinearAlgebraExpr<Tag, TagV>::mult(const IMatrix& a, const IVector& x, IVector& r) const
8293
{
@@ -123,6 +134,24 @@ void LinearAlgebraExpr<Tag, TagV>::copy(const IVector& x, IVector& r) const
123134
m_algebra->copy(vx, vr);
124135
}
125136

137+
template <class Tag, class TagV>
138+
void LinearAlgebraExpr<Tag, TagV>::copy(const IMatrix& x, IMatrix& r) const
139+
{
140+
const auto& mx = x.impl()->get<TagV>();
141+
auto& mr = r.impl()->get<TagV>(true);
142+
ALIEN_ASSERT((mx.rowSpace() == mr.rowSpace()), ("Incompatible spaces"));
143+
m_algebra->copy(mx, mr);
144+
}
145+
146+
template <class Tag, class TagV>
147+
void LinearAlgebraExpr<Tag, TagV>::add(const IMatrix& a, IMatrix& b) const
148+
{
149+
const auto& ma = a.impl()->get<Tag>();
150+
auto& mb = b.impl()->get<Tag>(true);
151+
ALIEN_ASSERT((ma.rowSpace() == mb.rowSpace()), ("Incompatible spaces"));
152+
m_algebra->add(ma, mb);
153+
}
154+
126155
/*---------------------------------------------------------------------------*/
127156

128157
template <class Tag, class TagV>
@@ -166,6 +195,13 @@ void LinearAlgebraExpr<Tag, TagV>::scal(Real alpha, IVector& x) const
166195
m_algebra->scal(alpha, vx);
167196
}
168197

198+
template <class Tag, class TagV>
199+
void LinearAlgebraExpr<Tag, TagV>::scal(Real alpha, IMatrix& A) const
200+
{
201+
auto& ma = A.impl()->get<Tag>(true);
202+
m_algebra->scal(alpha, ma);
203+
}
204+
169205
/*---------------------------------------------------------------------------*/
170206
/*---------------------------------------------------------------------------*/
171207

src/core/alien/kernels/simple_csr/SimpleCSRMatrix.h

+17
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,23 @@ class SimpleCSRMatrix : public IMatrixImpl
282282
return matrix;
283283
}
284284

285+
void copy(SimpleCSRMatrix const& matrix)
286+
{
287+
m_is_parallel = matrix.m_is_parallel;
288+
m_local_size = matrix.m_local_size;
289+
m_local_offset = matrix.m_local_offset;
290+
m_global_size = matrix.m_global_size;
291+
m_ghost_size = matrix.m_ghost_size;
292+
m_send_policy = matrix.m_send_policy;
293+
m_recv_policy = matrix.m_recv_policy;
294+
m_nproc = matrix.m_nproc;
295+
m_myrank = matrix.m_myrank;
296+
m_parallel_mng = matrix.m_parallel_mng;
297+
m_trace = matrix.m_trace;
298+
m_matrix.copy(matrix.m_matrix);
299+
m_matrix_dist_info.copy(matrix.m_matrix_dist_info);
300+
}
301+
285302
void notifyChanges()
286303
{
287304
m_matrix.notifyChanges();

src/core/alien/kernels/simple_csr/algebra/CBLASMPIKernel.h

+26
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ class CBLASMPIKernel
9898
return value;
9999
}
100100

101+
template <typename Distribution, typename VectorT>
102+
static typename VectorT::ValueType nrm1(Distribution const& dist, const VectorT& x)
103+
{
104+
typedef typename VectorT::ValueType ValueType;
105+
typename VectorT::ValueType value = cblas::nrm1(x.scalarizedLocalSize(),
106+
(ValueType*)x.getDataPtr(), 1);
107+
if (dist.isParallel()) {
108+
value = Arccore::MessagePassing::mpAllReduce(
109+
dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value);
110+
}
111+
return value;
112+
}
113+
101114
template <typename Distribution, typename VectorT>
102115
static typename VectorT::ValueType nrm2(Distribution const& dist, const VectorT& x)
103116
{
@@ -110,6 +123,19 @@ class CBLASMPIKernel
110123
}
111124
return std::sqrt(value);
112125
}
126+
127+
template <typename Distribution, typename MatrixT>
128+
static typename MatrixT::ValueType matrix_nrm2(Distribution const& dist, const MatrixT& x)
129+
{
130+
typedef typename MatrixT::ValueType ValueType;
131+
typename MatrixT::ValueType value = cblas::dot(x.getProfile().getNnz(),
132+
(ValueType*)x.data(), 1, (ValueType*)x.data(), 1);
133+
if (dist.isParallel()) {
134+
value = Arccore::MessagePassing::mpAllReduce(
135+
dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value);
136+
}
137+
return std::sqrt(value);
138+
}
113139
};
114140

115141
} // namespace Alien

src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.cc

+41-4
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ Real SimpleCSRInternalLinearAlgebra::norm0(const CSRVector& vx ALIEN_UNUSED_PARA
9696

9797
/*---------------------------------------------------------------------------*/
9898

99-
Real SimpleCSRInternalLinearAlgebra::norm1(const CSRVector& vx ALIEN_UNUSED_PARAM) const
99+
Real SimpleCSRInternalLinearAlgebra::norm1(const CSRVector& vx) const
100100
{
101-
// return CBLASMPIKernel::nrm1(x.space().structInfo(),vx);
102-
throw NotImplementedException(
103-
A_FUNCINFO, "SimpleCSRLinearAlgebra::norm1 not implemented");
101+
#ifdef ALIEN_USE_PERF_TIMER
102+
SentryType s(m_timer, "CSR-NORM1");
103+
#endif
104+
105+
return CBLASMPIKernel::nrm1(vx.distribution(), vx);
104106
}
105107

106108
/*---------------------------------------------------------------------------*/
@@ -290,6 +292,15 @@ Real SimpleCSRInternalLinearAlgebraExpr::norm2(const CSRVector& vx) const
290292

291293
/*---------------------------------------------------------------------------*/
292294

295+
Real SimpleCSRInternalLinearAlgebraExpr::norm2(const CSRMatrix& mx) const
296+
{
297+
#ifdef ALIEN_USE_PERF_TIMER
298+
SentryType s(m_timer, "MATRIX-CSR-NORM2");
299+
#endif
300+
return CBLASMPIKernel::matrix_nrm2(mx.distribution(), mx);
301+
}
302+
/*---------------------------------------------------------------------------*/
303+
293304
void SimpleCSRInternalLinearAlgebraExpr::mult(
294305
const CSRMatrix& ma, const CSRVector& vx, CSRVector& vr) const
295306
{
@@ -342,6 +353,32 @@ void SimpleCSRInternalLinearAlgebraExpr::copy(const CSRVector& vx, CSRVector& vr
342353
CBLASMPIKernel::copy(vx.distribution(), vx, vr);
343354
}
344355

356+
void SimpleCSRInternalLinearAlgebraExpr::copy(const CSRMatrix& ma, CSRMatrix& mr) const
357+
{
358+
#ifdef ALIEN_USE_PERF_TIMER
359+
SentryType s(m_timer, "MATRIX-CSR-COPY");
360+
#endif
361+
mr.copy(ma);
362+
}
363+
364+
void SimpleCSRInternalLinearAlgebraExpr::add(const CSRMatrix& ma, CSRMatrix& mr) const
365+
{
366+
#ifdef ALIEN_USE_PERF_TIMER
367+
SentryType s(m_timer, "MATRIX-CSR-ADD");
368+
#endif
369+
370+
cblas::axpy(mr.getProfile().getNnz(), 1, (CSRMatrix::ValueType*)ma.data(), 1, mr.data(), 1);
371+
}
372+
373+
void SimpleCSRInternalLinearAlgebraExpr::scal(Real alpha, CSRMatrix& mr) const
374+
{
375+
#ifdef ALIEN_USE_PERF_TIMER
376+
SentryType s(m_timer, "MATRIX-CSR-SCAL");
377+
#endif
378+
379+
cblas::scal(mr.getProfile().getNnz(), alpha, mr.data(), 1);
380+
}
381+
345382
/*---------------------------------------------------------------------------*/
346383

347384
Real SimpleCSRInternalLinearAlgebraExpr::dot(

src/core/alien/kernels/simple_csr/algebra/SimpleCSRInternalLinearAlgebra.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class SimpleCSRInternalLinearAlgebraExpr
184184
Real norm0(const Vector& x) const;
185185
Real norm1(const Vector& x) const;
186186
Real norm2(const Vector& x) const;
187+
Real norm2(const Matrix& x) const;
187188
void mult(const Matrix& a, const Vector& x, Vector& r) const;
188189
void axpy(Real alpha, const Vector& x, Vector& r) const;
189190
void aypx(Real alpha, Vector& y, const Vector& x) const;
@@ -194,7 +195,7 @@ class SimpleCSRInternalLinearAlgebraExpr
194195
void reciprocal(Vector& x) const;
195196
void pointwiseMult(const Vector& x, const Vector& y, Vector& w) const;
196197

197-
// IInternalLinearAlgebra interface.
198+
// IInternalLinearAlgebraExpr interface.
198199

199200
void mult(const Matrix& a, const UniqueArray<Real>& x, UniqueArray<Real>& r) const;
200201
void axpy(Real alpha, UniqueArray<Real> const& x, UniqueArray<Real>& r) const;
@@ -205,6 +206,10 @@ class SimpleCSRInternalLinearAlgebraExpr
205206

206207
void scal(Real alpha, UniqueArray<Real>& x) const;
207208

209+
void copy(const Matrix& a, Matrix& r) const;
210+
void add(const Matrix& a, Matrix& r) const;
211+
void scal(Real alpha, Matrix& r) const;
212+
208213
private:
209214
// No member.
210215
};

src/core/alien/kernels/simple_csr/algebra/alien_cblas.h

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void cblas_dcopy(const int n, const double* x, const int incx, double* y,
1818
const int incy);
1919
void cblas_sscal(const int n, const float alpha, float* x, const int incx);
2020
void cblas_dscal(const int n, const double alpha, double* x, const int incx);
21+
double cblas_dasum(const int n, const double* x, const int incx);
2122
double cblas_dnrm2(const int n, const double* x, const int incx);
2223

2324
#ifdef __cplusplus
@@ -68,6 +69,11 @@ void scal(const int n, const double alpha, double* x, const int incx)
6869
return cblas_dscal(n, alpha, x, incx);
6970
}
7071

72+
double nrm1(const int n, const double* x, const int incx)
73+
{
74+
return cblas_dasum(n, x, incx);
75+
}
76+
7177
double nrm2(const int n, const double* x, const int incx)
7278
{
7379
return cblas_dnrm2(n, x, incx);

src/refsemantic/alien/ref/data/scalar/Matrix.h

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class ALIEN_REFSEMANTIC_EXPORT Matrix final : public IMatrix
5454
Matrix(const Matrix&) = delete;
5555
Matrix& operator=(const Matrix&) = delete;
5656

57+
template <typename E>
58+
Matrix& operator=(const E&);
59+
5760
public:
5861
// Pour les expressions
5962
void visit(ICopyOnWriteMatrix&) const;

src/refsemantic/alien/ref/data/scalar/Vector.h

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class ALIEN_REFSEMANTIC_EXPORT Vector final : public IVector
5353
Vector(const Vector&) = delete;
5454
Vector& operator=(const Vector&) = delete;
5555

56-
// FIXME: not implemented !
5756
template <typename E>
5857
Vector& operator=(const E&);
5958

0 commit comments

Comments
 (0)