queso-0.53.0
Private Member Functions | Private Attributes | List of all members
QUESO::ScalarSequence< T > Class Template Reference

Class for handling scalar samples. More...

#include <ScalarSequence.h>

Collaboration diagram for QUESO::ScalarSequence< T >:
Collaboration graph
[legend]

Public Types

Class typedefs
typedef std::vector< T >::iterator seqScalarPositionIteratorTypedef
 
typedef std::vector< T >
::const_iterator 
seqScalarPositionConstIteratorTypedef
 

Public Member Functions

Constructor/Destructor methods
 ScalarSequence (const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
 Default constructor. More...
 
 ~ScalarSequence ()
 Destructor. More...
 
Set methods
ScalarSequence< T > & operator= (const ScalarSequence< T > &rhs)
 Assignment operator; it copies rhs to this. More...
 
Access methods
const T & operator[] (unsigned int posId) const
 Access position posId of the sequence of scalars (const). More...
 
T & operator[] (unsigned int posId)
 Access position posId of the sequence of scalars (non-const). More...
 
Sequence methods
const BaseEnvironmentenv () const
 Access to QUESO environment. More...
 
const std::string & name () const
 Access to the name of the sequence of scalars. More...
 
void setName (const std::string &newName)
 Sets a new name to the sequence of scalars. More...
 
void clear ()
 Clears the sequence of scalars. More...
 
unsigned int subSequenceSize () const
 Size of the sub-sequence of scalars. More...
 
unsigned int unifiedSequenceSize (bool useOnlyInter0Comm) const
 Size of the unified sequence of scalars. More...
 
void resizeSequence (unsigned int newSequenceSize)
 Resizes the size of the sequence of scalars. More...
 
void resetValues (unsigned int initialPos, unsigned int)
 Sets numPos values of the sequence to zero, starting at position initialPos. More...
 
void erasePositions (unsigned int initialPos, unsigned int numPos)
 Erases numPos values of the sequence, starting at position initialPos. More...
 
void getUnifiedContentsAtProc0Only (bool useOnlyInter0Comm, std::vector< T > &outputVec) const
 Gets the unified contents of processor of rank equals to 0. More...
 
const T & subMinPlain () const
 Finds the minimum value of the sub-sequence of scalars. More...
 
const T & unifiedMinPlain (bool useOnlyInter0Comm) const
 Finds the minimum value of the unified sequence of scalars. More...
 
const T & subMaxPlain () const
 Finds the maximum value of the sub-sequence of scalars. More...
 
const T & unifiedMaxPlain (bool useOnlyInter0Comm) const
 Finds the maximum value of the unified sequence of scalars. More...
 
const T & subMeanPlain () const
 Finds the mean value of the sub-sequence of scalars. More...
 
const T & unifiedMeanPlain (bool useOnlyInter0Comm) const
 Finds the mean value of the unified sequence of scalars. More...
 
const T & subMedianPlain () const
 Finds the median value of the sub-sequence of scalars. More...
 
const T & unifiedMedianPlain (bool useOnlyInter0Comm) const
 Finds the median value of the unified sequence of scalars. More...
 
const T & subSampleVariancePlain () const
 Finds the variance of a sample of the sub-sequence of scalars. More...
 
const T & unifiedSampleVariancePlain (bool useOnlyInter0Comm) const
 Finds the variance of a sample of the unified sequence of scalars. More...
 
void deleteStoredScalars ()
 Deletes all stored scalars. More...
 
void setGaussian (const T &mean, const T &stdDev)
 Sets the values of the sequence as a Gaussian distribution of mean given by meanVec and standard deviation by stdDevVec. More...
 
void setUniform (const T &a, const T &b)
 Sets the values of the sequence as a uniform distribution between the values given by vectors aVec and bVec. More...
 
void subUniformlySampledCdf (unsigned int numIntervals, T &minDomainValue, T &maxDomainValue, std::vector< T > &cdfValues) const
 Uniformly samples from the CDF from the sub-sequence. More...
 
void unifiedUniformlySampledCdf (bool useOnlyInter0Comm, unsigned int numIntervals, T &unifiedMinDomainValue, T &unifiedMaxDomainValue, std::vector< T > &unifiedCdfValues) const
 Uniformly samples from the CDF from the unified sequence. More...
 
void subBasicCdf (unsigned int numIntervals, UniformOneDGrid< T > *&gridValues, std::vector< T > &cdfValues) const
 Finds the Cumulative Distribution Function (CDF) of the sub-sequence of scalars. More...
 
void subWeightCdf (unsigned int numIntervals, std::vector< T > &gridValues, std::vector< T > &cdfValues) const
 Finds the Weighted Cumulative Distribution Function (CDF) of the sub-sequence of scalars. More...
 
void subWeightCdf (unsigned int numIntervals, UniformOneDGrid< T > *&gridValues, std::vector< T > &cdfValues) const
 Finds the Weighted Cumulative Distribution Function (CDF) of the sub-sequence of scalars. More...
 
subMeanExtra (unsigned int initialPos, unsigned int numPos) const
 Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPos. More...
 
unifiedMeanExtra (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos) const
 Finds the mean value of the unified sequence of numPos positions starting at position initialPos. More...
 
subMedianExtra (unsigned int initialPos, unsigned int numPos) const
 Finds the median value of the sub-sequence, considering numPos positions starting at position initialPos. More...
 
unifiedMedianExtra (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos) const
 Finds the median value of the unified sequence, considering numPos positions starting at position initialPos. More...
 
subSampleVarianceExtra (unsigned int initialPos, unsigned int numPos, const T &meanValue) const
 Finds the sample variance of the sub-sequence, considering numPos positions starting at position initialPos and of mean meanVec. More...
 
unifiedSampleVarianceExtra (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos, const T &unifiedMeanValue) const
 Finds the sample variance of the unified sequence, considering numPos positions starting at position initialPos and of mean meanVec. More...
 
subSampleStd (unsigned int initialPos, unsigned int numPos, const T &meanValue) const
 Finds the sample standard deviation of the unified sequence, considering numPos positions starting at position initialPos and of mean meanValue. More...
 
unifiedSampleStd (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos, const T &unifiedMeanValue) const
 Finds the sample standard deviation of the unified sequence, considering localnumPos positions starting at position initialPos and of mean unifiedMeanValue. More...
 
subPopulationVariance (unsigned int initialPos, unsigned int numPos, const T &meanValue) const
 Finds the population variance of the sub-sequence, considering numPos positions starting at position initialPos and of mean meanValue. More...
 
unifiedPopulationVariance (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, const T &unifiedMeanValue) const
 Finds the population variance of the unified sequence, considering numPos positions starting at position initialPos and of mean meanValue. More...
 
autoCovariance (unsigned int initialPos, unsigned int numPos, const T &meanValue, unsigned int lag) const
 Calculates the autocovariance. More...
 
autoCorrViaDef (unsigned int initialPos, unsigned int numPos, unsigned int lag) const
 Calculates the autocorrelation via definition. More...
 
void autoCorrViaFft (unsigned int initialPos, unsigned int numPos, unsigned int maxLag, std::vector< T > &autoCorrs) const
 Calculates the autocorrelation via Fast Fourier transforms (FFT). More...
 
void autoCorrViaFft (unsigned int initialPos, unsigned int numPos, unsigned int numSum, T &autoCorrsSum) const
 Calculates the sum of autocorrelation via Fast Fourier transforms (FFT). More...
 
void subMinMaxExtra (unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
 Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting at position initialPos. More...
 
void unifiedMinMaxExtra (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, T &unifiedMinValue, T &unifiedMaxValue) const
 Finds the minimum and the maximum values of the unified sequence, considering numPos positions starting at position initialPos. More...
 
void subHistogram (unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, std::vector< T > &centers, std::vector< unsigned int > &bins) const
 Calculates the histogram of the sub-sequence. More...
 
void unifiedHistogram (bool useOnlyInter0Comm, unsigned int initialPos, const T &unifiedMinHorizontalValue, const T &unifiedMaxHorizontalValue, std::vector< T > &unifiedCenters, std::vector< unsigned int > &unifiedBins) const
 Calculates the histogram of the unified sequence. More...
 
void subBasicHistogram (unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, UniformOneDGrid< T > *&gridValues, std::vector< unsigned int > &bins) const
 Calculates the histogram of the sub-sequence. More...
 
void subWeightHistogram (unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, UniformOneDGrid< T > *&gridValues, std::vector< unsigned int > &bins) const
 Calculates the weighted histogram of the sub-sequence. More...
 
void subWeightHistogram (unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, std::vector< T > &gridValues, std::vector< unsigned int > &bins) const
 Calculates the weighted histogram of the sub-sequence. More...
 
void subSort (unsigned int initialPos, ScalarSequence< T > &sortedSequence) const
 Sorts the sub-sequence of scalars. More...
 
void unifiedSort (bool useOnlyInter0Comm, unsigned int initialPos, ScalarSequence< T > &unifiedSortedSequence) const
 Sorts the unified sequence of scalars. More...
 
subInterQuantileRange (unsigned int initialPos) const
 Returns the interquartile range of the values in the sub-sequence. More...
 
unifiedInterQuantileRange (bool useOnlyInter0Comm, unsigned int initialPos) const
 Returns the interquartile range of the values in the unified sequence. More...
 
subScaleForKde (unsigned int initialPos, const T &iqrValue, unsigned int kdeDimension) const
 Selects the scales (output value) for the kernel density estimation, considering only the sub-sequence. More...
 
unifiedScaleForKde (bool useOnlyInter0Comm, unsigned int initialPos, const T &unifiedIqrValue, unsigned int kdeDimension) const
 Selects the scales (bandwidth) for the kernel density estimation, considering the unified sequence. More...
 
void subGaussian1dKde (unsigned int initialPos, double scaleValue, const std::vector< T > &evaluationPositions, std::vector< double > &densityValues) const
 Gaussian kernel for the KDE estimate of the sub-sequence. More...
 
void unifiedGaussian1dKde (bool useOnlyInter0Comm, unsigned int initialPos, double unifiedScaleValue, const std::vector< T > &unifiedEvaluationPositions, std::vector< double > &unifiedDensityValues) const
 Gaussian kernel for the KDE estimate of the unified sequence. More...
 
void filter (unsigned int initialPos, unsigned int spacing)
 Filters positions in the sequence of vectors. More...
 
brooksGelmanConvMeasure (bool useOnlyInter0Comm, unsigned int initialPos, unsigned int spacing) const
 Estimates convergence rate using Brooks & Gelman method. More...
 
void append (const ScalarSequence< T > &src, unsigned int srcInitialPos, unsigned int srcNumPos)
 Appends the scalar sequence src to this sequence. More...
 
subPositionsOfMaximum (const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &subPositionsOfMaximum)
 Finds the positions where the maximum element occurs in the sub-sequence. More...
 
unifiedPositionsOfMaximum (const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &unifiedPositionsOfMaximum)
 Finds the positions where the maximum element occurs in the unified sequence. More...
 
void subWriteContents (unsigned int initialPos, unsigned int numPos, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
 Writes the sub-sequence to a file. More...
 
void subWriteContents (unsigned int initialPos, unsigned int numPos, std::ofstream &ofs, const std::string &fileType) const
 Writes the sub-sequence to a file. More...
 
void unifiedWriteContents (const std::string &fileName, const std::string &fileType) const
 Writes the unified sequence to a file. More...
 
void unifiedReadContents (const std::string &fileName, const std::string &fileType, const unsigned int subSequenceSize)
 Reads the unified sequence from a file. More...
 

Private Member Functions

void copy (const ScalarSequence< T > &src)
 Copies the scalar sequence src to this. More...
 
void extractScalarSeq (unsigned int initialPos, unsigned int spacing, unsigned int numPos, ScalarSequence< T > &scalarSeq) const
 Extracts a sequence of scalars. More...
 
void extractRawData (unsigned int initialPos, unsigned int spacing, unsigned int numPos, std::vector< double > &rawData) const
 Extracts the raw data. More...
 
std::vector< T > & rawData ()
 The sequence of scalars. Access to private attribute m_seq. More...
 
void subSort ()
 Sorts the sequence of scalars in the private attribute m_seq. More...
 
void parallelMerge (std::vector< T > &sortedBuffer, const std::vector< T > &leafData, unsigned int treeLevel) const
 Sorts/merges data in parallel using MPI. More...
 

Private Attributes

const BaseEnvironmentm_env
 
std::string m_name
 
std::vector< T > m_seq
 
T * m_subMinPlain
 
T * m_unifiedMinPlain
 
T * m_subMaxPlain
 
T * m_unifiedMaxPlain
 
T * m_subMeanPlain
 
T * m_unifiedMeanPlain
 
T * m_subMedianPlain
 
T * m_unifiedMedianPlain
 
T * m_subSampleVariancePlain
 
T * m_unifiedSampleVariancePlain
 

Detailed Description

template<class T = double>
class QUESO::ScalarSequence< T >

Class for handling scalar samples.

This class handles scalar samples generated by an algorithm, as well as operations that can be carried over them, e.g., calculation of means, correlation and covariance matrices.

Definition at line 54 of file ScalarSequence.h.

Member Typedef Documentation

template<class T = double>
typedef std::vector<T>::const_iterator QUESO::ScalarSequence< T >::seqScalarPositionConstIteratorTypedef

Definition at line 60 of file ScalarSequence.h.

template<class T = double>
typedef std::vector<T>::iterator QUESO::ScalarSequence< T >::seqScalarPositionIteratorTypedef

Definition at line 59 of file ScalarSequence.h.

Constructor & Destructor Documentation

template<class T >
QUESO::ScalarSequence< T >::ScalarSequence ( const BaseEnvironment env,
unsigned int  subSequenceSize,
const std::string &  name 
)

Default constructor.

Definition at line 32 of file ScalarSequence.C.

36  :
37  m_env (env),
38  m_name (name),
40  m_subMinPlain (NULL),
41  m_unifiedMinPlain (NULL),
42  m_subMaxPlain (NULL),
43  m_unifiedMaxPlain (NULL),
44  m_subMeanPlain (NULL),
45  m_unifiedMeanPlain (NULL),
46  m_subMedianPlain (NULL),
47  m_unifiedMedianPlain (NULL),
50 {
51 }
const BaseEnvironment & m_env
const std::string & name() const
Access to the name of the sequence of scalars.
std::vector< T > m_seq
const BaseEnvironment & env() const
Access to QUESO environment.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
QUESO::ScalarSequence< T >::~ScalarSequence ( )

Destructor.

Definition at line 54 of file ScalarSequence.C.

55 {
57 }
void deleteStoredScalars()
Deletes all stored scalars.

Member Function Documentation

template<class T>
void QUESO::ScalarSequence< T >::append ( const ScalarSequence< T > &  src,
unsigned int  srcInitialPos,
unsigned int  srcNumPos 
)

Appends the scalar sequence src to this sequence.

This routine deletes all stored computed scalars.

Definition at line 2497 of file ScalarSequence.C.

References QUESO::ScalarSequence< T >::m_seq, queso_require_greater_equal_msg, and QUESO::ScalarSequence< T >::subSequenceSize().

Referenced by QUESO::MLSampling< P_V, P_M >::generateBalLinkedChains_all(), and QUESO::MLSampling< P_V, P_M >::generateUnbLinkedChains_all().

2501 {
2502  queso_require_greater_equal_msg(src.subSequenceSize(), (srcInitialPos+1), "srcInitialPos is too big");
2503 
2504  queso_require_greater_equal_msg(src.subSequenceSize(), (srcInitialPos+srcNumPos), "srcNumPos is too big");
2505 
2507  unsigned int currentSize = this->subSequenceSize();
2508  m_seq.resize(currentSize+srcNumPos,0.);
2509  for (unsigned int i = 0; i < srcNumPos; ++i) {
2510  m_seq[currentSize+i] = src.m_seq[srcInitialPos+i];
2511  }
2512 
2513  return;
2514 }
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::autoCorrViaDef ( unsigned int  initialPos,
unsigned int  numPos,
unsigned int  lag 
) const

Calculates the autocorrelation via definition.

Autocorrelation is the cross-correlation of a variable with itself; it describes the correlation between values of the process at different times, as a function of the two times. It is calculated over a sequence of vectors with initial position initialPos, considering numPos positions, a lag of lag, with mean given by meanValue. Output: the calculated autocorrelations of the sequence of vectors.

Definition at line 1332 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::autoCorrViaDef().

1336 {
1337  bool bRC = ((initialPos < this->subSequenceSize()) &&
1338  (0 < numPos ) &&
1339  ((initialPos+numPos) <= this->subSequenceSize()) &&
1340  (lag < numPos )); // lag should not be too large
1341  queso_require_msg(bRC, "invalid input data");
1342 
1343  T meanValue = this->subMeanExtra(initialPos,
1344  numPos);
1345 
1346  T covValueZero = this->autoCovariance(initialPos,
1347  numPos,
1348  meanValue,
1349  0); // lag
1350 
1351  T corrValue = this->autoCovariance(initialPos,
1352  numPos,
1353  meanValue,
1354  lag);
1355 
1356  return corrValue/covValueZero;
1357 }
T autoCovariance(unsigned int initialPos, unsigned int numPos, const T &meanValue, unsigned int lag) const
Calculates the autocovariance.
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::autoCorrViaFft ( unsigned int  initialPos,
unsigned int  numPos,
unsigned int  maxLag,
std::vector< T > &  autoCorrs 
) const

Calculates the autocorrelation via Fast Fourier transforms (FFT).

Definition at line 1361 of file ScalarSequence.C.

References QUESO::Fft< T >::forward(), and QUESO::Fft< T >::inverse().

Referenced by QUESO::ArrayOfSequences< V, M >::autoCorrViaFft(), and QUESO::SequenceOfVectors< V, M >::autoCorrViaFft().

1366 {
1367  unsigned int fftSize = 0;
1368  {
1369  double tmp = log((double) maxLag)/log(2.);
1370  double fractionalPart = tmp - ((double) ((unsigned int) tmp));
1371  if (fractionalPart > 0.) tmp += (1. - fractionalPart);
1372  unsigned int fftSize1 = (unsigned int) std::pow(2.,tmp+1.); // Yes, tmp+1
1373  fftSize1 = fftSize1; // To remove warning
1374 
1375  tmp = log((double) numPos)/log(2.);
1376  fractionalPart = tmp - ((double) ((unsigned int) tmp));
1377  if (fractionalPart > 0.) tmp += (1. - fractionalPart);
1378  unsigned int fftSize2 = (unsigned int) std::pow(2.,tmp+1);
1379 
1380  fftSize = fftSize2;
1381  }
1382 
1383  std::vector<double> rawDataVec(numPos,0.);
1384  std::vector<std::complex<double> > resultData(0,std::complex<double>(0.,0.));
1385  Fft<T> fftObj(m_env);
1386 
1387  // Forward FFT
1388  this->extractRawData(initialPos,
1389  1, // spacing
1390  numPos,
1391  rawDataVec);
1392  T meanValue = this->subMeanExtra(initialPos,
1393  numPos);
1394  for (unsigned int j = 0; j < numPos; ++j) {
1395  rawDataVec[j] -= meanValue; // IMPORTANT
1396  }
1397 
1398  rawDataVec.resize(fftSize,0.);
1399 
1400  //if (m_env.subDisplayFile()) {
1401  // *m_env.subDisplayFile() << "In ScalarSequence<T>::autoCorrViaFft()"
1402  // << ": about to call fftObj.forward()"
1403  // << " with rawDataVec.size() = " << rawDataVec.size()
1404  // << ", fftSize = " << fftSize
1405  // << ", resultData.size() = " << resultData.size()
1406  // << std::endl;
1407  //}
1408  fftObj.forward(rawDataVec,fftSize,resultData);
1409 
1410  // Inverse FFT
1411  for (unsigned int j = 0; j < fftSize; ++j) {
1412  rawDataVec[j] = std::norm(resultData[j]);
1413  }
1414  //if (m_env.subDisplayFile()) {
1415  // *m_env.subDisplayFile() << "In ScalarSequence<T>::autoCorrViaFft()"
1416  // << ": about to call fftObj.inverse()"
1417  // << " with rawDataVec.size() = " << rawDataVec.size()
1418  // << ", fftSize = " << fftSize
1419  // << ", resultData.size() = " << resultData.size()
1420  // << std::endl;
1421  //}
1422  fftObj.inverse(rawDataVec,fftSize,resultData);
1423  //if (m_env.subDisplayFile()) {
1424  // *m_env.subDisplayFile() << "In ScalarSequence<T>::autoCorrViaFft()"
1425  // << ": returned succesfully from fftObj.inverse()"
1426  // << std::endl;
1427  //}
1428 
1429  // Prepare return data
1430  autoCorrs.resize(maxLag+1,0.); // Yes, +1
1431  for (unsigned int j = 0; j < autoCorrs.size(); ++j) {
1432  double ratio = ((double) j)/((double) (numPos-1));
1433  autoCorrs[j] = ( resultData[j].real()/resultData[0].real() )*(1.-ratio);
1434  }
1435 
1436  return;
1437 }
const BaseEnvironment & m_env
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
void extractRawData(unsigned int initialPos, unsigned int spacing, unsigned int numPos, std::vector< double > &rawData) const
Extracts the raw data.
template<class T>
void QUESO::ScalarSequence< T >::autoCorrViaFft ( unsigned int  initialPos,
unsigned int  numPos,
unsigned int  numSum,
T &  autoCorrsSum 
) const

Calculates the sum of autocorrelation via Fast Fourier transforms (FFT).

Definition at line 1441 of file ScalarSequence.C.

References QUESO::Fft< T >::forward(), and QUESO::Fft< T >::inverse().

1446 {
1447  //if (m_env.subDisplayFile()) {
1448  // *m_env.subDisplayFile() << "Entering ScalarSequence<T>::autoCorrViaFft(), for sum"
1449  // << ": initialPos = " << initialPos
1450  // << ", numPos = " << numPos
1451  // << std::endl;
1452  //}
1453 
1454  double tmp = log((double) numPos)/log(2.);
1455  double fractionalPart = tmp - ((double) ((unsigned int) tmp));
1456  if (fractionalPart > 0.) tmp += (1. - fractionalPart);
1457  unsigned int fftSize = (unsigned int) std::pow(2.,tmp+1);
1458 
1459  std::vector<double> rawDataVec(numPos,0.);
1460  std::vector<std::complex<double> > resultData(0,std::complex<double>(0.,0.));
1461  Fft<T> fftObj(m_env);
1462 
1463  // Forward FFT
1464  this->extractRawData(initialPos,
1465  1, // spacing
1466  numPos,
1467  rawDataVec);
1468  T meanValue = this->subMeanExtra(initialPos,
1469  numPos);
1470  for (unsigned int j = 0; j < numPos; ++j) {
1471  rawDataVec[j] -= meanValue; // IMPORTANT
1472  }
1473  rawDataVec.resize(fftSize,0.);
1474 
1475  //if (m_env.subDisplayFile()) {
1476  // *m_env.subDisplayFile() << "In ScalarSequence<T>::autoCorrViaFft(), for sum"
1477  // << ": about to call fftObj.forward()"
1478  // << " with rawDataVec.size() = " << rawDataVec.size()
1479  // << ", fftSize = " << fftSize
1480  // << ", resultData.size() = " << resultData.size()
1481  // << std::endl;
1482  //}
1483  fftObj.forward(rawDataVec,fftSize,resultData);
1484 
1485  // Inverse FFT
1486  for (unsigned int j = 0; j < fftSize; ++j) {
1487  rawDataVec[j] = std::norm(resultData[j]);
1488  }
1489  fftObj.inverse(rawDataVec,fftSize,resultData);
1490 
1491  //if (m_env.subDisplayFile()) {
1492  // *m_env.subDisplayFile() << "In ScalarSequence<T>::autoCorrViaFft(), for sum"
1493  // << ": computed auto covariance for lag 0 = " << resultData[0].real()/((double) (numPos))
1494  // << ", computed resultData[0].imag() = " << resultData[0].imag()
1495  // << std::endl;
1496  //}
1497 
1498  // Prepare return data
1499  autoCorrsSum = 0.;
1500  for (unsigned int j = 0; j < numSum; ++j) { // Yes, begin at lag '0'
1501  double ratio = ((double) j)/((double) (numPos-1));
1502  autoCorrsSum += ( resultData[j].real()/resultData[0].real() )*(1.-ratio);
1503  }
1504 
1505  return;
1506 }
const BaseEnvironment & m_env
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
void extractRawData(unsigned int initialPos, unsigned int spacing, unsigned int numPos, std::vector< double > &rawData) const
Extracts the raw data.
template<class T>
T QUESO::ScalarSequence< T >::autoCovariance ( unsigned int  initialPos,
unsigned int  numPos,
const T &  meanValue,
unsigned int  lag 
) const

Calculates the autocovariance.

The autocovariance is the covariance of a variable with itself at some other time. It is calculated over a sequence of scalars with initial position initialPos, considering numPos positions, a lag of lag, with mean given by meanValue. Output: the calculated autocovariances of the sequence of scalars.

Definition at line 1302 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::autoCovariance().

1307 {
1308  bool bRC = ((initialPos < this->subSequenceSize()) &&
1309  (0 < numPos ) &&
1310  ((initialPos+numPos) <= this->subSequenceSize()) &&
1311  (lag < numPos )); // lag should not be too large
1312  queso_require_msg(bRC, "invalid input data");
1313 
1314  unsigned int loopSize = numPos - lag;
1315  unsigned int finalPosPlus1 = initialPos + loopSize;
1316  T diff1;
1317  T diff2;
1318  T covValue = 0.;
1319  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1320  diff1 = m_seq[j ] - meanValue;
1321  diff2 = m_seq[j+lag] - meanValue;
1322  covValue += diff1*diff2;
1323  }
1324 
1325  covValue /= (T) loopSize;
1326 
1327  return covValue;
1328 }
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::brooksGelmanConvMeasure ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  spacing 
) const

Estimates convergence rate using Brooks & Gelman method.

TODO: implement me!

Definition at line 2468 of file ScalarSequence.C.

References queso_error_msg, and queso_not_implemented.

2472 {
2473  double resultValue = 0.;
2474 
2475  // FIX ME: put logic if (numSubEnvs == 1) ...
2476 
2477  if (useOnlyInter0Comm) {
2478  if (m_env.inter0Rank() >= 0) {
2480  }
2481  else {
2482  // Node not in the 'inter0' communicator
2483  // Do nothing
2484  }
2485  }
2486  else {
2487  queso_error_msg("parallel vectors not supported yet");
2488  }
2489 
2490  //m_env.fullComm().Barrier();
2491 
2492  return resultValue;
2493 }
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_not_implemented()
Definition: asserts.h:56
template<class T >
void QUESO::ScalarSequence< T >::clear ( )

Clears the sequence of scalars.

Resets its values and then its size to zero.

Definition at line 123 of file ScalarSequence.C.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence_Step02_inter0().

124 {
125  unsigned int numPos = this->subSequenceSize();
126  if (numPos) {
127  this->resetValues(0,numPos);
128  this->resizeSequence(0);
129  }
130 
131  return;
132 }
void resizeSequence(unsigned int newSequenceSize)
Resizes the size of the sequence of scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void resetValues(unsigned int initialPos, unsigned int)
Sets numPos values of the sequence to zero, starting at position initialPos.
template<class T>
void QUESO::ScalarSequence< T >::copy ( const ScalarSequence< T > &  src)
private

Copies the scalar sequence src to this.

This routine deletes all stored computed scalars.

Definition at line 3102 of file ScalarSequence.C.

References QUESO::ScalarSequence< T >::m_name, QUESO::ScalarSequence< T >::m_seq, and QUESO::ScalarSequence< T >::subSequenceSize().

3103 {
3104  m_name = src.m_name;
3105  m_seq.clear();
3106  m_seq.resize(src.subSequenceSize(),0.);
3107  for (unsigned int i = 0; i < m_seq.size(); ++i) {
3108  m_seq[i] = src.m_seq[i];
3109  }
3111 
3112  return;
3113 }
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
template<class T >
void QUESO::ScalarSequence< T >::deleteStoredScalars ( )

Deletes all stored scalars.

Definition at line 465 of file ScalarSequence.C.

466 {
467  if (m_subMinPlain) {
468  delete m_subMinPlain;
469  m_subMinPlain = NULL;
470  }
471  if (m_unifiedMinPlain) {
472  delete m_unifiedMinPlain;
473  m_unifiedMinPlain = NULL;
474  }
475  if (m_subMaxPlain) {
476  delete m_subMaxPlain;
477  m_subMaxPlain = NULL;
478  }
479  if (m_unifiedMaxPlain) {
480  delete m_unifiedMaxPlain;
481  m_unifiedMaxPlain = NULL;
482  }
483  if (m_subMeanPlain) {
484  delete m_subMeanPlain;
485  m_subMeanPlain = NULL;
486  }
487  if (m_unifiedMeanPlain) {
488  delete m_unifiedMeanPlain;
489  m_unifiedMeanPlain = NULL;
490  }
491  if (m_subMedianPlain) {
492  delete m_subMedianPlain;
493  m_subMedianPlain = NULL;
494  }
495  if (m_unifiedMedianPlain) {
496  delete m_unifiedMedianPlain;
497  m_unifiedMedianPlain = NULL;
498  }
502  }
506  }
507 
508  return;
509 }
template<class T >
const BaseEnvironment & QUESO::ScalarSequence< T >::env ( ) const

