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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
Authors: Marek Kokot
Version: 3.2.4
Date : 2024-02-09
*/
#ifndef _QUEUES_H_
#define _QUEUES_H_
#include "defs.h"
#include "bundle.h"
#include <mutex>
#include <vector>
#include <condition_variable>
#include <list>
#include <queue>
class CSufWriteQueue
{
uint32 buf_size;
uint32 max_inside;
using elem_t = std::pair<uchar*, uint32>;
std::list<elem_t> content;
mutable std::mutex mtx;
uint32 n_writers;
std::condition_variable cv_pop, cv_push;
public:
void init(uint32 _buf_size, uint32 _max_inside)
{
buf_size = _buf_size;
max_inside = _max_inside;
n_writers = 1;
}
void push(uchar* &buf, uint32 size)
{
std::unique_lock<std::mutex> lck(mtx);
cv_push.wait(lck, [this]{return content.size() < max_inside; });
bool was_empty = content.empty();
content.push_back(std::make_pair(buf, size));
buf = new uchar[buf_size];
if (was_empty)
cv_pop.notify_all();
}
bool pop(uchar* &buf, uint32& size)
{
std::unique_lock<std::mutex> lck(mtx);
cv_pop.wait(lck, [this]{return !content.empty() || !n_writers; });
if (!n_writers && content.empty())
return false;
bool was_full = max_inside == content.size();
buf = content.front().first;
size = content.front().second;
content.pop_front();
if (was_full)
cv_push.notify_all();
return true;
}
void mark_completed()
{
std::lock_guard<std::mutex> lck(mtx);
--n_writers;
if (!n_writers)
cv_pop.notify_all();
}
};
template<unsigned SIZE> class CCircularQueue
{
std::vector<CBundleData<SIZE>> buff;
bool full, is_completed;
int start, end;
mutable std::mutex mtx;
std::condition_variable cv_push;
std::condition_variable cv_pop;
bool forced_to_finish = false;
public:
CCircularQueue(int size, uint32 bundle_size) : full(false), is_completed(false), start(0), end(0)
{
buff.reserve(size);
for (int i = 0; i < size; ++i)
buff.emplace_back(bundle_size);
}
CCircularQueue(int size) : buff(size), full(false), is_completed(false), start(0), end(0)
{
}
bool push(CBundleData<SIZE>& bundle_data)
{
std::unique_lock<std::mutex> lck(mtx);
cv_push.wait(lck, [this]{return !full || forced_to_finish; });
if (forced_to_finish)
{
return false;
}
bool was_empty = start == end;
std::swap(buff[end], bundle_data);
bundle_data.Clear();
end = (end + 1) % buff.size();
if (end == start)
full = true;
if (was_empty)
cv_pop.notify_all();
return true;
}
bool pop(CBundleData<SIZE>& bundle_data)
{
std::unique_lock<std::mutex> lck(mtx);
cv_pop.wait(lck, [this]{ return start != end || full || is_completed || forced_to_finish; });
if (forced_to_finish)
return false;
if (is_completed && !full && start == end)
return false;
bool was_full = full;
std::swap(buff[start], bundle_data);
buff[start].Clear();
start = (start + 1) % buff.size();
full = false;
if (was_full)
cv_push.notify_all();
return true;
}
void mark_completed()
{
std::lock_guard<std::mutex> lck(mtx);
is_completed = true;
cv_pop.notify_all();
}
void force_finish()
{
std::lock_guard<std::mutex> lck(mtx);
forced_to_finish = true;
cv_pop.notify_all();
cv_push.notify_all();
}
};
class CInputFilesQueue {
typedef std::string elem_t;
typedef std::queue<elem_t, std::list<elem_t>> queue_t;
queue_t q;
mutable std::mutex mtx; // The mutex to synchronise on
public:
CInputFilesQueue(const std::vector<std::string> &file_names) {
std::unique_lock<std::mutex> lck(mtx);
for (auto p = file_names.cbegin(); p != file_names.cend(); ++p)
q.push(*p);
};
bool pop(std::string &file_name) {
std::lock_guard<std::mutex> lck(mtx);
if (q.empty())
return false;
file_name = q.front();
q.pop();
return true;
}
};
class CMemoryPool {
int64 total_size;
int64 part_size;
int64 n_parts_total;
int64 n_parts_free;
uchar *buffer, *raw_buffer;
uint32 *stack;
mutable std::mutex mtx; // The mutex to synchronise on
std::condition_variable cv; // The condition to wait for
public:
CMemoryPool(int64 _total_size, int64 _part_size) {
raw_buffer = NULL;
buffer = NULL;
stack = NULL;
prepare(_total_size, _part_size);
}
~CMemoryPool() {
release();
}
void prepare(int64 _total_size, int64 _part_size) {
release();
n_parts_total = _total_size / _part_size;
part_size = (_part_size + 15) / 16 * 16; // to allow mapping pointer to int*
n_parts_free = n_parts_total;
total_size = n_parts_total * part_size;
raw_buffer = new uchar[total_size + 64];
buffer = raw_buffer;
while (((uint64)buffer) % 64)
buffer++;
stack = new uint32[n_parts_total];
for (uint32 i = 0; i < n_parts_total; ++i)
stack[i] = i;
}
void release(void) {
if (raw_buffer)
delete[] raw_buffer;
raw_buffer = NULL;
buffer = NULL;
if (stack)
delete[] stack;
stack = NULL;
}
// Allocate memory buffer - uchar*
void reserve(uchar* &part)
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [this]{return n_parts_free > 0; });
part = buffer + stack[--n_parts_free] * part_size;
}
// Allocate memory buffer - char*
void reserve(char* &part)
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [this]{return n_parts_free > 0; });
part = (char*)(buffer + stack[--n_parts_free] * part_size);
}
// Allocate memory buffer - uint32*
void reserve(uint32* &part)
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [this]{return n_parts_free > 0; });
part = (uint32*)(buffer + stack[--n_parts_free] * part_size);
}
// Allocate memory buffer - uint64*
void reserve(uint64* &part)
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [this]{return n_parts_free > 0; });
part = (uint64*)(buffer + stack[--n_parts_free] * part_size);
}
// Allocate memory buffer - double*
void reserve(double* &part)
{
std::unique_lock<std::mutex> lck(mtx);
cv.wait(lck, [this]{return n_parts_free > 0; });
part = (double*)(buffer + stack[--n_parts_free] * part_size);
}
// Deallocate memory buffer - uchar*
void free(uchar* part)
{
std::lock_guard<std::mutex> lck(mtx);
stack[n_parts_free++] = (uint32)((part - buffer) / part_size);
cv.notify_all();
}
// Deallocate memory buffer - char*
void free(char* part)
{
std::lock_guard<std::mutex> lck(mtx);
stack[n_parts_free++] = (uint32)(((uchar*)part - buffer) / part_size);
cv.notify_all();
}
// Deallocate memory buffer - uint32*
void free(uint32* part)
{
std::lock_guard<std::mutex> lck(mtx);
stack[n_parts_free++] = (uint32)((((uchar *)part) - buffer) / part_size);
cv.notify_all();
}
// Deallocate memory buffer - uint64*
void free(uint64* part)
{
std::lock_guard<std::mutex> lck(mtx);
stack[n_parts_free++] = (uint32)((((uchar *)part) - buffer) / part_size);
cv.notify_all();
}
// Deallocate memory buffer - double*
void free(double* part)
{
std::lock_guard<std::mutex> lck(mtx);
stack[n_parts_free++] = (uint32)((((uchar *)part) - buffer) / part_size);
cv.notify_all();
}
};
class CPartQueue {
typedef std::pair<uchar *, uint64> elem_t;
typedef std::queue<elem_t, std::list<elem_t>> queue_t;
queue_t q;
bool is_completed;
int n_readers;
mutable std::mutex mtx; // The mutex to synchronise on
std::condition_variable cv_queue_empty;
public:
CPartQueue(int _n_readers) {
std::unique_lock<std::mutex> lck(mtx);
is_completed = false;
n_readers = _n_readers;
};
~CPartQueue() {};
bool empty() {
std::lock_guard<std::mutex> lck(mtx);
return q.empty();
}
bool completed() {
std::lock_guard<std::mutex> lck(mtx);
return q.empty() && !n_readers;
}
void mark_completed() {
std::lock_guard<std::mutex> lck(mtx);
n_readers--;
if (!n_readers)
cv_queue_empty.notify_all();
}
void push(uchar *part, uint64 size) {
std::unique_lock<std::mutex> lck(mtx);
bool was_empty = q.empty();
q.push(std::make_pair(part, size));
if (was_empty)
cv_queue_empty.notify_all();
}
bool pop(uchar *&part, uint64 &size) {
std::unique_lock<std::mutex> lck(mtx);
cv_queue_empty.wait(lck, [this]{return !this->q.empty() || !this->n_readers; });
if (q.empty())
return false;
std::tie(part, size) = q.front();
q.pop();
return true;
}
};
struct CFilteringQueues
{
CInputFilesQueue *input_files_queue;
CPartQueue *input_part_queue, *filtered_part_queue;
CMemoryPool *pmm_fastq_reader;
CMemoryPool *pmm_fastq_filter;
};
#endif
|