| 1 |
#ifndef __PLINK2_BITMAP_H__ |
| 2 |
#define __PLINK2_BITMAP_H__ |
| 3 |
|
| 4 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This library is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU Lesser General Public License as published by the |
| 9 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 10 |
// option) any later version. |
| 11 |
// |
| 12 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 15 |
// for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// Compressed-bitmap-file support. Similar to, but much simpler than, .pgen. |
| 21 |
// |
| 22 |
// Header format: |
| 23 |
// - Two initial magic bytes, 0x6c 0x1c. |
| 24 |
// - Number of rows, then number of columns, both as little-endian uint32s. |
| 25 |
// - Number of bytes (in 1..4) used for each row-record-byte-length. |
| 26 |
// - .pgen-style row block offsets, all but the last block have 2^16 rows. |
| 27 |
// - .pgen-style alternation of 2^16 row record types (2 bits each) and row |
| 28 |
// record byte-lengths. |
| 29 |
// The row record types are as follows: |
| 30 |
// 0 = Direct storage. |
| 31 |
// 1 = Differences-from-earlier-row encoding. The last row without this type |
| 32 |
// of encoding is the base. (There is of course no need for an analogue |
| 33 |
// of the "raregeno" field here, or in the last two record types.) |
| 34 |
// 2 = Simple difflist, base value is 0. |
| 35 |
// 3 = Simple difflist, base value is 1. |
| 36 |
|
| 37 |
#include "plink2_bits.h" |
| 38 |
|
| 39 |
#ifdef __cplusplus |
| 40 |
namespace plink2 { |
| 41 |
#endif |
| 42 |
|
| 43 |
CONSTI32(kPglRblockSize, 65536); |
| 44 |
CONSTI32(kPglMaxBitmapDifflistLenDivisor, 16); |
| 45 |
CONSTI32(kPglMaxBitmapBytesPerRow, 0x20000000); |
| 46 |
|
| 47 |
FLAGSET_DEF_START() |
| 48 |
kfPgrPrevdiffCache0, |
| 49 |
kfPgrPrevdiffCacheBit = (1 << 0), |
| 50 |
kfPgrPrevdiffCacheRawBit = (1 << 1), |
| 51 |
kfPgrPrevdiffCachePopcount = (1 << 2) |
| 52 |
FLAGSET_DEF_END(PgrPrevdiffCacheFlags); |
| 53 |
|
| 54 |
typedef struct BitmapReaderMainStruct { |
| 55 |
NONCOPYABLE(BitmapReaderMainStruct); |
| 56 |
// ----- Header information, constant after initialization ----- |
| 57 |
uint32_t row_ct; |
| 58 |
uint32_t col_ct; |
| 59 |
|
| 60 |
// size (row_ct + 1) so that the number of bytes of (zero-based) row n is |
| 61 |
// row_fpos[n+1] - row_fpos[n]. |
| 62 |
// --pmerge can *almost* use a sequential reader, which would let us get away |
| 63 |
// with a much smaller memory allocation here. Unfortunately, same-position |
| 64 |
// variants may need to be reordered by ID, and that makes it simpler to |
| 65 |
// stick to a random-access API with this entire array preloaded. |
| 66 |
uint64_t* row_fpos; |
| 67 |
|
| 68 |
uintptr_t* rrtype_nyparr; |
| 69 |
|
| 70 |
// ----- Mutable state ----- |
| 71 |
// If we don't fseek, what's the next row we'd read? |
| 72 |
uint32_t fp_ridx; |
| 73 |
|
| 74 |
FILE* ff; |
| 75 |
// allocated to word boundary, any trailing bytes always zeroed |
| 76 |
unsigned char* fread_buf; |
| 77 |
|
| 78 |
uint32_t prevdiff_ridx; |
| 79 |
PgrPrevdiffCacheFlags prevdiff_stypes; |
| 80 |
|
| 81 |
uintptr_t* prevdiff_base_raw_bitvec; // trailing bytes always zeroed |
| 82 |
uintptr_t* prevdiff_base_bitvec; // after col_include applied |
| 83 |
// should add prevdiff_base_col_ids (and uint32_t list length) if we add a |
| 84 |
// DifflistOrBitvec getter |
| 85 |
|
| 86 |
// might want workspace and/or prevdiff_base_raw_bitvec popcount |
| 87 |
} BitmapReaderMain; |
| 88 |
|
| 89 |
typedef struct BitmapReaderStruct { |
| 90 |
#ifdef __cplusplus |
| 91 |
BitmapReaderMain& GET_PRIVATE_m() { return m; } |
| 92 |
BitmapReaderMain const& GET_PRIVATE_m() const { return m; } |
| 93 |
private: |
| 94 |
#endif |
| 95 |
BitmapReaderMain m; |
| 96 |
} BitmapReader; |
| 97 |
|
| 98 |
HEADER_INLINE uintptr_t GetRrtype(const BitmapReaderMain* brp, uint32_t ridx) { |
| 99 |
return GetNyparrEntry(brp->rrtype_nyparr, ridx); |
| 100 |
} |
| 101 |
|
| 102 |
HEADER_INLINE uint64_t GetBrpFpos(const BitmapReaderMain* brp, uint32_t ridx) { |
| 103 |
return brp->row_fpos[ridx]; |
| 104 |
} |
| 105 |
|
| 106 |
HEADER_INLINE uint64_t GetBrpRrecWidth(const BitmapReaderMain* brp, uint32_t ridx) { |
| 107 |
return brp->row_fpos[ridx + 1] - brp->row_fpos[ridx]; |
| 108 |
} |
| 109 |
|
| 110 |
HEADER_INLINE uint32_t RrtypeIsPrevdiff(uint32_t rrtype) { |
| 111 |
return (rrtype == 1); |
| 112 |
} |
| 113 |
|
| 114 |
HEADER_INLINE void PgrClearPrevdiffCache(BitmapReader* br_ptr) { |
| 115 |
BitmapReaderMain* brp = &GET_PRIVATE(*br_ptr, m); |
| 116 |
brp->prevdiff_stypes &= kfPgrPrevdiffCacheRawBit; |
| 117 |
brp->prevdiff_ridx = 0x80000000U; |
| 118 |
} |
| 119 |
|
| 120 |
typedef struct PgrColSubsetIndexStruct { |
| 121 |
#ifdef __cplusplus |
| 122 |
const uint32_t*& GET_PRIVATE_cumulative_popcounts() { return cumulative_popcounts; } |
| 123 |
const uint32_t* const& GET_PRIVATE_cumulative_popcounts() const { return cumulative_popcounts; } |
| 124 |
private: |
| 125 |
#endif |
| 126 |
const uint32_t* cumulative_popcounts; |
| 127 |
} PgrColSubsetIndex; |
| 128 |
|
| 129 |
HEADER_INLINE void PgrSetColSubsetIndex(const uint32_t* col_include_cumulative_popcounts, BitmapReader* br_ptr, PgrColSubsetIndex* pcsi_ptr) { |
| 130 |
GET_PRIVATE(*pcsi_ptr, cumulative_popcounts) = col_include_cumulative_popcounts; |
| 131 |
PgrClearPrevdiffCache(br_ptr); |
| 132 |
} |
| 133 |
|
| 134 |
HEADER_INLINE void PgrClearColSubsetIndex(BitmapReader* br_ptr, PgrColSubsetIndex* pcsi_ptr) { |
| 135 |
GET_PRIVATE(*pcsi_ptr, cumulative_popcounts) = nullptr; |
| 136 |
if (br_ptr) { |
| 137 |
PgrClearPrevdiffCache(br_ptr); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void PreinitBitmapReader(BitmapReader* br_ptr); |
| 142 |
|
| 143 |
PglErr BitmapReaderInitPhase1(const char* fname, BitmapReader* br_ptr, uintptr_t* br_alloc_cacheline_ct_ptr, char* errstr_buf); |
| 144 |
|
| 145 |
PglErr BitmapReaderInitPhase2(BitmapReader* br_ptr, unsigned char* br_alloc, char* errstr_buf); |
| 146 |
|
| 147 |
HEADER_INLINE uint32_t BitmapReaderRowCt(const BitmapReader* br_ptr) { |
| 148 |
const BitmapReaderMain* brp = &GET_PRIVATE(*br_ptr, m); |
| 149 |
return brp->row_ct; |
| 150 |
} |
| 151 |
|
| 152 |
HEADER_INLINE uint32_t BitmapReaderColCt(const BitmapReader* br_ptr) { |
| 153 |
const BitmapReaderMain* brp = &GET_PRIVATE(*br_ptr, m); |
| 154 |
return brp->col_ct; |
| 155 |
} |
| 156 |
|
| 157 |
PglErr BitmapGet(const uintptr_t* __restrict col_include, PgrColSubsetIndex pcsi, uint32_t col_ct, uint32_t ridx, BitmapReader* br_ptr, uintptr_t* __restrict dst); |
| 158 |
|
| 159 |
typedef struct BitmapWriterMainStruct { |
| 160 |
NONCOPYABLE(BitmapWriterMainStruct); |
| 161 |
uint32_t row_ct; |
| 162 |
uint32_t col_ct; |
| 163 |
|
| 164 |
uint64_t* rblock_fpos; |
| 165 |
unsigned char* rrec_len_buf; |
| 166 |
uintptr_t* rrtype_buf; |
| 167 |
|
| 168 |
uint32_t prevdiff_base_set_bit_ct; |
| 169 |
|
| 170 |
// should match ftello() return value |
| 171 |
uint64_t rblock_fpos_offset; |
| 172 |
|
| 173 |
// these must hold col_ct entries |
| 174 |
uintptr_t* difflist_bitvec_buf; |
| 175 |
uintptr_t* prevdiff_base_bitvec; |
| 176 |
|
| 177 |
unsigned char* fwrite_buf; |
| 178 |
unsigned char* fwrite_bufp; |
| 179 |
|
| 180 |
uint32_t prevdiff_list_len; |
| 181 |
|
| 182 |
uintptr_t rrec_len_byte_ct; |
| 183 |
|
| 184 |
uint32_t ridx; |
| 185 |
|
| 186 |
FILE* ff; |
| 187 |
} BitmapWriterMain; |
| 188 |
|
| 189 |
typedef struct BitmapWriterStruct { |
| 190 |
#ifdef __cplusplus |
| 191 |
BitmapWriterMain& GET_PRIVATE_m() { return m; } |
| 192 |
BitmapWriterMain const& GET_PRIVATE_m() const { return m; } |
| 193 |
private: |
| 194 |
#endif |
| 195 |
BitmapWriterMain m; |
| 196 |
} BitmapWriter; |
| 197 |
|
| 198 |
void PreinitBitmapWriter(BitmapWriter* bw_ptr); |
| 199 |
|
| 200 |
PglErr BitmapWriterInitPhase1(const char* fname, uint32_t row_ct, uint32_t col_ct, BitmapWriter* bw_ptr, uintptr_t* bw_alloc_cacheline_ct_ptr); |
| 201 |
|
| 202 |
PglErr BitmapWriterInitPhase2(BitmapWriter* bw_ptr, unsigned char* bw_alloc); |
| 203 |
|
| 204 |
// trailing bits of bitvec must be zeroed out |
| 205 |
PglErr BitmapAppend(const uintptr_t* bitvec, BitmapWriter* bw_ptr); |
| 206 |
|
| 207 |
PglErr BitmapWriterFinish(BitmapWriter* bw_ptr); |
| 208 |
|
| 209 |
BoolErr CleanupBitmapReader(BitmapReader* br_ptr, PglErr* reterrp); |
| 210 |
|
| 211 |
BoolErr CleanupBitmapWriter(BitmapWriter* bw_ptr, PglErr* reterrp); |
| 212 |
|
| 213 |
#ifdef __cplusplus |
| 214 |
} // namespace plink2 |
| 215 |
#endif |
| 216 |
|
| 217 |
#endif // __PLINK2_BITMAP_H__ |