Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kuro337 committed Jan 20, 2024
0 parents commit 421b839
Show file tree
Hide file tree
Showing 8 changed files with 1,165 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.cache
main.cc
build
read.md
images
117 changes: 117 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
cmake_minimum_required(VERSION 3.20)
project(opencvOCR)

# change standard as required
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


find_package(OpenCV REQUIRED)

find_package(OpenSSL REQUIRED)

find_package(OpenMP)

find_package(Folly CONFIG REQUIRED)

find_package(gflags CONFIG REQUIRED)

find_package(CURL REQUIRED)

include_directories("/opt/homebrew/opt/tesseract/include")
link_directories("/opt/homebrew/opt/tesseract/lib")

# gtest
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # windows setting
FetchContent_MakeAvailable(googletest)


# main

add_executable(
main
main.cc
)

target_link_libraries(main
PUBLIC GTest::gtest_main
PUBLIC Folly::folly
PUBLIC OpenSSL::Crypto
PUBLIC tesseract
PUBLIC ${OpenCV_LIBS}
PUBLIC ${CURL_LIBRARIES}
)

if(OpenMP_CXX_FOUND)
message(STATUS "Using OpenMP")
target_link_libraries(main PUBLIC OpenMP::OpenMP_CXX)
else()
message(STATUS "OpenMP not found")

endif()

# tests


enable_testing()

add_executable(ocr_test tests/ocr_test.cc)
add_executable(similarity_test tests/similarity_test.cc)
add_executable(cache_benchmark benchmarks/cache_benchmark.cc)

target_link_libraries(
cache_benchmark
PUBLIC GTest::gtest_main
PUBLIC ${CURL_LIBRARIES}
PUBLIC ${OpenCV_LIBS}
PUBLIC OpenSSL::Crypto
PUBLIC tesseract
PUBLIC Folly::folly
PUBLIC Folly::follybenchmark
)


target_link_libraries(
ocr_test
PUBLIC GTest::gtest_main
PUBLIC ${CURL_LIBRARIES}
PUBLIC ${OpenCV_LIBS}
PUBLIC OpenSSL::Crypto
PUBLIC tesseract
PUBLIC Folly::folly
)

target_link_libraries(
similarity_test
PUBLIC GTest::gtest_main
PUBLIC ${CURL_LIBRARIES}
PUBLIC ${OpenCV_LIBS}
PUBLIC OpenSSL::Crypto
PUBLIC tesseract
PUBLIC Folly::folly
)



include(GoogleTest)
gtest_discover_tests(ocr_test)
gtest_discover_tests(similarity_test)


if(OpenMP_CXX_FOUND)
message(STATUS "Using OpenMP")
target_link_libraries(ocr_test PUBLIC OpenMP::OpenMP_CXX)
target_link_libraries(similarity_test PUBLIC OpenMP::OpenMP_CXX)
else()
message(STATUS "OpenMP not found")

endif()

Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
155 changes: 155 additions & 0 deletions benchmarks/cache_benchmark.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <folly/AtomicUnorderedMap.h>
#include <folly/Benchmark.h>
#include <folly/concurrency/ConcurrentHashMap.h>
#include <folly/init/Init.h>

/*
1 second: 1
1 millisecond (ms): 0.001 seconds
1 microsecond (us): 0.000001 seconds
1 nanosecond (ns): 0.000000001 seconds
*/

void mutexMapBenchmark(int threadCount, int insertionsPerThread) {
std::unordered_map<int, std::string> map;
std::mutex mapMutex;

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; ++i) {
threads.emplace_back([&map, &mapMutex, i, insertionsPerThread] {
for (int j = 1; j <= insertionsPerThread; ++j) {
int key = i * insertionsPerThread + j;
std::string value = "Value " + std::to_string(key);

std::lock_guard<std::mutex> lock(mapMutex);
map[key] = std::move(value);
}
});
}

for (auto &t : threads) {
t.join();
}
}

void concurrentMapBenchmark(int threadCount, int insertionsPerThread) {
folly::ConcurrentHashMap<int, std::string> map;

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; ++i) {
threads.emplace_back([&map, i, insertionsPerThread] {
for (int j = 0; j < insertionsPerThread; ++j) {
map.insert_or_assign(j, "Value " + std::to_string(i * 100 + j));
}
});
}

