| 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 ROLTEUCHOSBATCHMANAGER_HPP |
| 11 |
#define ROLTEUCHOSBATCHMANAGER_HPP |
| 12 |
|
| 13 |
#include "ROL_BatchManager.hpp" |
| 14 |
#include "ROL_Ptr.hpp" |
| 15 |
#include "Teuchos_Comm.hpp" |
| 16 |
#include "Teuchos_CommHelpers.hpp" |
| 17 |
|
| 18 |
namespace ROL { |
| 19 |
|
| 20 |
template<class Real, class Ordinal> |
| 21 |
class TeuchosBatchManager : public BatchManager<Real> { |
| 22 |
private: |
| 23 |
const ROL::Ptr<const Teuchos::Comm<int> > comm_; |
| 24 |
|
| 25 |
public: |
| 26 |
TeuchosBatchManager(const ROL::Ptr<const Teuchos::Comm<int> > &comm) |
| 27 |
: comm_(comm) {} |
| 28 |
|
| 29 |
int batchID(void) { |
| 30 |
return Teuchos::rank<int>(*comm_); |
| 31 |
} |
| 32 |
|
| 33 |
int numBatches(void) { |
| 34 |
return Teuchos::size<int>(*comm_); |
| 35 |
} |
| 36 |
|
| 37 |
void reduceAll(Real* input, Real* output, int dim, |
| 38 |
const Elementwise::ReductionOp<Real> &r) { |
| 39 |
int nB = this->numBatches(); |
| 40 |
std::vector<Real> receiveBuffer(nB); |
| 41 |
Teuchos::gather<int,Real>(input,1,&receiveBuffer[0],1,0,*comm_); |
| 42 |
output[0] = r.initialValue(); |
| 43 |
for (int i = 0; i < nB; i++) { |
| 44 |
r.reduce(receiveBuffer[i],output[0]); |
| 45 |
} |
| 46 |
Teuchos::broadcast<int,Real>(*comm_,0,1,output); |
| 47 |
} |
| 48 |
|
| 49 |
void minAll(Real* input, Real* output, int dim) { |
| 50 |
Teuchos::reduceAll<int,Real>(*comm_,Teuchos::REDUCE_MIN, |
| 51 |
dim, input, output); |
| 52 |
} |
| 53 |
|
| 54 |
void maxAll(Real* input, Real* output, int dim) { |
| 55 |
Teuchos::reduceAll<int,Real>(*comm_,Teuchos::REDUCE_MAX, |
| 56 |
dim, input, output); |
| 57 |
} |
| 58 |
|
| 59 |
void sumAll(Real* input, Real* output, int dim) { |
| 60 |
Teuchos::reduceAll<int,Real>(*comm_,Teuchos::REDUCE_SUM, |
| 61 |
dim, input, output); |
| 62 |
} |
| 63 |
|
| 64 |
void gatherAll(const Real* send, const int ssize, Real *receive, const int rsize) const { |
| 65 |
Teuchos::gatherAll<int,Real>(*comm_,ssize,send,rsize,receive); |
| 66 |
} |
| 67 |
|
| 68 |
void broadcast(Real* input, int cnt, int root) { |
| 69 |
Teuchos::broadcast<int,Real>(*comm_,root,cnt,input); |
| 70 |
} |
| 71 |
|
| 72 |
virtual void sumAll(Vector<Real> &input, Vector<Real> &output) { |
| 73 |
ROL_TEST_FOR_EXCEPTION( true, std::invalid_argument, |
| 74 |
">>> ERROR (ROL::TeuchosBatchManager): sumAll(Vector<Real> &input, Vector<Real> &output) is not implemented"); |
| 75 |
} |
| 76 |
|
| 77 |
void barrier(void) { |
| 78 |
Teuchos::barrier<int>(*comm_); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
#endif |