12#ifndef ARCCORE_ALINA_BUILTINBACKEND_H
13#define ARCCORE_ALINA_BUILTINBACKEND_H
31#include "arccore/alina/CSRMatrixOperations.h"
32#include "arccore/alina/SkylineLUSolver.h"
33#include "arccore/alina/MatrixOperationsImpl.h"
35#include "arccore/base/ConcurrencyBase.h"
36#include "arccore/common/SmallArray.h"
44namespace Arcane::Alina
56template <
typename ValueType,
57 typename ColumnType = AlinaDefaultColumnType,
58 typename PointerType = AlinaDefaultRowIndexType>
62 typedef ColumnType index_type;
63 typedef ColumnType col_type;
64 typedef PointerType ptr_type;
66 typedef typename math::rhs_of<value_type>::type rhs_type;
79 static std::string name() {
return "builtin"; }
82 static std::shared_ptr<matrix>
83 copy_matrix(std::shared_ptr<matrix> A,
const params&)
90 static std::shared_ptr<numa_vector<T>>
91 copy_vector(
const std::vector<T>& x,
const params&)
93 return std::make_shared<numa_vector<T>>(x);
98 static std::shared_ptr<numa_vector<T>>
99 copy_vector(std::shared_ptr<numa_vector<T>> x,
const params&)
105 static std::shared_ptr<vector>
106 create_vector(
size_t size,
const params&)
108 return std::make_shared<vector>(size);
113 std::vector<col_type> I;
115 gather(
size_t ,
const std::vector<col_type>& I,
const params&)
119 template <
class InVec,
class OutVec>
120 void operator()(
const InVec& vec, OutVec& vals)
const
122 for (
size_t i = 0; i < I.size(); ++i)
129 std::vector<col_type> I;
131 scatter(
size_t ,
const std::vector<col_type>& I,
const params&)
135 template <
class InVec,
class OutVec>
136 void operator()(
const InVec& vals, OutVec& vec)
const
138 for (
size_t i = 0; i < I.size(); ++i)
144 static std::shared_ptr<direct_solver>
145 create_solver(std::shared_ptr<matrix> A,
const params&)
147 return std::make_shared<direct_solver>(*A);
156namespace Arcane::Alina::backend
185template <
typename T1,
typename T2>
189template <
typename V,
typename C,
typename P>
198template <
typename V,
typename C,
typename P>
210 static size_t get(
const Vec& x)
212 typedef typename backend::value_type<Vec>::type V;
213 return x.size() *
sizeof(V);
217template <
typename V,
typename C,
typename P>
220 typedef const P* type;
227template <
typename V,
typename C,
typename P>
230 typedef const C* type;
237template <
typename V,
typename C,
typename P>
240 typedef const V* type;
247template <
typename V,
typename C,
typename P>
252 return A.nbRow() == 0 ? 0 : A.ptr[A.nbRow()];
256template <
typename V,
typename C,
typename P>
261 return A.ptr[row + 1] - A.ptr[row];
266struct clear_impl<Vec, typename std::enable_if<is_builtin_vector<Vec>::value>::type>
268 static void apply(Vec& x)
270 typedef typename backend::value_type<Vec>::type V;
272 const size_t n = x.size();
274 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
275 x[i] = math::zero<V>();
284template <
class Vec1,
class Vec2>
286 typename std::enable_if<
287 is_builtin_vector<Vec1>::value &&
288 is_builtin_vector<Vec2>::value>::type>
290 typedef typename value_type<Vec1>::type V;
292 typedef typename math::inner_product_impl<V>::return_type return_type;
294 static return_type get(
const Vec1& x,
const Vec2& y)
297 return parallel(x, y);
304 static return_type serial(
const Vec1& x,
const Vec2& y)
306 const size_t n = x.size();
308 return_type s = math::zero<return_type>();
309 return_type c = math::zero<return_type>();
311 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(n); ++i) {
312 return_type d = math::inner_product(x[i], y[i]) - c;
313 return_type t = s + d;
321 static return_type parallel(
const Vec1& x,
const Vec2& y)
323 const size_t n = x.size();
327 const return_type zero = math::zero<return_type>();
328 sum_array.
resize(nb_thread, zero);
330 for (
Int32 i = 0; i < nb_thread; ++i)
336 return_type s = zero;
337 return_type c = zero;
338 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
339 return_type d = math::inner_product(x[i], y[i]) - c;
340 return_type t = s + d;
347 return_type total = zero;
348 for (
Int32 i = 0; i < nb_thread; ++i) {
358template <
class A,
class Vec1,
class B,
class Vec2>
359struct axpby_impl<A, Vec1, B, Vec2, typename std::enable_if<is_builtin_vector<Vec1>::value && is_builtin_vector<Vec2>::value>::type>
361 static void apply(A a,
const Vec1& x, B b, Vec2& y)
363 const size_t n = x.size();
365 if (!math::is_zero(b)) {
366 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
367 y[i] = a * x[i] + b * y[i];
371 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
382template <
class A,
class Vec1,
class B,
class Vec2,
class C,
class Vec3>
384 typename std::enable_if<
385 is_builtin_vector<Vec1>::value &&
386 is_builtin_vector<Vec2>::value &&
387 is_builtin_vector<Vec3>::value>::type>
389 static void apply(A a,
const Vec1& x, B b,
const Vec2& y, C c, Vec3& z)
391 const size_t n = x.size();
393 if (!math::is_zero(c)) {
394 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
395 z[i] = a * x[i] + b * y[i] + c * z[i];
399 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
400 z[i] = a * x[i] + b * y[i];
410template <
class Alpha,
class Vec1,
class Vec2,
class Beta,
class Vec3>
412 typename std::enable_if<
413 is_builtin_vector<Vec1>::value &&
414 is_builtin_vector<Vec2>::value &&
415 is_builtin_vector<Vec3>::value &&
416 math::static_rows<typename value_type<Vec1>::type>::value == math::static_rows<typename value_type<Vec2>::type>::value &&
417 math::static_rows<typename value_type<Vec1>::type>::value == math::static_rows<typename value_type<Vec3>::type>::value>::type>
419 static void apply(Alpha a,
const Vec1& x,
const Vec2& y, Beta b, Vec3& z)
421 const size_t n = x.size();
423 if (!math::is_zero(b)) {
424 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
425 z[i] = a * x[i] * y[i] + b * z[i];
429 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
430 z[i] = a * x[i] * y[i];
441template <
class Alpha,
class Vec1,
class Vec2,
class Beta,
class Vec3>
443 typename std::enable_if<is_builtin_vector<Vec1>::value &&
444 is_builtin_vector<Vec2>::value &&
445 is_builtin_vector<Vec3>::value &&
446 (math::static_rows<typename value_type<Vec1>::type>::value != math::static_rows<typename value_type<Vec2>::type>::value ||
447 math::static_rows<typename value_type<Vec1>::type>::value != math::static_rows<typename value_type<Vec3>::type>::value)>::type>
449 static void apply(Alpha a,
const Vec1& x,
const Vec2& y, Beta b, Vec3& z)
451 typedef typename value_type<Vec1>::type M_type;
452 auto Y = backend::reinterpret_as_rhs<M_type>(y);
453 auto Z = backend::reinterpret_as_rhs<M_type>(z);
455 const size_t n = x.size();
458 if (!math::is_zero(b)) {
459 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
460 Z[i] = a * x[i] * Y[i] + b * Z[i];
464 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
465 Z[i] = a * x[i] * Y[i];
475template <
class Vec1,
class Vec2>
477 typename std::enable_if<
478 is_builtin_vector<Vec1>::value &&
479 is_builtin_vector<Vec2>::value>::type>
481 static void apply(
const Vec1& x, Vec2& y)
483 const size_t n = x.size();
485 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
492template <
class MatrixValue,
class Vector,
bool IsConst>
494 typename std::enable_if<is_builtin_vector<Vector>::value>::type>
496 typedef typename backend::value_type<Vector>::type src_type;
497 typedef typename math::scalar_of<src_type>::type scalar_type;
498 typedef typename math::rhs_of<MatrixValue>::type rhs_type;
499 typedef typename math::replace_scalar<rhs_type, scalar_type>::type dst_type;
500 typedef typename std::conditional<IsConst, const dst_type*, dst_type*>::type ptr_type;
501 typedef typename std::conditional<IsConst, const dst_type, dst_type>::type return_value_type;
505 static return_type get(V&& x)
507 auto ptr =
reinterpret_cast<ptr_type
>(&x[0]);
508 const size_t n = x.
size() *
sizeof(src_type) /
sizeof(dst_type);
519 template <
typename V,
typename C,
typename P>
528namespace Arcane::Alina::detail
532template <
class V1,
class V2>
534 typename std::enable_if<math::static_rows<V1>::value != 1 || math::static_rows<V2>::value != 1>::type>
536 typedef typename math::scalar_of<V1>::type S1;
537 typedef typename math::scalar_of<V2>::type S2;
Class to handle empty parameters list.
NUMA-aware vector container.
Direct solver that uses Skyline LU factorization.
void resize(Int64 s)
Change le nombre d'éléments du tableau à s.
ArrayView< T > view() const
Vue mutable sur ce tableau.
static Int32 maxAllowedThread()
Nombre maximum de threads autorisés pour le multi-threading.
Informations d'exécution d'une boucle.
Tableau 1D de données avec buffer pré-alloué sur la pile.
Vue d'un tableau d'éléments de type T.
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Vue d'un tableau d'éléments de type T.
static Int32 currentTaskThreadIndex()
Indice (entre 0 et nbAllowedThread()-1) du thread exécutant la tâche actuelle.
Vector class, to be used by user.
void arccoreParallelFor(const ComplexForLoopRanges< RankValue > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applique en concurrence la fonction lambda lambda_function sur l'intervalle d'itération donné par loo...
std::int32_t Int32
Type entier signé sur 32 bits.
Alina::detail::empty_params params
Sparse matrix stored in CSR (Compressed Sparse Row) format.
Implementation for linear combination of two vectors.
Implementation for linear combination of three vectors.
Metafunction that checks if two backends are compatible.
Implementation for function returning number of bytes allocated for a matrix/vector.
Implementation for zeroing out a vector.
Implementation for function returning the number of columns in a matrix.
Implementation for vector copy.
Implementation for inner product.
Implementation for function returning the number of nonzeros in a matrix.
Reinterpret the vector to be compatible with the matrix value type.
Implementation for function returning the number of nonzeros in a matrix row.
Implementation for function returning the number of rows in a matrix.
Implementation for element-wize vector product.