for (auto &t : threads) {
t.join();
}
}

void concurrentMapBenchmarkComplex(int threadCount, int insertionsPerThread) {
auto v = std::vector<std::string>{"Complex", "Data", "Type"};

folly::ConcurrentHashMap<std::string, std::vector<std::string>> map;

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; ++i) {
threads.emplace_back([&map, &v, i, insertionsPerThread] {
for (int j = 1; j <= insertionsPerThread; ++j) {
std::string key = "Key" + std::to_string(i * insertionsPerThread + j);
map.insert_or_assign(std::move(key), v);
}
});
}

for (auto &t : threads) {
t.join();
}
}

void atomicMapBenchmark(int threadCount, int insertionsPerThread) {
folly::AtomicUnorderedInsertMap<int, std::string> atomicMap(
threadCount * insertionsPerThread);

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; ++i) {
threads.emplace_back([&atomicMap, i, insertionsPerThread] {
for (int j = 1; j <= insertionsPerThread; ++j) {
int key = i * insertionsPerThread + j;
atomicMap.emplace(key, "Value " + std::to_string(key));
}
});
}

for (auto &t : threads) {
t.join();
}
}

void atomicMapBenchmarkComplex(int threadCount, int insertionsPerThread) {
auto v = std::vector<std::string>{"Complex", "Data", "Type"};

folly::AtomicUnorderedInsertMap<std::string, std::vector<std::string>>
atomicMap(threadCount * insertionsPerThread);

std::vector<std::thread> threads;
for (int i = 0; i < threadCount; ++i) {
threads.emplace_back([&atomicMap, &v, i, insertionsPerThread] {
for (int j = 1; j <= insertionsPerThread; ++j) {
std::string key = "Key" + std::to_string(i * insertionsPerThread + j);
atomicMap.emplace(std::move(key), v);
}
});
}

for (auto &t : threads) {
t.join();
}
}

BENCHMARK(UnorderedMapMutexedSingleThreaded, n) { mutexMapBenchmark(1, n); }
BENCHMARK(UnorderedMapMutexedMultiThreaded, n) { mutexMapBenchmark(5, n); }
BENCHMARK(UnorderedMapMutexedMaxThreads, n) { mutexMapBenchmark(12, n); }

BENCHMARK(ConcurrentHashMapSingleThreaded, n) { concurrentMapBenchmark(1, n); }
BENCHMARK(ConcurrentHashMapMultiThreaded, n) { concurrentMapBenchmark(5, n); }
BENCHMARK(ConcurrentHashMapMaxThreads, n) { concurrentMapBenchmark(12, n); }

BENCHMARK(ConcurrentHashMapComplexSingleThreaded, n) {
concurrentMapBenchmarkComplex(1, n);
}

BENCHMARK(ConcurrentHashMapComplexMultiThreaded, n) {
concurrentMapBenchmarkComplex(5, n);
}

BENCHMARK(ConcurrentHashMapComplexMaxThreads, n) {
concurrentMapBenchmarkComplex(12, n);
}

BENCHMARK(AtomicUnorderedMapSingleThreaded, n) { atomicMapBenchmark(1, n); }
BENCHMARK(AtomicUnorderedMapMultiThreaded, n) { atomicMapBenchmark(5, n); }
BENCHMARK(AtomicUnorderedMapMaxThreads, n) { atomicMapBenchmark(12, n); }

BENCHMARK(AtomicUnorderedMapComplexSingleThreaded, n) {
atomicMapBenchmarkComplex(1, n);
}

BENCHMARK(AtomicUnorderedMapComplexMultiThreaded, n) {
atomicMapBenchmarkComplex(5, n);
}

BENCHMARK(AtomicUnorderedMapComplexMaxThreads, n) {
atomicMapBenchmarkComplex(12, n);
}

int main(int argc, char *argv[]) {
folly::Init init(&argc, &argv);
folly::runBenchmarks();
return 0;
}
Loading

0 comments on commit 421b839

Please sign in to comment.