| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/** |
| 11 |
* \class ROL::Belos::MultiVector |
| 12 |
* \brief Specializes the Belos::MultiVecTraits to use ROL::MultiVector |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
#ifndef ROL_BELOS_MULTIVECTOR_HPP |
| 17 |
#define ROL_BELOS_MULTIVECTOR_HPP |
| 18 |
|
| 19 |
#include "BelosConfigDefs.hpp" |
| 20 |
#include "BelosLinearProblem.hpp" |
| 21 |
#include "BelosMultiVecTraits.hpp" |
| 22 |
#include "BelosTypes.hpp" |
| 23 |
|
| 24 |
#include "ROL_MultiVector.hpp" |
| 25 |
|
| 26 |
#ifdef HAVE_BELOS_TSQR |
| 27 |
namespace { // (anonymous) |
| 28 |
|
| 29 |
/// \brief Stub TSQR adapter for the ROL::MultiVector specialization of |
| 30 |
/// Belos::MultiVecTraits. |
| 31 |
/// |
| 32 |
/// This is a stub. All the methods throw. The point is to make |
| 33 |
/// everything compile if TSQR is enabled. |
| 34 |
template<class Scalar> |
| 35 |
class RolStubTsqrAdapter { |
| 36 |
public: |
| 37 |
RolStubTsqrAdapter (const Teuchos::RCP<Teuchos::ParameterList>&) |
| 38 |
{ |
| 39 |
ROL_TEST_FOR_EXCEPTION(false, std::logic_error, "Not implemented"); |
| 40 |
} |
| 41 |
|
| 42 |
RolStubTsqrAdapter () |
| 43 |
{ |
| 44 |
ROL_TEST_FOR_EXCEPTION(false, std::logic_error, "Not implemented"); |
| 45 |
} |
| 46 |
|
| 47 |
Teuchos::RCP<const Teuchos::ParameterList> |
| 48 |
getValidParameters () const |
| 49 |
{ |
| 50 |
return Teuchos::rcp(new Teuchos::ParameterList>()); |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
setParameterList (const Teuchos::RCP<Teuchos::ParameterList>&) |
| 55 |
{} |
| 56 |
|
| 57 |
void |
| 58 |
factorExplicit (ROL::MultiVector<Scalar>& /* A */, |
| 59 |
ROL::MultiVector<Scalar>& /* Q */, |
| 60 |
Teuchos::SerialDenseMatrix<int, Scalar>& /* R */, |
| 61 |
const bool forceNonnegativeDiagonal = false) |
| 62 |
{ |
| 63 |
(void) forceNonnegativeDiagonal; // forestall "unused" warning |
| 64 |
ROL_TEST_FOR_EXCEPTION(false, std::logic_error, "Not implemented"); |
| 65 |
} |
| 66 |
|
| 67 |
int |
| 68 |
revealRank (ROL::MultiVector<Scalar>& /* Q */, |
| 69 |
Teuchos::SerialDenseMatrix<int, Scalar>& /* R */, |
| 70 |
const typename ROL::ScalarTraits<Scalar>::magnitudeType& /* tol */) |
| 71 |
{ |
| 72 |
return 0; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
} // namespace (anonymous) |
| 77 |
#endif // HAVE_BELOS_TSQR |
| 78 |
|
| 79 |
namespace Belos { |
| 80 |
|
| 81 |
template<class Scalar> |
| 82 |
class MultiVecTraits<Scalar, ROL::MultiVector<Scalar> > { |
| 83 |
|
| 84 |
typedef ROL::MultiVector<Scalar> MV; // ROL::MultiVector object |
| 85 |
typedef Teuchos::RCP<MV> PMV; // Pointer to ROL::MultiVector object |
| 86 |
|
| 87 |
typedef Teuchos::SerialDenseMatrix<int,Scalar> Matrix; |
| 88 |
|
| 89 |
typedef typename ROL::ScalarTraits<Scalar>::magnitudeType magnitudeType; |
| 90 |
|
| 91 |
public: |
| 92 |
|
| 93 |
/** \brief Make a new MultiVector containing a specified number of vectors |
| 94 |
|
| 95 |
@return A reference-counted pointer to the cloned MultiVector |
| 96 |
*/ |
| 97 |
static PMV Clone(const MV& mv, const int numVecs) { |
| 98 |
return mv.clone(numVecs); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** \brief Make a new MultiVector of the same size and containing a deep copy |
| 103 |
of the original MultiVector's data |
| 104 |
|
| 105 |
@return A reference-counted pointer to the cloned MultiVector |
| 106 |
*/ |
| 107 |
static PMV CloneCopy(const MV& mv) { |
| 108 |
return mv.deepCopy(); |
| 109 |
} |
| 110 |
|
| 111 |
/** \brief Make a new MultiVector containing a deep copy of specified columns |
| 112 |
of the original MultiVector's data |
| 113 |
|
| 114 |
@return A reference-counted pointer to the cloned MultiVector |
| 115 |
*/ |
| 116 |
static PMV CloneCopy(const MV& mv, const std::vector<int>& index) { |
| 117 |
return mv.deepCopy(index); |
| 118 |
} |
| 119 |
|
| 120 |
/** \brief Make a new MultiVector containing a deep copy of specified |
| 121 |
contiguous block of columns of the original MultiVector's data |
| 122 |
|
| 123 |
@return A reference-counted pointer to the cloned MultiVector |
| 124 |
*/ |
| 125 |
static PMV CloneCopy(const MV& mv, const Teuchos::Range1D& index) { |
| 126 |
int n = index.size(); |
| 127 |
int l = index.lbound(); |
| 128 |
|
| 129 |
std::vector<int> stdex(n); |
| 130 |
for( int i=0; i<n; ++i ) { |
| 131 |
stdex[i] = l+i; |
| 132 |
} |
| 133 |
|
| 134 |
return mv.deepCopy(stdex); |
| 135 |
} |
| 136 |
|
| 137 |
/** \brief Make a new MultiVector containing pointers to specified |
| 138 |
columns of the original MultiVector's data |
| 139 |
|
| 140 |
@return A reference-counted pointer to the cloned MultiVector |
| 141 |
*/ |
| 142 |
static PMV CloneViewNonConst(MV& mv, const std::vector<int>& index) { |
| 143 |
return mv.shallowCopy(index); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** \brief Make a new MultiVector containing pointers to a contiguous |
| 148 |
block of columns of the original MultiVector's data |
| 149 |
|
| 150 |
@return A reference-counted pointer to the cloned MultiVector |
| 151 |
*/ |
| 152 |
static PMV CloneViewNonConst(MV& mv, const Teuchos::Range1D& index) { |
| 153 |
int n = index.size(); |
| 154 |
int l = index.lbound(); |
| 155 |
|
| 156 |
std::vector<int> stdex(n); |
| 157 |
for( int i=0; i<n; ++i ) { |
| 158 |
stdex[i] = l+i; |
| 159 |
} |
| 160 |
|
| 161 |
return mv.shallowCopy(stdex); |
| 162 |
} |
| 163 |
|
| 164 |
/** \brief Make a new const MultiVector containing pointers to specified |
| 165 |
columns of the original MultiVector's data |
| 166 |
|
| 167 |
@return A reference-counted pointer to the cloned MultiVector |
| 168 |
*/ |
| 169 |
static PMV CloneView(const MV& mv, const std::vector<int>& index) { |
| 170 |
return mv.shallowCopyConst(index); |
| 171 |
} |
| 172 |
|
| 173 |
/** \brief Make a new const MultiVector containing pointers to a contiguous |
| 174 |
block of columns of the original MultiVector's data |
| 175 |
|
| 176 |
@return A reference-counted pointer to the cloned MultiVector |
| 177 |
*/ |
| 178 |
static PMV CloneView(const MV& mv, const Teuchos::Range1D& index) { |
| 179 |
int n = index.size(); |
| 180 |
int l = index.lbound(); |
| 181 |
|
| 182 |
std::vector<int> stdex(n); |
| 183 |
for( int i=0; i<n; ++i ) { |
| 184 |
stdex[i] = l+i; |
| 185 |
} |
| 186 |
|
| 187 |
return mv.shallowCopyConst(stdex); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** \brief Get length of the columns of the MultiVector |
| 192 |
|
| 193 |
@return Vector length |
| 194 |
*/ |
| 195 |
static ptrdiff_t GetGlobalLength(const MV& mv) { |
| 196 |
return mv.getLength(); |
| 197 |
} |
| 198 |
|
| 199 |
/** \brief Return the number of vectors in the MultiVector |
| 200 |
*/ |
| 201 |
static int GetNumberVecs(const MV& mv) { |
| 202 |
return mv.getNumberOfVectors(); |
| 203 |
} |
| 204 |
|
| 205 |
/// \brief Stride is undefined for ROL::Vector |
| 206 |
static bool HasConstantStride(const MV& mv) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
/** \brief Replace mv with \f$\alpha AB+\beta mv\f$ |
| 211 |
*/ |
| 212 |
static void MvTimesMatAddMv(Scalar alpha, const MV& A, |
| 213 |
const Matrix& B, Scalar beta, MV& mv) { |
| 214 |
mv.gemm(alpha,A,B,beta); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
static void MvAddMv(Scalar alpha, const MV& A, |
| 219 |
Scalar beta, const MV& B, MV& mv) { |
| 220 |
|
| 221 |
mv.set(A); |
| 222 |
mv.scale(alpha); |
| 223 |
mv.axpy(beta,B); |
| 224 |
} |
| 225 |
|
| 226 |
/** \brief Scale every element in the MultiVector by alpha |
| 227 |
*/ |
| 228 |
static void MvScale(MV& mv, Scalar alpha) { |
| 229 |
mv.scale(alpha); |
| 230 |
} |
| 231 |
|
| 232 |
/** \brief Scale the columns of the MultiVector by the values in alpha |
| 233 |
*/ |
| 234 |
static void MvScale(MV& mv, const std::vector<Scalar>& alphas) { |
| 235 |
mv.scale(alphas); |
| 236 |
} |
| 237 |
|
| 238 |
/** \brief Compute \f$\alpha A^\top B$\f$ and store the result in |
| 239 |
the matrix\f$C\f$ |
| 240 |
*/ |
| 241 |
static void MvTransMv(const Scalar alpha, const MV& A, |
| 242 |
const MV& B, Matrix& C) { |
| 243 |
B.innerProducts(alpha,A,C); |
| 244 |
} |
| 245 |
|
| 246 |
/** \brief Compute the columnwise dot products of A and B and store the |
| 247 |
result in dots |
| 248 |
*/ |
| 249 |
static void MvDot(const MV& A, const MV& B, std::vector<Scalar> &dots) { |
| 250 |
A.dots(B,dots); |
| 251 |
} |
| 252 |
|
| 253 |
/** \brief Compute the columnwise norms of mv and store the |
| 254 |
result in dots |
| 255 |
*/ |
| 256 |
static void MvNorm(const MV& mv, |
| 257 |
std::vector<magnitudeType>& normvec, |
| 258 |
NormType type=TwoNorm) { |
| 259 |
|
| 260 |
ROL_TEST_FOR_EXCEPTION( (type !=TwoNorm) , std::invalid_argument, |
| 261 |
"Belos::MultiVecTraits<Scalar,ROL::MultiVector<Scalar> >::MvNorm()\n" |
| 262 |
"ROL::MultiVector supports only Euclidean norm"); |
| 263 |
|
| 264 |
mv.norms(normvec); |
| 265 |
|
| 266 |
} |
| 267 |
|
| 268 |
/** \brief Copy the specified columns of A into mv |
| 269 |
*/ |
| 270 |
static void SetBlock(const MV& A, const std::vector<int>& index, MV& mv) { |
| 271 |
mv.set(A,index); |
| 272 |
} |
| 273 |
|
| 274 |
/** \brief Copy the contiguous block of columns of A into mv |
| 275 |
*/ |
| 276 |
static void SetBlock(const MV& A, Teuchos::Range1D& index, MV& mv) { |
| 277 |
int n = index.size(); |
| 278 |
int l = index.lbound(); |
| 279 |
|
| 280 |
std::vector<int> stdex(n); |
| 281 |
for( int i=0; i<n; ++i ) { |
| 282 |
stdex[i] = l+i; |
| 283 |
} |
| 284 |
mv.set(A,stdex); |
| 285 |
} |
| 286 |
|
| 287 |
/** \brief Copy the columns of A into mv |
| 288 |
*/ |
| 289 |
static void Assign(const MV& A, MV& mv) { |
| 290 |
mv.set(A); |
| 291 |
} |
| 292 |
|
| 293 |
/** \brief Randomize the vectors in mv (not implemented) |
| 294 |
*/ |
| 295 |
static void MvRandom(MV& mv) { |
| 296 |
ROL_TEST_FOR_EXCEPTION(true,std::logic_error, |
| 297 |
"Belos::MultiVecTraits<Scalar,ROL::MultiVector<Scalar> >::Random()\n" |
| 298 |
"Random initialization not implemented for ROL::MultiVector"); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
static void MvInit(MV& mv, |
| 303 |
const Scalar alpha = ROL::ScalarTraits<Scalar>::zero()) { |
| 304 |
ROL_TEST_FOR_EXCEPTION(alpha != 0,std::invalid_argument, |
| 305 |
"Belos::MultiVecTraits<Scalar,ROL::MultiVector<Scalar> >::MvInit()\n"); |
| 306 |
mv.zero(); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
static void MvPrint(const MV& mv, std::ostream& os) { |
| 311 |
ROL_TEST_FOR_EXCEPTION(true,std::logic_error, |
| 312 |
"Belos::MultiVecTraits<Scalar,ROL::MultiVector<Scalar> >::MvPrint()\n" |
| 313 |
"Print not implemented for ROL::MultiVector"); |
| 314 |
} |
| 315 |
|
| 316 |
#ifdef HAVE_BELOS_TSQR |
| 317 |
/// \typedef tsqr_adaptor_type |
| 318 |
/// \brief TsqrAdaptor specialization for ROL::MultiVector |
| 319 |
typedef RolStubTsqrAdapter<Scalar> tsqr_adaptor_type; |
| 320 |
#endif // HAVE_BELOS_TSQR |
| 321 |
|
| 322 |
}; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
#endif |