Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
HCSRVectorInternal.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#include <alien/kernels/sycl/SYCLPrecomp.h>
9
10#ifdef USE_SYCL2020
11#include <sycl/sycl.hpp>
12#else
13#include <CL/sycl.hpp>
14#endif
15
16#include <span>
17
18#include "HCSRVector.h"
19
20#include "SYCLEnv.h"
21#include "SYCLEnvInternal.h"
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25using namespace Arccore;
26
27namespace Alien
28{
29
30 namespace HCSRInternal
31 {
32
33 /*---------------------------------------------------------------------------*/
34
35 #ifndef USE_SYCL2020
36 using namespace cl ;
37 #endif
38
39 template <typename ValueT = Real>
40 class VectorInternal
41 {
42 public:
43 // clang-format off
44 typedef ValueT ValueType;
45 typedef VectorInternal<ValueType> ThisType;
46 typedef sycl::buffer<ValueType, 1> ValueBufferType;
47 // clang-format on
48
49 public:
50 VectorInternal(std::size_t size)
51 : m_values(sycl::range<1>(size))
52 {}
53
54 virtual ~VectorInternal() {}
55
56 /*
57 std::vector<ValueType>& hostValues() {
58 return m_h_values ;
59 }
60
61 std::vector<ValueType> const& hostValues() const {
62 return m_h_values ;
63 }*/
64
65 ValueBufferType& values()
66 {
67 return m_values;
68 }
69
70
71 ValueBufferType& values() const
72 {
73 return m_values;
74 }
75
76 void copyValuesToHost(std::size_t size, ValueT* ptr)
77 {
78 auto h_values = m_values.get_host_access();
79 for (std::size_t i = 0; i < size; ++i)
80 ptr[i] = h_values[i];
81 }
82
83 void copyValuesToDevice(std::size_t size, ValueT* ptr) const
84 {
85 auto env = SYCLEnv::instance() ;
86 auto& queue = env->internal()->queue() ;
87 auto max_num_treads = env->maxNumThreads() ;
88
89 queue.submit( [&](sycl::handler& cgh)
90 {
91 auto access_x = m_values.template get_access<sycl::access::mode::read>(cgh);
92 std::size_t y_length = size ;
93 cgh.parallel_for<class init_vector_ptr>(sycl::range<1>{max_num_treads}, [=] (sycl::item<1> itemId)
94 {
95 auto id = itemId.get_id(0);
96 for (auto i = id; i < y_length; i += itemId.get_range()[0])
97 ptr[i] = access_x[i];
98 });
99 });
100 queue.wait() ;
101 }
102
103 //mutable std::vector<ValueType> m_h_values ;
104 mutable ValueBufferType m_values;
105 };
106
107 /*---------------------------------------------------------------------------*/
108
109 } // namespace Alien::SYCLInternal
110 template <typename ValueT>
112 : IVectorImpl(nullptr, AlgebraTraits<BackEnd::tag::hcsr>::name())
113 , m_local_size(0)
114 {}
115
117 template <typename ValueT>
119 : IVectorImpl(multi_impl, AlgebraTraits<BackEnd::tag::hcsr>::name())
120 , m_local_size(0)
121 {}
122
123/*---------------------------------------------------------------------------*/
124template <typename ValueT>
125void HCSRVector<ValueT>::allocate()
126{
127 m_internal.reset(new InternalType(m_local_size)) ;
128}
129
130template <typename ValueT>
131void HCSRVector<ValueT>::resize(Integer alloc_size)
132{
133 m_local_size = alloc_size ;
134 m_internal.reset(new InternalType(m_local_size)) ;
135}
136
137/*
138template <typename ValueT>
139ValueT* HCSRVector<ValueT>::getDataPtr()
140{
141 if(m_internal.get())
142 return m_internal->hostValues().data() ;
143 else
144 return nullptr ;
145}
146
147template <typename ValueT>
148ValueT* HCSRVector<ValueT>::data()
149{
150 if(m_internal.get())
151 return m_internal->hostValues().data() ;
152 else
153 return nullptr ;
154}
155
156template <typename ValueT>
157ValueT const* HCSRVector<ValueT>::getDataPtr() const
158{
159 if(m_internal.get())
160 return m_internal->hostValues().data() ;
161 else
162 return nullptr ;
163}
164
165
166template <typename ValueT>
167ValueT const* HCSRVector<ValueT>::data() const
168{
169 if(m_internal.get())
170 return m_internal->hostValues().data() ;
171 else
172 return nullptr ;
173}
174
175
176template <typename ValueT>
177ValueT const* HCSRVector<ValueT>::getAddressData() const
178{
179 if(m_internal.get())
180 return m_internal->hostValues().data() ;
181 else
182 return nullptr ;
183}
184*/
185
186template <typename ValueT>
188 const bool need_allocate)
189{
190
191 alien_debug([&] { cout() << "Initializing HCSRVector " << this; });
192 if (this->m_multi_impl) {
193 m_local_size = this->scalarizedLocalSize();
194 }
195 else {
196 // Not associated vector
197 m_own_distribution = dist;
198 m_local_size = m_own_distribution.localSize();
199 }
200 if (need_allocate) {
201 allocate() ;
202 }
203}
204/*---------------------------------------------------------------------------*/
205
206} // namespace Alien
207
208/*---------------------------------------------------------------------------*/
209/*---------------------------------------------------------------------------*/
void init(const VectorDistribution &dist, const bool need_allocate) override
Initialize vector datas.
HCSRVector()
Constructeur sans association ? un MultiImpl.
Arccore::Integer scalarizedLocalSize() const override
Get the "scalarized" local size.
Definition HCSRVector.h:65
const MultiVectorImpl * m_multi_impl
Pointer on vectors implementations.
IVectorImpl(const MultiVectorImpl *multi_impl, BackEndId backend="")
Constructor.
Computes a vector distribution.
Arccore::Integer localSize() const
Get the local size.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17