Skip to content

Commit

Permalink
Another sequantial variant
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechMula committed Feb 18, 2018
1 parent 7939283 commit 22afd2c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
66 changes: 66 additions & 0 deletions parallel-and-bitmaps/SequentialAndZeroTracking.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include <vector>
#include "MultipleAndInterface.h"

class SequentialAndZeroTracking: public MultipleAndInterface {

protected:
std::vector<bitvector*> input;
std::vector<char> zero;
const size_t block_size = 64;

public:
SequentialAndZeroTracking(const std::vector<bitvector*>& input_)
: input(input_) {

assert(input.size() >= 2);

const size_t n = (input[0]->n + block_size - 1) / block_size;
zero.resize(n);
}

virtual std::unique_ptr<bitvector> calculate() override {

bitvector* bv = new bitvector(*input[0]);

#if 0
# define TRACE(msg, ...) printf(msg, __VA_ARGS__)
#else
# define TRACE(msg, ...)
#endif

size_t zero_blocks = 0;
for (size_t i=1; i < input.size(); i++) {
TRACE("bitmap %lu of %lu\n", i, input.size() - 1);
for (size_t j=0; j < zero.size(); j++) {
if (zero[j]) {
continue; // skip zero block
}

const size_t start = j * block_size;
const size_t end = std::min(start + block_size, bv->n);

uint64_t nonzero = 0;
for (size_t p=start; p < end; p++) {
bv->data[p] &= input[i]->data[p];
nonzero |= bv->data[p];
}

if (nonzero == 0) {
// the block is zero, skip in the future
TRACE("\tblock %lu will be skip\n", j);
zero[j] = 1;
zero_blocks += 1;
}
}

if (zero_blocks == zero.size()) {
TRACE("early exit after processing bitmap %lu\n", i);
break; // all blocks are zero
}
}

return std::unique_ptr<bitvector>(bv);
}
};
6 changes: 6 additions & 0 deletions parallel-and-bitmaps/bitvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <cassert>

class bitvector {
Expand All @@ -16,6 +17,11 @@ class bitvector {
data = new uint64_t[n];
}

bitvector(const bitvector& bv) : bitvector(bv.n) {

memcpy(data, bv.data, n * sizeof(uint64_t));
}

~bitvector() {
delete[] data;
}
Expand Down
10 changes: 7 additions & 3 deletions parallel-and-bitmaps/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <chrono>

#include "SequentialAnd.h"
#include "SequentialAndZeroTracking.h"
#include "ParallelAndNaive.h"
#include "ParallelAndSplit.h"

Expand All @@ -12,20 +13,20 @@ void test(const char* info, MultipleAndInterface& ma, const std::vector<bitvecto

assert(input.size() >= 2);

printf("%-20s (%lu bitmaps): ", info, input.size());
printf("%-30s (%lu bitmaps): ", info, input.size());
fflush(stdout);

const auto t1 = Clock::now();
auto res = ma.calculate();
const auto t2 = Clock::now();

const auto t_us = duration_cast<microseconds>(t2 - t1).count();
printf("%luus [cardinality=%lu]\n", t_us, res->cardinality());
printf("%10luus [cardinality=%lu]\n", t_us, res->cardinality());
}

int main() {

const size_t bitmap_size = 2000000;
const size_t bitmap_size = 1000000;
const size_t count = 100;

std::vector<bitvector*> input;
Expand All @@ -40,6 +41,9 @@ int main() {
SequentialAnd seq(input);
test("SequentialAnd", seq, input);

SequentialAndZeroTracking seq2(input);
test("SequentialAndZeroTracking", seq2, input);

ParallelAndNaive par1(input, 4);
test("ParallelAndNaive", par1, input);

Expand Down

0 comments on commit 22afd2c

Please sign in to comment.