Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
MultiMatrixImpl.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 MultiMatrixImpl.h
21 * \brief MultiMatrixImpl.h
22 */
23
24#pragma once
25
26#include <map>
27
28#include <alien/core/backend/BackEnd.h>
33#include <alien/data/Space.h>
35#include <alien/utils/ObjectWithLock.h>
36#include <alien/utils/Precomp.h>
37#include <alien/utils/UserFeatureMng.h>
38#include <alien/utils/time_stamp/TimestampMng.h>
39#include <cstdlib>
40#include <iostream>
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
45namespace Alien
46{
47
48/*---------------------------------------------------------------------------*/
49/*---------------------------------------------------------------------------*/
50
51/*!
52 * \ingroup impl
53 * \brief Multi matrices representation container
54 *
55 * This class allows to store and access several implementations of the same matrix.
56 * It also stores all shared information between those implementations such as
57 * distribution, or block information. It provides two accessors, one to access the matrix
58 * in read-only mode, that does not increase the timestamp and one in read-write mode, that can
59 * increase the timestamp. The former implementation might therefore become the up to date
60 * matrix.
61 *
62 * While requesting a matrix in a specific format, if the up to date matrix is not in that
63 * specific format, the up-to-date matrix will be converted, provided a converter exists.
64 */
65class ALIEN_EXPORT MultiMatrixImpl : public TimestampMng
66, public UserFeatureMng
67, public ObjectWithTrace
68, public ObjectWithLock
69{
70 public:
71 //! Default constructor
73
74 /*!
75 * \brief Constructor
76 * \param[in] row_space The row space of the matrix
77 * \param[in] col_space The column space of the matrix
78 * \param[in] dist The distribution of the matrix
79 */
80 MultiMatrixImpl(std::shared_ptr<ISpace> row_space, std::shared_ptr<ISpace> col_space,
81 std::shared_ptr<MatrixDistribution> dist);
82
83 //! Free resources
84 virtual ~MultiMatrixImpl();
85
86 protected:
87 /*!
88 * \brief Copy constructor
89 * \param[in] impl The MultiMatrixImpl to copy
90 */
92
93 public:
94 /*!
95 * \brief Set uniform block information
96 * \param[in] block_size The size of the blocks
97 */
98 void setBlockInfos(Arccore::Integer block_size);
99
100 /*!
101 * \brief Set uniform block information
102 * \param[in] blocks The block data
103 */
104 void setBlockInfos(const Block* blocks);
105
106 /*!
107 * \brief Set variable block information
108 * \param[in] blocks The block data
109 */
110 void setBlockInfos(const VBlock* blocks);
111
112 /*!
113 * \brief Set variable row block information
114 * \param[in] blocks The row block data
115 */
116 void setRowBlockInfos(const VBlock* blocks);
117
118 /*!
119 * \brief Set variable col block information
120 * \param[in] blocks The col block data
121 */
122 void setColBlockInfos(const VBlock* blocks);
123
124 //! Free resources
125 void free();
126 //! Clear resources
127 void clear();
128
129 /*!
130 * \brief Get the row space associated to the matrix
131 * \returns The row space
132 */
133 const ISpace& rowSpace() const { return *m_row_space.get(); }
134
135 /*!
136 * \brief Get the col space associated to the matrix
137 * \returns The col space
138 */
139 const ISpace& colSpace() const { return *m_col_space.get(); }
140
141 /*!
142 * \brief Get the matrix distribution
143 * \returns The matrix distribution
144 */
145 const MatrixDistribution& distribution() const { return *m_distribution.get(); }
146
147 /*!
148 * \brief Get uniform block datas
149 * \returns The matrix block data, or a nullptr if the matrix is scalar or has variable
150 * blocks
151 */
152 const Block* block() const;
153
154 /*!
155 * \brief Get variable block datas
156 * \returns The matrix block data, or a nullptr if the matrix is scalar or has uniform
157 * blocks
158 */
159 const VBlock* vblock() const;
160
161 /*!
162 * \brief Get variable row block datas
163 * \returns The matrix row block data, or a nullptr if the matrix is scalar or has
164 * uniform blocks
165 */
166 const VBlock* rowBlock() const;
167
168 /*!
169 * \brief Get variable col block datas
170 * \returns The matrix col block data, or a nullptr if the matrix is scalar or has
171 * uniform blocks
172 */
173 const VBlock* colBlock() const;
174
175 public:
176 /*!
177 * \brief Get a specific matrix implementation
178 *
179 * Might induce a conversion, depending on the up to date and requested matrix
180 * implementation
181 *
182 * \returns The up to date matrix in the requested implementation
183 */
184 template <typename tag>
185 const typename AlgebraTraits<tag>::matrix_type& get() const;
186
187 /*!
188 * \brief Get a specific matrix implementation
189 *
190 * Might induce a conversion, depending on the up to date and requested matrix
191 * implementation
192 *
193 * \param[in] update_stamp Whether or not the timestamp should be increased or not
194 * \returns The up to date matrix in the requested implementation
195 */
196 template <typename tag>
197 typename AlgebraTraits<tag>::matrix_type& get(const bool update_stamp);
198
199 //! Release a matrix implementation
200 template <typename tag>
201 void release() const;
202
203 /*!
204 * \brief Clone this object
205 * \return A clone of this object
206 */
207 MultiMatrixImpl* clone() const;
208
209 // TOCHECK : should be removed or not ?
210 // void setTimestamp(Int64 stamp)
211 // {
212 // TimestampMng::setTimestamp(stamp);
213 // }
214
215 private:
216 /*!
217 * \brief Get a specific matrix implementation
218 * \param[in] backend The id of the specific implementation
219 * \returns The matrix in the requested format
220 */
221 template <typename matrix_type>
222 IMatrixImpl*& getImpl(BackEndId backend) const;
223
224 /*!
225 * \brief Update a matrix implementation
226 * \param[in] target The targeted implementation
227 */
228 void updateImpl(IMatrixImpl* target) const;
229
230 protected:
231 /*!
232 * \brief Insert a matrix implementation in the multi matrix container
233 * \param[in] backend The implementation backend id
234 * \param[in] m The matrix to insert
235 */
236 template <typename matrix_type>
237 void insert(BackEndId backend, matrix_type* m);
238
239 private:
240 //! The matrix row space
241 std::shared_ptr<ISpace> m_row_space;
242 //! The matrix column space
243 std::shared_ptr<ISpace> m_col_space;
244 //! The matrix distribution
245 std::shared_ptr<MatrixDistribution> m_distribution;
246 //! The type of the matrix container
247 typedef std::map<BackEndId, IMatrixImpl*> MultiMatrixImplMap;
248 //! The matrices container
249 mutable MultiMatrixImplMap m_impls2;
250 //! The uniform block datas
251 std::shared_ptr<Block> m_block;
252 //! The variable row block datas
253 std::shared_ptr<VBlock> m_rows_block;
254 //! The variable col block datas
255 std::shared_ptr<VBlock> m_cols_block;
256};
257
258/*---------------------------------------------------------------------------*/
259/*---------------------------------------------------------------------------*/
260
261template <typename tag>
262const typename AlgebraTraits<tag>::matrix_type&
264{
265 // TOCHECK : to be removed or not ?
266 // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access"));
267 // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access"));
269 IMatrixImpl*& impl2 = getImpl<matrix_type>(AlgebraTraits<tag>::name());
270 ALIEN_ASSERT(
271 (impl2->backend() == AlgebraTraits<tag>::name()), ("Inconsistent backend"));
272 updateImpl(impl2);
273 return *dynamic_cast<matrix_type*>(impl2);
274}
275
276/*---------------------------------------------------------------------------*/
277/*---------------------------------------------------------------------------*/
278
279template <typename tag>
281MultiMatrixImpl::get(const bool update_stamp)
282{
283 // TOCHECK : to be removed or not ?
284 // ALIEN_ASSERT(!m_row_space.isNull(), ("Null row space matrix access"));
285 // ALIEN_ASSERT(!m_col_space.isNull(), ("Null col space matrix access"));
287 IMatrixImpl*& impl2 = getImpl<matrix_type>(AlgebraTraits<tag>::name());
288 ALIEN_ASSERT(
289 (impl2->backend() == AlgebraTraits<tag>::name()), ("Inconsistent backend"));
290 updateImpl(impl2);
291 if (update_stamp)
292 impl2->updateTimestamp();
293 return *dynamic_cast<matrix_type*>(impl2);
294}
295
296/*---------------------------------------------------------------------------*/
297/*---------------------------------------------------------------------------*/
298
299template <typename tag>
301{
302 auto finder = m_impls2.find(AlgebraTraits<tag>::name());
303 if (finder == m_impls2.end())
304 return; // already freed
305 delete finder->second, finder->second = NULL;
306}
307
308/*---------------------------------------------------------------------------*/
309/*---------------------------------------------------------------------------*/
310
311template <typename matrix_type>
313MultiMatrixImpl::getImpl(BackEndId backend) const
314{
315 auto inserter = m_impls2.insert(MultiMatrixImplMap::value_type(backend, NULL));
316 IMatrixImpl*& impl2 = inserter.first->second;
317 if (impl2 == NULL) {
318 matrix_type* new_impl = new matrix_type(this); // constructeur associé à un multi-impl
319 // new_impl->init(*m_row_space.get(),
320 // *m_col_space.get(),
321 // m_distribution);
322 impl2 = new_impl;
323 }
324 return impl2;
325}
326
327/*---------------------------------------------------------------------------*/
328/*---------------------------------------------------------------------------*/
329
330template <typename matrix_type>
331void MultiMatrixImpl::insert(BackEndId backend, matrix_type* m)
332{
333 if (m_impls2.find(backend) != m_impls2.end()) {
334 alien_fatal([&] { cout() << "try to insert already inserted value"; });
335 }
336 m_impls2[backend] = m;
337}
338
339/*---------------------------------------------------------------------------*/
340/*---------------------------------------------------------------------------*/
341
342} // namespace Alien
343
344/*---------------------------------------------------------------------------*/
345/*---------------------------------------------------------------------------*/
Block.h.
IMatrixImpl.h.
MatrixConverterRegisterer.h.
MatrixDistribution.h.
Space.h.
VBlock.h.
Block elements for block matrices.
Definition Block.h:45
Interface to handle abstract matrices implementation.
Definition IMatrixImpl.h:47
virtual BackEndId backend() const
Definition IMatrixImpl.h:94
Interface for algebraic space objects.
Definition ISpace.h:44
Computes a matrix distribution.
Multi matrices representation container.
void setColBlockInfos(const VBlock *blocks)
Set variable col block information.
void insert(BackEndId backend, matrix_type *m)
Insert a matrix implementation in the multi matrix container.
void setRowBlockInfos(const VBlock *blocks)
Set variable row block information.
void release() const
Release a matrix implementation.
MultiMatrixImpl()
Default constructor.
void clear()
Clear resources.
const MatrixDistribution & distribution() const
Get the matrix distribution.
void free()
Free resources.
const ISpace & rowSpace() const
Get the row space associated to the matrix.
const AlgebraTraits< tag >::matrix_type & get() const
Get a specific matrix implementation.
const ISpace & colSpace() const
Get the col space associated to the matrix.
void setBlockInfos(Arccore::Integer block_size)
Set uniform block information.
void updateTimestamp()
Met à jour le timestamp.
Definition Timestamp.cc:50
Variable size block elements for block matrices.
Definition VBlock.h:46
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17