Skip to content

Commit

Permalink
added more gtests
Browse files Browse the repository at this point in the history
  • Loading branch information
LBern committed Jul 30, 2020
1 parent 1e61267 commit a8f71fb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ size_t parallelProcess(
// Nothing todo.
return 0u;
}

const size_t num_processing_threads = std::min(num_elements, num_threads);
VLOG(1) << "num_processing_threads vs num_threads: " << num_processing_threads
<< " vs " << num_threads;
const size_t num_elements_per_thread =
std::ceil(static_cast<double>(num_elements) / num_threads);
std::ceil(static_cast<double>(num_elements) / num_processing_threads);

// If we have less elements than threads, we simply run one thread.
std::vector<std::thread> thread_pool;
for (size_t thread_idx = 0u; thread_idx < num_threads; ++thread_idx) {
for (size_t thread_idx = 0u; thread_idx < num_processing_threads;
++thread_idx) {
const size_t start_idx =
global_start_index + num_elements_per_thread * thread_idx;

// This is needed when there are more threads than elements.
if (start_idx >= global_end_index) {
// Just add this as a sanity check.
CHECK_GT(num_elements, num_threads);
CHECK_GT(num_elements, num_processing_threads);
break;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "dense-mapping/dense-mapping.h"

#include <atomic>
#include <chrono>
#include <functional>
#include <thread>
Expand Down Expand Up @@ -114,21 +115,73 @@ TEST_F(DenseMappingTest, TestDenseMapping) {
LOG(INFO) << timing::Timing::Print();
}

TEST_F(DenseMappingTest, TestParallelProcess) {
const std::size_t num_threads = 10;
TEST_F(DenseMappingTest, TestParallelProcessEqualThreads) {
const std::size_t num_threads = 4;
const std::size_t start = 0;
const std::size_t end = 10;
const std::size_t end = 4;
std::atomic<std::size_t> thread_accum{0u};
std::function<void(std::size_t, std::size_t, std::size_t)> functor =
[start, end](
[start, end, &thread_accum](
std::size_t thread_idx, std::size_t start_idx, std::size_t end_idx) {
EXPECT_GE(start_idx, start);
EXPECT_LE(end_idx, end);
EXPECT_GE(thread_idx, start_idx);
EXPECT_LE(thread_idx, end_idx);
++thread_accum;
};

std::size_t actual_num_threads = parallelProcess(functor, 0, 10, num_threads);
const std::size_t actual_num_threads =
parallelProcess(functor, start, end, num_threads);
EXPECT_EQ(actual_num_threads, num_threads);
EXPECT_EQ(thread_accum.load(), end - start);
}

TEST_F(DenseMappingTest, TestParallelProcessLessThreads) {
const std::size_t num_threads = 4;
const std::size_t start = 0;
const std::size_t end = 6;
std::atomic<std::size_t> thread_accum{0u};
std::function<void(std::size_t, std::size_t, std::size_t)> functor =
[start, end, &thread_accum](
std::size_t thread_idx, std::size_t start_idx, std::size_t end_idx) {
EXPECT_GE(start_idx, start);
EXPECT_LE(end_idx, end);
EXPECT_GE(thread_idx, start);
EXPECT_LE(thread_idx, end);
for (std::size_t i = start_idx; i < end_idx; ++i) {
++thread_accum;
}
};

const std::size_t actual_num_threads =
parallelProcess(functor, start, end, num_threads);
EXPECT_GT(actual_num_threads, 0);
EXPECT_LE(actual_num_threads, num_threads);
EXPECT_EQ(thread_accum.load(), end - start);
}

TEST_F(DenseMappingTest, TestParallelProcessMoreThreads) {
const std::size_t num_threads = 4;
const std::size_t start = 0;
const std::size_t end = 2;
std::atomic<std::size_t> thread_accum{0u};
std::function<void(std::size_t, std::size_t, std::size_t)> functor =
[start, end, &thread_accum](
std::size_t thread_idx, std::size_t start_idx, std::size_t end_idx) {
EXPECT_GE(start_idx, start);
EXPECT_LE(end_idx, end);
EXPECT_GE(thread_idx, start);
EXPECT_LE(thread_idx, end);
for (std::size_t i = start_idx; i < end_idx; ++i) {
++thread_accum;
}
};

const std::size_t actual_num_threads =
parallelProcess(functor, start, end, num_threads);
EXPECT_GT(actual_num_threads, 0);
EXPECT_LE(actual_num_threads, num_threads);
EXPECT_EQ(thread_accum.load(), end - start);
}

} // namespace dense_mapping
Expand Down

0 comments on commit a8f71fb

Please sign in to comment.