From 33a2c6a5c7ec92c72b8828ca6d77f9ee4ad45f22 Mon Sep 17 00:00:00 2001 From: Simon Vidanovic Date: Tue, 16 Aug 2022 09:25:40 -0700 Subject: [PATCH 1/4] CMatrixSeries interpolation function is parallelized. --- src/Common/src/MatrixSeries.cpp | 32 ++++++++++++++++--- .../src/EquivalentBSDFLayer.cpp | 15 --------- .../src/EquivalentBSDFLayer.hpp | 31 ++++-------------- src/MultiLayerOptics/src/MultiPaneBSDF.cpp | 3 +- 4 files changed, 34 insertions(+), 47 deletions(-) diff --git a/src/Common/src/MatrixSeries.cpp b/src/Common/src/MatrixSeries.cpp index 695f8fe9..f7ea8edf 100644 --- a/src/Common/src/MatrixSeries.cpp +++ b/src/Common/src/MatrixSeries.cpp @@ -1,16 +1,20 @@ #include #include +#include + #include "MatrixSeries.hpp" #include "SquareMatrix.hpp" #include "Series.hpp" #include "IntegratorStrategy.hpp" +#include "Utility.hpp" namespace FenestrationCommon { CMatrixSeries::CMatrixSeries(const size_t t_Size1, const size_t t_Size2, size_t seriesSize) : - m_Size1(t_Size1), m_Size2(t_Size2) + m_Size1(t_Size1), + m_Size2(t_Size2) { m_Matrix = std::vector>(m_Size1); for(size_t i = 0; i < m_Size1; ++i) @@ -122,7 +126,6 @@ namespace FenestrationCommon { for(size_t j = 0; j < m_Matrix[i].size(); ++j) { - // assert( t_Series[ i ]->size() == ( *m_Matrix[ i ][ j ] ).size() ); m_Matrix[i][j] = m_Matrix[i][j] * t_Series[i]; } } @@ -149,13 +152,32 @@ namespace FenestrationCommon void CMatrixSeries::interpolate(const std::vector & t_Wavelengths) { - for(size_t i = 0; i < m_Matrix.size(); ++i) + for(size_t i = 0u; i < m_Matrix.size(); ++i) { - for(size_t j = 0; j < m_Matrix[i].size(); ++j) + auto numberOfThreads{1u}; +#if MULTITHREADING + numberOfThreads = std::thread::hardware_concurrency(); +#endif + const auto chunks{ + FenestrationCommon::chunkIt(0u, m_Matrix[i].size() - 1u, numberOfThreads)}; + + std::vector workers; + for(const auto & chunk : chunks) { - m_Matrix[i][j] = m_Matrix[i][j].interpolate(t_Wavelengths); + workers.emplace_back([&]() { + for(size_t j = chunk.start; j < chunk.end; ++j) + { + m_Matrix[i][j] = m_Matrix[i][j].interpolate(t_Wavelengths); + } + }); + } + + for(auto & worker : workers) + { + worker.join(); } } + } std::vector> diff --git a/src/MultiLayerOptics/src/EquivalentBSDFLayer.cpp b/src/MultiLayerOptics/src/EquivalentBSDFLayer.cpp index 8e272de9..189d2364 100644 --- a/src/MultiLayerOptics/src/EquivalentBSDFLayer.cpp +++ b/src/MultiLayerOptics/src/EquivalentBSDFLayer.cpp @@ -102,15 +102,6 @@ namespace MultiLayerOptics return m_Layer.size(); } - void CEquivalentBSDFLayer::setMatrixLayerWavelengths(const std::vector & wavelenghts) - { - m_CombinedLayerWavelengths = wavelenghts; - for(const auto & layer : m_Layer) - { - layer->setBandWavelengths(wavelenghts); - } - } - void CEquivalentBSDFLayer::calculate() { const size_t matrixSize = m_Lambda.size(); @@ -134,12 +125,6 @@ namespace MultiLayerOptics void CEquivalentBSDFLayer::calculateWavelengthByWavelengthProperties() { - std::vector wavelengthIndexes; - for(size_t i = 0; i < m_CombinedLayerWavelengths.size(); ++i) - { - wavelengthIndexes.push_back(i); - } - std::mutex absorptanceMutex; std::mutex jscMutex; std::mutex totMutex; diff --git a/src/MultiLayerOptics/src/EquivalentBSDFLayer.hpp b/src/MultiLayerOptics/src/EquivalentBSDFLayer.hpp index 948b58d8..aa107293 100644 --- a/src/MultiLayerOptics/src/EquivalentBSDFLayer.hpp +++ b/src/MultiLayerOptics/src/EquivalentBSDFLayer.hpp @@ -32,43 +32,25 @@ namespace MultiLayerOptics [[nodiscard]] double getMaxLambda() const; // Absorptance wavelength by wavelength matrices - FenestrationCommon::CMatrixSeries getTotalA(const FenestrationCommon::Side t_Side); + FenestrationCommon::CMatrixSeries getTotalA(FenestrationCommon::Side t_Side); // Photovoltaic current (scaled to income irradiance equal to one) FenestrationCommon::CMatrixSeries getTotalJSC(FenestrationCommon::Side t_Side); // Transmittance and reflectance wavelength by wavelength matrices - FenestrationCommon::CMatrixSeries - getTotal(const FenestrationCommon::Side t_Side, - const FenestrationCommon::PropertySimple t_Property); + FenestrationCommon::CMatrixSeries getTotal(FenestrationCommon::Side t_Side, + FenestrationCommon::PropertySimple t_Property); void setSolarRadiation(FenestrationCommon::CSeries & t_SolarRadiation); [[nodiscard]] std::vector> & getLayers(); [[nodiscard]] size_t numberOfLayers() const; - void setMatrixLayerWavelengths(const std::vector & wavelenghts); - - private: - struct wavelenghtData - { - wavelenghtData(double wl, CEquivalentBSDFLayerSingleBand & layerWl) : - wavelength(wl), - layer(layerWl) - {} - - double wavelength; - CEquivalentBSDFLayerSingleBand & layer; - std::map, std::vector> totA; - std::map, std::vector> totJSC; - std::map, - FenestrationCommon::SquareMatrix> - tot; - }; - void calculate(); - CEquivalentBSDFLayerSingleBand getEquivalentLayerAtWavelength(size_t wavelengthIndex) const; + private: + [[nodiscard]] CEquivalentBSDFLayerSingleBand + getEquivalentLayerAtWavelength(size_t wavelengthIndex) const; static std::vector unionOfLayerWavelengths( const std::vector> & t_Layer); @@ -97,7 +79,6 @@ namespace MultiLayerOptics bool m_Calculated; void calculateWavelengthByWavelengthProperties(); - }; } // namespace MultiLayerOptics diff --git a/src/MultiLayerOptics/src/MultiPaneBSDF.cpp b/src/MultiLayerOptics/src/MultiPaneBSDF.cpp index 88236c5a..29b1cc2f 100644 --- a/src/MultiLayerOptics/src/MultiPaneBSDF.cpp +++ b/src/MultiLayerOptics/src/MultiPaneBSDF.cpp @@ -1,11 +1,9 @@ #include #include -#include #include #include #include "MultiPaneBSDF.hpp" -#include "EquivalentBSDFLayer.hpp" #include "EquivalentBSDFLayerSingleBand.hpp" #include "WCESingleLayerOptics.hpp" #include "WCECommon.hpp" @@ -112,6 +110,7 @@ namespace MultiLayerOptics m_IncomingSolar.push_back(iTotalSolar.sum(minLambda, maxLambda)); } + // Produce local results matrices for each side and property std::map, SquareMatrix> aResults; From ec245b74ccdb7a05cbcdbdd1b886f3181caf884a Mon Sep 17 00:00:00 2001 From: Simon Vidanovic Date: Tue, 16 Aug 2022 09:38:15 -0700 Subject: [PATCH 2/4] CMatrixSeries integration function is parallelized. --- src/Common/src/MatrixSeries.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Common/src/MatrixSeries.cpp b/src/Common/src/MatrixSeries.cpp index f7ea8edf..4763ae2a 100644 --- a/src/Common/src/MatrixSeries.cpp +++ b/src/Common/src/MatrixSeries.cpp @@ -142,10 +142,28 @@ namespace FenestrationCommon { for(size_t i = 0; i < m_Matrix.size(); ++i) { - for(size_t j = 0; j < m_Matrix[i].size(); ++j) + auto numberOfThreads{1u}; +#if MULTITHREADING + numberOfThreads = std::thread::hardware_concurrency(); +#endif + const auto chunks{ + FenestrationCommon::chunkIt(0u, m_Matrix[i].size() - 1u, numberOfThreads)}; + + std::vector workers; + for(const auto & chunk : chunks) { - m_Matrix[i][j] = m_Matrix[i][j].integrate( - t_Integration, normalizationCoefficient, integrationPoints); + workers.emplace_back([&]() { + for(size_t j = chunk.start; j < chunk.end; ++j) + { + m_Matrix[i][j] = m_Matrix[i][j].integrate( + t_Integration, normalizationCoefficient, integrationPoints); + } + }); + } + + for(auto & worker : workers) + { + worker.join(); } } } @@ -177,7 +195,6 @@ namespace FenestrationCommon worker.join(); } } - } std::vector> From 6a0bec344555d46d0c5bf38779e9b68e210adfbf Mon Sep 17 00:00:00 2001 From: Simon Vidanovic Date: Tue, 16 Aug 2022 09:51:48 -0700 Subject: [PATCH 3/4] Added comment on CMatrixSeris mMult parallelization. (No improvement) --- src/Common/src/MatrixSeries.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Common/src/MatrixSeries.cpp b/src/Common/src/MatrixSeries.cpp index 4763ae2a..1cca1525 100644 --- a/src/Common/src/MatrixSeries.cpp +++ b/src/Common/src/MatrixSeries.cpp @@ -110,6 +110,7 @@ namespace FenestrationCommon void CMatrixSeries::mMult(const CSeries & t_Series) { + // Parallelization here did not show any improvements. Program was performing even slower. for(size_t i = 0; i < m_Matrix.size(); ++i) { for(size_t j = 0; j < m_Matrix[i].size(); ++j) @@ -122,6 +123,7 @@ namespace FenestrationCommon void CMatrixSeries::mMult(const std::vector & t_Series) { + // Parallelization here did not show any improvements. Program was performing even slower. for(size_t i = 0; i < m_Matrix.size(); ++i) { for(size_t j = 0; j < m_Matrix[i].size(); ++j) From df29932eaf23f0ccad51c0b4ac20f57a8e046882 Mon Sep 17 00:00:00 2001 From: Simon Vidanovic Date: Tue, 16 Aug 2022 10:17:32 -0700 Subject: [PATCH 4/4] Update to version number in CMake file. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48765a9f..07949a9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) -project(Windows-CalcEngine VERSION 1.0.32 LANGUAGES CXX) +project(Windows-CalcEngine VERSION 1.0.33 LANGUAGES CXX) set(target_name ${PROJECT_NAME})