Painless
A framework to ease parallelization of sequential CDCL SAT solvers
Loading...
Searching...
No Matches
BloomFilter.hpp
1#pragma once
2
3#include <algorithm>
4#include <atomic>
5#include <climits>
6#include <cmath>
7#include <iostream>
8#include <stdexcept>
9#include <unordered_map>
10#include <vector>
11// #include <spot/misc/clz.hh>
12
13#include "containers/ClauseUtils.hpp"
14
15#define NUM_BITS 1048576 // 8MB
16// #define NUM_BITS 67108864 // 8MB
17
18typedef size_t hash_t;
19typedef hash_t (*hash_function_t)(const int*, unsigned int size);
20typedef std::vector<hash_function_t> hash_functions_t;
21
22/* Lock-free concurrent Bloom Filter implementation */
24{
25 private:
26 size_t mem_size_;
27 size_t mem_size_bits_;
28 hash_functions_t hash_functions_;
29 std::unordered_map<hash_t, uint8_t> count_per_checksum;
30
31 /* Internal concurrent bitset */
32 static const size_t BITS_PER_ELEMENT = sizeof(uint64_t) * CHAR_BIT;
33
34 uint64_t* bits_;
35 size_t get_index(size_t bit) const;
36 size_t get_mask(size_t bit) const;
37 void set(size_t bit);
38 bool test(size_t bit) const;
39
40 public:
41 BloomFilter(size_t mem_size, hash_functions_t hash_functions)
42 : mem_size_(mem_size)
43 ,
44 // std::pow(2, std::ceil(std::log(mem_size)/std::log(2)))),
45 mem_size_bits_(mem_size * BITS_PER_ELEMENT)
46 , hash_functions_(hash_functions)
47 {
48 // std::cout << "BF: size " << mem_size_ << " "<< mem_size << std::endl;
49 bits_ = new uint64_t[mem_size_]{ 0 };
50 if (hash_functions.empty())
51 throw std::invalid_argument("Bloom filter has no hash functions");
52 }
53
54 // Default internal hash function
55 BloomFilter(size_t mem_size)
57 {
58 }
59
61 : BloomFilter(NUM_BITS)
62 {
63 }
64
65 ~BloomFilter() { delete[] bits_; }
66 void insert(const int* clause, unsigned int size);
67 uint8_t test_and_insert(const int* clause, unsigned int size);
68 uint8_t test_and_insert(size_t checksum, int max_limit_duplicas);
69 bool contains_or_insert(const int* clause, unsigned int size);
70 bool contains(const int* clause, unsigned int size);
71};
Definition BloomFilter.hpp:24
size_t lookup3_hash_clause(const int *clause, unsigned int size)
Computes a hash value for a clause using the Jenkins lookup3 algorithm.
Definition ClauseUtils.hpp:55