From 5914c22a398e442f2c50c20939fa16e997e67da2 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Fri, 19 Apr 2024 18:09:28 -0400 Subject: [PATCH] Parallel image loading --- opensplat.cpp | 4 ++-- utils.cpp | 2 +- utils.hpp | 26 ++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/opensplat.cpp b/opensplat.cpp index ccf4336..5819b81 100644 --- a/opensplat.cpp +++ b/opensplat.cpp @@ -94,9 +94,9 @@ int main(int argc, char *argv[]){ try{ InputData inputData = inputDataFromX(projectRoot); - for (Camera &cam : inputData.cameras){ + parallel_for(inputData.cameras.begin(), inputData.cameras.end(), [&downScaleFactor](Camera &cam){ cam.loadImage(downScaleFactor); - } + }); // Withhold a validation camera if necessary auto t = inputData.getCameras(validate, valImage); diff --git a/utils.cpp b/utils.cpp index 0c58fd6..0ee624c 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1 +1 @@ -#include "utils.hpp" \ No newline at end of file +#include "utils.hpp" diff --git a/utils.hpp b/utils.hpp index f2b1a21..948b9d1 100644 --- a/utils.hpp +++ b/utils.hpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include template class InfiniteRandomIterator @@ -32,4 +34,28 @@ class InfiniteRandomIterator std::default_random_engine engine; }; +template +void parallel_for(IndexType begin, IndexType end, FuncType func) { + size_t range = end - begin; + if (range <= 0) return; + size_t numThreads = (std::min)(static_cast(std::thread::hardware_concurrency()), range); + size_t chunkSize = (range + numThreads - 1) / numThreads; + std::vector threads; + + for (unsigned int i = 0; i < numThreads; i++) { + IndexType chunkBegin = begin + i * chunkSize; + IndexType chunkEnd = (std::min)(chunkBegin + chunkSize, end); + + threads.emplace_back([chunkBegin, chunkEnd, &func]() { + for (IndexType item = chunkBegin; item < chunkEnd; item++) { + func(*item); + } + }); + } + + for (std::thread& t : threads) { + t.join(); + } +} + #endif \ No newline at end of file