Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
MultiVectorImpl.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 IFPEN-CEA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 */
18
19/*!
20 * \file MultiVectorImpl.h
21 * \brief MultiVectorImpl.h
22 */
23
24#pragma once
25
26#include <iostream>
27#include <map>
28
29#include <alien/core/backend/BackEnd.h>
34#include <alien/data/Space.h>
36#include <alien/utils/Precomp.h>
37#include <alien/utils/UserFeatureMng.h>
38#include <alien/utils/time_stamp/TimestampMng.h>
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
43namespace Alien
44{
45
46/*---------------------------------------------------------------------------*/
47/*---------------------------------------------------------------------------*/
48
49class ALIEN_EXPORT MultiVectorImpl : public TimestampMng
50, public UserFeatureMng
51, public ObjectWithTrace
52{
53 /*!
54 * \ingroup impl
55 * \brief Multi vectors representation container
56 *
57 * This class allows to store and access several implementations of the same vector.
58 * It also stores all shared information between those implementation such as
59 * distribution, or block information. It provided two accessors, one to access the
60 * vector in read-only, that do not increase the timestamp and one in read-write mode,
61 * that can increase the timestamp. The former implementation might therefore become the
62 * up to date vector.
63 *
64 * While requesting a vector in a specific format, if the up to date vector is not in
65 * that specific format, the up to vector matrix will be converted, provided a converter
66 * exists.
67 */
68 public:
69 //! Default constructor
71
72 /*!
73 * \brief Constructor
74 * \param[in] space The space of the vector
75 * \param[in] dist The distribution of the vector
76 */
78 std::shared_ptr<ISpace> space, std::shared_ptr<VectorDistribution> dist);
79
80 //! Free resources
81 ~MultiVectorImpl() override;
82
83 protected:
84 /*!
85 * \brief Copy constructor
86 * \param[in] impl The MultiVectorImpl to copy
87 */
89
90 public:
91 MultiVectorImpl(MultiVectorImpl&& impl) = delete;
92 void operator=(const MultiVectorImpl&) = delete;
93 void operator=(MultiVectorImpl&&) = delete;
94
95 public:
96 /*!
97 * \brief Set uniform block information
98 * \param[in] blocks The block data
99 */
100 void setBlockInfos(const Block* blocks);
101
102 /*!
103 * \brief Set variable block information
104 * \param[in] blocks The block data
105 */
106 void setBlockInfos(const VBlock* blocks);
107
108 /*!
109 * \brief Set uniform block information
110 * \param[in] block_size The size of the blocks
111 */
112 void setBlockInfos(Arccore::Integer block_size);
113
114 //! Free resources
115 void free();
116
117 //! Clear resources
118 void clear();
119
120 /*!
121 * \brief Get the space associated with the vector
122 * \returns The space
123 */
124 const ISpace& space() const { return *m_space.get(); }
125
126 /*!
127 * \brief Get the vector distribution
128 * \returns The vector distribution
129 */
130 const VectorDistribution& distribution() const { return *m_distribution.get(); }
131
132 /*!
133 * \brief Get uniform block datas
134 * \returns The vector block data, or a nullptr if the vector is scalar or has variable
135 * blocks
136 */
137 const Block* block() const;
138
139 /*!
140 * \brief Get variable block datas
141 * \returns The vector block data or a nullptr if the vector is scalar or has uniform
142 * blocks
143 */
144 const VBlock* vblock() const;
145
146 public:
147 /*!
148 * \brief Clone this object
149 * \return A clone of this object
150 */
151 MultiVectorImpl* clone() const;
152
153 /*!
154 * \brief Get a specific vector implementation
155 *
156 * Might induce a conversion, depending on the up to date and requested vector
157 * implementation
158 *
159 * \returns The up to date vector in the requested implementation
160 */
161 template <typename tag>
162 const typename AlgebraTraits<tag>::vector_type& get() const;
163
164 /*!
165 * \brief Get a specific vector implementation
166 *
167 * Might induce a conversion, depending on the up to date and requested vector
168 * implementation
169 *
170 * \param[in] update_stamp Whether or not the timestamp should be increased or not
171 * \returns The up to date vector in the requested implementation
172 */
173 template <typename tag>
174 typename AlgebraTraits<tag>::vector_type& get(bool update_stamp);
175
176 //! Release a vector implementation
177 template <typename tag>
178 void release() const;
179
180 private:
181 /*!
182 * \brief Get a specific vector implementation
183 * \param[in] backend The id of the specific implementation
184 * \returns The vector in the requested format
185 */
186 template <typename vector_type>
187 IVectorImpl*& getImpl(BackEndId backend) const;
188
189 /*!
190 * \brief Update a vector implementation
191 * \param[in] target The targeted implementation
192 */
193 void updateImpl(IVectorImpl* target) const;
194
195 protected:
196 /*!
197 * \brief Insert a vector implementation in the multi vector container
198 * \param[in] backend The implementation backend id
199 * \param[in] v The vector to insert
200 */
201 template <typename vector_type>
202 void insert(const BackEndId& backend, vector_type* v);
203
204 private:
205 //! The vector space
206 std::shared_ptr<ISpace> m_space;
207 //! The vector distribution
208 std::shared_ptr<VectorDistribution> m_distribution;
209 //! The type of the vector container
210 typedef std::map<BackEndId, IVectorImpl*> MultiVectorImplMap;
211 //! The vectors container
212 mutable MultiVectorImplMap m_impls2;
213 //! The uniform block datas
214 std::shared_ptr<Block> m_block;
215 //! The variable block datas
216 std::shared_ptr<VBlock> m_variable_block;
217};
218
219/*---------------------------------------------------------------------------*/
220/*---------------------------------------------------------------------------*/
221
222template <typename tag>
223const typename AlgebraTraits<tag>::vector_type&
225{
227 IVectorImpl*& impl2 = getImpl<vector_type>(AlgebraTraits<tag>::name());
228 ALIEN_ASSERT(
229 (impl2->backend() == AlgebraTraits<tag>::name()), ("Inconsistent backend"));
230 updateImpl(impl2);
231 return *dynamic_cast<vector_type*>(impl2);
232}
233
234/*---------------------------------------------------------------------------*/
235/*---------------------------------------------------------------------------*/
236
237template <typename tag>
239MultiVectorImpl::get(const bool update_stamp)
240{
242 IVectorImpl*& impl2 = getImpl<vector_type>(AlgebraTraits<tag>::name());
243 ALIEN_ASSERT(
244 (impl2->backend() == AlgebraTraits<tag>::name()), ("Inconsistent backend"));
245 updateImpl(impl2);
246 if (update_stamp) {
247 impl2->updateTimestamp();
248 }
249 return *dynamic_cast<vector_type*>(impl2);
250}
251
252/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
254
255template <typename tag>
257{
258 auto finder = m_impls2.find(AlgebraTraits<tag>::name());
259 if (finder == m_impls2.end())
260 return; // already freed
261 delete finder->second, finder->second = NULL;
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267template <typename vector_type>
269MultiVectorImpl::getImpl(BackEndId backend) const
270{
271 auto inserter = m_impls2.insert(MultiVectorImplMap::value_type(backend, NULL));
272 IVectorImpl*& impl2 = inserter.first->second;
273 if (impl2 == NULL) {
274 auto new_impl = new vector_type(this); // constructeur associ� � un multi-impl
275 new_impl->init(*m_distribution.get(), true);
276 impl2 = new_impl;
277 }
278 return impl2;
279}
280
281/*---------------------------------------------------------------------------*/
282/*---------------------------------------------------------------------------*/
283
284template <typename vector_type>
285void MultiVectorImpl::insert(const BackEndId& backend, vector_type* v)
286{
287 if (m_impls2.find(backend) != m_impls2.end()) {
288 alien_fatal([&] { cout() << "try to insert already inserted value"; });
289 }
290 m_impls2[backend] = v;
291}
292
293/*---------------------------------------------------------------------------*/
294/*---------------------------------------------------------------------------*/
295
296} // namespace Alien
297
298/*---------------------------------------------------------------------------*/
299/*---------------------------------------------------------------------------*/
Block.h.
IVectorImpl.h.
Space.h.
VBlock.h.
VectorConverterRegisterer.h.
VectorDistribution.h.
Block elements for block matrices.
Definition Block.h:45
Interface for algebraic space objects.
Definition ISpace.h:44
Interface to handle abstract vectors implementation.
Definition IVectorImpl.h:47
virtual BackEndId backend() const
Definition IVectorImpl.h:95
void free()
Free resources.
void insert(const BackEndId &backend, vector_type *v)
Insert a vector implementation in the multi vector container.
void clear()
Clear resources.
const AlgebraTraits< tag >::vector_type & get() const
Get a specific vector implementation.
const VectorDistribution & distribution() const
Get the vector distribution.
void setBlockInfos(Arccore::Integer block_size)
Set uniform block information.
void release() const
Release a vector implementation.
void setBlockInfos(const Block *blocks)
Set uniform block information.
const ISpace & space() const
Get the space associated with the vector.
void updateTimestamp()
Met à jour le timestamp.
Definition Timestamp.cc:50
Variable size block elements for block matrices.
Definition VBlock.h:46
Computes a vector distribution.
MultiVectorImpl()
Multi vectors representation container.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17