Skip to content

Commit

Permalink
More tweaks to PointInPolygon benchmarking.
Browse files Browse the repository at this point in the history
  • Loading branch information
AngusJohnson committed Dec 1, 2023
1 parent ab6a16e commit 147b9b5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 64 deletions.
34 changes: 34 additions & 0 deletions CPP/BenchMark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,52 @@ set(benchmark_srcs
# more to add
)

set(CLIPPER2_INC
${CLIPPER2_INC_FOLDER}/clipper.h
${CLIPPER2_INC_FOLDER}/clipper.version.h
${CLIPPER2_INC_FOLDER}/clipper.core.h
)

add_library(Clipper2_bm INTERFACE)
target_include_directories(Clipper2_bm INTERFACE CLIPPER2_INC)

set(CLIPPER2_UTILS_INC
../Utils/clipper.svg.h
../Utils/ClipFileLoad.h
../Utils/ClipFileSave.h
../Utils/Timer.h
../Utils/Colors.h
../Utils/CommonUtils.h
)
set(CLIPPER2_UTILS_SRC
../Utils/clipper.svg.cpp
../Utils/ClipFileLoad.cpp
../Utils/ClipFileSave.cpp
)
set(CLIPPER2_UTILS "")
list(APPEND CLIPPER2_UTILS Clipper2utils_bm)
add_library(Clipper2utils_bm STATIC ${CLIPPER2_UTILS_INC} ${CLIPPER2_UTILS_SRC})
target_include_directories(Clipper2utils_bm
PUBLIC ../Clipper2Lib/include
PUBLIC ../Utils
)
target_link_libraries(Clipper2utils_bm PUBLIC Clipper2_bm)

# add each benchmark from the benchmark_srcs
foreach(benchmark ${benchmark_srcs})
get_filename_component(benchmark_target ${benchmark} NAME_WE)

message(STATUS "${PROJECT_NAME} add benchmark ${benchmark_target}")
add_executable(${benchmark_target} ${benchmark})

target_include_directories(${benchmark_target}
PUBLIC ../Clipper2Lib/include
PUBLIC ../Utils
)

target_link_libraries(${benchmark_target}
benchmark::benchmark
Clipper2_bm
Clipper2utils_bm
)
endforeach()
106 changes: 42 additions & 64 deletions CPP/BenchMark/PointInPolygonBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,66 +386,6 @@ static void DoErrorTest2(int index)
std::cout << " No errors found." << std::endl;
}

/////////////////////////////////////////////////////////
// Benchmark functions
/////////////////////////////////////////////////////////

// DoBenchmark1
/////////////////////////////////////////////////////////
static void DoBenchmark1()
{
// compare 3 PIP algorithms
pipResults.clear();
pipResults.resize(3);
for (size_t i = 0; i < 3; ++i) pipResults[i].resize(paths.size());
benchmark::Initialize(0, nullptr);
BENCHMARK(BM_PIP1)->Apply(CustomArguments); // current Clipper2
BENCHMARK(BM_PIP2)->Apply(CustomArguments); // modified Clipper2
BENCHMARK(BM_PIP3)->Apply(CustomArguments); // Hao et al. (2018)
benchmark::RunSpecifiedBenchmarks(benchmark::CreateDefaultDisplayReporter());
benchmark::ClearRegisteredBenchmarks();
benchmark::Shutdown();
}

