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
189template <
typename T1,
typename T2>
193template <
typename V,
typename C,
typename P>
202template <
typename V,
typename C,
typename P>
214 static size_t get(
const Vec& x)
216 typedef typename backend::value_type<Vec>::type V;
217 return x.size() *
sizeof(V);
221template <
typename V,
typename C,
typename P>
224 typedef const P* type;
231template <
typename V,
typename C,
typename P>
234 typedef const C* type;
241template <
typename V,
typename C,
typename P>
244 typedef const V* type;
251template <
typename V,
typename C,
typename P>
256 return A.nbRow() == 0 ? 0 : A.ptr[A.nbRow()];
260template <
typename V,
typename C,
typename P>
265 return A.ptr[row + 1] - A.ptr[row];
270struct clear_impl<Vec, typename std::enable_if<is_builtin_vector<Vec>::value>::type>
272 static void apply(Vec& x)
274 typedef typename backend::value_type<Vec>::type V;
276 const size_t n = x.size();
278 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
279 x[i] = math::zero<V>();
288template <
class Vec1,
class Vec2>
290 typename std::enable_if<
291 is_builtin_vector<Vec1>::value &&
292 is_builtin_vector<Vec2>::value>::type>
294 typedef typename value_type<Vec1>::type V;
296 typedef typename math::inner_product_impl<V>::return_type return_type;
298 static return_type get(
const Vec1& x,
const Vec2& y)
301 return parallel(x, y);
308 static return_type serial(
const Vec1& x,
const Vec2& y)
310 const size_t n = x.size();
312 return_type s = math::zero<return_type>();
313 return_type c = math::zero<return_type>();
315 for (ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(n); ++i) {
316 return_type d = math::inner_product(x[i], y[i]) - c;
317 return_type t = s + d;
325 static return_type parallel(
const Vec1& x,
const Vec2& y)
327 const size_t n = x.size();
331 const return_type zero = math::zero<return_type>();
332 sum_array.
resize(nb_thread, zero);
334 for (
Int32 i = 0; i < nb_thread; ++i)
340 return_type s = zero;
341 return_type c = zero;
342 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
343 return_type d = math::inner_product(x[i], y[i]) - c;
344 return_type t = s + d;
351 return_type total = zero;
352 for (
Int32 i = 0; i < nb_thread; ++i) {
362template <
class A,
class Vec1,
class B,
class Vec2>
363struct axpby_impl<A, Vec1, B, Vec2, typename std::enable_if<is_builtin_vector<Vec1>::value && is_builtin_vector<Vec2>::value>::type>
365 static void apply(A a,
const Vec1& x, B b, Vec2& y)
367 const size_t n = x.size();
369 if (!math::is_zero(b)) {
370 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
371 y[i] = a * x[i] + b * y[i];
375 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
386template <
class A,
class Vec1,
class B,
class Vec2,
class C,
class Vec3>
388 typename std::enable_if<
389 is_builtin_vector<Vec1>::value &&
390 is_builtin_vector<Vec2>::value &&
391 is_builtin_vector<Vec3>::value>::type>
393 static void apply(A a,
const Vec1& x, B b,
const Vec2& y, C c, Vec3& z)
395 const size_t n = x.size();
397 if (!math::is_zero(c)) {
398 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
399 z[i] = a * x[i] + b * y[i] + c * z[i];
403 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
404 z[i] = a * x[i] + b * y[i];
414template <
class Alpha,
class Vec1,
class Vec2,
class Beta,
class Vec3>
416 typename std::enable_if<
417 is_builtin_vector<Vec1>::value &&
418 is_builtin_vector<Vec2>::value &&
419 is_builtin_vector<Vec3>::value &&
420 math::static_rows<typename value_type<Vec1>::type>::value == math::static_rows<typename value_type<Vec2>::type>::value &&
421 math::static_rows<typename value_type<Vec1>::type>::value == math::static_rows<typename value_type<Vec3>::type>::value>::type>
423 static void apply(Alpha a,
const Vec1& x,
const Vec2& y, Beta b, Vec3& z)
425 const size_t n = x.size();
427 if (!math::is_zero(b)) {
428 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
429 z[i] = a * x[i] * y[i] + b * z[i];
433 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
434 z[i] = a * x[i] * y[i];
445template <
class Alpha,
class Vec1,
class Vec2,
class Beta,
class Vec3>
447 typename std::enable_if<is_builtin_vector<Vec1>::value &&
448 is_builtin_vector<Vec2>::value &&
449 is_builtin_vector<Vec3>::value &&
450 (math::static_rows<typename value_type<Vec1>::type>::value != math::static_rows<typename value_type<Vec2>::type>::value ||
451 math::static_rows<typename value_type<Vec1>::type>::value != math::static_rows<typename value_type<Vec3>::type>::value)>::type>
453 static void apply(Alpha a,
const Vec1& x,
const Vec2& y, Beta b, Vec3& z)
455 typedef typename value_type<Vec1>::type M_type;
456 auto Y = backend::reinterpret_as_rhs<M_type>(y);
457 auto Z = backend::reinterpret_as_rhs<M_type>(z);
459 const size_t n = x.size();
462 if (!math::is_zero(b)) {
463 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
464 Z[i] = a * x[i] * Y[i] + b * Z[i];
468 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
469 Z[i] = a * x[i] * Y[i];
479template <
class Vec1,
class Vec2>
481 typename std::enable_if<
482 is_builtin_vector<Vec1>::value &&
483 is_builtin_vector<Vec2>::value>::type>
485 static void apply(
const Vec1& x, Vec2& y)
487 const size_t n = x.size();
489 for (ptrdiff_t i = begin; i < (begin + size); ++i) {
496template <
class MatrixValue,
class Vector,
bool IsConst>
498 typename std::enable_if<is_builtin_vector<Vector>::value>::type>
500 typedef typename backend::value_type<Vector>::type src_type;
501 typedef typename math::scalar_of<src_type>::type scalar_type;
502 typedef typename math::rhs_of<MatrixValue>::type rhs_type;
503 typedef typename math::replace_scalar<rhs_type, scalar_type>::type dst_type;
504 typedef typename std::conditional<IsConst, const dst_type*, dst_type*>::type ptr_type;
505 typedef typename std::conditional<IsConst, const dst_type, dst_type>::type return_value_type;
509 static return_type get(V&& x)
511 auto ptr =
reinterpret_cast<ptr_type
>(&x[0]);
512 const size_t n = x.
size() *
sizeof(src_type) /
sizeof(dst_type);
523 template <
typename V,
typename C,
typename P>
532namespace Arcane::Alina::detail
536template <
class V1,
class V2>
538 typename std::enable_if<math::static_rows<V1>::value != 1 || math::static_rows<V2>::value != 1>::type>
540 typedef typename math::scalar_of<V1>::type S1;
541 typedef typename math::scalar_of<V2>::type S2;
Class to handle empty parameter list.
NUMA-aware vector container.
Direct solver that uses Skyline LU factorization.
void resize(Int64 s)
Changes the number of elements in the array to s.
ArrayView< T > view() const
Mutable view of this array.
static Int32 maxAllowedThread()
Maximum number of allowed threads for multi-threading.
Loop execution information.
1D data array with pre-allocated stack buffer.
View of an array of elements of type T.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
View of an array of elements of type T.
static Int32 currentTaskThreadIndex()
Index (between 0 and nbAllowedThread()-1) of the thread executing the current task.
1D data vector with value semantics (STL style).
Vector class, to be used by user.
void arccoreParallelFor(const ComplexForLoopRanges< RankValue, IndexType_ > &loop_ranges, const ForLoopRunInfo &run_info, const LambdaType &lambda_function, const ReducerArgs &... reducer_args)
Applies the lambda function lambda_function concurrently over the iteration interval given by loop_ra...
std::int32_t Int32
Signed integer type of 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-wise vector product.