Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
MVExpr.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 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/* MVExpr.h (C) 2000-2024 */
9/* */
10/*---------------------------------------------------------------------------*/
11/*---------------------------------------------------------------------------*/
12
13#ifndef ALIEN_EXPRESSION_MVEXPR_MVEXPR_H_
14#define ALIEN_EXPRESSION_MVEXPR_MVEXPR_H_
15
16#include <alien/kernels/simple_csr/algebra/SimpleCSRLinearAlgebra.h>
17#include <alien/utils/Precomp.h>
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Alien
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28class Matrix;
29class Vector;
30
31namespace MVExpr
32{
33 namespace lazy
34 {
35 struct add_tag
36 {};
37 struct minus_tag
38 {};
39 struct mult_tag
40 {};
41 struct div_tag
42 {};
43 struct dot_tag
44 {};
45 struct cst_tag
46 {};
47 struct ref_tag
48 {};
50 {};
51 struct eval_tag
52 {};
53 } // namespace lazy
54
55 template <class A, class B>
56 auto add(A&& a, B&& b)
57 {
58 return [=](auto visitor) { return visitor(lazy::add_tag{}, a(visitor), b(visitor)); };
59 }
60
61 template <class A, class B>
62 auto minus(A&& a, B&& b)
63 {
64 return
65 [=](auto visitor) { return visitor(lazy::minus_tag{}, a(visitor), b(visitor)); };
66 }
67
68 template <class A, class B>
69 auto mul(A&& a, B&& b)
70 {
71 return
72 [=](auto visitor) { return visitor(lazy::mult_tag{}, a(visitor), b(visitor)); };
73 }
74
75 template <class A, class B>
76 auto div(A&& a, B&& b)
77 {
78 return [=](auto visitor) { return visitor(lazy::div_tag{}, a(visitor), b(visitor)); };
79 }
80
82 {
83 template <class T>
84 VectorDistribution const* operator()(lazy::cst_tag, T c)
85 {
86 return nullptr;
87 }
88
89 VectorDistribution const* operator()(lazy::ref_tag, Matrix const& r)
90 {
91 return &r.distribution().rowDistribution();
92 }
93
94 VectorDistribution const* operator()(lazy::ref_tag, Vector const& r)
95 {
96 return &r.distribution();
97 }
98
99 template <typename L>
100 VectorDistribution const* operator()(lazy::add_tag, L const& l, Vector const& r)
101 {
102 return &r.distribution();
103 }
104
105 template <typename L>
106 auto operator()(lazy::minus_tag, L const& l, Vector const& r)
107 {
108 return &r.distribution();
109 }
110
111 auto operator()(
113 {
114 if (l)
115 return l;
116 else
117 return r;
118 }
119
120 template <typename R>
121 auto operator()(lazy::mult_tag, Matrix const& l, Vector const& r)
122 {
123 return &r.distribution();
124 }
125
126 template <typename R>
127 auto operator()(lazy::mult_tag, Matrix const& l, R const& r)
128 {
129 return &l.distribution().rowDistribution();
130 }
131
132 template <typename L>
133 auto operator()(lazy::mult_tag, L const& l, Vector const& r)
134 {
135 return &r.distribution();
136 }
137 };
138
139 template <class A, class B>
140 auto scalMul(A&& a, B&& b)
141 {
142 return [=](auto visitor) {
143 return visitor(
144 lazy::dot_tag{}, a(distribution_evaluator()), a(visitor), b(visitor));
145 };
146 }
147
148 template <class T>
149 auto cst(T expr)
150 {
151 return [=](auto visitor) { return visitor(lazy::cst_tag{}, expr); };
152 }
153
154 template <class T>
155 auto ref(T const& expr)
156 {
157 return [&](auto visitor) -> decltype(visitor(lazy::ref_tag{}, expr)) {
158 return visitor(lazy::ref_tag{}, expr);
159 };
160 }
161
162 struct cpu_evaluator;
163 struct alloc_size_evaluator;
164
165 template <typename T>
166 auto matrixMult(Matrix const& matrix, UniqueArray<T> const& x)
167 {
168#ifdef DEBUG
169 std::cout << "\t\t MatrixVectorMult" << std::endl;
170#endif
171 std::size_t n = matrix.distribution().localRowSize();
172 UniqueArray<T> y(n, 0.);
173 SimpleCSRLinearAlgebraExpr alg;
174 alg.mult(matrix, x, y);
175 return std::move(y);
176 }
177
178 template <typename Tag, typename T>
179 auto matrixMultT(Matrix const& matrix, UniqueArray<T> const& x)
180 {
181 std::size_t n = matrix.distribution().localRowSize();
182 UniqueArray<T> y(n, 0.);
183 LinearAlgebraExpr<Tag> alg(matrix.distribution().parallelMng());
184 alg.mult(matrix, x, y);
185 return std::move(y);
186 }
187
188 template <typename T>
189 auto vectorAdd(UniqueArray<T> const& x, UniqueArray<T> const& y)
190 {
191#ifdef DEBUG
192 std::cout << "\t\t VectorAdd" << std::endl;
193#endif
194 std::size_t n = x.size();
195 UniqueArray<T> result(n, 0.);
196 SimpleCSRLinearAlgebraExpr alg;
197 alg.copy(y, result);
198 alg.axpy(1., x, result);
199 return std::move(result);
200 }
201
202 template <typename Tag, typename T>
203 auto vectorAddT(UniqueArray<T> const& x, UniqueArray<T> const& y)
204 {
205 std::size_t n = x.size();
206 UniqueArray<T> result(n, 0.);
207 LinearAlgebraExpr<Tag> alg(nullptr);
208 alg.copy(y, result);
209 alg.axpy(1., x, result);
210 return std::move(result);
211 }
212
213 template <typename Tag>
214 auto matrixAddT(Matrix const& a, Matrix const& b)
215 {
216 LinearAlgebraExpr<Tag> alg(a.distribution().parallelMng());
217 Matrix c(a.distribution());
218 alg.copy(b, c);
219 alg.add(a, c);
220 return std::move(c);
221 }
222
223 //template <typename T>
224 auto matrixAdd(Matrix const& a, Matrix const& b)
225 {
226#ifdef DEBUG
227 std::cout << "\t\t MatrixAdd" << std::endl;
228#endif
229
230 Matrix c(a.distribution());
231 SimpleCSRLinearAlgebraExpr alg;
232 alg.copy(b, c);
233 alg.add(a, c);
234 return std::move(c);
235 }
236
237 template <typename T>
238 auto vectorMinus(UniqueArray<T> const& x, UniqueArray<T> const& y)
239 {
240#ifdef DEBUG
241 std::cout << "\t\t VectorMinus" << std::endl;
242#endif
243 std::size_t n = x.size();
244 UniqueArray<T> result(n, 0.);
245
246 SimpleCSRLinearAlgebraExpr alg;
247 alg.copy(x, result);
248 alg.axpy(-1., y, result);
249 return std::move(result);
250 }
251
252 template <typename Tag, typename T>
253 auto vectorMinusT(UniqueArray<T> const& x, UniqueArray<T> const& y)
254 {
255 std::size_t n = x.size();
256 UniqueArray<T> result(n, 0.);
257
258 LinearAlgebraExpr<Tag> alg(nullptr);
259 alg.copy(x, result);
260 alg.axpy(-1., y, result);
261 return std::move(result);
262 }
263
264 template <typename T>
265 auto vectorMult(T const& lambda, UniqueArray<T> const& x)
266 {
267#ifdef DEBUG
268 std::cout << "\t\t VectorScal" << std::endl;
269#endif
270 std::size_t n = x.size();
271 UniqueArray<T> y(n, 0.);
272 SimpleCSRLinearAlgebraExpr alg;
273 alg.axpy(lambda, x, y);
274 return std::move(y);
275 }
276
277 template <typename Tag, typename T>
278 auto vectorMultT(T const& lambda, UniqueArray<T> const& x)
279 {
280 std::size_t n = x.size();
281 UniqueArray<T> y(n, 0.);
282 LinearAlgebraExpr<Tag> alg(nullptr);
283 alg.axpy(lambda, x, y);
284 return std::move(y);
285 }
286
287 auto vectorScalProduct(Vector const& a, Vector const& b)
288 {
289#ifdef DEBUG
290 std::cout << "\t\t VectorScal" << std::endl;
291#endif
292 SimpleCSRLinearAlgebraExpr alg;
293 return alg.dot(a, b);
294 }
295
296 template <typename Tag>
297 auto vectorScalProductT(Vector const& a, Vector const& b)
298 {
299 LinearAlgebraExpr<Tag> alg(a.distribution().parallelMng());
300 return alg.dot(a, b);
301 }
302
303 auto vectorScalProduct(
304 VectorDistribution const* distribution, Vector const& a, UniqueArray<Real> const& b)
305 {
306#ifdef DEBUG
307 std::cout << "\t\t VectorScal" << std::endl;
308#endif
309 SimpleCSRVector<Real> const& csr_a = a.impl()->get<BackEnd::tag::simplecsr>();
310 SimpleCSRLinearAlgebraExpr alg;
311 Integer local_size = distribution ? distribution->localSize() : b.size();
312 Real value = alg.dot(local_size, csr_a.getArrayValues(), b);
313 if (distribution && distribution->isParallel())
314 return Arccore::MessagePassing::mpAllReduce(
315 distribution->parallelMng(), Arccore::MessagePassing::ReduceSum, value);
316 else
317 return value;
318 }
319
320 template <typename Tag>
321 auto vectorScalProductT(
322 VectorDistribution const* distribution, Vector const& a, UniqueArray<Real> const& b)
323 {
324 auto const& csr_a = a.impl()->get<Tag>();
325 LinearAlgebraExpr<Tag> alg(nullptr);
326 Integer local_size = distribution ? distribution->localSize() : b.size();
327 Real value = alg.dot(local_size, csr_a.getArrayValues(), b);
328 if (distribution && distribution->isParallel())
329 return Arccore::MessagePassing::mpAllReduce(
330 distribution->parallelMng(), Arccore::MessagePassing::ReduceSum, value);
331 else
332 return value;
333 }
334
335 auto vectorScalProduct(
336 VectorDistribution const* distribution, UniqueArray<Real> const& a, Vector const& b)
337 {
338#ifdef DEBUG
339 std::cout << "\t\t VectorScal" << std::endl;
340#endif
341 SimpleCSRVector<Real> const& csr_b = b.impl()->get<BackEnd::tag::simplecsr>();
342 SimpleCSRLinearAlgebraExpr alg;
343 Integer local_size = distribution ? distribution->localSize() : a.size();
344 Real value = alg.dot(local_size, a, csr_b.getArrayValues());
345 if (distribution && distribution->isParallel())
346 return Arccore::MessagePassing::mpAllReduce(
347 distribution->parallelMng(), Arccore::MessagePassing::ReduceSum, value);
348 else
349 return value;
350 }
351
352 template <typename Tag>
353 auto vectorScalProductT(
354 VectorDistribution const* distribution, UniqueArray<Real> const& a, Vector const& b)
355 {
356 auto const& csr_b = b.impl()->get<Tag>();
357 LinearAlgebraExpr<Tag> alg(nullptr);
358 Integer local_size = distribution ? distribution->localSize() : a.size();
359 Real value = alg.dot(local_size, a, csr_b.getArrayValues());
360 if (distribution && distribution->isParallel())
361 return Arccore::MessagePassing::mpAllReduce(
362 distribution->parallelMng(), Arccore::MessagePassing::ReduceSum, value);
363 else
364 return value;
365 }
366
367 auto vectorScalProduct(VectorDistribution const* distribution,
368 UniqueArray<Real> const& a, UniqueArray<Real> const& b)
369 {
370#ifdef DEBUG
371 std::cout << "\t\t VectorScal" << std::endl;
372#endif
373 SimpleCSRLinearAlgebraExpr alg;
374 Integer local_size = distribution ? distribution->localSize() : a.size();
375 Real value = alg.dot(local_size, a, b);
376 if (distribution && distribution->isParallel())
377 return Arccore::MessagePassing::mpAllReduce(
378 distribution->parallelMng(), Arccore::MessagePassing::ReduceSum, value);
379 else
380 return value;
381 }
382
383 template <typename Tag>
384 auto vectorScalProductT(VectorDistribution const* distribution,
385 UniqueArray<Real> const& a, UniqueArray<Real> const& b)
386 {
387 LinearAlgebraExpr<Tag> alg(distribution->parallelMng());
388 Integer local_size = distribution ? distribution->localSize() : a.size();
389 Real value = alg.dot(local_size, a, b);
390 if (distribution && distribution->isParallel())
391 return Arccore::MessagePassing::mpAllReduce(
392 distribution->parallelMng(), Arccore::MessagePassing::ReduceSum, value);
393 else
394 return value;
395 }
396
397 template <typename Tag, typename T>
398 auto matrixScalT(T const& lambda, Matrix const& A)
399 {
400 Matrix B(A.distribution());
401 LinearAlgebraExpr<Tag> alg(A.distribution().parallelMng());
402 alg.copy(A, B);
403 alg.scal(lambda, B);
404 return std::move(B);
405 }
406
407 template <typename T>
408 auto matrixScal(T const& lambda, Matrix const& A)
409 {
410#ifdef DEBUG
411 std::cout << "\t\t MatrixScal" << std::endl;
412#endif
413 Matrix B(A.distribution());
414 SimpleCSRLinearAlgebraExpr alg;
415 alg.copy(A, B);
416 alg.scal(lambda, B);
417 return std::move(B);
418 }
419
421 {
422
423 template <typename T>
424 auto operator()(lazy::cst_tag, T c)
425 {
426#ifdef DEBUG
427 std::cout << "\t return cst" << std::endl;
428#endif
429 return c;
430 }
431
432 template <typename T>
433 T const& operator()(lazy::ref_tag, T const& r)
434 {
435 return r;
436 }
437
438 template <typename T>
439 auto operator()(lazy::mult_tag, Matrix const& a, UniqueArray<T> const& b)
440 {
441#ifdef DEBUG
442 std::cout << "\t visit A*b" << std::endl;
443#endif
444 return matrixMult(a, b);
445 }
446
447 // template<typename T>
448 auto operator()(lazy::mult_tag, Matrix const& a, Vector const& b)
449 {
450#ifdef DEBUG
451 std::cout << "\t visit A*b" << std::endl;
452#endif
453 SimpleCSRMatrix<Real> const& csr_matrix = a.impl()->get<BackEnd::tag::simplecsr>();
455 csr_b.resize(csr_matrix.getAllocSize());
456 return matrixMult(a, csr_b.getArrayValues());
457 }
458
459 auto operator()(lazy::mult_tag, Real lambda, Vector const& b)
460 {
461#ifdef DEBUG
462 std::cout << "\t visit lambda*b : " << b.name() << std::endl;
463#endif
465 return vectorMult(lambda, csr_b.getArrayValues());
466 }
467
468 auto operator()(lazy::mult_tag, Real lambda, Matrix const& a)
469 {
470 return matrixScal(lambda, a);
471 }
472
473 auto operator()(lazy::add_tag, Vector const& a, Vector const& b)
474 {
475#ifdef DEBUG
476 std::cout << "\t visit a+b" << std::endl;
477#endif
480 return vectorAdd(csr_a.getArrayValues(), csr_b.getArrayValues());
481 }
482
483 auto operator()(lazy::add_tag, Vector const& a, UniqueArray<Real> const& b)
484 {
485#ifdef DEBUG
486 std::cout << "\t visit a+b" << std::endl;
487#endif
489 return vectorAdd(csr_a.getArrayValues(), b);
490 }
491
492 template <class T>
493 auto operator()(lazy::add_tag, UniqueArray<T> const& a, UniqueArray<T> const& b)
494 {
495#ifdef DEBUG
496 std::cout << "\t visit a+b" << std::endl;
497#endif
498 return vectorAdd(a, b);
499 }
500
501 auto operator()(lazy::add_tag, Matrix const& a, Matrix const& b)
502 {
503#ifdef DEBUG
504 std::cout << "\t visit a+b" << std::endl;
505#endif
506 return matrixAdd(a, b);
507 }
508
509 // template<class A, class B>
510 // auto operator()(lazy::add_tag, A a, B b) { return a + b; }
511
512 auto operator()(lazy::minus_tag, Vector const& a, Vector const& b)
513 {
514#ifdef DEBUG
515 std::cout << "\t visit a-b" << std::endl;
516#endif
519 return vectorMinus(csr_a.getArrayValues(), csr_b.getArrayValues());
520 }
521
522 auto operator()(lazy::minus_tag, Vector const& a, UniqueArray<Real> const& b)
523 {
524#ifdef DEBUG
525 std::cout << "\t visit a-b" << std::endl;
526#endif
528 return vectorMinus(csr_a.getArrayValues(), b);
529 }
530
531 auto operator()(lazy::dot_tag, [[maybe_unused]] const VectorDistribution* distribution,
532 Vector const& a, Vector const& b)
533 {
534#ifdef DEBUG
535 std::cout << "\t visit dot(a,b)" << std::endl;
536#endif
537 return vectorScalProduct(a, b);
538 }
539
540 auto operator()(lazy::dot_tag, VectorDistribution const* distribution,
541 Vector const& a, UniqueArray<Real> const& b)
542 {
543#ifdef DEBUG
544 std::cout << "\t visit dot(a,b)" << std::endl;
545#endif
546 return vectorScalProduct(distribution, a, b);
547 }
548
549 auto operator()(lazy::dot_tag, VectorDistribution const* distribution,
550 UniqueArray<Real> const& a, Vector const& b)
551 {
552#ifdef DEBUG
553 std::cout << "\t visit dot(a,b)" << std::endl;
554#endif
555 return vectorScalProduct(distribution, a, b);
556 }
557
558 auto operator()(lazy::dot_tag, VectorDistribution const* distribution,
559 UniqueArray<Real> const& a, UniqueArray<Real> const& b)
560 {
561#ifdef DEBUG
562 std::cout << "\t visit dot(a,b)" << std::endl;
563#endif
564 return vectorScalProduct(distribution, a, b);
565 }
566
567 template <class A, class B>
568 auto operator()(lazy::minus_tag, A a, B b)
569 {
570 return a - b;
571 }
572 };
573
574 template <typename Tag>
576 {
577
578 template <typename T>
579 auto operator()(lazy::cst_tag, T c) { return c; }
580
581 template <typename T>
582 T const& operator()(lazy::ref_tag, T const& r) { return r; }
583
584 template <typename T>
585 auto operator()(lazy::mult_tag, Matrix const& a, UniqueArray<T> const& b)
586 {
587 return matrixMultT<Tag>(a, b);
588 }
589
590 auto operator()(lazy::mult_tag, Matrix const& a, Vector const& b)
591 {
592 auto const& tag_matrix = a.impl()->template get<Tag>();
593 auto const& tag_b = b.impl()->template get<Tag>();
594 tag_b.resize(tag_matrix.getAllocSize());
595 return matrixMultT<Tag>(a, tag_b.getArrayValues());
596 }
597
598 auto operator()(lazy::mult_tag, Real lambda, Vector const& b)
599 {
600 auto const& tag_b = b.impl()->template get<Tag>();
601 return vectorMultT<Tag>(lambda, tag_b.getArrayValues());
602 }
603
604 auto operator()(lazy::mult_tag, Real lambda, Matrix const& a)
605 {
606 return matrixScalT<Tag, Real>(lambda, a);
607 }
608
609 auto operator()(lazy::add_tag, Vector const& a, Vector const& b)
610 {
611 auto const& csr_a = a.impl()->template get<Tag>();
612 auto const& csr_b = b.impl()->template get<Tag>();
613 return vectorAddT<Tag>(csr_a.getArrayValues(), csr_b.getArrayValues());
614 }
615
616 auto operator()(lazy::add_tag, Vector const& a, UniqueArray<Real> const& b)
617 {
618 auto const& csr_a = a.impl()->template get<Tag>();
619 return vectorAddT<Tag>(csr_a.getArrayValues(), b);
620 }
621
622 template <class T>
623 auto operator()(lazy::add_tag, UniqueArray<T> const& a, UniqueArray<T> const& b)
624 {
625 return vectorAddT<Tag>(a, b);
626 }
627
628 auto operator()(lazy::add_tag, Matrix const& a, Matrix const& b)
629 {
630 return matrixAddT<Tag>(a, b);
631 }
632
633 auto operator()(lazy::minus_tag, Vector const& a, Vector const& b)
634 {
635 auto const& csr_a = a.impl()->template get<Tag>();
636 auto const& csr_b = b.impl()->template get<Tag>();
637 return vectorMinusT<Tag>(csr_a.getArrayValues(), csr_b.getArrayValues());
638 }
639
640 auto operator()(lazy::minus_tag, Vector const& a, UniqueArray<Real> const& b)
641 {
642 auto const& csr_a = a.impl()->template get<Tag>();
643 return vectorMinusT<Tag>(csr_a.getArrayValues(), b);
644 }
645
646 auto operator()(lazy::dot_tag, const VectorDistribution* distribution,
647 Vector const& a, Vector const& b)
648 {
649 return vectorScalProductT<Tag>(a, b);
650 }
651
652 auto operator()(lazy::dot_tag, VectorDistribution const* distribution,
653 Vector const& a, UniqueArray<Real> const& b)
654 {
655 return vectorScalProductT<Tag>(distribution, a, b);
656 }
657
658 auto operator()(lazy::dot_tag, VectorDistribution const* distribution,
659 UniqueArray<Real> const& a, Vector const& b)
660 {
661 return vectorScalProductT<Tag>(distribution, a, b);
662 }
663
664 auto operator()(lazy::dot_tag, VectorDistribution const* distribution,
665 UniqueArray<Real> const& a, UniqueArray<Real> const& b)
666 {
667 return vectorScalProductT<Tag>(distribution, a, b);
668 }
669
670 template <class A, class B>
671 auto operator()(lazy::minus_tag, A a, B b)
672 {
673 return a - b;
674 }
675 };
676
678 {
679 template <class T>
680 auto operator()(lazy::cst_tag, T c)
681 {
682 return std::numeric_limits<size_t>::max();
683 }
684
685 auto operator()(lazy::ref_tag, Matrix const& r) { return r.rowSpace().size(); }
686
687 template <class T>
688 auto operator()(lazy::ref_tag, T const& r)
689 {
690 return r.rowSpace().size();
691 }
692
693 template <class T, class A, class B>
694 auto operator()(T, A a, B b)
695 {
696 return std::min(a, b);
697 }
698 };
699
700 std::size_t allocSize(Matrix const& A)
701 {
702 SimpleCSRMatrix<Real> const& csr_matrix = A.impl()->get<BackEnd::tag::simplecsr>();
703 return csr_matrix.getLocalSize() + csr_matrix.getGhostSize();
704 }
705
706 std::size_t allocSize(Vector const& x)
707 {
709 return csr_x.getAllocSize();
710 }
711
713 {
714 template <class T>
715 auto operator()(lazy::cst_tag, T c) { return std::size_t(0); }
716
717 auto operator()(lazy::ref_tag, Matrix const& r) { return allocSize(r); }
718
719 auto operator()(lazy::ref_tag, Vector const& r) { return allocSize(r); }
720
721 template <typename L>
722 auto operator()(lazy::add_tag, L const& l, Vector const& r)
723 {
724 return allocSize(r);
725 }
726
727 template <typename L>
728 auto operator()(lazy::minus_tag, L const& l, Vector const& r)
729 {
730 return allocSize(r);
731 }
732
733 template <typename L>
734 auto operator()(lazy::mult_tag, L const& l, Vector const& r)
735 {
736 return allocSize(r);
737 }
738 };
739
740
741 template <class E>
742 auto base_eval(E const& expr) { return expr(cpu_evaluator()); }
743 template <class E>
744 auto eval(E const& expr) { return expr(cpu_evaluator()); }
745
746 auto operator*(Matrix const& l, Vector const& r) { return mul(ref(l), ref(r)); }
747
748 auto operator*(Real lambda, Vector const& r)
749 {
750#ifdef DEBUG
751 std::cout << "lambda*x" << std::endl;
752#endif
753 return mul(cst(lambda), ref(r));
754 }
755
756 auto operator*(Real lambda, Matrix const& r)
757 {
758#ifdef DEBUG
759 std::cout << "lambda*A" << std::endl;
760#endif
761 return mul(cst(lambda), ref(r));
762 }
763
764 template <typename R>
765 auto operator*(Matrix const& l, R const& r)
766 {
767 return mul(ref(l), r);
768 }
769
770 /*
771 template<typename L, typename R>
772 auto operator*(L&& l, R&& r)
773 {
774 return mul(l,r) ;
775 }*/
776
777 auto operator+(Vector const& l, Vector const& r) { return add(ref(l), ref(r)); }
778
779 template <typename R>
780 auto operator+(Vector const& l, R const& r)
781 {
782 return add(ref(l), r);
783 }
784
785 template <typename L>
786 auto operator+(L const& l, Vector const& r)
787 {
788 return add(l, ref(r));
789 }
790
791 auto operator+(Matrix const& l, Matrix const& r) { return add(ref(l), ref(r)); }
792
793 /*
794 template<typename L, typename R>
795 auto operator+(L&& l, R&& r)
796 {
797 return add(l,r) ;
798 }*/
799
800 template <typename R>
801 auto operator-(Vector& l, R&& r) { return minus(ref(l), r); }
802
803 template <typename L>
804 auto operator-(L&& l, Vector& r) { return minus(l, ref(r)); }
805
806 template <typename L, typename R>
807 auto operator-(L&& l, R&& r) { return minus(l, r); }
808
809 auto dot(Vector const& x, Vector const& y) { return scalMul(ref(x), ref(y)); }
810
811 template <typename R>
812 auto dot(Vector const& x, R const& y)
813 {
814 return scalMul(ref(x), y);
815 }
816
817 template <typename L>
818 auto dot(L const& x, Vector const& y)
819 {
820 return scalMul(x, ref(y));
821 }
822
823 template <typename L, typename R>
824 auto dot(L const& x, R const& y)
825 {
826 return scalMul(x, y);
827 }
828
829 template <class E>
830 void assign(Vector& y, E const& expr)
831 {
832 SimpleCSRVector<Real>& csr_y = y.impl()->get<BackEnd::tag::simplecsr>(true);
833 csr_y.allocate();
834 csr_y.setArrayValues(expr(cpu_evaluator()));
835 }
836
837 template <typename Tag, class E>
838 void kassign(Vector& y, E const& expr)
839 {
840 auto& backend_y = y.impl()->template get<Tag>(true);
841 backend_y.allocate();
842 backend_y.setArrayValues(expr(kernel_evaluator<Tag>()));
843 }
844
845 template <typename A, typename B>
846 auto vassign(A&& a, B&& b)
847 {
848 return [&](auto visitor) { return visitor(lazy::assign_tag{}, a, b); };
849 }
850
851 template <typename E>
852 auto veval(double& value, E&& e)
853 {
854 return [&](auto visitor) { return visitor(lazy::eval_tag{}, value, e); };
855 }
856
858 {
859 template <typename T>
860 void eval(T&& expr)
861 {
862#ifdef DEBUG
863 std::cout << "pipeline eval" << std::endl;
864#endif
865 expr(*this);
866 }
867 template <typename T0, typename... T>
868 void eval(T0&& expr0, T&&... args)
869 {
870 eval(expr0);
871 eval(args...);
872 }
873
874 template <typename E>
875 auto operator()(lazy::assign_tag, Vector& a, E&& expr)
876 {
877#ifdef DEBUG
878 std::cout << "vector assignement" << std::endl;
879#endif
880 assign(a, expr);
881 }
882
883 template <typename E>
884 auto operator()(lazy::eval_tag, double& value, E&& expr)
885 {
886 value = base_eval(expr);
887 return value;
888 }
889 };
890
891 template <typename... T>
892 void pipeline(T... args)
893 {
894 pipeline_evaluator evaluator;
895 evaluator.eval(args...);
896 }
897} // namespace MVExpr
898
899template <typename E>
900Vector&
901Vector::operator=(E const& expr)
902{
903 SimpleCSRVector<Real>& csr_y = impl()->get<BackEnd::tag::simplecsr>(true);
904 csr_y.allocate();
905 csr_y.setArrayValues(expr(MVExpr::cpu_evaluator()));
906 return *this;
907}
908
909template <typename E>
910Matrix&
911Matrix::operator=(E const& expr)
912{
913 *this = expr(MVExpr::cpu_evaluator());
914 return *this;
915}
916
917} // namespace Alien
918
919#endif /* ALIEN_EXPRESSION_MVEXPR_MVEXPR_H_ */
virtual Arccore::Integer size() const =0
Get space size.
void mult(const IMatrix &a, const IVector &x, IVector &r) const
Compute a matrix vector product.
void copy(const IVector &x, IVector &r) const
Copy a vector in another one.
void axpy(Real alpha, const IVector &x, IVector &y) const
Scale a vector by a factor and adds the result to another vector.
Real dot(const IVector &x, const IVector &y) const
Compute the dot product of two vectors.
const VectorDistribution & rowDistribution() const
Get the row distribution.
const ISpace & rowSpace() const
Get row space associated to the matrix.
Definition Matrix.cc:100
MultiMatrixImpl * impl()
Get the multimatrix implementation.
Definition Matrix.cc:130
const AlgebraTraits< tag >::matrix_type & get() const
Get a specific matrix implementation.
const AlgebraTraits< tag >::vector_type & get() const
Get a specific vector implementation.
Computes a vector distribution.
MultiVectorImpl * impl()
Get the multivector implementation.
Definition Vector.cc:114
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17