Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
MultiVectorImpl.cc
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
23
24#include "MultiVectorImpl.h"
25
26#include <alien/kernels/simple_csr/SimpleCSRVector.h>
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31namespace Alien
32{
33
34using namespace Arccore;
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
43
44/*---------------------------------------------------------------------------*/
45/*---------------------------------------------------------------------------*/
46
48std::shared_ptr<ISpace> space, std::shared_ptr<VectorDistribution> distribution)
49: m_space(std::move(space))
51, m_block(nullptr)
52, m_variable_block(nullptr)
53{}
54
55/*---------------------------------------------------------------------------*/
56/*---------------------------------------------------------------------------*/
57
59: TimestampMng(impl)
60, m_space(impl.m_space)
62, m_block(nullptr)
63, m_variable_block(nullptr)
64{
65 if (impl.m_block)
66 m_block = impl.m_block;
67 if (impl.m_variable_block)
69}
70
71/*---------------------------------------------------------------------------*/
72/*---------------------------------------------------------------------------*/
73
78
79/*---------------------------------------------------------------------------*/
80/*---------------------------------------------------------------------------*/
81
83{
84 m_block = blocks->clone();
85}
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
91{
92 m_variable_block = blocks->clone();
93}
94
95/*---------------------------------------------------------------------------*/
96/*---------------------------------------------------------------------------*/
97
98void MultiVectorImpl::setBlockInfos(Integer block_size)
99{
100 m_block.reset(new Block(block_size));
101}
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
105
107{
108 for (MultiVectorImplMap::iterator i = m_impls2.begin(); i != m_impls2.end(); ++i) {
109 delete i->second;
110 i->second = NULL;
111 }
112 m_impls2.clear();
113}
114
115/*---------------------------------------------------------------------------*/
116/*---------------------------------------------------------------------------*/
117
119{
120 for (MultiVectorImplMap::iterator i = m_impls2.begin(); i != m_impls2.end(); ++i) {
121 i->second->clear();
122 }
123}
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
127
128const Block*
130{
131 return m_block.get();
132}
133
134/*---------------------------------------------------------------------------*/
135/*---------------------------------------------------------------------------*/
136
137const VBlock*
139{
140 return m_variable_block.get();
141}
142
143/*---------------------------------------------------------------------------*/
144/*---------------------------------------------------------------------------*/
145
148{
149 // Initialize muli-representation without data
150 auto impl = new MultiVectorImpl(*this);
151
152 // We get the last up to date implementation
153 typedef BackEnd::tag::simplecsr tag;
154 const SimpleCSRVector<Real>& vectorToClone = this->get<tag>();
155 // And clone it
156 SimpleCSRVector<Real>* vectorCloned = vectorToClone.cloneTo(impl);
157 vectorCloned->setTimestamp(impl, vectorToClone.timestamp());
158 vectorCloned->updateTimestamp();
159 impl->m_impls2.insert(
160 MultiVectorImplMap::value_type(AlgebraTraits<tag>::name(), vectorCloned));
161
162 // TOCHECK: to be removed or not ?
163 /* WARNING: this implementation is temporary. Later it should be implemented through a
164 clone() method for each kernel as written lower.
165 For now, we will convert the last up to date matrix in SimpleCSRMatrix, then clone
166 it.
167
168 for(std::map<BackEndId, IMatrixImpl*>::const_iterator it = m_impls2.begin(); it !=
169 m_impls2.end(); ++it)
170 impl->m_impls2.insert(MultiMatrixImplMap::value_type(it->first,
171 it->second->clone()));
172 */
173 return impl;
174}
175
176/*---------------------------------------------------------------------------*/
177/*---------------------------------------------------------------------------*/
178
180{
181 // If we are up to date, do nothing
182 if (timestamp() == target->timestamp()) {
183 return;
184 }
185
186 // Otherwise we are looking for a converter
187 for (auto& impl : m_impls2) {
188 auto* candidate = impl.second;
189 // if the timestamp is not good we keep looking
190 if (candidate->timestamp() != timestamp())
191 continue;
192 auto* converter =
193 VectorConverterRegisterer::getConverter(candidate->backend(), target->backend());
194 // If no converter is found, we continue
195 if (converter == nullptr)
196 continue;
197 // Otherwise we convert the vector and return
198 converter->convert(candidate, target);
199 target->copyTimestamp(*candidate);
200 return;
201 }
202
203 // If there is no candidate up to date, throw an error
204 UniqueArray<IVectorImpl*> candidates;
205 for (auto& impl : m_impls2) {
206 auto* candidate = impl.second;
207 if (candidate->timestamp() == timestamp())
208 candidates.add(candidate);
209 }
210 if (candidates.empty()) {
211 auto msg = String::format("Vector converter to: ", target->backend(),
212 " internal error: no timestamp matching");
213 throw FatalErrorException(A_FUNCINFO, msg);
214 }
215
216 // Otherwise, we have candidates but no converter
217 // In that case we try to use simplecsr as a third party converter
218
219 // Error message that will be printed
220 auto print_error = [&] {
221 alien_fatal([&] {
222 cout() << "ALIEN FATAL ERROR \n"
223 << "Vector converting to target backend '" << target->backend() << "' :\n"
224 << "* no converter available from source backend(s)";
225
226 for (auto* candidate : candidates) {
227 cout() << " ** '" << candidate->backend() << "'";
228 }
229 cout();
230 });
231 };
232
233 // Request simplecsr implementation
234 auto* simplecsr = getImpl<SimpleCSRVector<Real>>("simplecsr");
235
236 // Checking that we have a converter from simplecsr to the requested implementation
237 auto* simplecsr_target =
238 VectorConverterRegisterer::getConverter(simplecsr->backend(), target->backend());
239
240 // If not, throw error
241 if (simplecsr_target == nullptr)
242 print_error();
243
244 // We look for each candidate
245 for (auto* candidate : candidates) {
246 auto* candidat_simplecsr = VectorConverterRegisterer::getConverter(
247 candidate->backend(), simplecsr->backend());
248 // If we have one, we can convert to the requested implementation through simplecsr
249 if (candidat_simplecsr != nullptr) {
250 // Conversion from candidate to simplecsr
251 candidat_simplecsr->convert(candidate, simplecsr);
252 simplecsr->copyTimestamp(*candidate);
253 // Conversion from simplecsr to target
254 simplecsr_target->convert(simplecsr, target);
255 target->copyTimestamp(*simplecsr);
256 return;
257 }
258 }
259
260 // If we reach this line, all conversions possibilities have failed. Throw an exception
261 print_error();
262}
263
264/*---------------------------------------------------------------------------*/
265/*---------------------------------------------------------------------------*/
266
267} // namespace Alien
268
269/*---------------------------------------------------------------------------*/
270/*---------------------------------------------------------------------------*/
MultiVectorImpl.h.
Block elements for block matrices.
Definition Block.h:45
std::shared_ptr< Block > clone() const
Clone this object.
Definition Block.cc:117
Interface to handle abstract vectors implementation.
Definition IVectorImpl.h:47
virtual BackEndId backend() const
Definition IVectorImpl.h:95
std::shared_ptr< ISpace > m_space
The vector space.
std::shared_ptr< VectorDistribution > m_distribution
The vector distribution.
std::shared_ptr< Block > m_block
The uniform block datas.
~MultiVectorImpl() override
Free resources.
void free()
Free resources.
void updateImpl(IVectorImpl *target) const
Update a vector implementation.
MultiVectorImplMap m_impls2
The vectors container.
void clear()
Clear resources.
MultiVectorImpl * clone() const
Clone this object.
const AlgebraTraits< tag >::vector_type & get() const
Get a specific vector implementation.
const Block * block() const
Get uniform block datas.
const VectorDistribution & distribution() const
Get the vector distribution.
void setBlockInfos(const Block *blocks)
Set uniform block information.
std::shared_ptr< VBlock > m_variable_block
The variable block datas.
const ISpace & space() const
Get the space associated with the vector.
IVectorImpl *& getImpl(BackEndId backend) const
Get a specific vector implementation.
const VBlock * vblock() const
Get variable block datas.
Int64 timestamp() const
Valeur du timestamp de référence.
virtual Int64 timestamp() const
Donne la valeur courante du timestamp.
Definition Timestamp.cc:43
void updateTimestamp()
Met à jour le timestamp.
Definition Timestamp.cc:50
void copyTimestamp(const Timestamp &v)
Copy un autre timestamp.
Definition Timestamp.cc:58
Variable size block elements for block matrices.
Definition VBlock.h:46
std::shared_ptr< VBlock > clone() const
Copy this object.
Definition VBlock.cc:184
static IVectorConverter * getConverter(BackEndId from, BackEndId to)
Get the converter from one vector format to another one.
MultiVectorImpl()
Multi vectors representation container.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17