Reference documentation for deal.II version 9.5.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
trilinos_vector.cc
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2008 - 2023 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
17
18#ifdef DEAL_II_WITH_TRILINOS
19
20# include <deal.II/base/mpi.h>
22
27
28# include <boost/io/ios_state.hpp>
29
30# include <Epetra_Export.h>
31# include <Epetra_Import.h>
32# include <Epetra_Vector.h>
33
34# include <cmath>
35# include <memory>
36
37
39
40namespace TrilinosWrappers
41{
42# ifndef DOXYGEN
43 namespace internal
44 {
45 VectorReference::operator TrilinosScalar() const
46 {
47 AssertIndexRange(index, vector.size());
48
49 // Trilinos allows for vectors to be referenced by the [] or ()
50 // operators but only () checks index bounds. We check these bounds by
51 // ourselves, so we can use []. Note that we can only get local values.
52
53 const TrilinosWrappers::types::int_type local_index =
54 vector.vector->Map().LID(
55 static_cast<TrilinosWrappers::types::int_type>(index));
56 Assert(local_index >= 0,
58 index,
59 vector.local_size(),
60 vector.vector->Map().MinMyGID(),
61 vector.vector->Map().MaxMyGID()));
62
63
64 return (*(vector.vector))[0][local_index];
65 }
66 } // namespace internal
67# endif
68
69 namespace MPI
70 {
72 : Subscriptor()
73 , last_action(Zero)
74 , compressed(true)
75 , has_ghosts(false)
76 , vector(new Epetra_FEVector(
77 Epetra_Map(0, 0, 0, Utilities::Trilinos::comm_self())))
78 {}
79
80
81
82 Vector::Vector(const IndexSet &parallel_partitioning,
83 const MPI_Comm communicator)
84 : Vector()
85 {
86 reinit(parallel_partitioning, communicator);
87 }
88
89
90
92 : Vector()
93 {
95 vector = std::make_unique<Epetra_FEVector>(*v.vector);
97 }
98
99
100
101 Vector::Vector(Vector &&v) // NOLINT
102 : Vector()
103 {
104 // initialize a minimal, valid object and swap
105 static_cast<Subscriptor &>(*this) = static_cast<Subscriptor &&>(v);
106 swap(v);
107 }
108
109
110
111 Vector::Vector(const IndexSet &parallel_partitioner,
112 const Vector & v,
113 const MPI_Comm communicator)
114 : Vector()
115 {
116 AssertThrow(parallel_partitioner.size() ==
117 static_cast<size_type>(
119 ExcDimensionMismatch(parallel_partitioner.size(),
121 v.vector->Map())));
122
123 vector = std::make_unique<Epetra_FEVector>(
124 parallel_partitioner.make_trilinos_map(communicator, true));
125 reinit(v, false, true);
126 }
127
128
129
131 const IndexSet &ghost,
132 const MPI_Comm communicator)
133 : Vector()
134 {
135 reinit(local, ghost, communicator, false);
136 }
137
138
139
140 void
142 {
143 // When we clear the vector, reset the pointer and generate an empty
144 // vector.
145 Epetra_Map map(0, 0, Epetra_MpiComm(MPI_COMM_SELF));
146
147 has_ghosts = false;
148 vector = std::make_unique<Epetra_FEVector>(map);
149 last_action = Zero;
150 }
151
152
153
154 void
155 Vector::reinit(const IndexSet &parallel_partitioner,
156 const MPI_Comm communicator,
157 const bool /*omit_zeroing_entries*/)
158 {
159 nonlocal_vector.reset();
160
161 const bool overlapping =
162 !parallel_partitioner.is_ascending_and_one_to_one(communicator);
163
164 Epetra_Map map =
165 parallel_partitioner.make_trilinos_map(communicator, overlapping);
166
167 vector = std::make_unique<Epetra_FEVector>(map);
168
169 has_ghosts = vector->Map().UniqueGIDs() == false;
170
171 // If the IndexSets are overlapping, we don't really know
172 // which process owns what. So we decide that no process
173 // owns anything in that case. In particular asking for
174 // the locally owned elements is not allowed.
175 if (has_ghosts)
176 {
179 }
180 else
181 owned_elements = parallel_partitioner;
182
183# ifdef DEBUG
184 const size_type n_elements_global =
186
187 Assert(has_ghosts || n_elements_global == size(), ExcInternalError());
188# endif
189
190 last_action = Zero;
191 }
192
193
194
195 void
197 const bool omit_zeroing_entries,
198 const bool allow_different_maps)
199 {
200 nonlocal_vector.reset();
201
202 // In case we do not allow to have different maps, this call means that
203 // we have to reset the vector. So clear the vector, initialize our map
204 // with the map in v, and generate the vector.
205 if (allow_different_maps == false)
206 {
207 // check equality for MPI communicators: We can only choose the fast
208 // version in case the underlying Epetra_MpiComm object is the same,
209 // otherwise we might access an MPI_Comm object that has been
210 // deleted
211 const Epetra_MpiComm *my_comm =
212 dynamic_cast<const Epetra_MpiComm *>(&vector->Comm());
213 const Epetra_MpiComm *v_comm =
214 dynamic_cast<const Epetra_MpiComm *>(&v.vector->Comm());
215 const bool same_communicators =
216 my_comm != nullptr && v_comm != nullptr &&
217 my_comm->DataPtr() == v_comm->DataPtr();
218 if (!same_communicators ||
219 vector->Map().SameAs(v.vector->Map()) == false)
220 {
221 vector = std::make_unique<Epetra_FEVector>(v.vector->Map());
223 last_action = Zero;
225 }
226 else if (omit_zeroing_entries == false)
227 {
228 // old and new vectors have exactly the same map, i.e. size and
229 // parallel distribution
230 int ierr;
231 ierr = vector->GlobalAssemble(last_action);
232 (void)ierr;
233 Assert(ierr == 0, ExcTrilinosError(ierr));
234
235 ierr = vector->PutScalar(0.0);
236 Assert(ierr == 0, ExcTrilinosError(ierr));
237
238 last_action = Zero;
239 }
240 }
241
242 // Otherwise, we have to check that the two vectors are already of the
243 // same size, create an object for the data exchange and then insert all
244 // the data. The first assertion is only a check whether the user knows
245 // what they are doing.
246 else
247 {
248 Assert(omit_zeroing_entries == false,
250 "It is not possible to exchange data with the "
251 "option 'omit_zeroing_entries' set, which would not write "
252 "elements."));
253
254 AssertThrow(size() == v.size(),
256
257 Epetra_Import data_exchange(vector->Map(), v.vector->Map());
258
259 const int ierr = vector->Import(*v.vector, data_exchange, Insert);
260 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
261
262 last_action = Insert;
263 }
264# ifdef DEBUG
265 const Epetra_MpiComm *comm_ptr =
266 dynamic_cast<const Epetra_MpiComm *>(&(v.vector->Comm()));
267 Assert(comm_ptr != nullptr, ExcInternalError());
268 const size_type n_elements_global =
269 Utilities::MPI::sum(owned_elements.n_elements(), comm_ptr->Comm());
270 Assert(has_ghosts || n_elements_global == size(), ExcInternalError());
271# endif
272 }
273
274
275
276 void
277 Vector::reinit(const MPI::BlockVector &v, const bool import_data)
278 {
279 nonlocal_vector.reset();
282
283 // In case we do not allow to have different maps, this call means that
284 // we have to reset the vector. So clear the vector, initialize our map
285 // with the map in v, and generate the vector.
286 if (v.n_blocks() == 0)
287 return;
288
289 // create a vector that holds all the elements contained in the block
290 // vector. need to manually create an Epetra_Map.
291 size_type n_elements = 0, added_elements = 0, block_offset = 0;
292 for (size_type block = 0; block < v.n_blocks(); ++block)
293 n_elements += v.block(block).local_size();
294 std::vector<TrilinosWrappers::types::int_type> global_ids(n_elements, -1);
295 for (size_type block = 0; block < v.n_blocks(); ++block)
296 {
297 TrilinosWrappers::types::int_type *glob_elements =
299 v.block(block).trilinos_partitioner());
300 for (size_type i = 0; i < v.block(block).local_size(); ++i)
301 global_ids[added_elements++] = glob_elements[i] + block_offset;
302 owned_elements.add_indices(v.block(block).owned_elements,
303 block_offset);
304 block_offset += v.block(block).size();
305 }
306
307 Assert(n_elements == added_elements, ExcInternalError());
308 Epetra_Map new_map(v.size(),
309 n_elements,
310 global_ids.data(),
311 0,
312 v.block(0).trilinos_partitioner().Comm());
313
314 auto actual_vec = std::make_unique<Epetra_FEVector>(new_map);
315
316 TrilinosScalar *entries = (*actual_vec)[0];
317 for (size_type block = 0; block < v.n_blocks(); ++block)
318 {
319 v.block(block).trilinos_vector().ExtractCopy(entries, 0);
320 entries += v.block(block).local_size();
321 }
322
323 if (import_data == true)
324 {
326 *actual_vec)) == v.size(),
328 *actual_vec),
329 v.size()));
330
331 Epetra_Import data_exchange(vector->Map(), actual_vec->Map());
332
333 const int ierr = vector->Import(*actual_vec, data_exchange, Insert);
334 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
335
336 last_action = Insert;
337 }
338 else
339 vector = std::move(actual_vec);
340# ifdef DEBUG
341 const Epetra_MpiComm *comm_ptr =
342 dynamic_cast<const Epetra_MpiComm *>(&(vector->Comm()));
343 Assert(comm_ptr != nullptr, ExcInternalError());
344 const size_type n_elements_global =
345 Utilities::MPI::sum(owned_elements.n_elements(), comm_ptr->Comm());
346
347 Assert(has_ghosts || n_elements_global == size(), ExcInternalError());
348# endif
349 }
350
351
352
353 void
354 Vector::reinit(const IndexSet &locally_owned_entries,
355 const IndexSet &ghost_entries,
356 const MPI_Comm communicator,
357 const bool vector_writable)
358 {
359 nonlocal_vector.reset();
360 owned_elements = locally_owned_entries;
361 if (vector_writable == false)
362 {
363 IndexSet parallel_partitioner = locally_owned_entries;
364 parallel_partitioner.add_indices(ghost_entries);
365 Epetra_Map map =
366 parallel_partitioner.make_trilinos_map(communicator, true);
367 vector = std::make_unique<Epetra_FEVector>(map);
368 }
369 else
370 {
371 Epetra_Map map =
372 locally_owned_entries.make_trilinos_map(communicator, true);
373 Assert(map.IsOneToOne(),
374 ExcMessage("A writable vector must not have ghost entries in "
375 "its parallel partitioning"));
376
377 if (vector->Map().SameAs(map) == false)
378 vector = std::make_unique<Epetra_FEVector>(map);
379 else
380 {
381 const int ierr = vector->PutScalar(0.);
382 (void)ierr;
383 Assert(ierr == 0, ExcTrilinosError(ierr));
384 }
385
386 IndexSet nonlocal_entries(ghost_entries);
387 nonlocal_entries.subtract_set(locally_owned_entries);
388 if (Utilities::MPI::n_mpi_processes(communicator) > 1)
389 {
390 Epetra_Map nonlocal_map =
391 nonlocal_entries.make_trilinos_map(communicator, true);
393 std::make_unique<Epetra_MultiVector>(nonlocal_map, 1);
394 }
395 }
396
397 has_ghosts = vector->Map().UniqueGIDs() == false;
398
399 last_action = Zero;
400
401# ifdef DEBUG
402 const size_type n_elements_global =
404
405 Assert(has_ghosts || n_elements_global == size(), ExcInternalError());
406# endif
407 }
408
409
410
411 void
413 const std::shared_ptr<const Utilities::MPI::Partitioner> &partitioner,
414 const bool make_ghosted,
415 const bool vector_writable)
416 {
417 if (make_ghosted)
418 {
419 Assert(partitioner->ghost_indices_initialized(),
420 ExcMessage("You asked to create a ghosted vector, but the "
421 "partitioner does not provide ghost indices."));
422
423 this->reinit(partitioner->locally_owned_range(),
424 partitioner->ghost_indices(),
425 partitioner->get_mpi_communicator(),
426 vector_writable);
427 }
428 else
429 {
430 this->reinit(partitioner->locally_owned_range(),
431 partitioner->get_mpi_communicator());
432 }
433 }
434
435
436
437 Vector &
439 {
440 Assert(vector.get() != nullptr,
441 ExcMessage("Vector is not constructed properly."));
442
443 // check equality for MPI communicators to avoid accessing a possibly
444 // invalid MPI_Comm object
445 const Epetra_MpiComm *my_comm =
446 dynamic_cast<const Epetra_MpiComm *>(&vector->Comm());
447 const Epetra_MpiComm *v_comm =
448 dynamic_cast<const Epetra_MpiComm *>(&v.vector->Comm());
449 const bool same_communicators = my_comm != nullptr && v_comm != nullptr &&
450 my_comm->DataPtr() == v_comm->DataPtr();
451 // Need to ask MPI whether the communicators are the same. We would like
452 // to use the following checks but currently we cannot make sure the
453 // memory of my_comm is not stale from some MPI_Comm_free
454 // somewhere. This can happen when a vector lives in GrowingVectorMemory
455 // data structures. Thus, the following code is commented out.
456 //
457 // if (my_comm != nullptr &&
458 // v_comm != nullptr &&
459 // my_comm->DataPtr() != v_comm->DataPtr())
460 // {
461 // int communicators_same = 0;
462 // const int ierr = MPI_Comm_compare (my_comm->GetMpiComm(),
463 // v_comm->GetMpiComm(),
464 // &communicators_same);
465 // AssertThrowMPI(ierr);
466 // if (!(communicators_same == MPI_IDENT ||
467 // communicators_same == MPI_CONGRUENT))
468 // same_communicators = false;
469 // else
470 // same_communicators = true;
471 // }
472
473 // distinguish three cases. First case: both vectors have the same
474 // layout (just need to copy the local data, not reset the memory and
475 // the underlying Epetra_Map). The third case means that we have to
476 // rebuild the calling vector.
477 if (same_communicators && v.vector->Map().SameAs(vector->Map()))
478 {
479 *vector = *v.vector;
480 if (v.nonlocal_vector.get() != nullptr)
482 std::make_unique<Epetra_MultiVector>(v.nonlocal_vector->Map(), 1);
483 last_action = Zero;
484 }
485 // Second case: vectors have the same global
486 // size, but different parallel layouts (and
487 // one of them a one-to-one mapping). Then we
488 // can call the import/export functionality.
489 else if (size() == v.size() &&
490 (v.vector->Map().UniqueGIDs() || vector->Map().UniqueGIDs()))
491 {
492 reinit(v, false, true);
493 }
494 // Third case: Vectors do not have the same
495 // size.
496 else
497 {
498 vector = std::make_unique<Epetra_FEVector>(*v.vector);
499 last_action = Zero;
502 }
503
504 if (v.nonlocal_vector.get() != nullptr)
506 std::make_unique<Epetra_MultiVector>(v.nonlocal_vector->Map(), 1);
507
508 return *this;
509 }
510
511
512
513 Vector &
515 {
516 static_cast<Subscriptor &>(*this) = static_cast<Subscriptor &&>(v);
517 swap(v);
518 return *this;
519 }
520
521
522
523 template <typename number>
524 Vector &
525 Vector::operator=(const ::Vector<number> &v)
526 {
527 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
528
529 // this is probably not very efficient but works. in particular, we could
530 // do better if we know that number==TrilinosScalar because then we could
531 // elide the copying of elements
532 //
533 // let's hope this isn't a particularly frequent operation
534 std::pair<size_type, size_type> local_range = this->local_range();
535 for (size_type i = local_range.first; i < local_range.second; ++i)
536 (*vector)[0][i - local_range.first] = v(i);
537
538 return *this;
539 }
540
541
542
543 void
545 const Vector & v)
546 {
547 Assert(m.trilinos_matrix().Filled() == true,
548 ExcMessage("Matrix is not compressed. "
549 "Cannot find exchange information!"));
550 Assert(v.vector->Map().UniqueGIDs() == true,
551 ExcMessage("The input vector has overlapping data, "
552 "which is not allowed."));
553
554 if (vector->Map().SameAs(m.trilinos_matrix().ColMap()) == false)
555 vector =
556 std::make_unique<Epetra_FEVector>(m.trilinos_matrix().ColMap());
557
558 Epetra_Import data_exchange(vector->Map(), v.vector->Map());
559 const int ierr = vector->Import(*v.vector, data_exchange, Insert);
560
561 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
562
563 last_action = Insert;
564 }
565
566
567 void
569 const VectorOperation::values operation)
570 {
571 Assert(
572 this->size() == rwv.size(),
574 "Both vectors need to have the same size for import_elements() to work!"));
575 // TODO: a generic import_elements() function should handle any kind of
576 // data layout in ReadWriteVector, but this function is of limited use as
577 // this class will (hopefully) be retired eventually.
580
581 if (operation == VectorOperation::insert)
582 {
583 for (const auto idx : this->locally_owned_elements())
584 (*this)[idx] = rwv[idx];
585 }
586 else if (operation == VectorOperation::add)
587 {
588 for (const auto idx : this->locally_owned_elements())
589 (*this)[idx] += rwv[idx];
590 }
591 else
593
594 this->compress(operation);
595 }
596
597
598 void
600 {
601 // Select which mode to send to Trilinos. Note that we use last_action if
602 // available and ignore what the user tells us to detect wrongly mixed
603 // operations. Typically given_last_action is only used on machines that
604 // do not execute an operation (because they have no own cells for
605 // example).
606 Epetra_CombineMode mode = last_action;
607 if (last_action == Zero)
608 {
609 if (given_last_action == VectorOperation::add)
610 mode = Add;
611 else if (given_last_action == VectorOperation::insert)
612 mode = Insert;
613 else
614 Assert(
615 false,
617 "compress() can only be called with VectorOperation add, insert, or unknown"));
618 }
619 else
620 {
621 Assert(
622 ((last_action == Add) &&
623 (given_last_action == VectorOperation::add)) ||
624 ((last_action == Insert) &&
625 (given_last_action == VectorOperation::insert)),
627 "The last operation on the Vector and the given last action in the compress() call do not agree!"));
628 }
629
630
631# ifdef DEBUG
632 // check that every process has decided to use the same mode. This will
633 // otherwise result in undefined behavior in the call to
634 // GlobalAssemble().
635 const double double_mode = mode;
636 const Epetra_MpiComm *comm_ptr =
637 dynamic_cast<const Epetra_MpiComm *>(&(trilinos_partitioner().Comm()));
638 Assert(comm_ptr != nullptr, ExcInternalError());
639
640 const Utilities::MPI::MinMaxAvg result =
641 Utilities::MPI::min_max_avg(double_mode, comm_ptr->GetMpiComm());
642 Assert(result.max == result.min,
644 "Not all processors agree whether the last operation on "
645 "this vector was an addition or a set operation. This will "
646 "prevent the compress() operation from succeeding."));
647
648# endif
649
650 // Now pass over the information about what we did last to the vector.
651 int ierr = 0;
652 if (nonlocal_vector.get() == nullptr || mode != Add)
653 ierr = vector->GlobalAssemble(mode);
654 else
655 {
656 Epetra_Export exporter(nonlocal_vector->Map(), vector->Map());
657 ierr = vector->Export(*nonlocal_vector, exporter, mode);
658 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
659 ierr = nonlocal_vector->PutScalar(0.);
660 }
661 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
662 last_action = Zero;
663
664 compressed = true;
665 }
666
667
668
670 Vector::operator()(const size_type index) const
671 {
672 // Extract local indices in the vector.
673 TrilinosWrappers::types::int_type trilinos_i = vector->Map().LID(
674 static_cast<TrilinosWrappers::types::int_type>(index));
675 TrilinosScalar value = 0.;
676
677 // If the element is not present on the current processor, we can't
678 // continue. This is the main difference to the el() function.
679 if (trilinos_i == -1)
680 {
681 Assert(false,
683 local_size(),
684 vector->Map().MinMyGID(),
685 vector->Map().MaxMyGID()));
686 }
687 else
688 value = (*vector)[0][trilinos_i];
689
690 return value;
691 }
692
693
694
695 void
696 Vector::add(const Vector &v, const bool allow_different_maps)
697 {
698 if (allow_different_maps == false)
699 *this += v;
700 else
701 {
703 AssertThrow(size() == v.size(),
705
706# if DEAL_II_TRILINOS_VERSION_GTE(11, 11, 0)
707 Epetra_Import data_exchange(vector->Map(), v.vector->Map());
708 int ierr =
709 vector->Import(*v.vector, data_exchange, Epetra_AddLocalAlso);
710 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
711 last_action = Add;
712# else
713 // In versions older than 11.11 the Import function is broken for
714 // adding Hence, we provide a workaround in this case
715
716 Epetra_MultiVector dummy(vector->Map(), 1, false);
717 Epetra_Import data_exchange(dummy.Map(), v.vector->Map());
718
719 int ierr = dummy.Import(*v.vector, data_exchange, Insert);
720 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
721
722 ierr = vector->Update(1.0, dummy, 1.0);
723 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
724# endif
725 }
726 }
727
728
729
730 bool
732 {
733 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
734 if (local_size() != v.local_size())
735 return false;
736
737 size_type i;
738 for (i = 0; i < local_size(); ++i)
739 if ((*(v.vector))[0][i] != (*vector)[0][i])
740 return false;
741
742 return true;
743 }
744
745
746
747 bool
749 {
750 Assert(size() == v.size(), ExcDimensionMismatch(size(), v.size()));
751
752 return (!(*this == v));
753 }
754
755
756
757 bool
759 {
760 // get a representation of the vector and
761 // loop over all the elements
762 TrilinosScalar * start_ptr = (*vector)[0];
763 const TrilinosScalar *ptr = start_ptr, *eptr = start_ptr + local_size();
764 unsigned int flag = 0;
765 while (ptr != eptr)
766 {
767 if (*ptr != 0)
768 {
769 flag = 1;
770 break;
771 }
772 ++ptr;
773 }
774
775 // in parallel, check that the vector
776 // is zero on _all_ processors.
777 const Epetra_MpiComm *mpi_comm =
778 dynamic_cast<const Epetra_MpiComm *>(&vector->Map().Comm());
779 Assert(mpi_comm != nullptr, ExcInternalError());
780 unsigned int num_nonzero = Utilities::MPI::sum(flag, mpi_comm->Comm());
781 return num_nonzero == 0;
782 }
783
784
785
786 bool
788 {
789 // get a representation of the vector and
790 // loop over all the elements
791 TrilinosScalar * start_ptr = (*vector)[0];
792 const TrilinosScalar *ptr = start_ptr, *eptr = start_ptr + local_size();
793 unsigned int flag = 0;
794 while (ptr != eptr)
795 {
796 if (*ptr < 0.0)
797 {
798 flag = 1;
799 break;
800 }
801 ++ptr;
802 }
803
804 // in parallel, check that the vector
805 // is zero on _all_ processors.
806 const auto max_n_negative =
808 return max_n_negative == 0;
809 }
810
811
812
813 void
814 Vector::print(std::ostream & out,
815 const unsigned int precision,
816 const bool scientific,
817 const bool across) const
818 {
819 AssertThrow(out.fail() == false, ExcIO());
820 boost::io::ios_flags_saver restore_flags(out);
821
822
823 out.precision(precision);
824 if (scientific)
825 out.setf(std::ios::scientific, std::ios::floatfield);
826 else
827 out.setf(std::ios::fixed, std::ios::floatfield);
828
829 if (size() != local_size())
830 {
831 auto global_id = [&](const size_type index) {
832 return gid(vector->Map(), index);
833 };
834 out << "size:" << size() << " local_size:" << local_size() << " :"
835 << std::endl;
836 for (size_type i = 0; i < local_size(); ++i)
837 out << "[" << global_id(i) << "]: " << (*(vector))[0][i]
838 << std::endl;
839 }
840 else
841 {
842 TrilinosScalar *val;
843 int leading_dimension;
844 int ierr = vector->ExtractView(&val, &leading_dimension);
845
846 AssertThrow(ierr == 0, ExcTrilinosError(ierr));
847 if (across)
848 for (size_type i = 0; i < size(); ++i)
849 out << static_cast<double>(val[i]) << ' ';
850 else
851 for (size_type i = 0; i < size(); ++i)
852 out << static_cast<double>(val[i]) << std::endl;
853 out << std::endl;
854 }
855
856 AssertThrow(out.fail() == false, ExcIO());
857 }
858
859
860
861 void
863 {
864 std::swap(last_action, v.last_action);
865 std::swap(compressed, v.compressed);
866 std::swap(has_ghosts, v.has_ghosts);
867 std::swap(vector, v.vector);
868 std::swap(nonlocal_vector, v.nonlocal_vector);
869 std::swap(owned_elements, v.owned_elements);
870 }
871
872
873
874 std::size_t
876 {
877 // TODO[TH]: No accurate memory
878 // consumption for Trilinos vectors
879 // yet. This is a rough approximation with
880 // one index and the value per local
881 // entry.
882 return sizeof(*this) +
883 this->local_size() *
884 (sizeof(double) + sizeof(TrilinosWrappers::types::int_type));
885 }
886
887 // explicit instantiations
888# ifndef DOXYGEN
889# include "trilinos_vector.inst"
890# endif
891 } // namespace MPI
892} // namespace TrilinosWrappers
893
895
896#endif // DEAL_II_WITH_TRILINOS
unsigned int n_blocks() const
std::size_t size() const
BlockType & block(const unsigned int i)
bool is_ascending_and_one_to_one(const MPI_Comm communicator) const
Definition index_set.cc:878
size_type size() const
Definition index_set.h:1661
size_type n_elements() const
Definition index_set.h:1816
void set_size(const size_type size)
Definition index_set.h:1649
Epetra_Map make_trilinos_map(const MPI_Comm communicator=MPI_COMM_WORLD, const bool overlapping=false) const
Definition index_set.cc:789
void clear()
Definition index_set.h:1637
void subtract_set(const IndexSet &other)
Definition index_set.cc:268
void add_indices(const ForwardIterator &begin, const ForwardIterator &end)
Definition index_set.h:1727
const IndexSet & get_stored_elements() const
void compress(VectorOperation::values operation)
void add(const std::vector< size_type > &indices, const std::vector< TrilinosScalar > &values)
std::unique_ptr< Epetra_MultiVector > nonlocal_vector
void import_elements(const LinearAlgebra::ReadWriteVector< double > &rwv, const VectorOperation::values operation)
MPI_Comm get_mpi_communicator() const
const Epetra_BlockMap & trilinos_partitioner() const
reference operator()(const size_type index)
void import_nonlocal_data_for_fe(const ::TrilinosWrappers::SparseMatrix &matrix, const Vector &vector)
std::unique_ptr< Epetra_FEVector > vector
size_type local_size() const
void print(std::ostream &out, const unsigned int precision=3, const bool scientific=true, const bool across=true) const
IndexSet locally_owned_elements() const
void reinit(const Vector &v, const bool omit_zeroing_entries=false, const bool allow_different_maps=false)
std::pair< size_type, size_type > local_range() const
bool operator!=(const Vector &v) const
bool operator==(const Vector &v) const
Vector & operator=(const TrilinosScalar s)
std::size_t memory_consumption() const
const Epetra_CrsMatrix & trilinos_matrix() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcGhostsPresent()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcAccessToNonLocalElement(size_type arg1, size_type arg2, size_type arg3, size_type arg4)
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcTrilinosError(int arg1)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
void swap(BlockVector &u, BlockVector &v)
TrilinosWrappers::types::int_type global_length(const Epetra_MultiVector &vector)
int gid(const Epetra_BlockMap &map, int i)
TrilinosWrappers::types::int_type * my_global_elements(const Epetra_BlockMap &map)
TrilinosWrappers::types::int64_type n_global_elements(const Epetra_BlockMap &map)
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:150
T max(const T &t, const MPI_Comm mpi_communicator)
MinMaxAvg min_max_avg(const double my_value, const MPI_Comm mpi_communicator)
Definition mpi.cc:124
double TrilinosScalar
Definition types.h:175