Browse Source

update: build all cases

legacy
Dnomd343 2 years ago
parent
commit
82cc24cd91
  1. 3
      all_cases/CMakeLists.txt
  2. 65
      all_cases/all_cases.cc
  3. 29
      all_cases/all_cases.h
  4. 34
      all_cases/main.cc

3
all_cases/CMakeLists.txt

@ -1,6 +1,5 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
#add_executable(klotski main.cc all_cases.cc) add_executable(klotski main.cc common.cc basic_ranges.cc all_cases.cc)
add_executable(klotski main.cc basic_ranges.cc common.cc)
target_link_libraries(klotski pthread) target_link_libraries(klotski pthread)

65
all_cases/all_cases.cc

@ -1,45 +1,36 @@
#include "common.h"
#include "all_cases.h" #include "all_cases.h"
#include <iostream> std::mutex AllCases::all_cases_building;
#include <thread> bool AllCases::all_cases_available = false;
#include <unistd.h> std::vector<uint32_t> AllCases::all_cases[];
std::vector<uint32_t> AllCases::basic_ranges; const std::vector<uint32_t> (*AllCases::get_all_cases())[16] { // get const ptr of all cases
if (all_cases->empty()) {
bool AllCases::basic_ranges_available = false; build_all_cases(); // all cases initialize
std::mutex AllCases::basic_ranges_building;
void AllCases::build_basic_ranges() {
std::cout << std::this_thread::get_id() << " enter build function" << std::endl;
if (AllCases::basic_ranges_available) {
std::cout << std::this_thread::get_id() << " data already built -> skip" << std::endl;
return; // basic ranges already built
} }
return &all_cases; // return ptr
}
if (AllCases::basic_ranges_building.try_lock()) { // lock success -> not building void AllCases::build_all_cases() { // build all cases
if (AllCases::all_cases_available) {
std::cout << std::this_thread::get_id() << " try lock success -> start build process" << std::endl; return; // all cases already built
}
AllCases::basic_ranges.emplace_back(0); if (AllCases::all_cases_building.try_lock()) { // lock success -> start building
AllCases::basic_ranges.emplace_back(1); /// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14
AllCases::basic_ranges.emplace_back(2); for (uint32_t head = 0; head < 16; ++head) { // address of 2x2 block
sleep(2); // assume using a lot of time if ((head & 0b11) == 0b11) {
continue; // invalid 2x2 address
AllCases::basic_ranges_available = true; // enable available flag }
for (uint32_t const &range : *get_basic_ranges()) { // check base on 2x2 address and range
std::cout << std::this_thread::get_id() << " build complete -> continue" << std::endl; if (Common::check_case(head, range)) {
all_cases[head].emplace_back(Common::range_reverse(range)); // found valid case
}
}
}
AllCases::all_cases_available = true; // set available flag
} else { // another thread building } else { // another thread building
AllCases::all_cases_building.lock(); // blocking waiting
std::cout << std::this_thread::get_id() << " another thread building -> wait" << std::endl;
AllCases::basic_ranges_building.lock(); // blocking wait
} }
std::cout << std::this_thread::get_id() << " work complete -> unlock" << std::endl; AllCases::all_cases_building.unlock();
AllCases::basic_ranges_building.unlock();
} }

29
all_cases/all_cases.h

@ -3,22 +3,15 @@
#include <mutex> #include <mutex>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include "basic_ranges.h"
//class BasicRanges { class AllCases : public BasicRanges {
//public: public:
// static void build_all_cases();
// static const std::vector<uint32_t> (*get_all_cases())[16];
//
// static std::vector<uint32_t> basic_ranges; private:
// static bool all_cases_available;
// static void build_basic_ranges(); static std::mutex all_cases_building;
// static std::vector<uint32_t> all_cases[16];
// static bool basic_ranges_available; };
//
// static std::mutex basic_ranges_building;
//
//private:
//
// void generate_ranges(int n1, int n2, int n3, int n4);
//
//};

34
all_cases/main.cc

@ -1,5 +1,5 @@
#include <iostream> #include <iostream>
//#include "all_cases.h" #include "all_cases.h"
#include "basic_ranges.h" #include "basic_ranges.h"
#include <thread> #include <thread>
@ -20,20 +20,34 @@ void get_status() {
int main() { int main() {
get_status(); // get_status();
//
std::thread t1(BasicRanges::build_basic_ranges); // std::thread t1(BasicRanges::build_basic_ranges);
std::thread t2(BasicRanges::build_basic_ranges); // std::thread t2(BasicRanges::build_basic_ranges);
std::thread t3(BasicRanges::build_basic_ranges); // std::thread t3(BasicRanges::build_basic_ranges);
std::thread t(get_status); // std::thread t(get_status);
// t1.join();
// t2.join();
// t3.join();
// t.join();
//
// get_status();
//
// std::cout << BasicRanges::get_basic_ranges()->size() << std::endl;
std::thread t1(AllCases::build_all_cases);
std::thread t2(AllCases::build_all_cases);
std::thread t3(AllCases::build_all_cases);
t1.join(); t1.join();
t2.join(); t2.join();
t3.join(); t3.join();
t.join();
get_status(); AllCases::build_all_cases();
std::cout << BasicRanges::get_basic_ranges()->size() << std::endl; for (auto const &all_case : *AllCases::get_all_cases()) {
std::cout << all_case.size() << std::endl;
}
return 0; return 0;
} }

Loading…
Cancel
Save