Access to QUESO environment.

Definition at line 101 of file ScalarSequence.C.

Referenced by QUESO::ComputeCovCorrBetweenScalarSequences(), and QUESO::ComputeUnifiedGaussian2dKde().

102 {
103  return m_env;
104 }
const BaseEnvironment & m_env
template<class T >
void QUESO::ScalarSequence< T >::erasePositions ( unsigned int  initialPos,
unsigned int  numPos 
)

Erases numPos values of the sequence, starting at position initialPos.

This routine deletes all stored computed scalars.

Definition at line 206 of file ScalarSequence.C.

References queso_require_equal_to_msg, and queso_require_msg.

Referenced by QUESO::ArrayOfSequences< V, M >::erasePositions().

207 {
208  if (this->subSequenceSize() == 0) return;
209 
210  bool bRC = ((initialPos < this->subSequenceSize()) &&
211  (0 < numPos ) &&
212  ((initialPos+numPos) <= this->subSequenceSize()));
213  queso_require_msg(bRC, "invalid input data");
214 
215  seqScalarPositionIteratorTypedef posIteratorBegin = m_seq.begin();
216  if (initialPos < this->subSequenceSize()) std::advance(posIteratorBegin,initialPos);
217  else posIteratorBegin = m_seq.end();
218 
219  unsigned int posEnd = initialPos + numPos - 1;
220  seqScalarPositionIteratorTypedef posIteratorEnd = m_seq.begin();
221  if (posEnd < this->subSequenceSize()) std::advance(posIteratorEnd,posEnd);
222  else posIteratorEnd = m_seq.end();
223 
224  unsigned int oldSequenceSize = this->subSequenceSize();
225  m_seq.erase(posIteratorBegin,posIteratorEnd);
226  queso_require_equal_to_msg((oldSequenceSize - numPos), this->subSequenceSize(), "(oldSequenceSize - numPos) != this->subSequenceSize()");
227 
229 
230  return;
231 }
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
std::vector< T >::iterator seqScalarPositionIteratorTypedef
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
void QUESO::ScalarSequence< T >::extractRawData ( unsigned int  initialPos,
unsigned int  spacing,
unsigned int  numPos,
std::vector< double > &  rawData 
) const
private

Extracts the raw data.

This method saves in rawData the data from the sequence of scalars (in private attribute m_seq) starting at position (initialPos), with a spacing of spacing until numPos positions have been extracted.

Definition at line 3158 of file ScalarSequence.C.

3163 {
3164  rawDataVec.resize(numPos);
3165  if (spacing == 1) {
3166  for (unsigned int j = 0; j < numPos; ++j) {
3167  rawDataVec[j] = m_seq[initialPos+j ];
3168  }
3169  }
3170  else {
3171  for (unsigned int j = 0; j < numPos; ++j) {
3172  rawDataVec[j] = m_seq[initialPos+j*spacing];
3173  }
3174  }
3175 
3176  return;
3177 }
std::vector< T > m_seq
template<class T>
void QUESO::ScalarSequence< T >::extractScalarSeq ( unsigned int  initialPos,
unsigned int  spacing,
unsigned int  numPos,
ScalarSequence< T > &  scalarSeq 
) const
private

Extracts a sequence of scalars.

The sequence of scalars has size numPos, and it will be extracted starting at position (initialPos) of this sequence of scalars, given spacing spacing.

Definition at line 3117 of file ScalarSequence.C.

References QUESO::ScalarSequence< T >::resizeSequence().

3122 {
3123  scalarSeq.resizeSequence(numPos);
3124  if (spacing == 1) {
3125  for (unsigned int j = 0; j < numPos; ++j) {
3126  //if ((initialPos+j*spacing) > m_seq.size()) {
3127  // std::cerr << "In ScalarSequence<T>::extraScalarSeq()"
3128  // << ": initialPos = " << initialPos
3129  // << ", spacing = " << spacing
3130  // << ", numPos = " << numPos
3131  // << ", j = " << j
3132  // << ", position got too large"
3133  // << std::endl;
3134  //}
3135  scalarSeq[j] = m_seq[initialPos+j ];
3136  }
3137  }
3138  else {
3139  for (unsigned int j = 0; j < numPos; ++j) {
3140  //if ((initialPos+j*spacing) > m_seq.size()) {
3141  // std::cerr << "In ScalarSequence<T>::extraScalarSeq()"
3142  // << ": initialPos = " << initialPos
3143  // << ", spacing = " << spacing
3144  // << ", numPos = " << numPos
3145  // << ", j = " << j
3146  // << ", position got too large"
3147  // << std::endl;
3148  //}
3149  scalarSeq[j] = m_seq[initialPos+j*spacing];
3150  }
3151  }
3152 
3153  return;
3154 }
std::vector< T > m_seq
template<class T >
void QUESO::ScalarSequence< T >::filter ( unsigned int  initialPos,
unsigned int  spacing 
)

Filters positions in the sequence of vectors.

Filtered positions will start at initialPos, and with spacing given by spacing.

Definition at line 2429 of file ScalarSequence.C.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence(), QUESO::MetropolisHastingsSG< P_V, P_M >::generateSequence(), and QUESO::MLSampling< P_V, P_M >::generateSequence_Step11_inter0().

2432 {
2433  if (m_env.subDisplayFile()) {
2434  *m_env.subDisplayFile() << "Entering ScalarSequence<V,M>::filter()"
2435  << ": initialPos = " << initialPos
2436  << ", spacing = " << spacing
2437  << ", subSequenceSize = " << this->subSequenceSize()
2438  << std::endl;
2439  }
2440 
2441  unsigned int i = 0;
2442  unsigned int j = initialPos;
2443  unsigned int originalSubSequenceSize = this->subSequenceSize();
2444  while (j < originalSubSequenceSize) {
2445  if (i != j) {
2446  //*m_env.subDisplayFile() << i << "--" << j << " ";
2447  m_seq[i] = m_seq[j];
2448  }
2449  i++;
2450  j += spacing;
2451  }
2452 
2453  this->resizeSequence(i);
2454 
2455  if (m_env.subDisplayFile()) {
2456  *m_env.subDisplayFile() << "Leaving ScalarSequence<V,M>::filter()"
2457  << ": initialPos = " << initialPos
2458  << ", spacing = " << spacing
2459  << ", subSequenceSize = " << this->subSequenceSize()
2460  << std::endl;
2461  }
2462 
2463  return;
2464 }
const BaseEnvironment & m_env
void resizeSequence(unsigned int newSequenceSize)
Resizes the size of the sequence of scalars.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::getUnifiedContentsAtProc0Only ( bool  useOnlyInter0Comm,
std::vector< T > &  outputVec 
) const

Gets the unified contents of processor of rank equals to 0.

Definition at line 235 of file ScalarSequence.C.

References queso_error_msg, queso_require_equal_to_msg, RawValue_MPI_DOUBLE, RawValue_MPI_IN_PLACE, and RawValue_MPI_INT.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence_Step05_inter0(), and QUESO::MLSampling< P_V, P_M >::generateSequence_Step09_all().

238 {
239  // The logic (numSubEnvs == 1) does *not* apply here because 'outputVec' needs to be filled
240  //if (m_env.numSubEnvironments() == 1) {
241  // // No need to do anything
242  // return;
243  //}
244 
245  if (useOnlyInter0Comm) {
246  if (m_env.inter0Rank() >= 0) {
247  int auxSubSize = (int) this->subSequenceSize();
248  unsigned int auxUnifiedSize = this->unifiedSequenceSize(useOnlyInter0Comm);
249  outputVec.resize(auxUnifiedSize,0.);
250 
251  //******************************************************************
252  // Use MPI_Gatherv for the case different nodes have different amount of data // KAUST4
253  //******************************************************************
254  std::vector<int> recvcnts(m_env.inter0Comm().NumProc(),0); // '0' is NOT the correct value for recvcnts[0]
255  m_env.inter0Comm().Gather((void *) &auxSubSize, 1, RawValue_MPI_INT, (void *) &recvcnts[0], (int) 1, RawValue_MPI_INT, 0,
256  "ScalarSequence<T>::getUnifiedContentsAtProc0Only()",
257  "failed MPI.Gather()");
258  if (m_env.inter0Rank() == 0) {
259  //recvcnts[0] = (int) this->subSequenceSize(); // FIX ME: really necessary????
260  queso_require_equal_to_msg(recvcnts[0], (int) this->subSequenceSize(), "failed MPI.Gather() result at proc 0");
261  }
262 
263  std::vector<int> displs(m_env.inter0Comm().NumProc(),0);
264  for (unsigned int r = 1; r < (unsigned int) m_env.inter0Comm().NumProc(); ++r) { // Yes, from '1' on
265  displs[r] = displs[r-1] + recvcnts[r-1];
266  }
267 
268 #if 0 // for debug only
269  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
270  for (unsigned int r = 0; r < (unsigned int) m_env.inter0Comm().NumProc(); ++r) {
271  *m_env.subDisplayFile() << " auxSubSize = " << auxSubSize
272  << ", recvcnts[" << r << "] = " << recvcnts[r]
273  << ", displs[" << r << "] = " << displs[r]
274  << ", m_seq.size() = " << m_seq.size()
275  << ", outputVec.size() = " << outputVec.size()
276  << std::endl;
277  }
278  for (unsigned int i = 0; i < m_seq.size(); ++i) {
279  *m_env.subDisplayFile() << " (before gatherv) m_seq[" << i << "]= " << m_seq[i]
280  << std::endl;
281  }
282  }
283 #endif
284  m_env.inter0Comm().Gatherv((void *) &m_seq[0], auxSubSize, RawValue_MPI_DOUBLE, (void *) &outputVec[0], (int *) &recvcnts[0], (int *) &displs[0], RawValue_MPI_DOUBLE, 0,
285  "ScalarSequence<T>::getUnifiedContentsAtProc0Only()",
286  "failed MPI.Gatherv()");
287 
288 #if 0 // for debug only
289  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
290  for (unsigned int i = 0; i < m_seq.size(); ++i) {
291  *m_env.subDisplayFile() << " (after gatherv) m_seq[" << i << "]= " << m_seq[i]
292  << std::endl;
293  }
294  for (unsigned int i = 0; i < outputVec.size(); ++i) {
295  *m_env.subDisplayFile() << " (after gatherv) outputVec[" << i << "]= " << outputVec[i]
296  << std::endl;
297  }
298  }
299 #endif
300 
301 #if 0 // for debug only
302  if (m_env.inter0Rank() == 0) {
303  for (unsigned int i = 0; i < auxSubSize; ++i) {
304  outputVec[i] = m_seq[i];
305  }
306  m_env.inter0Comm().Gatherv(RawValue_MPI_IN_PLACE, auxSubSize, RawValue_MPI_DOUBLE, (void *) &outputVec[0], (int *) &recvcnts[0], (int *) &displs[0], RawValue_MPI_DOUBLE, 0,
307  "ScalarSequence<T>::getUnifiedContentsAtProc0Only(1)",
308  "failed MPI.Gatherv()");
309  }
310  else {
311  m_env.inter0Comm().Gatherv((void *) &m_seq[0], auxSubSize, RawValue_MPI_DOUBLE, (void *) &outputVec[0], (int *) &recvcnts[0], (int *) &displs[0], RawValue_MPI_DOUBLE, 0,
312  "ScalarSequence<T>::getUnifiedContentsAtProc0Only(2)",
313  "failed MPI.Gatherv()");
314  }
315  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
316  for (unsigned int i = 0; i < m_seq.size(); ++i) {
317  *m_env.subDisplayFile() << " (after 2nd gatherv) m_seq[" << i << "]= " << m_seq[i]
318  << std::endl;
319  }
320  for (unsigned int i = 0; i < outputVec.size(); ++i) {
321  *m_env.subDisplayFile() << " (after 2nd gatherv) outputVec[" << i << "]= " << outputVec[i]
322  << std::endl;
323  }
324  }
325 #endif
326  }
327  else {
328  // Node not in the 'inter0' communicator
329  // No need to do anything
330  }
331  }
332  else {
333  queso_error_msg("parallel vectors not supported yet");
334  }
335 
336  return;
337 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_IN_PLACE
Definition: MpiComm.h:44
const BaseEnvironment & m_env
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int unifiedSequenceSize(bool useOnlyInter0Comm) const
Size of the unified sequence of scalars.
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
void Gatherv(void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype, void *recvbuf, int *recvcnts, int *displs, RawType_MPI_Datatype recvtype, int root, const char *whereMsg, const char *whatMsg) const
Gathers into specified locations from all processes in a group.
Definition: MpiComm.C:158
#define RawValue_MPI_INT
Definition: MpiComm.h:47
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void Gather(void *sendbuf, int sendcnt, RawType_MPI_Datatype sendtype, void *recvbuf, int recvcount, RawType_MPI_Datatype recvtype, int root, const char *whereMsg, const char *whatMsg) const
Gather values from each process to collect on all processes.
Definition: MpiComm.C:141
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
template<class T >
const std::string & QUESO::ScalarSequence< T >::name ( ) const

Access to the name of the sequence of scalars.

Definition at line 108 of file ScalarSequence.C.

109 {
110  return m_name;
111 }
template<class T>
ScalarSequence< T > & QUESO::ScalarSequence< T >::operator= ( const ScalarSequence< T > &  rhs)

Assignment operator; it copies rhs to this.

Definition at line 61 of file ScalarSequence.C.

References copy.

62 {
63  this->copy(rhs);
64  return *this;
65 }
void copy(const ScalarSequence< T > &src)
Copies the scalar sequence src to this.
template<class T >
const T & QUESO::ScalarSequence< T >::operator[] ( unsigned int  posId) const

Access position posId of the sequence of scalars (const).

Definition at line 69 of file ScalarSequence.C.

References queso_require_less_msg.

70 {
71  if (posId >= this->subSequenceSize()) {
72  std::cerr << "In ScalarSequence<T>::operator[]() const"
73  << ": posId = " << posId
74  << ", this->subSequenceSize() = " << this->subSequenceSize()
75  << std::endl;
76  }
77  queso_require_less_msg(posId, this->subSequenceSize(), "posId > subSequenceSize()");
78 
79  return m_seq[posId];
80 }
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T & QUESO::ScalarSequence< T >::operator[] ( unsigned int  posId)

