Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SYCLBEllPackMatrix.cc
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 * Copyright 2020 IFPEN-CEA
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * SPDX-License-Identifier: Apache-2.0
23 */
24
25#include <cassert>
26
27#include <alien/kernels/sycl/data/SYCLEnvInternal.h>
28#include <alien/kernels/sycl/data/SYCLEnv.h>
29
30#include <alien/kernels/sycl/data/SYCLVectorInternal.h>
31#include <alien/kernels/sycl/data/SYCLVector.h>
32
33#include <alien/kernels/sycl/data/SYCLBEllPackInternal.h>
34#include <alien/kernels/sycl/data/SYCLBEllPackMatrix.h>
35
36/*---------------------------------------------------------------------------*/
37
38namespace Alien
39{
40
41SYCLEnv::SYCLEnv()
42{
43 SYCLInternal::EnvInternal::printPlatformInfo() ;
44 m_internal.reset(new SYCLInternal::EnvInternal{});
45}
46
47SYCLEnv::SYCLEnv(int device_id)
48{
49 SYCLInternal::EnvInternal::printPlatformInfo() ;
50 auto gpu_devices = sycl::device::get_devices(sycl::info::device_type::gpu);
51 if(device_id < gpu_devices.size())
52 {
53 sycl::device selected_device = gpu_devices[device_id];
54 m_internal.reset(new SYCLInternal::EnvInternal{selected_device,device_id});
55 }
56 else
57 m_internal.reset(new SYCLInternal::EnvInternal{});
58}
59
60
61SYCLEnv::~SYCLEnv()
62{
63}
64
65SYCLEnv* SYCLEnv::m_instance = nullptr;
66
67SYCLEnv* SYCLEnv::instance()
68{
69 if (!m_instance)
70 m_instance = new SYCLEnv{};
71 return m_instance;
72}
73
74SYCLEnv* SYCLEnv::instance(int device_id)
75{
76 if (!m_instance)
77 m_instance = new SYCLEnv{device_id};
78 if (m_instance->deviceId()!= device_id)
79 {
80 delete m_instance ;
81 m_instance = new SYCLEnv{device_id};
82 }
83 return m_instance;
84}
85
86int SYCLEnv::deviceId()
87{
88 return m_internal->deviceId() ;
89}
90
91std::size_t SYCLEnv::maxNumGroups()
92{
93 return m_internal->maxNumGroups();
94}
95
96std::size_t SYCLEnv::maxWorkGroupSize()
97{
98 return m_internal->maxWorkGroupSize();
99}
100
101std::size_t SYCLEnv::maxNumThreads()
102{
103 return m_internal->maxNumThreads();
104}
105
106template <int EllpackSize, typename IndexT>
107void BEllPackStructInfo<EllpackSize, IndexT>::computeBlockRowOffset(std::vector<int>& block_row_offset,
108 std::size_t nrows,
109 int const* kcol)
110{
111 std::size_t block_nrows = BEllPackStructInfo<EllpackSize, IndexT>::nbBlocks(nrows);
112 block_row_offset.resize(block_nrows + 1);
113 int offset = 0;
114 for (std::size_t ib = 0; ib < block_nrows; ++ib) {
115 block_row_offset[ib] = offset;
116 int max_row_size = 0;
117 for (int i = 0; i < std::min(EllpackSize, int(nrows - ib * EllpackSize)); ++i) {
118 auto irow = ib * EllpackSize + i;
119 int row_size = kcol[irow + 1] - kcol[irow];
120 max_row_size = std::max(max_row_size, row_size);
121 }
122 offset += max_row_size;
123 }
124 block_row_offset[block_nrows] = offset;
125}
126
127template <int EllpackSize, typename IndexT>
129StructInfoInternal(std::size_t nrows,
130 std::size_t nnz,
131 std::size_t block_nrows,
132 std::size_t block_nnz,
133 int const* h_kcol,
134 int const* h_cols,
135 int const* h_block_row_offset,
136 int const* h_local_row_size)
137: m_nrows(nrows)
138, m_nnz(nnz)
139, m_block_nrows(block_nrows)
140, m_block_nnz(block_nnz)
141, m_h_kcol(h_kcol, h_kcol + nrows + 1)
142, m_h_cols(h_cols, h_cols + nnz)
143, m_h_block_cols(block_nnz * ellpack_size)
144, m_block_row_offset(h_block_row_offset, sycl::range<1>(m_block_nrows + 1))
145, m_block_cols(m_h_block_cols.data(), sycl::range<1>(m_block_nnz * ellpack_size))
146, m_kcol(m_h_kcol.data(), sycl::range<1>(m_nrows + 1))
147{
148 auto env = SYCLEnv::instance();
149
150 auto& queue = env->internal()->queue();
151
152 auto num_groups = env->internal()->maxNumGroups();
153
154 auto max_work_group_size = env->internal()->maxWorkGroupSize();
155
156 // building the best number of global thread
157 auto total_threads = num_groups * ellpack_size;
158
159 // clang-format off
160 if(h_local_row_size==nullptr)
161 {
162 IndexBufferType cols_buffer(m_h_cols.data(), sycl::range<1>(m_nnz));
163
164 queue.submit([&](sycl::handler& cgh)
165 {
166 auto access_kcol_buffer = m_kcol.template get_access<sycl::access::mode::read>(cgh);
167 auto access_block_row_offset = m_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
168
169 auto access_cols_buffer = cols_buffer.template get_access<sycl::access::mode::read>(cgh);
170 auto access_block_cols = m_block_cols.template get_access<sycl::access::mode::discard_write>(cgh);
171
172 cgh.parallel_for<class struct_info_sycl>(sycl::range<1>{total_threads},
173 [=] (sycl::item<1> itemId)
174 {
175 auto id = itemId.get_id(0);
176
177 for (auto i = id; i < nrows; i += itemId.get_range()[0])
178 {
179 auto block_id = i/ellpack_size ;
180 auto local_id = i%ellpack_size ;
181
182 int begin = access_kcol_buffer[i] ;
183 int end = access_kcol_buffer[i+1] ;
184 int row_size = end - begin ;
185
186 int block_row_offset = access_block_row_offset[block_id]*ellpack_size ;
187 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
188
189 //access_rx[i] = row_size;
190 /*
191 access_brs[i] = block_row_size ;
192 access_begin[i] = begin ;
193 access_end[i] = end ;
194 access_first_cols[i] = access_cols_buffer[begin] ;
195 */
196 for(int k=begin;k<end;++k)
197 {
198 access_block_cols[block_row_offset+(k-begin)*ellpack_size+local_id] = access_cols_buffer[k] ;
199 }
200 for(int k=row_size;k<block_row_size;++k)
201 {
202 access_block_cols[block_row_offset+k*ellpack_size+local_id] = -1 ;
203 }
204
205 }
206 });
207 });
208 }
209 else
210 {
211 IndexBufferType cols_buffer(m_h_cols.data(), sycl::range<1>(m_nnz));
212 IndexBufferType lrowsize_buffer(h_local_row_size, sycl::range<1>(nrows));
213 // clang-format off
214 queue.submit([&](sycl::handler& cgh)
215 {
216 auto access_kcol_buffer = m_kcol.template get_access<sycl::access::mode::read>(cgh);
217 auto access_block_row_offset = m_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
218 auto access_lrowsize_buffer = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
219
220 auto access_cols_buffer = cols_buffer.template get_access<sycl::access::mode::read>(cgh);
221 auto access_block_cols = m_block_cols.template get_access<sycl::access::mode::discard_write>(cgh);
222
223 cgh.parallel_for<class struct_info_sycl2>(sycl::range<1>{total_threads},
224 [=] (sycl::item<1> itemId)
225 {
226 auto id = itemId.get_id(0);
227
228 for (auto i = id; i < nrows; i += itemId.get_range()[0])
229 {
230 auto block_id = i/ellpack_size ;
231 auto local_id = i%ellpack_size ;
232
233 int begin = access_kcol_buffer[i] ;
234 int lrow_size = access_lrowsize_buffer[i] ;
235
236 int block_row_offset = access_block_row_offset[block_id]*ellpack_size ;
237 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
238
239 for(int k=begin;k<begin+lrow_size;++k)
240 {
241 access_block_cols[block_row_offset+(k-begin)*ellpack_size+local_id] = access_cols_buffer[k] ;
242 }
243 for(int k=lrow_size;k<block_row_size;++k)
244 {
245 access_block_cols[block_row_offset+k*ellpack_size+local_id] = -1 ;
246 }
247 }
248 });
249 });
250 }
251 // clang-format on
252}
253
254template <int EllpackSize, typename IndexT>
255void SYCLInternal::StructInfoInternal<EllpackSize, IndexT>::getUpperDiagOffset() const
256{
257 if (m_h_dcol.size() == 0) {
258 m_h_dcol.resize(m_nrows);
259 for (std::size_t irow = 0; irow < m_nrows; ++irow) {
260 int index = m_h_kcol[irow];
261 for (int k = m_h_kcol[irow]; k < m_h_kcol[irow + 1]; ++k) {
262 if ((std::size_t)m_h_cols[k] < irow)
263 ++index;
264 else
265 break;
266 }
267 m_h_dcol[irow] = index;
268 }
269 }
270}
271
272template <int EllpackSize, typename IndexT>
273void SYCLInternal::StructInfoInternal<EllpackSize, IndexT>::computeLowerUpperMask() const
274{
275
276 if (not m_lower_upper_mask_ready) {
277 m_lower_mask.reset(new MaskBufferType(sycl::range<1>(m_block_nnz * ellpack_size)));
278 m_upper_mask.reset(new MaskBufferType(sycl::range<1>(m_block_nnz * ellpack_size)));
279
280 auto env = SYCLEnv::instance();
281
282 auto& queue = env->internal()->queue();
283
284 auto num_groups = env->internal()->maxNumGroups();
285
286 auto max_work_group_size = env->internal()->maxWorkGroupSize();
287
288 // building the best number of global thread
289 auto total_threads = num_groups * ellpack_size;
290
291 {
292 IndexBufferType dcol_buffer(m_h_dcol.data(), sycl::range<1>(m_nrows));
293
294 auto nrows = m_nrows;
295
296 // clang-format off
297 queue.submit([&](sycl::handler& cgh)
298 {
299 auto access_dcol_buffer = dcol_buffer.template get_access<sycl::access::mode::read>(cgh);
300
301 auto access_kcol_buffer = m_kcol.template get_access<sycl::access::mode::read>(cgh);
302 auto access_block_row_offset = m_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
303
304 auto access_lower_mask = sycl::accessor { *m_lower_mask, cgh, sycl::write_only, sycl::property::no_init{}};
305 auto access_upper_mask = sycl::accessor { *m_upper_mask, cgh, sycl::write_only, sycl::property::no_init{}};
306
307
308 cgh.parallel_for<class lower_upper_mask>(sycl::range<1>{total_threads},
309 [=] (sycl::item<1> itemId)
310 {
311 auto id = itemId.get_id(0);
312
313 for (auto i = id; i < nrows; i += itemId.get_range()[0])
314 {
315 auto block_id = i/ellpack_size ;
316 auto local_id = i%ellpack_size ;
317
318 int begin = access_kcol_buffer[i] ;
319 int end = access_kcol_buffer[i+1] ;
320 int diag = access_dcol_buffer[i] ;
321 int row_size = end - begin ;
322
323 int block_row_offset = access_block_row_offset[block_id]*ellpack_size ;
324 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
325
326 access_lower_mask[block_row_offset+(diag-begin)*ellpack_size+local_id] = 0 ;
327 access_upper_mask[block_row_offset+(diag-begin)*ellpack_size+local_id] = 0 ;
328
329 for(int k=begin;k<diag;++k)
330 {
331 access_lower_mask[block_row_offset+(k-begin)*ellpack_size+local_id] = 1 ;
332 access_upper_mask[block_row_offset+(k-begin)*ellpack_size+local_id] = 0 ;
333 }
334 for(int k=diag+1;k<end;++k)
335 {
336 access_lower_mask[block_row_offset+(k-begin)*ellpack_size+local_id] = 0 ;
337 access_upper_mask[block_row_offset+(k-begin)*ellpack_size+local_id] = 1 ;
338 }
339 for(int k=row_size;k<block_row_size;++k)
340 {
341 access_lower_mask[block_row_offset+k*ellpack_size+local_id] = 0 ;
342 access_upper_mask[block_row_offset+k*ellpack_size+local_id] = 0 ;
343 }
344
345 }
346 });
347 });
348 // clang-format on
349 }
350 m_lower_upper_mask_ready = true;
351 }
352}
353
354template <int EllpackSize, typename IndexT>
355typename SYCLInternal::StructInfoInternal<EllpackSize, IndexT>::MaskBufferType&
356SYCLInternal::StructInfoInternal<EllpackSize, IndexT>::getLowerMask() const
357{
358 computeLowerUpperMask();
359 return *m_lower_mask;
360}
361
362template <int EllpackSize, typename IndexT>
363typename SYCLInternal::StructInfoInternal<EllpackSize, IndexT>::MaskBufferType&
364SYCLInternal::StructInfoInternal<EllpackSize, IndexT>::getUpperMask() const
365{
366 computeLowerUpperMask();
367 return *m_upper_mask;
368}
369
370template <int EllpackSize, typename IndexT>
371BEllPackStructInfo<EllpackSize, IndexT>::BEllPackStructInfo(std::size_t nrows,
372 int const* kcol,
373 int const* cols,
374 int const* h_block_row_offset,
375 int const* h_local_row_size)
376
377: BaseBEllPackStructInfo(nrows, kcol[nrows])
378, m_block_nrows(BEllPackStructInfo<EllpackSize, IndexT>::nbBlocks(nrows))
379, m_block_nnz(h_block_row_offset[m_block_nrows])
380, m_h_local_row_size(h_local_row_size)
381{
382 // clang-format off
383 alien_debug([&] {
384 cout() << "COMPUTE BELLPACK PROFILE";
385 cout() << " NROWS : "<<this->m_nrows;
386 cout() << " NNZ : "<<this->m_nnz;
387 cout() << " BLOCK NROWS : "<<m_block_nrows;
388 cout() <<" BLOCK NNZ : "<<m_block_nnz;
389 });
390 // clang-format on
391
392 //m_h_block_cols.assign(m_block_nnz*ellpack_size,-1) ;
393 m_internal = new InternalType{ this->m_nrows,
394 this->m_nnz,
395 m_block_nrows,
396 m_block_nnz,
397 kcol,
398 cols,
399 h_block_row_offset,
400 h_local_row_size };
401}
402
403template <int EllpackSize, typename IndexT>
404BEllPackStructInfo<EllpackSize, IndexT>::~BEllPackStructInfo()
405{
406 delete m_internal ;
407}
408
409template <int EllpackSize, typename IndexT>
410typename BEllPackStructInfo<EllpackSize, IndexT>::IndexType const*
411BEllPackStructInfo<EllpackSize, IndexT>::kcol() const
412{
413 return m_internal->kcol();
414}
415
416template <int EllpackSize, typename IndexT>
417typename BEllPackStructInfo<EllpackSize, IndexT>::IndexType const*
418BEllPackStructInfo<EllpackSize, IndexT>::cols() const
419{
420 return m_internal->cols();
421}
422
423template <int EllpackSize, typename IndexT>
424typename BEllPackStructInfo<EllpackSize, IndexT>::IndexType const*
425BEllPackStructInfo<EllpackSize, IndexT>::dcol() const
426{
427 return m_internal->dcol();
428}
429
430namespace SYCLInternal
431{
432 template <typename ValueT, int EllpackSize>
433 MatrixInternal<ValueT, EllpackSize>::MatrixInternal(ProfileType const* profile, int N)
434 : m_profile(profile)
435 , m_N(N)
436 , m_NxN(N*N)
437 , m_h_values(profile->getBlockNnz() * ellpack_size * N*N)
438 , m_values(m_h_values.data(), sycl::range<1>(profile->getBlockNnz() * ellpack_size * N*N))
439 {
440 //m_values.set_final_data(nullptr);
441 //alien_debug([&] { cout() << "SYCL InternalMATRIX" << profile->getBlockNnz() * ellpack_size<< "N="<<m_N; });
442 }
443
444 template <typename ValueT, int EllpackSize>
445 bool MatrixInternal<ValueT, EllpackSize>::setMatrixValuesFromHost()
446 {
447 //alien_debug([&] { cout() << "SYCLMatrix setMatrixValuesFromHost "; });
448 auto env = SYCLEnv::instance();
449 auto& queue = env->internal()->queue();
450 auto num_groups = env->internal()->maxNumGroups();
451 auto max_work_group_size = env->internal()->maxWorkGroupSize();
452 auto total_threads = num_groups * ellpack_size;
453
454 int N = this->m_N;
455 int NxN = this->m_NxN;
456 int pack_size = ellpack_size ;
457 auto nrows = m_profile->getNRows();
458 auto nnz = m_profile->getNnz();
459 auto block_nnz = m_profile->getBlockNnz();
460
461 auto internal_profile = m_profile->internal();
462 auto& kcol = internal_profile->getKCol();
463 auto& block_row_offset = internal_profile->getBlockRowOffset();
464 auto& block_cols = internal_profile->getBlockCols();
465 auto local_row_size = m_profile->localRowSize();
466 if(N==1)
467 {
468 if (local_row_size == nullptr)
469 {
470 assert(m_h_csr_values.size()>=nnz) ;
471 ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
472 // COMPUTE COLS
473 // clang-format off
474 queue.submit([&](sycl::handler& cgh)
475 {
476 auto access_kcol_buffer = kcol.template get_access<sycl::access::mode::read>(cgh);
477 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
478 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
479 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
480
481 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
482 cgh.parallel_for<class set_matrix_values>(
483 sycl::range<1>{total_threads},
484 [=] (sycl::item<1> item_id)
485 {
486 auto id = item_id.get_id(0);
487 //auto local_id = item_id.get_local_id(0);
488 //auto block_id = item_id.get_group(0) ;
489 //auto global_id = item_id.get_global_id(0);
490
491 for (auto i = id; i < nrows; i += item_id.get_range()[0])
492 {
493 auto block_id = i/ellpack_size ;
494 auto local_id = i%ellpack_size ;
495
496 auto begin = access_kcol_buffer[i] ;
497 auto end = access_kcol_buffer[i+1] ;
498 auto row_size = end - begin ;
499
500 int block_row_offset = access_block_row_offset[block_id];
501 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
502
503 for(int k=0;k<row_size;++k)
504 {
505 access_block_values[(block_row_offset+k)*ellpack_size+local_id] = access_values_buffer[begin+k] ;
506 }
507 for(int k=row_size;k<block_row_size;++k)
508 {
509 access_block_values[(block_row_offset+k)*ellpack_size+local_id] = 0 ;
510 }
511 }
512 });
513 }) ;
514 // clang-format on
515 m_values_is_update = true;
516 }
517 else
518 {
519 ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
520 IndexBufferType lrowsize_buffer(local_row_size, sycl::range<1>(nrows));
521 // COMPUTE COLS
522 // clang-format off
523 queue.submit([&](sycl::handler& cgh)
524 {
525 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
526 auto access_lrowsize_buffer = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
527
528 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
529 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
530 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
531
532 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
533 cgh.parallel_for<class set_matrix_values2>(
534 sycl::range<1>{total_threads},
535 [=] (sycl::item<1> item_id)
536 {
537 auto id = item_id.get_id(0);
538 //auto local_id = item_id.get_local_id(0);
539 //auto block_id = item_id.get_group(0) ;
540 //auto global_id = item_id.get_global_id(0);
541
542 for (auto i = id; i < nrows; i += item_id.get_range()[0])
543 {
544 auto block_id = i/pack_size ;
545 auto local_id = i%pack_size ;
546
547 auto begin = access_kcol_buffer[i] ;
548 auto lrow_size = access_lrowsize_buffer[i] ;
549 auto end = begin + lrow_size ;
550
551 int block_row_offset = access_block_row_offset[block_id];
552 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
553
554 for(int k=0;k<lrow_size;++k)
555 {
556 access_block_values[(block_row_offset+k)*pack_size+local_id] = access_values_buffer[begin+k] ;
557 }
558 for(int k=lrow_size;k<block_row_size;++k)
559 {
560 access_block_values[(block_row_offset+k)*pack_size+local_id] = 0. ;
561 }
562 }
563 });
564 }) ;
565
566 auto interface_nrows = m_ext_profile->getNRows();
567 auto ext_nnz = m_ext_profile->getNnz();
568 auto ext_block_nnz = m_ext_profile->getBlockNnz();
569
570 auto ext_internal_profile = m_ext_profile->internal();
571 auto& ext_kcol = ext_internal_profile->getKCol();
572 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
573 //m_h_ext_values.resize(ext_block_nnz) ;
574 m_ext_values.reset(new ValueBufferType(ext_block_nnz * ellpack_size * NxN)) ;
575 {
576 ValueBufferType ext_csr_values_buffer(m_h_csr_ext_values.data(), sycl::range<1>(ext_nnz * NxN));
577 queue.submit([&](sycl::handler& cgh)
578 {
579 auto access_kcol_buffer = ext_kcol.template get_access<sycl::access::mode::read>(cgh);
580 auto access_block_row_offset = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
581 auto access_values_buffer = ext_csr_values_buffer.template get_access<sycl::access::mode::read>(cgh);
582 //auto access_block_values = m_ext_values->template get_access<sycl::access::mode::read_write>(cgh);
583 auto access_block_values = sycl::accessor { *m_ext_values, cgh, sycl::write_only, sycl::property::no_init{}};
584
585 cgh.parallel_for<class set_matrix_values3>(
586 sycl::range<1>{total_threads},
587 [=] (sycl::item<1> item_id)
588 {
589 auto id = item_id.get_id(0);
590
591 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
592 {
593 auto block_id = i/ellpack_size ;
594 auto local_id = i%ellpack_size ;
595 auto begin = access_kcol_buffer[i] ;
596 auto end = access_kcol_buffer[i+1] ;
597 auto row_size = end - begin ;
598
599 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
600 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
601
602 for(int k=begin*NxN;k<end*NxN;++k)
603 {
604 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
605 }
606 for(int k=row_size*NxN;k<block_row_size*NxN;++k)
607 {
608 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
609 }
610 }
611 });
612 }).wait() ;
613 // clang-format on
614 }
615 m_values_is_update = true;
616 }
617 }
618 else
619 {
620 if (local_row_size == nullptr)
621 {
622 assert(m_h_csr_values.size()>=nnz*NxN) ;
623 ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz*NxN));
624 // COMPUTE COLS
625 // clang-format off
626 queue.submit([&](sycl::handler& cgh)
627 {
628 auto access_kcol_buffer = kcol.template get_access<sycl::access::mode::read>(cgh);
629 auto access_cols_buffer = block_cols.template get_access<sycl::access::mode::read>(cgh);
630 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
631 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
632 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
633
634 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
635 cgh.parallel_for<class set_block_matrix_values>(
636 sycl::range<1>{total_threads},
637 [=] (sycl::item<1> item_id)
638 {
639 auto id = item_id.get_id(0);
640 //auto local_id = item_id.get_local_id(0);
641 //auto block_id = item_id.get_group(0) ;
642 //auto global_id = item_id.get_global_id(0);
643
644 for (std::size_t i = id; i < nrows; i += item_id.get_range()[0])
645 {
646 auto block_id = i/pack_size ;
647 auto local_id = i%pack_size ;
648
649 int begin = access_kcol_buffer[i] ;
650 int end = access_kcol_buffer[i+1] ;
651 int row_size = end - begin ;
652
653 int block_row_offset = access_block_row_offset[block_id] ;
654 int block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
655
656 for(int k=0;k<row_size;++k)
657 {
658 for(int i=0;i<N;++i)
659 for(int j=0;j<N;++j)
660 {
661 access_block_values[((block_row_offset+k)*NxN+i*N+j)*pack_size+local_id] = access_values_buffer[(begin+k)*NxN + i*N+j] ;
662 }
663 /*
664 printf("MATRIX[%zu,%d] (%d,%d) = (%f,%f,%f,%f)\n",i,k,block_row_offset,begin, access_values_buffer[(begin+k)*NxN], access_values_buffer[(begin+k)*NxN+1], access_values_buffer[(begin+k)*NxN+2], access_values_buffer[(begin+k)*NxN +3]);*/
665
666 }
667 for(int k=row_size;k<block_row_size;++k)
668 {
669 for(int i=0;i<N;++i)
670 for(int j=0;j<N;++j)
671 {
672 access_block_values[((block_row_offset+k)*NxN + i*N+j)*pack_size+local_id] = 0. ;
673 }
674 }
675 }
676 });
677 }).wait() ;
678 // clang-format on
679 m_values_is_update = true;
680 /*
681 {
682 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(m_values);
683 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(block_row_offset);
684 for(int irow=0;irow<nrows;++irow)
685 {
686 auto ib=irow/ellpack_size ;
687 auto il=irow%ellpack_size ;
688 std::cout<<"MAT["<<irow<<","<<ib<<","<<il<<"]:";
689 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
690 {
691 for(int i=0;i<N;++i)
692 for(int j=0;j<N;++j)
693 std::cout<<h_acc[(k*NxN+i*N+j)*ellpack_size+il]<<",";
694 std::cout<<std::endl;
695 }
696 }
697 }*/
698 }
699 else
700 {
701 /*
702 alien_info([&] {
703 auto h_kcol = internal_profile->kcol() ;
704 auto cols = internal_profile->cols() ;
705 for(int irow=0;irow<nrows;++irow)
706 {
707 cout()<<"ROW["<<irow<<"]:";
708 for(int k=h_kcol[irow];k<h_kcol[irow]+local_row_size[irow];++k)
709 {
710 cout()<<"\t COL "<<cols[k];
711 for(int i=0;i<N;++i)
712 for(int j=0;j<N;++j)
713 cout()<<"\t\t MAT["<<i<<" "<<j<<"] "<<m_h_csr_values[k*NxN+i*N+j];
714 }
715 }
716 });*/
717 ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz*NxN));
718 IndexBufferType lrowsize_buffer(local_row_size, sycl::range<1>(nrows));
719 // COMPUTE COLS
720 // clang-format off
721 queue.submit([&](sycl::handler& cgh)
722 {
723 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
724 auto access_lrowsize_buffer = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
725
726 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
727 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
728 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
729
730 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
731 cgh.parallel_for<class set_block_matrix_values2>(
732 sycl::range<1>{total_threads},
733 [=] (sycl::item<1> item_id)
734 {
735 auto id = item_id.get_id(0);
736 //auto local_id = item_id.get_local_id(0);
737 //auto block_id = item_id.get_group(0) ;
738 //auto global_id = item_id.get_global_id(0);
739
740 for (auto i = id; i < nrows; i += item_id.get_range()[0])
741 {
742 auto block_id = i/pack_size ;
743 auto local_id = i%pack_size ;
744
745 auto begin = access_kcol_buffer[i] ;
746 auto lrow_size = access_lrowsize_buffer[i] ;
747 auto end = begin + lrow_size ;
748
749 auto block_row_offset = access_block_row_offset[block_id];
750 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
751
752 for(int k=0;k<lrow_size;++k)
753 {
754 for(int i=0;i<N;++i)
755 for(int j=0;j<N;++j)
756 {
757 access_block_values[((block_row_offset+k)*NxN+i*N+j)*pack_size+local_id] = access_values_buffer[(begin+k)*NxN+i*N+j] ;
758 }
759 }
760 for(int k=lrow_size;k<block_row_size;++k)
761 {
762 for(int i=0;i<N;++i)
763 for(int j=0;j<N;++j)
764 {
765 access_block_values[((block_row_offset+k)*NxN+i*N+j)*pack_size+local_id] = 0. ;
766 }
767 }
768 }
769 });
770 });
771
772
773 auto interface_nrows = m_ext_profile->getNRows();
774 auto ext_nnz = m_ext_profile->getNnz();
775 auto ext_block_nnz = m_ext_profile->getBlockNnz();
776
777 auto ext_internal_profile = m_ext_profile->internal();
778 auto& ext_kcol = ext_internal_profile->getKCol();
779 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
780 auto& ext_block_cols = ext_internal_profile->getBlockCols();
781 //m_h_ext_values.resize(ext_block_nnz) ;
782 m_ext_values.reset(new ValueBufferType(ext_block_nnz * ellpack_size * NxN)) ;
783 {
784 ValueBufferType ext_csr_values_buffer(m_h_csr_ext_values.data(), sycl::range<1>(ext_nnz * NxN));
785 queue.submit([&](sycl::handler& cgh)
786 {
787 auto access_kcol_buffer = ext_kcol.template get_access<sycl::access::mode::read>(cgh);
788 auto access_block_row_offset = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
789 auto access_values_buffer = ext_csr_values_buffer.template get_access<sycl::access::mode::read>(cgh);
790 //auto access_block_values = m_ext_values->template get_access<sycl::access::mode::read_write>(cgh);
791 auto access_block_values = sycl::accessor { *m_ext_values, cgh, sycl::write_only, sycl::property::no_init{}};
792
793 cgh.parallel_for<class set_matrix_values3>(
794 sycl::range<1>{total_threads},
795 [=] (sycl::item<1> item_id)
796 {
797 auto id = item_id.get_id(0);
798
799 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
800 {
801 auto block_id = i/pack_size ;
802 auto local_id = i%pack_size ;
803 auto begin = access_kcol_buffer[i] ;
804 auto end = access_kcol_buffer[i+1] ;
805 auto row_size = end - begin ;
806
807 int block_row_offset = access_block_row_offset[block_id] ;
808 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
809
810 for(int k=0;k<row_size;++k)
811 {
812 for(int i=0;i<N;++i)
813 for(int j=0;j<N;++j)
814 {
815 access_block_values[((block_row_offset+k)*NxN+i*N+j)*pack_size+local_id] = access_values_buffer[(begin+k)*NxN+i*N+j] ;
816 }
817 }
818 for(int k=row_size;k<block_row_size;++k)
819 {
820 for(int i=0;i<N;++i)
821 for(int j=0;j<N;++j)
822 {
823 access_block_values[((block_row_offset+k)*NxN+i*N+j)*pack_size+local_id] = 0 ;
824 }
825 }
826 }
827 });
828 }).wait() ;
829 // clang-format on
830 }
831 /*
832 {
833 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(*m_ext_values);
834 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(ext_block_row_offset);
835 sycl::host_accessor<int, 1, sycl::access::mode::read> cols_acc(ext_block_cols);
836
837 alien_info([&] {
838 for(int i=0;i<interface_nrows;++i)
839 {
840 auto ib = i / pack_size ;
841 auto il = i % pack_size ;
842 cout()<<"EXT MAT["<<i<<","<<ib<<","<<il<<"]:";
843 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
844 {
845 cout()<<"\t K"<<k<<" "<<cols_acc[k*pack_size+il];
846 for(int i=0;i<N;++i)
847 for(int j=0;j<N;++j)
848 cout()<<"\t\t"<<h_acc[(k*NxN+i*N+j)*pack_size+il];
849 //std::cout<<std::endl;
850 }
851 }
852 });
853 }*/
854 m_values_is_update = true;
855 }
856 }
857 return true;
858 }
859
860 template <typename ValueT, int EllpackSize>
861 bool MatrixInternal<ValueT, EllpackSize>::setMatrixValues(ValueBufferType& values_buffer)
862 {
863 //alien_debug([&] { cout() << "SYCLMatrix setMatrixValues "; });
864 auto env = SYCLEnv::instance();
865 auto& queue = env->internal()->queue();
866 auto num_groups = env->internal()->maxNumGroups();
867 auto max_work_group_size = env->internal()->maxWorkGroupSize();
868 auto total_threads = num_groups * ellpack_size;
869
870 int NxN = this->m_NxN;
871
872 auto nrows = m_profile->getNRows();
873 auto nnz = m_profile->getNnz();
874 auto block_nnz = m_profile->getBlockNnz();
875
876 auto internal_profile = m_profile->internal();
877 auto& kcol = internal_profile->getKCol();
878 auto& block_row_offset = internal_profile->getBlockRowOffset();
879
880 auto local_row_size = m_profile->localRowSize();
881 if (local_row_size == nullptr)
882 {
883 //ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
884 // COMPUTE COLS
885 // clang-format off
886 queue.submit([&](sycl::handler& cgh)
887 {
888 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
889 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
890 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
891 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
892
893 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
894 cgh.parallel_for<class set_matrix_values4>(sycl::range<1>{total_threads},
895 [=] (sycl::item<1> item_id)
896 {
897 auto id = item_id.get_id(0);
898 //auto local_id = item_id.get_local_id(0);
899 //auto block_id = item_id.get_group(0) ;
900 //auto global_id = item_id.get_global_id(0);
901
902 for (auto i = id; i < nrows; i += item_id.get_range()[0])
903 {
904 auto block_id = i/ellpack_size ;
905 auto local_id = i%ellpack_size ;
906
907 auto begin = access_kcol_buffer[i] ;
908 auto end = access_kcol_buffer[i+1] ;
909 auto row_size = end - begin ;
910
911 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
912 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
913
914 for(int k=begin*NxN;k<end*NxN;++k)
915 {
916 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
917 }
918 for(int k=row_size*NxN;k<block_row_size*NxN;++k)
919 {
920 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
921 }
922 }
923 });
924 }) ;
925 // clang-format on
926 m_values_is_update = true;
927 }
928 else
929 {
930 //ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
931 IndexBufferType lrowsize_buffer(local_row_size, sycl::range<1>(nrows));
932
933 // COMPUTE COLS
934 // clang-format off
935 queue.submit([&](sycl::handler& cgh)
936 {
937 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
938 auto access_lrowsize_buffer = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
939
940 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
941 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
942 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
943
944 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
945 cgh.parallel_for<class set_matrix_values5>(sycl::range<1>{total_threads},
946 [=] (sycl::item<1> item_id)
947 {
948 auto id = item_id.get_id(0);
949 //auto local_id = item_id.get_local_id(0);
950 //auto block_id = item_id.get_group(0) ;
951 //auto global_id = item_id.get_global_id(0);
952
953 for (auto i = id; i < nrows; i += item_id.get_range()[0])
954 {
955 auto block_id = i/ellpack_size ;
956 auto local_id = i%ellpack_size ;
957
958 auto begin = access_kcol_buffer[i] ;
959 auto lrow_size = access_lrowsize_buffer[i] ;
960 auto end = begin + lrow_size ;
961
962 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
963 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
964
965 for(int k=begin*NxN;k<end*NxN;++k)
966 {
967 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
968 }
969 for(int k=lrow_size*NxN;k<block_row_size*NxN;++k)
970 {
971 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
972 }
973 }
974 });
975 }) ;
976 auto kcol = m_profile->kcol() ;
977 auto local_row_size = m_profile->localRowSize() ;
978
979 auto interface_nrows = m_ext_profile->getNRows();
980 auto ext_nnz = m_ext_profile->getNnz();
981 auto ext_block_nnz = m_ext_profile->getBlockNnz();
982
983 auto ext_internal_profile = m_ext_profile->internal();
984 auto& ext_kcol = ext_internal_profile->getKCol();
985 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
986 //m_h_ext_values.resize(ext_block_nnz) ;
987 m_ext_values.reset(new ValueBufferType(ext_block_nnz * ellpack_size * NxN)) ;
988
989 // EXTRACT EXTERNAL PROFILE
990 {
991 ValueBufferType ext_csr_values_buffer(m_h_csr_ext_values.data(), sycl::range<1>(ext_nnz * NxN));
992
993 std::vector<Integer> h_local_row_offset(interface_nrows+1) ;
994 {
995 Integer offset = 0 ;
996 for(std::size_t i=0;i<interface_nrows;++i)
997 {
998 h_local_row_offset[i] = offset ;
999 offset += local_row_size[i] ;
1000 }
1001 h_local_row_offset[interface_nrows] = offset ;
1002 }
1003 IndexBufferType local_row_offset(h_local_row_offset.data(),sycl::range<1>(interface_nrows+1)) ;
1004
1005 queue.submit([&](sycl::handler& cgh)
1006 {
1007 auto access_kcol = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
1008 auto access_interface_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
1009 auto access_local_row_size = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
1010 auto access_local_row_offset = local_row_offset.template get_access<sycl::access::mode::read>(cgh);
1011 auto access_csr_values = values_buffer.template get_access<sycl::access::mode::read>(cgh);
1012 auto access_ext_csr_values = ext_csr_values_buffer.template get_access<sycl::access::mode::read_write>(cgh);
1013 cgh.parallel_for<class set_matrix_values6>(sycl::range<1>{total_threads},
1014 [=] (sycl::item<1> item_id)
1015 {
1016 auto id = item_id.get_id(0);
1017 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
1018 {
1019 Integer jcol = access_local_row_offset[i] ;
1020 for (int k = (access_kcol[i] + access_local_row_size[i])*NxN; k < access_kcol[i + 1] * NxN; ++k)
1021 access_ext_csr_values[jcol++] = access_csr_values[k];
1022 }
1023 });
1024 }).wait() ;
1025
1026 queue.submit([&](sycl::handler& cgh)
1027 {
1028 auto access_kcol_buffer = ext_kcol.template get_access<sycl::access::mode::read>(cgh);
1029 auto access_block_row_offset = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1030 auto access_values_buffer = ext_csr_values_buffer.template get_access<sycl::access::mode::read>(cgh);
1031 //auto access_block_values = m_ext_values->template get_access<sycl::access::mode::read_write>(cgh);
1032 auto access_block_values = sycl::accessor { *m_ext_values, cgh, sycl::write_only, sycl::property::no_init{}};
1033
1034 cgh.parallel_for<class set_matrix_values7>(sycl::range<1>{total_threads},
1035 [=] (sycl::item<1> item_id)
1036 {
1037 auto id = item_id.get_id(0);
1038
1039 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
1040 {
1041 auto block_id = i/ellpack_size ;
1042 auto local_id = i%ellpack_size ;
1043 auto begin = access_kcol_buffer[i] ;
1044 auto end = access_kcol_buffer[i+1] ;
1045 auto row_size = end - begin ;
1046
1047 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
1048 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1049
1050 for(int k=begin*NxN;k<end*NxN;++k)
1051 {
1052 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
1053 }
1054 for(int k=row_size*NxN;k<block_row_size*NxN;++k)
1055 {
1056 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
1057 }
1058 }
1059 });
1060 }).wait() ;
1061 // clang-format on
1062 }
1063 m_values_is_update = true;
1064 }
1065 return true;
1066 }
1067
1068 template <typename ValueT, int EllpackSize>
1069 bool MatrixInternal<ValueT, EllpackSize>::setMatrixValues(ValueBufferType& values_buffer,
1070 ValueBufferType& ext_values_buffer)
1071 {
1072 //alien_debug([&] { cout() << "SYCLMatrix setMatrixValues "; });
1073 auto env = SYCLEnv::instance();
1074 auto& queue = env->internal()->queue();
1075 auto num_groups = env->internal()->maxNumGroups();
1076 auto max_work_group_size = env->internal()->maxWorkGroupSize();
1077 auto total_threads = num_groups * ellpack_size;
1078
1079 int NxN = this->m_NxN;
1080
1081 auto nrows = m_profile->getNRows();
1082 auto nnz = m_profile->getNnz();
1083 auto block_nnz = m_profile->getBlockNnz();
1084
1085 auto internal_profile = m_profile->internal();
1086 auto& kcol = internal_profile->getKCol();
1087 auto& block_row_offset = internal_profile->getBlockRowOffset();
1088
1089 auto local_row_size = m_profile->localRowSize();
1090 if (local_row_size == nullptr)
1091 {
1092 //ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
1093 // COMPUTE COLS
1094 // clang-format off
1095 queue.submit([&](sycl::handler& cgh)
1096 {
1097 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
1098 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
1099 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
1100 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
1101
1102 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
1103 cgh.parallel_for<class set_matrix_values4>(sycl::range<1>{total_threads},
1104 [=] (sycl::item<1> item_id)
1105 {
1106 auto id = item_id.get_id(0);
1107 //auto local_id = item_id.get_local_id(0);
1108 //auto block_id = item_id.get_group(0) ;
1109 //auto global_id = item_id.get_global_id(0);
1110
1111 for (auto i = id; i < nrows; i += item_id.get_range()[0])
1112 {
1113 auto block_id = i/ellpack_size ;
1114 auto local_id = i%ellpack_size ;
1115
1116 auto begin = access_kcol_buffer[i] ;
1117 auto end = access_kcol_buffer[i+1] ;
1118 auto row_size = end - begin ;
1119
1120 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
1121 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1122
1123 for(int k=begin*NxN;k<end*NxN;++k)
1124 {
1125 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
1126 }
1127 for(int k=row_size*NxN;k<block_row_size*NxN;++k)
1128 {
1129 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
1130 }
1131 }
1132 });
1133 }) ;
1134 // clang-format on
1135 m_values_is_update = true;
1136 }
1137 else
1138 {
1139 //ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
1140 IndexBufferType lrowsize_buffer(local_row_size, sycl::range<1>(nrows));
1141
1142 // COMPUTE COLS
1143 // clang-format off
1144 queue.submit([&](sycl::handler& cgh)
1145 {
1146 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
1147 auto access_lrowsize_buffer = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
1148
1149 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
1150 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
1151 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
1152
1153 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
1154 cgh.parallel_for<class set_matrix_values5>(sycl::range<1>{total_threads},
1155 [=] (sycl::item<1> item_id)
1156 {
1157 auto id = item_id.get_id(0);
1158 //auto local_id = item_id.get_local_id(0);
1159 //auto block_id = item_id.get_group(0) ;
1160 //auto global_id = item_id.get_global_id(0);
1161
1162 for (auto i = id; i < nrows; i += item_id.get_range()[0])
1163 {
1164 auto block_id = i/ellpack_size ;
1165 auto local_id = i%ellpack_size ;
1166
1167 auto begin = access_kcol_buffer[i] ;
1168 auto lrow_size = access_lrowsize_buffer[i] ;
1169 auto end = begin + lrow_size ;
1170
1171 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
1172 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1173
1174 for(int k=begin*NxN;k<end*NxN;++k)
1175 {
1176 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
1177 }
1178 for(int k=lrow_size*NxN;k<block_row_size*NxN;++k)
1179 {
1180 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
1181 }
1182 }
1183 });
1184 }) ;
1185 auto kcol = m_profile->kcol() ;
1186 auto local_row_size = m_profile->localRowSize() ;
1187
1188 auto interface_nrows = m_ext_profile->getNRows();
1189 auto ext_nnz = m_ext_profile->getNnz();
1190 auto ext_block_nnz = m_ext_profile->getBlockNnz();
1191
1192 auto ext_internal_profile = m_ext_profile->internal();
1193 auto& ext_kcol = ext_internal_profile->getKCol();
1194 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
1195 //m_h_ext_values.resize(ext_block_nnz) ;
1196 m_ext_values.reset(new ValueBufferType(ext_block_nnz * ellpack_size * NxN)) ;
1197
1198 // EXTRACT EXTERNAL PROFILE
1199 {
1200 ValueBufferType ext_csr_values_buffer(m_h_csr_ext_values.data(), sycl::range<1>(ext_nnz * NxN));
1201
1202 std::vector<Integer> h_local_row_offset(interface_nrows+1) ;
1203 {
1204 Integer offset = 0 ;
1205 for(std::size_t i=0;i<interface_nrows;++i)
1206 {
1207 h_local_row_offset[i] = offset ;
1208 offset += local_row_size[i] ;
1209 }
1210 h_local_row_offset[interface_nrows] = offset ;
1211 }
1212 IndexBufferType local_row_offset(h_local_row_offset.data(),sycl::range<1>(interface_nrows+1)) ;
1213
1214 queue.submit([&](sycl::handler& cgh)
1215 {
1216 auto access_kcol = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
1217 auto access_interface_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
1218 auto access_local_row_size = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
1219 auto access_local_row_offset = local_row_offset.template get_access<sycl::access::mode::read>(cgh);
1220 auto access_csr_values = values_buffer.template get_access<sycl::access::mode::read>(cgh);
1221 auto access_ext_csr_values = ext_csr_values_buffer.template get_access<sycl::access::mode::read_write>(cgh);
1222 cgh.parallel_for<class set_matrix_values6>(sycl::range<1>{total_threads},
1223 [=] (sycl::item<1> item_id)
1224 {
1225 auto id = item_id.get_id(0);
1226 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
1227 {
1228 Integer jcol = access_local_row_offset[i] ;
1229 for (int k = (access_kcol[i] + access_local_row_size[i])*NxN; k < access_kcol[i + 1] * NxN; ++k)
1230 access_ext_csr_values[jcol++] = access_csr_values[k];
1231 }
1232 });
1233 }).wait() ;
1234
1235 queue.submit([&](sycl::handler& cgh)
1236 {
1237 auto access_kcol_buffer = ext_kcol.template get_access<sycl::access::mode::read>(cgh);
1238 auto access_block_row_offset = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1239 auto access_values_buffer = ext_csr_values_buffer.template get_access<sycl::access::mode::read>(cgh);
1240 //auto access_block_values = m_ext_values->template get_access<sycl::access::mode::read_write>(cgh);
1241 auto access_block_values = sycl::accessor { *m_ext_values, cgh, sycl::write_only, sycl::property::no_init{}};
1242
1243 cgh.parallel_for<class set_matrix_values7>(sycl::range<1>{total_threads},
1244 [=] (sycl::item<1> item_id)
1245 {
1246 auto id = item_id.get_id(0);
1247
1248 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
1249 {
1250 auto block_id = i/ellpack_size ;
1251 auto local_id = i%ellpack_size ;
1252 auto begin = access_kcol_buffer[i] ;
1253 auto end = access_kcol_buffer[i+1] ;
1254 auto row_size = end - begin ;
1255
1256 int block_row_offset = access_block_row_offset[block_id]*ellpack_size*NxN ;
1257 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1258
1259 for(int k=begin*NxN;k<end*NxN;++k)
1260 {
1261 access_block_values[block_row_offset+(k-begin*NxN)*ellpack_size+local_id] = access_values_buffer[k] ;
1262 }
1263 for(int k=row_size*NxN;k<block_row_size*NxN;++k)
1264 {
1265 access_block_values[block_row_offset+k*ellpack_size+local_id] = 0 ;
1266 }
1267 }
1268 });
1269 }).wait() ;
1270 // clang-format on
1271 }
1272 m_values_is_update = true;
1273 }
1274 return true;
1275 }
1276
1277 template <typename ValueT, int EllpackSize>
1278 bool MatrixInternal<ValueT, EllpackSize>::setMatrixValues(ValueT const* values, bool only_host)
1279 {
1280 //alien_debug([&] { cout() << "SYCLMatrix setMatrixValues " << only_host; });
1281
1282 int NxN = this->m_NxN;
1283
1284 auto nnz = m_profile->getNnz();
1285 m_h_csr_values.resize(nnz*NxN);
1286 std::copy(values, values + nnz*NxN, m_h_csr_values.begin());
1287 if (m_ext_profile) {
1288 // clang-format off
1289 auto kcol = m_profile->kcol() ;
1290 auto local_row_size = m_profile->localRowSize() ;
1291
1292 auto interface_nrows = m_ext_profile->getNRows() ;
1293 auto ext_nnz = m_ext_profile->getNnz();
1294 // clang-format on
1295
1296 m_h_csr_ext_values.resize(ext_nnz*NxN);
1297
1298 // EXTRACT EXTERNAL PROFILE
1299 {
1300 int jcol = 0;
1301 for (std::size_t i = 0; i < interface_nrows; ++i) {
1302 auto id = m_h_interface_row_ids[i];
1303 for (int k = (kcol[id] + local_row_size[id])*NxN; k < kcol[id + 1]*NxN; ++k) {
1304 m_h_csr_ext_values[jcol++] = values[k];
1305 }
1306 }
1307 }
1308 }
1309 if (only_host) {
1310 m_values_is_update = false;
1311 }
1312 else {
1313 setMatrixValuesFromHost();
1314 m_values_is_update = true;
1315 }
1316 return true;
1317 }
1318
1319 template <typename ValueT, int EllpackSize>
1320 bool MatrixInternal<ValueT, EllpackSize>::copy(std::size_t nb_blocks,
1321 int block_size,
1322 ValueBufferType& values_buffer,
1323 int rhs_block_size)
1324 {
1325 auto env = SYCLEnv::instance();
1326 auto& queue = env->internal()->queue();
1327 auto num_groups = env->internal()->maxNumGroups();
1328 auto max_work_group_size = env->internal()->maxWorkGroupSize();
1329 auto total_threads = num_groups * ellpack_size;
1330
1331 int pack_size = ellpack_size;
1332 int N1 = block_size ;
1333 int N1xN1 = N1*N1 ;
1334 int N2 = rhs_block_size ;
1335 int N2xN2 = N2*N2 ;
1336 auto nrows = m_profile->getNRows();
1337 auto nnz = m_profile->getNnz();
1338 auto block_nnz = m_profile->getBlockNnz();
1339
1340 auto internal_profile = m_profile->internal();
1341 auto& kcol = internal_profile->getKCol();
1342 auto& block_row_offset = internal_profile->getBlockRowOffset();
1343
1344 auto local_row_size = m_profile->localRowSize();
1345 if (local_row_size == nullptr)
1346 {
1347 //ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
1348 // COMPUTE COLS
1349 // clang-format off
1350 queue.submit([&](sycl::handler& cgh)
1351 {
1352 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
1353 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
1354 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
1355 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
1356
1357 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
1358 cgh.parallel_for<class set_matrix_values4>(
1359 sycl::range<1>{total_threads},
1360 [=] (sycl::item<1> item_id)
1361 {
1362 auto id = item_id.get_id(0);
1363 //auto local_id = item_id.get_local_id(0);
1364 //auto block_id = item_id.get_group(0) ;
1365 //auto global_id = item_id.get_global_id(0);
1366
1367 for (auto i = id; i < nrows; i += item_id.get_range()[0])
1368 {
1369 auto block_id = i/pack_size ;
1370 auto local_id = i%pack_size ;
1371
1372 auto begin = access_block_row_offset[block_id] ;
1373 auto end = access_block_row_offset[block_id+1] ;
1374
1375 for(int k=begin;k<end;++k)
1376 {
1377 for(int i=0;i<N1;++i)
1378 for(int j=0;j<N1;++j)
1379 access_block_values[(k*N1xN1 +i*N1+j)*pack_size+local_id] = access_values_buffer[(k*N2xN2+i*N2+j)*pack_size+local_id] ;
1380 }
1381 }
1382 });
1383 }) ;
1384 // clang-format on
1385 }
1386 else
1387 {
1388 //ValueBufferType values_buffer(m_h_csr_values.data(), sycl::range<1>(nnz));
1389 IndexBufferType lrowsize_buffer(local_row_size, sycl::range<1>(nrows));
1390
1391 // COMPUTE COLS
1392 // clang-format off
1393 queue.submit([&](sycl::handler& cgh)
1394 {
1395 auto access_kcol_buffer = internal_profile->getKCol().template get_access<sycl::access::mode::read>(cgh);
1396 auto access_lrowsize_buffer = lrowsize_buffer.template get_access<sycl::access::mode::read>(cgh);
1397
1398 auto access_block_row_offset = internal_profile->getBlockRowOffset().template get_access<sycl::access::mode::read>(cgh);
1399 auto access_values_buffer = values_buffer.template get_access<sycl::access::mode::read>(cgh);
1400 auto access_block_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
1401
1402 //cgh.parallel_for<class vector_axpy>(sycl::nd_range<1>{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}},[=](sycl::nd_item<1> item_id)
1403 cgh.parallel_for<class set_matrix_values5>(
1404 sycl::range<1>{total_threads},
1405 [=] (sycl::item<1> item_id)
1406 {
1407 auto id = item_id.get_id(0);
1408 //auto local_id = item_id.get_local_id(0);
1409 //auto block_id = item_id.get_group(0) ;
1410 //auto global_id = item_id.get_global_id(0);
1411
1412 for (auto i = id; i < nrows; i += item_id.get_range()[0])
1413 {
1414 auto block_id = i/pack_size ;
1415 auto local_id = i%pack_size ;
1416
1417
1418 int block_row_offset = access_block_row_offset[block_id] ;
1419
1420 auto begin = access_block_row_offset[block_id] ;
1421 auto end = access_block_row_offset[block_id+1] ;
1422 auto lrow_size = access_lrowsize_buffer[i] ;
1423
1424 for(int k=begin;k<begin+lrow_size;++k)
1425 {
1426 for(int i=0;i<N1;++i)
1427 for(int j=0;j<N1;++j)
1428 access_block_values[(k*N1xN1 +i*N1+j)*pack_size+local_id] = access_values_buffer[(k*N2xN2+i*N2+j)*pack_size+local_id] ;
1429 }
1430 for(int k=begin+lrow_size;k<end;++k)
1431 {
1432 for(int i=0;i<N1;++i)
1433 for(int j=0;j<N1;++j)
1434 access_block_values[(k*N1xN1 +i*N1+j)*pack_size+local_id] = 0 ;
1435 }
1436 }
1437 });
1438 }) ;
1439 // clang-format on
1440 }
1441 m_values_is_update = true;
1442 return true;
1443 }
1444
1445 template <typename ValueT, int EllpackSize>
1446 bool MatrixInternal<ValueT, EllpackSize>::copy(std::size_t nb_blocks,
1447 int block_size,
1448 ValueBufferType& values_buffer,
1449 ValueBufferType& ext_values_buffer,
1450 int rhs_block_size)
1451 {
1452 copy(nb_blocks,block_size,values_buffer,rhs_block_size) ;
1453
1454 auto env = SYCLEnv::instance();
1455 auto& queue = env->internal()->queue();
1456 auto num_groups = env->internal()->maxNumGroups();
1457 auto max_work_group_size = env->internal()->maxWorkGroupSize();
1458 auto total_threads = num_groups * ellpack_size;
1459
1460 int pack_size = ellpack_size;
1461 int N1 = block_size ;
1462 int N1xN1 = N1*N1 ;
1463 int N2 = rhs_block_size ;
1464 int N2xN2 = N2*N2 ;
1465 auto nrows = m_profile->getNRows();
1466 auto nnz = m_profile->getNnz();
1467 auto block_nnz = m_profile->getBlockNnz();
1468
1469 auto internal_profile = m_profile->internal();
1470 auto& kcol = internal_profile->getKCol();
1471 auto& block_row_offset = internal_profile->getBlockRowOffset();
1472
1473 assert(m_ext_profile) ;
1474 auto interface_nrows = m_ext_profile->getNRows();
1475 auto ext_nnz = m_ext_profile->getNnz();
1476 auto ext_block_nnz = m_ext_profile->getBlockNnz();
1477 auto ext_internal_profile = m_ext_profile->internal();
1478 auto& ext_kcol = ext_internal_profile->getKCol();
1479 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
1480 auto& ext_block_cols = ext_internal_profile->getBlockCols() ;
1481
1482 m_h_ext_values.resize(ext_block_nnz*N1xN1) ;
1483 m_ext_values.reset(new ValueBufferType(ext_block_nnz * N1xN1 * pack_size)) ;
1484 {
1485 queue.submit([&](sycl::handler& cgh)
1486 {
1487 auto access_block_row_offset = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1488 auto access_ext_values_buffer = ext_values_buffer.template get_access<sycl::access::mode::read>(cgh);
1489 auto access_ext_values = sycl::accessor { *m_ext_values, cgh, sycl::write_only, sycl::property::no_init{}};
1490
1491 cgh.parallel_for<class set_matrix_ext_values3>(
1492 sycl::range<1>{total_threads},
1493 [=] (sycl::item<1> item_id)
1494 {
1495 auto id = item_id.get_id(0);
1496 auto stride = item_id.get_range()[0];
1497 for (auto i = id; i < interface_nrows; i += stride)
1498 {
1499 auto block_id = i / pack_size ;
1500 auto local_id = i % pack_size ;
1501
1502 auto begin = access_block_row_offset[block_id] ;
1503 auto end = access_block_row_offset[block_id+1] ;
1504
1505 for(int k=begin;k<end;++k)
1506 {
1507 for(int i=0;i<N1;++i)
1508 for(int j=0;j<N1;++j)
1509 access_ext_values[(k*N1xN1 +i*N1+j)*pack_size+local_id] = access_ext_values_buffer[(k*N2xN2+i*N2+j)*pack_size+local_id] ;
1510 }
1511 }
1512 });
1513 }) ;
1514 // clang-format on
1515 }
1516 m_values_is_update = true;
1517 return true;
1518 }
1519
1520
1521 template <typename ValueT, int EllpackSize>
1522 bool MatrixInternal<ValueT, EllpackSize>::needUpdate()
1523 {
1524 return m_values_is_update != true;
1525 }
1526
1527 template <typename ValueT, int EllpackSize>
1528 void MatrixInternal<ValueT, EllpackSize>::notifyChanges()
1529 {
1530 m_values_is_update = false;
1531 }
1532
1533 template <typename ValueT, int EllpackSize>
1534 void MatrixInternal<ValueT, EllpackSize>::endUpdate()
1535 {
1536 if (not m_values_is_update) {
1537 setMatrixValuesFromHost();
1538 }
1539 }
1540
1541 /*
1542 template<int N>
1543 template <typename ValueT, int EllPackSize>
1544 void MatrixInternal<ValueT, EllPackSize>::multN(ValueBufferType& x, ValueBufferType& y, sycl::queue& queue) const
1545 {
1546 auto device = queue.get_device();
1547
1548 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
1549 // getting the maximum work group size per thread
1550 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
1551 // building the best number of global thread
1552
1553 // clang-format off
1554 std::size_t pack_size = ellpack_size;
1555 auto nrows = m_profile->getNRows();
1556 auto nnz = m_profile->getNnz();
1557
1558 auto internal_profile = m_profile->internal();
1559 auto& kcol = internal_profile->getKCol();
1560 auto& block_row_offset = internal_profile->getBlockRowOffset();
1561 auto& block_cols = internal_profile->getBlockCols();
1562
1563 auto blocks_needed = (nrows + ellpack_size - 1) / ellpack_size;
1564 auto blocks_target = std::max(blocks_needed, num_groups * 4UL);
1565 auto total_threads = blocks_target * pack_size;
1566
1567 queue.submit(
1568 [&](sycl::handler& cgh)
1569 {
1570 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1571 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
1572 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
1573
1574 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
1575 auto access_y = y.template get_access<sycl::access::mode::discard_write>(cgh);
1576
1577 auto tile = TileT<N>() ;
1578
1579 sycl::local_accessor<ValueType, 1> lds_x{pack_size*N, cgh};
1580 sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{pack_size}};
1581 cgh.parallel_for<class compute_mult>(r,
1582 [=](sycl::nd_item<1> item_id)
1583 {
1584 auto local_id = item_id.get_local_id(0);
1585 auto global_id = item_id.get_global_id(0);
1586
1587 for (auto i = global_id; i < nrows; i += item_id.get_global_range()[0])
1588 {
1589 auto block_id = i/pack_size ;
1590
1591 int begin = access_block_row_offset[block_id] ;
1592 int end = access_block_row_offset[block_id+1] ;
1593 for(int ieq=0;ieq<N;++ieq)
1594 {
1595 ValueType value = 0. ;
1596 for(int k=begin;k<end;++k)
1597 {
1598 //auto k = block_row_offset+j*ellpack_size+local_id ;
1599 const int col = access_cols[k * pack_size + local_id];
1600 if(col>=0)
1601 for(int ju=0;ju<N;++ju)
1602 lds_x[N*local_id+ju] = access_x[col*N+ju];
1603 item_id.barrier(sycl::access::fence_space::local_space);
1604 if(col>=0)
1605 {
1606 for(int ju=0;ju<N;++ju)
1607 value += access_values[tile.ijk(k,ieq,ju) + local_id] * lds_x[local_id*N+ju] ;
1608 }
1609 item_id.barrier(sycl::access::fence_space::local_space);
1610 }
1611 access_y[i*N+ieq] = value ;
1612 }
1613 }
1614 });
1615 });
1616 }*/
1617
1618 template <typename ValueT, int EllPackSize>
1619 void MatrixInternal<ValueT, EllPackSize>::mult(ValueBufferType& x, ValueBufferType& y, sycl::queue& queue) const
1620 {
1621 // clang-format on
1622 if(this->m_N==1)
1623 {
1624 auto device = queue.get_device();
1625
1626 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
1627 // getting the maximum work group size per thread
1628 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
1629 // building the best number of global thread
1630 //auto total_threads = num_groups * ellpack_size;
1631
1632 // clang-format off
1633 //int NxN = this->m_NxN;
1634 std::size_t pack_size = ellpack_size;
1635 auto nrows = m_profile->getNRows();
1636 auto nnz = m_profile->getNnz();
1637
1638 auto internal_profile = m_profile->internal();
1639 auto& kcol = internal_profile->getKCol();
1640 auto& block_row_offset = internal_profile->getBlockRowOffset();
1641 auto& block_cols = internal_profile->getBlockCols();
1642
1643 auto blocks_needed = (nrows + ellpack_size - 1) / ellpack_size;
1644 auto blocks_target = std::max(blocks_needed, num_groups * 4UL);
1645 auto total_threads = blocks_target * ellpack_size;
1646
1647 // COMPUTE VALUES
1648 // clang-format off
1649 queue.submit([&](sycl::handler& cgh)
1650 {
1651 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1652 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
1653 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
1654
1655
1656 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
1657 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
1658#ifdef OLD
1659 //sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}};
1660 //cgh.parallel_for<class compute_mult>(r, [&](sycl::nd_item<1> item_id)
1661 cgh.parallel_for<class compute_mult>(
1662 sycl::range<1>{total_threads},
1663 [=] (sycl::item<1> item_id)
1664 {
1665 auto id = item_id.get_id(0);
1666 //auto local_id = item_id.get_local_id(0);
1667 //auto block_id = item_id.get_group(0) ;
1668 //auto global_id = item_id.get_global_id(0);
1669
1670 for (auto i = id; i < nrows; i += item_id.get_range()[0])
1671 {
1672 auto block_id = i/ellpack_size ;
1673 auto local_id = i%ellpack_size ;
1674 int block_row_offset = access_block_row_offset[block_id]*ellpack_size ;
1675 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1676
1677 ValueType value = 0. ;
1678 for(int j=0;j<block_row_size;++j)
1679 {
1680 auto k = block_row_offset+j*ellpack_size+local_id ;
1681 value += access_values[k]* access_x[access_cols[k]] ;
1682 }
1683 access_y[i] = value ;
1684 }
1685 });
1686
1687#endif
1688 sycl::local_accessor<ValueType, 1> lds_x{pack_size, cgh};
1689 sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{pack_size}};
1690 cgh.parallel_for<class compute_mult>(r,
1691 [=](sycl::nd_item<1> item_id)
1692 {
1693 //auto id = item_id.get_id(0);
1694 auto local_id = item_id.get_local_id(0);
1695 //auto block_id = item_id.get_group(0) ;
1696 auto global_id = item_id.get_global_id(0);
1697
1698 //for (auto i = id; i < nrows; i += item_id.get_range()[0])
1699 for (auto i = global_id; i < nrows; i += item_id.get_global_range()[0])
1700 {
1701 auto block_id = i/pack_size ;
1702 //auto local_id = i%ellpack_size ;
1703
1704 int begin = access_block_row_offset[block_id] ;
1705 int end = access_block_row_offset[block_id+1] ;
1706 ValueType value = 0. ;
1707 for(int k=begin;k<end;++k)
1708 {
1709 //auto k = block_row_offset+j*ellpack_size+local_id ;
1710 const int col = access_cols[k * pack_size + local_id];
1711 if(col>=0)
1712 lds_x[local_id] = access_x[col];
1713 item_id.barrier(sycl::access::fence_space::local_space);
1714 if(col>=0)
1715 value += access_values[k * pack_size + local_id] * lds_x[local_id] ;
1716 item_id.barrier(sycl::access::fence_space::local_space);
1717 }
1718 access_y[i] = value ;
1719 }
1720 });
1721 });
1722 // clang-format on
1723 }
1724 else
1725 {
1726 const bool done = dispatchN<2, 20>(this->m_N, [&](auto N) {
1727 multN<decltype(N)::value>(x, y, queue);
1728 });
1729 if (!done)
1730 {
1731 auto device = queue.get_device();
1732
1733 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
1734 // getting the maximum work group size per thread
1735 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
1736 // building the best number of global thread
1737 auto total_threads = num_groups * ellpack_size;
1738
1739 // clang-format off
1740 int N = this->m_N;
1741 //int NxN = this->m_NxN;
1742 std::size_t pack_size = ellpack_size;
1743 auto nrows = m_profile->getNRows();
1744 auto nnz = m_profile->getNnz();
1745
1746 auto internal_profile = m_profile->internal();
1747 auto& kcol = internal_profile->getKCol();
1748 auto& block_row_offset = internal_profile->getBlockRowOffset();
1749 auto& block_cols = internal_profile->getBlockCols();
1750
1751 queue.submit(
1752 [&](sycl::handler& cgh)
1753 {
1754 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1755 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
1756 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
1757
1758
1759 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
1760 auto access_y = y.template get_access<sycl::access::mode::discard_write>(cgh);
1761
1762 //sycl::local_accessor<ValueT,1> local_mem(sycl::range<1>(ellpack_size*N), cgh);
1763 //sycl::nd_range<1> range{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}};
1764 sycl::range<1> range{total_threads} ;
1765 auto tile = Tile(N) ;
1766 //cgh.parallel_for<class compute_mult>(r, [&](sycl::nd_item<1> item_id)
1767 cgh.parallel_for<class compute_block_mult>(range,
1768 [=] (sycl::item<1> item_id /*sycl::nd_item<1> item_id*/)
1769 {
1770 auto id = item_id.get_id(0);
1771 //auto local_id = item_id.get_local_id(0);
1772 //auto block_id = item_id.get_group(0) ;
1773 //auto global_id = item_id.get_global_id(0);
1774
1775
1776
1777 for (auto i = id; i < nrows; i += item_id.get_range()[0] /*item_id.get_local_range(0)*/)
1778 {
1779 auto block_id = i/pack_size ;
1780 auto local_id = i%pack_size ;
1781
1782 std::size_t block_row_offset = access_block_row_offset[block_id] ;
1783 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1784
1785 for(int ieq=0;ieq<N;++ieq)
1786 {
1787 ValueType value = 0. ;
1788 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
1789 {
1790 // value += access_values[k]* access_x[access_cols[k]] ;
1791 value += tile.mult(ieq,
1792 local_id,
1793 k,
1794 access_cols,
1795 access_values,
1796 access_x) ;
1797 }
1798 access_y[i*N+ieq] = value;
1799 }
1800 }
1801 });
1802 });
1803 }
1804
1805#ifdef OLD
1806 switch(this->m_N)
1807 {
1808 case 2:
1809 multN<2>(x,y,queue) ;
1810 break ;
1811 case 3:
1812 multN<3>(x,y,queue) ;
1813 break;
1814 case 4:
1815 multN<4>(x,y,queue) ;
1816 break;
1817 default:
1818 auto device = queue.get_device();
1819
1820 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
1821 // getting the maximum work group size per thread
1822 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
1823 // building the best number of global thread
1824 auto total_threads = num_groups * ellpack_size;
1825
1826 // clang-format off
1827 int N = this->m_N;
1828 //int NxN = this->m_NxN;
1829 std::size_t pack_size = ellpack_size;
1830 auto nrows = m_profile->getNRows();
1831 auto nnz = m_profile->getNnz();
1832
1833 auto internal_profile = m_profile->internal();
1834 auto& kcol = internal_profile->getKCol();
1835 auto& block_row_offset = internal_profile->getBlockRowOffset();
1836 auto& block_cols = internal_profile->getBlockCols();
1837
1838 queue.submit(
1839 [&](sycl::handler& cgh)
1840 {
1841 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1842 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
1843 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
1844
1845
1846 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
1847 auto access_y = y.template get_access<sycl::access::mode::discard_write>(cgh);
1848
1849 //sycl::local_accessor<ValueT,1> local_mem(sycl::range<1>(ellpack_size*N), cgh);
1850 //sycl::nd_range<1> range{sycl::range<1>{total_threads},sycl::range<1>{ellpack_size}};
1851 sycl::range<1> range{total_threads} ;
1852 auto tile = Tile(N) ;
1853 //cgh.parallel_for<class compute_mult>(r, [&](sycl::nd_item<1> item_id)
1854 cgh.parallel_for<class compute_block_mult>(range,
1855 [=] (sycl::item<1> item_id /*sycl::nd_item<1> item_id*/)
1856 {
1857 auto id = item_id.get_id(0);
1858 //auto local_id = item_id.get_local_id(0);
1859 //auto block_id = item_id.get_group(0) ;
1860 //auto global_id = item_id.get_global_id(0);
1861
1862
1863
1864 for (auto i = id; i < nrows; i += item_id.get_range()[0] /*item_id.get_local_range(0)*/)
1865 {
1866 auto block_id = i/pack_size ;
1867 auto local_id = i%pack_size ;
1868
1869 std::size_t block_row_offset = access_block_row_offset[block_id] ;
1870 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
1871
1872 for(int ieq=0;ieq<N;++ieq)
1873 {
1874 ValueType value = 0. ;
1875 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
1876 {
1877 // value += access_values[k]* access_x[access_cols[k]] ;
1878 value += tile.mult(ieq,
1879 local_id,
1880 k,
1881 access_cols,
1882 access_values,
1883 access_x) ;
1884 }
1885 access_y[i*N+ieq] = value;
1886 }
1887 }
1888 });
1889 });
1890 }
1891#endif
1892 /*
1893 {
1894 int N = this->m_N;
1895 //int NxN = this->m_NxN;
1896 std::size_t pack_size = ellpack_size;
1897 auto nrows = m_profile->getNRows();
1898 auto nnz = m_profile->getNnz();
1899
1900 auto internal_profile = m_profile->internal();
1901 auto& kcol = internal_profile->getKCol();
1902 auto& block_row_offset = internal_profile->getBlockRowOffset();
1903 auto& block_cols = internal_profile->getBlockCols();
1904
1905 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> x_acc(x);
1906 for(int il=0;il<nrows;++il)
1907 {
1908 alien_info([&] { cout()<<"X["<<il<<"]:"; });
1909 for(int i=0;i<N;++i)
1910 alien_info([&] { cout()<<x_acc[il*N+i]<<","; });
1911
1912 }
1913 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> y_acc(y);
1914 for(int il=0;il<nrows;++il)
1915 {
1916 alien_info([&] { cout()<<"Y["<<il<<"]:";});
1917 for(int i=0;i<N;++i)
1918 alien_info([&] { cout()<<y_acc[il*N+i]<<","; });
1919 }
1920 Universe().traceMng()->flush() ;
1921
1922 auto tile = Tile(N) ;
1923 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> matrix_acc(m_values);
1924 sycl::host_accessor<int, 1, sycl::access::mode::read> cols_acc(block_cols);
1925 sycl::host_accessor<int, 1, sycl::access::mode::read> brow_offset_acc(block_row_offset);
1926 for(std::size_t irow=0;irow<nrows;++irow)
1927 {
1928 auto ib = irow/ellpack_size ;
1929 auto il = irow%ellpack_size ;
1930 for(int ieq=0;ieq<N;++ieq)
1931 {
1932 std::cout<<"LINE["<<il<<","<<ieq<<"] : ";
1933 ValueType value = 0. ;
1934 for(std::size_t k=brow_offset_acc[ib];k<brow_offset_acc[ib+1];++k)
1935 {
1936 // value += access_values[k]* access_x[access_cols[k]] ;
1937 value += tile.mult(ieq,
1938 il,
1939 k,
1940 cols_acc,
1941 matrix_acc,
1942 x_acc) ;
1943 }
1944 std::cout<<"\nY_CPU["<<irow<<","<<ieq<<"]:"<<value<<std::endl;
1945 }
1946 }
1947 }*/
1948 // clang-format on
1949 }
1950 }
1951
1952 template <typename ValueT, int EllPackSize>
1953 void MatrixInternal<ValueT, EllPackSize>::mult(ValueBufferType& x, ValueBufferType& y) const
1954 {
1955 this->mult(x, y, SYCLEnv::instance()->internal()->queue());
1956 }
1957
1958 template <typename ValueT, int EllPackSize>
1959 void MatrixInternal<ValueT, EllPackSize>::addExtMult(ValueBufferType& x,
1960 ValueBufferType& y,
1961 sycl::queue& queue) const
1962 {
1963 auto device = queue.get_device();
1964
1965 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
1966 // getting the maximum work group size per thread
1967 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
1968 // building the best number of global thread
1969 auto total_threads = num_groups * ellpack_size;
1970
1971 //int N = this->m_N;
1972 //int NxN = this->m_NxN;
1973 std::size_t pack_size = ellpack_size;
1974
1975 // clang-format off
1976 auto nrows = m_profile->getNRows();
1977 auto nnz = m_profile->getNnz();
1978
1979 auto interface_nrow = m_ext_profile->getNRows();
1980 auto ext_profile = m_ext_profile->internal();
1981 auto& kcol = ext_profile->getKCol();
1982 auto& block_row_offset = ext_profile->getBlockRowOffset();
1983 auto& block_cols = ext_profile->getBlockCols();
1984 // clang-format on
1985 if(this->m_N==1)
1986 {
1987 // clang-format off
1988 queue.submit([&](sycl::handler& cgh)
1989 {
1990 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
1991 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
1992 auto access_values = m_ext_values->template get_access<sycl::access::mode::read>(cgh);
1993
1994 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
1995
1996 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
1997 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
1998
1999 cgh.parallel_for<class compute_ext_mult>(
2000 sycl::range<1>{total_threads},
2001 [=] (sycl::item<1> item_id)
2002 {
2003 auto id = item_id.get_id(0);
2004
2005 for (auto i = id; i < interface_nrow; i += item_id.get_range()[0])
2006 {
2007 auto block_id = i / pack_size ;
2008 auto local_id = i % pack_size ;
2009
2010 int block_row_offset = access_block_row_offset[block_id]*ellpack_size ;
2011 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2012
2013 ValueType value = 0. ;
2014 for(int j=0;j<block_row_size;++j)
2015 {
2016 auto k = block_row_offset+j*ellpack_size+local_id ;
2017 value += access_values[k]* access_x[access_cols[k]] ;
2018 }
2019 access_y[access_row_ids[i]] += value ;
2020 }
2021 });
2022 });
2023 // clang-format on
2024 }
2025 else
2026 {
2027 const bool done = dispatchN<2, 20>(
2028 this->m_N,
2029 [&](auto N)
2030 {
2031 addExtMultN<decltype(N)::value>(1., x, y, queue);
2032 });
2033 if (!done)
2034 {
2035 // clang-format off
2036
2037 int N = this->m_N;
2038 queue.submit([&](sycl::handler& cgh)
2039 {
2040 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2041 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2042 auto access_values = m_ext_values->template get_access<sycl::access::mode::read>(cgh);
2043
2044 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
2045
2046 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2047 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2048
2049 auto tile = Tile(N) ;
2050 cgh.parallel_for<class compute_ext_mult>(
2051 sycl::range<1>{total_threads},
2052 [=] (sycl::item<1> item_id)
2053 {
2054 auto id = item_id.get_id(0);
2055
2056 for (auto i = id; i < interface_nrow; i += item_id.get_range()[0])
2057 {
2058 auto block_id = i / pack_size ;
2059 auto local_id = i % pack_size ;
2060
2061 int block_row_offset = access_block_row_offset[block_id] * pack_size ;
2062 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2063
2064 for(int ieq=0;ieq<N;++ieq)
2065 {
2066 ValueType value = 0. ;
2067 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2068 {
2069 // value += access_values[k]* access_x[access_cols[k]] ;
2070 value += tile.mult(ieq,
2071 local_id,
2072 k,
2073 access_cols,
2074 access_values,
2075 access_x) ;
2076 }
2077 access_y[access_row_ids[i]*N+ieq] += value;
2078 }
2079 }
2080 });
2081 });
2082 // clang-format on
2083 }
2084 }
2085 }
2086
2087 template <typename ValueT, int EllPackSize>
2088 void MatrixInternal<ValueT, EllPackSize>::addExtMult(ValueBufferType& x,
2089 ValueBufferType& y) const
2090 {
2091 this->addExtMult(x, y, SYCLEnv::instance()->internal()->queue());
2092 }
2093
2094 template <typename ValueT, int EllPackSize>
2095 void MatrixInternal<ValueT, EllPackSize>::addLMult(ValueType alpha,
2096 ValueBufferType& x,
2097 ValueBufferType& y,
2098 sycl::queue& queue) const
2099 {
2100
2101 if(this->m_N==1)
2102 {
2103 auto device = queue.get_device();
2104
2105 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2106 // getting the maximum work group size per thread
2107 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2108
2109 std::size_t pack_size = ellpack_size;
2110 auto nrows = m_profile->getNRows();
2111 auto nnz = m_profile->getNnz();
2112
2113 auto internal_profile = m_profile->internal();
2114 auto& kcol = internal_profile->getKCol();
2115 auto& block_row_offset = internal_profile->getBlockRowOffset();
2116 auto& block_cols = internal_profile->getBlockCols();
2117
2118 auto& mask = internal_profile->getLowerMask();
2119
2120 // clang-format off
2121 queue.submit(
2122 [&](sycl::handler& cgh)
2123 {
2124 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2125 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2126 auto access_mask = mask.template get_access<sycl::access::mode::read>(cgh);
2127 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
2128
2129
2130 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2131 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2132
2133 auto blocks_needed = (nrows + ellpack_size - 1) / ellpack_size;
2134 auto blocks_target = std::max(blocks_needed, num_groups * 4UL);
2135 auto total_threads = blocks_target * ellpack_size;
2136
2137 sycl::local_accessor<ValueType, 1> lds_x{pack_size, cgh};
2138 sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{pack_size}};
2139 //cgh.parallel_for<class compute_lmult>(sycl::range<1>{total_threads},[=] (sycl::item<1> item_id)
2140 cgh.parallel_for<class compute_lmult>(r,
2141 [=](sycl::nd_item<1> item_id)
2142 {
2143 //auto id = item_id.get_id(0);
2144 auto local_id = item_id.get_local_id(0);
2145 //auto block_id = item_id.get_group(0) ;
2146 auto global_id = item_id.get_global_id(0);
2147
2148 //for (auto i = id; i < nrows; i += item_id.get_range()[0])
2149 for (auto i = global_id; i < nrows; i += item_id.get_global_range()[0])
2150 {
2151 auto block_id = i/pack_size ;
2152 //auto local_id = i%ellpack_size ;
2153
2154 int begin = access_block_row_offset[block_id] ;
2155 int end = access_block_row_offset[block_id+1] ;
2156
2157 ValueType value = access_y[i] ;
2158 for(int k=begin;k<end;++k)
2159 {
2160 //auto k = block_row_offset+j*ellpack_size+local_id ;
2161 const int col = access_cols[k * pack_size + local_id];
2162 if(col>=0)
2163 lds_x[local_id] = access_x[col];
2164 item_id.barrier(sycl::access::fence_space::local_space);
2165 if(access_mask[k * pack_size + local_id])
2166 value += alpha * access_values[k * pack_size + local_id] * lds_x[local_id] ;
2167 item_id.barrier(sycl::access::fence_space::local_space);
2168 }
2169 access_y[i] = value ;
2170 }
2171 });
2172 });
2173 }
2174 else
2175 {
2176 const bool done = dispatchN<2, 20>(
2177 this->m_N,
2178 [&](auto N)
2179 {
2180 addLMultN<decltype(N)::value>(alpha,x, y, queue);
2181 });
2182
2183 if (!done)
2184 {
2185 auto device = queue.get_device();
2186
2187 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2188 // getting the maximum work group size per thread
2189 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2190 auto total_threads = num_groups * ellpack_size;
2191
2192 int N = this->m_N;
2193 int NxN = this->m_NxN;
2194 std::size_t pack_size = ellpack_size;
2195 auto nrows = m_profile->getNRows();
2196 auto nnz = m_profile->getNnz();
2197
2198 auto internal_profile = m_profile->internal();
2199 auto& kcol = internal_profile->getKCol();
2200 auto& block_row_offset = internal_profile->getBlockRowOffset();
2201 auto& block_cols = internal_profile->getBlockCols();
2202
2203 auto& mask = internal_profile->getLowerMask();
2204
2205 // clang-format off
2206 queue.submit(
2207 [&](sycl::handler& cgh)
2208 {
2209 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2210 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2211 auto access_mask = mask.template get_access<sycl::access::mode::read>(cgh);
2212 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
2213
2214 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2215 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2216 sycl::range<1> range{total_threads} ;
2217 auto tile = Tile(N) ;
2218 cgh.parallel_for<class compute_block_lmult>(
2219 sycl::range<1>{total_threads},
2220 [=] (sycl::item<1> item_id)
2221 {
2222 auto id = item_id.get_id(0);
2223 //auto local_id = item_id.get_local_id(0);
2224 //auto block_id = item_id.get_group(0) ;
2225 //auto global_id = item_id.get_global_id(0);
2226
2227 for (auto i = id; i < nrows; i += item_id.get_range()[0])
2228 {
2229 auto block_id = i/ellpack_size ;
2230 auto local_id = i%ellpack_size ;
2231
2232 int block_row_offset = access_block_row_offset[block_id] ;
2233 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2234
2235 for(int ieq=0;ieq<N;++ieq)
2236 {
2237 ValueType value = 0. ;
2238 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2239 {
2240 // value += access_values[k]* access_x[access_cols[k]] ;
2241 value += tile.mult(ieq,
2242 local_id,
2243 k,
2244 access_cols,
2245 access_mask,
2246 access_values,
2247 access_x) ;
2248 }
2249 access_y[i*N+ieq] += alpha*value;
2250 }
2251 }
2252 });
2253 });
2254 }
2255#ifdef OLD
2256 switch(this->m_N)
2257 {
2258 case 2:
2259 addLMultN<2>(alpha,x,y,queue);
2260 break;
2261 case 3:
2262 addLMultN<3>(alpha,x,y,queue);
2263 break;
2264 case 4:
2265 addLMultN<4>(alpha,x,y,queue);
2266 break;
2267 default:
2268 {
2269 auto device = queue.get_device();
2270
2271 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2272 // getting the maximum work group size per thread
2273 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2274 auto total_threads = num_groups * ellpack_size;
2275
2276 int N = this->m_N;
2277 int NxN = this->m_NxN;
2278 std::size_t pack_size = ellpack_size;
2279 auto nrows = m_profile->getNRows();
2280 auto nnz = m_profile->getNnz();
2281
2282 auto internal_profile = m_profile->internal();
2283 auto& kcol = internal_profile->getKCol();
2284 auto& block_row_offset = internal_profile->getBlockRowOffset();
2285 auto& block_cols = internal_profile->getBlockCols();
2286
2287 auto& mask = internal_profile->getLowerMask();
2288
2289 // clang-format off
2290 queue.submit(
2291 [&](sycl::handler& cgh)
2292 {
2293 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2294 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2295 auto access_mask = mask.template get_access<sycl::access::mode::read>(cgh);
2296 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
2297
2298 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2299 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2300 sycl::range<1> range{total_threads} ;
2301 auto tile = Tile(N) ;
2302 cgh.parallel_for<class compute_block_lmult>(
2303 sycl::range<1>{total_threads},
2304 [=] (sycl::item<1> item_id)
2305 {
2306 auto id = item_id.get_id(0);
2307 //auto local_id = item_id.get_local_id(0);
2308 //auto block_id = item_id.get_group(0) ;
2309 //auto global_id = item_id.get_global_id(0);
2310
2311 for (auto i = id; i < nrows; i += item_id.get_range()[0])
2312 {
2313 auto block_id = i/ellpack_size ;
2314 auto local_id = i%ellpack_size ;
2315
2316 int block_row_offset = access_block_row_offset[block_id] ;
2317 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2318
2319 for(int ieq=0;ieq<N;++ieq)
2320 {
2321 ValueType value = 0. ;
2322 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2323 {
2324 // value += access_values[k]* access_x[access_cols[k]] ;
2325 value += tile.mult(ieq,
2326 local_id,
2327 k,
2328 access_cols,
2329 access_mask,
2330 access_values,
2331 access_x) ;
2332 }
2333 access_y[i*N+ieq] += alpha*value;
2334 }
2335 }
2336 });
2337 });
2338 }
2339 break;
2340 }
2341#endif
2342 }
2343
2344#ifdef PRINT_DEBUG_INFO
2345 {
2346 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(m_values);
2347 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(block_row_offset);
2348 sycl::host_accessor<uint8_t, 1, sycl::access::mode::read> mask_acc(mask) ;
2349 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> x_acc(x);
2350 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> y_acc(y);
2351 for(int irow=0;irow<nrows;++irow)
2352 {
2353 std::cout<<"XK["<<irow<<"]=\n";
2354 for(int i=0;i<N;++i)
2355 std::cout<<x_acc[irow*N+i]<<"\n";
2356 }
2357 for(int irow=0;irow<nrows;++irow)
2358 {
2359 auto ib=irow/ellpack_size ;
2360 auto il=irow%ellpack_size ;
2361 std::cout<<"LSOLVE MAT["<<irow<<"]:"<<std::endl;
2362 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
2363 {
2364 if(mask_acc[k*ellpack_size+il])
2365 {
2366 for(int i=0;i<N;++i)
2367 {
2368 for(int j=0;j<N;++j)
2369 std::cout<<h_acc[(k*NxN+i*N+j)*ellpack_size+il]<<" ";
2370 std::cout<<std::endl;
2371 }
2372 }
2373 }
2374 std::cout<<"Y ="<<std::endl;
2375 for(int i=0;i<N;++i)
2376 std::cout<<y_acc[irow*N+i]<<"\n";
2377 }
2378 }
2379#endif
2380 }
2381
2382 template <typename ValueT, int EllPackSize>
2383 void MatrixInternal<ValueT, EllPackSize>::addLMult(ValueType alpha, ValueBufferType& x, ValueBufferType& y) const
2384 {
2385 this->addLMult(alpha, x, y, SYCLEnv::instance()->internal()->queue());
2386 }
2387
2388 template <typename ValueT, int EllPackSize>
2389 void MatrixInternal<ValueT, EllPackSize>::addExtLMult(ValueType alpha,
2390 ValueBufferType& x,
2391 ValueBufferType& y,
2392 sycl::queue& queue) const
2393 {
2394 auto device = queue.get_device();
2395
2396 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2397 // getting the maximum work group size per thread
2398 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2399 // building the best number of global thread
2400 auto total_threads = num_groups * ellpack_size;
2401
2402 //int N = this->m_N;
2403 //int NxN = this->m_NxN;
2404 std::size_t pack_size = ellpack_size;
2405
2406 // clang-format off
2407 auto nrows = m_profile->getNRows();
2408 auto nnz = m_profile->getNnz();
2409
2410 auto interface_nrow = m_ext_profile->getNRows();
2411 auto first_upper_ghost = m_first_upper_ghost_index ;
2412 auto ext_profile = m_ext_profile->internal();
2413 auto& kcol = ext_profile->getKCol();
2414 auto& block_row_offset = ext_profile->getBlockRowOffset();
2415 auto& block_cols = ext_profile->getBlockCols();
2416
2417 if(this->m_N==1)
2418 {
2419 // clang-format off
2420 queue.submit([&](sycl::handler& cgh)
2421 {
2422 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2423 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2424 auto access_values = m_ext_values->template get_access<sycl::access::mode::read>(cgh);
2425
2426 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
2427
2428 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2429 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2430
2431 cgh.parallel_for<class compute_ext_lmult>(
2432 sycl::range<1>{total_threads},
2433 [=] (sycl::item<1> item_id)
2434 {
2435 auto id = item_id.get_id(0);
2436
2437 for (auto i = id; i < interface_nrow; i += item_id.get_range()[0])
2438 {
2439 auto block_id = i / pack_size ;
2440 auto local_id = i % pack_size ;
2441
2442 int block_row_offset = access_block_row_offset[block_id] * pack_size ;
2443 auto block_row_size = access_block_row_offset[block_id+1] - access_block_row_offset[block_id] ;
2444
2445 ValueType value = 0. ;
2446 for(int j=0;j<block_row_size;++j)
2447 {
2448 auto k = block_row_offset+j*ellpack_size+local_id ;
2449 if(access_cols[k]<first_upper_ghost)
2450 value += access_values[k]* access_x[access_cols[k]] ;
2451 }
2452 access_y[access_row_ids[i]] += alpha * value ;
2453 }
2454 });
2455 });
2456 // clang-format on
2457 }
2458 else
2459 {
2460 const bool done = dispatchN<2, 20>(
2461 this->m_N,
2462 [&](auto N)
2463 {
2464 addExtLMultN<decltype(N)::value>(alpha, x, y, queue);
2465 });
2466 if (!done)
2467 {
2468 // clang-format off
2469
2470 int N = this->m_N;
2471 queue.submit([&](sycl::handler& cgh)
2472 {
2473 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2474 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2475 auto access_values = m_ext_values->template get_access<sycl::access::mode::read>(cgh);
2476
2477 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
2478
2479 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2480 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2481
2482 auto tile = Tile(N) ;
2483 cgh.parallel_for<class compute_ext_lmult>(
2484 sycl::range<1>{total_threads},
2485 [=] (sycl::item<1> item_id)
2486 {
2487 auto id = item_id.get_id(0);
2488
2489 for (auto i = id; i < interface_nrow; i += item_id.get_range()[0])
2490 {
2491 std::size_t block_id = i / pack_size ;
2492 std::size_t local_id = i % pack_size ;
2493
2494 std::size_t block_row_offset = access_block_row_offset[block_id] * pack_size ;
2495 std::size_t block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2496
2497 for(int ieq=0;ieq<N;++ieq)
2498 {
2499 ValueType value = 0. ;
2500 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2501 {
2502 // value += access_values[k]* access_x[access_cols[k]] ;
2503 if(access_cols[k]<first_upper_ghost)
2504 value += tile.mult(ieq,
2505 local_id,
2506 k,
2507 access_cols,
2508 access_values,
2509 access_x) ;
2510 }
2511 access_y[access_row_ids[i]*N+ieq] += alpha * value;
2512 }
2513 }
2514 });
2515 });
2516 // clang-format on
2517 }
2518 }
2519 }
2520
2521 template <typename ValueT, int EllPackSize>
2522 void MatrixInternal<ValueT, EllPackSize>::addExtLMult(ValueType alpha,
2523 ValueBufferType& x,
2524 ValueBufferType& y) const
2525 {
2526 this->addExtLMult(alpha, x, y, SYCLEnv::instance()->internal()->queue());
2527 }
2528
2529 template <typename ValueT, int EllPackSize>
2530 void MatrixInternal<ValueT, EllPackSize>::addUMult(ValueType alpha,
2531 ValueBufferType& x,
2532 ValueBufferType& y,
2533 sycl::queue& queue) const
2534 {
2535 if(this->m_N==1)
2536 {
2537 auto device = queue.get_device();
2538
2539 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2540 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2541
2542 // clang-format off
2543 int N = this->m_N;
2544 int NxN = this->m_NxN;
2545 std::size_t pack_size = ellpack_size;
2546 auto nrows = m_profile->getNRows() ;
2547 auto nnz = m_profile->getNnz() ;
2548
2549 auto blocks_needed = (nrows + ellpack_size - 1) / ellpack_size;
2550 auto blocks_target = std::max(blocks_needed, num_groups * 4UL);
2551 auto total_threads = blocks_target * ellpack_size;
2552
2553 auto internal_profile = m_profile->internal() ;
2554 auto& kcol = internal_profile->getKCol() ;
2555 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
2556 auto& block_cols = internal_profile->getBlockCols() ;
2557 auto& mask = internal_profile->getUpperMask() ;
2558
2559 // COMPUTE VALUES
2560 queue.submit(
2561 [&](sycl::handler& cgh)
2562 {
2563 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2564 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2565 auto access_mask = mask.template get_access<sycl::access::mode::read>(cgh);
2566 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
2567
2568
2569 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2570 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2571
2572 sycl::local_accessor<ValueType, 1> lds_x{pack_size, cgh};
2573 sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{pack_size}};
2574 //cgh.parallel_for<class compute_umult>(sycl::range<1>{total_threads},[=] (sycl::item<1> item_id)
2575 cgh.parallel_for<class compute_umult>(r,
2576 [=](sycl::nd_item<1> item_id)
2577 {
2578 //auto id = item_id.get_id(0);
2579 auto local_id = item_id.get_local_id(0);
2580 //auto block_id = item_id.get_group(0) ;
2581 auto global_id = item_id.get_global_id(0);
2582
2583 for (auto i = global_id; i < nrows; i += item_id.get_global_range()[0])
2584 {
2585 auto block_id = i/pack_size ;
2586 //auto local_id = i%ellpack_size ;
2587
2588 auto begin = access_block_row_offset[block_id] ;
2589 auto end = access_block_row_offset[block_id+1] ;
2590 //auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2591
2592 ValueType value = access_y[i] ;
2593 for(int k=begin;k<end;++k)
2594 {
2595 //auto k = block_row_offset+j*ellpack_size+local_id ;
2596 //value += alpha * access_mask[k] * access_values[k]* access_x[access_cols[k]] ;
2597 const int col = access_cols[k * pack_size + local_id];
2598 if(col>=0)
2599 lds_x[local_id] = access_x[col];
2600 item_id.barrier(sycl::access::fence_space::local_space);
2601 if(access_mask[k * pack_size + local_id])
2602 value += alpha * access_values[k * pack_size + local_id] * lds_x[local_id] ;
2603 item_id.barrier(sycl::access::fence_space::local_space);
2604 }
2605 access_y[i] = value ;
2606 }
2607 });
2608 });
2609 }
2610 else
2611 {
2612 const bool done = dispatchN<2, 20>(
2613 this->m_N,
2614 [&](auto N)
2615 {
2616 addUMultN<decltype(N)::value>(alpha, x, y, queue);
2617 });
2618
2619 if (!done)
2620 {
2621 auto device = queue.get_device();
2622
2623 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2624 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2625 auto total_threads = num_groups * ellpack_size;
2626
2627 // clang-format off
2628 int N = this->m_N;
2629 int NxN = this->m_NxN;
2630 std::size_t pack_size = ellpack_size;
2631 auto nrows = m_profile->getNRows() ;
2632 auto nnz = m_profile->getNnz() ;
2633
2634 auto internal_profile = m_profile->internal() ;
2635 auto& kcol = internal_profile->getKCol() ;
2636 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
2637 auto& block_cols = internal_profile->getBlockCols() ;
2638 auto& mask = internal_profile->getUpperMask() ;
2639
2640 // COMPUTE VALUES
2641 queue.submit(
2642 [&](sycl::handler& cgh)
2643 {
2644 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2645 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2646 auto access_mask = mask.template get_access<sycl::access::mode::read>(cgh);
2647 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
2648
2649
2650 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2651 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2652
2653 auto tile = Tile(N) ;
2654 cgh.parallel_for<class compute_block_umult>(
2655 sycl::range<1>{total_threads},
2656 [=] (sycl::item<1> item_id)
2657 {
2658 auto id = item_id.get_id(0);
2659 //auto local_id = item_id.get_local_id(0);
2660 //auto block_id = item_id.get_group(0) ;
2661 //auto global_id = item_id.get_global_id(0);
2662
2663 for (auto i = id; i < nrows; i += item_id.get_range()[0])
2664 {
2665 auto block_id = i/ellpack_size ;
2666 auto local_id = i%ellpack_size ;
2667
2668 std::size_t block_row_offset = access_block_row_offset[block_id] ;
2669 std::size_t block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2670 for(int ieq=0;ieq<N;++ieq)
2671 {
2672 ValueType value = 0. ;
2673 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2674 {
2675 // value += access_values[k]* access_x[access_cols[k]] ;
2676 value += tile.mult(ieq,
2677 local_id,
2678 k,
2679 access_cols,
2680 access_mask,
2681 access_values,
2682 access_x) ;
2683 }
2684 access_y[i*N+ieq] += alpha*value;
2685 }
2686 }
2687 });
2688 });
2689 }
2690#ifdef OLD
2691 switch(this->m_N)
2692 {
2693 case 2:
2694 addUMultN<2>(alpha,x,y,queue);
2695 break;
2696 case 3:
2697 addUMultN<3>(alpha,x,y,queue);
2698 break;
2699 case 4:
2700 addUMultN<4>(alpha,x,y,queue);
2701 break;
2702 default:
2703 {
2704 auto device = queue.get_device();
2705
2706 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2707 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2708 auto total_threads = num_groups * ellpack_size;
2709
2710 // clang-format off
2711 int N = this->m_N;
2712 int NxN = this->m_NxN;
2713 std::size_t pack_size = ellpack_size;
2714 auto nrows = m_profile->getNRows() ;
2715 auto nnz = m_profile->getNnz() ;
2716
2717 auto internal_profile = m_profile->internal() ;
2718 auto& kcol = internal_profile->getKCol() ;
2719 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
2720 auto& block_cols = internal_profile->getBlockCols() ;
2721 auto& mask = internal_profile->getUpperMask() ;
2722
2723 // COMPUTE VALUES
2724 queue.submit(
2725 [&](sycl::handler& cgh)
2726 {
2727 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2728 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2729 auto access_mask = mask.template get_access<sycl::access::mode::read>(cgh);
2730 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
2731
2732
2733 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2734 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2735
2736 auto tile = Tile(N) ;
2737 cgh.parallel_for<class compute_block_umult>(
2738 sycl::range<1>{total_threads},
2739 [=] (sycl::item<1> item_id)
2740 {
2741 auto id = item_id.get_id(0);
2742 //auto local_id = item_id.get_local_id(0);
2743 //auto block_id = item_id.get_group(0) ;
2744 //auto global_id = item_id.get_global_id(0);
2745
2746 for (auto i = id; i < nrows; i += item_id.get_range()[0])
2747 {
2748 auto block_id = i/ellpack_size ;
2749 auto local_id = i%ellpack_size ;
2750
2751 int block_row_offset = access_block_row_offset[block_id] ;
2752 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2753 for(int ieq=0;ieq<N;++ieq)
2754 {
2755 ValueType value = 0. ;
2756 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2757 {
2758 // value += access_values[k]* access_x[access_cols[k]] ;
2759 value += tile.mult(ieq,
2760 local_id,
2761 k,
2762 access_cols,
2763 access_mask,
2764 access_values,
2765 access_x) ;
2766 }
2767 access_y[i*N+ieq] += alpha*value;
2768 }
2769 }
2770 });
2771 });
2772 }
2773 break;
2774 }
2775#endif
2776 }
2777 // clang-format on
2778
2779#ifdef PRINT_DEBUG_INFO
2780 {
2781 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(m_values);
2782 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(block_row_offset);
2783 sycl::host_accessor<uint8_t, 1, sycl::access::mode::read> mask_acc(mask) ;
2784 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> x_acc(x);
2785 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> y_acc(y);
2786 for(int irow=0;irow<nrows;++irow)
2787 {
2788 std::cout<<"XK["<<irow<<"]=\n";
2789 for(int i=0;i<N;++i)
2790 std::cout<<x_acc[irow*N+i]<<"\n";
2791 }
2792 for(int irow=0;irow<nrows;++irow)
2793 {
2794 auto ib=irow/ellpack_size ;
2795 auto il=irow%ellpack_size ;
2796 std::cout<<"USOLVE MAT["<<irow<<"]:"<<std::endl;
2797 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
2798 {
2799 if(mask_acc[k*ellpack_size+il])
2800 {
2801 for(int i=0;i<N;++i)
2802 {
2803 for(int j=0;j<N;++j)
2804 std::cout<<h_acc[(k*NxN+i*N+j)*ellpack_size+il]<<" ";
2805 std::cout<<std::endl;
2806 }
2807 }
2808 }
2809 std::cout<<"Y ="<<std::endl;
2810 for(int i=0;i<N;++i)
2811 std::cout<<y_acc[irow*N+i]<<"\n";
2812 }
2813 }
2814#endif
2815 }
2816
2817 template <typename ValueT, int EllPackSize>
2818 void MatrixInternal<ValueT, EllPackSize>::addUMult(ValueType alpha, ValueBufferType& x, ValueBufferType& y) const
2819 {
2820 this->addUMult(alpha, x, y, SYCLEnv::instance()->internal()->queue());
2821 }
2822
2823 template <typename ValueT, int EllPackSize>
2824 void MatrixInternal<ValueT, EllPackSize>::addExtUMult(ValueType alpha,
2825 ValueBufferType& x,
2826 ValueBufferType& y,
2827 sycl::queue& queue) const
2828 {
2829 auto device = queue.get_device();
2830
2831 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2832 // getting the maximum work group size per thread
2833 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2834 // building the best number of global thread
2835 auto total_threads = num_groups * ellpack_size;
2836
2837 //int N = this->m_N;
2838 //int NxN = this->m_NxN;
2839 std::size_t pack_size = ellpack_size;
2840
2841 // clang-format off
2842 auto nrows = m_profile->getNRows();
2843 auto nnz = m_profile->getNnz();
2844
2845 auto interface_nrow = m_ext_profile->getNRows();
2846 auto first_upper_ghost = m_first_upper_ghost_index ;
2847 auto ext_profile = m_ext_profile->internal();
2848 auto& kcol = ext_profile->getKCol();
2849 auto& block_row_offset = ext_profile->getBlockRowOffset();
2850 auto& block_cols = ext_profile->getBlockCols();
2851 // clang-format on
2852 if(this->m_N==1)
2853 {
2854 // clang-format off
2855 queue.submit([&](sycl::handler& cgh)
2856 {
2857 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2858 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2859 auto access_values = m_ext_values->template get_access<sycl::access::mode::read>(cgh);
2860
2861 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
2862
2863 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2864 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2865
2866 cgh.parallel_for<class compute_ext_umult>(
2867 sycl::range<1>{total_threads},
2868 [=] (sycl::item<1> item_id)
2869 {
2870 auto id = item_id.get_id(0);
2871
2872 for (auto i = id; i < interface_nrow; i += item_id.get_range()[0])
2873 {
2874 std::size_t block_id = i / pack_size ;
2875 std::size_t local_id = i % pack_size ;
2876
2877 std::size_t block_row_offset = access_block_row_offset[block_id] * pack_size ;
2878 std::size_t block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
2879
2880 ValueType value = 0. ;
2881 for(int j=0;j<block_row_size;++j)
2882 {
2883 auto k = block_row_offset+j*pack_size+local_id ;
2884 if(access_cols[k]>=first_upper_ghost)
2885 value += access_values[k]* access_x[access_cols[k]] ;
2886 }
2887 access_y[access_row_ids[i]] += alpha * value ;
2888 }
2889 });
2890 });
2891 // clang-format on
2892 }
2893 else
2894 {
2895 const bool done = dispatchN<2, 20>(
2896 this->m_N,
2897 [&](auto N)
2898 {
2899 addExtUMultN<decltype(N)::value>(alpha, x, y, queue);
2900 });
2901 if (!done)
2902 {
2903 // clang-format off
2904
2905 int N = this->m_N;
2906 queue.submit([&](sycl::handler& cgh)
2907 {
2908 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
2909 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
2910 auto access_values = m_ext_values->template get_access<sycl::access::mode::read>(cgh);
2911
2912 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
2913
2914 auto access_x = x.template get_access<sycl::access::mode::read>(cgh);
2915 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
2916
2917 auto tile = Tile(N) ;
2918 cgh.parallel_for<class compute_ext_umult>(
2919 sycl::range<1>{total_threads},
2920 [=] (sycl::item<1> item_id)
2921 {
2922 auto id = item_id.get_id(0);
2923
2924 for (auto i = id; i < interface_nrow; i += item_id.get_range()[0])
2925 {
2926 std::size_t block_id = i / pack_size ;
2927 std::size_t local_id = i % pack_size ;
2928
2929 std::size_t block_row_offset = access_block_row_offset[block_id] * pack_size ;
2930 std::size_t block_row_size = access_block_row_offset[block_id+1] -
2931 access_block_row_offset[block_id] ;
2932
2933 for(int ieq=0;ieq<N;++ieq)
2934 {
2935 ValueType value = 0. ;
2936 for(std::size_t k=block_row_offset;k<block_row_offset+block_row_size;++k)
2937 {
2938 // value += access_values[k]* access_x[access_cols[k]] ;
2939 if(access_cols[k]>=first_upper_ghost)
2940 value += tile.mult(ieq,
2941 local_id,
2942 k,
2943 access_cols,
2944 access_values,
2945 access_x) ;
2946 }
2947 access_y[access_row_ids[i]*N+ieq] += alpha * value;
2948 }
2949 }
2950 });
2951 });
2952 // clang-format on
2953 }
2954 }
2955 }
2956
2957 template <typename ValueT, int EllPackSize>
2958 void MatrixInternal<ValueT, EllPackSize>::addExtUMult(ValueType alpha, ValueBufferType& x,
2959 ValueBufferType& y) const
2960 {
2961 this->addExtUMult(alpha, x, y, SYCLEnv::instance()->internal()->queue());
2962 }
2963
2964 template <typename ValueT, int EllPackSize>
2965 void MatrixInternal<ValueT, EllPackSize>::multDiag(ValueBufferType& x, ValueBufferType& y, sycl::queue& queue) const
2966 {
2967 }
2968
2969 template <typename ValueT, int EllPackSize>
2970 void MatrixInternal<ValueT, EllPackSize>::multDiag(ValueBufferType& x, ValueBufferType& y) const
2971 {
2972 this->multDiag(x, y, SYCLEnv::instance()->internal()->queue());
2973 }
2974
2975 template <typename ValueT, int EllPackSize>
2976 void MatrixInternal<ValueT, EllPackSize>::multDiag(ValueBufferType& y, sycl::queue& queue) const
2977 {
2978 throw NotImplementedException(
2979 A_FUNCINFO, "MatrixInternal<ValueT, EllPackSize>::multDiag not implemented");
2980 }
2981
2982 template <typename ValueT, int EllPackSize>
2983 void MatrixInternal<ValueT, EllPackSize>::multDiag(ValueBufferType& y) const
2984 {
2985 this->multDiag(y, SYCLEnv::instance()->internal()->queue());
2986 }
2987
2988 template <typename ValueT, int EllPackSize>
2989 void MatrixInternal<ValueT, EllPackSize>::computeDiag(ValueBufferType& y, sycl::queue& queue) const
2990 {
2991
2992 auto device = queue.get_device();
2993
2994 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
2995 // getting the maximum work group size per thread
2996 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
2997 // building the best number of global thread
2998 auto total_threads = num_groups * ellpack_size;
2999
3000 // clang-format off
3001 int N = this->m_N;
3002 int NxN = this->m_NxN;
3003 int pack_size = ellpack_size ;
3004 auto nrows = m_profile->getNRows() ;
3005 auto nnz = m_profile->getNnz() ;
3006
3007 auto internal_profile = m_profile->internal() ;
3008 auto& kcol = internal_profile->getKCol() ;
3009 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
3010 auto& block_cols = internal_profile->getBlockCols() ;
3011
3012 // COMPUTE VALUES
3013 queue.submit([&](sycl::handler& cgh)
3014 {
3015 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3016 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3017 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
3018 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
3019 if(N==1)
3020 {
3021 cgh.parallel_for<class compute_diag>(
3022 sycl::range<1>{total_threads},
3023 [=] (sycl::item<1> item_id)
3024 {
3025 auto id = item_id.get_id(0);
3026 //auto local_id = item_id.get_local_id(0);
3027 //auto block_id = item_id.get_group(0) ;
3028 //auto global_id = item_id.get_global_id(0);
3029
3030 for (auto i = id; i < nrows; i += item_id.get_range()[0])
3031 {
3032 auto block_id = i/pack_size ;
3033 auto local_id = i%pack_size ;
3034
3035 auto begin = access_block_row_offset[block_id] ;
3036 auto end = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
3037 for(int k=begin;k<end;++k)
3038 {
3039 auto kcol = k*pack_size+local_id ;
3040 if((access_cols[kcol])==int(i))
3041 access_y[i] = access_values[kcol] ;
3042 }
3043 }
3044 });
3045 }
3046 else
3047 {
3048 cgh.parallel_for<class compute_diagN>(
3049 sycl::range<1>{total_threads},
3050 [=] (sycl::item<1> item_id)
3051 {
3052 auto id = item_id.get_id(0);
3053 //auto local_id = item_id.get_local_id(0);
3054 //auto block_id = item_id.get_group(0) ;
3055 //auto global_id = item_id.get_global_id(0);
3056
3057 for (std::size_t i = id; i < nrows; i += item_id.get_range()[0])
3058 {
3059 auto block_id = i/pack_size ;
3060 auto local_id = i%pack_size ;
3061
3062 int block_row_offset = access_block_row_offset[block_id] ;
3063 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
3064 for(int k=0;k<block_row_size;++k)
3065 {
3066 auto kcol = (block_row_offset+k)*pack_size+local_id ;
3067 std::size_t col = access_cols[kcol] ;
3068 if(col==i)
3069 {
3070 for(int ieq=0;ieq<N;++ieq)
3071 {
3072 auto kval = (block_row_offset+k)*NxN + ieq*N + ieq ;
3073 access_y[i*N+ieq] = access_values[kval*pack_size+local_id] ;
3074 }
3075 }
3076 }
3077 }
3078 });
3079 }
3080 });
3081 /*
3082 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(y);
3083 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> mat_acc(m_values);
3084 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(block_row_offset);
3085 sycl::host_accessor<int, 1, sycl::access::mode::read> cols_acc(block_cols);
3086 for(int irow=0;irow<nrows;++irow)
3087 {
3088 auto ib=irow/ellpack_size ;
3089 auto il=irow%ellpack_size ;
3090 std::cout<<"MAT["<<irow<<","<<ib<<"]:";
3091 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
3092 {
3093 std::cout<<"("<<cols_acc[k*ellpack_size+il]<<",";
3094 for(int i=0;i<N;++i)
3095 for(int j=0;j<N;++j)
3096 std::cout<<mat_acc[(k*NxN+i*N+j)*ellpack_size+il]<<",";
3097 std::cout<<")"<<std::endl;
3098 }
3099 }
3100 for(int irow=0;irow<nrows;++irow)
3101 {
3102 auto ib=irow/ellpack_size ;
3103 auto il=irow%ellpack_size ;
3104 std::cout<<"DIAG["<<irow<<","<<il<<"]:";
3105 for(int i=0;i<N;++i)
3106 std::cout<<h_acc[il*N+i]<<",";
3107 std::cout<<std::endl;
3108 }*/
3109 // clang-format on
3110 }
3111
3112 template <typename ValueT, int EllPackSize>
3113 void MatrixInternal<ValueT, EllPackSize>::computeBlockDiag(ValueBufferType& y, sycl::queue& queue) const
3114 {
3115
3116 auto device = queue.get_device();
3117
3118 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
3119 // getting the maximum work group size per thread
3120 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
3121 // building the best number of global thread
3122 auto total_threads = num_groups * ellpack_size;
3123
3124 // clang-format off
3125 int N = this->m_N;
3126 int NxN = this->m_NxN;
3127 int pack_size = ellpack_size ;
3128 auto nrows = m_profile->getNRows() ;
3129 auto nnz = m_profile->getNnz() ;
3130
3131 assert(y.size()>=NxN*nrows) ;
3132
3133 auto internal_profile = m_profile->internal() ;
3134 auto& kcol = internal_profile->getKCol() ;
3135 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
3136 auto& block_cols = internal_profile->getBlockCols() ;
3137 {
3138 ValueBufferType buffer (sycl::range<1>(nrows*NxN)) ;
3139 // COMPUTE VALUES
3140 queue.submit([&](sycl::handler& cgh)
3141 {
3142 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3143 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3144 auto matrix = m_values.template get_access<sycl::access::mode::read>(cgh);
3145 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
3146 auto A = buffer.template get_access<sycl::access::mode::read_write>(cgh);
3147
3148 auto tile = Tile(N) ;
3149 cgh.parallel_for<class compute_block_diag>(
3150 sycl::range<1>{total_threads},
3151 [=] (sycl::item<1> item_id)
3152 {
3153 auto id = item_id.get_id(0);
3154 //auto local_id = item_id.get_local_id(0);
3155 //auto block_id = item_id.get_group(0) ;
3156 //auto global_id = item_id.get_global_id(0);
3157
3158 for (std::size_t global_id = id; global_id < nrows; global_id += item_id.get_range()[0])
3159 {
3160 auto block_id = global_id/pack_size ;
3161 auto local_id = global_id%pack_size ;
3162
3163 int block_row_offset = access_block_row_offset[block_id] ;
3164 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
3165 for(int k=0;k<block_row_size;++k)
3166 {
3167 auto kcol = (block_row_offset+k)*ellpack_size+local_id ;
3168 std::size_t col = access_cols[kcol] ;
3169 if(col==global_id)
3170 {
3171 // Copy Diag Matrix in A
3172 for(int i=0;i<N;++i)
3173 for(int j=0;j<N;++j)
3174 A[tile._ij(local_id,i,j)] = matrix[tile._ijk(k,i,j)+local_id] ;
3175 }
3176 }
3177 }
3178 });
3179 });
3180 }
3181 // clang-format on
3182 }
3183
3184 template <typename ValueT, int EllPackSize>
3185 void MatrixInternal<ValueT, EllPackSize>::computeDiag(ValueBufferType& y) const
3186 {
3187 this->computeDiag(y, SYCLEnv::instance()->internal()->queue());
3188 }
3189
3190 template <typename ValueT, int EllPackSize>
3191 void MatrixInternal<ValueT, EllPackSize>::computeBlockDiag(ValueBufferType& y) const
3192 {
3193 this->computeBlockDiag(y, SYCLEnv::instance()->internal()->queue());
3194 }
3195
3196
3197 template <typename ValueT, int EllPackSize>
3198 void MatrixInternal<ValueT, EllPackSize>::multInvDiag(ValueBufferType& y, sycl::queue& queue) const
3199 {
3200 throw NotImplementedException(
3201 A_FUNCINFO, "MatrixInternal<ValueT, EllPackSize>::multInvDiag not implemented");
3202 }
3203
3204 template <typename ValueT, int EllPackSize>
3205 void MatrixInternal<ValueT, EllPackSize>::multInvDiag(ValueBufferType& y) const
3206 {
3207 this->multInvDiag(y, SYCLEnv::instance()->internal()->queue());
3208 }
3209
3210 template <typename ValueT, int EllPackSize>
3211 void MatrixInternal<ValueT, EllPackSize>::computeInvDiag(ValueBufferType& y, sycl::queue& queue) const
3212 {
3213
3214 auto device = queue.get_device();
3215
3216 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
3217 // getting the maximum work group size per thread
3218 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
3219
3220 // clang-format off
3221 int N = this->m_N;
3222 int NxN = this->m_NxN;
3223 std::size_t pack_size = ellpack_size ;
3224 auto nrows = m_profile->getNRows() ;
3225 auto nnz = m_profile->getNnz() ;
3226
3227 auto internal_profile = m_profile->internal() ;
3228 auto& kcol = internal_profile->getKCol() ;
3229 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
3230 auto& block_cols = internal_profile->getBlockCols() ;
3231
3232 auto blocks_needed = (nrows + ellpack_size - 1) / ellpack_size;
3233 auto blocks_target = std::max(blocks_needed, num_groups * 4UL);
3234 auto total_threads = blocks_target * ellpack_size;
3235
3236 // COMPUTE VALUES
3237 queue.submit([&](sycl::handler& cgh)
3238 {
3239 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3240 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3241 auto access_values = m_values.template get_access<sycl::access::mode::read>(cgh);
3242 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
3243 if(N==1)
3244 {
3245 //sycl::local_accessor<ValueType, 1> lds_x{pack_size, cgh};
3246 sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{pack_size}};
3247
3248 cgh.parallel_for<class compute_inv_diag>(r,
3249 [=](sycl::nd_item<1> item_id)
3250 {
3251 auto local_id = item_id.get_local_id(0);
3252 auto global_id = item_id.get_global_id(0);
3253 for (auto i = global_id; i < nrows; i += item_id.get_global_range()[0])
3254 {
3255 auto block_id = i/pack_size ;
3256 //auto local_id = i%ellpack_size ;
3257
3258 int begin = access_block_row_offset[block_id];
3259 int end = access_block_row_offset[block_id+1];
3260 for(int k=begin;k<end;++k)
3261 {
3262 auto kcol = k*pack_size+local_id ;
3263 if((access_cols[kcol])==int(i) && (access_values[kcol]!=0) )
3264 access_y[i] = 1./access_values[kcol] ;
3265 }
3266 }
3267 });
3268
3269 /*
3270 cgh.parallel_for<class compute_inv_diag>(
3271 sycl::range<1>{total_threads},
3272 [=] (sycl::item<1> item_id)
3273 {
3274 auto id = item_id.get_id(0);
3275 //auto local_id = item_id.get_local_id(0);
3276 //auto block_id = item_id.get_group(0) ;
3277 //auto global_id = item_id.get_global_id(0);
3278
3279 for (auto i = id; i < nrows; i += item_id.get_range()[0])
3280 {
3281 auto block_id = i/ellpack_size ;
3282 auto local_id = i%ellpack_size ;*/
3283
3284 }
3285 else
3286 {
3287 //sycl::local_accessor<ValueType, 1> lds_x{pack_size, cgh};
3288 sycl::nd_range<1> r{sycl::range<1>{total_threads},sycl::range<1>{pack_size}};
3289 cgh.parallel_for<class compute_inv_diagN>(r,
3290 [=](sycl::nd_item<1> item_id)
3291 {
3292 auto local_id = item_id.get_local_id(0);
3293 auto global_id = item_id.get_global_id(0);
3294 for (auto i = global_id; i < nrows; i += item_id.get_global_range()[0])
3295 {
3296 auto block_id = i/pack_size ;
3297
3298 int begin = access_block_row_offset[block_id];
3299 int end = access_block_row_offset[block_id+1];
3300
3301 for(int k=begin;k<end;++k)
3302 {
3303 auto kcol = k*pack_size+local_id ;
3304 std::size_t col = access_cols[kcol] ;
3305 if(col==i)
3306 {
3307 for(int ieq=0;ieq<N;++ieq)
3308 {
3309 auto kval = k*NxN + ieq*N + ieq ;
3310 auto diag = access_values[kval*pack_size+local_id] ;
3311 if(diag!=0)
3312 access_y[i*N+ieq] = 1./diag;
3313 else
3314 access_y[i*N+ieq] = 1.;
3315 }
3316 }
3317 }
3318 }
3319 });
3320
3321 /*
3322 cgh.parallel_for<class compute_inv_diagN>(
3323 sycl::range<1>{total_threads},
3324 [=] (sycl::item<1> item_id)
3325 {
3326 auto id = item_id.get_id(0);
3327 //auto local_id = item_id.get_local_id(0);
3328 //auto block_id = item_id.get_group(0) ;
3329 //auto global_id = item_id.get_global_id(0);
3330
3331 for (std::size_t i = id; i < nrows; i += item_id.get_range()[0])
3332 {
3333 auto block_id = i/pack_size ;
3334 auto local_id = i%pack_size ;
3335*/
3336 }
3337 });
3338 /*
3339 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(y);
3340 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> mat_acc(m_values);
3341 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(block_row_offset);
3342 sycl::host_accessor<int, 1, sycl::access::mode::read> cols_acc(block_cols);
3343 for(int irow=0;irow<nrows;++irow)
3344 {
3345 auto ib=irow/ellpack_size ;
3346 auto il=irow%ellpack_size ;
3347 std::cout<<"MAT["<<irow<<","<<ib<<"]:";
3348 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
3349 {
3350 std::cout<<"("<<cols_acc[k*ellpack_size+il]<<",";
3351 for(int i=0;i<N;++i)
3352 for(int j=0;j<N;++j)
3353 std::cout<<mat_acc[(k*NxN+i*N+j)*ellpack_size+il]<<",";
3354 std::cout<<")"<<std::endl;
3355 }
3356 }
3357 for(int irow=0;irow<nrows;++irow)
3358 {
3359 auto ib=irow/ellpack_size ;
3360 auto il=irow%ellpack_size ;
3361 std::cout<<"DIAG["<<irow<<","<<il<<"]:";
3362 for(int i=0;i<N;++i)
3363 std::cout<<h_acc[il*N+i]<<",";
3364 std::cout<<std::endl;
3365 }*/
3366 // clang-format on
3367 }
3368
3369 template <typename ValueT, int EllPackSize>
3370 void MatrixInternal<ValueT, EllPackSize>::computeInvBlockDiag(ValueBufferType& y, sycl::queue& queue) const
3371 {
3372
3373 auto device = queue.get_device();
3374
3375 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
3376 // getting the maximum work group size per thread
3377 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
3378 // building the best number of global thread
3379 auto total_threads = num_groups * ellpack_size;
3380
3381 // clang-format off
3382 int N = this->m_N;
3383 int NxN = this->m_NxN;
3384 int pack_size = ellpack_size ;
3385 auto nrows = m_profile->getNRows() ;
3386 auto nnz = m_profile->getNnz() ;
3387
3388 assert(y.size()>=NxN*nrows) ;
3389
3390 auto internal_profile = m_profile->internal() ;
3391 auto& kcol = internal_profile->getKCol() ;
3392 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
3393 auto& block_cols = internal_profile->getBlockCols() ;
3394 {
3395 ValueBufferType buffer (sycl::range<1>(internal_profile->m_block_nrows*NxN*pack_size)) ;
3396 // COMPUTE VALUES
3397 queue.submit([&](sycl::handler& cgh)
3398 {
3399 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3400 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3401 auto matrix = m_values.template get_access<sycl::access::mode::read>(cgh);
3402 auto access_y = y.template get_access<sycl::access::mode::read_write>(cgh);
3403 auto A = buffer.template get_access<sycl::access::mode::read_write>(cgh);
3404
3405 auto lu = LU<decltype(matrix),decltype(access_y),decltype(A)>{N,matrix} ;
3406 //sycl::local_accessor<ValueT,1> local_mem(sycl::range<1>(pack_size*NxN), cgh);
3407
3408 cgh.parallel_for<class compute_inv_block_diag>(
3409 sycl::range<1>{total_threads},
3410 [=] (sycl::item<1> item_id)
3411 {
3412 auto id = item_id.get_id(0);
3413 //auto local_id = item_id.get_local_id(0);
3414 //auto block_id = item_id.get_group(0) ;
3415 //auto global_id = item_id.get_global_id(0);
3416
3417 for (std::size_t global_id = id; global_id < nrows; global_id += item_id.get_range()[0])
3418 {
3419 auto block_id = global_id/pack_size ;
3420 auto local_id = global_id%pack_size ;
3421
3422 int begin = access_block_row_offset[block_id];
3423 int end = access_block_row_offset[block_id+1];
3424 for(int k=begin;k<end;++k)
3425 {
3426 auto kcol = k*pack_size+local_id ;
3427 std::size_t col = access_cols[kcol] ;
3428 if(col==global_id)
3429 {
3430 lu.factorize(global_id,
3431 local_id,
3432 block_id,
3433 k,
3434 A);
3435
3436 lu.inverse(global_id,
3437 local_id,
3438 block_id,
3439 A,
3440 access_y);
3441 /*
3442 {
3443 // Copy Diag Matrix in A
3444 for(int i=0;i<N;++i)
3445 for(int j=0;j<N;++j)
3446 A[tile._ij(local_id,i,j)] = matrix[tile._ijk(k,i,j)+local_id] ;
3447
3448 //Factorize A = LU
3449 for (int k = 0; k < N; ++k)
3450 {
3451 assert(A[tile._ij(local_id,k,k)] != 0);
3452 A[tile._ij(global_id,k,k)] = 1 / A[tile._ij(global_id,k,k)];
3453 for (int i = k + 1; i < N; ++i) {
3454 A[tile._ij(global_id,i,k)] *= A[tile._ij(global_id,k,k)];
3455 }
3456 for (int i = k + 1; i < N; ++i) {
3457 for (int j = k + 1; j < N; ++j) {
3458 A[tile._ij(global_id,i,j)] -= A[tile._ij(global_id,i,k)] * A[tile._ij(global_id,k,j)];
3459 }
3460 }
3461 }
3462
3463 // SET Y to Id
3464 for(int i=0;i<N;++i)
3465 for(int j=0;j<N;++j)
3466 access_y[tile._ij(global_id,i,j)] = 0. ;
3467 for(int i=0;i<m_N;++i)
3468 access_y[tile._ij(global_id,i,i)] = 1. ;
3469
3470 // L solve
3471 for (int i = 1; i < N; ++i)
3472 {
3473 for (int j = 0; j < i; ++j)
3474 {
3475 for(int k=0;k<N;++k)
3476 access_y[tile._ij(global_id,i,k)] -= A[tile._ij(global_id,i,j)] * access_y[tile._ij(global_id,j,k)];
3477 }
3478 }
3479
3480 // U solve
3481 for (int i = N - 1; i >= 0; --i)
3482 {
3483 for (int j = N - 1; j > i; --j)
3484 {
3485 for(int k=0;k<N;++k)
3486 access_y[tile._ij(global_id,i,k)] -= A[tile._ij(global_id,i,j)] * access_y[tile._ij(global_id,j,k)];
3487 }
3488 for(int k=0;k<N;++k)
3489 access_y[tile._ij(global_id,i,k)] *= A[tile._ij(global_id,i,i)];
3490 }
3491 }*/
3492 }
3493 }
3494 }
3495 });
3496 });
3497 }
3498#ifdef PRINT_DEBUG_INFO
3499 {
3500 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_acc(y);
3501 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> mat_acc(m_values);
3502 sycl::host_accessor<int, 1, sycl::access::mode::read> kcol_acc(block_row_offset);
3503 sycl::host_accessor<int, 1, sycl::access::mode::read> cols_acc(block_cols);
3504 std::vector<ValueT> test_y(nrows*NxN) ;
3505 std::vector<ValueT> test_A(internal_profile->m_block_nrows*NxN*pack_size) ;
3506 auto lu = LU<decltype(mat_acc),decltype(test_y.data()),decltype(test_A.data())>{N,mat_acc} ;
3507 for(int irow=0;irow<nrows;++irow)
3508 {
3509 auto ib=irow/ellpack_size ;
3510 auto il=irow%ellpack_size ;
3511 alien_info([&] {
3512 cout() <<"MAT["<<irow<<","<<ib<<"]:";
3513 });
3514 for(int k=kcol_acc[ib];k<kcol_acc[ib+1];++k)
3515 {
3516 alien_info([&] {
3517 cout() <<"\tCOLS["<<k<<"] = "<<cols_acc[k*pack_size+il];
3518 });
3519 /*
3520 for(int i=0;i<N;++i)
3521 {
3522 for(int j=0;j<N;++j)
3523 std::cout<<mat_acc[(k*NxN+i*N+j)*pack_size+il]<<" ";
3524 std::cout<<std::endl;
3525 }*/
3526 if(cols_acc[k*pack_size+il]==irow)
3527 {
3528
3529 alien_info([&] {
3530 cout() <<"\tCOMPUTE INVERSE["<<cols_acc[k*pack_size+il]<<"]" ;
3531 for(int i=0;i<N;++i)
3532 {
3533 for(int j=0;j<N;++j)
3534 cout()<<mat_acc[(k*NxN+i*N+j)*pack_size+il]<<" ";
3535 }
3536 });
3537 lu.factorize(irow,
3538 il,
3539 ib,
3540 k,
3541 test_A.data());
3542
3543 lu.inverse(irow,
3544 il,
3545 ib,
3546 test_A.data(),
3547 test_y.data());
3548
3549 alien_info([&] {
3550 for(int i=0;i<N;++i)
3551 {
3552 for(int j=0;j<N;++j)
3553 std::cout<<test_y[irow*NxN+i*N+j]<<",";
3554 std::cout<<std::endl;
3555 }
3556 });
3557 }
3558 }
3559 }
3560 for(int irow=0;irow<nrows;++irow)
3561 {
3562 auto ib=irow/ellpack_size ;
3563 auto il=irow%ellpack_size ;
3564 alien_info([&] {
3565 cout()<<"INV BLOCK DIAG["<<irow<<","<<il<<"]:\n";
3566 for(int i=0;i<N;++i)
3567 {
3568 for(int j=0;j<N;++j)
3569 cout()<<h_acc[irow*NxN+i*N+j]<<",";
3570 //std::cout<<std::endl;
3571 }
3572 });
3573 }
3574 }
3575#endif
3576 // clang-format on
3577 }
3578
3579 template <typename ValueT, int EllPackSize>
3580 void MatrixInternal<ValueT, EllPackSize>::computeInvDiag(ValueBufferType& y) const
3581 {
3582 this->computeInvDiag(y, SYCLEnv::instance()->internal()->queue());
3583 }
3584
3585 template <typename ValueT, int EllPackSize>
3586 void MatrixInternal<ValueT, EllPackSize>::computeInvBlockDiag(ValueBufferType& y) const
3587 {
3588 this->computeInvBlockDiag(y, SYCLEnv::instance()->internal()->queue());
3589 }
3590
3591
3592 template <typename ValueT, int EllPackSize>
3593 void MatrixInternal<ValueT, EllPackSize>::scal(ValueBufferType& y, sycl::queue& queue)
3594 {
3595
3596 auto device = queue.get_device();
3597
3598 auto num_groups = queue.get_device().get_info<sycl::info::device::max_compute_units>();
3599 // getting the maximum work group size per thread
3600 auto max_work_group_size = queue.get_device().get_info<sycl::info::device::max_work_group_size>();
3601 // building the best number of global thread
3602 auto total_threads = num_groups * ellpack_size;
3603
3604 // clang-format off
3605 auto nrows = m_profile->getNRows() ;
3606 auto nnz = m_profile->getNnz() ;
3607
3608 auto internal_profile = m_profile->internal() ;
3609 auto& kcol = internal_profile->getKCol() ;
3610 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
3611 auto& block_cols = internal_profile->getBlockCols() ;
3612 {
3613 // COMPUTE VALUES
3614 queue.submit([&](sycl::handler& cgh)
3615 {
3616 auto access_block_row_offset = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3617 auto access_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3618 auto access_values = m_values.template get_access<sycl::access::mode::read_write>(cgh);
3619 auto access_y = y.template get_access<sycl::access::mode::read>(cgh);
3620
3621
3622 cgh.parallel_for<class scal_matrix>(sycl::range<1>{total_threads},
3623 [=] (sycl::item<1> item_id)
3624 {
3625 auto id = item_id.get_id(0);
3626 //auto local_id = item_id.get_local_id(0);
3627 //auto block_id = item_id.get_group(0) ;
3628 //auto global_id = item_id.get_global_id(0);
3629
3630 for (auto i = id; i < nrows; i += item_id.get_range()[0])
3631 {
3632 auto block_id = i/ellpack_size ;
3633 auto local_id = i%ellpack_size ;
3634
3635 int block_row_offset = access_block_row_offset[block_id]*ellpack_size ;
3636 auto block_row_size = access_block_row_offset[block_id+1]-access_block_row_offset[block_id] ;
3637 for(int j=0;j<block_row_size;++j)
3638 {
3639 auto k = block_row_offset+j*ellpack_size+local_id ;
3640 access_values[k] *= access_y[i] ;
3641 }
3642 }
3643 });
3644 });
3645 }
3646
3647 if(m_ext_profile)
3648 {
3649
3650 auto interface_nrows = m_ext_profile->getNRows();
3651 auto ext_nnz = m_ext_profile->getNnz();
3652 auto ext_block_nnz = m_ext_profile->getBlockNnz();
3653
3654 auto ext_internal_profile = m_ext_profile->internal();
3655 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
3656
3657 {
3658 queue.submit([&](sycl::handler& cgh)
3659 {
3660 auto access_block_row_offset = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3661 auto access_block_values = m_ext_values->template get_access<sycl::access::mode::read_write>(cgh);
3662 auto access_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
3663 auto access_y = y.template get_access<sycl::access::mode::read>(cgh);
3664
3665 cgh.parallel_for<class set_matrix_values7>(sycl::range<1>{total_threads},
3666 [=] (sycl::item<1> item_id)
3667 {
3668 auto id = item_id.get_id(0);
3669
3670 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
3671 {
3672 auto block_id = i/ellpack_size ;
3673 auto local_id = i%ellpack_size ;
3674 auto begin = access_block_row_offset[block_id] ;
3675 auto end = access_block_row_offset[block_id+1] ;
3676
3677 for(int k=begin;k<end;++k)
3678 {
3679 access_block_values[k*ellpack_size+local_id] *= access_y[access_row_ids[i]] ;
3680 }
3681 }
3682 });
3683 });
3684 }
3685 }
3686 }
3687
3688 template <typename ValueT, int EllPackSize>
3689 void MatrixInternal<ValueT, EllPackSize>::scal(ValueBufferType& y)
3690 {
3691 this->scal(y, SYCLEnv::instance()->internal()->queue());
3692 }
3693
3694 template <typename ValueT, int EllPackSize>
3695 void MatrixInternal<ValueT, EllPackSize>::
3696 copyDevicePointers(int local_offset,
3697 std::size_t nrows,
3698 std::size_t nnz,
3699 int* rows,
3700 int* ncols,
3701 int* cols,
3702 ValueT* values) const
3703 {
3704#ifdef PRINT_DEBUG_INFO
3705 alien_info([&] {
3706 cout()<<"SYCLMATRIX COPY DEVICE POINTERS : "<<nrows<<" "<<nnz;
3707 });
3708#endif
3709
3710 auto env = SYCLEnv::instance() ;
3711 auto max_num_threads = env->maxNumThreads() ;
3712 auto& queue = env->internal()->queue() ;
3713 auto pack_size = ellpack_size;
3714
3715 auto internal_profile = m_profile->internal() ;
3716 auto& kcol = internal_profile->getKCol() ;
3717 auto& block_row_offset = internal_profile->getBlockRowOffset() ;
3718 auto& block_cols = internal_profile->getBlockCols() ;
3719
3720 auto local_row_size = m_profile->localRowSize();
3721 if(local_row_size == nullptr)
3722 {
3723
3724 queue.submit( [&](sycl::handler& cgh)
3725 {
3726 auto acc_val = m_values.template get_access<sycl::access::mode::read>(cgh);
3727 auto acc_bkcol = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3728 auto acc_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3729 auto acc_kcol = kcol.template get_access<sycl::access::mode::read>(cgh);
3730 cgh.parallel_for<class copy_device_ptr>(
3731 sycl::range<1>{max_num_threads},
3732 [=] (sycl::item<1> itemId)
3733 {
3734 auto id = itemId.get_id(0);
3735 for (auto i = id; i < nrows; i += itemId.get_range()[0])
3736 {
3737 auto block_id = i/pack_size ;
3738 auto local_id = i%pack_size ;
3739
3740 int begin = acc_bkcol[block_id] ;
3741 int end = acc_bkcol[block_id+1] ;
3742 int row_size = acc_kcol[i+1] - acc_kcol[i];
3743
3744 rows[i] = (int) (local_offset + i) ;
3745 ncols[i] = row_size ;
3746
3747 auto offset = acc_kcol[i];
3748
3749 for(int k=begin;k<end;++k)
3750 {
3751 auto j = k-begin ;
3752 if(j<row_size)
3753 {
3754 values[offset+j] = acc_val[k*pack_size+local_id] ;
3755 cols[offset+j] = acc_cols[k*pack_size+local_id] ;
3756 }
3757 }
3758 }
3759 });
3760 }).wait();
3761#ifdef PRINT_DEBUG_INFO
3762 {
3763 std::vector<ValueT> h_data(nnz) ;
3764 queue.memcpy(h_data.data(), values, nnz * sizeof(ValueT)).wait();
3765 std::vector<int> h_cols(nnz) ;
3766 queue.memcpy(h_cols.data(), cols, nnz * sizeof(int)).wait();
3767 sycl::host_accessor<int, 1, sycl::access::mode::read> h_kcol(kcol);
3768 alien_info([&]{
3769 cout()<<"SYCL DEVICE COPY : "<<nrows<<" "<<nnz;
3770 for(int irow=0;irow<nrows;++irow)
3771 {
3772 cout()<<"COPY MAT["<<irow<<"]:"<<h_kcol[irow+1]-h_kcol[irow];
3773 for(int k=h_kcol[irow];k<h_kcol[irow+1];++k)
3774 {
3775 std::cout<<h_cols[k]<<" "<<h_data[k]<<" ";
3776 }
3777 std::cout<<std::endl ;
3778 }
3779 });
3780 }
3781#endif
3782 }
3783 else
3784 {
3785
3786 IndexBufferType lrowsize(local_row_size, sycl::range<1>(nrows));
3787 queue.submit( [&](sycl::handler& cgh)
3788 {
3789 auto acc_val = m_values.template get_access<sycl::access::mode::read>(cgh);
3790 auto acc_bkcol = block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3791 auto acc_cols = block_cols.template get_access<sycl::access::mode::read>(cgh);
3792 auto acc_kcol = kcol.template get_access<sycl::access::mode::read>(cgh);
3793 auto acc_lrowsize = lrowsize.template get_access<sycl::access::mode::read>(cgh);
3794 cgh.parallel_for<class copy_device_ptr>(
3795 sycl::range<1>{max_num_threads},
3796 [=] (sycl::item<1> itemId)
3797 {
3798 auto id = itemId.get_id(0);
3799 for (auto i = id; i < nrows; i += itemId.get_range()[0])
3800 {
3801 auto block_id = i/pack_size ;
3802 auto local_id = i%pack_size ;
3803
3804 int begin = acc_bkcol[block_id] ;
3805 int end = acc_bkcol[block_id+1] ;
3806 int row_size = acc_kcol[i+1] - acc_kcol[i];
3807 int lrow_size = acc_lrowsize[i] ;
3808 rows[i] = (int) (local_offset + i) ;
3809 ncols[i] = row_size ;
3810
3811 auto offset = acc_kcol[i];
3812
3813 for(int k=begin;k<end;++k)
3814 {
3815 auto j = k-begin ;
3816 if(j<lrow_size)
3817 {
3818 values[offset+j] = acc_val[k*pack_size+local_id];
3819 cols[offset+j] = local_offset + acc_cols[k*pack_size+local_id];
3820 }
3821 }
3822 }
3823 });
3824 });
3825
3826 assert(m_ext_profile) ;
3827
3828 auto interface_nrows = m_ext_profile->getNRows();
3829 auto ext_nnz = m_ext_profile->getNnz();
3830 auto ext_block_nnz = m_ext_profile->getBlockNnz();
3831
3832 auto ext_internal_profile = m_ext_profile->internal();
3833 auto& ext_block_row_offset = ext_internal_profile->getBlockRowOffset();
3834 auto& ext_block_cols = ext_internal_profile->getBlockCols() ;
3835
3836 queue.submit([&](sycl::handler& cgh)
3837 {
3838 auto acc_kcol = kcol.template get_access<sycl::access::mode::read>(cgh);
3839 auto acc_ext_bkcol = ext_block_row_offset.template get_access<sycl::access::mode::read>(cgh);
3840 auto acc_ext_cols = ext_block_cols.template get_access<sycl::access::mode::read>(cgh);
3841 auto acc_ext_values = m_ext_values->template get_access<sycl::access::mode::read_write>(cgh);
3842 auto acc_row_ids = m_interface_row_ids->template get_access<sycl::access::mode::read>(cgh);
3843 auto acc_lrowsize = lrowsize.template get_access<sycl::access::mode::read>(cgh);
3844 auto acc_recv_uids = m_recv_uids->template get_access<sycl::access::mode::read>(cgh);
3845
3846 cgh.parallel_for<class ext_copy_device_ptr>(
3847 sycl::range<1>{max_num_threads},
3848 [=] (sycl::item<1> item_id)
3849 {
3850 auto id = item_id.get_id(0);
3851
3852 for (auto i = id; i < interface_nrows; i += item_id.get_range()[0])
3853 {
3854 auto block_id = i / pack_size ;
3855 auto local_id = i % pack_size ;
3856 auto begin = acc_ext_bkcol[block_id] ;
3857 auto end = acc_ext_bkcol[block_id+1] ;
3858
3859 auto row_id = acc_row_ids[i];
3860 int row_size = acc_kcol[row_id+1] - acc_kcol[row_id];
3861 int lrow_size = acc_lrowsize[row_id] ;
3862 auto offset = acc_kcol[row_id] + lrow_size;
3863
3864 for(int k=begin;k<end;++k)
3865 {
3866 auto j = k-begin ;
3867 if(j<row_size-lrow_size)
3868 {
3869 values[offset+j] = acc_ext_values[k*pack_size+local_id] ;
3870 cols[offset+j] = acc_recv_uids[acc_ext_cols[k*pack_size+local_id]] ;
3871 }
3872 }
3873 }
3874 });
3875 });
3876#ifdef PRINT_DEBUG_INFO
3877 {
3878 std::vector<ValueT> h_data(nnz) ;
3879 queue.memcpy(h_data.data(), values, nnz * sizeof(ValueT)).wait();
3880 std::vector<int> h_cols(nnz) ;
3881 queue.memcpy(h_cols.data(), cols, nnz * sizeof(int)).wait();
3882
3883 sycl::host_accessor<int, 1, sycl::access::mode::read> h_kcol(kcol);
3884 sycl::host_accessor<int, 1, sycl::access::mode::read> h_bkcol(ext_block_row_offset);
3885 sycl::host_accessor<int, 1, sycl::access::mode::read> h_bcols(ext_block_cols);
3886 sycl::host_accessor<int, 1, sycl::access::mode::read> h_row_ids(*m_interface_row_ids);
3887 sycl::host_accessor<int, 1, sycl::access::mode::read> h_recv_uids(*m_recv_uids);
3888 sycl::host_accessor<ValueT, 1, sycl::access::mode::read> h_values(*m_ext_values);
3889
3890 alien_info([&]
3891 {
3892 for(int i=0;i<interface_nrows;++i)
3893 {
3894 auto block_id = i / pack_size ;
3895 auto local_id = i % pack_size ;
3896
3897 auto row_id = h_row_ids[i];
3898
3899 int begin = h_bkcol[block_id] ;
3900 int end = h_bkcol[block_id+1] ;
3901 int row_size = h_kcol[row_id+1] - h_kcol[row_id];
3902 int lrow_size = local_row_size[row_id] ;
3903
3904 auto offset = h_kcol[row_id] + lrow_size;
3905 /*
3906 cout()<<"KCOL["<<i<<" "<<row_id<<"] = "<<h_kcol[row_id]
3907 <<" ROW SIZE = "<<row_size
3908 <<" LOCAL ROW SIZE = "<<lrow_size ;
3909
3910 for(int k=begin;k<end;++k)
3911 {
3912 auto j = k-begin ;
3913 if(j<row_size-lrow_size)
3914 {
3915 cout() << "\t\t("<<h_recv_uids[h_bcols[k*pack_size+local_id]]
3916 <<","<<h_bcols[k*pack_size+local_id]
3917 <<","<<h_values[k*pack_size+local_id]<<")" ;
3918 }
3919 }*/
3920 cout()<<"CXR ["<<row_id<<"] = "<<h_kcol[row_id]<<" "<<h_kcol[row_id+1]<<" "<<lrow_size<<" "<<row_size ;
3921 for (int k = h_kcol[row_id]; k < h_kcol[row_id + 1]; ++k)
3922 {
3923 cout() << "\t\t(" << h_cols[k] << "," << h_data[k] << ")";
3924 }
3925 }
3926 });
3927 /*
3928 alien_info([&] {
3929 cout()<<"SYCL DEVICE COPY : "<<nrows<<" "<<nnz<<" "<<ext_nnz<<" "<<interface_nrows;
3930 for(int irow=0;irow<nrows;++irow)
3931 {
3932 std::cout<<"COPY MAT["<<irow<<"]:"<<local_row_size[irow]<<" "<<h_kcol[irow+1]-h_kcol[irow];
3933 for(int k=h_kcol[irow];k<h_kcol[irow+1];++k)
3934 {
3935 std::cout<<h_cols[k]<<" "<<h_data[k]<<" ";
3936 }
3937 std::cout<<std::endl ;
3938 }
3939 });*/
3940 }
3941#endif
3942 }
3943 }
3944} // namespace SYCLInternal
3945
3946
3947
3948
3949template <typename ValueT>
3951: IMatrixImpl(nullptr, AlgebraTraits<BackEnd::tag::sycl>::name())
3952, m_send_policy(SimpleCSRInternal::CommProperty::ASynch)
3953, m_recv_policy(SimpleCSRInternal::CommProperty::ASynch)
3954{}
3955
3956template <typename ValueT>
3958: IMatrixImpl(multi_impl, AlgebraTraits<BackEnd::tag::sycl>::name())
3959, m_send_policy(SimpleCSRInternal::CommProperty::ASynch)
3960, m_recv_policy(SimpleCSRInternal::CommProperty::ASynch)
3961{}
3962
3963template <typename ValueT>
3965{
3966#ifdef ALIEN_USE_PERF_TIMER
3967 m_timer.printInfo("SYCLBELLPACK-MATRIX");
3968#endif
3969}
3970
3971template <typename ValueT>
3973initMatrix(Arccore::MessagePassing::IMessagePassingMng* parallel_mng,
3974 Integer local_offset,
3975 Integer global_size,
3976 std::size_t nrows,
3977 int const* kcol,
3978 int const* cols,
3979 SimpleCSRInternal::DistStructInfo const& matrix_dist_info,
3980 int block_size)
3981{
3982#ifdef PRINT_DEBUG_INFO
3983 alien_debug([&] {
3984 cout()<<"SYCLBellPackMatrix::initMatrix : "<<local_offset<<" "<<nrows<<" "<<global_size;
3985 cout()<<"ParalleMng : "<<parallel_mng;
3986 });
3987#endif
3988
3989 m_nproc = 1;
3990 m_myrank = 0;
3991 m_parallel_mng = parallel_mng;
3992 if (parallel_mng) {
3993 m_nproc = m_parallel_mng->commSize();
3994 m_myrank = m_parallel_mng->commRank();
3995 }
3996 m_is_parallel = (m_nproc > 1);
3997
3998
3999 m_matrix_dist_info.copy(matrix_dist_info);
4000
4001 m_ellpack_size = PKSIZE;
4002 if (m_nproc > 1)
4003 {
4004 m_local_size = nrows;
4005 m_local_offset = local_offset;
4006 m_global_size = global_size;
4007
4008 m_ghost_size = m_matrix_dist_info.m_ghost_nrow;
4009
4010 auto& local_row_size = m_matrix_dist_info.m_local_row_size;
4011 auto sorted_cols = m_matrix_dist_info.m_cols.data();
4012
4013 std::size_t block_nrows = ProfileInternalType::nbBlocks(nrows);
4014
4015 ProfileInternalType::computeBlockRowOffset(m_block_row_offset, nrows, kcol);
4016
4017 // clang-format off
4018 alien_debug([&] {
4019 cout() << "OFFSET = "<<m_local_offset ;
4020 cout() << "NROWS = "<<nrows;
4021 cout() << "NNZ = "<<kcol[nrows];
4022 cout() << "BNROWS = "<<block_nrows;
4023 cout() << "BNNZ = "<<m_block_row_offset[block_nrows];
4024 });
4025 // clang-format on
4026 //Universe().traceMng()->flush();
4027 m_profilePKSIZE.reset( new ProfileInternalType{ nrows,
4028 kcol,
4029 sorted_cols,
4030 m_block_row_offset.data(),
4031 local_row_size.data() });
4032
4033 m_matrixPKSIZE.reset(new MatrixInternalType{ m_profilePKSIZE.get(), block_size });
4034
4035 // EXTRACT EXTERNAL PROFILE
4036 std::size_t interface_nrows = m_matrix_dist_info.m_interface_nrow;
4037 std::vector<int> ext_kcol(interface_nrows + 1);
4038
4039 int ext_nnz = 0;
4040 for (std::size_t i = 0; i < interface_nrows; ++i) {
4041 auto id = m_matrix_dist_info.m_interface_rows[i];
4042 ext_kcol[i] = ext_nnz;
4043 ext_nnz += kcol[id + 1] - kcol[id] - local_row_size[id];
4044 }
4045 ext_kcol[interface_nrows] = ext_nnz;
4046
4047 std::vector<int> ext_cols(ext_nnz);
4048 {
4049 int jcol = 0;
4050 for (std::size_t i = 0; i < interface_nrows; ++i) {
4051 auto id = m_matrix_dist_info.m_interface_rows[i];
4052 for (int k = kcol[id] + local_row_size[id]; k < kcol[id + 1]; ++k) {
4053 ext_cols[jcol++] = (int)(sorted_cols[k] - nrows);
4054 }
4055 }
4056 }
4057
4058 std::size_t ext_block_nrows = ProfileInternalType::nbBlocks(interface_nrows);
4059 ProfileInternalType::computeBlockRowOffset(m_ext_block_row_offset, interface_nrows, ext_kcol.data());
4060 // clang-format off
4061 alien_debug([&] {
4062 cout() << "EXT NROWS = "<<interface_nrows;
4063 cout() << "EXT NNZ = "<<ext_kcol[interface_nrows];
4064 cout() << "EXT BNROWS = "<<ext_block_nrows;
4065 cout() << "EXT BNNZ = "<<m_block_row_offset[ext_block_nrows];
4066 });
4067 // clang-format on
4068
4069 m_ext_profilePKSIZE.reset( new ProfileInternalType{ interface_nrows,
4070 ext_kcol.data(),
4071 ext_cols.data(),
4072 m_ext_block_row_offset.data(),
4073 nullptr });
4074
4075 m_matrixPKSIZE->m_ext_profile = m_ext_profilePKSIZE.get();
4076 typedef typename MatrixInternalType::IndexBufferType IndexBufferType;
4077 m_matrixPKSIZE->m_h_interface_row_ids = m_matrix_dist_info.m_interface_rows.data();
4078 m_matrixPKSIZE->m_interface_row_ids.reset(new IndexBufferType{ m_matrix_dist_info.m_interface_rows.data(),
4079 m_matrix_dist_info.m_interface_rows.size() });
4080
4081 m_matrixPKSIZE->m_send_ids.reset(new IndexBufferType{ m_matrix_dist_info.m_send_info.m_ids.data(),
4082 m_matrix_dist_info.m_send_info.m_ids.size() });
4083 m_matrixPKSIZE->m_recv_ids.reset(new IndexBufferType{ m_matrix_dist_info.m_recv_info.m_ids.data(),
4084 m_matrix_dist_info.m_recv_info.m_ids.size() });
4085 m_matrixPKSIZE->m_recv_uids.reset(new IndexBufferType{ m_matrix_dist_info.m_recv_info.m_uids.data(),
4086 m_matrix_dist_info.m_recv_info.m_uids.size() });
4087 }
4088 else
4089 {
4090 // clang-format off
4091 m_local_offset = 0;
4092 m_local_size = nrows;
4093 m_global_size = m_local_size;
4094 m_ghost_size = 0;
4095 // clang-format on
4096
4097 std::size_t block_nrows = ProfileInternalType::nbBlocks(nrows);
4098
4099 ProfileInternalType::computeBlockRowOffset(m_block_row_offset, nrows, kcol);
4100
4101 // clang-format off
4102 alien_debug([&] {
4103 cout() << "NROWS = "<<nrows;
4104 cout() << "NNZ = "<<kcol[nrows];
4105 cout() << "BNROWS = "<<block_nrows;
4106 cout() << "BNNZ = "<<m_block_row_offset[block_nrows];
4107 });
4108 // clang-format on
4109
4110 m_profilePKSIZE.reset( new ProfileInternalType{ nrows,
4111 kcol,
4112 cols,
4113 m_block_row_offset.data(),
4114 nullptr });
4115
4116 m_matrixPKSIZE.reset( new MatrixInternalType{ m_profilePKSIZE.get(), block_size });
4117 m_matrixPKSIZE->m_first_upper_ghost_index = m_matrix_dist_info.m_first_upper_ghost_index - m_local_size;
4118 }
4119
4120 return true;
4121}
4122
4123template <typename ValueT>
4125SYCLBEllPackMatrix<ValueT>::cloneTo(const MultiMatrixImpl* multi) const
4126{
4127 auto block_size = blockSize() ;
4129 matrix->setBlockSize(block_size);
4130 matrix->initMatrix(m_parallel_mng,
4131 m_local_offset,
4132 m_global_size,
4133 m_profilePKSIZE->getNRows(),
4134 m_profilePKSIZE->kcol(),
4135 m_profilePKSIZE->cols(),
4136 m_matrix_dist_info,
4137 block_size);
4138 matrix->setMatrixValues(getAddressData(), true);
4139 return matrix;
4140}
4141
4142template <typename ValueT>
4143bool SYCLBEllPackMatrix<ValueT>::setMatrixValues(Arccore::Real const* values, bool only_host)
4144{
4145 return m_matrixPKSIZE->setMatrixValues(values, only_host);
4146}
4147
4148template <typename ValueT>
4149void SYCLBEllPackMatrix<ValueT>::notifyChanges()
4150{
4151 if (m_matrixPKSIZE.get())
4152 m_matrixPKSIZE->notifyChanges();
4153}
4154
4155template <typename ValueT>
4156void SYCLBEllPackMatrix<ValueT>::endUpdate()
4157{
4158 if (m_matrixPKSIZE.get() && m_matrixPKSIZE->needUpdate()) {
4159 m_matrixPKSIZE->endUpdate();
4160 this->updateTimestamp();
4161 }
4162}
4163
4164template <typename ValueT>
4165ValueT const* SYCLBEllPackMatrix<ValueT>::getAddressData() const
4166{
4167 return m_matrixPKSIZE->getHCsrData();
4168}
4169
4170template <typename ValueT>
4171ValueT const* SYCLBEllPackMatrix<ValueT>::data() const
4172{
4173 return m_matrixPKSIZE->getHCsrData();
4174}
4175
4176template <typename ValueT>
4177ValueT* SYCLBEllPackMatrix<ValueT>::getAddressData()
4178{
4179 return m_matrixPKSIZE->getHCsrData();
4180}
4181
4182template <typename ValueT>
4183ValueT* SYCLBEllPackMatrix<ValueT>::data()
4184{
4185 return m_matrixPKSIZE->getHCsrData();
4186}
4187
4188template <typename ValueT>
4189void SYCLBEllPackMatrix<ValueT>::mult(SYCLVector<ValueT> const& x, SYCLVector<ValueT>& y) const
4190{
4191 return m_matrixPKSIZE->mult(x.internal()->values(), y.internal()->values());
4192}
4193
4194template <typename ValueT>
4195void SYCLBEllPackMatrix<ValueT>::endDistMult(SYCLVector<ValueT> const& x, SYCLVector<ValueT>& y) const
4196{
4197 m_matrixPKSIZE->addExtMult(x.internal()->ghostValues(getGhostSize()*blockSize()), y.internal()->values());
4198}
4199
4200template <typename ValueT>
4201void SYCLBEllPackMatrix<ValueT>::addLMult(ValueT alpha, SYCLVector<ValueT> const& x, SYCLVector<ValueT>& y) const
4202{
4203 m_profilePKSIZE->dcol();
4204 return m_matrixPKSIZE->addLMult(alpha, x.internal()->values(), y.internal()->values());
4205}
4206
4207template <typename ValueT>
4208void SYCLBEllPackMatrix<ValueT>::endDistLMult(ValueT alpha, SYCLVector<ValueT> const& x, SYCLVector<ValueT>& y) const
4209{
4210 m_matrixPKSIZE->addExtLMult(alpha, x.internal()->ghostValues(getGhostSize()*blockSize()), y.internal()->values());
4211}
4212
4213template <typename ValueT>
4214void SYCLBEllPackMatrix<ValueT>::addUMult(ValueT alpha, SYCLVector<ValueT> const& x, SYCLVector<ValueT>& y) const
4215{
4216 m_profilePKSIZE->dcol();
4217 return m_matrixPKSIZE->addUMult(alpha, x.internal()->values(), y.internal()->values());
4218}
4219
4220template <typename ValueT>
4221void SYCLBEllPackMatrix<ValueT>::endDistUMult(ValueT alpha, SYCLVector<ValueT> const& x, SYCLVector<ValueT>& y) const
4222{
4223 m_matrixPKSIZE->addExtUMult(alpha, x.internal()->ghostValues(getGhostSize()*blockSize()), y.internal()->values());
4224}
4225
4226template <typename ValueT>
4227void SYCLBEllPackMatrix<ValueT>::multDiag(SYCLVector<ValueType> const& x, SYCLVector<ValueType>& y) const
4228{
4229 return m_matrixPKSIZE->multDiag(x.internal()->values(), y.internal()->values());
4230}
4231
4232template <typename ValueT>
4233void SYCLBEllPackMatrix<ValueT>::multDiag(SYCLVector<ValueType>& y) const
4234{
4235 return m_matrixPKSIZE->multDiag(y.internal()->values());
4236}
4237
4238template <typename ValueT>
4239void SYCLBEllPackMatrix<ValueT>::computeDiag(SYCLVector<ValueType>& y) const
4240{
4241 auto block_size = blockSize() ;
4242 if((y.blockSize()==1)||(y.blockSize()==block_size))
4243 return m_matrixPKSIZE->computeDiag(y.internal()->values());
4244 else if(y.blockSize()==block_size*block_size)
4245 return m_matrixPKSIZE->computeBlockDiag(y.internal()->values());
4246 else
4247 throw Arccore::FatalErrorException(A_FUNCINFO, "Matrix and Vector Block Size are not compatibility");
4248}
4249
4250template <typename ValueT>
4251void SYCLBEllPackMatrix<ValueT>::multInvDiag(SYCLVector<ValueType>& y) const
4252{
4253 return m_matrixPKSIZE->multInvDiag(y.internal()->values());
4254}
4255
4256template <typename ValueT>
4257void SYCLBEllPackMatrix<ValueT>::computeInvDiag(SYCLVector<ValueType>& y) const
4258{
4259 auto block_size = blockSize() ;
4260 if((y.blockSize()==1)||(y.blockSize()==block_size))
4261 return m_matrixPKSIZE->computeInvDiag(y.internal()->values());
4262 else if(y.blockSize()==block_size*block_size)
4263 return m_matrixPKSIZE->computeInvBlockDiag(y.internal()->values());
4264 else
4265 throw Arccore::FatalErrorException(A_FUNCINFO, "Matrix and Vector Block Size are not compatibility");
4266}
4267
4268template <typename ValueT>
4269void SYCLBEllPackMatrix<ValueT>::scal(SYCLVector<ValueType> const& diag)
4270{
4271 return m_matrixPKSIZE->scal(diag.internal()->values());
4272}
4273
4274
4275template <typename ValueT>
4276void SYCLBEllPackMatrix<ValueT>::copy(SYCLBEllPackMatrix<ValueT> const& matrix)
4277{
4278 auto block_size = blockSize() ;
4279 initMatrix(matrix.m_parallel_mng,
4280 matrix.m_local_offset,
4281 matrix.m_global_size,
4282 matrix.m_profilePKSIZE->getNRows(),
4283 matrix.m_profilePKSIZE->kcol(),
4284 matrix.m_profilePKSIZE->cols(),
4285 matrix.m_matrix_dist_info,
4286 block_size);
4287 if(block_size==matrix.blockSize())
4288 {
4289 if(m_is_parallel)
4290 m_matrixPKSIZE->setMatrixValues(matrix.m_matrixPKSIZE->m_values,
4291 (*matrix.m_matrixPKSIZE->m_ext_values));
4292 else
4293 m_matrixPKSIZE->setMatrixValues(matrix.m_matrixPKSIZE->m_values);
4294 }
4295 else
4296 {
4297 assert(m_profilePKSIZE.get());
4298 assert(m_matrixPKSIZE.get());
4299 auto nb_blocks = m_profilePKSIZE->getBlockNnz();
4300 if(m_is_parallel)
4301 m_matrixPKSIZE->copy(nb_blocks,
4302 block_size,
4303 matrix.m_matrixPKSIZE->m_values,
4304 (*matrix.m_matrixPKSIZE->m_ext_values),
4305 matrix.blockSize()) ;
4306 else
4307 m_matrixPKSIZE->copy(nb_blocks,
4308 block_size,
4309 matrix.m_matrixPKSIZE->m_values,
4310 matrix.blockSize()) ;
4311 }
4312}
4313
4314
4315template <typename ValueT>
4316void SYCLBEllPackMatrix<ValueT>::copy(SYCLBEllPackMatrix<ValueT> const& matrix,
4317 ConstArrayView<Integer> filter,
4318 UniqueArray<Integer>& parent_lids)
4319{
4320 m_parallel_mng = distribution().parallelMng();
4321 if(m_parallel_mng)
4322 {
4323 m_nproc = m_parallel_mng->commSize();
4324 m_myrank = m_parallel_mng->commRank();
4325 }
4326 else
4327 {
4328 m_nproc = 1;
4329 m_myrank = 0;
4330 }
4331
4332 m_send_policy = matrix.m_send_policy;
4333 m_recv_policy = matrix.m_recv_policy;
4334 m_trace = matrix.m_trace;
4335
4336 m_local_size = std::accumulate(filter.begin(), filter.end(), 0);
4337 UniqueArray<Integer> new_offset(m_nproc + 1);
4338 new_offset[0] = 0;
4339 if(m_nproc>1)
4340 {
4341 m_is_parallel = true;
4342 Arccore::MessagePassing::mpAllGather(m_parallel_mng,
4343 ConstArrayView<Integer>(1,&m_local_size),
4344 ArrayView<Integer>(m_nproc,&new_offset[1]));
4345 for (Integer i = 1; i <= m_nproc; ++i)
4346 new_offset[i] += new_offset[i - 1];
4347 }
4348 else
4349 {
4350 new_offset[1] = m_local_size;
4351 m_is_parallel = false;
4352 }
4353
4354 m_local_offset = new_offset[m_myrank];
4355 m_global_size = new_offset[m_nproc];
4356 m_own_row_distribution.reset(new VectorDistribution(m_global_size,m_local_size,m_parallel_mng));
4357 if(m_is_parallel)
4358 {
4359 UniqueArray<Integer> all_filter(matrix.m_local_size + matrix.m_ghost_size);
4360 std::copy(filter.begin(),filter.end(),all_filter.begin());
4361 /*
4362 using SendRecvOp = SimpleCSRInternal::SendRecvOp<Integer> ;
4363 auto op = SendRecvOp(all_filter.unguardedBasePointer(),
4364 matrix.m_matrix_dist_info.m_send_info,
4365 matrix.m_send_policy,
4366 all_filter.unguardedBasePointer(),
4367 matrix.m_matrix_dist_info.m_recv_info,
4368 matrix.m_recv_policy,
4369 m_parallel_mng,
4370 nullptr);
4371 op.start();
4372 op.end();
4373 */
4374 m_ghost_size = std::accumulate(all_filter.begin()+matrix.m_local_size, all_filter.end(), 0);
4375
4376
4377 parent_lids.resize(m_local_size+m_ghost_size) ;
4378 UniqueArray<Integer> old2new_uid(matrix.m_local_size + matrix.m_ghost_size);
4379 old2new_uid.fill(-1) ;
4380 Integer irow = 0 ;
4381 for (Integer i = 0; i < matrix.m_local_size; ++i)
4382 {
4383 if (all_filter[i] == 1)
4384 {
4385 parent_lids[irow] = i ;
4386 old2new_uid[i] = m_local_offset + irow ;
4387 ++irow;
4388 }
4389 }
4390 assert(irow==m_local_size) ;
4391 /*
4392 {
4393 using SendRecvOp = SimpleCSRInternal::SendRecvOp<Integer> ;
4394 auto op = SendRecvOp( old2new_uid.unguardedBasePointer(),
4395 matrix.m_matrix_dist_info.m_send_info,
4396 matrix.m_send_policy,
4397 old2new_uid.unguardedBasePointer(),
4398 matrix.m_matrix_dist_info.m_recv_info,
4399 matrix.m_recv_policy,
4400 m_parallel_mng,
4401 nullptr);
4402 op.start();
4403 op.end();
4404 }*/
4405
4406 for (Integer i = matrix.m_local_size; i < matrix.m_local_size + matrix.m_ghost_size; ++i)
4407 {
4408 if (all_filter[i] == 1)
4409 {
4410 parent_lids[irow] = i ;
4411 ++irow;
4412 }
4413 }
4414 assert(irow==m_local_size+m_ghost_size) ;
4415 /*
4416 m_matrix.computeSubMatrix(parent_lids,
4417 old2new_uid,
4418 matrix.m_local_offset,
4419 matrix.m_local_size,
4420 matrix.m_matrix.m_values,
4421 matrix.getCSRProfile().getRowOffset(),
4422 matrix.m_matrix_dist_info.m_cols,
4423 matrix.m_matrix_dist_info.m_local_row_size,
4424 all_filter,
4425 new_offset,
4426 m_myrank);*/
4427 }
4428 else
4429 {
4430 parent_lids.resize(m_local_size) ;
4431 UniqueArray<Integer> old2new_uid(matrix.m_local_size);
4432 old2new_uid.fill(-1) ;
4433 Integer irow = 0 ;
4434 for (Integer i = 0; i < matrix.m_local_size; ++i)
4435 {
4436 if (filter[i] == 1)
4437 {
4438 parent_lids[irow] = i ;
4439 old2new_uid[i] = irow ;
4440 ++irow;
4441 }
4442 }
4443 assert(irow==m_local_size) ;
4444 /*
4445 m_matrix.computeSubMatrix(parent_lids,
4446 old2new_uid,
4447 matrix.m_local_size,
4448 matrix.m_matrix.m_values,
4449 matrix.getCSRProfile().getRowOffset(),
4450 matrix.getCSRProfile().getCols(),
4451 matrix.m_local_offset,
4452 filter,
4453 new_offset,
4454 m_myrank);*/
4455 }
4456
4457 //m_matrix_dist_info.compute(m_nproc,new_offset,m_myrank,m_parallel_mng,*m_matrix.m_profile);
4458}
4459
4460template <typename ValueT>
4462allocateDevicePointers(std::size_t nrows,
4463 std::size_t nnz,
4464 int** rows,
4465 int** ncols,
4466 int** cols,
4467 ValueT** values) const
4468{
4469 auto env = SYCLEnv::instance() ;
4470 auto max_num_treads = env->maxNumThreads() ;
4471 auto& queue = env->internal()->queue() ;
4472 auto rows_ptr = malloc_device<IndexType>(nrows, queue);
4473 auto ncols_ptr = malloc_device<IndexType>(nrows, queue);
4474 auto cols_ptr = malloc_device<IndexType>(nnz, queue);
4475 auto values_ptr = malloc_device<ValueT>(nnz, queue);
4476
4477 *rows = rows_ptr ;
4478 *ncols = ncols_ptr ;
4479 *cols = cols_ptr ;
4480 *values = values_ptr ;
4481}
4482
4483template <typename ValueT>
4485freeDevicePointers(int* rows, int* ncols, int* cols, ValueT* values) const
4486{
4487 auto env = SYCLEnv::instance() ;
4488 auto& queue = env->internal()->queue() ;
4489 sycl::free(rows,queue) ;
4490 sycl::free(ncols,queue) ;
4491 sycl::free(cols,queue) ;
4492 sycl::free(values,queue) ;
4493}
4494
4495
4496template <typename ValueT>
4498copyDevicePointers(std::size_t nrows,
4499 std::size_t nnz,
4500 int* rows,
4501 int* ncols,
4502 int* cols,
4503 ValueT* values) const
4504{
4505 m_matrixPKSIZE->copyDevicePointers(m_local_offset,nrows,nnz,rows,ncols,cols,values) ;
4506}
4507
4508
4509template <typename ValueT>
4510SYCLBEllPackMatrix<ValueT>::HCSRView
4511SYCLBEllPackMatrix<ValueT>::hcsrView(BackEnd::Memory::eType memory, int nrows, int nnz) const
4512{
4513 return HCSRView(this,memory, nrows, nnz) ;
4514}
4515/*---------------------------------------------------------------------------*/
4516
4517template class ALIEN_EXPORT SYCLBEllPackMatrix<double>;
4518template class ALIEN_EXPORT BEllPackStructInfo<PKSIZE, Integer>;
4519template class ALIEN_EXPORT SYCLInternal::MatrixInternal<double,PKSIZE>;
4520
4521
4522//template bool Alien::SYCLInternal::MatrixInternal<double,1014>::setMatrixValues(Alien::SYCLInternal::MatrixInternal<double,1014>::ValueBufferType& buffer) ;
4523// to force instanciation may be there is a better way to do it
4524void force_int_instance()
4525{
4527 SYCLBEllPackMatrix<double>::MatrixInternalType::ValueBufferType buffer{sycl::range<1>(10)} ;
4528 matrix.internal()->setMatrixValues(buffer) ;
4529 std::size_t pack_size =SYCLBEllPackMatrix<double>::MatrixInternalType::ellpack_size ;
4530}
4531/*---------------------------------------------------------------------------*/
4532
4533} // namespace Alien
4534
4535/*---------------------------------------------------------------------------*/
IMatrixImpl(const MultiMatrixImpl *multi_impl, BackEndId backend="")
Constructor.
Multi matrices representation container.
Computes a vector distribution.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17