Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
CBLASMPIKernel.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 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#pragma once
9
10#include <cmath>
11
12#include <arccore/message_passing/Messages.h>
13
14#include <alien/utils/Precomp.h>
15#include <alien/kernels/simple_csr/algebra/alien_cblas.h>
16
17namespace Alien
18{
19
21{
22 public:
23 // static const tag::eType type = tag::CPU;
24 static const bool is_hybrid = false;
25 static const bool is_mpi = true;
26
27 template <typename Distribution, typename VectorT>
28 static void copy(
29 Distribution const& dist ALIEN_UNUSED_PARAM, const VectorT& x, VectorT& y)
30 {
31 typedef typename VectorT::ValueType ValueType;
32 cblas::copy(
33 x.scalarizedLocalSize(), (ValueType*)x.getDataPtr(), 1, y.getDataPtr(), 1);
34 }
35
36 template <typename Distribution, typename VectorT>
37 static void copy(
38 Distribution const& dist, const VectorT& x, Integer stride_x, VectorT& y, Integer stride_y)
39 {
40 typedef typename VectorT::ValueType ValueType;
41 cblas::copy(dist.localSize(), (ValueType*)x.getDataPtr(), stride_x, y.getDataPtr(), stride_y);
42 }
43
44
45 template <typename Distribution, typename VectorT>
46 static void copy([[maybe_unused]] Distribution const& dist,
47 const VectorT& x,
48 ConstArrayView<Integer> strides,
49 VectorT& y)
50 {
51 typedef typename VectorT::ValueType ValueType;
52 std::size_t local_size = y.scalarizedLocalSize();
53 auto x_ptr = x.getDataPtr();
54 auto y_ptr = y.getDataPtr();
55 for (std::size_t i = 0; i < local_size; ++i)
56 {
57 y_ptr[i] = x_ptr[strides[i]] ;
58 }
59 }
60
61 template <typename Distribution, typename VectorT>
62 static void copy([[maybe_unused]] Distribution const& dist,
63 const VectorT& x,
64 VectorT& y,
65 ConstArrayView<Integer> strides)
66 {
67 typedef typename VectorT::ValueType ValueType;
68 std::size_t local_size = x.scalarizedLocalSize();
69 auto x_ptr = x.getDataPtr();
70 auto y_ptr = y.getDataPtr();
71 for (std::size_t i = 0; i < local_size; ++i)
72 {
73 y_ptr[strides[i]] = x_ptr[i] ;
74 }
75 }
76
77
78 template <typename Distribution, typename VectorT>
79 static void axpy(Distribution const& dist ALIEN_UNUSED_PARAM,
80 typename VectorT::ValueType alpha, const VectorT& x, VectorT& y)
81 {
82 cblas::axpy(x.scalarizedLocalSize(), alpha, x.getDataPtr(), 1, y.getDataPtr(), 1);
83 }
84
85 template <typename Distribution, typename VectorT>
86 static void axpy(Distribution const& dist,
87 typename VectorT::ValueType alpha,
88 const VectorT& x,
89 Integer stride_x,
90 VectorT& y,
91 Integer stride_y)
92 {
93 cblas::axpy(x.scalarizedLocalSize(), alpha, x.getDataPtr(), stride_x, y.getDataPtr(), stride_y);
94 }
95
96 template <typename Distribution, typename VectorT>
97 static void axpy([[maybe_unused]] Distribution const& dist,
98 typename VectorT::ValueType alpha,
99 const VectorT& x,
100 VectorT& y,
101 ConstArrayView<Integer> strides)
102 {
103 typedef typename VectorT::ValueType ValueType;
104 std::size_t local_size = x.scalarizedLocalSize();
105 auto x_ptr = x.getDataPtr();
106 auto y_ptr = y.getDataPtr();
107 for (std::size_t i = 0; i < local_size; ++i)
108 {
109 y_ptr[strides[i]] += alpha * x_ptr[i] ;
110 }
111 }
112
113 template <typename Distribution, typename VectorT>
114 static void scal(Distribution const& dist ALIEN_UNUSED_PARAM,
115 typename VectorT::ValueType alpha, VectorT& x)
116 {
117 cblas::scal(x.scalarizedLocalSize(), alpha, x.getDataPtr(), 1);
118 }
119
120 template <typename Distribution, typename VectorT>
121 static void pointwiseMult(Distribution const& dist,
122 VectorT const& x,
123 VectorT const& y,
124 VectorT& z)
125 {
126 auto local_size = x.scalarizedLocalSize();
127 auto x_ptr = x.getDataPtr();
128 auto y_ptr = y.getDataPtr();
129 auto z_ptr = z.getDataPtr();
130 for (Integer i = 0; i < local_size; ++i) {
131 z_ptr[i] = x_ptr[i] * y_ptr[i];
132#ifdef PRINT_DEBUG_INFO
133 std::cout<<"X Y Z ["<<i<<"] : "<<x_ptr[i]<<"*"<<y_ptr[i]<<"="<<z_ptr[i]<<std::endl ;
134#endif
135 }
136 }
137
138 template <typename Distribution, typename VectorT>
139 static void assign(Distribution const& dist,
140 typename VectorT::ValueType alpha,
141 VectorT& y)
142 {
143 auto local_size = y.scalarizedLocalSize();
144 auto y_ptr = y.getDataPtr();
145 for (Integer i = 0; i < local_size; ++i) {
146 y_ptr[i] = alpha;
147 }
148 }
149
150 template <typename Distribution, typename VectorT>
151 static typename VectorT::ValueType dot(
152 Distribution const& dist, const VectorT& x, const VectorT& y)
153 {
154 typedef typename VectorT::ValueType ValueType;
155 ValueType value = cblas::dot(x.scalarizedLocalSize(), (ValueType*)x.getDataPtr(), 1,
156 (ValueType*)y.getDataPtr(), 1);
157 if (dist.isParallel()) {
158 value = Arccore::MessagePassing::mpAllReduce(dist.parallelMng(),
159 Arccore::MessagePassing::ReduceSum, value);
160 }
161 return value;
162 }
163
164 template <typename Distribution, typename VectorT>
165 static typename VectorT::ValueType nrm0(Distribution const& dist, const VectorT& x)
166 {
167 typedef typename VectorT::ValueType ValueType;
168 std::size_t local_size = x.scalarizedLocalSize();
169 auto x_ptr = x.getDataPtr();
170 ValueType value = ValueType() ;
171 for(std::size_t i = 0; i < local_size; ++i)
172 value += (std::abs(x_ptr[i])>0?1:0) ;
173
174 if (dist.isParallel()) {
175 value = Arccore::MessagePassing::mpAllReduce(
176 dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value);
177 }
178 return value;
179 }
180
181 template <typename Distribution, typename VectorT>
182 static typename VectorT::ValueType nrm1(Distribution const& dist, const VectorT& x)
183 {
184 typedef typename VectorT::ValueType ValueType;
185 typename VectorT::ValueType value = cblas::nrm1(x.scalarizedLocalSize(),
186 (ValueType*)x.getDataPtr(), 1);
187 if (dist.isParallel()) {
188 value = Arccore::MessagePassing::mpAllReduce(
189 dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value);
190 }
191 return value;
192 }
193
194 template <typename Distribution, typename VectorT>
195 static typename VectorT::ValueType nrm2(Distribution const& dist, const VectorT& x)
196 {
197 typedef typename VectorT::ValueType ValueType;
198 typename VectorT::ValueType value = cblas::dot(x.scalarizedLocalSize(),
199 (ValueType*)x.getDataPtr(), 1, (ValueType*)x.getDataPtr(), 1);
200 if (dist.isParallel()) {
201 value = Arccore::MessagePassing::mpAllReduce(
202 dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value);
203 }
204 return std::sqrt(value);
205 }
206
207 template <typename Distribution, typename VectorT>
208 static typename VectorT::ValueType nrmInf(Distribution const& dist, const VectorT& x)
209 {
210 typedef typename VectorT::ValueType ValueType;
211 std::size_t local_size = x.scalarizedLocalSize();
212 auto x_ptr = x.getDataPtr();
213 ValueType value = ValueType() ;
214 for(std::size_t i = 0; i < local_size; ++i)
215 value = std::max(value,std::abs(x_ptr[i])) ;
216
217 if (dist.isParallel()) {
218 value = Arccore::MessagePassing::mpAllReduce(
219 dist.parallelMng(), Arccore::MessagePassing::ReduceMax, value);
220 }
221 return value;
222 }
223
224 template <typename Distribution, typename MatrixT>
225 static typename MatrixT::ValueType matrix_nrm2(Distribution const& dist, const MatrixT& x)
226 {
227 typedef typename MatrixT::ValueType ValueType;
228 typename MatrixT::ValueType value = cblas::dot(x.getProfile().getNnz(),
229 (ValueType*)x.data(), 1, (ValueType*)x.data(), 1);
230 if (dist.isParallel()) {
231 value = Arccore::MessagePassing::mpAllReduce(
232 dist.parallelMng(), Arccore::MessagePassing::ReduceSum, value);
233 }
234 return std::sqrt(value);
235 }
236};
237
238} // namespace Alien
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17