Access position posId of the sequence of scalars (non-const).

This routine deletes all stored computed scalars.

Definition at line 84 of file ScalarSequence.C.

References queso_require_less_msg.

85 {
86  if (posId >= this->subSequenceSize()) {
87  std::cerr << "In ScalarSequence<T>::operator[]()"
88  << ": posId = " << posId
89  << ", this->subSequenceSize() = " << this->subSequenceSize()
90  << std::endl;
91  }
92  queso_require_less_msg(posId, this->subSequenceSize(), "posId > subSequenceSize()");
93 
95 
96  return m_seq[posId];
97 }
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::parallelMerge ( std::vector< T > &  sortedBuffer,
const std::vector< T > &  leafData,
unsigned int  treeLevel 
) const
private

Sorts/merges data in parallel using MPI.

Definition at line 3199 of file ScalarSequence.C.

References k, RawValue_MPI_DOUBLE, RawValue_MPI_UNSIGNED, SCALAR_SEQUENCE_DATA_MPI_MSG, SCALAR_SEQUENCE_INIT_MPI_MSG, and SCALAR_SEQUENCE_SIZE_MPI_MSG.

3203 {
3204  int parentNode = m_env.inter0Rank() & ~(1 << currentTreeLevel);
3205 
3206  if (m_env.inter0Rank() >= 0) { // KAUST
3207  if (currentTreeLevel == 0) {
3208  // Leaf node: sort own local data.
3209  unsigned int leafDataSize = leafData.size();
3210  sortedBuffer.resize(leafDataSize,0.);
3211  for (unsigned int i = 0; i < leafDataSize; ++i) {
3212  sortedBuffer[i] = leafData[i];
3213  }
3214  std::sort(sortedBuffer.begin(), sortedBuffer.end());
3215  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
3216  *m_env.subDisplayFile() << "In ScalarSequence<T>::parallelMerge()"
3217  << ": tree node " << m_env.inter0Rank()
3218  << ", leaf sortedBuffer[0] = " << sortedBuffer[0]
3219  << ", leaf sortedBuffer[" << sortedBuffer.size()-1 << "] = " << sortedBuffer[sortedBuffer.size()-1]
3220  << std::endl;
3221  }
3222  }
3223  else {
3224  int nextTreeLevel = currentTreeLevel - 1;
3225  int rightChildNode = m_env.inter0Rank() | (1 << nextTreeLevel);
3226 
3227  if (rightChildNode >= m_env.inter0Comm().NumProc()) { // No right child. Move down one level.
3228  this->parallelMerge(sortedBuffer,
3229  leafData,
3230  nextTreeLevel);
3231  }
3232  else {
3233  unsigned int uintBuffer[1];
3234  uintBuffer[0] = nextTreeLevel;
3235  m_env.inter0Comm().Send((void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, rightChildNode, SCALAR_SEQUENCE_INIT_MPI_MSG,
3236  "ScalarSequence<T>::parallelMerge()",
3237  "failed MPI.Send() for init");
3238 
3239  this->parallelMerge(sortedBuffer,
3240  leafData,
3241  nextTreeLevel);
3242 
3243  // Prepare variable 'leftSortedBuffer': just copy own current sorted data.
3244  unsigned int leftSize = sortedBuffer.size();
3245  std::vector<T> leftSortedBuffer(leftSize,0.);
3246  for (unsigned int i = 0; i < leftSize; ++i) {
3247  leftSortedBuffer[i] = sortedBuffer[i];
3248  }
3249 
3250  // Prepare variable 'rightSortedBuffer': receive data from right child node.
3251  RawType_MPI_Status status;
3252  m_env.inter0Comm().Recv((void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, rightChildNode, SCALAR_SEQUENCE_SIZE_MPI_MSG, &status,
3253  "ScalarSequence<T>::parallelMerge()",
3254  "failed MPI.Recv() for size");
3255  //if (status) {}; // just to remove compiler warning
3256 
3257  unsigned int rightSize = uintBuffer[0];
3258  std::vector<T> rightSortedBuffer(rightSize,0.);
3259  m_env.inter0Comm().Recv((void *) &rightSortedBuffer[0], (int) rightSize, RawValue_MPI_DOUBLE, rightChildNode, SCALAR_SEQUENCE_DATA_MPI_MSG, &status,
3260  "ScalarSequence<T>::parallelMerge()",
3261  "failed MPI.Recv() for data");
3262 
3263  // Merge the two results into 'sortedBuffer'.
3264  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
3265  *m_env.subDisplayFile() << "In ScalarSequence<T>::parallelMerge()"
3266  << ": tree node " << m_env.inter0Rank()
3267  << " is combining " << leftSortedBuffer.size()
3268  << " left doubles with " << rightSortedBuffer.size()
3269  << " right doubles"
3270  << std::endl;
3271  }
3272 
3273  sortedBuffer.clear();
3274  sortedBuffer.resize(leftSortedBuffer.size()+rightSortedBuffer.size(),0.);
3275  unsigned int i = 0;
3276  unsigned int j = 0;
3277  unsigned int k = 0;
3278  while ((i < leftSize ) &&
3279  (j < rightSize)) {
3280  if (leftSortedBuffer[i] > rightSortedBuffer[j]) sortedBuffer[k++] = rightSortedBuffer[j++];
3281  else sortedBuffer[k++] = leftSortedBuffer [i++];
3282  }
3283  while (i < leftSize ) sortedBuffer[k++] = leftSortedBuffer [i++];
3284  while (j < rightSize) sortedBuffer[k++] = rightSortedBuffer[j++];
3285  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
3286  *m_env.subDisplayFile() << "In ScalarSequence<T>::parallelMerge()"
3287  << ": tree node " << m_env.inter0Rank()
3288  << ", merged sortedBuffer[0] = " << sortedBuffer[0]
3289  << ", merged sortedBuffer[" << sortedBuffer.size()-1 << "] = " << sortedBuffer[sortedBuffer.size()-1]
3290  << std::endl;
3291  }
3292  }
3293  }
3294 
3295  if (parentNode != m_env.inter0Rank()) {
3296  // Transmit data to parent node.
3297  unsigned int uintBuffer[1];
3298  uintBuffer[0] = sortedBuffer.size();
3299  m_env.inter0Comm().Send((void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, parentNode, SCALAR_SEQUENCE_SIZE_MPI_MSG,
3300  "ScalarSequence<T>::parallelMerge()",
3301  "failed MPI.Send() for size");
3302 
3303  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
3304  *m_env.subDisplayFile() << "In ScalarSequence<T>::parallelMerge()"
3305  << ": tree node " << m_env.inter0Rank()
3306  << " is sending " << sortedBuffer.size()
3307  << " doubles to tree node " << parentNode
3308  << ", with sortedBuffer[0] = " << sortedBuffer[0]
3309  << " and sortedBuffer[" << sortedBuffer.size()-1 << "] = " << sortedBuffer[sortedBuffer.size()-1]
3310  << std::endl;
3311  }
3312 
3313  m_env.inter0Comm().Send((void *) &sortedBuffer[0], (int) sortedBuffer.size(), RawValue_MPI_DOUBLE, parentNode, SCALAR_SEQUENCE_DATA_MPI_MSG,
3314  "ScalarSequence<T>::parallelMerge()",
3315  "failed MPI.Send() for data");
3316  }
3317  } // KAUST
3318 
3319  return;
3320 }
#define SCALAR_SEQUENCE_INIT_MPI_MSG
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
void parallelMerge(std::vector< T > &sortedBuffer, const std::vector< T > &leafData, unsigned int treeLevel) const
Sorts/merges data in parallel using MPI.
MPI_Status RawType_MPI_Status
Definition: MpiComm.h:42
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
void Send(void *buf, int count, RawType_MPI_Datatype datatype, int dest, int tag, const char *whereMsg, const char *whatMsg) const
Possibly blocking send of data from this process to another process.
Definition: MpiComm.C:185
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define SCALAR_SEQUENCE_DATA_MPI_MSG
#define SCALAR_SEQUENCE_SIZE_MPI_MSG
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
void Recv(void *buf, int count, RawType_MPI_Datatype datatype, int source, int tag, RawType_MPI_Status *status, const char *whereMsg, const char *whatMsg) const
Blocking receive of data from this process to another process.
Definition: MpiComm.C:175
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
int k
Definition: ann_sample.cpp:53
template<class T >
std::vector< T > & QUESO::ScalarSequence< T >::rawData ( )
private

The sequence of scalars. Access to private attribute m_seq.

Definition at line 3181 of file ScalarSequence.C.

Referenced by QUESO::ScalarSequence< T >::unifiedSort().

3182 {
3183  return m_seq;
3184 }
std::vector< T > m_seq
template<class T >
void QUESO::ScalarSequence< T >::resetValues ( unsigned int  initialPos,
unsigned int  numPos 
)

Sets numPos values of the sequence to zero, starting at position initialPos.

This routine deletes all stored computed scalars.

Definition at line 186 of file ScalarSequence.C.

References queso_require_msg.

187 {
188  if (this->subSequenceSize() == 0) return;
189 
190  bool bRC = ((initialPos < this->subSequenceSize()) &&
191  (0 < numPos ) &&
192  ((initialPos+numPos) <= this->subSequenceSize()));
193  queso_require_msg(bRC, "invalid input data");
194 
195  for (unsigned int j = 0; j < numPos; ++j) {
196  m_seq[initialPos+j] = 0.;
197  }
198 
200 
201  return;
202 }
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
void QUESO::ScalarSequence< T >::resizeSequence ( unsigned int  newSequenceSize)

Resizes the size of the sequence of scalars.

This routine deletes all stored computed scalars.

Definition at line 173 of file ScalarSequence.C.

Referenced by QUESO::ArrayOfSequences< V, M >::extractScalarSeq(), QUESO::SequenceOfVectors< V, M >::extractScalarSeq(), QUESO::ScalarSequence< T >::extractScalarSeq(), QUESO::MetropolisHastingsSG< P_V, P_M >::generateFullChain(), QUESO::MLSampling< P_V, P_M >::generateSequence_Level0_all(), QUESO::ScalarSequence< T >::subMedianExtra(), QUESO::ScalarSequence< T >::subPositionsOfMaximum(), QUESO::ScalarSequence< T >::subSort(), QUESO::ScalarSequence< T >::unifiedPositionsOfMaximum(), and QUESO::ScalarSequence< T >::unifiedSort().

174 {
175  if (newSequenceSize != this->subSequenceSize()) {
176  m_seq.resize(newSequenceSize,0.);
177  std::vector<T>(m_seq).swap(m_seq);
179  }
180 
181  return;
182 }
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::setGaussian ( const T &  mean,
const T &  stdDev 
)

Sets the values of the sequence as a Gaussian distribution of mean given by meanVec and standard deviation by stdDevVec.

This routine deletes all stored computed scalars.

Definition at line 513 of file ScalarSequence.C.

Referenced by QUESO::ArrayOfSequences< V, M >::setGaussian().

514 {
515  unsigned int maxJ = this->subSequenceSize();
516  if (meanValue == 0.) {
517  for (unsigned int j = 0; j < maxJ; ++j) {
518  m_seq[j] = m_env.rngObject()->gaussianSample(stdDev);
519  }
520  }
521  else {
522  for (unsigned int j = 0; j < maxJ; ++j) {
523  m_seq[j] = meanValue + m_env.rngObject()->gaussianSample(stdDev);
524  }
525  }
526 
528 
529  return;
530 }
const BaseEnvironment & m_env
virtual double gaussianSample(double stdDev) const =0
Samples a value from a Gaussian distribution with standard deviation given by stdDev.
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:417
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
void QUESO::ScalarSequence< T >::setName ( const std::string &  newName)
template<class T>
void QUESO::ScalarSequence< T >::setUniform ( const T &  a,
const T &  b 
)

Sets the values of the sequence as a uniform distribution between the values given by vectors aVec and bVec.

This routine deletes all stored computed scalars.

Definition at line 534 of file ScalarSequence.C.

Referenced by QUESO::ArrayOfSequences< V, M >::setUniform().

535 {
536  unsigned int maxJ = this->subSequenceSize();
537  if (a == 0.) {
538  if (b == 1.) {
539  for (unsigned int j = 0; j < maxJ; ++j) {
541  }
542  }
543  else {
544  for (unsigned int j = 0; j < maxJ; ++j) {
545  m_seq[j] = b*m_env.rngObject()->uniformSample();
546  }
547  }
548  }
549  else {
550  if ((b-a) == 1.) {
551  for (unsigned int j = 0; j < maxJ; ++j) {
552  m_seq[j] = a + m_env.rngObject()->uniformSample();
553  }
554  }
555  else {
556  for (unsigned int j = 0; j < maxJ; ++j) {
557  m_seq[j] = a + (b-a)*m_env.rngObject()->uniformSample();
558  }
559  }
560  }
561 
563 
564  return;
565 }
const BaseEnvironment & m_env
std::vector< T > m_seq
void deleteStoredScalars()
Deletes all stored scalars.
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:417
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
template<class T>
void QUESO::ScalarSequence< T >::subBasicCdf ( unsigned int  numIntervals,
UniformOneDGrid< T > *&  gridValues,
std::vector< T > &  cdfValues 
) const

Finds the Cumulative Distribution Function (CDF) of the sub-sequence of scalars.

Definition at line 710 of file ScalarSequence.C.

714 {
715  T tmpMinValue;
716  T tmpMaxValue;
717  std::vector<unsigned int> bins(numEvaluationPoints,0);
718 
719  subMinMaxExtra(0, // initialPos
720  this->subSequenceSize(),
721  tmpMinValue,
722  tmpMaxValue);
723  subBasicHistogram(0, // initialPos,
724  tmpMinValue,
725  tmpMaxValue,
726  gridValues,
727  bins);
728 
729  unsigned int sumOfBins = 0;
730  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
731  sumOfBins += bins[i];
732  }
733 
734  cdfValues.clear();
735  cdfValues.resize(numEvaluationPoints);
736  unsigned int partialSum = 0;
737  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
738  partialSum += bins[i];
739  cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
740  }
741 
742  return;
743 }
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
void subBasicHistogram(unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, UniformOneDGrid< T > *&gridValues, std::vector< unsigned int > &bins) const
Calculates the histogram of the sub-sequence.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::subBasicHistogram ( unsigned int  initialPos,
const T &  minHorizontalValue,
const T &  maxHorizontalValue,
UniformOneDGrid< T > *&  gridValues,
std::vector< unsigned int > &  bins 
) const

Calculates the histogram of the sub-sequence.

It requires the specification of the maximum (maxHorizontalValue) and the minimum (minHorizontalValue) values if the data and the initial position (initialPos) from where the data will be considered. Output: grid values that will act as the center of each bin (gridValues) and the number of data occurrences in each bin (bins).

Definition at line 1747 of file ScalarSequence.C.

References queso_require_greater_equal_msg.

1753 {
1754  queso_require_greater_equal_msg(bins.size(), 3, "number of 'bins' is too small: should be at least 3");
1755 
1756  for (unsigned int j = 0; j < bins.size(); ++j) {
1757  bins[j] = 0;
1758  }
1759 
1760  double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((double) bins.size()) - 2.); // IMPORTANT: -2
1761  double minCenter = minHorizontalValue - horizontalDelta/2.;
1762  double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1763  gridValues = new UniformOneDGrid<T>(m_env,
1764  "",
1765  bins.size(),
1766  minCenter,
1767  maxCenter);
1768 
1769  unsigned int dataSize = this->subSequenceSize();
1770  for (unsigned int j = 0; j < dataSize; ++j) {
1771  double value = m_seq[j];
1772  if (value < minHorizontalValue) {
1773  bins[0] += value;
1774  }
1775  else if (value >= maxHorizontalValue) {
1776  bins[bins.size()-1] += value;
1777  }
1778  else {
1779  unsigned int index = 1 + (unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1780  bins[index] += value;
1781  }
1782  }
1783 
1784  return;
1785 }
const BaseEnvironment & m_env
std::vector< T > m_seq
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::subGaussian1dKde ( unsigned int  initialPos,
double  scaleValue,
const std::vector< T > &  evaluationPositions,
std::vector< double > &  densityValues 
) const

Gaussian kernel for the KDE estimate of the sub-sequence.

Computes a probability density estimate of the sample in this sub-sequence, starting at position initialPos. densityValues is the vector of density values evaluated at the points in evaluationPositions. The estimate is based on Gaussian (normal) kernel function, using a window parameter (scaleValue).

Definition at line 2319 of file ScalarSequence.C.

References k, QUESO::MiscGaussianDensity(), and queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::subGaussian1dKde().

2324 {
2325  bool bRC = ((initialPos < this->subSequenceSize() ) &&
2326  (0 < evaluationPositions.size()) &&
2327  (evaluationPositions.size() == densityValues.size() ));
2328  queso_require_msg(bRC, "invalid input data");
2329 
2330  unsigned int dataSize = this->subSequenceSize() - initialPos;
2331  unsigned int numEvals = evaluationPositions.size();
2332 
2333  double scaleInv = 1./scaleValue;
2334  for (unsigned int j = 0; j < numEvals; ++j) {
2335  double x = evaluationPositions[j];
2336  double value = 0.;
2337  for (unsigned int k = 0; k < dataSize; ++k) {
2338  double xk = m_seq[initialPos+k];
2339  value += MiscGaussianDensity((x-xk)*scaleInv,0.,1.);
2340  }
2341  densityValues[j] = scaleInv * (value/(double) dataSize);
2342  }
2343 
2344  return;
2345 }
double MiscGaussianDensity(double x, double mu, double sigma)
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
int k
Definition: ann_sample.cpp:53
template<class T>
void QUESO::ScalarSequence< T >::subHistogram ( unsigned int  initialPos,
const T &  minHorizontalValue,
const T &  maxHorizontalValue,
std::vector< T > &  centers,
std::vector< unsigned int > &  bins 
) const

Calculates the histogram of the sub-sequence.

It requires the specification of the maximum (maxHorizontalValue) and the minimum (minHorizontalValue) values if the data and the initial position (initialPos) from where the data will be considered. Output: the center of each bin (centers) and the number of data occurrences in each bin (bins).

Definition at line 1610 of file ScalarSequence.C.

References queso_require_equal_to_msg, and queso_require_greater_equal_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::subHistogram().

1616 {
1617  queso_require_equal_to_msg(centers.size(), bins.size(), "vectors 'centers' and 'bins' have different sizes");
1618 
1619  queso_require_greater_equal_msg(bins.size(), 3, "number of 'bins' is too small: should be at least 3");
1620 
1621  if (initialPos) {}; // just to remove compiler warning
1622 
1623  for (unsigned int j = 0; j < bins.size(); ++j) {
1624  centers[j] = 0.;
1625  bins[j] = 0;
1626  }
1627 
1628  double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((double) bins.size()) - 2.); // IMPORTANT: -2
1629 
1630  double minCenter = minHorizontalValue - horizontalDelta/2.;
1631  double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1632  for (unsigned int j = 0; j < centers.size(); ++j) {
1633  double factor = ((double) j)/(((double) centers.size()) - 1.);
1634  centers[j] = (1. - factor) * minCenter + factor * maxCenter;
1635  }
1636 
1637  unsigned int dataSize = this->subSequenceSize();
1638  for (unsigned int j = 0; j < dataSize; ++j) {
1639  double value = m_seq[j];
1640  if (value < minHorizontalValue) {
1641  bins[0]++;
1642  }
1643  else if (value >= maxHorizontalValue) {
1644  bins[bins.size()-1]++;
1645  }
1646  else {
1647  unsigned int index = 1 + (unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1648  bins[index]++;
1649  }
1650  }
1651 
1652  return;
1653 }
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::vector< T > m_seq
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::subInterQuantileRange ( unsigned int  initialPos) const

Returns the interquartile range of the values in the sub-sequence.

The IQR is a robust estimate of the spread of the data, since changes in the upper and lower 25% of the data do not affect it. If there are outliers in the data, then the IQR is more representative than the standard deviation as an estimate of the spread of the body of the data. The IQR is less efficient than the standard deviation as an estimate of the spread when the data is all from the normal distribution. (from Matlab)

Definition at line 2004 of file ScalarSequence.C.

References queso_require_equal_to_msg, queso_require_less_msg, and QUESO::ScalarSequence< T >::subSequenceSize().

Referenced by QUESO::SequenceOfVectors< V, M >::subInterQuantileRange().

