ROL
ROL_BatchStdVector.hpp
Go to the documentation of this file.
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 #ifndef ROL_BATCHSTDVECTOR_H
11 #define ROL_BATCHSTDVECTOR_H
12 
13 #include "ROL_StdVector.hpp"
14 #include "ROL_BatchManager.hpp"
15 
21 namespace ROL {
22 
23 template <class Real>
24 class BatchStdVector : public StdVector<Real> {
26 private:
27  const Ptr<BatchManager<Real>> bman_;
28 
29 public:
30  BatchStdVector(const Ptr<std::vector<Real>> &vec,
31  const Ptr<BatchManager<Real>> &bman)
32  : StdVector<Real>(vec), bman_(bman) {}
33 
34  virtual Real dot(const Vector<Real> &x) const {
35  const std::vector<Real> &xval = *(dynamic_cast<const StdVector<Real>&>(x).getVector());
36  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
37  uint numMySamples = yval.size();
38  ROL_TEST_FOR_EXCEPTION( xval.size() != numMySamples, std::invalid_argument,
39  "Error: Vectors must have the same dimension." );
40  Real val(0), sum_val(0);
41  for (uint i = 0; i < numMySamples; ++i) {
42  val += xval[i] * yval[i];
43  }
44  // Global sum
45  bman_->sumAll(&val,&sum_val,1);
46  return sum_val;
47  }
48 
49  virtual Ptr<Vector<Real>> clone(void) const {
50  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
51  uint numMySamples = yval.size();
52  return makePtr<BatchStdVector>(
53  makePtr<std::vector<Real>>(numMySamples),bman_);
54  }
55 
56  int dimension(void) const {
57  Real dim = (Real)StdVector<Real>::dimension();
58  Real sum = 0.;
59  bman_->sumAll(&dim,&sum,1);
60  return (int)sum;
61  }
62 
63  Real reduce(const Elementwise::ReductionOp<Real> &r) const {
64  const std::vector<Real> &yval = *(StdVector<Real>::getVector());
65  uint numMySamples = yval.size();
66  Real result = r.initialValue();
67  for (uint i = 0; i < numMySamples; i++) {
68  r.reduce(yval[i],result);
69  }
70  // Global sum
71  Real sum = 0.;
72  bman_->reduceAll(&result,&sum,1,r);
73  return sum;
74  }
75 
76  const Ptr<BatchManager<Real>> getBatchManager(void) const {
77  return bman_;
78  }
79 };
80 
81 } // namespace ROL
82 
83 #endif
typename PV< Real >::size_type size_type
virtual Real dot(const Vector< Real > &x) const
Compute where .
Provides the std::vector implementation of the ROL::Vector interface.
Ptr< const std::vector< Real > > getVector() const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual Ptr< Vector< Real > > clone(void) const
Clone to make a new (uninitialized) vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
const Ptr< BatchManager< Real > > bman_
BatchStdVector(const Ptr< std::vector< Real >> &vec, const Ptr< BatchManager< Real >> &bman)
const Ptr< BatchManager< Real > > getBatchManager(void) const
std::vector< Real >::size_type uint
Real reduce(const Elementwise::ReductionOp< Real > &r) const
int dimension(void) const
Return dimension of the vector space.
constexpr auto dim