3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-23 17:15:31 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2019-06-03 19:50:25 -07:00 committed by Nuno Lopes
parent 2788f72bbb
commit 9262908ebb
30 changed files with 191 additions and 341 deletions

View file

@ -17,24 +17,20 @@ Notes:
--*/
#ifndef _NO_OMP_
#include "util/cooperate.h"
#include "util/trace.h"
#include "util/debug.h"
#include "util/z3_omp.h"
#include <thread>
#include <mutex>
struct cooperation_lock {
omp_nest_lock_t m_lock;
std::recursive_mutex m_lock;
char const * m_task;
volatile int m_owner_thread;
std::thread::id m_owner_thread;
cooperation_lock() {
omp_set_nested(1);
omp_init_nest_lock(&m_lock);
m_task = nullptr;
m_owner_thread = -1;
}
~cooperation_lock() {
omp_destroy_nest_lock(&m_lock);
}
};
@ -45,20 +41,18 @@ bool cooperation_ctx::g_cooperate = false;
void cooperation_ctx::checkpoint(char const * task) {
SASSERT(cooperation_ctx::enabled());
int tid = omp_get_thread_num();
std::thread::id tid = std::this_thread::get_id();
if (g_lock.m_owner_thread == tid) {
g_lock.m_owner_thread = -1;
omp_unset_nest_lock(&(g_lock.m_lock));
g_lock.m_owner_thread = std::thread::id();
g_lock.m_lock.unlock();
}
// this critical section is used to force the owner thread to give a chance to
// another thread to get the lock
#pragma omp critical (z3_cooperate)
{
omp_set_nest_lock(&(g_lock.m_lock));
TRACE("cooperate_detail", tout << task << ", tid: " << tid << "\n";);
CTRACE("cooperate", g_lock.m_task != task, tout << "moving to task: " << task << "\n";);
g_lock.m_owner_thread = tid;
}
std::this_thread::yield();
g_lock.m_lock.lock();
TRACE("cooperate_detail", tout << task << ", tid: " << tid << "\n";);
CTRACE("cooperate", g_lock.m_task != task, tout << "moving to task: " << task << "\n";);
g_lock.m_owner_thread = tid;
}
#endif

View file

