-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallelize DIMACS formula printing #6431
Open
srogatch
wants to merge
1
commit into
diffblue:develop
Choose a base branch
from
srogatch:srogatch/feat/parallel-dimacs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ Author: Daniel Kroening, [email protected] | |
#include <util/magic.h> | ||
|
||
#include <iostream> | ||
#include <thread> | ||
|
||
dimacs_cnft::dimacs_cnft(message_handlert &message_handler) | ||
: cnf_clause_listt(message_handler), break_lines(false) | ||
|
@@ -76,25 +77,62 @@ void dimacs_cnft::write_dimacs_clause( | |
out << "0" << "\n"; | ||
} | ||
|
||
void dimacs_cnft::wait_file_block(const size_t ordinal) { | ||
std::unique_lock<std::mutex> lock(writing_sync); | ||
for(;;) { | ||
assert(next_file_block <= ordinal); | ||
if(next_file_block == ordinal) { | ||
return; | ||
} | ||
block_written.wait(lock); | ||
} | ||
} | ||
|
||
void dimacs_cnft::printer_entry(std::ostream *out) { | ||
clause_range item; | ||
std::stringstream output_block; | ||
while(print_queue.pop(item)) { | ||
output_block.str(""); | ||
output_block.clear(); | ||
|
||
for(clausest::const_iterator it=item.first; it!=item.limit; it++) { | ||
write_dimacs_clause(*it, output_block, break_lines); | ||
} | ||
|
||
wait_file_block(item.ordinal); | ||
*out << output_block.str(); | ||
std::unique_lock<std::mutex> lock(writing_sync); | ||
next_file_block++; | ||
block_written.notify_all(); | ||
} | ||
} | ||
|
||
void dimacs_cnft::write_clauses(std::ostream &out) | ||
{ | ||
std::size_t count = 0; | ||
std::stringstream output_block; | ||
std::vector<std::thread> pool; | ||
const size_t thread_count = std::max(2u, std::thread::hardware_concurrency()) - 1; | ||
for(size_t i=0; i<thread_count; i++) { | ||
pool.emplace_back(&dimacs_cnft::printer_entry, this, &out); | ||
} | ||
next_file_block = 0; | ||
size_t total_blocks = 0; | ||
for(clausest::const_iterator it=clauses.begin(); | ||
it!=clauses.end(); it++) | ||
it!=clauses.end(); ) | ||
{ | ||
write_dimacs_clause(*it, output_block, break_lines); | ||
|
||
// print the block once in a while | ||
if(++count % CNF_DUMP_BLOCK_SIZE == 0) | ||
{ | ||
out << output_block.str(); | ||
output_block.str(""); | ||
clausest::const_iterator first = it; | ||
size_t total_size = 0; | ||
while(total_size < target_block_size && it != clauses.end()) { | ||
total_size += it->size(); | ||
it++; | ||
} | ||
print_queue.push(clause_range(total_blocks, first, it)); | ||
total_blocks++; | ||
} | ||
print_queue.request_shutdown(); | ||
wait_file_block(total_blocks); | ||
for(size_t i=0; i<pool.size(); i++) { | ||
pool[i].join(); | ||
} | ||
|
||
// make sure the final block is printed as well | ||
out << output_block.str(); | ||
} | ||
|
||
void dimacs_cnf_dumpt::lcnf(const bvt &bv) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,29 @@ Author: Daniel Kroening, [email protected] | |
#define CPROVER_SOLVERS_SAT_DIMACS_CNF_H | ||
|
||
#include <iosfwd> | ||
#include <atomic> | ||
#include <condition_variable> | ||
|
||
#include "cnf_clause_list.h" | ||
#include <util/blocking_queue.h> | ||
|
||
class dimacs_cnft:public cnf_clause_listt | ||
{ | ||
protected: | ||
// Optimal block size, in clauses | ||
const size_t target_block_size = 1<<16; | ||
|
||
struct clause_range { | ||
size_t ordinal; | ||
clausest::const_iterator first, limit; | ||
explicit clause_range(const size_t ordinal, | ||
const clausest::const_iterator first, | ||
const clausest::const_iterator limit) | ||
: ordinal(ordinal), first(first), limit(limit) | ||
{ } | ||
clause_range() : ordinal(0), first(nullptr), limit(nullptr) { } | ||
}; | ||
|
||
public: | ||
explicit dimacs_cnft(message_handlert &); | ||
virtual ~dimacs_cnft() { } | ||
|
@@ -38,8 +56,14 @@ class dimacs_cnft:public cnf_clause_listt | |
protected: | ||
void write_problem_line(std::ostream &out); | ||
void write_clauses(std::ostream &out); | ||
void wait_file_block(const size_t ordinal); | ||
void printer_entry(std::ostream *out); | ||
|
||
bool break_lines; | ||
size_t next_file_block; | ||
std::mutex writing_sync; | ||
std::condition_variable block_written; | ||
blocking_queue<clause_range> print_queue; | ||
}; | ||
|
||
class dimacs_cnf_dumpt:public cnft | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef CPROVER_UTIL_BLOCKING_QUEUE | ||
#define CPROVER_UTIL_BLOCKING_QUEUE | ||
|
||
#include <mutex> | ||
#include <condition_variable> | ||
#include <queue> | ||
|
||
template <typename T> class blocking_queue { | ||
std::condition_variable can_pop; | ||
std::mutex sync; | ||
std::queue<T> qu; | ||
bool shutdown = false; | ||
|
||
public: | ||
void push(const T& item) | ||
{ | ||
{ | ||
std::unique_lock<std::mutex> lock(sync); | ||
qu.push(item); | ||
} | ||
can_pop.notify_one(); | ||
} | ||
|
||
void request_shutdown() | ||
{ | ||
{ | ||
std::unique_lock<std::mutex> lock(sync); | ||
shutdown = true; | ||
} | ||
can_pop.notify_all(); | ||
} | ||
|
||
bool pop(T &item) | ||
{ | ||
std::unique_lock<std::mutex> lock(sync); | ||
for (;;) | ||
{ | ||
if (qu.empty()) | ||
{ | ||
if (shutdown) | ||
{ | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
can_pop.wait(lock); | ||
} | ||
item = std::move(qu.front()); | ||
qu.pop(); | ||
return true; | ||
} | ||
}; | ||
|
||
#endif // CPROVER_UTIL_BLOCKING_QUEUE |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't add
assert
s, use the various variants ofINVARIANT
.