Refactor PartitionedMatrixView to cache the partitions

The constructor now takes a LinearSolver::Options as input
and uses that to compute the partitioning once and uses it
for its lifetime.

Change-Id: I9ef30df0b60f8fa91c8b5601c397b2d9314a2cc7
This commit is contained in:
Sameer Agarwal
2022-11-14 11:40:49 -08:00
parent d3201798ea
commit e4bef95054
11 changed files with 300 additions and 577 deletions
+36
View File
@@ -299,6 +299,42 @@ void ParallelFor(ContextImpl* context,
});
}
// Execute function for every element in the range [start, end) with at most
// num_threads, using the user provided partitioning. taking into account
// user-provided integer cumulative costs of iterations.
template <typename F>
void ParallelFor(ContextImpl* context,
int start,
int end,
int num_threads,
const F& function,
const std::vector<int>& partitions) {
using namespace parallel_for_details;
CHECK_GT(num_threads, 0);
if (start >= end) {
return;
}
CHECK_EQ(partitions.front(), start);
CHECK_EQ(partitions.back(), end);
if (num_threads == 1 || end - start <= num_threads) {
ParallelFor(context, start, end, num_threads, function);
return;
}
CHECK_GT(partitions.size(), 1);
const int num_partitions = partitions.size() - 1;
ParallelFor(context,
0,
num_partitions,
num_threads,
[&function, &partitions](int thread_id, int partition_id) {
const int partition_start = partitions[partition_id];
const int partition_end = partitions[partition_id + 1];
for (int i = partition_start; i < partition_end; ++i) {
Invoke<F>(thread_id, i, function);
}
});
}
} // namespace ceres::internal
// Backend-specific implementations of ParallelInvoke