queso-0.53.0
ScalarSequence.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <algorithm>
26 #include <queso/ScalarSequence.h>
27 
28 namespace QUESO {
29 
30 // Default constructor -----------------------------
31 template <class T>
33  const BaseEnvironment& env,
34  unsigned int subSequenceSize,
35  const std::string& name)
36  :
37  m_env (env),
38  m_name (name),
39  m_seq (subSequenceSize,0.),
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),
48  m_subSampleVariancePlain (NULL),
49  m_unifiedSampleVariancePlain(NULL)
50 {
51 }
52 // Destructor ---------------------------------------
53 template <class T>
55 {
56  deleteStoredScalars();
57 }
58 // Set methods --------------------------------------
59 template <class T>
62 {
63  this->copy(rhs);
64  return *this;
65 }
66 // Access methods -----------------------------------
67 template <class T>
68 const T&
69 ScalarSequence<T>::operator[](unsigned int posId) const
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 }
81 //---------------------------------------------------
82 template <class T>
83 T&
84 ScalarSequence<T>::operator[](unsigned int posId)
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 
94  deleteStoredScalars();
95 
96  return m_seq[posId];
97 }
98 // Sequence methods ---------------------------------
99 template <class T>
100 const BaseEnvironment&
102 {
103  return m_env;
104 }
105 // --------------------------------------------------
106 template <class T>
107 const std::string&
109 {
110  return m_name;
111 }
112 // --------------------------------------------------
113 template <class T>
114 void
115 ScalarSequence<T>::setName(const std::string& newName)
116 {
117  m_name = newName;
118  return;
119 }
120 // --------------------------------------------------
121 template <class T>
122 void
124 {
125  unsigned int numPos = this->subSequenceSize();
126  if (numPos) {
127  this->resetValues(0,numPos);
128  this->resizeSequence(0);
129  }
130 
131  return;
132 }
133 // --------------------------------------------------
134 template <class T>
135 unsigned int
137 {
138  return m_seq.size();
139 }
140 // --------------------------------------------------
141 template <class T>
142 unsigned int
143 ScalarSequence<T>::unifiedSequenceSize(bool useOnlyInter0Comm) const
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 }
170 // --------------------------------------------------
171 template <class T>
172 void
173 ScalarSequence<T>::resizeSequence(unsigned int newSequenceSize)
174 {
175  if (newSequenceSize != this->subSequenceSize()) {
176  m_seq.resize(newSequenceSize,0.);
177  std::vector<T>(m_seq).swap(m_seq);
178  deleteStoredScalars();
179  }
180 
181  return;
182 }
183 // --------------------------------------------------
184 template <class T>
185 void
186 ScalarSequence<T>::resetValues(unsigned int initialPos, unsigned int numPos)
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 
199  deleteStoredScalars();
200 
201  return;
202 }
203 // --------------------------------------------------
204 template <class T>
205 void
206 ScalarSequence<T>::erasePositions(unsigned int initialPos, unsigned int numPos)
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 
228  deleteStoredScalars();
229 
230  return;
231 }
232 // --------------------------------------------------
233 template <class T>
234 void
236  bool useOnlyInter0Comm,
237  std::vector<T>& outputVec) const
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 }
338 // --------------------------------------------------
339 template <class T>
340 const T&
342 {
343  if (m_subMinPlain == NULL) {
344  m_subMinPlain = new T(0.);
345  if (m_subMaxPlain == NULL) m_subMaxPlain = new T(0.);
346  subMinMaxExtra(0,this->subSequenceSize(),*m_subMinPlain,*m_subMaxPlain);
347  }
348 
349  return *m_subMinPlain;
350 }
351 // --------------------------------------------------
352 template <class T>
353 const T&
354 ScalarSequence<T>::unifiedMinPlain(bool useOnlyInter0Comm) const
355 {
356  if (m_unifiedMinPlain == NULL) {
357  m_unifiedMinPlain = new T(0.);
358  if (m_unifiedMaxPlain == NULL) m_unifiedMaxPlain = new T(0.);
359  unifiedMinMaxExtra(useOnlyInter0Comm,0,this->subSequenceSize(),*m_unifiedMinPlain,*m_unifiedMaxPlain);
360  }
361 
362  return *m_unifiedMinPlain;
363 }
364 // --------------------------------------------------
365 template <class T>
366 const T&
368 {
369  if (m_subMaxPlain == NULL) {
370  if (m_subMinPlain == NULL) m_subMinPlain = new T(0.);
371  m_subMaxPlain = new T(0.);
372  subMinMaxExtra(0,this->subSequenceSize(),*m_subMinPlain,*m_subMaxPlain);
373  }
374 
375  return *m_subMaxPlain;
376 }
377 // --------------------------------------------------
378 template <class T>
379 const T&
380 ScalarSequence<T>::unifiedMaxPlain(bool useOnlyInter0Comm) const
381 {
382  if (m_unifiedMaxPlain == NULL) {
383  if (m_unifiedMinPlain == NULL) m_unifiedMinPlain = new T(0.);
384  m_unifiedMaxPlain = new T(0.);
385  unifiedMinMaxExtra(useOnlyInter0Comm,0,this->subSequenceSize(),*m_unifiedMinPlain,*m_unifiedMaxPlain);
386  }
387 
388  return *m_unifiedMaxPlain;
389 }
390 // --------------------------------------------------
391 template <class T>
392 const T&
394 {
395  if (m_subMeanPlain == NULL) {
396  m_subMeanPlain = new T(0.);
397  *m_subMeanPlain = subMeanExtra(0,subSequenceSize());
398  }
399 
400  return *m_subMeanPlain;
401 }
402 // --------------------------------------------------
403 template <class T>
404 const T&
405 ScalarSequence<T>::unifiedMeanPlain(bool useOnlyInter0Comm) const
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 }
414 // --------------------------------------------------
415 template <class T>
416 const T&
418 {
419  if (m_subMedianPlain == NULL) {
420  m_subMedianPlain = new T(0.);
421  *m_subMedianPlain = subMedianExtra(0,subSequenceSize());
422  }
423 
424  return *m_subMedianPlain;
425 }
426 // --------------------------------------------------
427 template <class T>
428 const T&
429 ScalarSequence<T>::unifiedMedianPlain(bool useOnlyInter0Comm) const
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 }
438 // --------------------------------------------------
439 template <class T>
440 const T&
442 {
443  if (m_subSampleVariancePlain == NULL) {
444  m_subSampleVariancePlain = new T(0.);
445  *m_subSampleVariancePlain = subSampleVarianceExtra(0,subSequenceSize(),subMeanPlain());
446  }
447 
448  return *m_subSampleVariancePlain;
449 }
450 // --------------------------------------------------
451 template <class T>
452 const T&
454 {
455  if (m_unifiedSampleVariancePlain == NULL) {
456  m_unifiedSampleVariancePlain = new T(0.);
457  *m_unifiedSampleVariancePlain = unifiedSampleVarianceExtra(useOnlyInter0Comm,0,subSequenceSize(),unifiedMeanPlain(useOnlyInter0Comm));
458  }
459 
460  return *m_unifiedSampleVariancePlain;
461 }
462 // --------------------------------------------------
463 template <class T>
464 void
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  }
499  if (m_subSampleVariancePlain) {
500  delete m_subSampleVariancePlain;
501  m_subSampleVariancePlain = NULL;
502  }
503  if (m_unifiedSampleVariancePlain) {
504  delete m_unifiedSampleVariancePlain;
505  m_unifiedSampleVariancePlain = NULL;
506  }
507 
508  return;
509 }
510 // --------------------------------------------------
511 template <class T>
512 void
513 ScalarSequence<T>::setGaussian(const T& meanValue, const T& stdDev)
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 
527  deleteStoredScalars();
528 
529  return;
530 }
531 // --------------------------------------------------
532 template <class T>
533 void
534 ScalarSequence<T>::setUniform(const T& a, const T& b)
535 {
536  unsigned int maxJ = this->subSequenceSize();
537  if (a == 0.) {
538  if (b == 1.) {
539  for (unsigned int j = 0; j < maxJ; ++j) {
540  m_seq[j] = m_env.rngObject()->uniformSample();
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 
562  deleteStoredScalars();
563 
564  return;
565 }
566 // --------------------------------------------------
567 template <class T>
568 void
570  unsigned int numEvaluationPoints,
571  T& minDomainValue,
572  T& maxDomainValue,
573  std::vector<T>& cdfValues) const
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 }
608 // --------------------------------------------------
609 template <class T>
610 void
612  bool useOnlyInter0Comm,
613  unsigned int numEvaluationPoints,
614  T& unifiedMinDomainValue,
615  T& unifiedMaxDomainValue,
616  std::vector<T>& unifiedCdfValues) const
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 }
707 // --------------------------------------------------
708 template <class T>
709 void
711  unsigned int numEvaluationPoints,
712  UniformOneDGrid<T>*& gridValues,
713  std::vector<T>& cdfValues) const
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 }
744 // --------------------------------------------------
745 template <class T>
746 void
748  unsigned int numEvaluationPoints,
749  std::vector<T>& gridValues,
750  std::vector<T>& cdfValues) const
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 }
796 // --------------------------------------------------
797 template <class T>
798 void
800  unsigned int numEvaluationPoints,
801  UniformOneDGrid<T>*& gridValues,
802  std::vector<T>& cdfValues) const
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 }
833 // --------------------------------------------------
834 template <class T>
835 T
837  unsigned int initialPos,
838  unsigned int numPos) const
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 }
871 // --------------------------------------------------
872 template <class T>
873 T
875  bool useOnlyInter0Comm,
876  unsigned int initialPos,
877  unsigned int numPos) const
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 }
949 // --------------------------------------------------
950 template <class T>
951 T
953  unsigned int initialPos,
954  unsigned int numPos) const
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 }
992 // --------------------------------------------------
993 template <class T>
994 T
996  bool useOnlyInter0Comm,
997  unsigned int initialPos,
998  unsigned int numPos) const
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 }
1039 // --------------------------------------------------
1040 template <class T>
1041 T
1043  unsigned int initialPos,
1044  unsigned int numPos,
1045  const T& meanValue) const
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 }
1066 // --------------------------------------------------
1067 template <class T>
1068 T
1070  bool useOnlyInter0Comm,
1071  unsigned int initialPos,
1072  unsigned int numPos,
1073  const T& unifiedMeanValue) const
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 }
1125 // --------------------------------------------------
1126 template <class T>
1127 T
1129  unsigned int initialPos,
1130  unsigned int numPos,
1131  const T& meanValue) const
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 }
1153 // --------------------------------------------------
1154 template <class T>
1155 T
1157  bool useOnlyInter0Comm,
1158  unsigned int initialPos,
1159  unsigned int numPos,
1160  const T& unifiedMeanValue) const
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 }
1213 // --------------------------------------------------
1214 template <class T>
1215 T
1217  unsigned int initialPos,
1218  unsigned int numPos,
1219  const T& meanValue) const
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 }
1240 // --------------------------------------------------
1241 template <class T>
1242 T
1244  bool useOnlyInter0Comm,
1245  unsigned int initialPos,
1246  unsigned int numPos,
1247  const T& unifiedMeanValue) const
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 }
1299 // --------------------------------------------------
1300 template <class T>
1301 T
1303  unsigned int initialPos,
1304  unsigned int numPos,
1305  const T& meanValue,
1306  unsigned int lag) const
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 }
1329 // --------------------------------------------------
1330 template <class T>
1331 T
1333  unsigned int initialPos,
1334  unsigned int numPos,
1335  unsigned int lag) const
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 }
1358 // --------------------------------------------------
1359 template <class T>
1360 void
1362  unsigned int initialPos,
1363  unsigned int numPos,
1364  unsigned int maxLag,
1365  std::vector<T>& autoCorrs) const
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 }
1438 // --------------------------------------------------
1439 template <class T>
1440 void
1442  unsigned int initialPos,
1443  unsigned int numPos,
1444  unsigned int numSum,
1445  T& autoCorrsSum) const
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 }
1507 // --------------------------------------------------
1508 template <class T>
1509 void
1511  unsigned int initialPos,
1512  unsigned int numPos,
1513  T& minValue,
1514  T& maxValue) const
1515 {
1516  queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(), "invalid input");
1517 
1518  seqScalarPositionConstIteratorTypedef pos1 = m_seq.begin();
1519  std::advance(pos1,initialPos);
1520 
1521  seqScalarPositionConstIteratorTypedef pos2 = m_seq.begin();
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 }
1536 // --------------------------------------------------
1537 template <class T>
1538 void
1540  bool useOnlyInter0Comm,
1541  unsigned int initialPos,
1542  unsigned int numPos,
1543  T& unifiedMinValue,
1544  T& unifiedMaxValue) const
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 }
1607 // --------------------------------------------------
1608 template <class T>
1609 void
1611  unsigned int initialPos,
1612  const T& minHorizontalValue,
1613  const T& maxHorizontalValue,
1614  std::vector<T>& centers,
1615  std::vector<unsigned int>& bins) const
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 }
1654 // --------------------------------------------------
1655 template <class T>
1656 void
1658  bool useOnlyInter0Comm,
1659  unsigned int initialPos,
1660  const T& unifiedMinHorizontalValue,
1661  const T& unifiedMaxHorizontalValue,
1662  std::vector<T>& unifiedCenters,
1663  std::vector<unsigned int>& unifiedBins) const
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 }
1744 // --------------------------------------------------
1745 template <class T>
1746 void
1748  unsigned int initialPos,
1749  const T& minHorizontalValue,
1750  const T& maxHorizontalValue,
1751  UniformOneDGrid<T>*& gridValues,
1752  std::vector<unsigned int>& bins) const
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 }
1786 // --------------------------------------------------
1787 template <class T>
1788 void
1790  unsigned int initialPos,
1791  const T& minHorizontalValue,
1792  const T& maxHorizontalValue,
1793  UniformOneDGrid<T>*& gridValues,
1794  std::vector<unsigned int>& bins) const
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 }
1828 // --------------------------------------------------
1829 template <class T>
1830 void
1832  unsigned int initialPos,
1833  const T& minHorizontalValue,
1834  const T& maxHorizontalValue,
1835  std::vector<T>& gridValues,
1836  std::vector<unsigned int>& bins) const
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 }
1880 // --------------------------------------------------
1881 template <class T>
1882 void
1884  unsigned int initialPos,
1885  ScalarSequence<T>& sortedSequence) const
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 }
1897 // --------------------------------------------------
1898 template <class T>
1899 void
1901  bool useOnlyInter0Comm,
1902  unsigned int initialPos,
1903  ScalarSequence<T>& unifiedSortedSequence) const
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;
1947  m_env.inter0Comm().Recv((void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, RawValue_MPI_ANY_SOURCE, SCALAR_SEQUENCE_INIT_MPI_MSG, &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 }
2001 // --------------------------------------------------
2002 template <class T>
2003 T
2004 ScalarSequence<T>::subInterQuantileRange(unsigned int initialPos) const
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 }
2114 // --------------------------------------------------
2115 template <class T>
2116 T
2118  bool useOnlyInter0Comm,
2119  unsigned int initialPos) const
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 }
2205 // --------------------------------------------------
2206 template <class T>
2207 T
2209  unsigned int initialPos,
2210  const T& iqrValue,
2211  unsigned int kdeDimension) const
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 }
2245 // --------------------------------------------------
2246 template <class T>
2247 T
2249  bool useOnlyInter0Comm,
2250  unsigned int initialPos,
2251  const T& unifiedIqrValue,
2252  unsigned int kdeDimension) const
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 }
2316 // --------------------------------------------------
2317 template <class T>
2318 void
2320  unsigned int initialPos,
2321  double scaleValue,
2322  const std::vector<T>& evaluationPositions,
2323  std::vector<double>& densityValues) const
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 }
2346 // --------------------------------------------------
2347 template <class T>
2348 void
2350  bool useOnlyInter0Comm,
2351  unsigned int initialPos,
2352  double unifiedScaleValue,
2353  const std::vector<T>& unifiedEvaluationPositions,
2354  std::vector<double>& unifiedDensityValues) const
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 }
2426 // --------------------------------------------------
2427 template <class T>
2428 void
2430  unsigned int initialPos,
2431  unsigned int spacing)
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 }
2465 // --------------------------------------------------
2466 template <class T>
2467 T
2469  bool useOnlyInter0Comm,
2470  unsigned int initialPos,
2471  unsigned int spacing) const
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 }
2494 // --------------------------------------------------
2495 template <class T>
2496 void
2498  const ScalarSequence<T>& src,
2499  unsigned int srcInitialPos,
2500  unsigned int srcNumPos)
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 
2506  deleteStoredScalars();
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 }
2515 // --------------------------------------------------
2516 template <class T>
2517 T
2519  const ScalarSequence<T>& subCorrespondingScalarValues,
2520  ScalarSequence<T>& subPositionsOfMaximum)
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 }
2545 // --------------------------------------------------
2546 template <class T>
2547 T
2549  const ScalarSequence<T>& subCorrespondingScalarValues,
2550  ScalarSequence<T>& unifiedPositionsOfMaximum)
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 }
2575 // --------------------------------------------------
2576 template <class T>
2577 void
2579  unsigned int initialPos,
2580  unsigned int numPos,
2581  const std::string& fileName,
2582  const std::string& fileType,
2583  const std::set<unsigned int>& allowedSubEnvIds) const
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 }
2603 // --------------------------------------------------
2604 template <class T>
2605 void
2607  unsigned int initialPos,
2608  unsigned int numPos,
2609  std::ofstream& ofs,
2610  const std::string& fileType) const // "m or hdf"
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 }
2629 // --------------------------------------------------
2630 template <class T>
2631 void
2633  const std::string& fileName,
2634  const std::string& inputFileType) const
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 }
2830 // --------------------------------------------------
2831 template <class T>
2832 void
2834  const std::string& fileName,
2835  const std::string& inputFileType,
2836  const unsigned int subReadSize)
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 }
3099 // Private methods-----------------------------------
3100 template <class T>
3101 void
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  }
3110  deleteStoredScalars();
3111 
3112  return;
3113 }
3114 // --------------------------------------------------
3115 template <class T>
3116 void
3118  unsigned int initialPos,
3119  unsigned int spacing,
3120  unsigned int numPos,
3121  ScalarSequence<T>& scalarSeq) const
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 }
3155 // --------------------------------------------------
3156 template <class T>
3157 void
3159  unsigned int initialPos,
3160  unsigned int spacing,
3161  unsigned int numPos,
3162  std::vector<double>& rawDataVec) const
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 }
3178 // --------------------------------------------------
3179 template <class T>
3180 std::vector<T>&
3182 {
3183  return m_seq;
3184 }
3185 
3186 // --------------------------------------------------
3187 template <class T>
3188 void
3190 {
3191  std::sort(m_seq.begin(), m_seq.end());
3192  return;
3193 }
3194 // --------------------------------------------------
3195 // Acknowledgement: the tree idea in this routine was taken from
3196 // 'http://penguin.ewu.edu/~trolfe/ParallelMerge/ParallelMerge.html', as of March 08, 2009
3197 template <class T>
3198 void
3200  std::vector<T>& sortedBuffer,
3201  const std::vector<T>& leafData,
3202  unsigned int currentTreeLevel) const
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 }
3321 
3322 // --------------------------------------------------
3323 // Methods conditionally available ------------------
3324 // --------------------------------------------------
3325 // --------------------------------------------------
3326 
3327 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
3328 template <class T>
3329 void
3331  unsigned int numEvaluationPoints,
3332  T& minDomainValue,
3333  T& maxDomainValue,
3334  std::vector<T>& mdfValues) const
3335 {
3336  T tmpMinValue;
3337  T tmpMaxValue;
3338  std::vector<T> centers(numEvaluationPoints,0.);
3339  std::vector<unsigned int> bins (numEvaluationPoints,0);
3340 
3341  subMinMaxExtra(0, // initialPos
3342  this->subSequenceSize(),
3343  tmpMinValue,
3344  tmpMaxValue);
3345  subHistogram(0, // initialPos,
3346  tmpMinValue,
3347  tmpMaxValue,
3348  centers,
3349  bins);
3350 
3351  minDomainValue = centers[0];
3352  maxDomainValue = centers[centers.size()-1];
3353  T binSize = (maxDomainValue - minDomainValue)/((double)(centers.size() - 1));
3354 
3355  unsigned int totalCount = 0;
3356  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
3357  totalCount += bins[i];
3358  }
3359 
3360  mdfValues.clear();
3361  mdfValues.resize(numEvaluationPoints);
3362  for (unsigned int i = 0; i < numEvaluationPoints; ++i) {
3363  mdfValues[i] = ((T) bins[i])/((T) totalCount)/binSize;
3364  }
3365 
3366  return;
3367 }
3368 // --------------------------------------------------
3369 template <class T>
3370 T
3371 ScalarSequence<T>::subMeanCltStd(
3372  unsigned int initialPos,
3373  unsigned int numPos,
3374  const T& meanValue) const
3375 {
3376  if (this->subSequenceSize() == 0) return 0.;
3377 
3378  bool bRC = ((initialPos < this->subSequenceSize()) &&
3379  (0 < numPos ) &&
3380  ((initialPos+numPos) <= this->subSequenceSize()));
3381  queso_require_msg(bRC, "invalid input data");
3382 
3383  unsigned int finalPosPlus1 = initialPos + numPos;
3384  T diff;
3385  T stdValue = 0.;
3386  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
3387  diff = m_seq[j] - meanValue;
3388  stdValue += diff*diff;
3389  }
3390 
3391  stdValue /= (((T) numPos) - 1.);
3392  stdValue /= (((T) numPos) - 1.);
3393  stdValue = sqrt(stdValue);
3394 
3395  return stdValue;
3396 }
3397 // --------------------------------------------------
3398 template <class T>
3399 T
3400 ScalarSequence<T>::unifiedMeanCltStd(
3401  bool useOnlyInter0Comm,
3402  unsigned int initialPos,
3403  unsigned int numPos,
3404  const T& unifiedMeanValue) const
3405 {
3406  if (m_env.numSubEnvironments() == 1) {
3407  return this->subMeanCltStd(initialPos,
3408  numPos,
3409  unifiedMeanValue);
3410  }
3411 
3412  // As of 14/Nov/2009, this routine does *not* require sub sequences to have equal size. Good.
3413 
3414  T unifiedStdValue = 0.;
3415  if (useOnlyInter0Comm) {
3416  if (m_env.inter0Rank() >= 0) {
3417  bool bRC = ((initialPos < this->subSequenceSize()) &&
3418  (0 < numPos ) &&
3419  ((initialPos+numPos) <= this->subSequenceSize()));
3420  queso_require_msg(bRC, "invalid input data");
3421 
3422  unsigned int finalPosPlus1 = initialPos + numPos;
3423  T diff;
3424  T localStdValue = 0.;
3425  for (unsigned int j = initialPos; j < finalPosPlus1; ++j) {
3426  diff = m_seq[j] - unifiedMeanValue;
3427  localStdValue += diff*diff;
3428  }
3429 
3430  unsigned int unifiedNumPos = 0;
3431  m_env.inter0Comm().Allreduce((void *) &numPos, (void *) &unifiedNumPos, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
3432  "ScalarSequence<T>::unifiedMeanCltStd()",
3433  "failed MPI.Allreduce() for numPos");
3434 
3435  m_env.inter0Comm().Allreduce((void *) &localStdValue, (void *) &unifiedStdValue, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
3436  "ScalarSequence<T>::unifiedMeanCltStd()",
3437  "failed MPI.Allreduce() for stdValue");
3438 
3439  unifiedStdValue /= (((T) unifiedNumPos) - 1.);
3440  unifiedStdValue /= (((T) unifiedNumPos) - 1.);
3441  unifiedStdValue /= sqrt(unifiedStdValue);
3442  }
3443  else {
3444  // Node not in the 'inter0' communicator
3445  this->subMeanCltStd(initialPos,
3446  numPos,
3447  unifiedMeanValue);
3448  }
3449  }
3450  else {
3451  queso_error_msg("parallel vectors not supported yet");
3452  }
3453  //m_env.fullComm().Barrier();
3454 
3455  return unifiedStdValue;
3456 }
3457 //---------------------------------------------------
3458 template <class T>
3459 T
3460 ScalarSequence<T>::bmm(
3461  unsigned int initialPos,
3462  unsigned int batchLength) const
3463 {
3464  bool bRC = ((initialPos < this->subSequenceSize() ) &&
3465  (batchLength < (this->subSequenceSize()-initialPos)));
3466  queso_require_msg(bRC, "invalid input data");
3467 
3468  unsigned int numberOfBatches = (this->subSequenceSize() - initialPos)/batchLength;
3469  ScalarSequence<T> batchMeans(m_env,numberOfBatches,"");
3470 
3471  for (unsigned int batchId = 0; batchId < numberOfBatches; batchId++) {
3472  batchMeans[batchId] = this->subMeanExtra(initialPos + batchId*batchLength,
3473  batchLength);
3474  }
3475 
3476  T meanOfBatchMeans = batchMeans.subMeanExtra(0,
3477  batchMeans.subSequenceSize());
3478 
3479  //T covLag0OfBatchMeans = batchMeans.autoCovariance(0,
3480  // batchMeans.subSequenceSize(),
3481  // meanOfBatchMeans,
3482  // 0); // lag
3483 
3484  T bmmValue = batchMeans.subSampleVarianceExtra(0,
3485  batchMeans.subSequenceSize(),
3486  meanOfBatchMeans);
3487 
3488  bmmValue /= (T) batchMeans.subSequenceSize(); // CHECK
3489 //bmmValue *= (T) (this->subSequenceSize() - initialPos); // CHECK
3490 
3491  return bmmValue;
3492 }
3493 //---------------------------------------------------
3494 template <class T>
3495 void
3496 ScalarSequence<T>::psd(
3497  unsigned int initialPos,
3498  unsigned int numBlocks,
3499  double hopSizeRatio,
3500  std::vector<double>& psdResult) const
3501 {
3502  bool bRC = ((initialPos < this->subSequenceSize() ) &&
3503  (hopSizeRatio != 0. ) &&
3504  (numBlocks < (this->subSequenceSize() - initialPos)) &&
3505  (fabs(hopSizeRatio) < (double) (this->subSequenceSize() - initialPos)));
3506  queso_require_msg(bRC, "invalid input data");
3507 
3508  unsigned int dataSize = this->subSequenceSize() - initialPos;
3509 
3510  T meanValue = this->subMeanExtra(initialPos,
3511  dataSize);
3512 
3513  // Determine hopSize and blockSize
3514  unsigned int hopSize = 0;
3515  unsigned int blockSize = 0;
3516  if (hopSizeRatio <= -1.) {
3517  double overlapSize = -hopSizeRatio;
3518  double tmp = ((double) (dataSize + (numBlocks - 1)*overlapSize))/((double) numBlocks);
3519  blockSize = (unsigned int) tmp;
3520  }
3521  else if (hopSizeRatio < 0.) {
3522  double tmp = ((double) dataSize)/(( ((double) numBlocks) - 1. )*(-hopSizeRatio) + 1.);
3523  blockSize = (unsigned int) tmp;
3524  hopSize = (unsigned int) ( ((double) blockSize) * (-hopSizeRatio) );
3525  }
3526  else if (hopSizeRatio == 0.) {
3527  queso_error_msg("hopSizeRatio == 0");
3528  }
3529  else if (hopSizeRatio < 1.) {
3530  double tmp = ((double) dataSize)/(( ((double) numBlocks) - 1. )*hopSizeRatio + 1.);
3531  blockSize = (unsigned int) tmp;
3532  hopSize = (unsigned int) ( ((double) blockSize) * hopSizeRatio );
3533  }
3534  else {
3535  hopSize = (unsigned int) hopSizeRatio;
3536  blockSize = dataSize - (numBlocks - 1)*hopSize;
3537  }
3538 
3539  int numberOfDiscardedDataElements = dataSize - (numBlocks-1)*hopSize - blockSize;
3540  //if (m_env.subDisplayFile()) {
3541  // *m_env.subDisplayFile() << "initialPos = " << initialPos
3542  // << ", N = " << dataSize
3543  // << ", #Blocks = " << numBlocks
3544  // << ", R (hop size) = " << hopSize
3545  // << ", B (block size) = " << blockSize
3546  // << ", overlap = " << blockSize - hopSize
3547  // << ", [(#Blocks - 1) * R + B] = " << (numBlocks-1)*hopSize + blockSize
3548  // << ", numberOfDiscardedDataElements = " << numberOfDiscardedDataElements
3549  // << std::endl;
3550  //}
3551  queso_require_greater_equal_msg(numberOfDiscardedDataElements, 0., "eventual extra space for last block should not be negative");
3552 
3553  double tmp = log((double) blockSize)/log(2.);
3554  double fractionalPart = tmp - ((double) ((unsigned int) tmp));
3555  if (fractionalPart > 0.) tmp += (1. - fractionalPart);
3556  unsigned int fftSize = (unsigned int) std::pow(2.,tmp);
3557  //if (m_env.subDisplayFile()) {
3558  // *m_env.subDisplayFile() << "fractionalPart = " << fractionalPart
3559  // << ", B = " << blockSize
3560  // << ", fftSize = " << fftSize
3561  // << std::endl;
3562  //}
3563 
3564  double modificationScale = 0.;
3565  for (unsigned int j = 0; j < blockSize; ++j) {
3566  double tmpValue = MiscHammingWindow(blockSize-1,j);
3567  modificationScale += tmpValue*tmpValue;
3568  }
3569  modificationScale = 1./modificationScale;
3570 
3571  std::vector<double> blockData(blockSize,0.);
3572  Fft<T> fftObj(m_env);
3573  std::vector<std::complex<double> > fftResult(0,std::complex<double>(0.,0.));
3574 
3575 #if 0
3576  unsigned int halfFFTSize = fftSize/2;
3577  psdResult.clear();
3578  psdResult.resize(1+halfFFTSize,0.);
3579  double factor = 1./M_PI/((double) numBlocks); // /((double) blockSize);
3580 #else
3581  psdResult.clear();
3582  psdResult.resize(fftSize,0.);
3583  double factor = 1./2./M_PI/((double) numBlocks); // /((double) blockSize);
3584 #endif
3585 
3586  for (unsigned int blockId = 0; blockId < numBlocks; blockId++) {
3587  // Fill block using Hamming window
3588  unsigned int initialDataPos = initialPos + blockId*hopSize;
3589  for (unsigned int j = 0; j < blockSize; ++j) {
3590  unsigned int dataPos = initialDataPos + j;
3591  queso_require_less_msg(dataPos, dataSize, "too large position to be accessed in data");
3592  blockData[j] = MiscHammingWindow(blockSize-1,j) * ( m_seq[dataPos] - meanValue ); // IMPORTANT
3593  }
3594 
3595  fftObj.forward(blockData,fftSize,fftResult);
3596 
3597  //if (m_env.subDisplayFile()) {
3598  // *m_env.subDisplayFile() << "blockData.size() = " << blockData.size()
3599  // << ", fftSize = " << fftSize
3600  // << ", fftResult.size() = " << fftResult.size()
3601  // << ", psdResult.size() = " << psdResult.size()
3602  // << std::endl;
3603  //}
3604  // Normalized spectral density: power per radians per sample
3605  for (unsigned int j = 0; j < psdResult.size(); ++j) {
3606  psdResult[j] += norm(fftResult[j])*modificationScale;
3607  }
3608  }
3609 
3610  for (unsigned int j = 0; j < psdResult.size(); ++j) {
3611  psdResult[j] *= factor;
3612  }
3613 
3614  return;
3615 }
3616 //---------------------------------------------------
3617 template <class T>
3618 T
3619 ScalarSequence<T>::geweke(
3620  unsigned int initialPos,
3621  double ratioNa,
3622  double ratioNb) const
3623 {
3624  double doubleFullDataSize = (double) (this->subSequenceSize() - initialPos);
3625  ScalarSequence<T> tmpSeq(m_env,0,"");
3626  std::vector<double> psdResult(0,0.);
3627 
3628  unsigned int dataSizeA = (unsigned int) (doubleFullDataSize * ratioNa);
3629  double doubleDataSizeA = (double) dataSizeA;
3630  unsigned int initialPosA = initialPos;
3631  this->extractScalarSeq(initialPosA,
3632  1,
3633  dataSizeA,
3634  tmpSeq);
3635  double meanA = tmpSeq.subMeanExtra(0,
3636  dataSizeA);
3637  tmpSeq.psd(0,
3638  (unsigned int) std::sqrt((double) dataSizeA), // numBlocks
3639  .5, // hopSizeRatio
3640  psdResult);
3641  double psdA = psdResult[0];
3642  double varOfMeanA = 2.*M_PI*psdA/doubleDataSizeA;
3643 
3644  unsigned int dataSizeB = (unsigned int) (doubleFullDataSize * ratioNb);
3645  double doubleDataSizeB = (double) dataSizeB;
3646  unsigned int initialPosB = this->subSequenceSize() - dataSizeB;
3647  this->extractScalarSeq(initialPosB,
3648  1,
3649  dataSizeB,
3650  tmpSeq);
3651  double meanB = tmpSeq.subMeanExtra(0,
3652  dataSizeB);
3653  tmpSeq.psd(0,
3654  (unsigned int) std::sqrt((double) dataSizeB), // numBlocks
3655  .5, // hopSizeRatio
3656  psdResult);
3657  double psdB = psdResult[0];
3658  double varOfMeanB = 2.*M_PI*psdB/doubleDataSizeB;
3659 
3660  if (m_env.subDisplayFile()) {
3661  *m_env.subDisplayFile() << "In ScalarSequence<T>::geweke()"
3662  << ", before computation of gewCoef"
3663  << ":\n"
3664  << ", dataSizeA = " << dataSizeA
3665  << ", numBlocksA = " << (unsigned int) std::sqrt((double) dataSizeA)
3666  << ", meanA = " << meanA
3667  << ", psdA = " << psdA
3668  << ", varOfMeanA = " << varOfMeanA
3669  << "\n"
3670  << ", dataSizeB = " << dataSizeB
3671  << ", numBlocksB = " << (unsigned int) std::sqrt((double) dataSizeB)
3672  << ", meanB = " << meanB
3673  << ", psdB = " << psdB
3674  << ", varOfMeanB = " << varOfMeanB
3675  << std::endl;
3676  }
3677  double gewCoef = (meanA - meanB)/std::sqrt(varOfMeanA + varOfMeanB);
3678 
3679  return gewCoef;
3680 }
3681 //---------------------------------------------------
3682 template <class T>
3683 T
3684 ScalarSequence<T>::meanStacc(
3685  unsigned int initialPos) const
3686 {
3687  queso_not_implemented(); // ERNESTO
3688 
3689  double value = 0.;
3690 
3691  return value;
3692 }
3693 //---------------------------------------------------
3694 template <class T>
3695 void
3696 ScalarSequence<T>::subCdfPercentageRange(
3697  unsigned int initialPos,
3698  unsigned int numPos,
3699  double range, // \in [0,1]
3700  T& lowerValue,
3701  T& upperValue) const
3702 {
3703  queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(), "invalid input");
3704 
3705  queso_require_msg(!((range < 0) || (range > 1.)), "invalid 'range' value");
3706 
3707  ScalarSequence<T> sortedSequence(m_env,0,"");;
3708  sortedSequence.resizeSequence(numPos);
3709  this->extractScalarSeq(initialPos,
3710  1,
3711  numPos,
3712  sortedSequence);
3713  sortedSequence.subSort();
3714 
3715  unsigned int lowerId = (unsigned int) round( 0.5*(1.-range)*((double) numPos) );
3716  lowerValue = sortedSequence[lowerId];
3717 
3718  unsigned int upperId = (unsigned int) round( 0.5*(1.+range)*((double) numPos) );
3719  if (upperId == numPos) {
3720  upperId = upperId-1;
3721  }
3722  queso_require_less_msg(upperId, numPos, "'upperId' got too big");
3723  upperValue = sortedSequence[upperId];
3724 
3725  return;
3726 }
3727 //---------------------------------------------------
3728 template <class T>
3729 void
3730 ScalarSequence<T>::unifiedCdfPercentageRange(
3731  bool useOnlyInter0Comm,
3732  unsigned int initialPos,
3733  unsigned int numPos,
3734  double range, // \in [0,1]
3735  T& unifiedLowerValue,
3736  T& unifiedUpperValue) const
3737 {
3738  if (m_env.numSubEnvironments() == 1) {
3739  return this->subCdfPercentageRange(initialPos,
3740  numPos,
3741  range,
3742  unifiedLowerValue,
3743  unifiedUpperValue);
3744  }
3745 
3746  // As of 07/Feb/2011, this routine does *not* require sub sequences to have equal size. Good.
3747 
3748  queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(), "invalid input");
3749 
3750  queso_require_msg(!((range < 0) || (range > 1.)), "invalid 'range' value");
3751 
3752  if (useOnlyInter0Comm) {
3753  if (m_env.inter0Rank() >= 0) {
3754  queso_error_msg("code not implemented yet");
3755  }
3756  else {
3757  // Node not in the 'inter0' communicator
3758  this->subCdfPercentageRange(initialPos,
3759  numPos,
3760  unifiedLowerValue,
3761  unifiedUpperValue);
3762  }
3763  }
3764  else {
3765  queso_error_msg("parallel vectors not supported yet");
3766  }
3767  //m_env.fullComm().Barrier();
3768 
3769  return;
3770 }
3771 //---------------------------------------------------
3772 template <class T>
3773 void
3774 ScalarSequence<T>::subCdfStacc(
3775  unsigned int initialPos,
3776  std::vector<double>& cdfStaccValues,
3777  std::vector<double>& cdfStaccValuesUp,
3778  std::vector<double>& cdfStaccValuesLow,
3779  const ScalarSequence<T>& sortedDataValues) const
3780 {
3781  queso_require_msg(!(false), "not implemented yet"); // Joseph
3782  bool bRC = (initialPos < this->subSequenceSize() );
3783  queso_require_msg(bRC, "invalid input data");
3784 
3785  unsigned int numPoints = subSequenceSize()-initialPos;
3786  double auxNumPoints = numPoints;
3787  double maxLamb = 0.;
3788  std::vector<double> ro (numPoints,0.);
3789  std::vector<double> Isam_mat(numPoints,0.);
3790 
3791  for (unsigned int pointId = 0; pointId < numPoints; pointId++) {
3792  double p = ( ((double) pointId) + 1.0 )/auxNumPoints;
3793  double ro0 = p*(1.0-p);
3794  cdfStaccValues[pointId] = p;
3795 
3796  //std::cout << "x-data" << data[pointId]
3797  // << std::endl;
3798 
3799  for (unsigned int k = 0; k < numPoints; k++) {
3800  if (m_seq[k] <= sortedDataValues[pointId]) {
3801  Isam_mat[k] = 1;
3802  }
3803  else {
3804  Isam_mat[k] = 0;
3805  }
3806  }
3807 
3808  for (unsigned int tau = 0; tau < (numPoints-1); tau++) {
3809  ro[tau] = 0.;
3810  for (unsigned int kk = 0; kk < numPoints-(tau+1); kk++) {
3811  ro[tau] += (Isam_mat[kk+tau+1]-p)*(Isam_mat[kk]-p);
3812  }
3813  ro[tau] *= 1.0/auxNumPoints;
3814  }
3815  double lamb = 0.;
3816  for (unsigned int tau = 0; tau < (numPoints-1); tau++) {
3817  double auxTau = tau;
3818  lamb += (1.-(auxTau+1.)/auxNumPoints)*ro[tau]/ro0;
3819  if (lamb > maxLamb) maxLamb = lamb;
3820  }
3821  lamb = 2.*maxLamb;
3822 
3823  //double ll=gsl_cdf_gaussian_Pinv(-0.05);
3824  cdfStaccValuesUp [pointId] = cdfStaccValues[pointId]+1.96*sqrt(ro0/auxNumPoints*(1.+lamb));
3825  cdfStaccValuesLow[pointId] = cdfStaccValues[pointId]-1.96*sqrt(ro0/auxNumPoints*(1.+lamb));
3826  if (cdfStaccValuesLow[pointId] < 0.) cdfStaccValuesLow[pointId] = 0.;
3827  if (cdfStaccValuesUp [pointId] > 1.) cdfStaccValuesUp [pointId] = 1.;
3828  //if (pointId==1 | pointId==100 | pointId==900) {
3829  // std::cout<<lamb<<ll<<std::endl;
3830  // std::cout<<lamb<<std::endl;
3831  //}
3832  }
3833 
3834 #if 0
3835  unsigned int dataSize = this->subSequenceSize() - initialPos;
3836  unsigned int numEvals = evaluationPositions.size();
3837 
3838  for (unsigned int j = 0; j < numEvals; ++j) {
3839  double x = evaluationPositions[j];
3840  double value = 0.;
3841  for (unsigned int k = 0; k < dataSize; ++k) {
3842  double xk = m_seq[initialPos+k];
3843  value += 0.;//MiscGaussianDensity((x-xk)*scaleInv,0.,1.);
3844  }
3845  cdfStaccValues[j] = value/(double) dataSize;
3846  }
3847 #endif
3848 
3849  return;
3850 }
3851 //---------------------------------------------------
3852 template <class T>
3853 void
3854 ScalarSequence<T>::subCdfStacc(
3855  unsigned int initialPos,
3856  const std::vector<T>& evaluationPositions,
3857  std::vector<double>& cdfStaccValues) const
3858 {
3860 
3861  bool bRC = ((initialPos < this->subSequenceSize() ) &&
3862  (0 < evaluationPositions.size()) &&
3863  (evaluationPositions.size() == cdfStaccValues.size() ));
3864  queso_require_msg(bRC, "invalid input data");
3865 
3866  // For Joseph:
3867  // Maybe sort first
3868  // For each of the evaluation positions:
3869  // --> 1) form temporary scalar seq that contains only 0 and 1
3870  // --> 2) call "temporary scalar seq"->meanStacc()
3871 
3872 #if 0
3873  unsigned int dataSize = this->subSequenceSize() - initialPos;
3874  unsigned int numEvals = evaluationPositions.size();
3875 
3876  for (unsigned int j = 0; j < numEvals; ++j) {
3877  //double x = evaluationPositions[j];
3878  double value = 0.;
3879  for (unsigned int k = 0; k < dataSize; ++k) {
3880  //double xk = m_seq[initialPos+k];
3881  value += 0.;//MiscGaussianDensity((x-xk)*scaleInv,0.,1.);
3882  }
3883  cdfStaccValues[j] = value/(double) dataSize;
3884  }
3885 #endif
3886 
3887  return;
3888 }
3889 #endif // #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
3890 
3891 // --------------------------------------------------
3892 // Additional methods -------------------------------
3893 // (outside class declaration) ----------------------
3894 //---------------------------------------------------
3895 template <class T>
3896 void
3898  const ScalarSequence<T>& scalarSeq2,
3899  unsigned int initialPos,
3900  double scaleValue1,
3901  double scaleValue2,
3902  const std::vector<T>& evaluationPositions1,
3903  const std::vector<T>& evaluationPositions2,
3904  std::vector<double>& densityValues)
3905 {
3906  queso_require_equal_to_msg(initialPos, 0, "not implemented yet for initialPos != 0");
3907 
3908  double covValue = 0.;
3909  double corrValue = 0.;
3911  scalarSeq2,
3912  scalarSeq1.subSequenceSize(),
3913  covValue,
3914  corrValue);
3915 
3916  bool bRC = ((initialPos < scalarSeq1.subSequenceSize()) &&
3917  (scalarSeq1.subSequenceSize() == scalarSeq2.subSequenceSize()) &&
3918  (0 < evaluationPositions1.size() ) &&
3919  (evaluationPositions1.size() == evaluationPositions2.size() ) &&
3920  (evaluationPositions1.size() == densityValues.size() ));
3921  queso_require_msg(bRC, "invalid input data");
3922 
3923  unsigned int dataSize = scalarSeq1.subSequenceSize() - initialPos;
3924  unsigned int numEvals = evaluationPositions1.size();
3925 
3926  double scale1Inv = 1./scaleValue1;
3927  double scale2Inv = 1./scaleValue2;
3928  //corrValue = 0.;
3929  double r = 1. - corrValue*corrValue;
3930  if (r <= 0.) { // prudencio 2010-07-23
3931  std::cerr << "In ComputeSubGaussian2dKde()"
3932  << ": WARNING, r = " << r
3933  << std::endl;
3934  }
3935 
3936  // scalarSeq1.env().worldRank(),
3937  // "ComputeSubGaussian2dKde()",
3938  // "negative r");
3939  for (unsigned int j = 0; j < numEvals; ++j) {
3940  double x1 = evaluationPositions1[j];
3941  double x2 = evaluationPositions2[j];
3942  double value = 0.;
3943  for (unsigned int k = 0; k < dataSize; ++k) {
3944  double d1k = scale1Inv*(x1 - scalarSeq1[initialPos+k]);
3945  double d2k = scale2Inv*(x2 - scalarSeq2[initialPos+k]);
3946  value += exp(-.5*( d1k*d1k + 2*corrValue*d1k*d2k + d2k*d2k )/r);
3947  }
3948  densityValues[j] = scale1Inv * scale2Inv * (value/(double) dataSize) / 2. / M_PI / sqrt(r);
3949  }
3950 
3951  return;
3952 }
3953 //---------------------------------------------------
3954 template <class T>
3955 void
3956 ComputeUnifiedGaussian2dKde(bool useOnlyInter0Comm, // INPUT
3957  const ScalarSequence<T>& scalarSeq1, // INPUT
3958  const ScalarSequence<T>& scalarSeq2, // INPUT
3959  unsigned int initialPos, // INPUT
3960  double unifiedScaleValue1, // INPUT
3961  double unifiedScaleValue2, // INPUT
3962  const std::vector<T>& unifiedEvaluationPositions1, // INPUT
3963  const std::vector<T>& unifiedEvaluationPositions2, // INPUT
3964  std::vector<double>& unifiedDensityValues) // OUTPUT
3965 {
3966  if (scalarSeq1.env().numSubEnvironments() == 1) {
3967  return ComputeSubGaussian2dKde(scalarSeq1,
3968  scalarSeq2,
3969  initialPos,
3970  unifiedScaleValue1,
3971  unifiedScaleValue2,
3972  unifiedEvaluationPositions1,
3973  unifiedEvaluationPositions2,
3974  unifiedDensityValues);
3975  }
3976 
3977  // As of 14/Nov/2009, this routine needs to be checked if it requires sub sequences to have equal size. Good.
3978 
3979  if (useOnlyInter0Comm) {
3980  if (scalarSeq1.env().inter0Rank() >= 0) {
3981  queso_error_msg("inter0 case not supported yet");
3982  }
3983  else {
3984  // Node not in the 'inter0' communicator
3985  ComputeSubGaussian2dKde(scalarSeq1,
3986  scalarSeq2,
3987  initialPos,
3988  unifiedScaleValue1,
3989  unifiedScaleValue2,
3990  unifiedEvaluationPositions1,
3991  unifiedEvaluationPositions2,
3992  unifiedDensityValues);
3993  }
3994  }
3995  else {
3996  queso_error_msg("parallel vectors not supported yet");
3997  }
3998 
3999  //scalarSeq1.env().fullComm().Barrier();
4000 
4001  return;
4002 }
4003 // --------------------------------------------------
4004 template <class T>
4005 void
4007  const ScalarSequence<T>& subPSeq,
4008  const ScalarSequence<T>& subQSeq,
4009  unsigned int subNumSamples,
4010  T& covValue,
4011  T& corrValue)
4012 {
4013  // Check input data consistency
4014  const BaseEnvironment& env = subPSeq.env();
4015 
4016  queso_require_msg(!((subNumSamples > subPSeq.subSequenceSize()) || (subNumSamples > subQSeq.subSequenceSize())), "subNumSamples is too large");
4017 
4018  // For both P and Q vector sequences: fill them
4019  T tmpP = 0.;
4020  T tmpQ = 0.;
4021 
4022  // For both P and Q vector sequences: compute the unified mean
4023  T unifiedMeanP = subPSeq.unifiedMeanExtra(true,0,subNumSamples);
4024  T unifiedMeanQ = subQSeq.unifiedMeanExtra(true,0,subNumSamples);
4025 
4026  // Compute "sub" covariance matrix
4027  covValue = 0.;
4028  for (unsigned k = 0; k < subNumSamples; ++k) {
4029  // For both P and Q vector sequences: get the difference (wrt the unified mean) in them
4030  tmpP = subPSeq[k] - unifiedMeanP;
4031  tmpQ = subQSeq[k] - unifiedMeanQ;
4032  covValue += tmpP*tmpQ;
4033  }
4034 
4035  // For both P and Q vector sequences: compute the unified variance
4036  T unifiedSampleVarianceP = subPSeq.unifiedSampleVarianceExtra(true,
4037  0,
4038  subNumSamples,
4039  unifiedMeanP);
4040 
4041  T unifiedSampleVarianceQ = subQSeq.unifiedSampleVarianceExtra(true,
4042  0,
4043  subNumSamples,
4044  unifiedMeanQ);
4045 
4046  // Compute unified covariance
4047  if (env.inter0Rank() >= 0) {
4048  unsigned int unifiedNumSamples = 0;
4049  env.inter0Comm().Allreduce((void *) &subNumSamples, (void *) &unifiedNumSamples, (int) 1, RawValue_MPI_UNSIGNED, RawValue_MPI_SUM,
4050  "ComputeCovCorrBetweenScalarSequences()",
4051  "failed MPI.Allreduce() for subNumSamples");
4052 
4053  double aux = 0.;
4054  env.inter0Comm().Allreduce((void *) &covValue, (void *) &aux, (int) 1, RawValue_MPI_DOUBLE, RawValue_MPI_SUM,
4055  "ComputeCovCorrBetweenScalarSequences()",
4056  "failed MPI.Allreduce() for a matrix position");
4057  covValue = aux/((double) (unifiedNumSamples-1)); // Yes, '-1' in order to compensate for the 'N-1' denominator factor in the calculations of sample variances above (whose square roots will be used below)
4058 
4059  corrValue = covValue/std::sqrt(unifiedSampleVarianceP)/std::sqrt(unifiedSampleVarianceQ);
4060 
4061  if ((corrValue < -1.) || (corrValue > 1.)) { // prudencio 2010-07-23
4062  std::cerr << "In ComputeCovCorrBetweenScalarSequences()"
4063  << ": computed correlation is out of range, corrValue = " << corrValue
4064  << std::endl;
4065  }
4066 
4067  // env.worldRank(),
4068  // "ComputeCovCorrBetweenScalarSequences()",
4069  // "computed correlation is out of range");
4070  }
4071  else {
4072  // Node not in the 'inter0' communicator: do nothing extra
4073  }
4074 
4075  return;
4076 }
4077 
4078 } // End namespace QUESO
4079 
4080 template class QUESO::ScalarSequence<double>;
void ComputeUnifiedGaussian2dKde(bool useOnlyInter0Comm, const ScalarSequence< T > &scalarSeq1, const ScalarSequence< T > &scalarSeq2, unsigned int initialPos, double unifiedScaleValue1, double unifiedScaleValue2, const std::vector< T > &unifiedEvaluationPositions1, const std::vector< T > &unifiedEvaluationPositions2, std::vector< double > &unifiedDensityValues)
#define SCALAR_SEQUENCE_INIT_MPI_MSG
T autoCorrViaDef(unsigned int initialPos, unsigned int numPos, unsigned int lag) const
Calculates the autocorrelation via definition.
void subUniformlySampledCdf(unsigned int numIntervals, T &minDomainValue, T &maxDomainValue, std::vector< T > &cdfValues) const
Uniformly samples from the CDF from the sub-sequence.
void copy(const ScalarSequence< T > &src)
Copies the scalar sequence src to this.
#define RawValue_MPI_SUM
Definition: MpiComm.h:52
const T & subMeanPlain() const
Finds the mean value of the sub-sequence of scalars.
#define RawValue_MPI_IN_PLACE
Definition: MpiComm.h:44
double MiscGaussianDensity(double x, double mu, double sigma)
void ComputeSubGaussian2dKde(const ScalarSequence< T > &scalarSeq1, const ScalarSequence< T > &scalarSeq2, unsigned int initialPos, double scaleValue1, double scaleValue2, const std::vector< T > &evaluationPositions1, const std::vector< T > &evaluationPositions2, std::vector< double > &densityValues)
void setName(const std::string &newName)
Sets a new name to the sequence of scalars.
const T & operator[](unsigned int posId) const
Access position posId of the sequence of scalars (const).
T subInterQuantileRange(unsigned int initialPos) const
Returns the interquartile range of the values in the sub-sequence.
const std::string & name() const
Access to the name of the sequence of scalars.
std::vector< T > & rawData()
The sequence of scalars. Access to private attribute m_seq.
istream * dataIn
Definition: ann_sample.cpp:58
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.
void clear()
Clears the sequence of scalars.
std::vector< T >::const_iterator seqScalarPositionConstIteratorTypedef
const T & unifiedMaxPlain(bool useOnlyInter0Comm) const
Finds the maximum value of the unified sequence of scalars.
ScalarSequence< T > & operator=(const ScalarSequence< T > &rhs)
Assignment operator; it copies rhs to this.
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...
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 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
T autoCovariance(unsigned int initialPos, unsigned int numPos, const T &meanValue, unsigned int lag) const
Calculates the autocovariance.
#define UQ_FILE_EXTENSION_FOR_HDF_FORMAT
Definition: Defines.h:90
const T & unifiedMeanPlain(bool useOnlyInter0Comm) const
Finds the mean value of the unified sequence of scalars.
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 resizeSequence(unsigned int newSequenceSize)
Resizes the size of the sequence of scalars.
void ComputeCovCorrBetweenScalarSequences(const ScalarSequence< T > &subPSeq, const ScalarSequence< T > &subQSeq, unsigned int subNumSamples, T &covValue, T &corrValue)
#define RawValue_MPI_ANY_SOURCE
Definition: MpiComm.h:45
double MiscHammingWindow(unsigned int N, unsigned int j)
const T & unifiedSampleVariancePlain(bool useOnlyInter0Comm) const
Finds the variance of a sample of the unified sequence of scalars.
const T & subMinPlain() const
Finds the minimum value of the sub-sequence of scalars.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
Definition: Environment.h:81
#define RawValue_MPI_MIN
Definition: MpiComm.h:50
Struct for handling data input and output from files.
Definition: Environment.h:72
T subPositionsOfMaximum(const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &subPositionsOfMaximum)
Finds the positions where the maximum element occurs in the sub-sequence.
void inverse(const std::vector< T > &data, unsigned int fftSize, std::vector< std::complex< double > > &result)
Calculates the inverse Fourier transform for real and complex data.
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 setGaussian(const T &mean, const T &stdDev)
Sets the values of the sequence as a Gaussian distribution of mean given by meanVec and standard devi...
#define queso_require_less_equal_msg(expr1, expr2, msg)
Definition: asserts.h:89
ScalarSequence(const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
Default constructor.
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 posit...
#define queso_error_msg(msg)
Definition: asserts.h:47
unsigned int unifiedSequenceSize(bool useOnlyInter0Comm) const
Size of the unified sequence of scalars.
T subMedianExtra(unsigned int initialPos, unsigned int numPos) const
Finds the median value of the sub-sequence, considering numPos positions starting at position initial...
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...
void append(const ScalarSequence< T > &src, unsigned int srcInitialPos, unsigned int srcNumPos)
Appends the scalar sequence src to this sequence.
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
Definition: Environment.C:288
void autoCorrViaFft(unsigned int initialPos, unsigned int numPos, unsigned int maxLag, std::vector< T > &autoCorrs) const
Calculates the autocorrelation via Fast Fourier transforms (FFT).
const T & subMaxPlain() const
Finds the maximum value of the sub-sequence of scalars.
int inter0Rank() const
Returns the process inter0 rank.
Definition: Environment.C:261
#define SCALAR_SEQUENCE_DATA_MPI_MSG
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...
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...
const T & unifiedMinPlain(bool useOnlyInter0Comm) const
Finds the minimum value of the unified sequence of scalars.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
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 an...
#define SCALAR_SEQUENCE_SIZE_MPI_MSG
T unifiedPositionsOfMaximum(const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &unifiedPositionsOfMaximum)
Finds the positions where the maximum element occurs in the unified sequence.
double MiscGetEllapsedSeconds(struct timeval *timeval0)
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
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...
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
T unifiedInterQuantileRange(bool useOnlyInter0Comm, unsigned int initialPos) const
Returns the interquartile range of the values in the unified sequence.
std::vector< T > m_seq
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.
void extractScalarSeq(unsigned int initialPos, unsigned int spacing, unsigned int numPos, ScalarSequence< T > &scalarSeq) const
Extracts a sequence of scalars.
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 starti...
void erasePositions(unsigned int initialPos, unsigned int numPos)
Erases numPos values of the sequence, starting at position initialPos.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
void unifiedWriteContents(const std::string &fileName, const std::string &fileType) const
Writes the unified sequence to a file.
void extractRawData(unsigned int initialPos, unsigned int spacing, unsigned int numPos, std::vector< double > &rawData) const
Extracts the raw data.
#define RawValue_MPI_INT
Definition: MpiComm.h:47
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.
#define UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
Definition: Defines.h:89
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 ...
Class for accommodating uniform one-dimensional grids.
#define queso_not_implemented()
Definition: asserts.h:56
void unifiedSort(bool useOnlyInter0Comm, unsigned int initialPos, ScalarSequence< T > &unifiedSortedSequence) const
Sorts the unified sequence of scalars.
void deleteStoredScalars()
Deletes all stored scalars.
const int UQ_OK_RC
Definition: Defines.h:76
#define RawValue_MPI_MAX
Definition: MpiComm.h:51
std::vector< T >::iterator seqScalarPositionIteratorTypedef
T brooksGelmanConvMeasure(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int spacing) const
Estimates convergence rate using Brooks &amp; Gelman method.
~ScalarSequence()
Destructor.
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
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.
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.
void subSort(unsigned int initialPos, ScalarSequence< T > &sortedSequence) const
Sorts the sub-sequence of scalars.
void forward(const std::vector< T > &data, unsigned int fftSize, std::vector< std::complex< double > > &result)
Calculates the forward Fourier transform (for real data. TODO: complex data).
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
std::ifstream * ifsVar
Provides a stream interface to read data from files.
Definition: Environment.h:84
const BaseEnvironment & env() const
Access to QUESO environment.
Class for a Fast Fourier Transform (FFT) algorithm.
Definition: Fft.h:66
const T & subSampleVariancePlain() const
Finds the variance of a sample of the sub-sequence of scalars.
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.
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.
void getUnifiedContentsAtProc0Only(bool useOnlyInter0Comm, std::vector< T > &outputVec) const
Gets the unified contents of processor of rank equals to 0.
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 ...
Class for handling scalar samples.
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.
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:48
const T & subMedianPlain() const
Finds the median value 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.
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...
and that you are informed that you can do these things To protect your we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it For if you distribute copies of the whether gratis or for a you must give the recipients all the rights that we gave you You must make sure that receive or can get the source code If you link other code with the you must provide complete object files to the so that they can relink them with the library after making changes to the library and recompiling it And you must show them these terms so they know their rights We protect your rights with a two step which gives you legal permission to copy
Definition: License.txt:60
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:49
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.
void filter(unsigned int initialPos, unsigned int spacing)
Filters positions in the sequence of vectors.
const T & unifiedMedianPlain(bool useOnlyInter0Comm) const
Finds the median value of the unified sequence of scalars.
int k
Definition: ann_sample.cpp:53
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...
void unifiedReadContents(const std::string &fileName, const std::string &fileType, const unsigned int subSequenceSize)
Reads the unified sequence from a file.

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