Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SYCLVector.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#pragma once
9
10#include <vector>
11#include <iostream>
12
16#include <alien/data/ISpace.h>
17#include <alien/kernels/sycl/SYCLBackEnd.h>
18#include <alien/kernels/sycl/SYCLPrecomp.h>
19
20/*---------------------------------------------------------------------------*/
21
22namespace Alien
23{
24
25namespace SYCLInternal
26{
27 template <typename ValueT>
28 class VectorInternal;
29}
30
31template <typename ValueT>
32class ALIEN_EXPORT SYCLVector : public IVectorImpl
33{
34 public:
35 typedef ValueT ValueType;
36 typedef Integer IndexType;
37
38 typedef SYCLInternal::VectorInternal<ValueType> VectorInternal;
39
42
44 SYCLVector(const MultiVectorImpl* multi_impl) ;
45
46 virtual ~SYCLVector() ;
47
48 //virtual ~SYCLVector();
49
50 VectorInternal* internal()
51 {
52 return m_internal.get();
53 }
54
55 VectorInternal const* internal() const
56 {
57 return m_internal.get();
58 }
59
60 Integer blockSize() const
61 {
62 if (block())
63 {
64 return block()->size();
65 }
66 else if (vblock()) {
67 return -1 ;
68 }
69 else {
70 return m_own_block_size ;
71 }
72 }
73
74 void setBlockSize(Integer block_size)
75 {
76 if(this->m_multi_impl)
77 const_cast<MultiVectorImpl*>(this->m_multi_impl)->setBlockInfos(block_size) ;
78 else
79 m_own_block_size = block_size ;
80 }
81
82 Integer getAllocSize() const
83 {
84 return Integer(m_local_size*blockSize());
85 }
86
87 void allocate();
88
89 void resize(Integer alloc_size) const;
90
91 void clear();
92
93 void init(const VectorDistribution& dist, const bool need_allocate)
94 {
95 //alien_debug([&] { cout() << "Initializing SYCLVector " << this; });
96 if (this->m_multi_impl) {
97 m_local_size = dist.localSize();
98 }
99 else
100 {
101 // Not associated vector
102 m_own_distribution = dist;
103 m_local_size = m_own_distribution.localSize();
104 }
105 if (need_allocate) {
106 allocate();
107 }
108 //alien_debug([&] { cout() << "After Initializing SYCLVector " << m_local_size<<" "<<m_h_values.size(); });
109 //Universe().traceMng()->flush() ;
110 }
111
112 void init(const VectorDistribution& dist, Integer block_size, const bool need_allocate)
113 {
114 alien_debug([&] { cout() << "Initializing SYCLVector " << this; });
115 if(blockSize()==1)
116 setBlockSize(block_size) ;
117 if (this->m_multi_impl) {
118 if (this->vblock()) {
119 m_vblock.reset(new VBlockImpl(*this->vblock(), this->distribution()));
120 }
121 m_local_size = this->distribution().localSize();
122 }
123 else {
124 // Not associated vector
125 m_own_distribution = dist;
126 m_local_size = m_own_distribution.localSize();
127 }
128 if (need_allocate) {
129 allocate();
130 }
131 //alien_debug([&] { cout() << "After Initializing SYCLVector " << m_local_size<<" "<<m_h_values.size(); });
132 //Universe().traceMng()->flush() ;
133 }
134
136 {
137 if (this->m_multi_impl)
139 else
140 return m_own_distribution;
141 }
142
143 Arccore::Integer scalarizedLocalSize() const
144 {
145 if (this->m_multi_impl)
147 else
148 return m_own_distribution.localSize()*m_own_block_size;
149 }
150
151 Arccore::Integer scalarizedGlobalSize() const
152 {
153 if (this->m_multi_impl)
155 else
156 return m_own_distribution.globalSize()*m_own_block_size;
157 }
158
159 Arccore::Integer scalarizedOffset() const
160 {
161 if (this->m_multi_impl)
163 else
164 return m_own_distribution.offset()*m_own_block_size;
165 }
166
167 ValueType* getDataPtr() { return m_h_values.data(); }
168 ValueType* data() { return m_h_values.data(); }
169
170 ValueType const* getDataPtr() const { return m_h_values.data(); }
171 ValueType const* data() const { return m_h_values.data(); }
172 ValueType const* getAddressData() const { return m_h_values.data(); }
173
174 void initDevicePointers(int** rows, ValueType** values) const ;
175
176 static void allocateDevicePointers(std::size_t local_size,
177 int** rows,
178 ValueType** values);
179
180 static void allocateDevicePointers(std::size_t local_size,
181 ValueType** values);
182
183 static void freeDevicePointers(int* rows, ValueType* values);
184
185 static void freeDevicePointers(ValueType* values);
186
187 static void initDevicePointers(std::size_t local_size,
188 ValueType const* host_values,
189 int** rows,
190 ValueType** values) ;
191
192 static void copyDeviceToHost(std::size_t local_size,
193 ValueType const* device_values,
194 ValueType* host_values) ;
195
196 template <typename LambdaT>
197 void apply(LambdaT const& lambda)
198 {
199 auto block_size = blockSize() ;
200 for (std::size_t i = 0; i < m_local_size*block_size; ++i) {
201 m_h_values[i] = lambda(i);
202 }
203 setValuesFromHost();
204 }
205
206 void setValues(std::size_t size, ValueType const* ptr);
207
208 void setValuesFromHost(std::size_t size, ValueType const* ptr);
209
210 void setValuesFromDevice(std::size_t size, ValueType const* ptr);
211
212 void setValuesFromHost();
213
214 void copyValuesTo(std::size_t size, ValueType* ptr) const;
215
216 void copyValuesToDevice(std::size_t size, ValueType* ptr) const;
217
218 void copyValuesToDevice(ValueType* ptr) const
219 {
220 copyValuesToDevice(m_local_size,ptr) ;
221 }
222
223 void pointWiseMult(SYCLVector const& y, SYCLVector& z) const ;
224
225 // FIXME: not implemented !
226 template <typename E>
227 SYCLVector& operator=(E const& expr);
228
229 private:
230 // clang-format off
231 //mutable VectorInternal* m_internal = nullptr;
232 mutable std::unique_ptr<VectorInternal> m_internal ;
233 mutable std::vector<ValueType> m_h_values ;
234 std::size_t m_local_size = 0;
235 Integer m_own_block_size = 1 ;
236 VectorDistribution m_own_distribution ;
237 mutable std::unique_ptr<VBlockImpl> m_vblock ;
238 // clang-format on
239};
240
241//extern template class SYCLVector<Real>;
242} // namespace Alien
ISpace.h.
IVectorImpl.h.
MultiVectorImpl.h.
VBlockOffsets.h.
virtual Arccore::Integer scalarizedOffset() const
Get the "scalarized" offset.
const MultiVectorImpl * m_multi_impl
Pointer on vectors implementations.
virtual const VectorDistribution & distribution() const
Get the distribution of the vector.
IVectorImpl(const MultiVectorImpl *multi_impl, BackEndId backend="")
Constructor.
virtual Arccore::Integer scalarizedGlobalSize() const
Get the "scalarized" global size.
virtual Arccore::Integer scalarizedLocalSize() const
Get the "scalarized" local size.
void init(const VectorDistribution &dist, const bool need_allocate)
Initialize vector datas.
Definition SYCLVector.h:93
SYCLVector()
Constructeur sans association un MultiImpl.
Definition SYCLVector.cc:23
void clear()
Wipe out internal data.
Definition SYCLVector.cc:56
const VectorDistribution & distribution() const
Get the distribution of the vector.
Definition SYCLVector.h:135
SYCLVector(const MultiVectorImpl *multi_impl)
Constructeur avec association ? un MultiImpl.
Definition SYCLVector.cc:29
Arccore::Integer scalarizedOffset() const
Get the "scalarized" offset.
Definition SYCLVector.h:159
Arccore::Integer scalarizedLocalSize() const
Get the "scalarized" local size.
Definition SYCLVector.h:143
Arccore::Integer scalarizedGlobalSize() const
Get the "scalarized" global size.
Definition SYCLVector.h:151
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