2005 {
2006  queso_require_less_msg(initialPos, this->subSequenceSize(), "'initialPos' is too big");
2007 
2008  ScalarSequence sortedSequence(m_env,0,"");
2009  this->subSort(initialPos,
2010  sortedSequence);
2011 
2012  // The test above guarantees that 'dataSize >= 1'
2013  unsigned int dataSize = this->subSequenceSize() - initialPos;
2014 
2015  queso_require_equal_to_msg(dataSize, sortedSequence.subSequenceSize(), "inconsistent size variables");
2016 
2017  bool everythingOk = true;
2018 
2019  // pos1 = (dataSize+1)/4 - 1
2020  // pos1 >= 0 <==> dataSize >= 3
2021  // pos1 < (dataSize-1) <==> 3*dataSize > 1
2022  unsigned int pos1 = (unsigned int) ( (((double) dataSize) + 1.)*1./4. - 1. );
2023  if (pos1 > (dataSize-1)) {
2024  pos1 = 0;
2025  everythingOk = false;
2026  }
2027  unsigned int pos1inc = pos1+1;
2028  if (pos1inc > (dataSize-1)) {
2029  pos1inc = dataSize-1;
2030  everythingOk = false;
2031  }
2032 
2033  // pos3 = (dataSize+1)*3/4 - 1
2034  // pos3 >= 0 <==> dataSize >= 1/3
2035  // pos3 < (dataSize-1) <==> dataSize > 3
2036  unsigned int pos3 = (unsigned int) ( (((double) dataSize) + 1.)*3./4. - 1. );
2037  if (pos3 > (dataSize-1)) {
2038  pos3 = 0;
2039  everythingOk = false;
2040  }
2041  unsigned int pos3inc = pos3+1;
2042  if (pos3inc > (dataSize-1)) {
2043  pos3inc = dataSize-1;
2044  everythingOk = false;
2045  }
2046 
2047  double fraction1 = (((double) dataSize) + 1.)*1./4. - 1. - ((double) pos1);
2048  if (fraction1 < 0.) {
2049  fraction1 = 0.;
2050  everythingOk = false;
2051  }
2052  double fraction3 = (((double) dataSize) + 1.)*3./4. - 1. - ((double) pos3);
2053  if (fraction3 < 0.) {
2054  fraction3 = 0.;
2055  everythingOk = false;
2056  }
2057 
2058  if (everythingOk == false) {
2059  std::cerr << "In ScalarSequence<T>::subInterQuantileRange()"
2060  << ", worldRank = " << m_env.worldRank()
2061  << ": at least one adjustment was necessary"
2062  << std::endl;
2063  }
2064 
2065  //if (m_env.subDisplayFile()) {
2066  // *m_env.subDisplayFile() << "In ScalarSequence::subInterQuantileRange()"
2067  // << ", initialPos = " << initialPos
2068  // << ", this->subSequenceSize() = " << this->subSequenceSize()
2069  // << ", dataSize = " << dataSize
2070  // << ", sortedSequence.size() = " << sortedSequence.size()
2071  // << ", pos1 = " << pos1
2072  // << ", pos3 = " << pos3
2073  // << std::endl;
2074  //}
2075 
2076  T value1 = (1.-fraction1) * sortedSequence[pos1] + fraction1 * sortedSequence[pos1inc];
2077  T value3 = (1.-fraction3) * sortedSequence[pos3] + fraction3 * sortedSequence[pos3inc];
2078  T iqrValue = value3 - value1;
2079 
2080  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2081  *m_env.subDisplayFile() << "In ScalarSequence<T>::subInterQuantileRange()"
2082  << ": iqrValue = " << iqrValue
2083  << ", dataSize = " << dataSize
2084  << ", pos1 = " << pos1
2085  << ", pos3 = " << pos3
2086  << ", value1 = " << value1
2087  << ", value3 = " << value3
2088  << std::endl;
2089 
2090  // Save data only once into a separate file
2091  //std::ofstream* ofsvar = new std::ofstream(("sort_sub"+m_env.subIdString()+".m").c_str(), std::ofstream::out | std::ofstream::in | std::ofstream::ate);
2092  //if ((ofsvar == NULL ) ||
2093  // (ofsvar->is_open() == false)) {
2094  // delete ofsvar;
2095  // ofsvar = new std::ofstream(("sort_sub"+m_env.subIdString()+".m").c_str(), std::ofstream::out | std::ofstream::trunc);
2096 
2097  // *ofsvar << "var_sort_sub" << m_env.subIdString() << " = zeros(" << 1
2098  // << "," << dataSize
2099  // << ");"
2100  // << std::endl;
2101  // for (unsigned int j = 0; j < dataSize; ++j) {
2102  // *ofsvar << "var_sort_sub" << m_env.subIdString() << "(" << 1
2103  // << "," << j+1
2104  // << ") = " << sortedSequence[j]
2105  // << ";"
2106  // << std::endl;
2107  // }
2108  //}
2109  //delete ofsvar;
2110  }
2111 
2112  return iqrValue;
2113 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
ScalarSequence(const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
Default constructor.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
void subSort()
Sorts the sequence of scalars in the private attribute m_seq.
int worldRank() const
Returns the process world rank.
Definition: Environment.C:216
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
const T & QUESO::ScalarSequence< T >::subMaxPlain ( ) const

Finds the maximum value of the sub-sequence of scalars.

Definition at line 367 of file ScalarSequence.C.

Referenced by QUESO::BaseVectorSequence< V, M >::subPositionsOfMaximum(), QUESO::ScalarSequence< T >::subPositionsOfMaximum(), QUESO::BaseVectorSequence< V, M >::unifiedPositionsOfMaximum(), and QUESO::ScalarSequence< T >::unifiedPositionsOfMaximum().

368 {
369  if (m_subMaxPlain == NULL) {
370  if (m_subMinPlain == NULL) m_subMinPlain = new T(0.);
371  m_subMaxPlain = new T(0.);
373  }
374 
375  return *m_subMaxPlain;
376 }
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::subMeanExtra ( unsigned int  initialPos,
unsigned int  numPos 
) const

Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPos.

Definition at line 836 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::ArrayOfSequences< V, M >::mean(), and QUESO::SequenceOfVectors< V, M >::subMeanExtra().

839 {
840  if (this->subSequenceSize() == 0) return 0.;
841 
842  bool bRC = ((initialPos < this->subSequenceSize()) &&
843  (0 < numPos ) &&
844  ((initialPos+numPos) <= this->subSequenceSize()));
845  if (bRC == false) {
846  std::cerr << "In ScalarSequence<T>::subMeanExtra()"
847  << ": ERROR at fullRank " << m_env.fullRank()
848  << ", initialPos = " << initialPos
849  << ", numPos = " << numPos
850  << ", this->subSequenceSize() = " << this->subSequenceSize()
851  << std::endl;
852  if (m_env.subDisplayFile()) {
853  *m_env.subDisplayFile() << "In ScalarSequence<T>::subMeanExtra()"
854  << ": ERROR at fullRank " << m_env.fullRank()
855  << ", initialPos = " << initialPos
856  << ", numPos = " << numPos
857  << ", this->subSequenceSize() = " << this->subSequenceSize()
858  << std::endl;
859  }
860  }
861  queso_require_msg(bRC, "invalid input data");
862 
863  unsigned int finalPosPlus1 = initialPos + numPos;
864  T tmpSum = 0.;
865  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
866  tmpSum += m_seq[j];
867  }
868 
869  return tmpSum/(T) numPos;
870 }
const BaseEnvironment & m_env
int fullRank() const
Returns the process full rank.
Definition: Environment.C:222
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
const T & QUESO::ScalarSequence< T >::subMeanPlain ( ) const

Finds the mean value of the sub-sequence of scalars.

Definition at line 393 of file ScalarSequence.C.

394 {
395  if (m_subMeanPlain == NULL) {
396  m_subMeanPlain = new T(0.);
398  }
399 
400  return *m_subMeanPlain;
401 }
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::subMedianExtra ( unsigned int  initialPos,
unsigned int  numPos 
) const

Finds the median value of the sub-sequence, considering numPos positions starting at position initialPos.

Definition at line 952 of file ScalarSequence.C.

References queso_require_msg, QUESO::ScalarSequence< T >::resizeSequence(), and QUESO::ScalarSequence< T >::subSort().

Referenced by QUESO::SequenceOfVectors< V, M >::subMedianExtra().

955 {
956  if (this->subSequenceSize() == 0) return 0.;
957 
958  bool bRC = ((initialPos < this->subSequenceSize()) &&
959  (0 < numPos ) &&
960  ((initialPos+numPos) <= this->subSequenceSize()));
961  if (bRC == false) {
962  std::cerr << "In ScalarSequence<T>::subMedianExtra()"
963  << ": ERROR at fullRank " << m_env.fullRank()
964  << ", initialPos = " << initialPos
965  << ", numPos = " << numPos
966  << ", this->subSequenceSize() = " << this->subSequenceSize()
967  << std::endl;
968  if (m_env.subDisplayFile()) {
969  *m_env.subDisplayFile() << "In ScalarSequence<T>::subMedianExtra()"
970  << ": ERROR at fullRank " << m_env.fullRank()
971  << ", initialPos = " << initialPos
972  << ", numPos = " << numPos
973  << ", this->subSequenceSize() = " << this->subSequenceSize()
974  << std::endl;
975  }
976  }
977  queso_require_msg(bRC, "invalid input data");
978 
979  ScalarSequence sortedSequence(m_env,0,"");
980  sortedSequence.resizeSequence(numPos);
981  this->extractScalarSeq(initialPos,
982  1,
983  numPos,
984  sortedSequence);
985  sortedSequence.subSort();
986 
987  unsigned int tmpPos = (unsigned int) (0.5 * (double) numPos);
988  T resultValue = sortedSequence[tmpPos];
989 
990  return resultValue;
991 }
const BaseEnvironment & m_env
int fullRank() const
Returns the process full rank.
Definition: Environment.C:222
ScalarSequence(const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
Default constructor.
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
void extractScalarSeq(unsigned int initialPos, unsigned int spacing, unsigned int numPos, ScalarSequence< T > &scalarSeq) const
Extracts a sequence of scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
const T & QUESO::ScalarSequence< T >::subMedianPlain ( ) const

Finds the median value of the sub-sequence of scalars.

Definition at line 417 of file ScalarSequence.C.

418 {
419  if (m_subMedianPlain == NULL) {
420  m_subMedianPlain = new T(0.);
422  }
423 
424  return *m_subMedianPlain;
425 }
T subMedianExtra(unsigned int initialPos, unsigned int numPos) const
Finds the median value of the sub-sequence, considering numPos positions starting at position initial...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::subMinMaxExtra ( unsigned int  initialPos,
unsigned int  numPos,
T &  minValue,
T &  maxValue 
) const

Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting at position initialPos.

Definition at line 1510 of file ScalarSequence.C.

References queso_require_less_equal_msg, and queso_require_msg.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence(), QUESO::ArrayOfSequences< V, M >::minMax(), and QUESO::SequenceOfVectors< V, M >::subMinMaxExtra().

1515 {
1516  queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(), "invalid input");
1517 
1519  std::advance(pos1,initialPos);
1520 
1522  std::advance(pos2,initialPos+numPos);
1523 
1524  if ((initialPos+numPos) == this->subSequenceSize()) {
1525  queso_require_msg(!(pos2 != m_seq.end()), "invalid state");
1526  }
1527 
1529  pos = std::min_element(pos1, pos2);
1530  minValue = *pos;
1531  pos = std::max_element(pos1, pos2);
1532  maxValue = *pos;
1533 
1534  return;
1535 }
std::vector< T >::const_iterator seqScalarPositionConstIteratorTypedef
#define queso_require_less_equal_msg(expr1, expr2, msg)
Definition: asserts.h:89
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
const T & QUESO::ScalarSequence< T >::subMinPlain ( ) const

Finds the minimum value of the sub-sequence of scalars.

Definition at line 341 of file ScalarSequence.C.

342 {
343  if (m_subMinPlain == NULL) {
344  m_subMinPlain = new T(0.);
345  if (m_subMaxPlain == NULL) m_subMaxPlain = new T(0.);
347  }
348 
349  return *m_subMinPlain;
350 }
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
T QUESO::ScalarSequence< T >::subPopulationVariance ( unsigned int  initialPos,
unsigned int  numPos,
const T &  meanValue 
) const

Finds the population variance of the sub-sequence, considering numPos positions starting at position initialPos and of mean meanValue.

Output: calculated population variance of the sub-sequence of scalars. The population variance $ \sigma^2 $ is defined by $ \sigma^2 = \frac{1}{n-1} \sum_{i=1}^n \left(y_i - \mu \right)^2 $, where $ \mu $ is the sample mean and $ n $ is the sample size. This procedure lets the users choose the initial position and the number of elements of the sequence which will be used to evaluate the population variance .

Definition at line 1216 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::subPopulationVariance().

1220 {
1221  if (this->subSequenceSize() == 0) return 0.;
1222 
1223  bool bRC = ((initialPos < this->subSequenceSize()) &&
1224  (0 < numPos ) &&
1225  ((initialPos+numPos) <= this->subSequenceSize()));
1226  queso_require_msg(bRC, "invalid input data");
1227 
1228  unsigned int finalPosPlus1 = initialPos + numPos;
1229  T diff;
1230  T popValue = 0.;
1231  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1232  diff = m_seq[j] - meanValue;
1233  popValue += diff*diff;
1234  }
1235 
1236  popValue /= (T) numPos;
1237 
1238  return popValue;
1239 }
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
T QUESO::ScalarSequence< T >::subPositionsOfMaximum ( const ScalarSequence< T > &  subCorrespondingScalarValues,
ScalarSequence< T > &  subPositionsOfMaximum 
)

Finds the positions where the maximum element occurs in the sub-sequence.

Definition at line 2518 of file ScalarSequence.C.

References queso_require_equal_to_msg, QUESO::ScalarSequence< T >::resizeSequence(), QUESO::ScalarSequence< T >::subMaxPlain(), and QUESO::ScalarSequence< T >::subSequenceSize().

2521 {
2522  queso_require_equal_to_msg(subCorrespondingScalarValues.subSequenceSize(), this->subSequenceSize(), "invalid input");
2523 
2524  T subMaxValue = subCorrespondingScalarValues.subMaxPlain();
2525  unsigned int iMax = subCorrespondingScalarValues.subSequenceSize();
2526 
2527  unsigned int subNumPos = 0;
2528  for (unsigned int i = 0; i < iMax; ++i) {
2529  if (subCorrespondingScalarValues[i] == subMaxValue) {
2530  subNumPos++;
2531  }
2532  }
2533 
2534  subPositionsOfMaximum.resizeSequence(subNumPos);
2535  unsigned int j = 0;
2536  for (unsigned int i = 0; i < iMax; ++i) {
2537  if (subCorrespondingScalarValues[i] == subMaxValue) {
2538  subPositionsOfMaximum[j] = (*this)[i];
2539  j++;
2540  }
2541  }
2542 
2543  return subMaxValue;
2544 }
T subPositionsOfMaximum(const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &subPositionsOfMaximum)
Finds the positions where the maximum element occurs in the sub-sequence.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
T QUESO::ScalarSequence< T >::subSampleStd ( unsigned int  initialPos,
unsigned int  numPos,
const T &  meanValue 
) const

Finds the sample standard deviation of the unified sequence, considering numPos positions starting at position initialPos and of mean meanValue.

Definition at line 1128 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::subSampleStd().

1132 {
1133  if (this->subSequenceSize() == 0) return 0.;
1134 
1135  bool bRC = ((initialPos < this->subSequenceSize()) &&
1136  (0 < numPos ) &&
1137  ((initialPos+numPos) <= this->subSequenceSize()));
1138  queso_require_msg(bRC, "invalid input data");
1139 
1140  unsigned int finalPosPlus1 = initialPos + numPos;
1141  T diff;
1142  T stdValue = 0.;
1143  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1144  diff = m_seq[j] - meanValue;
1145  stdValue += diff*diff;
1146  }
1147 
1148  stdValue /= (((T) numPos) - 1.);
1149  stdValue = sqrt(stdValue);
1150 
1151  return stdValue;
1152 }
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
T QUESO::ScalarSequence< T >::subSampleVarianceExtra ( unsigned int  initialPos,
unsigned int  numPos,
const T &  meanValue 
) const

Finds the sample variance of the sub-sequence, considering numPos positions starting at position initialPos and of mean meanVec.

Output: calculated sample variance of the sub-sequence of scalars. The sample variance $ \sigma_y^2 $ is the second sample central moment and is defined by $ \sigma_y^2 = \frac{1}{n} \sum_{i=1}^n \left(y_i - \mu \right)^2 $, where $ \mu $ is the sample mean and $ n $ is the sample size. This procedure lets the users choose the initial position and the number of elements of the sequence which will be used to evaluate the sample variance.

Definition at line 1042 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::subSampleVarianceExtra().

1046 {
1047  if (this->subSequenceSize() == 0) return 0.;
1048 
1049  bool bRC = ((initialPos < this->subSequenceSize()) &&
1050  (0 < numPos ) &&
1051  ((initialPos+numPos) <= this->subSequenceSize()));
1052  queso_require_msg(bRC, "invalid input data");
1053 
1054  unsigned int finalPosPlus1 = initialPos + numPos;
1055  T diff;
1056  T samValue = 0.;
1057  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1058  diff = m_seq[j] - meanValue;
1059  samValue += diff*diff;
1060  }
1061 
1062  samValue /= (((T) numPos) - 1.);
1063 
1064  return samValue;
1065 }
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
const T & QUESO::ScalarSequence< T >::subSampleVariancePlain ( ) const

Finds the variance of a sample of the sub-sequence of scalars.

Definition at line 441 of file ScalarSequence.C.

442 {
443  if (m_subSampleVariancePlain == NULL) {
444  m_subSampleVariancePlain = new T(0.);
446  }
447 
448  return *m_subSampleVariancePlain;
449 }
const T & subMeanPlain() const
Finds the mean value of the sub-sequence of scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
T subSampleVarianceExtra(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
Finds the sample variance of the sub-sequence, considering numPos positions starting at position init...
template<class T>
T QUESO::ScalarSequence< T >::subScaleForKde ( unsigned int  initialPos,
const T &  iqrValue,
unsigned int  kdeDimension 
) const

Selects the scales (output value) for the kernel density estimation, considering only the sub-sequence.

The bandwidth of the kernel is a free parameter which exhibits a strong influence on the resulting estimate. Silverman (1986) suggests the following normal-based estimates: S1 = 1.06 × (standard deviation) × n^{-1/5} S2 = 0.79 × (iqrValue) × n^{-1/5}, where iqrValue is the interquartile range OutputValue = 0.90 × minimum(standard deviation, iqrValue /1.34) × n^{-1/5}. These estimates are popular due to their simplicity, and are used in QUESO with the adaptation of the exponent oven the sample size n (-1/5) with -1/(4 + kdeDimension) where kdeDimension is the KDE dimension.

Definition at line 2208 of file ScalarSequence.C.

References queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::subScalesForKde().

2212 {
2213  bool bRC = (initialPos < this->subSequenceSize());
2214  queso_require_msg(bRC, "invalid input data");
2215 
2216  unsigned int dataSize = this->subSequenceSize() - initialPos;
2217 
2218  T meanValue = this->subMeanExtra(initialPos,
2219  dataSize);
2220 
2221  T samValue = this->subSampleVarianceExtra(initialPos,
2222  dataSize,
2223  meanValue);
2224 
2225  T scaleValue;
2226  if (iqrValue <= 0.) {
2227  scaleValue = 1.06*std::sqrt(samValue)/std::pow(dataSize,1./(4. + ((double) kdeDimension)));
2228  }
2229  else {
2230  scaleValue = 1.06*std::min(std::sqrt(samValue),iqrValue/1.34)/std::pow(dataSize,1./(4. + ((double) kdeDimension)));
2231  }
2232 
2233  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2234  *m_env.subDisplayFile() << "In ScalarSequence<T>::subScaleForKde()"
2235  << ": iqrValue = " << iqrValue
2236  << ", meanValue = " << meanValue
2237  << ", samValue = " << samValue
2238  << ", dataSize = " << dataSize
2239  << ", scaleValue = " << scaleValue
2240  << std::endl;
2241  }
2242 
2243  return scaleValue;
2244 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
T subSampleVarianceExtra(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
Finds the sample variance of the sub-sequence, considering numPos positions starting at position init...
template<class T >
unsigned int QUESO::ScalarSequence< T >::subSequenceSize ( ) const
template<class T>
void QUESO::ScalarSequence< T >::subSort ( unsigned int  initialPos,
ScalarSequence< T > &  sortedSequence 
) const

Sorts the sub-sequence of scalars.

Definition at line 1883 of file ScalarSequence.C.

References QUESO::ScalarSequence< T >::resizeSequence(), and QUESO::ScalarSequence< T >::subSort().

Referenced by QUESO::ScalarSequence< T >::subMedianExtra(), and QUESO::ScalarSequence< T >::subSort().

1886 {
1887  unsigned int numPos = this->subSequenceSize() - initialPos;
1888  sortedSequence.resizeSequence(numPos);
1889  this->extractScalarSeq(initialPos,
1890  1,
1891  numPos,
1892  sortedSequence);
1893  sortedSequence.subSort();
1894 
1895  return;
1896 }
void extractScalarSeq(unsigned int initialPos, unsigned int spacing, unsigned int numPos, ScalarSequence< T > &scalarSeq) const
Extracts a sequence of scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::subSort ( )
private

Sorts the sequence of scalars in the private attribute m_seq.

Definition at line 3189 of file ScalarSequence.C.

3190 {
3191  std::sort(m_seq.begin(), m_seq.end());
3192  return;
3193 }
std::vector< T > m_seq
template<class T>
void QUESO::ScalarSequence< T >::subUniformlySampledCdf ( unsigned int  numIntervals,
T &  minDomainValue,
T &  maxDomainValue,
std::vector< T > &  cdfValues 
) const

Uniformly samples from the CDF from the sub-sequence.

Definition at line 569 of file ScalarSequence.C.

574 {
575  T tmpMinValue;
576  T tmpMaxValue;
577  std::vector<T> centers(numEvaluationPoints,0.);
578  std::vector<unsigned int> bins (numEvaluationPoints,0);
579 
580  subMinMaxExtra(0, // initialPos
581  this->subSequenceSize(),
582  tmpMinValue,
583  tmpMaxValue);
584  subHistogram(0, // initialPos,
585  tmpMinValue,
586  tmpMaxValue,
587  centers,
588  bins);
589 
590  minDomainValue = centers[0];
591  maxDomainValue = centers[centers.size()-1];
592 
593  unsigned int sumOfBins = 0;
594  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
595  sumOfBins += bins[i];
596  }
597 
598  cdfValues.clear();
599  cdfValues.resize(numEvaluationPoints);
600  unsigned int partialSum = 0;
601  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
602  partialSum += bins[i];
603  cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
604  }
605 
606  return;
607 }
void subHistogram(unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, std::vector< T > &centers, std::vector< unsigned int > &bins) const
Calculates the histogram of the sub-sequence.
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::subWeightCdf ( unsigned int  numIntervals,
std::vector< T > &  gridValues,
std::vector< T > &  cdfValues 
) const

