Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel image loading #80

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading