| 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 |
/*! \file printCDF.hpp |
| 11 |
\brief Sample and print objective function values in parallel. |
| 12 |
*/ |
| 13 |
|
| 14 |
#ifndef TOPOPT_PRINTCDF_HPP |
| 15 |
#define TOPOPT_PRINTCDF_HPP |
| 16 |
|
| 17 |
#include "Teuchos_Comm.hpp" |
| 18 |
#ifdef HAVE_MPI |
| 19 |
#include "Teuchos_DefaultMpiComm.hpp" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include <iostream> |
| 23 |
#include <algorithm> |
| 24 |
|
| 25 |
#include "ROL_Objective.hpp" |
| 26 |
#include "ROL_SampleGenerator.hpp" |
| 27 |
|
| 28 |
template<class Real> |
| 29 |
void TopOptPrintCDF(ROL::Objective<Real> &obj, |
| 30 |
const ROL::Vector<Real> &z, |
| 31 |
ROL::SampleGenerator<Real> &sampler, |
| 32 |
const int ngsamp, |
| 33 |
const ROL::Ptr<const Teuchos::Comm<int> > &comm, |
| 34 |
const std::string &filename) { |
| 35 |
Real tol(1e-8); |
| 36 |
// Build objective function distribution |
| 37 |
int nsamp = sampler.numMySamples(); |
| 38 |
std::vector<Real> myvalues(nsamp), myzerovec(nsamp, 0); |
| 39 |
std::vector<double> gvalues(ngsamp), gzerovec(ngsamp, 0); |
| 40 |
std::vector<Real> sample = sampler.getMyPoint(0); |
| 41 |
int sdim = sample.size(); |
| 42 |
std::vector<std::vector<Real> > mysamples(sdim, myzerovec); |
| 43 |
std::vector<std::vector<double> > gsamples(sdim, gzerovec); |
| 44 |
for (int i = 0; i < nsamp; ++i) { |
| 45 |
sample = sampler.getMyPoint(i); |
| 46 |
obj.setParameter(sample); |
| 47 |
myvalues[i] = static_cast<double>(obj.value(z,tol)); |
| 48 |
for (int j = 0; j < sdim; ++j) { |
| 49 |
mysamples[j][i] = static_cast<double>(sample[j]); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Send data to root processor |
| 54 |
#ifdef HAVE_MPI |
| 55 |
ROL::Ptr<const Teuchos::MpiComm<int> > mpicomm |
| 56 |
= ROL::dynamicPtrCast<const Teuchos::MpiComm<int> >(comm); |
| 57 |
int nproc = Teuchos::size<int>(*mpicomm); |
| 58 |
std::vector<int> sampleCounts(nproc, 0), sampleDispls(nproc, 0); |
| 59 |
MPI_Gather(&nsamp,1,MPI_INT,&sampleCounts[0],1,MPI_INT,0,*(mpicomm->getRawMpiComm())()); |
| 60 |
for (int i = 1; i < nproc; ++i) { |
| 61 |
sampleDispls[i] = sampleDispls[i-1] + sampleCounts[i-1]; |
| 62 |
} |
| 63 |
MPI_Gatherv(&myvalues[0],nsamp,MPI_DOUBLE,&gvalues[0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())()); |
| 64 |
for (int j = 0; j < sdim; ++j) { |
| 65 |
MPI_Gatherv(&mysamples[j][0],nsamp,MPI_DOUBLE,&gsamples[j][0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())()); |
| 66 |
} |
| 67 |
#else |
| 68 |
gvalues.assign(myvalues.begin(),myvalues.end()); |
| 69 |
for (int j = 0; j < sdim; ++j) { |
| 70 |
gsamples[j].assign(mysamples[j].begin(),mysamples[j].end()); |
| 71 |
} |
| 72 |
#endif |
| 73 |
|
| 74 |
// Print |
| 75 |
int rank = Teuchos::rank<int>(*comm); |
| 76 |
if ( rank==0 ) { |
| 77 |
std::ofstream file; |
| 78 |
file.open(filename); |
| 79 |
file << std::scientific << std::setprecision(15); |
| 80 |
for (int i = 0; i < ngsamp; ++i) { |
| 81 |
for (int j = 0; j < sdim; ++j) { |
| 82 |
file << std::setw(25) << std::left << gsamples[j][i]; |
| 83 |
} |
| 84 |
file << std::setw(25) << std::left << gvalues[i] << std::endl; |
| 85 |
} |
| 86 |
file.close(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#endif |