Finds the Weighted Cumulative Distribution Function (CDF) of the sub-sequence of scalars.

Definition at line 747 of file ScalarSequence.C.

751 {
752  T tmpMinValue;
753  T tmpMaxValue;
754  std::vector<unsigned int> bins(numEvaluationPoints,0);
755  gridValues.resize (numEvaluationPoints,0.);
756  cdfValues.resize (numEvaluationPoints,0.);
757 
758  subMinMaxExtra(0, // initialPos
759  this->subSequenceSize(),
760  tmpMinValue,
761  tmpMaxValue);
762 
763  if (tmpMinValue == tmpMaxValue) {
764  if (tmpMinValue < -1.e-12) {
765  tmpMinValue += tmpMinValue*(1.e-8);
766  }
767  else if (tmpMinValue > 1.e-12) {
768  tmpMinValue -= tmpMinValue*(1.e-8);
769  }
770  else {
771  tmpMinValue = 1.e-12;
772  }
773  }
774 
775  subWeightHistogram(0, // initialPos,
776  tmpMinValue,
777  tmpMaxValue,
778  gridValues,
779  bins);
780 
781  unsigned int sumOfBins = 0;
782  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
783  sumOfBins += bins[i];
784  }
785 
786  cdfValues.clear();
787  cdfValues.resize(numEvaluationPoints);
788  unsigned int partialSum = 0;
789  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
790  partialSum += bins[i];
791  cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
792  }
793 
794  return;
795 }
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void subWeightHistogram(unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, UniformOneDGrid< T > *&gridValues, std::vector< unsigned int > &bins) const
Calculates the weighted histogram of the sub-sequence.
template<class T>
void QUESO::ScalarSequence< T >::subWeightCdf ( unsigned int  numIntervals,
UniformOneDGrid< T > *&  gridValues,
std::vector< T > &  cdfValues 
) const

Finds the Weighted Cumulative Distribution Function (CDF) of the sub-sequence of scalars.

Definition at line 799 of file ScalarSequence.C.

803 {
804  T tmpMinValue;
805  T tmpMaxValue;
806  std::vector<unsigned int> bins(numEvaluationPoints,0);
807 
808  subMinMaxExtra(0, // initialPos
809  this->subSequenceSize(),
810  tmpMinValue,
811  tmpMaxValue);
812  subWeightHistogram(0, // initialPos,
813  tmpMinValue,
814  tmpMaxValue,
815  gridValues,
816  bins);
817 
818  unsigned int sumOfBins = 0;
819  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
820  sumOfBins += bins[i];
821  }
822 
823  cdfValues.clear();
824  cdfValues.resize(numEvaluationPoints);
825  unsigned int partialSum = 0;
826  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
827  partialSum += bins[i];
828  cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
829  }
830 
831  return;
832 }
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void subWeightHistogram(unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, UniformOneDGrid< T > *&gridValues, std::vector< unsigned int > &bins) const
Calculates the weighted histogram of the sub-sequence.
template<class T>
void QUESO::ScalarSequence< T >::subWeightHistogram ( unsigned int  initialPos,
const T &  minHorizontalValue,
const T &  maxHorizontalValue,
UniformOneDGrid< T > *&  gridValues,
std::vector< unsigned int > &  bins 
) const

Calculates the weighted histogram of the sub-sequence.

TODO:Note that this method is exactly the same as subBasicHistogram().

It requires the specification of the maximum (maxHorizontalValue) and the minimum (minHorizontalValue) values if the data and the initial position (initialPos) from where the data will be considered. Output: grid values that will act as the center of each bin (gridValues) and the number of data occurrences in each bin (bins).

Definition at line 1789 of file ScalarSequence.C.

References queso_require_greater_equal_msg.

1795 {
1796  queso_require_greater_equal_msg(bins.size(), 3, "number of 'bins' is too small: should be at least 3");
1797 
1798  for (unsigned int j = 0; j < bins.size(); ++j) {
1799  bins[j] = 0;
1800  }
1801 
1802  double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((double) bins.size()) - 2.); // IMPORTANT: -2
1803  double minCenter = minHorizontalValue - horizontalDelta/2.;
1804  double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1805  gridValues = new UniformOneDGrid<T>(m_env,
1806  "",
1807  bins.size(),
1808  minCenter,
1809  maxCenter);
1810 
1811  unsigned int dataSize = this->subSequenceSize();
1812  for (unsigned int j = 0; j < dataSize; ++j) {
1813  double value = m_seq[j];
1814  if (value < minHorizontalValue) {
1815  bins[0] += value;
1816  }
1817  else if (value >= maxHorizontalValue) {
1818  bins[bins.size()-1] += value;
1819  }
1820  else {
1821  unsigned int index = 1 + (unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1822  bins[index] += value;
1823  }
1824  }
1825 
1826  return;
1827 }
const BaseEnvironment & m_env
std::vector< T > m_seq
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::subWeightHistogram ( unsigned int  initialPos,
const T &  minHorizontalValue,
const T &  maxHorizontalValue,
std::vector< T > &  gridValues,
std::vector< unsigned int > &  bins 
) const

Calculates the weighted histogram of the sub-sequence.

In this method, gridValues is a std::vector<T>, but internally, it will be copied to an structure of the type UniformOneDGrid<T>.

Definition at line 1831 of file ScalarSequence.C.

References queso_require_greater_equal_msg.

1837 {
1838  queso_require_greater_equal_msg(bins.size(), 3, "number of 'bins' is too small: should be at least 3");
1839 
1840  for (unsigned int j = 0; j < bins.size(); ++j) {
1841  bins[j] = 0;
1842  }
1843 
1844  double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((double) bins.size()) - 2.); // IMPORTANT: -2
1845  double minCenter = minHorizontalValue - horizontalDelta/2.;
1846  double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1847  UniformOneDGrid<T> tmpGrid(m_env,
1848  "",
1849  bins.size(),
1850  minCenter,
1851  maxCenter);
1852  gridValues.clear();
1853  gridValues.resize(tmpGrid.size(),0.);
1854  for (unsigned int i = 0; i < tmpGrid.size(); ++i) {
1855  gridValues[i] = tmpGrid[i];
1856  }
1857 
1858  unsigned int dataSize = this->subSequenceSize();
1859  for (unsigned int j = 0; j < dataSize; ++j) {
1860  double value = m_seq[j];
1861  if (value < minHorizontalValue) {
1862  bins[0] += value;
1863  }
1864  else if (value >= maxHorizontalValue) {
1865  bins[bins.size()-1] += value;
1866  }
1867  else {
1868  unsigned int index = 1 + (unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1869  bins[index] += value;
1870  }
1871  }
1872 
1873  //std::cout << "In ScalarSequence<T>::subWeightHistogram():" << std::endl;
1874  //for (unsigned int j = 0; j < bins.size(); ++j) {
1875  // std::cout << "bins[" << j << "] = " << bins[j] << std::endl;
1876  //}
1877 
1878  return;
1879 }
const BaseEnvironment & m_env
std::vector< T > m_seq
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
void QUESO::ScalarSequence< T >::subWriteContents ( unsigned int  initialPos,
unsigned int  numPos,
const std::string &  fileName,
const std::string &  fileType,
const std::set< unsigned int > &  allowedSubEnvIds 
) const

Writes the sub-sequence to a file.

Given the allowed sub environments (allowedSubEnvIds) that are allowed to write to file, together with the file name and type (fileName, fileType), it writes the entire sub- sequence to the file. The sum of the initial position of the sequence (initialPos) with the number of positions that will be written (numPos) must equal the size of the sequence.

Definition at line 2578 of file ScalarSequence.C.

References queso_require_greater_equal_msg.

Referenced by QUESO::MetropolisHastingsSG< P_V, P_M >::generateFullChain(), and QUESO::MetropolisHastingsSG< P_V, P_M >::generateSequence().

2584 {
2585  queso_require_greater_equal_msg(m_env.subRank(), 0, "unexpected subRank");
2586 
2587  FilePtrSetStruct filePtrSet;
2588  if (m_env.openOutputFile(fileName,
2589  fileType,
2590  allowedSubEnvIds,
2591  false, // A 'true' causes problems when the user chooses (via options
2592  // in the input file) to use just one file for all outputs.
2593  filePtrSet)) {
2594  this->subWriteContents(initialPos,
2595  numPos,
2596  *filePtrSet.ofsVar,
2597  fileType);
2598  m_env.closeFile(filePtrSet,fileType);
2599  }
2600 
2601  return;
2602 }
const BaseEnvironment & m_env
int subRank() const
Access function for sub-rank.
Definition: Environment.C:241
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
Definition: Environment.C:1020
bool openOutputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens an output file for each sub-environment that was chosen to send data to the file...
Definition: Environment.C:467
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
void subWriteContents(unsigned int initialPos, unsigned int numPos, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
Writes the sub-sequence to a file.
template<class T >
void QUESO::ScalarSequence< T >::subWriteContents ( unsigned int  initialPos,
unsigned int  numPos,
std::ofstream &  ofs,
const std::string &  fileType 
) const

Writes the sub-sequence to a file.

Uses object of the type std::ofstream.

Definition at line 2606 of file ScalarSequence.C.

2611 {
2612  if (initialPos) {}; // just to remove compiler warning
2613  if (numPos) {}; // just to remove compiler warning
2614  if (&fileType) {}; // just to remove compiler warning
2615  ofs << m_name << "_sub" << m_env.subIdString() << " = zeros(" << this->subSequenceSize()
2616  << "," << 1
2617  << ");"
2618  << std::endl;
2619  ofs << m_name << "_sub" << m_env.subIdString() << " = [";
2620  unsigned int chainSize = this->subSequenceSize();
2621  for (unsigned int j = 0; j < chainSize; ++j) {
2622  ofs << m_seq[j]
2623  << std::endl;
2624  }
2625  ofs << "];\n";
2626 
2627  return;
2628 }
const BaseEnvironment & m_env
const std::string & subIdString() const
Access to the attribute m_subIdString; which stores the string for the sub-environment, and it will be used, for instance, to create the output files for each sub-environment.
Definition: Environment.C:301
std::vector< T > m_seq
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
void QUESO::ScalarSequence< T >::unifiedGaussian1dKde ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
double  unifiedScaleValue,
const std::vector< T > &  unifiedEvaluationPositions,
std::vector< double > &  unifiedDensityValues 
) const

Gaussian kernel for the KDE estimate of the unified sequence.

Definition at line 2349 of file ScalarSequence.C.

References k, QUESO::MiscGaussianDensity(), queso_error_msg, queso_require_msg, RawValue_MPI_DOUBLE, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedGaussian1dKde().

2355 {
2356  if (m_env.numSubEnvironments() == 1) {
2357  return this->subGaussian1dKde(initialPos,
2358  unifiedScaleValue,
2359  unifiedEvaluationPositions,
2360  unifiedDensityValues);
2361  }
2362 
2363  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
2364 
2365  if (useOnlyInter0Comm) {
2366  if (m_env.inter0Rank() >= 0) {
2367  bool bRC = ((initialPos < this->subSequenceSize() ) &&
2368  (0 < unifiedEvaluationPositions.size()) &&
2369  (unifiedEvaluationPositions.size() == unifiedDensityValues.size() ));
2370  queso_require_msg(bRC, "invalid input data");
2371 
2372  unsigned int localDataSize = this->subSequenceSize() - initialPos;
2373  unsigned int unifiedDataSize = 0;
2374  m_env.inter0Comm().Allreduce((void *) &localDataSize, (void *) &unifiedDataSize, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
2375  "ScalarSequence<T>::unifiedGaussian1dKde()",
2376  "failed MPI.Allreduce() for data size");
2377 
2378  unsigned int numEvals = unifiedEvaluationPositions.size();
2379 
2380  std::vector<double> densityValues(numEvals,0.);
2381  double unifiedScaleInv = 1./unifiedScaleValue;
2382  for (unsigned int j = 0; j < numEvals; ++j) {
2383  double x = unifiedEvaluationPositions[j];
2384  double value = 0.;
2385  for (unsigned int k = 0; k < localDataSize; ++k) {
2386  double xk = m_seq[initialPos+k];
2387  value += MiscGaussianDensity((x-xk)*unifiedScaleInv,0.,1.);
2388  }
2389  densityValues[j] = value;
2390  }
2391 
2392  for (unsigned int j = 0; j < numEvals; ++j) {
2393  unifiedDensityValues[j] = 0.;
2394  }
2395  m_env.inter0Comm().Allreduce((void *) &densityValues[0], (void *) &unifiedDensityValues[0], (int) numEvals, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
2396  "ScalarSequence<T>::unifiedGaussian1dKde()",
2397  "failed MPI.Allreduce() for density values");
2398 
2399  for (unsigned int j = 0; j < numEvals; ++j) {
2400  unifiedDensityValues[j] *= unifiedScaleInv/((double) unifiedDataSize);
2401  }
2402 
2403  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2404  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedGaussian1dKde()"
2405  << ": unifiedDensityValues[0] = " << unifiedDensityValues[0]
2406  << ", unifiedDensityValues[" << unifiedDensityValues.size()-1 << "] = " << unifiedDensityValues[unifiedDensityValues.size()-1]
2407  << std::endl;
2408  }
2409  }
2410  else {
2411  // Node not in the 'inter0' communicator
2412  this->subGaussian1dKde(initialPos,
2413  unifiedScaleValue,
2414  unifiedEvaluationPositions,
2415  unifiedDensityValues);
2416  }
2417  }
2418  else {
2419  queso_error_msg("parallel vectors not supported yet");
2420  }
2421 
2422  //m_env.fullComm().Barrier();
2423 
2424  return;
2425 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
double MiscGaussianDensity(double x, double mu, double sigma)
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
void subGaussian1dKde(unsigned int initialPos, double scaleValue, const std::vector< T > &evaluationPositions, std::vector< double > &densityValues) const
Gaussian kernel for the KDE estimate of the sub-sequence.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
int k
Definition: ann_sample.cpp:53
template<class T>
void QUESO::ScalarSequence< T >::unifiedHistogram ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
const T &  unifiedMinHorizontalValue,
const T &  unifiedMaxHorizontalValue,
std::vector< T > &  unifiedCenters,
std::vector< unsigned int > &  unifiedBins 
) const

Calculates the histogram of the unified sequence.

Definition at line 1657 of file ScalarSequence.C.

References queso_error_msg, queso_require_equal_to_msg, queso_require_greater_equal_msg, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedHistogram().

1664 {
1665  if (m_env.numSubEnvironments() == 1) {
1666  return this->subHistogram(initialPos,
1667  unifiedMinHorizontalValue,
1668  unifiedMaxHorizontalValue,
1669  unifiedCenters,
1670  unifiedBins);
1671  }
1672 
1673  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
1674 
1675  if (useOnlyInter0Comm) {
1676  if (m_env.inter0Rank() >= 0) {
1677  queso_require_equal_to_msg(unifiedCenters.size(), unifiedBins.size(), "vectors 'unifiedCenters' and 'unifiedBins' have different sizes");
1678 
1679  queso_require_greater_equal_msg(unifiedBins.size(), 3, "number of 'unifiedBins' is too small: should be at least 3");
1680 
1681  for (unsigned int j = 0; j < unifiedBins.size(); ++j) {
1682  unifiedCenters[j] = 0.;
1683  unifiedBins[j] = 0;
1684  }
1685 
1686  double unifiedHorizontalDelta = (unifiedMaxHorizontalValue - unifiedMinHorizontalValue)/(((double) unifiedBins.size()) - 2.); // IMPORTANT: -2
1687 
1688  double unifiedMinCenter = unifiedMinHorizontalValue - unifiedHorizontalDelta/2.;
1689  double unifiedMaxCenter = unifiedMaxHorizontalValue + unifiedHorizontalDelta/2.;
1690  for (unsigned int j = 0; j < unifiedCenters.size(); ++j) {
1691  double factor = ((double) j)/(((double) unifiedCenters.size()) - 1.);
1692  unifiedCenters[j] = (1. - factor) * unifiedMinCenter + factor * unifiedMaxCenter;
1693  }
1694 
1695  std::vector<unsigned int> localBins(unifiedBins.size(),0);
1696  unsigned int dataSize = this->subSequenceSize();
1697  for (unsigned int j = 0; j < dataSize; ++j) {
1698  double value = m_seq[j];
1699  if (value < unifiedMinHorizontalValue) {
1700  localBins[0]++;
1701  }
1702  else if (value >= unifiedMaxHorizontalValue) {
1703  localBins[localBins.size()-1]++;
1704  }
1705  else {
1706  unsigned int index = 1 + (unsigned int) ((value - unifiedMinHorizontalValue)/unifiedHorizontalDelta);
1707  localBins[index]++;
1708  }
1709  }
1710 
1711  m_env.inter0Comm().Allreduce((void *) &localBins[0], (void *) &unifiedBins[0], (int) localBins.size(), RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
1712  "ScalarSequence<T>::unifiedHistogram()",
1713  "failed MPI.Allreduce() for bins");
1714 
1715  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1716  for (unsigned int i = 0; i < unifiedCenters.size(); ++i) {
1717  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedHistogram()"
1718  << ": i = " << i
1719  << ", unifiedMinHorizontalValue = " << unifiedMinHorizontalValue
1720  << ", unifiedMaxHorizontalValue = " << unifiedMaxHorizontalValue
1721  << ", unifiedCenters = " << unifiedCenters[i]
1722  << ", unifiedBins = " << unifiedBins[i]
1723  << std::endl;
1724  }
1725  }
1726  }
1727  else {
1728  // Node not in the 'inter0' communicator
1729  this->subHistogram(initialPos,
1730  unifiedMinHorizontalValue,
1731  unifiedMaxHorizontalValue,
1732  unifiedCenters,
1733  unifiedBins);
1734  }
1735  }
1736  else {
1737  queso_error_msg("parallel vectors not supported yet");
1738  }
1739 
1740  //m_env.fullComm().Barrier();
1741 
1742  return;
1743 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
void subHistogram(unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, std::vector< T > &centers, std::vector< unsigned int > &bins) const
Calculates the histogram of the sub-sequence.
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T >
T QUESO::ScalarSequence< T >::unifiedInterQuantileRange ( bool  useOnlyInter0Comm,
unsigned int  initialPos 
) const

Returns the interquartile range of the values in the unified sequence.

Definition at line 2117 of file ScalarSequence.C.

References queso_error_msg, queso_require_equal_to_msg, RawValue_MPI_SUM, RawValue_MPI_UNSIGNED, and QUESO::ScalarSequence< T >::subSequenceSize().

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedInterQuantileRange().

2120 {
2121  T unifiedIqrValue = 0.;
2122 
2123  if (m_env.numSubEnvironments() == 1) {
2124  return this->subInterQuantileRange(initialPos);
2125  }
2126 
2127  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
2128 
2129  if (useOnlyInter0Comm) {
2130  if (m_env.inter0Rank() >= 0) {
2131  //m_env.syncPrintDebugMsg("In ScalarSequence<T>::unifiedInterQuantileRange(), beginning logic",3,3000000,m_env.inter0Comm()); // Dangerous to barrier on inter0Comm ... // KAUST
2132 
2133  ScalarSequence unifiedSortedSequence(m_env,0,"");
2134  this->unifiedSort(useOnlyInter0Comm,
2135  initialPos,
2136  unifiedSortedSequence);
2137  unsigned int unifiedDataSize = unifiedSortedSequence.subSequenceSize();
2138 
2139  unsigned int localDataSize = this->subSequenceSize() - initialPos;
2140  unsigned int sumOfLocalSizes = 0;
2141  m_env.inter0Comm().Allreduce((void *) &localDataSize, (void *) &sumOfLocalSizes, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
2142  "ScalarSequence<T>::unifiedInterQuantileRange()",
2143  "failed MPI.Allreduce() for data size");
2144 
2145  queso_require_equal_to_msg(sumOfLocalSizes, unifiedDataSize, "incompatible unified sizes");
2146 
2147  unsigned int pos1 = (unsigned int) ( (((double) unifiedDataSize) + 1.)*1./4. - 1. );
2148  unsigned int pos3 = (unsigned int) ( (((double) unifiedDataSize) + 1.)*3./4. - 1. );
2149 
2150  double fraction1 = (((double) unifiedDataSize) + 1.)*1./4. - 1. - ((double) pos1);
2151  double fraction3 = (((double) unifiedDataSize) + 1.)*3./4. - 1. - ((double) pos3);
2152 
2153  T value1 = (1.-fraction1) * unifiedSortedSequence[pos1] + fraction1 * unifiedSortedSequence[pos1+1];
2154  T value3 = (1.-fraction3) * unifiedSortedSequence[pos3] + fraction3 * unifiedSortedSequence[pos3+1];
2155  unifiedIqrValue = value3 - value1;
2156 
2157  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2158  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedInterQuantileRange()"
2159  << ": unifiedIqrValue = " << unifiedIqrValue
2160  << ", localDataSize = " << localDataSize
2161  << ", unifiedDataSize = " << unifiedDataSize
2162  << ", pos1 = " << pos1
2163  << ", pos3 = " << pos3
2164  << ", value1 = " << value1
2165  << ", value3 = " << value3
2166  << std::endl;
2167 
2168  // Save data only once into a separate file
2169  //std::ofstream* ofsvar = new std::ofstream(("unif_sort_sub"+m_env.subIdString()+".m").c_str(), std::ofstream::out | std::ofstream::in | std::ofstream::ate);
2170  //if ((ofsvar == NULL ) ||
2171  // (ofsvar->is_open() == false)) {
2172  // delete ofsvar;
2173  // ofsvar = new std::ofstream(("unif_sort_sub"+m_env.subIdString()+".m").c_str(), std::ofstream::out | std::ofstream::trunc);
2174 
2175  // *ofsvar << "var_unif_sort_sub" << m_env.subIdString() << " = zeros(" << 1
2176  // << "," << unifiedDataSize
2177  // << ");"
2178  // << std::endl;
2179  // for (unsigned int j = 0; j < unifiedDataSize; ++j) {
2180  // *ofsvar << "var_unif_sort_sub" << m_env.subIdString() << "(" << 1
2181  // << "," << j+1
2182  // << ") = " << unifiedSortedSequence[j]
2183  // << ";"
2184  // << std::endl;
2185  // }
2186  //}
2187  //delete ofsvar;
2188  }
2189 
2190  //m_env.syncPrintDebugMsg("In ScalarSequence<T>::unifiedInterQuantileRange(), ending logic",3,3000000,m_env.inter0Comm()); // Dangerous to barrier on inter0Comm ... // KAUST
2191  }
2192  else {
2193  // Node not in the 'inter0' communicator
2194  unifiedIqrValue = this->subInterQuantileRange(initialPos);
2195  }
2196  }
2197  else {
2198  queso_error_msg("parallel vectors not supported yet");
2199  }
2200 
2201  //m_env.fullComm().Barrier();
2202 
2203  return unifiedIqrValue;
2204 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
T subInterQuantileRange(unsigned int initialPos) const
Returns the interquartile range of the values in the sub-sequence.
ScalarSequence(const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
Default constructor.
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
void unifiedSort(bool useOnlyInter0Comm, unsigned int initialPos, ScalarSequence< T > &unifiedSortedSequence) const
Sorts the unified sequence of scalars.
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T >
const T & QUESO::ScalarSequence< T >::unifiedMaxPlain ( bool  useOnlyInter0Comm) const

Finds the maximum value of the unified sequence of scalars.

Definition at line 380 of file ScalarSequence.C.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence_Step03_inter0().

381 {
382  if (m_unifiedMaxPlain == NULL) {
383  if (m_unifiedMinPlain == NULL) m_unifiedMinPlain = new T(0.);
384  m_unifiedMaxPlain = new T(0.);
386  }
387 
388  return *m_unifiedMaxPlain;
389 }
void unifiedMinMaxExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, T &unifiedMinValue, T &unifiedMaxValue) const
Finds the minimum and the maximum values of the unified sequence, considering numPos positions starti...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::unifiedMeanExtra ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  localNumPos 
) const