// DoBenchmark2
/////////////////////////////////////////////////////////
static bool DoBenchmark2()
{
pipResults.clear();
pipResults.resize(3);
for (size_t i = 0; i < 3; ++i) pipResults[i].resize(paths.size());

benchmark::Initialize(0, nullptr);
BENCHMARK(BM_PIP1)->Apply(CustomArguments); // current Clipper2
BENCHMARK(BM_PIP2)->Apply(CustomArguments); // modified Clipper2
BENCHMARK(BM_PIP3)->Apply(CustomArguments); // Hao et al. (2018)
benchmark::RunSpecifiedBenchmarks(benchmark::CreateDefaultDisplayReporter());

std::cout << std::endl;
// compare results to ensure they all agree :)
bool result = true;
const std::string bad_filename = "test_pip_";
for (size_t i = 0; i < pipResults[0].size(); ++i)
{
if ((pipResults[0][i] == pipResults[1][i]) &&
(pipResults[0][i] == pipResults[2][i])) continue;

if (pipResults[0][i] != pipResults[1][i])
std::cout << "PIP2 returned the " << SetConsoleTextColor(red_bold) << "wrong " <<
SetConsoleTextColor(reset) << "result:" << std::endl;
if (pipResults[0][i] != pipResults[2][i])
std::cout << "PIP3 returned the " << SetConsoleTextColor(red_bold) << "wrong " <<
SetConsoleTextColor(reset) << "result:" << std::endl;

std::cout << "Problematic PIP path saved to - " << bad_filename << i << ".txt" << std::endl;
std::ofstream of(bad_filename);
of << paths[i] << std::endl;
of.close();
result = false;
}
return true;
}

/////////////////////////////////////////////////////////
// Main Entry
/////////////////////////////////////////////////////////
Expand Down Expand Up @@ -524,7 +464,8 @@ int main(int argc, char** argv)
int width = 600000, height = 400000;
const int power10_lo = 4, power10_high = 8;
mp = Point64(width / 2, height / 2);



std::cout << std::endl << SetConsoleTextColor(yellow_bold) <<
"Benchmarks 1:" << SetConsoleTextColor(reset) << std::endl;
//////////////////////////////////////////////////////////////
Expand All @@ -535,7 +476,16 @@ int main(int argc, char** argv)
"Edge counts between 10^" << power10_lo << " and 10^" <<
power10_high << std::endl << std::endl;

DoBenchmark1();
pipResults.clear();
pipResults.resize(3);
for (size_t i = 0; i < 3; ++i) pipResults[i].resize(paths.size());

benchmark::Initialize(0, nullptr);
BENCHMARK(BM_PIP1)->Apply(CustomArguments); // current Clipper2
BENCHMARK(BM_PIP2)->Apply(CustomArguments); // modified Clipper2
BENCHMARK(BM_PIP3)->Apply(CustomArguments); // Hao et al. (2018)
benchmark::RunSpecifiedBenchmarks(benchmark::CreateDefaultDisplayReporter());
benchmark::ClearRegisteredBenchmarks();
std::cout << std::endl;


Expand All @@ -551,8 +501,36 @@ int main(int argc, char** argv)
paths.clear();
for (int i = power10_lo; i <= power10_high; ++i)
paths.push_back(MakeRandomPoly(width, height, (unsigned)std::pow(10, i)));
DoBenchmark2();

pipResults.clear();
pipResults.resize(3);
for (size_t i = 0; i < 3; ++i) pipResults[i].resize(paths.size());
benchmark::Initialize(0, nullptr);
BENCHMARK(BM_PIP1)->Apply(CustomArguments); // current Clipper2
BENCHMARK(BM_PIP2)->Apply(CustomArguments); // modified Clipper2
BENCHMARK(BM_PIP3)->Apply(CustomArguments); // Hao et al. (2018)
benchmark::RunSpecifiedBenchmarks(benchmark::CreateDefaultDisplayReporter());

std::cout << std::endl;
// compare results to ensure they all agree :)
const std::string bad_filename = "test_pip_";
for (size_t i = 0; i < pipResults[0].size(); ++i)
{
if ((pipResults[0][i] == pipResults[1][i]) &&
(pipResults[0][i] == pipResults[2][i])) continue;

if (pipResults[0][i] != pipResults[1][i])
std::cout << "PIP2 returned the " << SetConsoleTextColor(red_bold) << "wrong " <<
SetConsoleTextColor(reset) << "result:" << std::endl;
if (pipResults[0][i] != pipResults[2][i])
std::cout << "PIP3 returned the " << SetConsoleTextColor(red_bold) << "wrong " <<
SetConsoleTextColor(reset) << "result:" << std::endl;

std::cout << "Problematic PIP path saved to - " << bad_filename << i << ".txt" << std::endl;
std::ofstream of(bad_filename);
of << paths[i] << std::endl;
of.close();
break;
}

return 0;
}

0 comments on commit 147b9b5

Please sign in to comment.