1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
* Copyright 2018, Chanhee Park <parkchanhee@gmail.com> and Daehwan Kim <infphilo@gmail.com>
*
* This file is part of HISAT 2.
*
* HISAT 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HISAT 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HISAT 2. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include "timer.h"
#include "aligner_sw.h"
#include "aligner_result.h"
#include "scoring.h"
#include "sstring.h"
#include "bit_packed_array.h"
TIndexOffU BitPackedArray::get(size_t index) const
{
assert_lt(index, cur_);
pair<size_t, size_t> addr = indexToAddress(index);
uint64_t *block = blocks_[addr.first];
pair<size_t, size_t> pos = columnToPosition(addr.second);
TIndexOffU val = getItem(block, pos.first, pos.second);
return val;
}
#define write_fp(x) fp.write((const char *)&(x), sizeof((x)))
void BitPackedArray::writeFile(ofstream &fp)
{
size_t sz = 0;
write_fp(item_bit_size_);
write_fp(elm_bit_size_);
write_fp(items_per_block_bit_);
write_fp(items_per_block_bit_mask_);
write_fp(items_per_block_);
write_fp(cur_);
write_fp(sz_);
write_fp(block_size_);
// number of blocks
sz = blocks_.size();
write_fp(sz);
for(size_t i = 0; i < sz; i++) {
fp.write((const char *)blocks_[i], block_size_);
}
}
void BitPackedArray::writeFile(const char *filename)
{
ofstream fp(filename, std::ofstream::binary);
writeFile(fp);
fp.close();
}
void BitPackedArray::writeFile(const string &filename)
{
writeFile(filename.c_str());
}
#define read_fp(x) fp.read((char *)&(x), sizeof((x)))
void BitPackedArray::readFile(ifstream &fp)
{
size_t val_sz = 0;
read_fp(val_sz);
init_by_log2(val_sz);
//rt_assert_eq(val_sz, item_bit_size_);
read_fp(val_sz);
rt_assert_eq(val_sz, elm_bit_size_);
read_fp(val_sz);
rt_assert_eq(val_sz, items_per_block_bit_);
read_fp(val_sz);
rt_assert_eq(val_sz, items_per_block_bit_mask_);
read_fp(val_sz);
rt_assert_eq(val_sz, items_per_block_);
// skip cur_
size_t prev_cnt = 0;
read_fp(prev_cnt);
cur_ = 0;
// skip sz_
size_t prev_sz = 0;
read_fp(prev_sz);
sz_ = 0;
// block_size_
read_fp(val_sz);
rt_assert_eq(val_sz, block_size_);
// alloc blocks
allocItems(prev_cnt);
rt_assert_eq(prev_sz, sz_);
// number of blocks
read_fp(val_sz);
rt_assert_eq(val_sz, blocks_.size());
for(size_t i = 0; i < blocks_.size(); i++) {
fp.read((char *)blocks_[i], block_size_);
}
cur_ = prev_cnt;
}
void BitPackedArray::readFile(const char *filename)
{
ifstream fp(filename, std::ifstream::binary);
readFile(fp);
fp.close();
}
void BitPackedArray::readFile(const string &filename)
{
readFile(filename.c_str());
}
void BitPackedArray::put(size_t index, TIndexOffU val)
{
assert_lt(index, cur_);
pair<size_t, size_t> addr = indexToAddress(index);
uint64_t *block = blocks_[addr.first];
pair<size_t, size_t> pos = columnToPosition(addr.second);
setItem(block, pos.first, pos.second, val);
}
void BitPackedArray::pushBack(TIndexOffU val)
{
if(cur_ == sz_) {
allocItems(items_per_block_);
}
put(cur_++, val);
assert_leq(cur_, sz_);
}
TIndexOffU BitPackedArray::getItem(uint64_t *block, size_t idx, size_t offset) const
{
size_t remains = item_bit_size_;
TIndexOffU val = 0;
while(remains > 0) {
size_t bits = min(elm_bit_size_ - offset, remains);
uint64_t mask = bitToMask(bits);
// get value from block
TIndexOffU t = (block[idx] >> offset) & mask;
val = val | (t << (item_bit_size_ - remains));
remains -= bits;
offset = 0;
idx++;
}
return val;
}
void BitPackedArray::setItem(uint64_t *block, size_t idx, size_t offset, TIndexOffU val)
{
size_t remains = item_bit_size_;
while(remains > 0) {
size_t bits = min(elm_bit_size_ - offset, remains);
uint64_t mask = bitToMask(bits);
uint64_t dest_mask = mask << offset;
// get 'bits' lsb from val
uint64_t t = val & mask;
val >>= bits;
// save 't' to block[idx]
t <<= offset;
block[idx] &= ~(dest_mask); // clear
block[idx] |= t;
idx++;
remains -= bits;
offset = 0;
}
}
pair<size_t, size_t> BitPackedArray::indexToAddress(size_t index) const
{
pair<size_t, size_t> addr;
addr.first = index >> items_per_block_bit_;
addr.second = index & items_per_block_bit_mask_;
return addr;
}
pair<size_t, size_t> BitPackedArray::columnToPosition(size_t col) const {
pair<size_t, size_t> pos;
pos.first = (col * item_bit_size_) / elm_bit_size_;
pos.second = (col * item_bit_size_) % elm_bit_size_;
return pos;
}
void BitPackedArray::expand(size_t count)
{
if((cur_ + count) > sz_) {
allocItems(count);
}
cur_ += count;
assert_leq(cur_, sz_);
}
void BitPackedArray::allocSize(size_t sz)
{
size_t num_block = (sz * sizeof(uint64_t) + block_size_ - 1) / block_size_;
for(size_t i = 0; i < num_block; i++) {
uint64_t *ptr = new uint64_t[block_size_];
blocks_.push_back(ptr);
sz_ += items_per_block_;
}
}
void BitPackedArray::allocItems(size_t count)
{
size_t sz = (count * item_bit_size_ + elm_bit_size_ - 1) / elm_bit_size_;
allocSize(sz);
}
void BitPackedArray::init_by_log2(size_t ceil_log2)
{
item_bit_size_ = ceil_log2;
elm_bit_size_ = sizeof(uint64_t) * 8;
items_per_block_bit_ = 20; // 1M
items_per_block_ = 1ULL << (items_per_block_bit_);
items_per_block_bit_mask_ = items_per_block_ - 1;
block_size_ = (items_per_block_ * item_bit_size_ + elm_bit_size_ - 1) / elm_bit_size_ * sizeof(uint64_t);
cur_ = 0;
sz_ = 0;
}
void BitPackedArray::init(size_t max_value)
{
init_by_log2((size_t)ceil(log2(max_value)));
}
void BitPackedArray::dump() const
{
cerr << "item_bit_size_: " << item_bit_size_ << endl;
cerr << "block_size_: " << block_size_ << endl;
cerr << "items_per_block_: " << items_per_block_ << endl;
cerr << "cur_: " << cur_ << endl;
cerr << "sz_: " << sz_ << endl;
cerr << "number of blocks: " << blocks_.size() << endl;
}
size_t BitPackedArray::getMemUsage() const
{
size_t tot = blocks_.size() * block_size_;
tot += blocks_.totalCapacityBytes();
return tot;
}
BitPackedArray::~BitPackedArray()
{
for(size_t i = 0; i < blocks_.size(); i++) {
uint64_t *ptr = blocks_[i];
delete [] ptr;
}
}
void BitPackedArray::reset()
{
cur_ = 0;
sz_ = 0;
for(size_t i = 0; i < blocks_.size(); i++) {
uint64_t *ptr = blocks_[i];
delete [] ptr;
}
blocks_.clear();
}
|