Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
HCSRMatrixInternal.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 <alien/kernels/sycl/SYCLPrecomp.h>
11
12#ifdef USE_SYCL2020
13#include <sycl/sycl.hpp>
14#else
15#include <CL/sycl.hpp>
16#endif
17
18#include <alien/kernels/simple_csr/CSRStructInfo.h>
19#include <alien/kernels/simple_csr/SimpleCSRPrecomp.h>
20#include "alien/kernels/sycl/data/HCSRMatrix.h"
21
22#include "SYCLEnv.h"
23#include "SYCLEnvInternal.h"
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27using namespace Arccore;
28
29namespace Alien
30{
31
32 namespace HCSRInternal
33 {
34
35 /*---------------------------------------------------------------------------*/
36
37 #ifndef USE_SYCL2020
38 using namespace cl ;
39 #endif
40
41 template <typename ValueT = Real>
42 class MatrixInternal
43 {
44 public:
45 // clang-format off
46 typedef ValueT ValueType;
47 typedef MatrixInternal<ValueType> ThisType;
48 typedef SimpleCSRInternal::CSRStructInfo ProfileType;
49 typedef typename ProfileType::IndexType IndexType ;
50 typedef sycl::buffer<ValueType, 1> ValueBufferType;
51 typedef sycl::buffer<IndexType, 1> IndexBufferType;
52
53
54 class HypreProfile
55 {
56 public:
57 HypreProfile(std::size_t nrows, IndexType* rows, IndexType* ncols)
58 : m_rows(rows,nrows)
59 , m_ncols(ncols,nrows)
60 {}
61 mutable IndexBufferType m_rows;
62 mutable IndexBufferType m_ncols;
63 };
64
65 class COOProfile
66 {
67 public:
68 COOProfile(std::size_t nrows,
69 std::size_t nnz,
70 std::size_t nghost,
71 IndexType* rows,
72 IndexType* ncols,
73 IndexType* ghost_uids)
74 : m_rows(rows,nnz)
75 , m_ncols(ncols,nrows)
76 //, m_ghost_uids(ghost_uids,nghost)
77 {}
78 mutable IndexBufferType m_rows;
79 mutable IndexBufferType m_ncols;
80 //mutable IndexBufferType m_ghost_uids;
81 };
82 // clang-format on
83
84 public:
85 MatrixInternal(ProfileType* profile)
86 : m_profile(profile)
87 , m_values(sycl::range<1>(profile->getNnz()+1))
88 , m_kcol(profile->kcol(),profile->getNRows()+1)
89 , m_cols(profile->cols(),profile->getNnz())
90 {
91 m_values.set_final_data(nullptr);
92 }
93
94 virtual ~MatrixInternal() {}
95
96
97 ProfileType& getCSRProfile() {
98 assert(m_profile) ;
99 return *m_profile ;
100 }
101
102 ProfileType const& getCSRProfile() const {
103 assert(m_profile) ;
104 return *m_profile ;
105 }
106
107 void setValues(ValueT value)
108 {
109 auto env = SYCLEnv::instance() ;
110 env->internal()->queue().submit([&](sycl::handler& cgh)
111 {
112 auto access_x = m_values.template get_access<sycl::access::mode::read_write>(cgh);
113 cgh.fill(access_x,value) ;
114 }) ;
115 }
116
117 /*
118 ConstArrayView<ValueType> getValues() const
119 {
120 return ConstArrayView<ValueType>(m_h_values.size(),m_h_values.data());
121 }
122
123 ArrayView<ValueType> getValues() {
124 return ArrayView<ValueType>(m_h_values.size(),m_h_values.data());
125 }
126 */
127
128 ValueBufferType& values()
129 {
130 return m_values;
131 }
132
133 ValueBufferType& values() const
134 {
135 return m_values;
136 }
137
138 IndexBufferType& kcol() {
139 return m_kcol;
140 }
141
142 IndexBufferType const& kcol() const {
143 return m_kcol;
144 }
145
146 IndexBufferType& cols() {
147 return m_cols;
148 }
149
150 IndexBufferType const& cols() const {
151 return m_cols;
152 }
153
154 HypreProfile& getHypreProfile(IndexType local_offset) const
155 {
156 if(m_hypre_profile.get()==nullptr)
157 {
158 assert(m_profile) ;
159 auto nrows = m_profile->getNRow();
160 auto kcol = m_profile->kcol() ;
161 m_global_rows_ids.resize(nrows) ;
162 m_row_sizes.resize(nrows) ;
163 for(IndexType irow=0;irow<nrows;++irow)
164 {
165 m_global_rows_ids[irow] = local_offset + irow;
166 m_row_sizes[irow] = kcol[irow+1] - kcol[irow];
167 }
168 m_hypre_profile.reset(new HypreProfile(nrows,m_global_rows_ids.data(),m_row_sizes.data()));
169 }
170 return *m_hypre_profile ;
171 }
172
173 COOProfile& getCOOProfile(IndexType local_offset,
174 std::size_t ghost_size=0,
175 const int* ghost_uids=nullptr) const
176 {
177 alien_debug([&] {
178 cout() << "create COOProfile "<<m_coo_profile.get()<<" "<<m_profile->getNRow()<<" "<< m_profile->getNnz()<< " "<<ghost_size<<" "<<ghost_uids;
179 });
180 if(m_coo_profile.get()==nullptr)
181 {
182 assert(m_profile) ;
183 auto nrows = m_profile->getNRow();
184 auto nnz = m_profile->getNnz();
185 auto kcol = m_profile->kcol() ;
186 m_global_rows_ids.resize(nnz) ;
187 m_row_sizes.resize(nrows) ;
188 for(IndexType irow=0;irow<nrows;++irow)
189 {
190 auto row_uid = local_offset + irow;
191 m_row_sizes[irow] = kcol[irow+1] - kcol[irow];
192 for(std::size_t k=kcol[irow];k<kcol[irow+1];++k)
193 m_global_rows_ids[k] = row_uid ;
194 }
195 IndexType* ghost_uids_ptr = ghost_uids ? (IndexType*)ghost_uids : (IndexType*)&m_empty_ghost_uids ;
196
197 m_coo_profile.reset(new COOProfile(nrows,
198 nnz,
199 ghost_size,
200 m_global_rows_ids.data(),
201 m_row_sizes.data(),
202 ghost_uids_ptr));
203 }
204 alien_debug([&] {
205 cout() << "create COOProfile OK";
206 });
207 return *m_coo_profile ;
208 }
209
210
211
212 void copyValuesToHost(std::size_t nnz, ValueT* ptr)
213 {
214 auto h_values = m_values.get_host_access();
215 for (std::size_t i = 0; i < nnz; ++i)
216 ptr[i] = h_values[i];
217 }
218 ProfileType* m_profile = nullptr ;
219
220 mutable ValueBufferType m_values;
221 mutable IndexBufferType m_kcol;
222 mutable IndexBufferType m_cols;
223 mutable std::vector<IndexType> m_global_rows_ids;
224 mutable std::vector<IndexType> m_row_sizes;
225 mutable std::unique_ptr<HypreProfile> m_hypre_profile ;
226 mutable std::unique_ptr<COOProfile> m_coo_profile ;
227 Integer m_empty_ghost_uids = 0 ;
228 };
229
230 /*---------------------------------------------------------------------------*/
231
232 } // namespace Alien::SYCLInternal
233
234 template <typename ValueT>
236 : IMatrixImpl(nullptr, AlgebraTraits<BackEnd::tag::hcsr>::name())
237 , m_local_size(0)
238 {
239 m_profile.reset(new ProfileType()) ;
240 }
241
243 template <typename ValueT>
245 : IMatrixImpl(multi_impl, AlgebraTraits<BackEnd::tag::hcsr>::name())
246 , m_local_size(0)
247 {
248 m_profile.reset(new ProfileType()) ;
249 }
250
251
252 template <typename ValueT>
257/*---------------------------------------------------------------------------*/
258template <typename ValueT>
259void HCSRMatrix<ValueT>::allocate()
260{
261 assert(m_profile.get()) ;
262 m_internal.reset(new InternalType(m_profile.get())) ;
263}
264
265
266/*---------------------------------------------------------------------------*/
267
268} // namespace Alien
269
270/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
IMatrixImpl(const MultiMatrixImpl *multi_impl, BackEndId backend="")
Constructor.
Multi matrices representation container.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17