Finds the mean value of the unified sequence of numPos positions starting at position initialPos.

Definition at line 874 of file ScalarSequence.C.

References queso_error_msg, queso_require_msg, RawValue_MPI_DOUBLE, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::ComputeCovCorrBetweenScalarSequences(), QUESO::MLSampling< P_V, P_M >::generateSequence(), and QUESO::SequenceOfVectors< V, M >::unifiedMeanExtra().

878 {
879  if (m_env.numSubEnvironments() == 1) {
880  return this->subMeanExtra(initialPos,
881  numPos);
882  }
883 
884  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
885 
886  T unifiedMeanValue = 0.;
887  if (useOnlyInter0Comm) {
888  if (m_env.inter0Rank() >= 0) {
889  bool bRC = ((initialPos < this->subSequenceSize()) &&
890  (0 < numPos ) &&
891  ((initialPos+numPos) <= this->subSequenceSize()));
892  queso_require_msg(bRC, "invalid input data");
893 
894  unsigned int finalPosPlus1 = initialPos + numPos;
895  T localSum = 0.;
896  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
897  localSum += m_seq[j];
898  }
899 
900  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
901  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedMeanExtra()"
902  << ": initialPos = " << initialPos
903  << ", numPos = " << numPos
904  << ", before MPI.Allreduce"
905  << std::endl;
906  }
907  //std::cout << m_env.inter0Comm().MyPID()
908  // << std::endl;
909  //sleep(1);
910  unsigned int unifiedNumPos = 0;
911  m_env.inter0Comm().Allreduce((void *) &numPos, (void *) &unifiedNumPos, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
912  "ScalarSequence<T>::unifiedMeanExtra()",
913  "failed MPI.Allreduce() for numPos");
914 
915  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
916  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedMeanExtra()"
917  << ": numPos = " << numPos
918  << ", unifiedNumPos = " << unifiedNumPos
919  << std::endl;
920  }
921 
922  m_env.inter0Comm().Allreduce((void *) &localSum, (void *) &unifiedMeanValue, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
923  "ScalarSequence<T>::unifiedMeanExtra()",
924  "failed MPI.Allreduce() for sum");
925 
926  unifiedMeanValue /= ((T) unifiedNumPos);
927 
928  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
929  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedMeanExtra()"
930  << ": localSum = " << localSum
931  << ", unifiedMeanValue = " << unifiedMeanValue
932  << std::endl;
933  }
934  }
935  else {
936  // Node not in the 'inter0' communicator
937  this->subMeanExtra(initialPos,
938  numPos);
939  }
940  }
941  else {
942  queso_error_msg("parallel vectors not supported yet");
943  }
944 
945  //m_env.fullComm().Barrier();
946 
947  return unifiedMeanValue;
948 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T >
const T & QUESO::ScalarSequence< T >::unifiedMeanPlain ( bool  useOnlyInter0Comm) const

Finds the mean value of the unified sequence of scalars.

Definition at line 405 of file ScalarSequence.C.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence().

406 {
407  if (m_unifiedMeanPlain == NULL) {
408  m_unifiedMeanPlain = new T(0.);
409  *m_unifiedMeanPlain = unifiedMeanExtra(useOnlyInter0Comm,0,subSequenceSize());
410  }
411 
412  return *m_unifiedMeanPlain;
413 }
T unifiedMeanExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos) const
Finds the mean value of the unified sequence of numPos positions starting at position initialPos...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
T QUESO::ScalarSequence< T >::unifiedMedianExtra ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  localNumPos 
) const

Finds the median value of the unified sequence, considering numPos positions starting at position initialPos.

Definition at line 995 of file ScalarSequence.C.

References queso_error_msg, and queso_require_msg.

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedMedianExtra().

999 {
1000  if (m_env.numSubEnvironments() == 1) {
1001  return this->subMedianExtra(initialPos,
1002  numPos);
1003  }
1004 
1005  // As of 07/Jul/2012, this routine does *not* require sub sequences to have equal size. Good.
1006 
1007  T unifiedMedianValue = 0.;
1008  if (useOnlyInter0Comm) {
1009  if (m_env.inter0Rank() >= 0) {
1010  bool bRC = ((initialPos < this->subSequenceSize()) &&
1011  (0 < numPos ) &&
1012  ((initialPos+numPos) <= this->subSequenceSize()));
1013  queso_require_msg(bRC, "invalid input data");
1014 
1015  ScalarSequence unifiedSortedSequence(m_env,0,"");
1016  this->unifiedSort(useOnlyInter0Comm,
1017  initialPos,
1018  unifiedSortedSequence);
1019  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1020  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedMedianExtra()"
1021  << ", unifiedMedianValue = " << unifiedMedianValue
1022  << std::endl;
1023  }
1024  }
1025  else {
1026  // Node not in the 'inter0' communicator
1027  this->subMedianExtra(initialPos,
1028  numPos);
1029  }
1030  }
1031  else {
1032  queso_error_msg("parallel vectors not supported yet");
1033  }
1034 
1035  //m_env.fullComm().Barrier();
1036 
1037  return unifiedMedianValue;
1038 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
ScalarSequence(const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
Default constructor.
#define queso_error_msg(msg)
Definition: asserts.h:47
T subMedianExtra(unsigned int initialPos, unsigned int numPos) const
Finds the median value of the sub-sequence, considering numPos positions starting at position initial...
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
void unifiedSort(bool useOnlyInter0Comm, unsigned int initialPos, ScalarSequence< T > &unifiedSortedSequence) const
Sorts the unified sequence of scalars.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
const T & QUESO::ScalarSequence< T >::unifiedMedianPlain ( bool  useOnlyInter0Comm) const

Finds the median value of the unified sequence of scalars.

Definition at line 429 of file ScalarSequence.C.

430 {
431  if (m_unifiedMedianPlain == NULL) {
432  m_unifiedMedianPlain = new T(0.);
433  *m_unifiedMedianPlain = unifiedMedianExtra(useOnlyInter0Comm,0,subSequenceSize());
434  }
435 
436  return *m_unifiedMedianPlain;
437 }
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
T unifiedMedianExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos) const
Finds the median value of the unified sequence, considering numPos positions starting at position ini...
template<class T>
void QUESO::ScalarSequence< T >::unifiedMinMaxExtra ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  numPos,
T &  unifiedMinValue,
T &  unifiedMaxValue 
) const

Finds the minimum and the maximum values of the unified sequence, considering numPos positions starting at position initialPos.

Definition at line 1539 of file ScalarSequence.C.

References queso_error_msg, RawValue_MPI_DOUBLE, RawValue_MPI_MAX, and RawValue_MPI_MIN.

Referenced by QUESO::MLSampling< P_V, P_M >::generateSequence(), and QUESO::SequenceOfVectors< V, M >::unifiedMinMaxExtra().

1545 {
1546  if (m_env.numSubEnvironments() == 1) {
1547  return this->subMinMaxExtra(initialPos,
1548  numPos,
1549  unifiedMinValue,
1550  unifiedMaxValue);
1551  }
1552 
1553  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
1554 
1555  if (useOnlyInter0Comm) {
1556  if (m_env.inter0Rank() >= 0) {
1557  // Find local min and max
1558  T minValue;
1559  T maxValue;
1560  this->subMinMaxExtra(initialPos,
1561  numPos,
1562  minValue,
1563  maxValue);
1564 
1565  // Get overall min
1566  std::vector<double> sendBuf(1,0.);
1567  for (unsigned int i = 0; i < sendBuf.size(); ++i) {
1568  sendBuf[i] = minValue;
1569  }
1570  m_env.inter0Comm().Allreduce((void *) &sendBuf[0], (void *) &unifiedMinValue, (int) sendBuf.size(), RawValue_MPI_DOUBLE, RawValue_MPI_MIN,
1571  "ScalarSequence<T>::unifiedMinMaxExtra()",
1572  "failed MPI.Allreduce() for min");
1573 
1574  // Get overall max
1575  for (unsigned int i = 0; i < sendBuf.size(); ++i) {
1576  sendBuf[i] = maxValue;
1577  }
1578  m_env.inter0Comm().Allreduce((void *) &sendBuf[0], (void *) &unifiedMaxValue, (int) sendBuf.size(), RawValue_MPI_DOUBLE, RawValue_MPI_MAX,
1579  "ScalarSequence<T>::unifiedMinMaxExtra()",
1580  "failed MPI.Allreduce() for max");
1581 
1582  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1583  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedMinMaxExtra()"
1584  << ": localMinValue = " << minValue
1585  << ", localMaxValue = " << maxValue
1586  << ", unifiedMinValue = " << unifiedMinValue
1587  << ", unifiedMaxValue = " << unifiedMaxValue
1588  << std::endl;
1589  }
1590  }
1591  else {
1592  // Node not in the 'inter0' communicator
1593  this->subMinMaxExtra(initialPos,
1594  numPos,
1595  unifiedMinValue,
1596  unifiedMaxValue);
1597  }
1598  }
1599  else {
1600  queso_error_msg("parallel vectors not supported yet");
1601  }
1602 
1603  //m_env.fullComm().Barrier();
1604 
1605  return;
1606 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
#define RawValue_MPI_MIN
Definition: MpiComm.h:50
void subMinMaxExtra(unsigned int initialPos, unsigned int numPos, T &minValue, T &maxValue) const
Finds the minimum and the maximum values of the sub-sequence, considering numPos positions starting a...
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
#define RawValue_MPI_MAX
Definition: MpiComm.h:51
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
template<class T >
const T & QUESO::ScalarSequence< T >::unifiedMinPlain ( bool  useOnlyInter0Comm) const

Finds the minimum value of the unified sequence of scalars.

Definition at line 354 of file ScalarSequence.C.

355 {
356  if (m_unifiedMinPlain == NULL) {
357  m_unifiedMinPlain = new T(0.);
358  if (m_unifiedMaxPlain == NULL) m_unifiedMaxPlain = new T(0.);
360  }
361 
362  return *m_unifiedMinPlain;
363 }
void unifiedMinMaxExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, T &unifiedMinValue, T &unifiedMaxValue) const
Finds the minimum and the maximum values of the unified sequence, considering numPos positions starti...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
T QUESO::ScalarSequence< T >::unifiedPopulationVariance ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  numPos,
const T &  unifiedMeanValue 
) const

Finds the population variance of the unified sequence, considering numPos positions starting at position initialPos and of mean meanValue.

Output: calculated population variance of the unified sequence of scalars.

Definition at line 1243 of file ScalarSequence.C.

References queso_error_msg, queso_require_msg, RawValue_MPI_DOUBLE, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedPopulationVariance().