@ -19,6 +19,9 @@ Notes:
#include "util/gparams.h"
#include "util/dictionary.h"
#include "util/trace.h"
#include <mutex>
static std::mutex gparams_mux;
extern void gparams_register_modules();
@ -110,14 +113,12 @@ public:
}
void reset() {
#pragma omp critical (gparams)
{
m_params.reset();
for (auto & kv : m_module_params) {
dealloc(kv.m_value);
}
m_module_params.reset();
std::lock_guard<std::mutex> lock(gparams_mux);
m_params.reset();
for (auto & kv : m_module_params) {
dealloc(kv.m_value);
}
m_module_params.reset();
}
// -----------------------------------------------
@ -327,8 +328,8 @@ public:
void set(char const * name, char const * value) {
bool error = false;
std::string error_msg;
#pragma omp critical (gparams)
{
std::lock_guard<std::mutex> lock(gparams_mux);
try {
symbol m, p;
normalize(name, m, p);
@ -379,8 +380,8 @@ public:
std::string r;
bool error = false;
std::string error_msg;
#pragma omp critical (gparams)
{
std::lock_guard<std::mutex> lock(gparams_mux);
try {
symbol m, p;
normalize(name, m, p);
@ -426,8 +427,8 @@ public:
params_ref get_module(symbol const & module_name) {
params_ref result;
params_ref * ps = nullptr;
#pragma omp critical (gparams)
{
std::lock_guard<std::mutex> lock(gparams_mux);
if (m_module_params.find(module_name, ps)) {
result.copy(*ps);
}
@ -446,8 +447,8 @@ public:
// -----------------------------------------------
void display(std::ostream & out, unsigned indent, bool smt2_style, bool include_descr) {
#pragma omp critical (gparams)
{
std::lock_guard<std::mutex> lock(gparams_mux);
out << "Global parameters\n";
get_param_descrs().display(out, indent + 4, smt2_style, include_descr);
out << "\n";
@ -469,44 +470,40 @@ public:
}
void display_modules(std::ostream & out) {
#pragma omp critical (gparams)
{
for (auto & kv : get_module_param_descrs()) {
out << "[module] " << kv.m_key;
char const * descr = nullptr;
if (get_module_descrs().find(kv.m_key, descr)) {
out << ", description: " << descr;
}
out << "\n";
std::lock_guard<std::mutex> lock(gparams_mux);
for (auto & kv : get_module_param_descrs()) {
out << "[module] " << kv.m_key;
char const * descr = nullptr;
if (get_module_descrs().find(kv.m_key, descr)) {
out << ", description: " << descr;
}
out << "\n";
}
}
void display_module(std::ostream & out, symbol const & module_name) {
bool error = false;
std::string error_msg;
#pragma omp critical (gparams)
{
try {
param_descrs * d = nullptr;
if (!get_module_param_descrs().find(module_name, d)) {
std::stringstream strm;
strm << "unknown module '" << module_name << "'";
throw exception(strm.str());
}
out << "[module] " << module_name;
char const * descr = nullptr;
if (get_module_descrs().find(module_name, descr)) {
out << ", description: " << descr;
}
out << "\n";
d->display(out, 4, false);
std::lock_guard<std::mutex> lock(gparams_mux);
try {
param_descrs * d = nullptr;
if (!get_module_param_descrs().find(module_name, d)) {
std::stringstream strm;
strm << "unknown module '" << module_name << "'";
throw exception(strm.str());
}
catch (z3_exception & ex) {
// Exception cannot cross critical section boundaries.
error = true;
error_msg = ex.msg();
out << "[module] " << module_name;
char const * descr = nullptr;
if (get_module_descrs().find(module_name, descr)) {
out << ", description: " << descr;
}
out << "\n";
d->display(out, 4, false);
}
catch (z3_exception & ex) {
// Exception cannot cross critical section boundaries.
error = true;
error_msg = ex.msg();
}
if (error)
throw exception(std::move(error_msg));
@ -515,8 +512,8 @@ public:
void display_parameter(std::ostream & out, char const * name) {
bool error = false;
std::string error_msg;
#pragma omp critical (gparams)
{
std::lock_guard<std::mutex> lock(gparams_mux);
try {
symbol m, p;
normalize(name, m, p);

View file

@ -7,10 +7,10 @@ Copyright (c) 2015 Microsoft Corporation
#include<iostream>
#include<stdlib.h>
#include<climits>
#include<mutex>
#include "util/trace.h"
#include "util/memory_manager.h"
#include "util/error_codes.h"
#include "util/z3_omp.h"
#include "util/debug.h"
// The following two function are automatically generated by the mk_make.py script.
// The script collects ADD_INITIALIZER and ADD_FINALIZER commands in the .h files.
@ -35,6 +35,7 @@ out_of_memory_error::out_of_memory_error():z3_error(ERR_MEMOUT) {
}
static std::mutex g_memory_mux;
static volatile bool g_memory_out_of_memory = false;
static bool g_memory_initialized = false;
static long long g_memory_alloc_size = 0;
@ -54,8 +55,8 @@ void memory::exit_when_out_of_memory(bool flag, char const * msg) {
}
static void throw_out_of_memory() {
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
g_memory_out_of_memory = true;
}
@ -90,8 +91,8 @@ mem_usage_report g_info;
void memory::initialize(size_t max_size) {
bool initialize = false;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
// only update the maximum size if max_size != UINT_MAX
if (max_size != UINT_MAX)
g_memory_max_size = max_size;
@ -116,8 +117,8 @@ void memory::initialize(size_t max_size) {
bool memory::is_out_of_memory() {
bool r = false;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
r = g_memory_out_of_memory;
}
return r;
@ -131,12 +132,8 @@ void memory::set_high_watermark(size_t watermark) {
bool memory::above_high_watermark() {
if (g_memory_watermark == 0)
return false;
bool r;
#pragma omp critical (z3_memory_manager)
{
r = g_memory_watermark < g_memory_alloc_size;
}
return r;
std::lock_guard<std::mutex> lock(g_memory_mux);
return g_memory_watermark < g_memory_alloc_size;
}
// The following methods are only safe to invoke at
@ -163,8 +160,8 @@ void memory::finalize() {
unsigned long long memory::get_allocation_size() {
long long r;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
r = g_memory_alloc_size;
}
if (r < 0)
@ -174,8 +171,8 @@ unsigned long long memory::get_allocation_size() {
unsigned long long memory::get_max_used_memory() {
unsigned long long r;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
r = g_memory_max_used_size;
}
return r;
@ -258,8 +255,8 @@ static void synchronize_counters(bool allocating) {
bool out_of_mem = false;
bool counts_exceeded = false;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
g_memory_alloc_size += g_memory_thread_alloc_size;
g_memory_alloc_count += g_memory_thread_alloc_count;
if (g_memory_alloc_size > g_memory_max_used_size)
@ -339,8 +336,8 @@ void memory::deallocate(void * p) {
size_t * sz_p = reinterpret_cast<size_t*>(p) - 1;
size_t sz = *sz_p;
void * real_p = reinterpret_cast<void*>(sz_p);
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
g_memory_alloc_size -= sz;
}
free(real_p);
@ -349,8 +346,8 @@ void memory::deallocate(void * p) {
void * memory::allocate(size_t s) {
s = s + sizeof(size_t); // we allocate an extra field!
bool out_of_mem = false, counts_exceeded = false;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
g_memory_alloc_size += s;
g_memory_alloc_count += 1;
if (g_memory_alloc_size > g_memory_max_used_size)
@ -379,8 +376,8 @@ void* memory::reallocate(void *p, size_t s) {
void * real_p = reinterpret_cast<void*>(sz_p);
s = s + sizeof(size_t); // we allocate an extra field!
bool out_of_mem = false, counts_exceeded = false;
#pragma omp critical (z3_memory_manager)
{
std::lock_guard<std::mutex> lock(g_memory_mux);
g_memory_alloc_size += s - sz;
g_memory_alloc_count += 1;
if (g_memory_alloc_size > g_memory_max_used_size)

View file

@ -29,11 +29,9 @@ static_assert(sizeof(mpn_double_digit) == 2 * sizeof(mpn_digit), "size alignment
const mpn_digit mpn_manager::zero = 0;
mpn_manager::mpn_manager() {
omp_init_nest_lock(&m_lock);
}
mpn_manager::~mpn_manager() {
omp_destroy_nest_lock(&m_lock);
}
int mpn_manager::compare(mpn_digit const * a, size_t const lnga,

View file

@ -20,18 +20,16 @@ Revision History:
#define MPN_H_
#include<ostream>
#include<mutex>
#include "util/util.h"
#include "util/buffer.h"
#include "util/z3_omp.h"
typedef unsigned int mpn_digit;
class mpn_manager {
#ifndef _NO_OMP_
omp_nest_lock_t m_lock;
#endif
#define MPN_BEGIN_CRITICAL() omp_set_nest_lock(&m_lock);
#define MPN_END_CRITICAL() omp_unset_nest_lock(&m_lock);
std::recursive_mutex m_lock;
#define MPN_BEGIN_CRITICAL() m_lock.lock();
#define MPN_END_CRITICAL() m_lock.unlock();
public:
mpn_manager();

View file

@ -139,8 +139,6 @@ uint64_t u64_gcd(uint64_t u, uint64_t v) {
template<bool SYNCH>
mpz_manager<SYNCH>::mpz_manager():
m_allocator("mpz_manager") {
if (SYNCH)
omp_init_nest_lock(&m_lock);
#ifndef _MP_GMP
if (sizeof(digit_t) == sizeof(uint64_t)) {
// 64-bit machine
@ -197,8 +195,6 @@ mpz_manager<SYNCH>::~mpz_manager() {
mpz_clear(m_int64_max);
mpz_clear(m_int64_min);
#endif
if (SYNCH)
omp_destroy_nest_lock(&m_lock);
}
#ifndef _MP_GMP

View file

@ -20,12 +20,12 @@ Revision History:
#define MPZ_H_
#include<string>
#include<mutex>
#include "util/util.h"
#include "util/small_object_allocator.h"
#include "util/trace.h"
#include "util/scoped_numeral.h"
#include "util/scoped_numeral_vector.h"
#include "util/z3_omp.h"
#include "util/mpn.h"
unsigned u_gcd(unsigned u, unsigned v);
@ -135,9 +135,9 @@ inline void swap(mpz & m1, mpz & m2) { m1.swap(m2); }
template<bool SYNCH = true>
class mpz_manager {
mutable small_object_allocator m_allocator;
mutable omp_nest_lock_t m_lock;
#define MPZ_BEGIN_CRITICAL() if (SYNCH) omp_set_nest_lock(&m_lock);
#define MPZ_END_CRITICAL() if (SYNCH) omp_unset_nest_lock(&m_lock);
mutable std::recursive_mutex m_lock;
#define MPZ_BEGIN_CRITICAL() if (SYNCH) m_lock.lock();
#define MPZ_END_CRITICAL() if (SYNCH) m_lock.unlock();
mutable mpn_manager m_mpn_manager;
#ifndef _MP_GMP
@ -702,11 +702,7 @@ public:
bool decompose(mpz const & n, svector<digit_t> & digits);
};
#ifndef _NO_OMP_
typedef mpz_manager<true> synch_mpz_manager;
#else
typedef mpz_manager<false> synch_mpz_manager;
#endif
typedef mpz_manager<false> unsynch_mpz_manager;
typedef _scoped_numeral<unsynch_mpz_manager> scoped_mpz;

View file

@ -17,6 +17,7 @@ Notes:
--*/
#include "util/prime_generator.h"
#include <mutex>
#define PRIME_LIST_MAX_SIZE 1<<20
@ -109,6 +110,8 @@ prime_iterator::prime_iterator(prime_generator * g):m_idx(0) {
}
}
static std::mutex g_prime_iterator;
uint64_t prime_iterator::next() {
unsigned idx = m_idx;
m_idx++;
@ -117,7 +120,7 @@ uint64_t prime_iterator::next() {
}
else {
uint64_t r;
#pragma omp critical (prime_iterator)
std::lock_guard<std::mutex> lock(g_prime_iterator);
{
r = (*m_generator)(idx);
}

View file

@ -17,6 +17,7 @@ Revision History:
--*/
#include<sstream>
#include<mutex>
#include "util/util.h"
#include "util/rational.h"
#ifdef _WINDOWS
@ -42,9 +43,11 @@ static void mk_power_up_to(vector<rational> & pws, unsigned n) {
}
}
static std::mutex g_powers_of_two;
rational rational::power_of_two(unsigned k) {
rational result;
#pragma omp critical (powers_of_two)
std::lock_guard<std::mutex> lock(g_powers_of_two);
{
if (k >= m_powers_of_two.size())
mk_power_up_to(m_powers_of_two, k+1);

View file

@ -18,6 +18,10 @@ Revision History:
--*/
#include "util/rlimit.h"
#include "util/common_msgs.h"
#include <mutex>
static std::mutex g_reslimit_cancel;
reslimit::reslimit():
m_cancel(0),
@ -69,48 +73,36 @@ char const* reslimit::get_cancel_msg() const {
}
void reslimit::push_child(reslimit* r) {
#pragma omp critical (reslimit_cancel)
{
m_children.push_back(r);
}
std::lock_guard<std::mutex> lock(g_reslimit_cancel);
m_children.push_back(r);
}
void reslimit::pop_child() {
#pragma omp critical (reslimit_cancel)
{
m_children.pop_back();
}
std::lock_guard<std::mutex> lock(g_reslimit_cancel);
m_children.pop_back();
}
void reslimit::cancel() {
#pragma omp critical (reslimit_cancel)
{
set_cancel(m_cancel+1);
}
std::lock_guard<std::mutex> lock(g_reslimit_cancel);
set_cancel(m_cancel+1);
}
void reslimit::reset_cancel() {
#pragma omp critical (reslimit_cancel)
{
set_cancel(0);
}
std::lock_guard<std::mutex> lock(g_reslimit_cancel);
set_cancel(0);
}
void reslimit::inc_cancel() {
#pragma omp critical (reslimit_cancel)
{
set_cancel(m_cancel+1);
}
std::lock_guard<std::mutex> lock(g_reslimit_cancel);
set_cancel(m_cancel+1);
}
void reslimit::dec_cancel() {
#pragma omp critical (reslimit_cancel)
{
if (m_cancel > 0) {
set_cancel(m_cancel-1);
}
std::lock_guard<std::mutex> lock(g_reslimit_cancel);
if (m_cancel > 0) {
set_cancel(m_cancel-1);
}
}

View file

@ -20,8 +20,10 @@ Revision History:
#include "util/str_hashtable.h"
#include "util/region.h"
#include "util/string_buffer.h"
#include "util/z3_omp.h"
#include <cstring>
#include <mutex>
static std::mutex g_symbol_lock;
symbol symbol::m_dummy(TAG(void*, nullptr, 2));
const symbol symbol::null;
@ -36,8 +38,7 @@ public:
char const * get_str(char const * d) {
const char * result;
#pragma omp critical (cr_symbol)
{
std::lock_guard<std::mutex> lock(g_symbol_lock);
str_hashtable::entry * e;
if (m_table.insert_if_not_there_core(d, e)) {
// new entry
@ -55,7 +56,6 @@ public:
result = e->get_data();
}
SASSERT(m_table.contains(result));
}
return result;
}
};

View file

@ -23,6 +23,10 @@ Revision History:
#include <thread>
#include "util/util.h"
static std::mutex g_verbose_mux;
void verbose_lock() { g_verbose_mux.lock(); }
void verbose_unlock() { g_verbose_mux.unlock(); }
static unsigned g_verbosity_level = 0;
void set_verbosity_level(unsigned lvl) {

View file

@ -21,10 +21,10 @@ Revision History:
#include "util/debug.h"
#include "util/memory_manager.h"
#include "util/z3_omp.h"
#include<iostream>
#include<climits>
#include<limits>
#include<mutex>
#include<stdint.h>
#ifndef SIZE_MAX
@ -174,11 +174,8 @@ void set_verbosity_level(unsigned lvl);
unsigned get_verbosity_level();
std::ostream& verbose_stream();
void set_verbose_stream(std::ostream& str);
#ifdef _NO_OMP_
# define is_threaded() false
#else
bool is_threaded();
#endif
#define IF_VERBOSE(LVL, CODE) { \
@ -191,25 +188,16 @@ bool is_threaded();
} \
} } ((void) 0)
#ifdef _MSC_VER
#define DO_PRAGMA(x) __pragma(x)
#define PRAGMA_LOCK __pragma(omp critical (verbose_lock))
#else
#define DO_PRAGMA(x) _Pragma(#x)
#define PRAGMA_LOCK _Pragma("omp critical (verbose_lock)")
#endif
#ifdef _NO_OMP_
#define LOCK_CODE(CODE) CODE;
#else
#define LOCK_CODE(CODE) \
{ \
PRAGMA_LOCK \
{ \
CODE; \
} \
}
#endif
void verbose_lock();
void verbose_unlock();
#define LOCK_CODE(CODE) \
{ \
verbose_lock(); \
CODE; \
verbose_unlock(); \
} \
template<typename T>
struct default_eq {

View file

@ -1,38 +0,0 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
z3_omp.h
Abstract:
Wrapper for OMP functions and data-structures
Author:
Leonardo (leonardo) 2012-01-05
Notes:
--*/
#ifndef Z3_OMP_H_
#define Z3_OMP_H_
#ifndef _NO_OMP_
#include<omp.h>
#else
#define omp_in_parallel() false
#define omp_set_num_threads(SZ) ((void)0)
#define omp_get_thread_num() 0
#define omp_get_num_procs() 1
#define omp_set_nested(V) ((void)0)
#define omp_init_nest_lock(L) ((void) 0)
#define omp_destroy_nest_lock(L) ((void) 0)
#define omp_set_nest_lock(L) ((void) 0)
#define omp_unset_nest_lock(L) ((void) 0)
struct omp_nest_lock_t {
};
#endif
#endif