26 #include <queso/ScalarSequence.h>
34 unsigned int subSequenceSize,
35 const std::string& name)
39 m_seq (subSequenceSize,0.),
41 m_unifiedMinPlain (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)
56 deleteStoredScalars();
71 if (posId >= this->subSequenceSize()) {
72 std::cerr <<
"In ScalarSequence<T>::operator[]() const"
73 <<
": posId = " << posId
74 <<
", this->subSequenceSize() = " << this->subSequenceSize()
77 queso_require_less_msg(posId, this->subSequenceSize(),
"posId > subSequenceSize()");
86 if (posId >= this->subSequenceSize()) {
87 std::cerr <<
"In ScalarSequence<T>::operator[]()"
88 <<
": posId = " << posId
89 <<
", this->subSequenceSize() = " << this->subSequenceSize()
92 queso_require_less_msg(posId, this->subSequenceSize(),
"posId > subSequenceSize()");
94 deleteStoredScalars();
125 unsigned int numPos = this->subSequenceSize();
127 this->resetValues(0,numPos);
128 this->resizeSequence(0);
145 if (m_env.numSubEnvironments() == 1) {
146 return this->subSequenceSize();
151 unsigned int unifiedNumSamples = 0;
152 if (useOnlyInter0Comm) {
153 if (m_env.inter0Rank() >= 0) {
154 unsigned int subNumSamples = this->subSequenceSize();
155 m_env.inter0Comm().template Allreduce<unsigned int>(&subNumSamples, &unifiedNumSamples, (int) 1, RawValue_MPI_SUM,
156 "ScalarSequence<T>::unifiedSequenceSize()",
157 "failed MPI.Allreduce() for unifiedSequenceSize()");
161 unifiedNumSamples = this->subSequenceSize();
165 queso_error_msg(
"parallel vectors not supported yet");
168 return unifiedNumSamples;
175 if (newSequenceSize != this->subSequenceSize()) {
176 m_seq.resize(newSequenceSize,0.);
177 std::vector<T>(m_seq).swap(m_seq);
178 deleteStoredScalars();
188 if (this->subSequenceSize() == 0)
return;
190 bool bRC = ((initialPos < this->subSequenceSize()) &&
192 ((initialPos+numPos) <= this->subSequenceSize()));
193 queso_require_msg(bRC,
"invalid input data");
195 for (
unsigned int j = 0; j < numPos; ++j) {
196 m_seq[initialPos+j] = 0.;
199 deleteStoredScalars();
208 if (this->subSequenceSize() == 0)
return;
210 bool bRC = ((initialPos < this->subSequenceSize()) &&
212 ((initialPos+numPos) <= this->subSequenceSize()));
213 queso_require_msg(bRC,
"invalid input data");
216 if (initialPos < this->subSequenceSize()) std::advance(posIteratorBegin,initialPos);
217 else posIteratorBegin = m_seq.end();
219 unsigned int posEnd = initialPos + numPos;
221 if (posEnd < this->subSequenceSize()) std::advance(posIteratorEnd,posEnd);
222 else posIteratorEnd = m_seq.end();
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()");
228 deleteStoredScalars();
236 bool useOnlyInter0Comm,
237 std::vector<T>& outputVec)
const
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.);
254 std::vector<int> recvcnts(m_env.inter0Comm().NumProc(),0);
255 m_env.inter0Comm().template Gather<int>(&auxSubSize, 1, &recvcnts[0], (int) 1, 0,
256 "ScalarSequence<T>::getUnifiedContentsAtProc0Only()",
257 "failed MPI.Gather()");
258 if (m_env.inter0Rank() == 0) {
263 std::vector<int> displs(m_env.inter0Comm().NumProc(),0);
264 for (
unsigned int r = 1; r < (
unsigned int) m_env.inter0Comm().NumProc(); ++r) {
265 displs[r] = displs[r-1] + recvcnts[r-1];
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()
278 for (
unsigned int i = 0; i < m_seq.size(); ++i) {
279 *m_env.subDisplayFile() <<
" (before gatherv) m_seq[" << i <<
"]= " << m_seq[i]
285 m_env.inter0Comm().template Gatherv<double>(&m_seq[0], auxSubSize,
286 &outputVec[0], (
int *) &recvcnts[0], (
int *) &displs[0], 0,
287 "ScalarSequence<T>::getUnifiedContentsAtProc0Only()",
288 "failed MPI.Gatherv()");
290 #if 0 // for debug only
291 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
292 for (
unsigned int i = 0; i < m_seq.size(); ++i) {
293 *m_env.subDisplayFile() <<
" (after gatherv) m_seq[" << i <<
"]= " << m_seq[i]
296 for (
unsigned int i = 0; i < outputVec.size(); ++i) {
297 *m_env.subDisplayFile() <<
" (after gatherv) outputVec[" << i <<
"]= " << outputVec[i]
303 #if 0 // for debug only
304 if (m_env.inter0Rank() == 0) {
305 for (
unsigned int i = 0; i < auxSubSize; ++i) {
306 outputVec[i] = m_seq[i];
308 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,
309 "ScalarSequence<T>::getUnifiedContentsAtProc0Only(1)",
310 "failed MPI.Gatherv()");
313 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,
314 "ScalarSequence<T>::getUnifiedContentsAtProc0Only(2)",
315 "failed MPI.Gatherv()");
317 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
318 for (
unsigned int i = 0; i < m_seq.size(); ++i) {
319 *m_env.subDisplayFile() <<
" (after 2nd gatherv) m_seq[" << i <<
"]= " << m_seq[i]
322 for (
unsigned int i = 0; i < outputVec.size(); ++i) {
323 *m_env.subDisplayFile() <<
" (after 2nd gatherv) outputVec[" << i <<
"]= " << outputVec[i]
335 queso_error_msg(
"parallel vectors not supported yet");
345 if (m_subMinPlain == NULL) {
346 m_subMinPlain =
new T(0.);
347 if (m_subMaxPlain == NULL) m_subMaxPlain =
new T(0.);
348 subMinMaxExtra(0,this->subSequenceSize(),*m_subMinPlain,*m_subMaxPlain);
351 return *m_subMinPlain;
358 if (m_unifiedMinPlain == NULL) {
359 m_unifiedMinPlain =
new T(0.);
360 if (m_unifiedMaxPlain == NULL) m_unifiedMaxPlain =
new T(0.);
361 unifiedMinMaxExtra(useOnlyInter0Comm,0,this->subSequenceSize(),*m_unifiedMinPlain,*m_unifiedMaxPlain);
364 return *m_unifiedMinPlain;
371 if (m_subMaxPlain == NULL) {
372 if (m_subMinPlain == NULL) m_subMinPlain =
new T(0.);
373 m_subMaxPlain =
new T(0.);
374 subMinMaxExtra(0,this->subSequenceSize(),*m_subMinPlain,*m_subMaxPlain);
377 return *m_subMaxPlain;
384 if (m_unifiedMaxPlain == NULL) {
385 if (m_unifiedMinPlain == NULL) m_unifiedMinPlain =
new T(0.);
386 m_unifiedMaxPlain =
new T(0.);
387 unifiedMinMaxExtra(useOnlyInter0Comm,0,this->subSequenceSize(),*m_unifiedMinPlain,*m_unifiedMaxPlain);
390 return *m_unifiedMaxPlain;
397 if (m_subMeanPlain == NULL) {
398 m_subMeanPlain =
new T(0.);
399 *m_subMeanPlain = subMeanExtra(0,subSequenceSize());
402 return *m_subMeanPlain;
409 if (m_unifiedMeanPlain == NULL) {
410 m_unifiedMeanPlain =
new T(0.);
411 *m_unifiedMeanPlain = unifiedMeanExtra(useOnlyInter0Comm,0,subSequenceSize());
414 return *m_unifiedMeanPlain;
421 if (m_subMedianPlain == NULL) {
422 m_subMedianPlain =
new T(0.);
423 *m_subMedianPlain = subMedianExtra(0,subSequenceSize());
426 return *m_subMedianPlain;
433 if (m_unifiedMedianPlain == NULL) {
434 m_unifiedMedianPlain =
new T(0.);
435 *m_unifiedMedianPlain = unifiedMedianExtra(useOnlyInter0Comm,0,subSequenceSize());
438 return *m_unifiedMedianPlain;
445 if (m_subSampleVariancePlain == NULL) {
446 m_subSampleVariancePlain =
new T(0.);
447 *m_subSampleVariancePlain = subSampleVarianceExtra(0,subSequenceSize(),subMeanPlain());
450 return *m_subSampleVariancePlain;
457 if (m_unifiedSampleVariancePlain == NULL) {
458 m_unifiedSampleVariancePlain =
new T(0.);
459 *m_unifiedSampleVariancePlain = unifiedSampleVarianceExtra(useOnlyInter0Comm,0,subSequenceSize(),unifiedMeanPlain(useOnlyInter0Comm));
462 return *m_unifiedSampleVariancePlain;
470 delete m_subMinPlain;
471 m_subMinPlain = NULL;
473 if (m_unifiedMinPlain) {
474 delete m_unifiedMinPlain;
475 m_unifiedMinPlain = NULL;
478 delete m_subMaxPlain;
479 m_subMaxPlain = NULL;
481 if (m_unifiedMaxPlain) {
482 delete m_unifiedMaxPlain;
483 m_unifiedMaxPlain = NULL;
485 if (m_subMeanPlain) {
486 delete m_subMeanPlain;
487 m_subMeanPlain = NULL;
489 if (m_unifiedMeanPlain) {
490 delete m_unifiedMeanPlain;
491 m_unifiedMeanPlain = NULL;
493 if (m_subMedianPlain) {
494 delete m_subMedianPlain;
495 m_subMedianPlain = NULL;
497 if (m_unifiedMedianPlain) {
498 delete m_unifiedMedianPlain;
499 m_unifiedMedianPlain = NULL;
501 if (m_subSampleVariancePlain) {
502 delete m_subSampleVariancePlain;
503 m_subSampleVariancePlain = NULL;
505 if (m_unifiedSampleVariancePlain) {
506 delete m_unifiedSampleVariancePlain;
507 m_unifiedSampleVariancePlain = NULL;
517 unsigned int maxJ = this->subSequenceSize();
518 for (
unsigned int j = 0; j < maxJ; ++j) {
519 m_seq[j] = meanValue + m_env.rngObject()->gaussianSample(stdDev);
522 deleteStoredScalars();
529 unsigned int maxJ = this->subSequenceSize();
530 for (
unsigned int j = 0; j < maxJ; ++j) {
531 m_seq[j] = a + (b-a)*m_env.rngObject()->uniformSample();
534 deleteStoredScalars();
540 unsigned int numEvaluationPoints,
543 std::vector<T>& cdfValues)
const
547 std::vector<T> centers(numEvaluationPoints,0.);
548 std::vector<unsigned int> bins (numEvaluationPoints,0);
551 this->subSequenceSize(),
560 minDomainValue = centers[0];
561 maxDomainValue = centers[centers.size()-1];
563 unsigned int sumOfBins = 0;
564 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
565 sumOfBins += bins[i];
569 cdfValues.resize(numEvaluationPoints);
570 unsigned int partialSum = 0;
571 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
572 partialSum += bins[i];
573 cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
582 bool useOnlyInter0Comm,
583 unsigned int numEvaluationPoints,
584 T& unifiedMinDomainValue,
585 T& unifiedMaxDomainValue,
586 std::vector<T>& unifiedCdfValues)
const
588 if (m_env.numSubEnvironments() == 1) {
589 return this->subUniformlySampledCdf(numEvaluationPoints,
590 unifiedMinDomainValue,
591 unifiedMaxDomainValue,
598 if (useOnlyInter0Comm) {
599 if (m_env.inter0Rank() >= 0) {
600 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
601 *m_env.subDisplayFile() <<
"Entering ScalarSequence<T>::unifiedUniformlySampledCdf()"
605 T unifiedTmpMinValue;
606 T unifiedTmpMaxValue;
607 std::vector<T> unifiedCenters(numEvaluationPoints,0.);
608 std::vector<unsigned int> unifiedBins (numEvaluationPoints,0);
610 this->unifiedMinMaxExtra(useOnlyInter0Comm,
612 this->subSequenceSize(),
615 this->unifiedHistogram(useOnlyInter0Comm,
622 unifiedMinDomainValue = unifiedCenters[0];
623 unifiedMaxDomainValue = unifiedCenters[unifiedCenters.size()-1];
625 unsigned int unifiedTotalSumOfBins = 0;
626 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
627 unifiedTotalSumOfBins += unifiedBins[i];
630 std::vector<unsigned int> unifiedPartialSumsOfBins(numEvaluationPoints,0);
631 unifiedPartialSumsOfBins[0] = unifiedBins[0];
632 for (
unsigned int i = 1; i < numEvaluationPoints; ++i) {
633 unifiedPartialSumsOfBins[i] = unifiedPartialSumsOfBins[i-1] + unifiedBins[i];
636 unifiedCdfValues.clear();
637 unifiedCdfValues.resize(numEvaluationPoints);
638 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
639 unifiedCdfValues[i] = ((T) unifiedPartialSumsOfBins[i])/((T) unifiedTotalSumOfBins);
642 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
643 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
644 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedUniformlySampledCdf()"
646 <<
", unifiedTmpMinValue = " << unifiedTmpMinValue
647 <<
", unifiedTmpMaxValue = " << unifiedTmpMaxValue
648 <<
", unifiedBins = " << unifiedBins[i]
649 <<
", unifiedCdfValue = " << unifiedCdfValues[i]
650 <<
", unifiedPartialSumsOfBins = " << unifiedPartialSumsOfBins[i]
651 <<
", unifiedTotalSumOfBins = " << unifiedTotalSumOfBins
656 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
657 *m_env.subDisplayFile() <<
"Leaving ScalarSequence<T>::unifiedUniformlySampledCdf()"
663 this->subUniformlySampledCdf(numEvaluationPoints,
664 unifiedMinDomainValue,
665 unifiedMaxDomainValue,
670 queso_error_msg(
"parallel vectors not supported yet");
681 unsigned int numEvaluationPoints,
683 std::vector<T>& cdfValues)
const
687 std::vector<unsigned int> bins(numEvaluationPoints,0);
690 this->subSequenceSize(),
699 unsigned int sumOfBins = 0;
700 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
701 sumOfBins += bins[i];
705 cdfValues.resize(numEvaluationPoints);
706 unsigned int partialSum = 0;
707 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
708 partialSum += bins[i];
709 cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
718 unsigned int numEvaluationPoints,
719 std::vector<T>& gridValues,
720 std::vector<T>& cdfValues)
const
724 std::vector<unsigned int> bins(numEvaluationPoints,0);
725 gridValues.resize (numEvaluationPoints,0.);
726 cdfValues.resize (numEvaluationPoints,0.);
729 this->subSequenceSize(),
733 if (tmpMinValue == tmpMaxValue) {
734 if (tmpMinValue < -1.e-12) {
735 tmpMinValue += tmpMinValue*(1.e-8);
737 else if (tmpMinValue > 1.e-12) {
738 tmpMinValue -= tmpMinValue*(1.e-8);
741 tmpMinValue = 1.e-12;
745 subWeightHistogram(0,
751 unsigned int sumOfBins = 0;
752 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
753 sumOfBins += bins[i];
757 cdfValues.resize(numEvaluationPoints);
758 unsigned int partialSum = 0;
759 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
760 partialSum += bins[i];
761 cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
770 unsigned int numEvaluationPoints,
772 std::vector<T>& cdfValues)
const
776 std::vector<unsigned int> bins(numEvaluationPoints,0);
779 this->subSequenceSize(),
782 subWeightHistogram(0,
788 unsigned int sumOfBins = 0;
789 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
790 sumOfBins += bins[i];
794 cdfValues.resize(numEvaluationPoints);
795 unsigned int partialSum = 0;
796 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
797 partialSum += bins[i];
798 cdfValues[i] = ((T) partialSum)/((T) sumOfBins);
807 unsigned int initialPos,
808 unsigned int numPos)
const
810 if (this->subSequenceSize() == 0)
return 0.;
812 bool bRC = ((initialPos < this->subSequenceSize()) &&
814 ((initialPos+numPos) <= this->subSequenceSize()));
816 std::cerr <<
"In ScalarSequence<T>::subMeanExtra()"
817 <<
": ERROR at fullRank " << m_env.fullRank()
818 <<
", initialPos = " << initialPos
819 <<
", numPos = " << numPos
820 <<
", this->subSequenceSize() = " << this->subSequenceSize()
822 if (m_env.subDisplayFile()) {
823 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::subMeanExtra()"
824 <<
": ERROR at fullRank " << m_env.fullRank()
825 <<
", initialPos = " << initialPos
826 <<
", numPos = " << numPos
827 <<
", this->subSequenceSize() = " << this->subSequenceSize()
831 queso_require_msg(bRC,
"invalid input data");
833 unsigned int finalPosPlus1 = initialPos + numPos;
835 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
839 return tmpSum/(T) numPos;
845 bool useOnlyInter0Comm,
846 unsigned int initialPos,
847 unsigned int numPos)
const
849 if (m_env.numSubEnvironments() == 1) {
850 return this->subMeanExtra(initialPos,
856 T unifiedMeanValue = 0.;
857 if (useOnlyInter0Comm) {
858 if (m_env.inter0Rank() >= 0) {
859 bool bRC = ((initialPos < this->subSequenceSize()) &&
861 ((initialPos+numPos) <= this->subSequenceSize()));
862 queso_require_msg(bRC,
"invalid input data");
864 unsigned int finalPosPlus1 = initialPos + numPos;
866 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
867 localSum += m_seq[j];
870 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
871 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedMeanExtra()"
872 <<
": initialPos = " << initialPos
873 <<
", numPos = " << numPos
874 <<
", before MPI.Allreduce"
880 unsigned int unifiedNumPos = 0;
881 m_env.inter0Comm().template Allreduce<unsigned int>(&numPos, &unifiedNumPos, (int) 1, RawValue_MPI_SUM,
882 "ScalarSequence<T>::unifiedMeanExtra()",
883 "failed MPI.Allreduce() for numPos");
885 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
886 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedMeanExtra()"
887 <<
": numPos = " << numPos
888 <<
", unifiedNumPos = " << unifiedNumPos
892 m_env.inter0Comm().template Allreduce<double>(&localSum, &unifiedMeanValue, (int) 1, RawValue_MPI_SUM,
893 "ScalarSequence<T>::unifiedMeanExtra()",
894 "failed MPI.Allreduce() for sum");
896 unifiedMeanValue /= ((T) unifiedNumPos);
898 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
899 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedMeanExtra()"
900 <<
": localSum = " << localSum
901 <<
", unifiedMeanValue = " << unifiedMeanValue
907 this->subMeanExtra(initialPos,
912 queso_error_msg(
"parallel vectors not supported yet");
917 return unifiedMeanValue;
923 unsigned int initialPos,
924 unsigned int numPos)
const
926 if (this->subSequenceSize() == 0)
return 0.;
928 bool bRC = ((initialPos < this->subSequenceSize()) &&
930 ((initialPos+numPos) <= this->subSequenceSize()));
932 std::cerr <<
"In ScalarSequence<T>::subMedianExtra()"
933 <<
": ERROR at fullRank " << m_env.fullRank()
934 <<
", initialPos = " << initialPos
935 <<
", numPos = " << numPos
936 <<
", this->subSequenceSize() = " << this->subSequenceSize()
938 if (m_env.subDisplayFile()) {
939 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::subMedianExtra()"
940 <<
": ERROR at fullRank " << m_env.fullRank()
941 <<
", initialPos = " << initialPos
942 <<
", numPos = " << numPos
943 <<
", this->subSequenceSize() = " << this->subSequenceSize()
947 queso_require_msg(bRC,
"invalid input data");
951 this->extractScalarSeq(initialPos,
957 unsigned int tmpPos = (
unsigned int) (0.5 * (
double) numPos);
958 T resultValue = sortedSequence[tmpPos];
966 bool useOnlyInter0Comm,
967 unsigned int initialPos,
968 unsigned int numPos)
const
970 if (m_env.numSubEnvironments() == 1) {
971 return this->subMedianExtra(initialPos,
977 T unifiedMedianValue = 0.;
978 if (useOnlyInter0Comm) {
979 if (m_env.inter0Rank() >= 0) {
980 bool bRC = ((initialPos < this->subSequenceSize()) &&
982 ((initialPos+numPos) <= this->subSequenceSize()));
983 queso_require_msg(bRC,
"invalid input data");
986 this->unifiedSort(useOnlyInter0Comm,
988 unifiedSortedSequence);
989 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
990 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedMedianExtra()"
991 <<
", unifiedMedianValue = " << unifiedMedianValue
997 this->subMedianExtra(initialPos,
1002 queso_error_msg(
"parallel vectors not supported yet");
1007 return unifiedMedianValue;
1013 unsigned int initialPos,
1014 unsigned int numPos,
1015 const T& meanValue)
const
1017 if (this->subSequenceSize() == 0)
return 0.;
1019 bool bRC = ((initialPos < this->subSequenceSize()) &&
1021 ((initialPos+numPos) <= this->subSequenceSize()));
1022 queso_require_msg(bRC,
"invalid input data");
1024 unsigned int finalPosPlus1 = initialPos + numPos;
1027 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1028 diff = m_seq[j] - meanValue;
1029 samValue += diff*diff;
1032 samValue /= (((T) numPos) - 1.);
1040 bool useOnlyInter0Comm,
1041 unsigned int initialPos,
1042 unsigned int numPos,
1043 const T& unifiedMeanValue)
const
1045 if (m_env.numSubEnvironments() == 1) {
1046 return this->subSampleVarianceExtra(initialPos,
1053 T unifiedSamValue = 0.;
1054 if (useOnlyInter0Comm) {
1055 if (m_env.inter0Rank() >= 0) {
1056 bool bRC = ((initialPos < this->subSequenceSize()) &&
1058 ((initialPos+numPos) <= this->subSequenceSize()));
1059 queso_require_msg(bRC,
"invalid input data");
1061 unsigned int finalPosPlus1 = initialPos + numPos;
1063 T localSamValue = 0.;
1064 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1065 diff = m_seq[j] - unifiedMeanValue;
1066 localSamValue += diff*diff;
1069 unsigned int unifiedNumPos = 0;
1070 m_env.inter0Comm().template Allreduce<unsigned int>(&numPos, &unifiedNumPos, (int) 1, RawValue_MPI_SUM,
1071 "ScalarSequence<T>::unifiedSampleVarianceExtra()",
1072 "failed MPI.Allreduce() for numPos");
1074 m_env.inter0Comm().template Allreduce<double>(&localSamValue, &unifiedSamValue, (int) 1, RawValue_MPI_SUM,
1075 "ScalarSequence<T>::unifiedSampleVarianceExtra()",
1076 "failed MPI.Allreduce() for samValue");
1078 unifiedSamValue /= (((T) unifiedNumPos) - 1.);
1082 this->subSampleVarianceExtra(initialPos,
1088 queso_error_msg(
"parallel vectors not supported yet");
1093 return unifiedSamValue;
1099 unsigned int initialPos,
1100 unsigned int numPos,
1101 const T& meanValue)
const
1103 if (this->subSequenceSize() == 0)
return 0.;
1105 bool bRC = ((initialPos < this->subSequenceSize()) &&
1107 ((initialPos+numPos) <= this->subSequenceSize()));
1108 queso_require_msg(bRC,
"invalid input data");
1110 unsigned int finalPosPlus1 = initialPos + numPos;
1113 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1114 diff = m_seq[j] - meanValue;
1115 stdValue += diff*diff;
1118 stdValue /= (((T) numPos) - 1.);
1119 stdValue = sqrt(stdValue);
1127 bool useOnlyInter0Comm,
1128 unsigned int initialPos,
1129 unsigned int numPos,
1130 const T& unifiedMeanValue)
const
1132 if (m_env.numSubEnvironments() == 1) {
1133 return this->subSampleStd(initialPos,
1140 T unifiedStdValue = 0.;
1141 if (useOnlyInter0Comm) {
1142 if (m_env.inter0Rank() >= 0) {
1143 bool bRC = ((initialPos < this->subSequenceSize()) &&
1145 ((initialPos+numPos) <= this->subSequenceSize()));
1146 queso_require_msg(bRC,
"invalid input data");
1148 unsigned int finalPosPlus1 = initialPos + numPos;
1150 T localStdValue = 0.;
1151 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1152 diff = m_seq[j] - unifiedMeanValue;
1153 localStdValue += diff*diff;
1156 unsigned int unifiedNumPos = 0;
1157 m_env.inter0Comm().template Allreduce<unsigned int>(&numPos, &unifiedNumPos, (int) 1, RawValue_MPI_SUM,
1158 "ScalarSequence<T>::unifiedSampleStd()",
1159 "failed MPI.Allreduce() for numPos");
1161 m_env.inter0Comm().template Allreduce<double>(&localStdValue, &unifiedStdValue, (int) 1, RawValue_MPI_SUM,
1162 "ScalarSequence<T>::unifiedSampleStd()",
1163 "failed MPI.Allreduce() for stdValue");
1165 unifiedStdValue /= (((T) unifiedNumPos) - 1.);
1166 unifiedStdValue = sqrt(unifiedStdValue);
1170 this->subSampleStd(initialPos,
1176 queso_error_msg(
"parallel vectors not supported yet");
1181 return unifiedStdValue;
1187 unsigned int initialPos,
1188 unsigned int numPos,
1189 const T& meanValue)
const
1191 if (this->subSequenceSize() == 0)
return 0.;
1193 bool bRC = ((initialPos < this->subSequenceSize()) &&
1195 ((initialPos+numPos) <= this->subSequenceSize()));
1196 queso_require_msg(bRC,
"invalid input data");
1198 unsigned int finalPosPlus1 = initialPos + numPos;
1201 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1202 diff = m_seq[j] - meanValue;
1203 popValue += diff*diff;
1206 popValue /= (T) numPos;
1214 bool useOnlyInter0Comm,
1215 unsigned int initialPos,
1216 unsigned int numPos,
1217 const T& unifiedMeanValue)
const
1219 if (m_env.numSubEnvironments() == 1) {
1220 return this->subPopulationVariance(initialPos,
1227 T unifiedPopValue = 0.;
1228 if (useOnlyInter0Comm) {
1229 if (m_env.inter0Rank() >= 0) {
1230 bool bRC = ((initialPos < this->subSequenceSize()) &&
1232 ((initialPos+numPos) <= this->subSequenceSize()));
1233 queso_require_msg(bRC,
"invalid input data");
1235 unsigned int finalPosPlus1 = initialPos + numPos;
1237 T localPopValue = 0.;
1238 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1239 diff = m_seq[j] - unifiedMeanValue;
1240 localPopValue += diff*diff;
1243 unsigned int unifiedNumPos = 0;
1244 m_env.inter0Comm().template Allreduce<unsigned int>(&numPos, &unifiedNumPos, (int) 1, RawValue_MPI_SUM,
1245 "ScalarSequence<T>::unifiedPopulationVariance()",
1246 "failed MPI.Allreduce() for numPos");
1248 m_env.inter0Comm().template Allreduce<double>(&localPopValue, &unifiedPopValue, (int) 1, RawValue_MPI_SUM,
1249 "ScalarSequence<T>::unifiedPopulationVariance()",
1250 "failed MPI.Allreduce() for popValue");
1252 unifiedPopValue /= ((T) unifiedNumPos);
1256 this->subPopulationVariance(initialPos,
1262 queso_error_msg(
"parallel vectors not supported yet");
1267 return unifiedPopValue;
1273 unsigned int initialPos,
1274 unsigned int numPos,
1276 unsigned int lag)
const
1278 bool bRC = ((initialPos < this->subSequenceSize()) &&
1280 ((initialPos+numPos) <= this->subSequenceSize()) &&
1282 queso_require_msg(bRC,
"invalid input data");
1284 unsigned int loopSize = numPos - lag;
1285 unsigned int finalPosPlus1 = initialPos + loopSize;
1289 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
1290 diff1 = m_seq[j ] - meanValue;
1291 diff2 = m_seq[j+lag] - meanValue;
1292 covValue += diff1*diff2;
1295 covValue /= (T) loopSize;
1303 unsigned int initialPos,
1304 unsigned int numPos,
1305 unsigned int lag)
const
1307 bool bRC = ((initialPos < this->subSequenceSize()) &&
1309 ((initialPos+numPos) <= this->subSequenceSize()) &&
1311 queso_require_msg(bRC,
"invalid input data");
1313 T meanValue = this->subMeanExtra(initialPos,
1316 T covValueZero = this->autoCovariance(initialPos,
1321 T corrValue = this->autoCovariance(initialPos,
1326 return corrValue/covValueZero;
1332 unsigned int initialPos,
1333 unsigned int numPos,
1334 unsigned int maxLag,
1335 std::vector<T>& autoCorrs)
const
1337 double tmp = log((
double) numPos)/log(2.);
1338 double fractionalPart = tmp - ((double) ((
unsigned int) tmp));
1339 if (fractionalPart > 0.) tmp += (1. - fractionalPart);
1340 unsigned int fftSize = (
unsigned int) std::pow(2.,tmp+1);
1342 std::vector<double> rawDataVec(numPos,0.);
1343 std::vector<std::complex<double> > resultData(0,std::complex<double>(0.,0.));
1347 this->extractRawData(initialPos,
1351 T meanValue = this->subMeanExtra(initialPos,
1353 for (
unsigned int j = 0; j < numPos; ++j) {
1354 rawDataVec[j] -= meanValue;
1357 rawDataVec.resize(fftSize,0.);
1367 fftObj.
forward(rawDataVec,fftSize,resultData);
1370 for (
unsigned int j = 0; j < fftSize; ++j) {
1371 rawDataVec[j] = std::norm(resultData[j]);
1381 fftObj.
inverse(rawDataVec,fftSize,resultData);
1389 autoCorrs.resize(maxLag+1,0.);
1390 for (
unsigned int j = 0; j < autoCorrs.size(); ++j) {
1391 double ratio = ((double) j)/((double) (numPos-1));
1392 autoCorrs[j] = ( resultData[j].real()/resultData[0].real() )*(1.-ratio);
1401 unsigned int initialPos,
1402 unsigned int numPos,
1403 unsigned int numSum,
1404 T& autoCorrsSum)
const
1413 double tmp = log((
double) numPos)/log(2.);
1414 double fractionalPart = tmp - ((double) ((
unsigned int) tmp));
1415 if (fractionalPart > 0.) tmp += (1. - fractionalPart);
1416 unsigned int fftSize = (
unsigned int) std::pow(2.,tmp+1);
1418 std::vector<double> rawDataVec(numPos,0.);
1419 std::vector<std::complex<double> > resultData(0,std::complex<double>(0.,0.));
1423 this->extractRawData(initialPos,
1427 T meanValue = this->subMeanExtra(initialPos,
1429 for (
unsigned int j = 0; j < numPos; ++j) {
1430 rawDataVec[j] -= meanValue;
1432 rawDataVec.resize(fftSize,0.);
1442 fftObj.
forward(rawDataVec,fftSize,resultData);
1445 for (
unsigned int j = 0; j < fftSize; ++j) {
1446 rawDataVec[j] = std::norm(resultData[j]);
1448 fftObj.
inverse(rawDataVec,fftSize,resultData);
1459 for (
unsigned int j = 0; j < numSum; ++j) {
1460 double ratio = ((double) j)/((double) (numPos-1));
1461 autoCorrsSum += ( resultData[j].real()/resultData[0].real() )*(1.-ratio);
1470 unsigned int initialPos,
1471 unsigned int numPos,
1475 queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(),
"invalid input");
1478 std::advance(pos1,initialPos);
1481 std::advance(pos2,initialPos+numPos);
1483 if ((initialPos+numPos) == this->subSequenceSize()) {
1484 queso_require_msg(!(pos2 != m_seq.end()),
"invalid state");
1488 pos = std::min_element(pos1, pos2);
1490 pos = std::max_element(pos1, pos2);
1499 bool useOnlyInter0Comm,
1500 unsigned int initialPos,
1501 unsigned int numPos,
1503 T& unifiedMaxValue)
const
1505 if (m_env.numSubEnvironments() == 1) {
1506 return this->subMinMaxExtra(initialPos,
1514 if (useOnlyInter0Comm) {
1515 if (m_env.inter0Rank() >= 0) {
1519 this->subMinMaxExtra(initialPos,
1525 std::vector<double> sendBuf(1,0.);
1526 for (
unsigned int i = 0; i < sendBuf.size(); ++i) {
1527 sendBuf[i] = minValue;
1529 m_env.inter0Comm().template Allreduce<double>(&sendBuf[0], &unifiedMinValue, (int) sendBuf.size(), RawValue_MPI_MIN,
1530 "ScalarSequence<T>::unifiedMinMaxExtra()",
1531 "failed MPI.Allreduce() for min");
1534 for (
unsigned int i = 0; i < sendBuf.size(); ++i) {
1535 sendBuf[i] = maxValue;
1537 m_env.inter0Comm().template Allreduce<double>(&sendBuf[0], &unifiedMaxValue, (int) sendBuf.size(), RawValue_MPI_MAX,
1538 "ScalarSequence<T>::unifiedMinMaxExtra()",
1539 "failed MPI.Allreduce() for max");
1541 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1542 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedMinMaxExtra()"
1543 <<
": localMinValue = " << minValue
1544 <<
", localMaxValue = " << maxValue
1545 <<
", unifiedMinValue = " << unifiedMinValue
1546 <<
", unifiedMaxValue = " << unifiedMaxValue
1552 this->subMinMaxExtra(initialPos,
1559 queso_error_msg(
"parallel vectors not supported yet");
1570 unsigned int initialPos,
1571 const T& minHorizontalValue,
1572 const T& maxHorizontalValue,
1573 std::vector<T>& centers,
1574 std::vector<unsigned int>& bins)
const
1578 queso_require_greater_equal_msg(bins.size(), 3,
"number of 'bins' is too small: should be at least 3");
1582 for (
unsigned int j = 0; j < bins.size(); ++j) {
1587 double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((
double) bins.size()) - 2.);
1589 double minCenter = minHorizontalValue - horizontalDelta/2.;
1590 double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1591 for (
unsigned int j = 0; j < centers.size(); ++j) {
1592 double factor = ((double) j)/(((double) centers.size()) - 1.);
1593 centers[j] = (1. - factor) * minCenter + factor * maxCenter;
1596 unsigned int dataSize = this->subSequenceSize();
1597 for (
unsigned int j = 0; j < dataSize; ++j) {
1598 double value = m_seq[j];
1599 if (value < minHorizontalValue) {
1602 else if (value >= maxHorizontalValue) {
1603 bins[bins.size()-1]++;
1606 unsigned int index = 1 + (
unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1617 bool useOnlyInter0Comm,
1618 unsigned int initialPos,
1619 const T& unifiedMinHorizontalValue,
1620 const T& unifiedMaxHorizontalValue,
1621 std::vector<T>& unifiedCenters,
1622 std::vector<unsigned int>& unifiedBins)
const
1624 if (m_env.numSubEnvironments() == 1) {
1625 return this->subHistogram(initialPos,
1626 unifiedMinHorizontalValue,
1627 unifiedMaxHorizontalValue,
1634 if (useOnlyInter0Comm) {
1635 if (m_env.inter0Rank() >= 0) {
1636 queso_require_equal_to_msg(unifiedCenters.size(), unifiedBins.size(),
"vectors 'unifiedCenters' and 'unifiedBins' have different sizes");
1638 queso_require_greater_equal_msg(unifiedBins.size(), 3,
"number of 'unifiedBins' is too small: should be at least 3");
1640 for (
unsigned int j = 0; j < unifiedBins.size(); ++j) {
1641 unifiedCenters[j] = 0.;
1645 double unifiedHorizontalDelta = (unifiedMaxHorizontalValue - unifiedMinHorizontalValue)/(((
double) unifiedBins.size()) - 2.);
1647 double unifiedMinCenter = unifiedMinHorizontalValue - unifiedHorizontalDelta/2.;
1648 double unifiedMaxCenter = unifiedMaxHorizontalValue + unifiedHorizontalDelta/2.;
1649 for (
unsigned int j = 0; j < unifiedCenters.size(); ++j) {
1650 double factor = ((double) j)/(((double) unifiedCenters.size()) - 1.);
1651 unifiedCenters[j] = (1. - factor) * unifiedMinCenter + factor * unifiedMaxCenter;
1654 std::vector<unsigned int> localBins(unifiedBins.size(),0);
1655 unsigned int dataSize = this->subSequenceSize();
1656 for (
unsigned int j = 0; j < dataSize; ++j) {
1657 double value = m_seq[j];
1658 if (value < unifiedMinHorizontalValue) {
1661 else if (value >= unifiedMaxHorizontalValue) {
1662 localBins[localBins.size()-1]++;
1665 unsigned int index = 1 + (
unsigned int) ((value - unifiedMinHorizontalValue)/unifiedHorizontalDelta);
1670 m_env.inter0Comm().template Allreduce<unsigned int>(&localBins[0], &unifiedBins[0], (int) localBins.size(), RawValue_MPI_SUM,
1671 "ScalarSequence<T>::unifiedHistogram()",
1672 "failed MPI.Allreduce() for bins");
1674 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1675 for (
unsigned int i = 0; i < unifiedCenters.size(); ++i) {
1676 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedHistogram()"
1678 <<
", unifiedMinHorizontalValue = " << unifiedMinHorizontalValue
1679 <<
", unifiedMaxHorizontalValue = " << unifiedMaxHorizontalValue
1680 <<
", unifiedCenters = " << unifiedCenters[i]
1681 <<
", unifiedBins = " << unifiedBins[i]
1688 this->subHistogram(initialPos,
1689 unifiedMinHorizontalValue,
1690 unifiedMaxHorizontalValue,
1696 queso_error_msg(
"parallel vectors not supported yet");
1707 unsigned int initialPos,
1708 const T& minHorizontalValue,
1709 const T& maxHorizontalValue,
1711 std::vector<unsigned int>& bins)
const
1713 queso_require_greater_equal_msg(bins.size(), 3,
"number of 'bins' is too small: should be at least 3");
1715 for (
unsigned int j = 0; j < bins.size(); ++j) {
1719 double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((
double) bins.size()) - 2.);
1720 double minCenter = minHorizontalValue - horizontalDelta/2.;
1721 double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1728 unsigned int dataSize = this->subSequenceSize();
1729 for (
unsigned int j = 0; j < dataSize; ++j) {
1730 double value = m_seq[j];
1731 if (value < minHorizontalValue) {
1734 else if (value >= maxHorizontalValue) {
1735 bins[bins.size()-1]++;
1738 unsigned int index = 1 + (
unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1749 unsigned int initialPos,
1750 const T& minHorizontalValue,
1751 const T& maxHorizontalValue,
1753 std::vector<unsigned int>& bins)
const
1755 queso_require_greater_equal_msg(bins.size(), 3,
"number of 'bins' is too small: should be at least 3");
1757 for (
unsigned int j = 0; j < bins.size(); ++j) {
1761 double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((
double) bins.size()) - 2.);
1762 double minCenter = minHorizontalValue - horizontalDelta/2.;
1763 double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1770 unsigned int dataSize = this->subSequenceSize();
1771 for (
unsigned int j = 0; j < dataSize; ++j) {
1772 double value = m_seq[j];
1773 if (value < minHorizontalValue) {
1776 else if (value >= maxHorizontalValue) {
1777 bins[bins.size()-1]++;
1780 unsigned int index = 1 + (
unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1791 unsigned int initialPos,
1792 const T& minHorizontalValue,
1793 const T& maxHorizontalValue,
1794 std::vector<T>& gridValues,
1795 std::vector<unsigned int>& bins)
const
1797 queso_require_greater_equal_msg(bins.size(), 3,
"number of 'bins' is too small: should be at least 3");
1799 for (
unsigned int j = 0; j < bins.size(); ++j) {
1803 double horizontalDelta = (maxHorizontalValue - minHorizontalValue)/(((
double) bins.size()) - 2.);
1804 double minCenter = minHorizontalValue - horizontalDelta/2.;
1805 double maxCenter = maxHorizontalValue + horizontalDelta/2.;
1812 gridValues.resize(tmpGrid.size(),0.);
1813 for (
unsigned int i = 0; i < tmpGrid.size(); ++i) {
1814 gridValues[i] = tmpGrid[i];
1817 unsigned int dataSize = this->subSequenceSize();
1818 for (
unsigned int j = 0; j < dataSize; ++j) {
1819 double value = m_seq[j];
1820 if (value < minHorizontalValue) {
1823 else if (value >= maxHorizontalValue) {
1824 bins[bins.size()-1]++;
1827 unsigned int index = 1 + (
unsigned int) ((value - minHorizontalValue)/horizontalDelta);
1843 unsigned int initialPos,
1846 unsigned int numPos = this->subSequenceSize() - initialPos;
1848 this->extractScalarSeq(initialPos,
1860 bool useOnlyInter0Comm,
1861 unsigned int initialPos,
1864 if (m_env.numSubEnvironments() == 1) {
1865 return this->subSort(initialPos,unifiedSortedSequence);
1870 if (useOnlyInter0Comm) {
1871 if (m_env.inter0Rank() >= 0) {
1874 unsigned int localNumPos = this->subSequenceSize() - initialPos;
1876 std::vector<T> leafData(localNumPos,0.);
1877 this->extractRawData(0,
1882 if (m_env.inter0Rank() == 0) {
1883 int minus1NumTreeLevels = 0;
1884 int power2NumTreeNodes = 1;
1886 while (power2NumTreeNodes < m_env.inter0Comm().NumProc()) {
1887 power2NumTreeNodes += power2NumTreeNodes;
1888 minus1NumTreeLevels++;
1891 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
1892 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedSort()"
1893 <<
": sorting tree has " << m_env.inter0Comm().NumProc()
1894 <<
" nodes and " << minus1NumTreeLevels+1
1899 this->parallelMerge(unifiedSortedSequence.
rawData(),
1901 minus1NumTreeLevels);
1903 else if (m_env.inter0Rank() > 0) {
1904 unsigned int uintBuffer[1];
1906 m_env.inter0Comm().Recv((
void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, RawValue_MPI_ANY_SOURCE, SCALAR_SEQUENCE_INIT_MPI_MSG, &status,
1907 "ScalarSequence<T>::unifiedSort()",
1908 "failed MPI.Recv() for init");
1911 unsigned int treeLevel = uintBuffer[0];
1912 this->parallelMerge(unifiedSortedSequence.
rawData(),
1920 unsigned int unifiedDataSize = unifiedSortedSequence.
subSequenceSize();
1921 m_env.inter0Comm().Bcast((
void *) &unifiedDataSize, (
int) 1, RawValue_MPI_UNSIGNED, 0,
1922 "ScalarSequence<T>::unifiedSort()",
1923 "failed MPI.Bcast() for unified data size");
1925 unsigned int sumOfNumPos = 0;
1926 m_env.inter0Comm().template Allreduce<unsigned int>(&localNumPos, &sumOfNumPos, (int) 1, RawValue_MPI_SUM,
1927 "ScalarSequence<T>::unifiedSort()",
1928 "failed MPI.Allreduce() for data size");
1933 m_env.inter0Comm().Bcast((
void *) &unifiedSortedSequence.
rawData()[0], (int) unifiedDataSize, RawValue_MPI_DOUBLE, 0,
1934 "ScalarSequence<T>::unifiedSort()",
1935 "failed MPI.Bcast() for unified data");
1937 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
1938 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::parallelMerge()"
1939 <<
": tree node " << m_env.inter0Rank()
1940 <<
", unifiedSortedSequence[0] = " << unifiedSortedSequence[0]
1941 <<
", unifiedSortedSequence[" << unifiedSortedSequence.
subSequenceSize()-1 <<
"] = " << unifiedSortedSequence[unifiedSortedSequence.
subSequenceSize()-1]
1949 this->subSort(initialPos,unifiedSortedSequence);
1953 queso_error_msg(
"parallel vectors not supported yet");
1965 queso_require_less_msg(initialPos, this->subSequenceSize(),
"'initialPos' is too big");
1968 this->subSort(initialPos,
1972 unsigned int dataSize = this->subSequenceSize() - initialPos;
1976 bool everythingOk =
true;
1981 unsigned int pos1 = (
unsigned int) ( (((
double) dataSize) + 1.)*1./4. - 1. );
1982 if (pos1 > (dataSize-1)) {
1984 everythingOk =
false;
1986 unsigned int pos1inc = pos1+1;
1987 if (pos1inc > (dataSize-1)) {
1988 pos1inc = dataSize-1;
1989 everythingOk =
false;
1995 unsigned int pos3 = (
unsigned int) ( (((
double) dataSize) + 1.)*3./4. - 1. );
1996 if (pos3 > (dataSize-1)) {
1998 everythingOk =
false;
2000 unsigned int pos3inc = pos3+1;
2001 if (pos3inc > (dataSize-1)) {
2002 pos3inc = dataSize-1;
2003 everythingOk =
false;
2006 double fraction1 = (((double) dataSize) + 1.)*1./4. - 1. - ((double) pos1);
2007 if (fraction1 < 0.) {
2009 everythingOk =
false;
2011 double fraction3 = (((double) dataSize) + 1.)*3./4. - 1. - ((double) pos3);
2012 if (fraction3 < 0.) {
2014 everythingOk =
false;
2017 if (everythingOk ==
false) {
2018 std::cerr <<
"In ScalarSequence<T>::subInterQuantileRange()"
2019 <<
", worldRank = " << m_env.
worldRank()
2020 <<
": at least one adjustment was necessary"
2035 T value1 = (1.-fraction1) * sortedSequence[pos1] + fraction1 * sortedSequence[pos1inc];
2036 T value3 = (1.-fraction3) * sortedSequence[pos3] + fraction3 * sortedSequence[pos3inc];
2037 T iqrValue = value3 - value1;
2039 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2040 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::subInterQuantileRange()"
2041 <<
": iqrValue = " << iqrValue
2042 <<
", dataSize = " << dataSize
2043 <<
", pos1 = " << pos1
2044 <<
", pos3 = " << pos3
2045 <<
", value1 = " << value1
2046 <<
", value3 = " << value3
2077 bool useOnlyInter0Comm,
2078 unsigned int initialPos)
const
2080 T unifiedIqrValue = 0.;
2082 if (m_env.numSubEnvironments() == 1) {
2083 return this->subInterQuantileRange(initialPos);
2088 if (useOnlyInter0Comm) {
2089 if (m_env.inter0Rank() >= 0) {
2093 this->unifiedSort(useOnlyInter0Comm,
2095 unifiedSortedSequence);
2096 unsigned int unifiedDataSize = unifiedSortedSequence.
subSequenceSize();
2098 unsigned int localDataSize = this->subSequenceSize() - initialPos;
2099 unsigned int sumOfLocalSizes = 0;
2100 m_env.inter0Comm().template Allreduce<unsigned int>(&localDataSize, &sumOfLocalSizes, (int) 1, RawValue_MPI_SUM,
2101 "ScalarSequence<T>::unifiedInterQuantileRange()",
2102 "failed MPI.Allreduce() for data size");
2106 unsigned int pos1 = (
unsigned int) ( (((
double) unifiedDataSize) + 1.)*1./4. - 1. );
2107 unsigned int pos3 = (
unsigned int) ( (((
double) unifiedDataSize) + 1.)*3./4. - 1. );
2109 double fraction1 = (((double) unifiedDataSize) + 1.)*1./4. - 1. - ((double) pos1);
2110 double fraction3 = (((double) unifiedDataSize) + 1.)*3./4. - 1. - ((double) pos3);
2112 T value1 = (1.-fraction1) * unifiedSortedSequence[pos1] + fraction1 * unifiedSortedSequence[pos1+1];
2113 T value3 = (1.-fraction3) * unifiedSortedSequence[pos3] + fraction3 * unifiedSortedSequence[pos3+1];
2114 unifiedIqrValue = value3 - value1;
2116 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2117 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedInterQuantileRange()"
2118 <<
": unifiedIqrValue = " << unifiedIqrValue
2119 <<
", localDataSize = " << localDataSize
2120 <<
", unifiedDataSize = " << unifiedDataSize
2121 <<
", pos1 = " << pos1
2122 <<
", pos3 = " << pos3
2123 <<
", value1 = " << value1
2124 <<
", value3 = " << value3
2153 unifiedIqrValue = this->subInterQuantileRange(initialPos);
2157 queso_error_msg(
"parallel vectors not supported yet");
2162 return unifiedIqrValue;
2168 unsigned int initialPos,
2170 unsigned int kdeDimension)
const
2172 bool bRC = (initialPos < this->subSequenceSize());
2173 queso_require_msg(bRC,
"invalid input data");
2175 unsigned int dataSize = this->subSequenceSize() - initialPos;
2177 T meanValue = this->subMeanExtra(initialPos,
2180 T samValue = this->subSampleVarianceExtra(initialPos,
2185 if (iqrValue <= 0.) {
2186 scaleValue = 1.06*std::sqrt(samValue)/std::pow(dataSize,1./(4. + ((
double) kdeDimension)));
2189 scaleValue = 1.06*std::min(std::sqrt(samValue),iqrValue/1.34)/std::pow(dataSize,1./(4. + ((
double) kdeDimension)));
2192 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2193 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::subScaleForKde()"
2194 <<
": iqrValue = " << iqrValue
2195 <<
", meanValue = " << meanValue
2196 <<
", samValue = " << samValue
2197 <<
", dataSize = " << dataSize
2198 <<
", scaleValue = " << scaleValue
2208 bool useOnlyInter0Comm,
2209 unsigned int initialPos,
2210 const T& unifiedIqrValue,
2211 unsigned int kdeDimension)
const
2213 if (m_env.numSubEnvironments() == 1) {
2214 return this->subScaleForKde(initialPos,
2221 T unifiedScaleValue = 0.;
2222 if (useOnlyInter0Comm) {
2223 if (m_env.inter0Rank() >= 0) {
2224 bool bRC = (initialPos < this->subSequenceSize());
2225 queso_require_msg(bRC,
"invalid input data");
2227 unsigned int localDataSize = this->subSequenceSize() - initialPos;
2229 T unifiedMeanValue = this->unifiedMeanExtra(useOnlyInter0Comm,
2233 T unifiedSamValue = this->unifiedSampleVarianceExtra(useOnlyInter0Comm,
2238 unsigned int unifiedDataSize = 0;
2239 m_env.inter0Comm().template Allreduce<unsigned int>(&localDataSize, &unifiedDataSize, (int) 1, RawValue_MPI_SUM,
2240 "ScalarSequence<T>::unifiedScaleForKde()",
2241 "failed MPI.Allreduce() for data size");
2243 if (unifiedIqrValue <= 0.) {
2244 unifiedScaleValue = 1.06*std::sqrt(unifiedSamValue)/std::pow(unifiedDataSize,1./(4. + ((
double) kdeDimension)));
2247 unifiedScaleValue = 1.06*std::min(std::sqrt(unifiedSamValue),unifiedIqrValue/1.34)/std::pow(unifiedDataSize,1./(4. + ((
double) kdeDimension)));
2250 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2251 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedScaleForKde()"
2252 <<
": unifiedIqrValue = " << unifiedIqrValue
2253 <<
", unifiedMeanValue = " << unifiedMeanValue
2254 <<
", unifiedSamValue = " << unifiedSamValue
2255 <<
", unifiedDataSize = " << unifiedDataSize
2256 <<
", unifiedScaleValue = " << unifiedScaleValue
2262 unifiedScaleValue = this->subScaleForKde(initialPos,
2268 queso_error_msg(
"parallel vectors not supported yet");
2273 return unifiedScaleValue;
2279 unsigned int initialPos,
2281 const std::vector<T>& evaluationPositions,
2282 std::vector<double>& densityValues)
const
2284 bool bRC = ((initialPos < this->subSequenceSize() ) &&
2285 (0 < evaluationPositions.size()) &&
2286 (evaluationPositions.size() == densityValues.size() ));
2287 queso_require_msg(bRC,
"invalid input data");
2289 unsigned int dataSize = this->subSequenceSize() - initialPos;
2290 unsigned int numEvals = evaluationPositions.size();
2292 double scaleInv = 1./scaleValue;
2293 for (
unsigned int j = 0; j < numEvals; ++j) {
2294 double x = evaluationPositions[j];
2296 for (
unsigned int k = 0; k < dataSize; ++k) {
2297 double xk = m_seq[initialPos+k];
2300 densityValues[j] = scaleInv * (value/(double) dataSize);
2309 bool useOnlyInter0Comm,
2310 unsigned int initialPos,
2311 double unifiedScaleValue,
2312 const std::vector<T>& unifiedEvaluationPositions,
2313 std::vector<double>& unifiedDensityValues)
const
2315 if (m_env.numSubEnvironments() == 1) {
2316 return this->subGaussian1dKde(initialPos,
2318 unifiedEvaluationPositions,
2319 unifiedDensityValues);
2324 if (useOnlyInter0Comm) {
2325 if (m_env.inter0Rank() >= 0) {
2326 bool bRC = ((initialPos < this->subSequenceSize() ) &&
2327 (0 < unifiedEvaluationPositions.size()) &&
2328 (unifiedEvaluationPositions.size() == unifiedDensityValues.size() ));
2329 queso_require_msg(bRC,
"invalid input data");
2331 unsigned int localDataSize = this->subSequenceSize() - initialPos;
2332 unsigned int unifiedDataSize = 0;
2333 m_env.inter0Comm().template Allreduce<unsigned int>(&localDataSize, &unifiedDataSize, (int) 1, RawValue_MPI_SUM,
2334 "ScalarSequence<T>::unifiedGaussian1dKde()",
2335 "failed MPI.Allreduce() for data size");
2337 unsigned int numEvals = unifiedEvaluationPositions.size();
2339 std::vector<double> densityValues(numEvals,0.);
2340 double unifiedScaleInv = 1./unifiedScaleValue;
2341 for (
unsigned int j = 0; j < numEvals; ++j) {
2342 double x = unifiedEvaluationPositions[j];
2344 for (
unsigned int k = 0; k < localDataSize; ++k) {
2345 double xk = m_seq[initialPos+k];
2348 densityValues[j] = value;
2351 for (
unsigned int j = 0; j < numEvals; ++j) {
2352 unifiedDensityValues[j] = 0.;
2354 m_env.inter0Comm().template Allreduce<double>(&densityValues[0], &unifiedDensityValues[0], (int) numEvals, RawValue_MPI_SUM,
2355 "ScalarSequence<T>::unifiedGaussian1dKde()",
2356 "failed MPI.Allreduce() for density values");
2358 for (
unsigned int j = 0; j < numEvals; ++j) {
2359 unifiedDensityValues[j] *= unifiedScaleInv/((double) unifiedDataSize);
2362 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
2363 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedGaussian1dKde()"
2364 <<
": unifiedDensityValues[0] = " << unifiedDensityValues[0]
2365 <<
", unifiedDensityValues[" << unifiedDensityValues.size()-1 <<
"] = " << unifiedDensityValues[unifiedDensityValues.size()-1]
2371 this->subGaussian1dKde(initialPos,
2373 unifiedEvaluationPositions,
2374 unifiedDensityValues);
2378 queso_error_msg(
"parallel vectors not supported yet");
2389 unsigned int initialPos,
2390 unsigned int spacing)
2392 if (m_env.subDisplayFile()) {
2393 *m_env.subDisplayFile() <<
"Entering ScalarSequence<V,M>::filter()"
2394 <<
": initialPos = " << initialPos
2395 <<
", spacing = " << spacing
2396 <<
", subSequenceSize = " << this->subSequenceSize()
2401 unsigned int j = initialPos;
2402 unsigned int originalSubSequenceSize = this->subSequenceSize();
2403 while (j < originalSubSequenceSize) {
2406 m_seq[i] = m_seq[j];
2412 this->resizeSequence(i);
2414 if (m_env.subDisplayFile()) {
2415 *m_env.subDisplayFile() <<
"Leaving ScalarSequence<V,M>::filter()"
2416 <<
": initialPos = " << initialPos
2417 <<
", spacing = " << spacing
2418 <<
", subSequenceSize = " << this->subSequenceSize()
2428 bool useOnlyInter0Comm,
2429 unsigned int initialPos,
2430 unsigned int spacing)
const
2432 double resultValue = 0.;
2436 if (useOnlyInter0Comm) {
2437 if (m_env.inter0Rank() >= 0) {
2438 queso_not_implemented();
2446 queso_error_msg(
"parallel vectors not supported yet");
2458 unsigned int srcInitialPos,
2459 unsigned int srcNumPos)
2461 queso_require_greater_equal_msg(src.
subSequenceSize(), (srcInitialPos+1),
"srcInitialPos is too big");
2463 queso_require_greater_equal_msg(src.
subSequenceSize(), (srcInitialPos+srcNumPos),
"srcNumPos is too big");
2465 deleteStoredScalars();
2466 unsigned int currentSize = this->subSequenceSize();
2467 m_seq.resize(currentSize+srcNumPos,0.);
2468 for (
unsigned int i = 0; i < srcNumPos; ++i) {
2469 m_seq[currentSize+i] = src.
m_seq[srcInitialPos+i];
2483 T subMaxValue = subCorrespondingScalarValues.
subMaxPlain();
2486 unsigned int subNumPos = 0;
2487 for (
unsigned int i = 0; i < iMax; ++i) {
2488 if (subCorrespondingScalarValues[i] == subMaxValue) {
2495 for (
unsigned int i = 0; i < iMax; ++i) {
2496 if (subCorrespondingScalarValues[i] == subMaxValue) {
2497 subPositionsOfMaximum[j] = (*this)[i];
2513 T maxValue = subCorrespondingScalarValues.
subMaxPlain();
2516 unsigned int numPos = 0;
2517 for (
unsigned int i = 0; i < iMax; ++i) {
2518 if (subCorrespondingScalarValues[i] == maxValue) {
2525 for (
unsigned int i = 0; i < iMax; ++i) {
2526 if (subCorrespondingScalarValues[i] == maxValue) {
2527 unifiedPositionsOfMaximum[j] = (*this)[i];
2538 unsigned int initialPos,
2539 unsigned int numPos,
2540 const std::string& fileName,
2541 const std::string& fileType,
2542 const std::set<unsigned int>& allowedSubEnvIds)
const
2544 queso_require_greater_equal_msg(m_env.subRank(), 0,
"unexpected subRank");
2547 if (m_env.openOutputFile(fileName,
2554 if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT ||
2555 fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT) {
2556 this->subWriteContents(initialPos,
2561 #ifdef QUESO_HAS_HDF5
2562 else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2565 hsize_t dims[1] = { this->subSequenceSize() };
2566 hid_t dataspace_id = H5Screate_simple(1, dims, dims);
2567 queso_require_greater_equal_msg(
2570 "error create dataspace with id: " << dataspace_id);
2573 hid_t dataset_id = H5Dcreate(filePtrSet.h5Var,
2581 queso_require_greater_equal_msg(
2584 "error creating dataset with id: " << dataset_id);
2587 herr_t status = H5Dwrite(
2595 queso_require_greater_equal_msg(
2598 "error writing dataset to file with id: " << filePtrSet.h5Var);
2601 H5Dclose(dataset_id);
2602 H5Sclose(dataspace_id);
2606 m_env.closeFile(filePtrSet,fileType);
2616 const std::string& fileType)
const
2618 if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2619 this->writeSubMatlabHeader(ofs, this->subSequenceSize());
2621 else if (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT) {
2622 this->writeTxtHeader(ofs, this->subSequenceSize());
2625 unsigned int chainSize = this->subSequenceSize();
2626 for (
unsigned int j = 0; j < chainSize; ++j) {
2631 if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2641 const std::string& fileName,
2642 const std::string& inputFileType)
const
2644 std::string fileType(inputFileType);
2645 #ifdef QUESO_HAS_HDF5
2648 if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2649 if (m_env.subDisplayFile()) {
2650 *m_env.subDisplayFile() <<
"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
2657 if (m_env.subRank() == 0) {
2658 std::cerr <<
"WARNING in ScalarSequence<T>::unifiedWriteContents()"
2659 <<
": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2660 <<
"' has been requested, but this QUESO library has not been built with 'hdf5'"
2661 <<
". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2665 fileType = UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT;
2672 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
2673 *m_env.subDisplayFile() <<
"Entering ScalarSequence<T>::unifiedWriteContents()"
2674 <<
": worldRank " << m_env.worldRank()
2675 <<
", subEnvironment " << m_env.subId()
2676 <<
", subRank " << m_env.subRank()
2677 <<
", inter0Rank " << m_env.inter0Rank()
2679 <<
", fileName = " << fileName
2680 <<
", fileType = " << fileType
2686 if (m_env.inter0Rank() >= 0) {
2687 for (
unsigned int r = 0; r < (
unsigned int) m_env.inter0Comm().NumProc(); ++r) {
2688 if (m_env.inter0Rank() == (int) r) {
2692 bool writeOver =
false;
2695 if (m_env.openUnifiedOutputFile(fileName,
2698 unifiedFilePtrSet)) {
2701 unsigned int chainSize = this->subSequenceSize();
2702 if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
2703 (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
2707 if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2708 this->writeUnifiedMatlabHeader(*unifiedFilePtrSet.
ofsVar,
2709 this->subSequenceSize()*m_env.inter0Comm().NumProc());
2712 this->writeTxtHeader(*unifiedFilePtrSet.
ofsVar,
2713 this->subSequenceSize()*m_env.inter0Comm().NumProc());
2717 for (
unsigned int j = 0; j < chainSize; ++j) {
2718 *unifiedFilePtrSet.
ofsVar << m_seq[j]
2722 m_env.closeFile(unifiedFilePtrSet,fileType);
2724 #ifdef QUESO_HAS_HDF5
2725 else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2726 unsigned int numParams = 1;
2728 hid_t datatype = H5Tcopy(H5T_NATIVE_DOUBLE);
2731 dimsf[0] = chainSize;
2732 hid_t dataspace = H5Screate_simple(1, dimsf, NULL);
2734 hid_t dataset = H5Dcreate2(unifiedFilePtrSet.
h5Var,
2743 struct timeval timevalBegin;
2745 iRC = gettimeofday(&timevalBegin,NULL);
2750 status = H5Dwrite(dataset,
2762 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
2763 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedWriteContents()"
2764 <<
": worldRank " << m_env.worldRank()
2765 <<
", fullRank " << m_env.fullRank()
2766 <<
", subEnvironment " << m_env.subId()
2767 <<
", subRank " << m_env.subRank()
2768 <<
", inter0Rank " << m_env.inter0Rank()
2769 <<
", fileName = " << fileName
2770 <<
", numParams = " << numParams
2771 <<
", chainSize = " << chainSize
2772 <<
", writeTime = " << writeTime <<
" seconds"
2778 H5Sclose(dataspace);
2784 queso_error_msg(
"hdf file type not supported for multiple sub-environments yet");
2791 m_env.inter0Comm().Barrier();
2794 if (m_env.inter0Rank() == 0) {
2795 if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
2796 (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
2798 if (m_env.openUnifiedOutputFile(fileName,
2801 unifiedFilePtrSet)) {
2803 if (fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) {
2804 *unifiedFilePtrSet.
ofsVar <<
"];\n";
2807 m_env.closeFile(unifiedFilePtrSet,fileType);
2810 else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2814 queso_error_msg(
"invalid file type");
2819 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
2820 *m_env.subDisplayFile() <<
"Leaving ScalarSequence<T>::unifiedWriteContents()"
2821 <<
", fileName = " << fileName
2822 <<
", fileType = " << fileType
2833 double sequenceSize)
const
2835 ofs << m_name <<
"_unified" <<
" = zeros(" << sequenceSize
2839 ofs << m_name <<
"_unified" <<
" = [";
2845 double sequenceSize)
const
2847 ofs << m_name <<
"_sub" << m_env.subIdString() <<
" = zeros(" << sequenceSize
2851 ofs << m_name <<
"_sub" << m_env.subIdString() <<
" = [";
2857 double sequenceSize)
const
2859 ofs << sequenceSize <<
" " << 1 << std::endl;
2866 const std::string& fileName,
2867 const std::string& inputFileType,
2868 const unsigned int subReadSize)
2870 queso_require_not_equal_to_msg(inputFileType, std::string(UQ_FILE_EXTENSION_FOR_TXT_FORMAT), std::string(
"reading txt files is not yet supported"));
2871 std::string fileType(inputFileType);
2872 #ifdef QUESO_HAS_HDF5
2875 if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
2876 if (m_env.subDisplayFile()) {
2877 *m_env.subDisplayFile() <<
"WARNING in ScalarSequence<T>::unifiedReadContents()"
2878 <<
": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2879 <<
"' has been requested, but this QUESO library has not been built with 'hdf5'"
2880 <<
". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2884 if (m_env.subRank() == 0) {
2885 std::cerr <<
"WARNING in ScalarSequence<T>::unifiedReadContents()"
2886 <<
": file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2887 <<
"' has been requested, but this QUESO library has not been built with 'hdf5'"
2888 <<
". Code will therefore process the file format '" << UQ_FILE_EXTENSION_FOR_HDF_FORMAT
2892 fileType = UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT;
2897 if (m_env.subDisplayFile()) {
2898 *m_env.subDisplayFile() <<
"Entering ScalarSequence<T>::unifiedReadContents()"
2899 <<
": worldRank " << m_env.worldRank()
2900 <<
", fullRank " << m_env.fullRank()
2901 <<
", subEnvironment " << m_env.subId()
2902 <<
", subRank " << m_env.subRank()
2903 <<
", inter0Rank " << m_env.inter0Rank()
2905 <<
", fileName = " << fileName
2906 <<
", subReadSize = " << subReadSize
2911 this->resizeSequence(subReadSize);
2913 if (m_env.inter0Rank() >= 0) {
2914 double unifiedReadSize = subReadSize*m_env.inter0Comm().NumProc();
2917 unsigned int idOfMyFirstLine = 1 + m_env.inter0Rank()*subReadSize;
2918 unsigned int idOfMyLastLine = (1 + m_env.inter0Rank())*subReadSize;
2919 unsigned int numParams = 1;
2921 for (
unsigned int r = 0; r < (
unsigned int) m_env.inter0Comm().NumProc(); ++r) {
2922 if (m_env.inter0Rank() == (int) r) {
2925 if (m_env.openUnifiedInputFile(fileName,
2927 unifiedFilePtrSet)) {
2928 if ((fileType == UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT) ||
2929 (fileType == UQ_FILE_EXTENSION_FOR_TXT_FORMAT)) {
2933 std::string tmpString;
2936 *unifiedFilePtrSet.
ifsVar >> tmpString;
2940 *unifiedFilePtrSet.
ifsVar >> tmpString;
2945 *unifiedFilePtrSet.
ifsVar >> tmpString;
2947 unsigned int posInTmpString = 6;
2951 std::string nPositionsString((
size_t) (tmpString.size()-posInTmpString+1),
' ');
2952 unsigned int posInPositionsString = 0;
2954 queso_require_less_msg(posInTmpString, tmpString.size(),
"symbol ',' not found in first line of file");
2955 nPositionsString[posInPositionsString++] = tmpString[posInTmpString++];
2956 }
while (tmpString[posInTmpString] !=
',');
2957 nPositionsString[posInPositionsString] =
'\0';
2962 std::string nParamsString((
size_t) (tmpString.size()-posInTmpString+1),
' ');
2963 unsigned int posInParamsString = 0;
2965 queso_require_less_msg(posInTmpString, tmpString.size(),
"symbol ')' not found in first line of file");
2966 nParamsString[posInParamsString++] = tmpString[posInTmpString++];
2967 }
while (tmpString[posInTmpString] !=
')');
2968 nParamsString[posInParamsString] =
'\0';
2971 unsigned int sizeOfChainInFile = (
unsigned int) strtod(nPositionsString.c_str(),NULL);
2972 unsigned int numParamsInFile = (
unsigned int) strtod(nParamsString.c_str(), NULL);
2973 if (m_env.subDisplayFile()) {
2974 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedReadContents()"
2975 <<
": worldRank " << m_env.worldRank()
2976 <<
", fullRank " << m_env.fullRank()
2977 <<
", sizeOfChainInFile = " << sizeOfChainInFile
2978 <<
", numParamsInFile = " << numParamsInFile
2983 queso_require_greater_equal_msg(sizeOfChainInFile, unifiedReadSize,
"size of chain in file is not big enough");
2986 queso_require_equal_to_msg(numParamsInFile, numParams,
"number of parameters of chain in file is different than number of parameters in this chain object");
2990 unsigned int maxCharsPerLine = 64*numParams;
2992 unsigned int lineId = 0;
2993 while (lineId < idOfMyFirstLine) {
2994 unifiedFilePtrSet.
ifsVar->ignore(maxCharsPerLine,
'\n');
3001 std::string tmpString;
3004 *unifiedFilePtrSet.
ifsVar >> tmpString;
3008 *unifiedFilePtrSet.
ifsVar >> tmpString;
3013 std::streampos tmpPos = unifiedFilePtrSet.
ifsVar->tellg();
3014 unifiedFilePtrSet.
ifsVar->seekg(tmpPos+(std::streampos)2);
3018 while (lineId <= idOfMyLastLine) {
3019 for (
unsigned int i = 0; i < numParams; ++i) {
3020 *unifiedFilePtrSet.
ifsVar >> tmpScalar;
3022 m_seq[lineId - idOfMyFirstLine] = tmpScalar;
3026 #ifdef QUESO_HAS_HDF5
3027 else if (fileType == UQ_FILE_EXTENSION_FOR_HDF_FORMAT) {
3029 hid_t dataset = H5Dopen2(unifiedFilePtrSet.
h5Var,
3032 hid_t datatype = H5Dget_type(dataset);
3033 H5T_class_t t_class = H5Tget_class(datatype);
3035 hid_t dataspace = H5Dget_space(dataset);
3036 int rank = H5Sget_simple_extent_ndims(dataspace);
3040 status_n = H5Sget_simple_extent_dims(dataspace, dims_in, NULL);
3047 queso_require_greater_equal_msg(dims_in[1], subReadSize,
"dims_in[1] is smaller that requested 'subReadSize'");
3049 struct timeval timevalBegin;
3051 iRC = gettimeofday(&timevalBegin,NULL);
3054 unsigned int chainSizeIn = (
unsigned int) dims_in[1];
3056 std::vector<double*> dataIn((
size_t) numParams,NULL);
3057 dataIn[0] = (
double*) malloc(numParams*chainSizeIn*
sizeof(
double));
3058 for (
unsigned int i = 1; i < numParams; ++i) {
3059 dataIn[i] = dataIn[i-1] + chainSizeIn;
3063 status = H5Dread(dataset,
3072 for (
unsigned int j = 0; j < subReadSize; ++j) {
3073 for (
unsigned int i = 0; i < numParams; ++i) {
3074 tmpScalar = dataIn[i][j];
3076 m_seq[j] = tmpScalar;
3080 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
3081 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::unifiedReadContents()"
3082 <<
": worldRank " << m_env.worldRank()
3083 <<
", fullRank " << m_env.fullRank()
3084 <<
", subEnvironment " << m_env.subId()
3085 <<
", subRank " << m_env.subRank()
3086 <<
", inter0Rank " << m_env.inter0Rank()
3087 <<
", fileName = " << fileName
3088 <<
", numParams = " << numParams
3089 <<
", chainSizeIn = " << chainSizeIn
3090 <<
", subReadSize = " << subReadSize
3091 <<
", readTime = " << readTime <<
" seconds"
3095 H5Sclose(dataspace);
3099 for (
unsigned int tmpIndex = 0; tmpIndex < dataIn.size(); tmpIndex++) {
3100 free (dataIn[tmpIndex]);
3104 queso_error_msg(
"hdf file type not supported for multiple sub-environments yet");
3109 queso_error_msg(
"invalid file type");
3111 m_env.closeFile(unifiedFilePtrSet,fileType);
3114 m_env.inter0Comm().Barrier();
3119 for (
unsigned int i = 1; i < subReadSize; ++i) {
3120 m_seq[i] = tmpScalar;
3124 if (m_env.subDisplayFile()) {
3125 *m_env.subDisplayFile() <<
"Leaving ScalarSequence<T>::unifiedReadContents()"
3126 <<
", fileName = " << fileName
3141 for (
unsigned int i = 0; i < m_seq.size(); ++i) {
3142 m_seq[i] = src.
m_seq[i];
3144 deleteStoredScalars();
3152 unsigned int initialPos,
3153 unsigned int spacing,
3154 unsigned int numPos,
3159 for (
unsigned int j = 0; j < numPos; ++j) {
3169 scalarSeq[j] = m_seq[initialPos+j ];
3173 for (
unsigned int j = 0; j < numPos; ++j) {
3183 scalarSeq[j] = m_seq[initialPos+j*spacing];
3193 unsigned int initialPos,
3194 unsigned int spacing,
3195 unsigned int numPos,
3196 std::vector<double>& rawDataVec)
const
3198 rawDataVec.resize(numPos);
3200 for (
unsigned int j = 0; j < numPos; ++j) {
3201 rawDataVec[j] = m_seq[initialPos+j ];
3205 for (
unsigned int j = 0; j < numPos; ++j) {
3206 rawDataVec[j] = m_seq[initialPos+j*spacing];
3225 std::sort(m_seq.begin(), m_seq.end());
3234 std::vector<T>& sortedBuffer,
3235 const std::vector<T>& leafData,
3236 unsigned int currentTreeLevel)
const
3238 int parentNode = m_env.inter0Rank() & ~(1 << currentTreeLevel);
3240 if (m_env.inter0Rank() >= 0) {
3241 if (currentTreeLevel == 0) {
3243 unsigned int leafDataSize = leafData.size();
3244 sortedBuffer.resize(leafDataSize,0.);
3245 for (
unsigned int i = 0; i < leafDataSize; ++i) {
3246 sortedBuffer[i] = leafData[i];
3248 std::sort(sortedBuffer.begin(), sortedBuffer.end());
3249 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
3250 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::parallelMerge()"
3251 <<
": tree node " << m_env.inter0Rank()
3252 <<
", leaf sortedBuffer[0] = " << sortedBuffer[0]
3253 <<
", leaf sortedBuffer[" << sortedBuffer.size()-1 <<
"] = " << sortedBuffer[sortedBuffer.size()-1]
3258 int nextTreeLevel = currentTreeLevel - 1;
3259 int rightChildNode = m_env.inter0Rank() | (1 << nextTreeLevel);
3261 if (rightChildNode >= m_env.inter0Comm().NumProc()) {
3262 this->parallelMerge(sortedBuffer,
3267 unsigned int uintBuffer[1];
3268 uintBuffer[0] = nextTreeLevel;
3269 m_env.inter0Comm().Send((
void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, rightChildNode, SCALAR_SEQUENCE_INIT_MPI_MSG,
3270 "ScalarSequence<T>::parallelMerge()",
3271 "failed MPI.Send() for init");
3273 this->parallelMerge(sortedBuffer,
3278 unsigned int leftSize = sortedBuffer.size();
3279 std::vector<T> leftSortedBuffer(leftSize,0.);
3280 for (
unsigned int i = 0; i < leftSize; ++i) {
3281 leftSortedBuffer[i] = sortedBuffer[i];
3286 m_env.inter0Comm().Recv((
void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, rightChildNode, SCALAR_SEQUENCE_SIZE_MPI_MSG, &status,
3287 "ScalarSequence<T>::parallelMerge()",
3288 "failed MPI.Recv() for size");
3291 unsigned int rightSize = uintBuffer[0];
3292 std::vector<T> rightSortedBuffer(rightSize,0.);
3293 m_env.inter0Comm().Recv((
void *) &rightSortedBuffer[0], (
int) rightSize, RawValue_MPI_DOUBLE, rightChildNode, SCALAR_SEQUENCE_DATA_MPI_MSG, &status,
3294 "ScalarSequence<T>::parallelMerge()",
3295 "failed MPI.Recv() for data");
3298 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
3299 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::parallelMerge()"
3300 <<
": tree node " << m_env.inter0Rank()
3301 <<
" is combining " << leftSortedBuffer.size()
3302 <<
" left doubles with " << rightSortedBuffer.size()
3307 sortedBuffer.clear();
3308 sortedBuffer.resize(leftSortedBuffer.size()+rightSortedBuffer.size(),0.);
3312 while ((i < leftSize ) &&
3314 if (leftSortedBuffer[i] > rightSortedBuffer[j]) sortedBuffer[k++] = rightSortedBuffer[j++];
3315 else sortedBuffer[k++] = leftSortedBuffer [i++];
3317 while (i < leftSize ) sortedBuffer[k++] = leftSortedBuffer [i++];
3318 while (j < rightSize) sortedBuffer[k++] = rightSortedBuffer[j++];
3319 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 0)) {
3320 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::parallelMerge()"
3321 <<
": tree node " << m_env.inter0Rank()
3322 <<
", merged sortedBuffer[0] = " << sortedBuffer[0]
3323 <<
", merged sortedBuffer[" << sortedBuffer.size()-1 <<
"] = " << sortedBuffer[sortedBuffer.size()-1]
3329 if (parentNode != m_env.inter0Rank()) {
3331 unsigned int uintBuffer[1];
3332 uintBuffer[0] = sortedBuffer.size();
3333 m_env.inter0Comm().Send((
void *) uintBuffer, 1, RawValue_MPI_UNSIGNED, parentNode, SCALAR_SEQUENCE_SIZE_MPI_MSG,
3334 "ScalarSequence<T>::parallelMerge()",
3335 "failed MPI.Send() for size");
3337 if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 10)) {
3338 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::parallelMerge()"
3339 <<
": tree node " << m_env.inter0Rank()
3340 <<
" is sending " << sortedBuffer.size()
3341 <<
" doubles to tree node " << parentNode
3342 <<
", with sortedBuffer[0] = " << sortedBuffer[0]
3343 <<
" and sortedBuffer[" << sortedBuffer.size()-1 <<
"] = " << sortedBuffer[sortedBuffer.size()-1]
3347 m_env.inter0Comm().Send((
void *) &sortedBuffer[0], (
int) sortedBuffer.size(), RawValue_MPI_DOUBLE, parentNode, SCALAR_SEQUENCE_DATA_MPI_MSG,
3348 "ScalarSequence<T>::parallelMerge()",
3349 "failed MPI.Send() for data");
3361 #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
3365 unsigned int numEvaluationPoints,
3368 std::vector<T>& mdfValues)
const
3372 std::vector<T> centers(numEvaluationPoints,0.);
3373 std::vector<unsigned int> bins (numEvaluationPoints,0);
3376 this->subSequenceSize(),
3385 minDomainValue = centers[0];
3386 maxDomainValue = centers[centers.size()-1];
3387 T binSize = (maxDomainValue - minDomainValue)/((
double)(centers.size() - 1));
3389 unsigned int totalCount = 0;
3390 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
3391 totalCount += bins[i];
3395 mdfValues.resize(numEvaluationPoints);
3396 for (
unsigned int i = 0; i < numEvaluationPoints; ++i) {
3397 mdfValues[i] = ((T) bins[i])/((T) totalCount)/binSize;
3406 unsigned int initialPos,
3407 unsigned int numPos,
3408 const T& meanValue)
const
3410 if (this->subSequenceSize() == 0)
return 0.;
3412 bool bRC = ((initialPos < this->subSequenceSize()) &&
3414 ((initialPos+numPos) <= this->subSequenceSize()));
3415 queso_require_msg(bRC,
"invalid input data");
3417 unsigned int finalPosPlus1 = initialPos + numPos;
3420 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
3421 diff = m_seq[j] - meanValue;
3422 stdValue += diff*diff;
3425 stdValue /= (((T) numPos) - 1.);
3426 stdValue /= (((T) numPos) - 1.);
3427 stdValue = sqrt(stdValue);
3435 bool useOnlyInter0Comm,
3436 unsigned int initialPos,
3437 unsigned int numPos,
3438 const T& unifiedMeanValue)
const
3440 if (m_env.numSubEnvironments() == 1) {
3441 return this->subMeanCltStd(initialPos,
3448 T unifiedStdValue = 0.;
3449 if (useOnlyInter0Comm) {
3450 if (m_env.inter0Rank() >= 0) {
3451 bool bRC = ((initialPos < this->subSequenceSize()) &&
3453 ((initialPos+numPos) <= this->subSequenceSize()));
3454 queso_require_msg(bRC,
"invalid input data");
3456 unsigned int finalPosPlus1 = initialPos + numPos;
3458 T localStdValue = 0.;
3459 for (
unsigned int j = initialPos; j < finalPosPlus1; ++j) {
3460 diff = m_seq[j] - unifiedMeanValue;
3461 localStdValue += diff*diff;
3464 unsigned int unifiedNumPos = 0;
3465 m_env.inter0Comm().template Allreduce<unsigned int>(&numPos, &unifiedNumPos, (int) 1, RawValue_MPI_SUM,
3466 "ScalarSequence<T>::unifiedMeanCltStd()",
3467 "failed MPI.Allreduce() for numPos");
3469 m_env.inter0Comm().template Allreduce<double>(&localStdValue, &unifiedStdValue, (int) 1, RawValue_MPI_SUM,
3470 "ScalarSequence<T>::unifiedMeanCltStd()",
3471 "failed MPI.Allreduce() for stdValue");
3473 unifiedStdValue /= (((T) unifiedNumPos) - 1.);
3474 unifiedStdValue /= (((T) unifiedNumPos) - 1.);
3475 unifiedStdValue /= sqrt(unifiedStdValue);
3479 this->subMeanCltStd(initialPos,
3485 queso_error_msg(
"parallel vectors not supported yet");
3489 return unifiedStdValue;
3495 unsigned int initialPos,
3496 unsigned int batchLength)
const
3498 bool bRC = ((initialPos < this->subSequenceSize() ) &&
3499 (batchLength < (this->subSequenceSize()-initialPos)));
3500 queso_require_msg(bRC,
"invalid input data");
3502 unsigned int numberOfBatches = (this->subSequenceSize() - initialPos)/batchLength;
3505 for (
unsigned int batchId = 0; batchId < numberOfBatches; batchId++) {
3506 batchMeans[batchId] = this->subMeanExtra(initialPos + batchId*batchLength,
3531 unsigned int initialPos,
3532 unsigned int numBlocks,
3533 double hopSizeRatio,
3534 std::vector<double>& psdResult)
const
3536 bool bRC = ((initialPos < this->subSequenceSize() ) &&
3537 (hopSizeRatio != 0. ) &&
3538 (numBlocks < (this->subSequenceSize() - initialPos)) &&
3539 (fabs(hopSizeRatio) < (double) (this->subSequenceSize() - initialPos)));
3540 queso_require_msg(bRC,
"invalid input data");
3542 unsigned int dataSize = this->subSequenceSize() - initialPos;
3544 T meanValue = this->subMeanExtra(initialPos,
3548 unsigned int hopSize = 0;
3549 unsigned int blockSize = 0;
3550 if (hopSizeRatio <= -1.) {
3551 double overlapSize = -hopSizeRatio;
3552 double tmp = ((double) (dataSize + (numBlocks - 1)*overlapSize))/((double) numBlocks);
3553 blockSize = (
unsigned int) tmp;
3555 else if (hopSizeRatio < 0.) {
3556 double tmp = ((double) dataSize)/(( ((double) numBlocks) - 1. )*(-hopSizeRatio) + 1.);
3557 blockSize = (
unsigned int) tmp;
3558 hopSize = (
unsigned int) ( ((
double) blockSize) * (-hopSizeRatio) );
3560 else if (hopSizeRatio == 0.) {
3561 queso_error_msg(
"hopSizeRatio == 0");
3563 else if (hopSizeRatio < 1.) {
3564 double tmp = ((double) dataSize)/(( ((double) numBlocks) - 1. )*hopSizeRatio + 1.);
3565 blockSize = (
unsigned int) tmp;
3566 hopSize = (
unsigned int) ( ((
double) blockSize) * hopSizeRatio );
3569 hopSize = (
unsigned int) hopSizeRatio;
3570 blockSize = dataSize - (numBlocks - 1)*hopSize;
3573 int numberOfDiscardedDataElements = dataSize - (numBlocks-1)*hopSize - blockSize;
3585 queso_require_greater_equal_msg(numberOfDiscardedDataElements, 0.,
"eventual extra space for last block should not be negative");
3587 double tmp = log((
double) blockSize)/log(2.);
3588 double fractionalPart = tmp - ((double) ((
unsigned int) tmp));
3589 if (fractionalPart > 0.) tmp += (1. - fractionalPart);
3590 unsigned int fftSize = (
unsigned int) std::pow(2.,tmp);
3598 double modificationScale = 0.;
3599 for (
unsigned int j = 0; j < blockSize; ++j) {
3601 modificationScale += tmpValue*tmpValue;
3603 modificationScale = 1./modificationScale;
3605 std::vector<double> blockData(blockSize,0.);
3607 std::vector<std::complex<double> > fftResult(0,std::complex<double>(0.,0.));
3610 unsigned int halfFFTSize = fftSize/2;
3612 psdResult.resize(1+halfFFTSize,0.);
3613 double factor = 1./M_PI/((double) numBlocks);
3616 psdResult.resize(fftSize,0.);
3617 double factor = 1./2./M_PI/((double) numBlocks);
3620 for (
unsigned int blockId = 0; blockId < numBlocks; blockId++) {
3622 unsigned int initialDataPos = initialPos + blockId*hopSize;
3623 for (
unsigned int j = 0; j < blockSize; ++j) {
3624 unsigned int dataPos = initialDataPos + j;
3625 queso_require_less_msg(dataPos, dataSize,
"too large position to be accessed in data");
3626 blockData[j] =
MiscHammingWindow(blockSize-1,j) * ( m_seq[dataPos] - meanValue );
3629 fftObj.
forward(blockData,fftSize,fftResult);
3639 for (
unsigned int j = 0; j < psdResult.size(); ++j) {
3640 psdResult[j] += norm(fftResult[j])*modificationScale;
3644 for (
unsigned int j = 0; j < psdResult.size(); ++j) {
3645 psdResult[j] *= factor;
3654 unsigned int initialPos,
3656 double ratioNb)
const
3658 double doubleFullDataSize = (double) (this->subSequenceSize() - initialPos);
3660 std::vector<double> psdResult(0,0.);
3662 unsigned int dataSizeA = (
unsigned int) (doubleFullDataSize * ratioNa);
3663 double doubleDataSizeA = (double) dataSizeA;
3664 unsigned int initialPosA = initialPos;
3665 this->extractScalarSeq(initialPosA,
3672 (
unsigned int) std::sqrt((
double) dataSizeA),
3675 double psdA = psdResult[0];
3676 double varOfMeanA = 2.*M_PI*psdA/doubleDataSizeA;
3678 unsigned int dataSizeB = (
unsigned int) (doubleFullDataSize * ratioNb);
3679 double doubleDataSizeB = (double) dataSizeB;
3680 unsigned int initialPosB = this->subSequenceSize() - dataSizeB;
3681 this->extractScalarSeq(initialPosB,
3688 (
unsigned int) std::sqrt((
double) dataSizeB),
3691 double psdB = psdResult[0];
3692 double varOfMeanB = 2.*M_PI*psdB/doubleDataSizeB;
3694 if (m_env.subDisplayFile()) {
3695 *m_env.subDisplayFile() <<
"In ScalarSequence<T>::geweke()"
3696 <<
", before computation of gewCoef"
3698 <<
", dataSizeA = " << dataSizeA
3699 <<
", numBlocksA = " << (
unsigned int) std::sqrt((
double) dataSizeA)
3700 <<
", meanA = " << meanA
3701 <<
", psdA = " << psdA
3702 <<
", varOfMeanA = " << varOfMeanA
3704 <<
", dataSizeB = " << dataSizeB
3705 <<
", numBlocksB = " << (
unsigned int) std::sqrt((
double) dataSizeB)
3706 <<
", meanB = " << meanB
3707 <<
", psdB = " << psdB
3708 <<
", varOfMeanB = " << varOfMeanB
3711 double gewCoef = (meanA - meanB)/std::sqrt(varOfMeanA + varOfMeanB);
3719 unsigned int initialPos)
const
3721 queso_not_implemented();
3731 unsigned int initialPos,
3732 unsigned int numPos,
3735 T& upperValue)
const
3737 queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(),
"invalid input");
3739 queso_require_msg(!((range < 0) || (range > 1.)),
"invalid 'range' value");
3743 this->extractScalarSeq(initialPos,
3749 unsigned int lowerId = (
unsigned int) round( 0.5*(1.-range)*((double) numPos) );
3750 lowerValue = sortedSequence[lowerId];
3752 unsigned int upperId = (
unsigned int) round( 0.5*(1.+range)*((double) numPos) );
3753 if (upperId == numPos) {
3754 upperId = upperId-1;
3756 queso_require_less_msg(upperId, numPos,
"'upperId' got too big");
3757 upperValue = sortedSequence[upperId];
3765 bool useOnlyInter0Comm,
3766 unsigned int initialPos,
3767 unsigned int numPos,
3769 T& unifiedLowerValue,
3770 T& unifiedUpperValue)
const
3772 if (m_env.numSubEnvironments() == 1) {
3773 return this->subCdfPercentageRange(initialPos,
3782 queso_require_less_equal_msg((initialPos+numPos), this->subSequenceSize(),
"invalid input");
3784 queso_require_msg(!((range < 0) || (range > 1.)),
"invalid 'range' value");
3786 if (useOnlyInter0Comm) {
3787 if (m_env.inter0Rank() >= 0) {
3788 queso_error_msg(
"code not implemented yet");
3792 this->subCdfPercentageRange(initialPos,
3799 queso_error_msg(
"parallel vectors not supported yet");
3809 unsigned int initialPos,
3810 std::vector<double>& cdfStaccValues,
3811 std::vector<double>& cdfStaccValuesUp,
3812 std::vector<double>& cdfStaccValuesLow,
3815 queso_require_msg(!(
false),
"not implemented yet");
3816 bool bRC = (initialPos < this->subSequenceSize() );
3817 queso_require_msg(bRC,
"invalid input data");
3819 unsigned int numPoints = subSequenceSize()-initialPos;
3820 double auxNumPoints = numPoints;
3821 double maxLamb = 0.;
3822 std::vector<double> ro (numPoints,0.);
3823 std::vector<double> Isam_mat(numPoints,0.);
3825 for (
unsigned int pointId = 0; pointId < numPoints; pointId++) {
3826 double p = ( ((double) pointId) + 1.0 )/auxNumPoints;
3827 double ro0 = p*(1.0-p);
3828 cdfStaccValues[pointId] = p;
3833 for (
unsigned int k = 0; k < numPoints; k++) {
3834 if (m_seq[k] <= sortedDataValues[pointId]) {
3842 for (
unsigned int tau = 0; tau < (numPoints-1); tau++) {
3844 for (
unsigned int kk = 0; kk < numPoints-(tau+1); kk++) {
3845 ro[tau] += (Isam_mat[kk+tau+1]-p)*(Isam_mat[kk]-p);
3847 ro[tau] *= 1.0/auxNumPoints;
3850 for (
unsigned int tau = 0; tau < (numPoints-1); tau++) {
3851 double auxTau = tau;
3852 lamb += (1.-(auxTau+1.)/auxNumPoints)*ro[tau]/ro0;
3853 if (lamb > maxLamb) maxLamb = lamb;
3858 cdfStaccValuesUp [pointId] = cdfStaccValues[pointId]+1.96*sqrt(ro0/auxNumPoints*(1.+lamb));
3859 cdfStaccValuesLow[pointId] = cdfStaccValues[pointId]-1.96*sqrt(ro0/auxNumPoints*(1.+lamb));
3860 if (cdfStaccValuesLow[pointId] < 0.) cdfStaccValuesLow[pointId] = 0.;
3861 if (cdfStaccValuesUp [pointId] > 1.) cdfStaccValuesUp [pointId] = 1.;
3869 unsigned int dataSize = this->subSequenceSize() - initialPos;
3870 unsigned int numEvals = evaluationPositions.size();
3872 for (
unsigned int j = 0; j < numEvals; ++j) {
3873 double x = evaluationPositions[j];
3875 for (
unsigned int k = 0; k < dataSize; ++k) {
3876 double xk = m_seq[initialPos+k];
3879 cdfStaccValues[j] = value/(double) dataSize;
3889 unsigned int initialPos,
3890 const std::vector<T>& evaluationPositions,
3891 std::vector<double>& cdfStaccValues)
const
3893 queso_not_implemented();
3895 bool bRC = ((initialPos < this->subSequenceSize() ) &&
3896 (0 < evaluationPositions.size()) &&
3897 (evaluationPositions.size() == cdfStaccValues.size() ));
3898 queso_require_msg(bRC,
"invalid input data");
3907 unsigned int dataSize = this->subSequenceSize() - initialPos;
3908 unsigned int numEvals = evaluationPositions.size();
3910 for (
unsigned int j = 0; j < numEvals; ++j) {
3913 for (
unsigned int k = 0; k < dataSize; ++k) {
3917 cdfStaccValues[j] = value/(double) dataSize;
3923 #endif // #ifdef QUESO_COMPUTES_EXTRA_POST_PROCESSING_STATISTICS
3933 unsigned int initialPos,
3936 const std::vector<T>& evaluationPositions1,
3937 const std::vector<T>& evaluationPositions2,
3938 std::vector<double>& densityValues)
3942 double covValue = 0.;
3943 double corrValue = 0.;
3952 (0 < evaluationPositions1.size() ) &&
3953 (evaluationPositions1.size() == evaluationPositions2.size() ) &&
3954 (evaluationPositions1.size() == densityValues.size() ));
3955 queso_require_msg(bRC,
"invalid input data");
3958 unsigned int numEvals = evaluationPositions1.size();
3960 double scale1Inv = 1./scaleValue1;
3961 double scale2Inv = 1./scaleValue2;
3963 double r = 1. - corrValue*corrValue;
3965 std::cerr <<
"In ComputeSubGaussian2dKde()"
3966 <<
": WARNING, r = " << r
3973 for (
unsigned int j = 0; j < numEvals; ++j) {
3974 double x1 = evaluationPositions1[j];
3975 double x2 = evaluationPositions2[j];
3977 for (
unsigned int k = 0; k < dataSize; ++k) {
3978 double d1k = scale1Inv*(x1 - scalarSeq1[initialPos+k]);
3979 double d2k = scale2Inv*(x2 - scalarSeq2[initialPos+k]);
3980 value += exp(-.5*( d1k*d1k + 2*corrValue*d1k*d2k + d2k*d2k )/r);
3982 densityValues[j] = scale1Inv * scale2Inv * (value/(double) dataSize) / 2. / M_PI / sqrt(r);
3993 unsigned int initialPos,
3994 double unifiedScaleValue1,
3995 double unifiedScaleValue2,
3996 const std::vector<T>& unifiedEvaluationPositions1,
3997 const std::vector<T>& unifiedEvaluationPositions2,
3998 std::vector<double>& unifiedDensityValues)
4006 unifiedEvaluationPositions1,
4007 unifiedEvaluationPositions2,
4008 unifiedDensityValues);
4013 if (useOnlyInter0Comm) {
4015 queso_error_msg(
"inter0 case not supported yet");
4024 unifiedEvaluationPositions1,
4025 unifiedEvaluationPositions2,
4026 unifiedDensityValues);
4030 queso_error_msg(
"parallel vectors not supported yet");
4043 unsigned int subNumSamples,
4062 for (
unsigned k = 0; k < subNumSamples; ++k) {
4064 tmpP = subPSeq[k] - unifiedMeanP;
4065 tmpQ = subQSeq[k] - unifiedMeanQ;
4066 covValue += tmpP*tmpQ;
4082 unsigned int unifiedNumSamples = 0;
4083 env.
inter0Comm().template Allreduce<unsigned int>(&subNumSamples, &unifiedNumSamples, (int) 1, RawValue_MPI_SUM,
4084 "ComputeCovCorrBetweenScalarSequences()",
4085 "failed MPI.Allreduce() for subNumSamples");
4088 env.
inter0Comm().template Allreduce<double>(&covValue, &aux, (int) 1, RawValue_MPI_SUM,
4089 "ComputeCovCorrBetweenScalarSequences()",
4090 "failed MPI.Allreduce() for a matrix position");
4091 covValue = aux/((double) (unifiedNumSamples-1));
4093 corrValue = covValue/std::sqrt(unifiedSampleVarianceP)/std::sqrt(unifiedSampleVarianceQ);
4095 if ((corrValue < -1.) || (corrValue > 1.)) {
4096 std::cerr <<
"In ComputeCovCorrBetweenScalarSequences()"
4097 <<
": computed correlation is out of range, corrValue = " << corrValue
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 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).
const T & subMaxPlain() const
Finds the maximum value of the sub-sequence of scalars.
T subInterQuantileRange(unsigned int initialPos) const
Returns the interquartile range of the values in the sub-sequence.
const T & subMeanPlain() const
Finds the mean value of the sub-sequence of scalars.
ScalarSequence(const BaseEnvironment &env, unsigned int subSequenceSize, const std::string &name)
Default constructor.
void resizeSequence(unsigned int newSequenceSize)
Resizes the size of the sequence of scalars.
const T & unifiedMinPlain(bool useOnlyInter0Comm) const
Finds the minimum value of the unified sequence of scalars.
unsigned int unifiedSequenceSize(bool useOnlyInter0Comm) const
Size of the unified sequence of scalars.
~ScalarSequence()
Destructor.
const T & subMinPlain() const
Finds the minimum value of the sub-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...
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
void erasePositions(unsigned int initialPos, unsigned int numPos)
Erases numPos values of the sequence, starting at position initialPos.
T subMeanExtra(unsigned int initialPos, unsigned int numPos) const
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
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 ...
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.
T brooksGelmanConvMeasure(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int spacing) const
Estimates convergence rate using Brooks & Gelman method.
void writeUnifiedMatlabHeader(std::ofstream &ofs, double sequenceSize) const
Helper function to write header info for matlab files from all chains.
const T & unifiedMeanPlain(bool useOnlyInter0Comm) const
Finds the mean value of the unified sequence of scalars.
double MiscHammingWindow(unsigned int N, unsigned int j)
void unifiedCdfPercentageRange(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, double range, T &lowerValue, T &upperValue) const
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix queso_require_not_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the absence of an options input file"))
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...
const MpiComm & inter0Comm() const
Access function for MpiComm communicator for processes with subRank() 0.
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...
void unifiedWriteContents(const std::string &fileName, const std::string &fileType) const
Writes the unified sequence to a file.
std::vector< T >::const_iterator seqScalarPositionConstIteratorTypedef
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...
T unifiedMeanCltStd(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int localNumPos, const T &unifiedMeanValue) const
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 subHistogram(unsigned int initialPos, const T &minHorizontalValue, const T &maxHorizontalValue, std::vector< T > ¢ers, std::vector< unsigned int > &bins) const
Calculates the histogram of the sub-sequence.
void autoCorrViaFft(unsigned int initialPos, unsigned int numPos, unsigned int maxLag, std::vector< T > &autoCorrs) const
Calculates the autocorrelation via Fast Fourier transforms (FFT).
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.
void extractScalarSeq(unsigned int initialPos, unsigned int spacing, unsigned int numPos, ScalarSequence< T > &scalarSeq) const
Extracts a sequence of scalars.
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...
std::vector< T > & rawData()
The sequence of scalars. Access to private attribute m_seq.
void psd(unsigned int initialPos, unsigned int numBlocks, double hopSizeRatio, std::vector< double > &psdSequence) const
T unifiedPositionsOfMaximum(const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &unifiedPositionsOfMaximum)
Finds the positions where the maximum element occurs in the unified sequence.
void subCdfStacc(unsigned int initialPos, std::vector< double > &cdfStaccValues, std::vector< double > &cdfStaccValuesup, std::vector< double > &cdfStaccValueslow, const ScalarSequence< T > &sortedDataValues) const
void unifiedSort(bool useOnlyInter0Comm, unsigned int initialPos, ScalarSequence< T > &unifiedSortedSequence) const
Sorts the unified sequence of scalars.
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...
void subUniformlySampledMdf(unsigned int numIntervals, T &minDomainValue, T &maxDomainValue, std::vector< T > &mdfValues) const
void subSort()
Sorts the sequence of scalars in the private attribute m_seq.
int worldRank() const
Returns the same thing as fullRank()
double MiscGaussianDensity(double x, double mu, double sigma)
std::vector< T >::iterator seqScalarPositionIteratorTypedef
void writeSubMatlabHeader(std::ofstream &ofs, double sequenceSize) const
Helper function to write header info for matlab files from one chain.
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 ...
void clear()
Clears the sequence of scalars.
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)
void deleteStoredScalars()
Deletes all stored scalars.
void writeTxtHeader(std::ofstream &ofs, double sequenceSize) const
Helper function to write txt info for matlab files.
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.
const T & operator[](unsigned int posId) const
Access position posId of the sequence of scalars (const).
const T & subSampleVariancePlain() const
Finds the variance of a sample of the sub-sequence of scalars.
Class for handling scalar samples.
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 resetValues(unsigned int initialPos, unsigned int)
Sets numPos values of the sequence to zero, starting at position initialPos.
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.
void extractRawData(unsigned int initialPos, unsigned int spacing, unsigned int numPos, std::vector< double > &rawData) const
Extracts the raw data.
T autoCovariance(unsigned int initialPos, unsigned int numPos, const T &meanValue, unsigned int lag) const
Calculates the autocovariance.
Class for a Fast Fourier Transform (FFT) algorithm.
void ComputeCovCorrBetweenScalarSequences(const ScalarSequence< T > &subPSeq, const ScalarSequence< T > &subQSeq, unsigned int subNumSamples, T &covValue, T &corrValue)
void unifiedMinMaxExtra(bool useOnlyInter0Comm, unsigned int initialPos, unsigned int numPos, T &unifiedMinValue, T &unifiedMaxValue) const
Finds the minimum and the maximum values of the unified sequence, considering numPos positions starti...
std::ifstream * ifsVar
Provides a stream interface to read data from files.
T unifiedInterQuantileRange(bool useOnlyInter0Comm, unsigned int initialPos) const
Returns the interquartile range of the values in the unified sequence.
T bmm(unsigned int initialPos, unsigned int batchLength) const
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 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 unifiedUniformlySampledCdf(bool useOnlyInter0Comm, unsigned int numIntervals, T &unifiedMinDomainValue, T &unifiedMaxDomainValue, std::vector< T > &unifiedCdfValues) const
Uniformly samples from the CDF from the unified sequence.
const T & unifiedSampleVariancePlain(bool useOnlyInter0Comm) const
Finds the variance of a sample of the unified sequence of scalars.
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
T subMeanCltStd(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
const T & unifiedMaxPlain(bool useOnlyInter0Comm) const
Finds the maximum value of the unified sequence of scalars.
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 subSort(unsigned int initialPos, ScalarSequence< T > &sortedSequence) const
Sorts the sub-sequence of scalars.
void parallelMerge(std::vector< T > &sortedBuffer, const std::vector< T > &leafData, unsigned int treeLevel) const
Sorts/merges data in parallel using MPI.
T subMedianExtra(unsigned int initialPos, unsigned int numPos) const
Finds the median value of the sub-sequence, considering numPos positions starting at position initial...
void append(const ScalarSequence< T > &src, unsigned int srcInitialPos, unsigned int srcNumPos)
Appends the scalar sequence src to this sequence.
T meanStacc(unsigned int initialPos) const
Class for accommodating uniform one-dimensional grids.
T autoCorrViaDef(unsigned int initialPos, unsigned int numPos, unsigned int lag) const
Calculates the autocorrelation via definition.
const T & subMedianPlain() const
Finds the median value of the sub-sequence of scalars.
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...
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 subCdfPercentageRange(unsigned int initialPos, unsigned int numPos, double range, T &lowerValue, T &upperValue) const
void setName(const std::string &newName)
Sets a new name to the sequence of scalars.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
const BaseEnvironment & env() const
Access to QUESO environment.
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 getUnifiedContentsAtProc0Only(bool useOnlyInter0Comm, std::vector< T > &outputVec) const
Gets the unified contents of processor of rank equals to 0.
Struct for handling data input and output from files.
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.
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.
unsigned int numSubEnvironments() const
Access function to the number of sub-environments.
const T & unifiedMedianPlain(bool useOnlyInter0Comm) const
Finds the median value of the unified sequence of scalars.
T geweke(unsigned int initialPos, double ratioNa, double ratioNb) const
double MiscGetEllapsedSeconds(struct timeval *timeval0)
void unifiedReadContents(const std::string &fileName, const std::string &fileType, const unsigned int subSequenceSize)
Reads the unified sequence from a file.
MPI_Status RawType_MPI_Status
int inter0Rank() const
Returns the process inter0 rank.
T subPositionsOfMaximum(const ScalarSequence< T > &subCorrespondingScalarValues, ScalarSequence< T > &subPositionsOfMaximum)
Finds the positions where the maximum element occurs in the sub-sequence.
unsigned int subSequenceSize() const
Size of the sub-sequence of scalars.
T subSampleVarianceExtra(unsigned int initialPos, unsigned int numPos, const T &meanValue) const
Finds the sample variance of the sub-sequence, considering numPos positions starting at position init...
const std::string & name() const
Access to the name of the sequence of scalars.