EpetraExt Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_BlockMultiVector.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
43 #include "EpetraExt_BlockUtility.h"
44 #include "Epetra_Map.h"
45 
46 using std::vector;
47 
48 namespace EpetraExt {
49 
50 //=============================================================================
51 // EpetraExt::BlockMultiVector Constructor
53  const Epetra_BlockMap & BaseMap,
54  const Epetra_BlockMap & GlobalMap,
55  int numVecs )
56  : Epetra_MultiVector( GlobalMap, numVecs ),
57  BaseMap_( BaseMap ),
58  Offset_( BlockUtility::CalculateOffset64( BaseMap ) )
59 {
60 }
61 
62 //=============================================================================
63 // EpetraExt::BlockMultiVector Constructor
66  const Epetra_BlockMap & BaseMap,
67  const Epetra_MultiVector & BlockVec)
68  : Epetra_MultiVector( CV, BlockVec, 0, BlockVec.NumVectors() ),
69  BaseMap_( BaseMap ),
70  Offset_( BlockUtility::CalculateOffset64( BaseMap ) )
71 {
72 }
73 
74 //==========================================================================
75 // Copy Constructor
77  : Epetra_MultiVector( dynamic_cast<const Epetra_MultiVector &>(Source) ),
78  BaseMap_( Source.BaseMap_ ),
79  Offset_( Source.Offset_ )
80 {
81 }
82 
83 //=========================================================================
85 {
86 }
87 
88 //=========================================================================
89 int BlockMultiVector::ExtractBlockValues(Epetra_MultiVector & BaseVector, long long GlobalBlockRow) const
90 {
91  long long IndexOffset = GlobalBlockRow * Offset_;
92  int localIndex=0;
93 
94  // For each entry in the base vector, translate its global ID
95  // by the IndexOffset and extract the value from this blockVector
96  for (int i=0; i<BaseMap_.NumMyElements(); i++) {
97  localIndex = this->Map().LID((IndexOffset + BaseMap_.GID64(i)));
98  if (localIndex==-1) {
99  std::cout << "Error in BlockMultiVector::GetBlock: " << i << " "
100  << IndexOffset << " " << BaseMap_.GID64(i) << std::endl;
101  return -1;
102  }
103  for (int j=0; j<NumVectors(); j++)
104  BaseVector[j][i] = (*this)[j][localIndex];
105  }
106 
107  return 0;
108 }
109 
110 //=========================================================================
111 int BlockMultiVector::LoadBlockValues(const Epetra_MultiVector & BaseVector, long long GlobalBlockRow)
112 {
113  long long IndexOffset = GlobalBlockRow * Offset_;
114  int localIndex=0;
115 
116  // For each entry in the base vector, translate its global ID
117  // by the IndexOffset and load into this blockVector
118  for (int i=0; i<BaseMap_.NumMyElements(); i++) {
119  localIndex = this->Map().LID((IndexOffset + BaseMap_.GID64(i)));
120  if (localIndex==-1) {
121  std::cout << "Error in BlockMultiVector::GetBlock: " << i << " "
122  << IndexOffset << " " << BaseMap_.GID64(i) << std::endl;
123  return -1;
124  }
125  for (int j=0; j<NumVectors(); j++)
126  (*this)[j][localIndex] = BaseVector[j][i];
127  }
128 
129  return 0;
130 }
131 
132 //=========================================================================
134 BlockMultiVector::GetBlock(long long GlobalBlockRow) const
135 {
136  long long offset = GlobalBlockRow * BaseMap_.NumMyElements();
137  int numVecs = NumVectors();
138  double **pointers = Pointers();
139  double **block_pointers = new double*[numVecs];
140  for (int i=0; i<numVecs; i++)
141  block_pointers[i] = pointers[i]+offset;
143  Teuchos::rcp(new Epetra_MultiVector(View, BaseMap_, block_pointers,
144  numVecs));
145  delete [] block_pointers;
146  return block;
147 }
148 
149 //=========================================================================
151 BlockMultiVector::GetBlock(long long GlobalBlockRow)
152 {
153  long long offset = GlobalBlockRow * BaseMap_.NumMyElements();
154  int numVecs = NumVectors();
155  double **pointers = Pointers();
156  double **block_pointers = new double*[numVecs];
157  for (int i=0; i<numVecs; i++)
158  block_pointers[i] = pointers[i]+offset;
160  Teuchos::rcp(new Epetra_MultiVector(View, BaseMap_, block_pointers,
161  numVecs));
162  delete [] block_pointers;
163  return block;
164 }
165 
166 //=========================================================================
167 const Epetra_BlockMap&
169 {
170  return BaseMap_;
171 }
172 
173 } //namespace EpetraExt
int ExtractBlockValues(Epetra_MultiVector &BaseVec, long long BlockRow) const
Extract a single block from a Block Vector: block row is global, not a stencil value.
const Epetra_BlockMap & GetBaseMap() const
Return base map.
int NumMyElements() const
long long GID64(int LID) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual const Epetra_BlockMap & Map() const =0
int LID(int GID) const
BlockMultiVector(const Epetra_BlockMap &BaseMap, const Epetra_BlockMap &GlobalMap, int NumVectors)
BlockMultiVector constuctor with one block row per processor.
int LoadBlockValues(const Epetra_MultiVector &BaseVec, long long BlockRow)
Load a single block into a Block Vector: block row is global, not a stencil value.
Epetra_DataAccess
Teuchos::RCP< const Epetra_MultiVector > GetBlock(long long BlockRow) const
Return Epetra_MultiVector for given block row.