SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
FastParallelExplanation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/interprocess/managed_shared_memory.hpp>
4 #include <boost/interprocess/containers/vector.hpp>
5 #include <boost/interprocess/allocators/allocator.hpp>
6 #include <unistd.h>
7 
8 #include "../utils/Bookkeeping.h"
9 
11 
12 namespace smtrat {
13 namespace mcsat {
14 
15 using namespace boost::interprocess;
16 typedef allocator<std::optional<Explanation> , managed_shared_memory::segment_manager> ShmemAllocator;
17 typedef vector<std::optional<Explanation> , ShmemAllocator> SharedVector;
18 
19 /**
20  * This explanation executes all given explanation in parallel processes and waits for the fastest explanation,
21  * returning the fastest delivered explanation, terminating all other parallel processes
22  */
23 //TODO make shared memory runnable
24 template<typename... Backends>
26 private:
27  using B = std::tuple<Backends...>;
29 
30  template<std::size_t N = 0, carl::EnableIfBool<N == std::tuple_size<B>::value> = carl::dummy>
31  void explain(const mcsat::Bookkeeping&, carl::Variable, const FormulasT&, std::vector<pid_t>& pids) const {
32  SMTRAT_LOG_DEBUG("smtrat.mcsat.explanation", "All explanations started.");
33  }
34 
35  template<std::size_t N = 0, carl::EnableIfBool<N < std::tuple_size<B>::value> = carl::dummy>
36  void explain(const mcsat::Bookkeeping& data, carl::Variable var, const FormulasT& reason, std::vector<pid_t>& pids) const {
37 
38  if( (pids[N] = fork()) == 0){
39  SMTRAT_LOG_DEBUG("smtrat.mcsat.explanation", "Concurrent strategy " << N+1 << " started");
40  std::optional<Explanation> explanation = std::get<N>(mBackends)(data, var, reason);
41  SMTRAT_LOG_DEBUG("smtrat.mcsat.explanation", "Concurrent strategy " << N+1 << " done");
42 
43  managed_shared_memory segment(open_only, "SharedVector");
44  SharedVector *explanations = segment.find<SharedVector>("ExplanationsVector").first;
45  *(explanations->begin()+N) = explanation;
46 
47  //wait
48  while (true){
49  (void)0;
50  }
51  SMTRAT_LOG_ERROR("smtrat.mcsat.explanation", "This should not be reachable");
52  } else if(pids[N] == -1){
53  SMTRAT_LOG_ERROR("smtrat.mcsat.explanation", "Error while forking.");
54  }
55 
56  explain<N+1>(data, var, reason, pids);
57  }
58 
59 public:
60  std::optional<Explanation> operator()(const mcsat::Bookkeeping& data, carl::Variable var, const FormulasT& reason) const {
61  const std::size_t NUM_PROCESSES = std::tuple_size<B>::value;
62  if(NUM_PROCESSES <= 0){
63  SMTRAT_LOG_ERROR("smtrat.mcsat.explanation", "No explanation given.");
64  return std::nullopt;
65  }
66 
67  struct shm_remove {
68  shm_remove() { shared_memory_object::remove("MySharedMemory"); }
69  ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
70  } remover;
71 
72  std::vector<pid_t> pids;
73 
74  managed_shared_memory segment(create_only, "SharedVector", 65536);
75  const ShmemAllocator alloc_inst (segment.get_segment_manager());
76  SharedVector *explanations = segment.construct<SharedVector>("ExplanationsVector")(alloc_inst);
77 
78  for(size_t i = 0; i < NUM_PROCESSES; i++) {
79  explanations->push_back(std::nullopt);
80  }
81 
82  // same workarround as in sequential explanation
83  explain<0>(data, var, reason, pids);
84  SMTRAT_LOG_DEBUG("smtrat.mcsat.explanation", "Start looking for explanation");
85  while (true){
86  for (auto it = explanations->begin(); it != explanations->end(); it++) {
87  if((*it) != std::nullopt){
88  for (size_t i = 0; i < NUM_PROCESSES; i++) {
89  if(!kill(pids[i], SIGKILL)){
90  SMTRAT_LOG_ERROR("smtrat.mcsat.explanation", "Error in Kill.");
91  }
92  }
93 
94  segment.destroy<SharedVector>("ExplanationsVector");
95  return *it;
96  }
97  }
98  }
99 
100  }
101 
102 };
103 
104 } // namespace mcsat
105 } // namespace smtrat
Represent the trail, i.e.
Definition: Bookkeeping.h:19
int var(Lit p)
Definition: SolverTypes.h:64
static void remove(V &ts, const T &t)
Definition: Alg.h:36
allocator< std::optional< Explanation >, managed_shared_memory::segment_manager > ShmemAllocator
vector< std::optional< Explanation >, ShmemAllocator > SharedVector
Class to create the formulas for axioms.
carl::Formulas< Poly > FormulasT
Definition: types.h:39
#define SMTRAT_LOG_ERROR(channel, msg)
Definition: logging.h:32
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
This explanation executes all given explanation in parallel processes and waits for the fastest expla...
SMTRAT_LOG_DEBUG("smtrat.mcsat.explanation", "Start looking for explanation")
void explain(const mcsat::Bookkeeping &, carl::Variable, const FormulasT &, std::vector< pid_t > &pids) const