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,
typename ColumnType = ptrdiff_t,
typename Po
interType = ColumnType>
60 typedef ColumnType index_type;
61 typedef ColumnType col_type;
62 typedef PointerType ptr_type;
64 typedef typename math::rhs_of<value_type>::type rhs_type;
77 static std::string name() {
return "builtin"; }
80 static std::shared_ptr<matrix>
81 copy_matrix(std::shared_ptr<matrix> A,
const params&)
88 static std::shared_ptr<numa_vector<T>>
89 copy_vector(
const std::vector<T>& x,
const params&)
91 return std::make_shared<numa_vector<T>>(x);
96 static std::shared_ptr<numa_vector<T>>
97 copy_vector(std::shared_ptr<numa_vector<T>> x,
const params&)
103 static std::shared_ptr<vector>
104 create_vector(
size_t size,
const params&)
106 return std::make_shared<vector>(size);
111 std::vector<col_type> I;
113 gather(
size_t ,
const std::vector<col_type>& I,
const params&)
117 template <
class InVec,
class OutVec>
118 void operator()(
const InVec& vec, OutVec& vals)
const
120 for (
size_t i = 0; i < I.size(); ++i)
127 std::vector<col_type> I;
129 scatter(
size_t ,
const std::vector<col_type>& I,
const params&)
133 template <
class InVec,
class OutVec>
134 void operator()(
const InVec& vals, OutVec& vec)
const
136 for (
size_t i = 0; i < I.size(); ++i)
142 static std::shared_ptr<direct_solver>
143 create_solver(std::shared_ptr<matrix> A,
const params&)
145 return std::make_shared<direct_solver>(*A);
154namespace Arcane::Alina::backend
183template <
typename T1,
typename T2>
187template <
typename V,
typename C,
typename P>
196template <
typename V,
typename C,
typename P>
208 static size_t get(
const Vec& x)
210 typedef typename backend::value_type<Vec>::type V;
211 return x.size() *
sizeof(V);
215template <
typename V,
typename C,
typename P>
218 typedef const P* type;
225template <
typename V,
typename C,
typename P>
228 typedef const C* type;
235template <
typename V,
typename C,
typename P>
238 typedef const V* type;
245template <
typename V,
typename C,
typename P>
250 return A.nbRow() == 0 ? 0 : A.ptr[A.nbRow()];
254template <
typename V,
typename C,
typename P>
259 return A.ptr[row + 1] - A.ptr[row];
264struct clear_impl<Vec, typename std::enable_if<is_builtin_vector<Vec>::value>::type>
266 static void apply(Vec& x)
268 typedef typename backend::value_type<Vec>::type V;
270 const size_t n = x.size();
272 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
273 x[i] = math::zero<V>();
282template <
class Vec1,
class Vec2>
284 typename std::enable_if<
285 is_builtin_vector<Vec1>::value &&
286 is_builtin_vector<Vec2>::value>::type>
288 typedef typename value_type<Vec1>::type V;
290 typedef typename math::inner_product_impl<V>::return_type return_type;
292 static return_type get(
const Vec1& x,
const Vec2& y)
295 return parallel(x, y);
302 static return_type serial(
const Vec1& x,
const Vec2& y)
304 const size_t n = x.size();
306 return_type s = math::zero<return_type>();
307 return_type c = math::zero<return_type>();
309 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(n); ++i) {
310 return_type d = math::inner_product(x[i], y[i]) - c;
311 return_type t = s + d;
319 static return_type parallel(
const Vec1& x,
const Vec2& y)
321 const size_t n = x.size();
325 const return_type zero = math::zero<return_type>();
326 sum_array.
resize(nb_thread, zero);
328 for (
Int32 i = 0; i < nb_thread; ++i)
334 return_type s = zero;
335 return_type c = zero;
336 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
337 return_type d = math::inner_product(x[i], y[i]) - c;
338 return_type t = s + d;
345 return_type total = zero;
346 for (
Int32 i = 0; i < nb_thread; ++i) {
356template <
class A,
class Vec1,
class B,
class Vec2>
357struct axpby_impl<A, Vec1, B, Vec2, typename std::enable_if<is_builtin_vector<Vec1>::value && is_builtin_vector<Vec2>::value>::type>
359 static void apply(A a,
const Vec1& x, B b, Vec2& y)
361 const size_t n = x.size();
363 if (!math::is_zero(b)) {
364 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
365 y[i] = a * x[i] + b * y[i];
369 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
380template <
class A,
class Vec1,
class B,
class Vec2,
class C,
class Vec3>
382 typename std::enable_if<
383 is_builtin_vector<Vec1>::value &&
384 is_builtin_vector<Vec2>::value &&
385 is_builtin_vector<Vec3>::value>::type>
387 static void apply(A a,
const Vec1& x, B b,
const Vec2& y, C c, Vec3& z)
389 const size_t n = x.size();
391 if (!math::is_zero(c)) {
392 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
393 z[i] = a * x[i] + b * y[i] + c * z[i];
397 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
398 z[i] = a * x[i] + b * y[i];
408template <
class Alpha,
class Vec1,
class Vec2,
class Beta,
class Vec3>
410 typename std::enable_if<
411 is_builtin_vector<Vec1>::value &&
412 is_builtin_vector<Vec2>::value &&
413 is_builtin_vector<Vec3>::value &&
414 math::static_rows<typename value_type<Vec1>::type>::value == math::static_rows<typename value_type<Vec2>::type>::value &&
415 math::static_rows<typename value_type<Vec1>::type>::value == math::static_rows<typename value_type<Vec3>::type>::value>::type>
417 static void apply(Alpha a,
const Vec1& x,
const Vec2& y, Beta b, Vec3& z)
419 const size_t n = x.size();
421 if (!math::is_zero(b)) {
422 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
423 z[i] = a * x[i] * y[i] + b * z[i];
427 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
428 z[i] = a * x[i] * y[i];
439template <
class Alpha,
class Vec1,
class Vec2,
class Beta,
class Vec3>
441 typename std::enable_if<is_builtin_vector<Vec1>::value &&
442 is_builtin_vector<Vec2>::value &&
443 is_builtin_vector<Vec3>::value &&
444 (math::static_rows<typename value_type<Vec1>::type>::value != math::static_rows<typename value_type<Vec2>::type>::value ||
445 math::static_rows<typename value_type<Vec1>::type>::value != math::static_rows<typename value_type<Vec3>::type>::value)>::type>
447 static void apply(Alpha a,
const Vec1& x,
const Vec2& y, Beta b, Vec3& z)
449 typedef typename value_type<Vec1>::type M_type;
450 auto Y = backend::reinterpret_as_rhs<M_type>(y);
451 auto Z = backend::reinterpret_as_rhs<M_type>(z);
453 const size_t n = x.size();
456 if (!math::is_zero(b)) {
457 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
458 Z[i] = a * x[i] * Y[i] + b * Z[i];
462 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
463 Z[i] = a * x[i] * Y[i];
473template <
class Vec1,
class Vec2>
475 typename std::enable_if<
476 is_builtin_vector<Vec1>::value &&
477 is_builtin_vector<Vec2>::value>::type>
479 static void apply(
const Vec1& x, Vec2& y)
481 const size_t n = x.size();
483 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
490template <
class MatrixValue,
class Vector,
bool IsConst>
492 typename std::enable_if<is_builtin_vector<Vector>::value>::type>
494 typedef typename backend::value_type<Vector>::type src_type;
495 typedef typename math::scalar_of<src_type>::type scalar_type;
496 typedef typename math::rhs_of<MatrixValue>::type rhs_type;
497 typedef typename math::replace_scalar<rhs_type, scalar_type>::type dst_type;
498 typedef typename std::conditional<IsConst, const dst_type*, dst_type*>::type ptr_type;
499 typedef typename std::conditional<IsConst, const dst_type, dst_type>::type return_value_type;
503 static return_type get(V&& x)
505 auto ptr =
reinterpret_cast<ptr_type
>(&x[0]);
506 const size_t n = x.
size() *
sizeof(src_type) /
sizeof(dst_type);
517 template <
typename V,
typename C,
typename P>
526namespace Arcane::Alina::detail
530template <
class V1,
class V2>
532 typename std::enable_if<math::static_rows<V1>::value != 1 || math::static_rows<V2>::value != 1>::type>
534 typedef typename math::scalar_of<V1>::type S1;
535 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.