Skip to content

Commit

Permalink
Merge pull request #80 from pierotofy/parallel
Browse files Browse the repository at this point in the history
Parallel image loading
  • Loading branch information
pierotofy authored Apr 19, 2024
2 parents ef0461f + 5914c22 commit f38b97c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions opensplat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion utils.cpp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include "utils.hpp"
#include "utils.hpp"
26 changes: 26 additions & 0 deletions utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <algorithm>
#include <random>
#include <iostream>
#include <thread>
#include <functional>

template <typename T>
class InfiniteRandomIterator
Expand Down Expand Up @@ -32,4 +34,28 @@ class InfiniteRandomIterator
std::default_random_engine engine;
};

template <typename IndexType, typename FuncType>
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<size_t>(std::thread::hardware_concurrency()), range);
size_t chunkSize = (range + numThreads - 1) / numThreads;
std::vector<std::thread> 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

0 comments on commit f38b97c

Please sign in to comment.