From 2fe3be6bcf6a558282c536ac3a3fe94c3d48edbb Mon Sep 17 00:00:00 2001 From: "Tim J. Baumann" Date: Tue, 7 Aug 2018 14:01:53 +0200 Subject: [PATCH 1/4] Use std::vector instead of very large fixed-size array The fixed-size array allocation caused a bad::alloc error on my machine, which is now gone. --- bitmap.cc | 15 +++++++++------ bitmap.h | 11 ++++++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bitmap.cc b/bitmap.cc index 3e38150..3dd7c97 100644 --- a/bitmap.cc +++ b/bitmap.cc @@ -40,7 +40,8 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits) uint64 basicBlockId = 0; pCount_ = 0; - memset(locCount_, 0, sizeof(locCount_)); + loc_.reserve(l1EntryCount_); + locCount_.reserve(l1EntryCount_); for (uint64 i = 0; i < l1EntryCount_; i++) { l1Entries_[i] = pCount_; @@ -50,8 +51,8 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits) l2Entries_[l2Id] = cum; for (int offset = 0; offset < 30; offset += 10) { - int c = popcountLinear(bits_, - basicBlockId * kWordCountPerBasicBlock, + int c = popcountLinear(bits_, + basicBlockId * kWordCountPerBasicBlock, kBasicBlockSize); cum += c; basicBlockId++; @@ -63,7 +64,8 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits) if (++l2Id >= l2EntryCount_) break; } - locCount_[i] = (cum + kLocFreq - 1) / kLocFreq; + assert(locCount_.size() == i); + locCount_.push_back((cum + kLocFreq - 1) / kLocFreq); pCount_ += cum; } @@ -71,7 +73,8 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits) //fprintf(stdout, "Starting the loop: %ld \n", locCount_[0]); for (uint64 i = 0; i < l1EntryCount_; i++) { - loc_[i] = new uint32[locCount_[i]]; + assert(loc_.size() == i); + loc_.push_back(new uint32[locCount_[i]]); //fprintf(stdout, "Starting the loop: %ld %ld \n", i, locCount_[i]); locCount_[i] = 0; @@ -124,7 +127,7 @@ uint64 BitmapPoppy::select(uint64 rank) //struct timeval tv_start, tv_end; //gettimeofday(&tv_start, NULL); - + uint64 l1Id; for (l1Id = l1EntryCount_ - 1; l1Id >= 0; l1Id--) { if (l1Entries_[l1Id] < rank) { diff --git a/bitmap.h b/bitmap.h index b10d60c..bc502d7 100644 --- a/bitmap.h +++ b/bitmap.h @@ -29,6 +29,7 @@ limitations under the License. #include #define __STDC_FORMAT_MACROS #include +#include #include "shared.h" @@ -55,7 +56,11 @@ class Bitmap { class BitmapPoppy: public Bitmap { public: BitmapPoppy(uint64* bits, uint64 nbits); - ~BitmapPoppy() {} + ~BitmapPoppy() { + for (uint32* p : loc_) { + delete[] p; + } + } uint64 rank(uint64 pos); uint64 select(uint64 rank); @@ -71,8 +76,8 @@ class BitmapPoppy: public Bitmap { uint64 l1EntryCount_; uint64 basicBlockCount_; - uint32* loc_[1ULL << 31]; - uint64 locCount_[1ULL << 31]; + std::vector loc_; + std::vector locCount_; static const int kLocFreq = 8192; static const int kLocFreqMask = 8191; From 9ee76737be87c950a91af14c107d117e8f0c3b8c Mon Sep 17 00:00:00 2001 From: "Tim J. Baumann" Date: Tue, 7 Aug 2018 14:08:55 +0200 Subject: [PATCH 2/4] Mark BitmapPoppy as final --- bitmap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitmap.h b/bitmap.h index bc502d7..d897d8e 100644 --- a/bitmap.h +++ b/bitmap.h @@ -53,7 +53,7 @@ class Bitmap { uint64 pCount_; }; -class BitmapPoppy: public Bitmap { +class BitmapPoppy final: public Bitmap { public: BitmapPoppy(uint64* bits, uint64 nbits); ~BitmapPoppy() { From abd4345ff853f7bb773434a3887aa28c9ab81cd5 Mon Sep 17 00:00:00 2001 From: "Tim J. Baumann" Date: Wed, 8 Aug 2018 01:12:22 +0200 Subject: [PATCH 3/4] Fix allocation of l1Entries_ and l2Entries_ Before, the allocations of these arrays were done in assert(...) calls. This is problematic since when building with -NDEBUG, these assert(...) calls are removed, which causes l1Entries_ and l2Entries_ to not be initialized. --- bitmap.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bitmap.cc b/bitmap.cc index 3dd7c97..351d66b 100644 --- a/bitmap.cc +++ b/bitmap.cc @@ -33,8 +33,14 @@ BitmapPoppy::BitmapPoppy(uint64 *bits, uint64 nbits) l2EntryCount_ = nbits_ >> 11; basicBlockCount_ = nbits_ / kBasicBlockSize; - assert(posix_memalign((void **) &l1Entries_, kCacheLineSize, l1EntryCount_ * sizeof(uint64)) >= 0); - assert(posix_memalign((void **) &l2Entries_, kCacheLineSize, l2EntryCount_ * sizeof(uint64)) >= 0); + if (posix_memalign((void **) &l1Entries_, kCacheLineSize, l1EntryCount_ * sizeof(uint64)) != 0) { + fprintf(stderr, "allocation of l1Entries_ failed"); + exit(1); + } + if (posix_memalign((void **) &l2Entries_, kCacheLineSize, l2EntryCount_ * sizeof(uint64)) != 0) { + fprintf(stderr, "allocation of l2Entries_ failed"); + exit(1); + } uint64 l2Id = 0; uint64 basicBlockId = 0; From 566182d9e2f5861b34dde0807d95c806776baf0d Mon Sep 17 00:00:00 2001 From: "Tim J. Baumann" Date: Wed, 8 Aug 2018 01:28:43 +0200 Subject: [PATCH 4/4] Free l1Entries_ and l2Entries_ after use --- bitmap.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bitmap.h b/bitmap.h index d897d8e..2a1323a 100644 --- a/bitmap.h +++ b/bitmap.h @@ -60,6 +60,8 @@ class BitmapPoppy final: public Bitmap { for (uint32* p : loc_) { delete[] p; } + free(static_cast(l1Entries_)); + free(static_cast(l2Entries_)); } uint64 rank(uint64 pos);