1248 {
1249  if (m_env.numSubEnvironments() == 1) {
1250  return this->subPopulationVariance(initialPos,
1251  numPos,
1252  unifiedMeanValue);
1253  }
1254 
1255  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
1256 
1257  T unifiedPopValue = 0.;
1258  if (useOnlyInter0Comm) {
1259  if (m_env.inter0Rank() >= 0) {
1260  bool bRC = ((initialPos < this->subSequenceSize()) &&
1261  (0 < numPos ) &&
1262  ((initialPos+numPos) <= this->subSequenceSize()));
1263  queso_require_msg(bRC, "invalid input data");
1264 
1265  unsigned int finalPosPlus1 = initialPos + numPos;
1266  T diff;
1267  T localPopValue = 0.;
1268  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1269  diff = m_seq[j] - unifiedMeanValue;
1270  localPopValue += diff*diff;
1271  }
1272 
1273  unsigned int unifiedNumPos = 0;
1274  m_env.inter0Comm().Allreduce((void *) &numPos, (void *) &unifiedNumPos, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
1275  "ScalarSequence<T>::unifiedPopulationVariance()",
1276  "failed MPI.Allreduce() for numPos");
1277 
1278  m_env.inter0Comm().Allreduce((void *) &localPopValue, (void *) &unifiedPopValue, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
1279  "ScalarSequence<T>::unifiedPopulationVariance()",
1280  "failed MPI.Allreduce() for popValue");
1281 
1282  unifiedPopValue /= ((T) unifiedNumPos);
1283  }
1284  else {
1285  // Node not in the 'inter0' communicator
1286  this->subPopulationVariance(initialPos,
1287  numPos,
1288  unifiedMeanValue);
1289  }
1290  }
1291  else {
1292  queso_error_msg("parallel vectors not supported yet");
1293  }
1294 
1295  //m_env.fullComm().Barrier();
1296 
1297  return unifiedPopValue;
1298 }
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
T subPopulationVariance(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
Finds the population variance of the sub-sequence, considering numPos positions starting at position ...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T>
T QUESO::ScalarSequence< T >::unifiedPositionsOfMaximum ( const ScalarSequence< T > &  subCorrespondingScalarValues,
ScalarSequence< T > &  unifiedPositionsOfMaximum 
)

Finds the positions where the maximum element occurs in the unified sequence.

Definition at line 2548 of file ScalarSequence.C.

References queso_require_equal_to_msg, QUESO::ScalarSequence< T >::resizeSequence(), QUESO::ScalarSequence< T >::subMaxPlain(), and QUESO::ScalarSequence< T >::subSequenceSize().

2551 {
2552  queso_require_equal_to_msg(subCorrespondingScalarValues.subSequenceSize(), this->subSequenceSize(), "invalid input");
2553 
2554  T maxValue = subCorrespondingScalarValues.subMaxPlain();
2555  unsigned int iMax = subCorrespondingScalarValues.subSequenceSize();
2556 
2557  unsigned int numPos = 0;
2558  for (unsigned int i = 0; i < iMax; ++i) {
2559  if (subCorrespondingScalarValues[i] == maxValue) {
2560  numPos++;
2561  }
2562  }
2563 
2564  unifiedPositionsOfMaximum.resizeSequence(numPos);
2565  unsigned int j = 0;
2566  for (unsigned int i = 0; i < iMax; ++i) {
2567  if (subCorrespondingScalarValues[i] == maxValue) {
2568  unifiedPositionsOfMaximum[j] = (*this)[i];
2569  j++;
2570  }
2571  }
2572 
2573  return maxValue;
2574 }
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
T unifiedPositionsOfMaximum(const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &unifiedPositionsOfMaximum)
Finds the positions where the maximum element occurs in the unified sequence.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
void QUESO::ScalarSequence< T >::unifiedReadContents ( const std::string &  fileName,
const std::string &  fileType,
const unsigned int  subSequenceSize 
)

Reads the unified sequence from a file.

Definition at line 2833 of file ScalarSequence.C.

References dataIn, QUESO::FilePtrSetStruct::ifsVar, QUESO::MiscGetEllapsedSeconds(), queso_error_msg, queso_require_equal_to_msg, queso_require_greater_equal_msg, queso_require_less_msg, UQ_FILE_EXTENSION_FOR_HDF_FORMAT, UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT, and QUESO::UQ_OK_RC.

Referenced by QUESO::MLSampling< P_V, P_M >::restartML().

2837 {
2838  std::string fileType(inputFileType);
2839 #ifdef QUESO_HAS_HDF5
2840  // Do nothing
2841 #else
2842  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2843  if (m_env.subDisplayFile()) {
2844  *m_env.subDisplayFile() << "WARNING in ScalarSequence<T>::unifiedReadContents()"
2845  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2846  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
2847  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2848  << "' instead..."
2849  << std::endl;
2850  }
2851  if (m_env.subRank() == 0) {
2852  std::cerr << "WARNING in ScalarSequence<T>::unifiedReadContents()"
2853  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2854  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
2855  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2856  << "' instead..."
2857  << std::endl;
2858  }
2860  }
2861 #endif
2862 
2863  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
2864  if (m_env.subDisplayFile()) {
2865  *m_env.subDisplayFile() << "Entering ScalarSequence<T>::unifiedReadContents()"
2866  << ": worldRank " << m_env.worldRank()
2867  << ", fullRank " << m_env.fullRank()
2868  << ", subEnvironment " << m_env.subId()
2869  << ", subRank " << m_env.subRank()
2870  << ", inter0Rank " << m_env.inter0Rank()
2871  //<< ", m_env.inter0Comm().NumProc() = " << m_env.inter0Comm().NumProc()
2872  << ", fileName = " << fileName
2873  << ", subReadSize = " << subReadSize
2874  //<< ", unifiedReadSize = " << unifiedReadSize
2875  << std::endl;
2876  }
2877 
2878  this->resizeSequence(subReadSize);
2879 
2880  if (m_env.inter0Rank() >= 0) {
2881  double unifiedReadSize = subReadSize*m_env.inter0Comm().NumProc();
2882 
2883  // In the logic below, the id of a line' begins with value 0 (zero)
2884  unsigned int idOfMyFirstLine = 1 + m_env.inter0Rank()*subReadSize;
2885  unsigned int idOfMyLastLine = (1 + m_env.inter0Rank())*subReadSize;
2886  unsigned int numParams = 1; // this->vectorSizeLocal();
2887 
2888  for (unsigned int r = 0; r < (unsigned int) m_env.inter0Comm().NumProc(); ++r) { // "m or hdf"
2889  if (m_env.inter0Rank() == (int) r) {
2890  // My turn
2891  FilePtrSetStruct unifiedFilePtrSet;
2892  if (m_env.openUnifiedInputFile(fileName,
2893  fileType,
2894  unifiedFilePtrSet)) {
2895  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2896  if (r == 0) {
2897  // Read number of chain positions in the file by taking care of the first line,
2898  // which resembles something like 'variable_name = zeros(n_positions,m_params);'
2899  std::string tmpString;
2900 
2901  // Read 'variable name' string
2902  *unifiedFilePtrSet.ifsVar >> tmpString;
2903  //std::cout << "Just read '" << tmpString << "'" << std::endl;
2904 
2905  // Read '=' sign
2906  *unifiedFilePtrSet.ifsVar >> tmpString;
2907  //std::cout << "Just read '" << tmpString << "'" << std::endl;
2908  queso_require_equal_to_msg(tmpString, "=", "string should be the '=' sign");
2909 
2910  // Read 'zeros(n_positions,n_params)' string
2911  *unifiedFilePtrSet.ifsVar >> tmpString;
2912  //std::cout << "Just read '" << tmpString << "'" << std::endl;
2913  unsigned int posInTmpString = 6;
2914 
2915  // Isolate 'n_positions' in a string
2916  //char nPositionsString[tmpString.size()-posInTmpString+1]; // avoid compiler warning
2917  std::string nPositionsString((size_t) (tmpString.size()-posInTmpString+1),' ');
2918  unsigned int posInPositionsString = 0;
2919  do {
2920  queso_require_less_msg(posInTmpString, tmpString.size(), "symbol ',' not found in first line of file");
2921  nPositionsString[posInPositionsString++] = tmpString[posInTmpString++];
2922  } while (tmpString[posInTmpString] != ',');
2923  nPositionsString[posInPositionsString] = '\0';
2924 
2925  // Isolate 'n_params' in a string
2926  posInTmpString++; // Avoid reading ',' char
2927  //char nParamsString[tmpString.size()-posInTmpString+1]; // avoid compiler warning
2928  std::string nParamsString((size_t) (tmpString.size()-posInTmpString+1),' ');
2929  unsigned int posInParamsString = 0;
2930  do {
2931  queso_require_less_msg(posInTmpString, tmpString.size(), "symbol ')' not found in first line of file");
2932  nParamsString[posInParamsString++] = tmpString[posInTmpString++];
2933  } while (tmpString[posInTmpString] != ')');
2934  nParamsString[posInParamsString] = '\0';
2935 
2936  // Convert 'n_positions' and 'n_params' strings to numbers
2937  unsigned int sizeOfChainInFile = (unsigned int) strtod(nPositionsString.c_str(),NULL);
2938  unsigned int numParamsInFile = (unsigned int) strtod(nParamsString.c_str(), NULL);
2939  if (m_env.subDisplayFile()) {
2940  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedReadContents()"
2941  << ": worldRank " << m_env.worldRank()
2942  << ", fullRank " << m_env.fullRank()
2943  << ", sizeOfChainInFile = " << sizeOfChainInFile
2944  << ", numParamsInFile = " << numParamsInFile
2945  << std::endl;
2946  }
2947 
2948  // Check if [size of chain in file] >= [requested unified sequence size]
2949  queso_require_greater_equal_msg(sizeOfChainInFile, unifiedReadSize, "size of chain in file is not big enough");
2950 
2951  // Check if [num params in file] == [num params in current chain]
2952  queso_require_equal_to_msg(numParamsInFile, numParams, "number of parameters of chain in file is different than number of parameters in this chain object");
2953  } // if (r == 0)
2954 
2955  // Code common to any core in 'inter0Comm', including core of rank 0
2956  unsigned int maxCharsPerLine = 64*numParams; // Up to about 60 characters to represent each parameter value
2957 
2958  unsigned int lineId = 0;
2959  while (lineId < idOfMyFirstLine) {
2960  unifiedFilePtrSet.ifsVar->ignore(maxCharsPerLine,'\n');
2961  lineId++;
2962  };
2963 
2964  if (r == 0) {
2965  // Take care of initial part of the first data line,
2966  // which resembles something like 'variable_name = [value1 value2 ...'
2967  std::string tmpString;
2968 
2969  // Read 'variable name' string
2970  *unifiedFilePtrSet.ifsVar >> tmpString;
2971  //std::cout << "Core 0 just read '" << tmpString << "'" << std::endl;
2972 
2973  // Read '=' sign
2974  *unifiedFilePtrSet.ifsVar >> tmpString;
2975  //std::cout << "Core 0 just read '" << tmpString << "'" << std::endl;
2976  queso_require_equal_to_msg(tmpString, "=", "in core 0, string should be the '=' sign");
2977 
2978  // Take into account the ' [' portion
2979  std::streampos tmpPos = unifiedFilePtrSet.ifsVar->tellg();
2980  unifiedFilePtrSet.ifsVar->seekg(tmpPos+(std::streampos)2);
2981  }
2982 
2983  T tmpScalar(0.); // V tmpVec(m_vectorSpace.zeroVector());
2984  while (lineId <= idOfMyLastLine) {
2985  for (unsigned int i = 0; i < numParams; ++i) {
2986  *unifiedFilePtrSet.ifsVar >> tmpScalar;
2987  }
2988  m_seq[lineId - idOfMyFirstLine] = tmpScalar;
2989  lineId++;
2990  };
2991  }
2992 #ifdef QUESO_HAS_HDF5
2993  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2994  if (r == 0) {
2995  hid_t dataset = H5Dopen2(unifiedFilePtrSet.h5Var,
2996  "seq_of_vectors",
2997  H5P_DEFAULT); // Dataset access property list
2998  hid_t datatype = H5Dget_type(dataset);
2999  H5T_class_t t_class = H5Tget_class(datatype);
3000  queso_require_equal_to_msg(t_class, H5T_FLOAT, "t_class is not H5T_DOUBLE");
3001  hid_t dataspace = H5Dget_space(dataset);
3002  int rank = H5Sget_simple_extent_ndims(dataspace);
3003  queso_require_equal_to_msg(rank, 2, "hdf rank is not 2");
3004  hsize_t dims_in[2];
3005  int status_n;
3006  status_n = H5Sget_simple_extent_dims(dataspace, dims_in, NULL);
3007  if (status_n) {}; // just to remove compiler warning
3008  //std::cout << "In ScalarSequence<T>::unifiedReadContents()"
3009  // << ": dims_in[0] = " << dims_in[0]
3010  // << ", dims_in[1] = " << dims_in[1]
3011  // << std::endl;
3012  queso_require_equal_to_msg(dims_in[0], numParams, "dims_in[0] is not equal to 'numParams'");
3013  queso_require_greater_equal_msg(dims_in[1], subReadSize, "dims_in[1] is smaller that requested 'subReadSize'");
3014 
3015  struct timeval timevalBegin;
3016  int iRC = UQ_OK_RC;
3017  iRC = gettimeofday(&timevalBegin,NULL);
3018  if (iRC) {}; // just to remove compiler warning
3019 
3020  unsigned int chainSizeIn = (unsigned int) dims_in[1];
3021  //double* dataIn[numParams]; // avoid compiler warning
3022  std::vector<double*> dataIn((size_t) numParams,NULL);
3023  dataIn[0] = (double*) malloc(numParams*chainSizeIn*sizeof(double));
3024  for (unsigned int i = 1; i < numParams; ++i) { // Yes, from '1'
3025  dataIn[i] = dataIn[i-1] + chainSizeIn; // Yes, just 'chainSizeIn', not 'chainSizeIn*sizeof(double)'
3026  }
3027  //std::cout << "In ScalarSequence<T>::unifiedReadContents(): h5 case, memory allocated" << std::endl;
3028  herr_t status;
3029  status = H5Dread(dataset,
3030  H5T_NATIVE_DOUBLE,
3031  H5S_ALL,
3032  dataspace,
3033  H5P_DEFAULT,
3034  dataIn[0]);
3035  if (status) {}; // just to remove compiler warning
3036  //std::cout << "In ScalarSequence<T>::unifiedReadContents(): h5 case, data read" << std::endl;
3037  T tmpScalar(0.); // V tmpVec(m_vectorSpace.zeroVector());
3038  for (unsigned int j = 0; j < subReadSize; ++j) { // Yes, 'subReadSize', not 'chainSizeIn'
3039  for (unsigned int i = 0; i < numParams; ++i) {
3040  tmpScalar = dataIn[i][j];
3041  }
3042  m_seq[j] = tmpScalar;
3043  }
3044 
3045  double readTime = MiscGetEllapsedSeconds(&timevalBegin);
3046  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
3047  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedReadContents()"
3048  << ": worldRank " << m_env.worldRank()
3049  << ", fullRank " << m_env.fullRank()
3050  << ", subEnvironment " << m_env.subId()
3051  << ", subRank " << m_env.subRank()
3052  << ", inter0Rank " << m_env.inter0Rank()
3053  << ", fileName = " << fileName
3054  << ", numParams = " << numParams
3055  << ", chainSizeIn = " << chainSizeIn
3056  << ", subReadSize = " << subReadSize
3057  << ", readTime = " << readTime << " seconds"
3058  << std::endl;
3059  }
3060 
3061  H5Sclose(dataspace);
3062  H5Tclose(datatype);
3063  H5Dclose(dataset);
3064  //free(dataIn[0]); // related to the changes above for compiler warning
3065  for (unsigned int tmpIndex = 0; tmpIndex < dataIn.size(); tmpIndex++) {
3066  free (dataIn[tmpIndex]);
3067  }
3068  }
3069  else {
3070  queso_error_msg("hdf file type not supported for multiple sub-environments yet");
3071  }
3072  }
3073 #endif
3074  else {
3075  queso_error_msg("invalid file type");
3076  }
3077  m_env.closeFile(unifiedFilePtrSet,fileType);
3078  } // if (m_env.openUnifiedInputFile())
3079  } // if (m_env.inter0Rank() == (int) r)
3080  m_env.inter0Comm().Barrier();
3081  } // for r
3082  } // if (m_env.inter0Rank() >= 0)
3083  else {
3084  T tmpScalar(0.); // V tmpVec(m_vectorSpace.zeroVector());
3085  for (unsigned int i = 1; i < subReadSize; ++i) {
3086  m_seq[i] = tmpScalar;
3087  }
3088  }
3089 
3090  if (m_env.subDisplayFile()) {
3091  *m_env.subDisplayFile() << "Leaving ScalarSequence<T>::unifiedReadContents()"
3092  << ", fileName = " << fileName
3093  << std::endl;
3094  }
3095  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
3096 
3097  return;
3098 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
int subRank() const
Access function for sub-rank.
Definition: Environment.C:241
istream * dataIn
Definition: ann_sample.cpp:58
#define UQ_FILE_EXTENSION_FOR_HDF_FORMAT
Definition: Defines.h:90
void resizeSequence(unsigned int newSequenceSize)
Resizes the size of the sequence of scalars.
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:122
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
int fullRank() const
Returns the process full rank.
Definition: Environment.C:222
#define queso_error_msg(msg)
Definition: asserts.h:47
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
Definition: Environment.C:1020
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
double MiscGetEllapsedSeconds(struct timeval *timeval0)
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
std::vector< T > m_seq
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
Definition: Defines.h:89
unsigned int subId() const
Access function to the number of each sub-environment Id: m_subId.
Definition: Environment.C:295
const int UQ_OK_RC
Definition: Defines.h:76
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
int worldRank() const
Returns the process world rank.
Definition: Environment.C:216
bool openUnifiedInputFile(const std::string &fileName, const std::string &fileType, FilePtrSetStruct &filePtrSet) const
Opens the unified input file.
Definition: Environment.C:928
template<class T>
T QUESO::ScalarSequence< T >::unifiedSampleStd ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  localNumPos,
const T &  unifiedMeanValue 
) const

Finds the sample standard deviation of the unified sequence, considering localnumPos positions starting at position initialPos and of mean unifiedMeanValue.

Definition at line 1156 of file ScalarSequence.C.

References queso_error_msg, queso_require_msg, RawValue_MPI_DOUBLE, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedSampleStd().

1161 {
1162  if (m_env.numSubEnvironments() == 1) {
1163  return this->subSampleStd(initialPos,
1164  numPos,
1165  unifiedMeanValue);
1166  }
1167 
1168  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
1169 
1170  T unifiedStdValue = 0.;
1171  if (useOnlyInter0Comm) {
1172  if (m_env.inter0Rank() >= 0) {
1173  bool bRC = ((initialPos < this->subSequenceSize()) &&
1174  (0 < numPos ) &&
1175  ((initialPos+numPos) <= this->subSequenceSize()));
1176  queso_require_msg(bRC, "invalid input data");
1177 
1178  unsigned int finalPosPlus1 = initialPos + numPos;
1179  T diff;
1180  T localStdValue = 0.;
1181  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1182  diff = m_seq[j] - unifiedMeanValue;
1183  localStdValue += diff*diff;
1184  }
1185 
1186  unsigned int unifiedNumPos = 0;
1187  m_env.inter0Comm().Allreduce((void *) &numPos, (void *) &unifiedNumPos, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
1188  "ScalarSequence<T>::unifiedSampleStd()",
1189  "failed MPI.Allreduce() for numPos");
1190 
1191  m_env.inter0Comm().Allreduce((void *) &localStdValue, (void *) &unifiedStdValue, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
1192  "ScalarSequence<T>::unifiedSampleStd()",
1193  "failed MPI.Allreduce() for stdValue");
1194 
1195  unifiedStdValue /= (((T) unifiedNumPos) - 1.);
1196  unifiedStdValue = sqrt(unifiedStdValue);
1197  }
1198  else {
1199  // Node not in the 'inter0' communicator
1200  this->subSampleStd(initialPos,
1201  numPos,
1202  unifiedMeanValue);
1203  }
1204  }
1205  else {
1206  queso_error_msg("parallel vectors not supported yet");
1207  }
1208 
1209  //m_env.fullComm().Barrier();
1210 
1211  return unifiedStdValue;
1212 }
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
T subSampleStd(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
Finds the sample standard deviation of the unified sequence, considering numPos positions starting at...
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T>
T QUESO::ScalarSequence< T >::unifiedSampleVarianceExtra ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
unsigned int  localNumPos,
const T &  unifiedMeanValue 
) const

Finds the sample variance of the unified sequence, considering numPos positions starting at position initialPos and of mean meanVec.

Output:

Parameters
samVecis the vector of the calculated sample variance of the unified sequence of scalars.

Definition at line 1069 of file ScalarSequence.C.

References queso_error_msg, queso_require_msg, RawValue_MPI_DOUBLE, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::ComputeCovCorrBetweenScalarSequences(), and QUESO::SequenceOfVectors< V, M >::unifiedSampleVarianceExtra().

1074 {
1075  if (m_env.numSubEnvironments() == 1) {
1076  return this->subSampleVarianceExtra(initialPos,
1077  numPos,
1078  unifiedMeanValue);
1079  }
1080 
1081  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
1082 
1083  T unifiedSamValue = 0.;
1084  if (useOnlyInter0Comm) {
1085  if (m_env.inter0Rank() >= 0) {
1086  bool bRC = ((initialPos < this->subSequenceSize()) &&
1087  (0 < numPos ) &&
1088  ((initialPos+numPos) <= this->subSequenceSize()));
1089  queso_require_msg(bRC, "invalid input data");
1090 
1091  unsigned int finalPosPlus1 = initialPos + numPos;
1092  T diff;
1093  T localSamValue = 0.;
1094  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1095  diff = m_seq[j] - unifiedMeanValue;
1096  localSamValue += diff*diff;
1097  }
1098 
1099  unsigned int unifiedNumPos = 0;
1100  m_env.inter0Comm().Allreduce((void *) &numPos, (void *) &unifiedNumPos, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
1101  "ScalarSequence<T>::unifiedSampleVarianceExtra()",
1102  "failed MPI.Allreduce() for numPos");
1103 
1104  m_env.inter0Comm().Allreduce((void *) &localSamValue, (void *) &unifiedSamValue, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
1105  "ScalarSequence<T>::unifiedSampleVarianceExtra()",
1106  "failed MPI.Allreduce() for samValue");
1107 
1108  unifiedSamValue /= (((T) unifiedNumPos) - 1.);
1109  }
1110  else {
1111  // Node not in the 'inter0' communicator
1112  this->subSampleVarianceExtra(initialPos,
1113  numPos,
1114  unifiedMeanValue);
1115  }
1116  }
1117  else {
1118  queso_error_msg("parallel vectors not supported yet");
1119  }
1120 
1121  //m_env.fullComm().Barrier();
1122 
1123  return unifiedSamValue;
1124 }
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::vector< T > m_seq
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
T subSampleVarianceExtra(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
Finds the sample variance of the sub-sequence, considering numPos positions starting at position init...
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T >
const T & QUESO::ScalarSequence< T >::unifiedSampleVariancePlain ( bool  useOnlyInter0Comm) const

Finds the variance of a sample of the unified sequence of scalars.

Definition at line 453 of file ScalarSequence.C.

454 {
455  if (m_unifiedSampleVariancePlain == NULL) {
456  m_unifiedSampleVariancePlain = new T(0.);
458  }
459 
461 }
const T & unifiedMeanPlain(bool useOnlyInter0Comm) const
Finds the mean value of the unified sequence of scalars.
T unifiedSampleVarianceExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos, const T &unifiedMeanValue) const
Finds the sample variance of the unified sequence, considering numPos positions starting at position ...
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T>
T QUESO::ScalarSequence< T >::unifiedScaleForKde ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
const T &  unifiedIqrValue,
unsigned int  kdeDimension 
) const

Selects the scales (bandwidth) for the kernel density estimation, considering the unified sequence.

Definition at line 2248 of file ScalarSequence.C.

References queso_error_msg, queso_require_msg, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::SequenceOfVectors< V, M >::unifiedScalesForKde().

2253 {
2254  if (m_env.numSubEnvironments() == 1) {
2255  return this->subScaleForKde(initialPos,
2256  unifiedIqrValue,
2257  kdeDimension);
2258  }
2259 
2260  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
2261 
2262  T unifiedScaleValue = 0.;
2263  if (useOnlyInter0Comm) {
2264  if (m_env.inter0Rank() >= 0) {
2265  bool bRC = (initialPos < this->subSequenceSize());
2266  queso_require_msg(bRC, "invalid input data");
2267 
2268  unsigned int localDataSize = this->subSequenceSize() - initialPos;
2269 
2270  T unifiedMeanValue = this->unifiedMeanExtra(useOnlyInter0Comm,
2271  initialPos,
2272  localDataSize);
2273 
2274  T unifiedSamValue = this->unifiedSampleVarianceExtra(useOnlyInter0Comm,
2275  initialPos,
2276  localDataSize,
2277  unifiedMeanValue);
2278 
2279  unsigned int unifiedDataSize = 0;
2280  m_env.inter0Comm().Allreduce((void *) &localDataSize, (void *) &unifiedDataSize, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
2281  "ScalarSequence<T>::unifiedScaleForKde()",
2282  "failed MPI.Allreduce() for data size");
2283 
2284  if (unifiedIqrValue <= 0.) {
2285  unifiedScaleValue = 1.06*std::sqrt(unifiedSamValue)/std::pow(unifiedDataSize,1./(4. + ((double) kdeDimension)));
2286  }
2287  else {
2288  unifiedScaleValue = 1.06*std::min(std::sqrt(unifiedSamValue),unifiedIqrValue/1.34)/std::pow(unifiedDataSize,1./(4. + ((double) kdeDimension)));
2289  }
2290 
2291  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2292  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedScaleForKde()"
2293  << ": unifiedIqrValue = " << unifiedIqrValue
2294  << ", unifiedMeanValue = " << unifiedMeanValue
2295  << ", unifiedSamValue = " << unifiedSamValue
2296  << ", unifiedDataSize = " << unifiedDataSize
2297  << ", unifiedScaleValue = " << unifiedScaleValue
2298  << std::endl;
2299  }
2300  }
2301  else {
2302  // Node not in the 'inter0' communicator
2303  unifiedScaleValue = this->subScaleForKde(initialPos,
2304  unifiedIqrValue,
2305  kdeDimension);
2306  }
2307  }
2308  else {
2309  queso_error_msg("parallel vectors not supported yet");
2310  }
2311 
2312  //m_env.fullComm().Barrier();
2313 
2314  return unifiedScaleValue;
2315 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
T unifiedMeanExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos) const
Finds the mean value of the unified sequence of numPos positions starting at position initialPos...
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
T subScaleForKde(unsigned int initialPos, const T &iqrValue, unsigned int kdeDimension) const
Selects the scales (output value) for the kernel density estimation, considering only the sub-sequenc...
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
T unifiedSampleVarianceExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos, const T &unifiedMeanValue) const
Finds the sample variance of the unified sequence, considering numPos positions starting at position ...
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T >
unsigned int QUESO::ScalarSequence< T >::unifiedSequenceSize ( bool  useOnlyInter0Comm) const

Size of the unified sequence of scalars.

Definition at line 143 of file ScalarSequence.C.

References queso_error_msg, RawValue_MPI_SUM, and RawValue_MPI_UNSIGNED.

Referenced by QUESO::MLSampling< P_V, P_M >::checkpointML(), QUESO::MLSampling< P_V, P_M >::generateSequence_Step02_inter0(), QUESO::MLSampling< P_V, P_M >::generateSequence_Step03_inter0(), QUESO::MLSampling< P_V, P_M >::generateSequence_Step05_inter0(), and QUESO::MLSampling< P_V, P_M >::generateSequence_Step09_all().

144 {
145  if (m_env.numSubEnvironments() == 1) {
146  return this->subSequenceSize();
147  }
148 
149  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
150 
151  unsigned int unifiedNumSamples = 0;
152  if (useOnlyInter0Comm) {
153  if (m_env.inter0Rank() >= 0) {
154  unsigned int subNumSamples = this->subSequenceSize();
155  m_env.inter0Comm().Allreduce((void *) &subNumSamples, (void *) &unifiedNumSamples, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
156  "ScalarSequence<T>::unifiedSequenceSize()",
157  "failed MPI.Allreduce() for unifiedSequenceSize()");
158  }
159  else {
160  // Node not in the 'inter0' communicator
161  unifiedNumSamples = this->subSequenceSize();
162  }
163  }
164  else {
165  queso_error_msg("parallel vectors not supported yet");
166  }
167 
168  return unifiedNumSamples;
169 }
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T>
void QUESO::ScalarSequence< T >::unifiedSort ( bool  useOnlyInter0Comm,
unsigned int  initialPos,
ScalarSequence< T > &  unifiedSortedSequence 
) const

Sorts the unified sequence of scalars.

Definition at line 1900 of file ScalarSequence.C.

References queso_error_msg, queso_require_equal_to_msg, QUESO::ScalarSequence< T >::rawData(), RawValue_MPI_ANY_SOURCE, RawValue_MPI_DOUBLE, RawValue_MPI_SUM, RawValue_MPI_UNSIGNED, QUESO::ScalarSequence< T >::resizeSequence(), SCALAR_SEQUENCE_INIT_MPI_MSG, and QUESO::ScalarSequence< T >::subSequenceSize().

1904 {
1905  if (m_env.numSubEnvironments() == 1) {
1906  return this->subSort(initialPos,unifiedSortedSequence);
1907  }
1908 
1909  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
1910 
1911  if (useOnlyInter0Comm) {
1912  if (m_env.inter0Rank() >= 0) {
1913  //m_env.syncPrintDebugMsg("In ScalarSequence<T>::unifiedSort(), beginning logic",3,3000000,m_env.inter0Comm()); // Dangerous to barrier on inter0Comm ... // KAUST
1914 
1915  unsigned int localNumPos = this->subSequenceSize() - initialPos;
1916 
1917  std::vector<T> leafData(localNumPos,0.);
1918  this->extractRawData(0,
1919  1,
1920  localNumPos,
1921  leafData);
1922 
1923  if (m_env.inter0Rank() == 0) {
1924  int minus1NumTreeLevels = 0;
1925  int power2NumTreeNodes = 1;
1926 
1927  while (power2NumTreeNodes < m_env.inter0Comm().NumProc()) {
1928  power2NumTreeNodes += power2NumTreeNodes;
1929  minus1NumTreeLevels++;
1930  }
1931 
1932  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1933  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedSort()"
1934  << ": sorting tree has " << m_env.inter0Comm().NumProc()
1935  << " nodes and " << minus1NumTreeLevels+1
1936  << " levels"
1937  << std::endl;
1938  }
1939 
1940  this->parallelMerge(unifiedSortedSequence.rawData(),
1941  leafData,
1942  minus1NumTreeLevels);
1943  }
1944  else if (m_env.inter0Rank() > 0) { // KAUST
1945  unsigned int uintBuffer[1];
1946  RawType_MPI_Status status;
1948  "ScalarSequence<T>::unifiedSort()",
1949  "failed MPI.Recv() for init");
1950  //if (status) {}; // just to remove compiler warning
1951 
1952  unsigned int treeLevel = uintBuffer[0];
1953  this->parallelMerge(unifiedSortedSequence.rawData(),
1954  leafData,
1955  treeLevel);
1956  }
1957 
1958  //m_env.syncPrintDebugMsg("In ScalarSequence<T>::unifiedSort(), returned from parallelMerge()",3,3000000,m_env.inter0Comm()); // Dangerous to barrier on inter0Comm ... // KAUST
1959 
1960  // Broadcast
1961  unsigned int unifiedDataSize = unifiedSortedSequence.subSequenceSize();
1962  m_env.inter0Comm().Bcast((void *) &unifiedDataSize, (int) 1, RawValue_MPI_UNSIGNED, 0,
1963  "ScalarSequence<T>::unifiedSort()",
1964  "failed MPI.Bcast() for unified data size");
1965 
1966  unsigned int sumOfNumPos = 0;
1967  m_env.inter0Comm().Allreduce((void *) &localNumPos, (void *) &sumOfNumPos, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
1968  "ScalarSequence<T>::unifiedSort()",
1969  "failed MPI.Allreduce() for data size");
1970 
1971  queso_require_equal_to_msg(sumOfNumPos, unifiedDataSize, "incompatible unified sizes");
1972 
1973  unifiedSortedSequence.resizeSequence(unifiedDataSize);
1974  m_env.inter0Comm().Bcast((void *) &unifiedSortedSequence.rawData()[0], (int) unifiedDataSize, RawValue_MPI_DOUBLE, 0,
1975  "ScalarSequence<T>::unifiedSort()",
1976  "failed MPI.Bcast() for unified data");
1977 
1978  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
1979  *m_env.subDisplayFile() << "In ScalarSequence<T>::parallelMerge()"
1980  << ": tree node " << m_env.inter0Rank()
1981  << ", unifiedSortedSequence[0] = " << unifiedSortedSequence[0]
1982  << ", unifiedSortedSequence[" << unifiedSortedSequence.subSequenceSize()-1 << "] = " << unifiedSortedSequence[unifiedSortedSequence.subSequenceSize()-1]
1983  << std::endl;
1984  }
1985 
1986  //m_env.syncPrintDebugMsg("In ScalarSequence<T>::unifiedSort(), ending logic",3,3000000,m_env.inter0Comm()); // Dangerous to barrier on inter0Comm ... // KAUST
1987  }
1988  else {
1989  // Node not in the 'inter0' communicator
1990  this->subSort(initialPos,unifiedSortedSequence);
1991  }
1992  }
1993  else {
1994  queso_error_msg("parallel vectors not supported yet");
1995  }
1996 
1997  //m_env.fullComm().Barrier();
1998 
1999  return;
2000 }
#define SCALAR_SEQUENCE_INIT_MPI_MSG
unsigned int displayVerbosity() const
Definition: Environment.C:396
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const BaseEnvironment & m_env
void parallelMerge(std::vector< T > &sortedBuffer, const std::vector< T > &leafData, unsigned int treeLevel) const
Sorts/merges data in parallel using MPI.
MPI_Status RawType_MPI_Status
Definition: MpiComm.h:42
#define RawValue_MPI_ANY_SOURCE
Definition: MpiComm.h:45
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
void Bcast(void *buffer, int count, RawType_MPI_Datatype datatype, int root, const char *whereMsg, const char *whatMsg) const
Broadcast values from the root process to the slave processes.
Definition: MpiComm.C:133
void extractRawData(unsigned int initialPos, unsigned int spacing, unsigned int numPos, std::vector< double > &rawData) const
Extracts the raw data.
void Recv(void *buf, int count, RawType_MPI_Datatype datatype, int source, int tag, RawType_MPI_Status *status, const char *whereMsg, const char *whatMsg) const
Blocking receive of data from this process to another process.
Definition: MpiComm.C:175
void subSort()
Sorts the sequence of scalars in the private attribute m_seq.
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
Definition: MpiComm.C:113
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
template<class T>
void QUESO::ScalarSequence< T >::unifiedUniformlySampledCdf ( bool  useOnlyInter0Comm,
unsigned int  numIntervals,
T &  unifiedMinDomainValue,
T &  unifiedMaxDomainValue,
std::vector< T > &  unifiedCdfValues 
) const

Uniformly samples from the CDF from the unified sequence.

Definition at line 611 of file ScalarSequence.C.

References queso_error_msg.

617 {
618  if (m_env.numSubEnvironments() == 1) {
619  return this->subUniformlySampledCdf(numEvaluationPoints,
620  unifiedMinDomainValue,
621  unifiedMaxDomainValue,
622  unifiedCdfValues);
623  }
624 
625  // KAUST2
626  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
627 
628  if (useOnlyInter0Comm) {
629  if (m_env.inter0Rank() >= 0) {
630  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
631  *m_env.subDisplayFile() << "Entering ScalarSequence<T>::unifiedUniformlySampledCdf()"
632  << std::endl;
633  }
634 
635  T unifiedTmpMinValue;
636  T unifiedTmpMaxValue;
637  std::vector<T> unifiedCenters(numEvaluationPoints,0.);
638  std::vector<unsigned int> unifiedBins (numEvaluationPoints,0);
639 
640  this->unifiedMinMaxExtra(useOnlyInter0Comm,
641  0, // initialPos
642  this->subSequenceSize(),
643  unifiedTmpMinValue,
644  unifiedTmpMaxValue);
645  this->unifiedHistogram(useOnlyInter0Comm,
646  0, // initialPos
647  unifiedTmpMinValue,
648  unifiedTmpMaxValue,
649  unifiedCenters,
650  unifiedBins);
651 
652  unifiedMinDomainValue = unifiedCenters[0];
653  unifiedMaxDomainValue = unifiedCenters[unifiedCenters.size()-1];
654 
655  unsigned int unifiedTotalSumOfBins = 0;
656  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
657  unifiedTotalSumOfBins += unifiedBins[i];
658  }
659 
660  std::vector<unsigned int> unifiedPartialSumsOfBins(numEvaluationPoints,0);
661  unifiedPartialSumsOfBins[0] = unifiedBins[0];
662  for (unsigned int i = 1; i < numEvaluationPoints; ++i) { // Yes, from '1'
663  unifiedPartialSumsOfBins[i] = unifiedPartialSumsOfBins[i-1] + unifiedBins[i];
664  }
665 
666  unifiedCdfValues.clear();
667  unifiedCdfValues.resize(numEvaluationPoints);
668  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
669  unifiedCdfValues[i] = ((T) unifiedPartialSumsOfBins[i])/((T) unifiedTotalSumOfBins);
670  }
671 
672  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
673  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
674  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedUniformlySampledCdf()"
675  << ": i = " << i
676  << ", unifiedTmpMinValue = " << unifiedTmpMinValue
677  << ", unifiedTmpMaxValue = " << unifiedTmpMaxValue
678  << ", unifiedBins = " << unifiedBins[i]
679  << ", unifiedCdfValue = " << unifiedCdfValues[i]
680  << ", unifiedPartialSumsOfBins = " << unifiedPartialSumsOfBins[i]
681  << ", unifiedTotalSumOfBins = " << unifiedTotalSumOfBins
682  << std::endl;
683  }
684  }
685 
686  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
687  *m_env.subDisplayFile() << "Leaving ScalarSequence<T>::unifiedUniformlySampledCdf()"
688  << std::endl;
689  }
690  }
691  else {
692  // Node not in the 'inter0' communicator
693  this->subUniformlySampledCdf(numEvaluationPoints,
694  unifiedMinDomainValue,
695  unifiedMaxDomainValue,
696  unifiedCdfValues);
697  }
698  }
699  else {
700  queso_error_msg("parallel vectors not supported yet");
701  }
702 
703  //m_env.fullComm().Barrier();
704 
705  return;
706 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
void subUniformlySampledCdf(unsigned int numIntervals, T &minDomainValue, T &maxDomainValue, std::vector< T > &cdfValues) const
Uniformly samples from the CDF from the sub-sequence.
const BaseEnvironment & m_env
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
void unifiedMinMaxExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, T &unifiedMinValue, T &unifiedMaxValue) const
Finds the minimum and the maximum values of the unified sequence, considering numPos positions starti...
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
void unifiedHistogram(bool useOnlyInter0Comm, unsigned int initialPos, const T &unifiedMinHorizontalValue, const T &unifiedMaxHorizontalValue, std::vector< T > &unifiedCenters, std::vector< unsigned int > &unifiedBins) const
Calculates the histogram of the unified sequence.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
template<class T >
void QUESO::ScalarSequence< T >::unifiedWriteContents ( const std::string &  fileName,
const std::string &  fileType 
) const

Writes the unified sequence to a file.

Writes the unified sequence in Matlab/Octave format or, if enabled, in HDF5 format.

Definition at line 2632 of file ScalarSequence.C.

References QUESO::MiscGetEllapsedSeconds(), QUESO::FilePtrSetStruct::ofsVar, queso_error_msg, UQ_FILE_EXTENSION_FOR_HDF_FORMAT, UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT, and QUESO::UQ_OK_RC.

Referenced by QUESO::MLSampling< P_V, P_M >::checkpointML(), QUESO::MLSampling< P_V, P_M >::generateSequence(), QUESO::MetropolisHastingsSG< P_V, P_M >::generateSequence(), QUESO::MLSampling< P_V, P_M >::generateSequence_Level0_all(), and QUESO::MLSampling< P_V, P_M >::generateSequence_Step11_inter0().

2635 {
2636  std::string fileType(inputFileType);
2637 #ifdef QUESO_HAS_HDF5
2638  // Do nothing
2639 #else
2640  if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2641  if (m_env.subDisplayFile()) {
2642  *m_env.subDisplayFile() << "WARNING in ScalarSequence<T>::unifiedWriteContents()"
2643  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2644  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
2645  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2646  << "' instead..."
2647  << std::endl;
2648  }
2649  if (m_env.subRank() == 0) {
2650  std::cerr << "WARNING in ScalarSequence<T>::unifiedWriteContents()"
2651  << ": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2652  << "' has been requested, but this QUESO library has not been built with 'hdf5'"
2653  << ". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2654  << "' instead..."
2655  << std::endl;
2656  }
2658  }
2659 #endif
2660 
2661  // All processors in 'fullComm' should call this routine...
2662 
2663  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
2664  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
2665  *m_env.subDisplayFile() << "Entering ScalarSequence<T>::unifiedWriteContents()"
2666  << ": worldRank " << m_env.worldRank()
2667  << ", subEnvironment " << m_env.subId()
2668  << ", subRank " << m_env.subRank()
2669  << ", inter0Rank " << m_env.inter0Rank()
2670  //<< ", m_env.inter0Comm().NumProc() = " << m_env.inter0Comm().NumProc()
2671  << ", fileName = " << fileName
2672  << ", fileType = " << fileType
2673  << std::endl;
2674  }
2675 
2676  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
2677 
2678  if (m_env.inter0Rank() >= 0) {
2679  for (unsigned int r = 0; r < (unsigned int) m_env.inter0Comm().NumProc(); ++r) {
2680  if (m_env.inter0Rank() == (int) r) {
2681  // My turn
2682  FilePtrSetStruct unifiedFilePtrSet;
2683  // bool writeOver = (r == 0);
2684  bool writeOver = false; // A 'true' causes problems when the user chooses (via options
2685  // in the input file) to use just one file for all outputs.
2686  //std::cout << "\n In ScalarSequence<T>::unifiedWriteContents(), pos 000 \n" << std::endl;
2687  if (m_env.openUnifiedOutputFile(fileName,
2688  fileType, // "m or hdf"
2689  writeOver,
2690  unifiedFilePtrSet)) {
2691  //std::cout << "\n In ScalarSequence<T>::unifiedWriteContents(), pos 001 \n" << std::endl;
2692 
2693  unsigned int chainSize = this->subSequenceSize();
2694  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2695  if (r == 0) {
2696  *unifiedFilePtrSet.ofsVar << m_name << "_unified" << " = zeros(" << this->subSequenceSize()*m_env.inter0Comm().NumProc()
2697  << "," << 1
2698  << ");"
2699  << std::endl;
2700  *unifiedFilePtrSet.ofsVar << m_name << "_unified" << " = [";
2701  }
2702 
2703  for (unsigned int j = 0; j < chainSize; ++j) {
2704  *unifiedFilePtrSet.ofsVar << m_seq[j]
2705  << std::endl;
2706  }
2707 
2708  m_env.closeFile(unifiedFilePtrSet,fileType);
2709  }
2710 #ifdef QUESO_HAS_HDF5
2711  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2712  unsigned int numParams = 1; // m_vectorSpace.dimLocal();
2713  if (r == 0) {
2714  hid_t datatype = H5Tcopy(H5T_NATIVE_DOUBLE);
2715  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data type created" << std::endl;
2716  hsize_t dimsf[2];
2717  dimsf[0] = numParams;
2718  dimsf[1] = chainSize;
2719  hid_t dataspace = H5Screate_simple(2, dimsf, NULL); // HDF5_rank = 2
2720  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data space created" << std::endl;
2721  hid_t dataset = H5Dcreate2(unifiedFilePtrSet.h5Var,
2722  "seq_of_vectors",
2723  datatype,
2724  dataspace,
2725  H5P_DEFAULT, // Link creation property list
2726  H5P_DEFAULT, // Dataset creation property list
2727  H5P_DEFAULT); // Dataset access property list
2728  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data set created" << std::endl;
2729 
2730  struct timeval timevalBegin;
2731  int iRC = UQ_OK_RC;
2732  iRC = gettimeofday(&timevalBegin,NULL);
2733  if (iRC) {}; // just to remove compiler warning
2734 
2735  //double* dataOut[numParams]; // avoid compiler warning
2736  std::vector<double*> dataOut((size_t) numParams,NULL);
2737  dataOut[0] = (double*) malloc(numParams*chainSize*sizeof(double));
2738  for (unsigned int i = 1; i < numParams; ++i) { // Yes, from '1'
2739  dataOut[i] = dataOut[i-1] + chainSize; // Yes, just 'chainSize', not 'chainSize*sizeof(double)'
2740  }
2741  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, memory allocated" << std::endl;
2742  for (unsigned int j = 0; j < chainSize; ++j) {
2743  T tmpScalar = m_seq[j];
2744  for (unsigned int i = 0; i < numParams; ++i) {
2745  dataOut[i][j] = tmpScalar;
2746  }
2747  }
2748  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, memory filled" << std::endl;
2749 
2750  herr_t status;
2751  //std::cout << "\n In ScalarSequence<T>::unifiedWriteContents(), pos 002 \n" << std::endl;
2752  status = H5Dwrite(dataset,
2753  H5T_NATIVE_DOUBLE,
2754  H5S_ALL,
2755  H5S_ALL,
2756  H5P_DEFAULT,
2757  (void*) dataOut[0]);
2758  if (status) {}; // just to remove compiler warning
2759 
2760  //std::cout << "\n In ScalarSequence<T>::unifiedWriteContents(), pos 003 \n" << std::endl;
2761  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data written" << std::endl;
2762 
2763  double writeTime = MiscGetEllapsedSeconds(&timevalBegin);
2764  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
2765  *m_env.subDisplayFile() << "In ScalarSequence<T>::unifiedWriteContents()"
2766  << ": worldRank " << m_env.worldRank()
2767  << ", fullRank " << m_env.fullRank()
2768  << ", subEnvironment " << m_env.subId()
2769  << ", subRank " << m_env.subRank()
2770  << ", inter0Rank " << m_env.inter0Rank()
2771  << ", fileName = " << fileName
2772  << ", numParams = " << numParams
2773  << ", chainSize = " << chainSize
2774  << ", writeTime = " << writeTime << " seconds"
2775  << std::endl;
2776  }
2777 
2778  H5Dclose(dataset);
2779  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data set closed" << std::endl;
2780  H5Sclose(dataspace);
2781  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data space closed" << std::endl;
2782  H5Tclose(datatype);
2783  //std::cout << "In ScalarSequence<T>::unifiedWriteContents(): h5 case, data type closed" << std::endl;
2784  //free(dataOut[0]); // related to the changes above for compiler warning
2785  for (unsigned int tmpIndex = 0; tmpIndex < dataOut.size(); tmpIndex++) {
2786  free (dataOut[tmpIndex]);
2787  }
2788  }
2789  else {
2790  queso_error_msg("hdf file type not supported for multiple sub-environments yet");
2791  }
2792  }
2793 #endif
2794  } // if (m_env.openUnifiedOutputFile())
2795  //std::cout << "\n In ScalarSequence<T>::unifiedWriteContents(), pos 004 \n" << std::endl;
2796  } // if (m_env.inter0Rank() == (int) r)
2797  m_env.inter0Comm().Barrier();
2798  } // for r
2799 
2800  if (m_env.inter0Rank() == 0) {
2801  if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2802  FilePtrSetStruct unifiedFilePtrSet;
2803  if (m_env.openUnifiedOutputFile(fileName,
2804  fileType,
2805  false, // Yes, 'writeOver = false' in order to close the array for matlab
2806  unifiedFilePtrSet)) {
2807  *unifiedFilePtrSet.ofsVar << "];\n";
2808  m_env.closeFile(unifiedFilePtrSet,fileType);
2809  }
2810  }
2811  else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2812  // Do nothing
2813  }
2814  else {
2815  queso_error_msg("invalid file type");
2816  }
2817  }
2818  } // if (m_env.inter0Rank() >= 0)
2819 
2820  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
2821  *m_env.subDisplayFile() << "Leaving ScalarSequence<T>::unifiedWriteContents()"
2822  << ", fileName = " << fileName
2823  << ", fileType = " << fileType
2824  << std::endl;
2825  }
2826  //m_env.fullComm().Barrier(); // Dangerous to barrier on fullComm ...
2827 
2828  return;
2829 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
const BaseEnvironment & m_env
int subRank() const
Access function for sub-rank.
Definition: Environment.C:241
#define UQ_FILE_EXTENSION_FOR_HDF_FORMAT
Definition: Defines.h:90
void Barrier() const
Pause every process in *this communicator until all the processes reach this point.
Definition: MpiComm.C:122
int NumProc() const
Returns total number of processes.
Definition: MpiComm.C:103
int fullRank() const
Returns the process full rank.
Definition: Environment.C:222
#define queso_error_msg(msg)
Definition: asserts.h:47
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
bool openUnifiedOutputFile(const std::string &fileName, const std::string &fileType, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens a unified output file, that will contain data from all sub-environments.
Definition: Environment.C:666
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
Definition: Environment.C:1020
double MiscGetEllapsedSeconds(struct timeval *timeval0)
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
std::vector< T > m_seq
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
Definition: Defines.h:89
unsigned int subId() const
Access function to the number of each sub-environment Id: m_subId.
Definition: Environment.C:295
const int UQ_OK_RC
Definition: Defines.h:76
const MpiComm & inter0Comm() const
Access function for MpiComm inter0-communicator.
Definition: Environment.C:267
int worldRank() const
Returns the process world rank.
Definition: Environment.C:216
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.

Member Data Documentation

template<class T = double>
const BaseEnvironment& QUESO::ScalarSequence< T >::m_env
private

Definition at line 516 of file ScalarSequence.h.

template<class T = double>
std::string QUESO::ScalarSequence< T >::m_name
private

Definition at line 517 of file ScalarSequence.h.

Referenced by QUESO::ScalarSequence< T >::copy().

template<class T = double>
std::vector<T> QUESO::ScalarSequence< T >::m_seq
private
template<class T = double>
T* QUESO::ScalarSequence< T >::m_subMaxPlain
mutableprivate

Definition at line 522 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_subMeanPlain
mutableprivate

Definition at line 524 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_subMedianPlain
mutableprivate

Definition at line 526 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_subMinPlain
mutableprivate

Definition at line 520 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_subSampleVariancePlain
mutableprivate

Definition at line 528 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_unifiedMaxPlain
mutableprivate

Definition at line 523 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_unifiedMeanPlain
mutableprivate

Definition at line 525 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_unifiedMedianPlain
mutableprivate

Definition at line 527 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_unifiedMinPlain
mutableprivate

Definition at line 521 of file ScalarSequence.h.

template<class T = double>
T* QUESO::ScalarSequence< T >::m_unifiedSampleVariancePlain
mutableprivate

Definition at line 529 of file ScalarSequence.h.


The documentation for this class was generated from the following files:

Generated on Thu Jun 11 2015 13:52:34 for queso-0.53.0 by  doxygen 1.8.5