Newer
Older
* Copyright (c) 2015-2018, Luca Fulchir<luca@fulchir.it>, All rights reserved.
*
* This file is part of "libRaptorQ".
*
* libRaptorQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* libRaptorQ 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
* and a copy of the GNU Lesser General Public License
* along with libRaptorQ. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RaptorQ/v1/common.hpp"
#include "RaptorQ/v1/block_sizes.hpp"
#ifdef RQ_HEADER_ONLY
#include "RaptorQ/v1/RaptorQ_Iterators.hpp"
#endif
#include "RaptorQ/v1/Encoder.hpp"
#include "RaptorQ/v1/Decoder.hpp"
template <typename Rnd_It, typename Fwd_It = Rnd_It>
class RAPTORQ_LOCAL Encoder;
template <typename In_It, typename Fwd_It = In_It>
class RAPTORQ_LOCAL Decoder;
} // namespace Impl
// expose classes, but only if header-only
#ifdef RQ_HEADER_ONLY
template <typename Rnd_It, typename Fwd_It>
template <typename Rnd_It, typename Fwd_It>
template <typename Rnd_It, typename Fwd_It>
class RAPTORQ_LOCAL Encoder
{
public:
Encoder (const Block_Size symbols, const size_t symbol_size);
Encoder() = delete;
Encoder (const Encoder&) = delete;
Encoder& operator= (const Encoder&) = delete;
Encoder (Encoder &&) = delete;
Encoder& operator= (Encoder &&) = delete;
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> begin_source();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> end_source();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> begin_repair();
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> end_repair
size_t set_data (const Rnd_It &from, const Rnd_It &to);
void clear_data();
std::shared_future<Error> precompute();
std::shared_future<Error> compute();
size_t encode (Fwd_It &output, const Fwd_It end, const uint32_t id);
enum class Enc_State : uint8_t {
INIT_ERROR = 1,
NEED_DATA = 2,
FULL = 3
};
Raw_Encoder<Rnd_It, Fwd_It, without_interleaver> encoder;
DenseMtx precomputed;
// avoid launching multiple computations for the encoder.
// it is guaranteed to succeed anyway.
std::mutex _mtx;
std::shared_future<Error> _single_wait;
std::thread _waiting;
};
template <typename In_It, typename Fwd_It>
class RAPTORQ_LOCAL Decoder
{
public:
Decoder (const Block_Size symbols, const size_t symbol_size,
Decoder (const Decoder&) = delete;
Decoder& operator= (const Decoder&) = delete;
Decoder (Decoder &&) = delete;
Decoder& operator= (Decoder &&) = delete;
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> begin();
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> end();
Error add_symbol (In_It &from, const In_It to, const uint32_t esi);
std::vector<bool> end_of_input (const Fill_With_Zeros fill);
void set_max_concurrency (const uint16_t max_threads);
Decoder_Result decode_once();
struct Decoder_wait_res poll();
struct Decoder_wait_res wait_sync();
std::future<struct Decoder_wait_res> wait();
Error decode_symbol (Fwd_It &start, const Fwd_It end, const uint16_t esi);
struct Decoder_written decode_bytes (Fwd_It &start, const Fwd_It end,
RaptorQ__v1::Work_State work;
Raw_Decoder<In_It> dec;
// 2* symbols. Actually tracks available and reported symbols.
// each symbol gets 2 bool: 1= available, 2=reported
std::deque<std::atomic<bool>> symbols_tracker;
std::mutex _mtx;
std::condition_variable _cond;
std::vector<std::thread> waiting;
static void waiting_thread (Decoder<In_It, Fwd_It> *obj,
std::promise<struct Decoder_wait_res> p);
};
///////////////////
//// Encoder
///////////////////
template <typename Rnd_It, typename Fwd_It>
Encoder<Rnd_It, Fwd_It>::~Encoder()
{
}
template <typename Rnd_It, typename Fwd_It>
Encoder<Rnd_It, Fwd_It>::Encoder (const Block_Size symbols,
: _symbol_size (symbol_size), _symbols (static_cast<uint16_t> (symbols)),
encoder (symbols, _symbol_size)
IS_RANDOM(Rnd_It, "RaptorQ__v1::Encoder");
IS_FORWARD(Fwd_It, "RaptorQ__v1::Encoder");
// check for proper initialization
uint16_t idx;
for (idx = 0; idx < (*blocks).size(); ++idx) {
if ((*blocks)[idx] == symbols)
break;
}
// check that the user did not try some cast trickery,
// and maximum size is ssize_t::max. But ssize_t is not standard,
// so we search the maximum ourselves.
if (idx == (*blocks).size() || symbol_size >= std::pow (2,
(sizeof(size_t) == 4 ? 31 : 63))) {
_state = Enc_State::INIT_ERROR;
}
_state = Enc_State::NEED_DATA;
template <typename Rnd_It, typename Fwd_It>
Encoder<Rnd_It, Fwd_It>::operator bool() const
{ return _state != Enc_State::INIT_ERROR; }
template <typename Rnd_It, typename Fwd_It>
uint16_t Encoder<Rnd_It, Fwd_It>::symbols() const
{
if (_state == Enc_State::INIT_ERROR)
return 0;
if (_state == Enc_State::INIT_ERROR)
return 0;
}
template <typename Rnd_It, typename Fwd_It>
uint32_t Encoder<Rnd_It, Fwd_It>::max_repair() const
{
// you can have up to 56403 symbols in a block
// rfc6330 limits you to 992173 repair symbols
// but limits are meant to be broken!
// the limit sould be up to 4294967279 repair symbols 2^32-(_param.S + H)
// but people might misuse the API, and call end_repair(max_repair),
// which would overflow.
// We are sorry for taking away from you that 0.0014% of repair symbols.
if (_state == Enc_State::INIT_ERROR)
return 0;
auto _param = Parameters (_symbols);
return static_cast<uint32_t> (std::numeric_limits<uint32_t>::max() -
_param.L);
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (this, 0);
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (this,
}
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
template <typename Rnd_It, typename Fwd_It>
RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It>
Encoder<Rnd_It, Fwd_It>::end_repair (const uint32_t repair)
return RaptorQ__v1::It::Encoder::Symbol_Iterator<Rnd_It, Fwd_It> (nullptr,
bool Encoder<Rnd_It, Fwd_It>::has_data() const
{
if (_state == Enc_State::INIT_ERROR)
return false;
return _state == Enc_State::FULL;
}
template <typename Rnd_It, typename Fwd_It>
size_t Encoder<Rnd_It, Fwd_It>::set_data (const Rnd_It &from, const Rnd_It &to)
{
if (_state == Enc_State::INIT_ERROR)
return 0;
return static_cast<size_t>(_to - _from) *
sizeof(typename std::iterator_traits<Rnd_It>::value_type);
template <typename Rnd_It, typename Fwd_It>
void Encoder<Rnd_It, Fwd_It>::clear_data()
{
std::lock_guard<std::mutex> lock (_mtx);
RQ_UNUSED (lock);
if (_waiting.joinable()) {
encoder.stop();
_waiting.join();
}
_single_wait = std::shared_future<Error>();
template <typename Rnd_It, typename Fwd_It>
bool Encoder<Rnd_It, Fwd_It>::ready() const
{
if (_state == Enc_State::INIT_ERROR)
return false;
return encoder.ready();
}
template <typename Rnd_It, typename Fwd_It>
void Encoder<Rnd_It, Fwd_It>::stop()
{ encoder.stop(); }
template <typename Rnd_It, typename Fwd_It>
bool Encoder<Rnd_It, Fwd_It>::precompute_sync()
{
if (_state == Enc_State::INIT_ERROR)
return false;
if (_single_wait.valid()) {
_single_wait.wait();
return true;
std::unique_lock<std::mutex> lock (_mtx);
if (_single_wait.valid() && _single_wait.get() == Error::NONE)
return true;
stop();
if (_waiting.joinable())
_waiting.join();
std::promise<Error> p;
_single_wait = p.get_future().share();
lock.unlock();
compute_thread (this, true, std::move(p));
template <typename Rnd_It, typename Fwd_It>
bool Encoder<Rnd_It, Fwd_It>::compute_sync()
{
if (_state == Enc_State::INIT_ERROR)
return false;
if (_single_wait.valid())
_single_wait.wait();
std::unique_lock<std::mutex> lock (_mtx);
if (_single_wait.valid() && _single_wait.get() == Error::NONE)
stop();
if (_waiting.joinable())
_waiting.join();
std::promise<Error> p;
_single_wait = p.get_future().share();
lock.unlock();
compute_thread (this, false, std::move(p));
return true;
template <typename Rnd_It, typename Fwd_It>
void Encoder<Rnd_It, Fwd_It>::compute_thread (
Encoder<Rnd_It, Fwd_It> *obj, bool force_precomputation,
std::promise<Error> p)
static RaptorQ__v1::Work_State work = RaptorQ__v1::Work_State::KEEP_WORKING;
if (force_precomputation) {
if (obj->precomputed.rows() == 0)
obj->precomputed = obj->encoder.get_precomputed (&work);
if (obj->precomputed.rows() == 0) {
// encoder always works. only possible reason:
p.set_value (Error::EXITING);
return;
}
// if we finished getting data by the time the computation
// finished, update it all.
if (obj->_state == Enc_State::FULL && !obj->encoder.ready())
obj->encoder.generate_symbols (obj->precomputed,
&obj->_from, &obj->_to);
p.set_value (Error::NONE);
} else {
if (obj->encoder.ready()) {
p.set_value (Error::NONE);
return;
}
if (obj->encoder.generate_symbols (&work, &obj->_from, &obj->_to)) {
p.set_value (Error::NONE);
return;
} else {
// only possible reason:
p.set_value (Error::EXITING);
return;
}
} else {
if (obj->precomputed.rows() == 0) {
obj->precomputed = obj->encoder.get_precomputed (&work);
if (obj->precomputed.rows() == 0) {
// only possible reason:
p.set_value (Error::EXITING);
return;
}
}
// if we finished getting data by the time the computation
// finished, update it all.
obj->encoder.generate_symbols (obj->precomputed,
&obj->_from, &obj->_to);
}
p.set_value (Error::NONE);
return;
}
}
}
template <typename Rnd_It, typename Fwd_It>
std::shared_future<Error> Encoder<Rnd_It, Fwd_It>::precompute()
std::unique_lock<std::mutex> lock (_mtx);
if (_single_wait.valid())
return _single_wait;
stop();
if (_waiting.joinable())
_waiting.join();
std::promise<Error> p;
_single_wait = p.get_future().share();
_waiting = std::thread (compute_thread, this, true, std::move(p));
lock.unlock();
return _single_wait;
if (_state == Enc_State::INIT_ERROR) {
p.set_value (Error::INITIALIZATION);
std::unique_lock<std::mutex> lock (_mtx);
if (_single_wait.valid())
return _single_wait;
stop();
if (_waiting.joinable())
_waiting.join();
_single_wait = p.get_future().share();
_waiting = std::thread (compute_thread, this, false, std::move(p));
lock.unlock();
return _single_wait;
size_t Encoder<Rnd_It, Fwd_It>::encode (Fwd_It &output, const Fwd_It end,
if (_state == Enc_State::INIT_ERROR)
return 0;
if (id >= _symbols) { // repair symbol
if (!encoder.ready()) {
if (!_single_wait.valid())
_single_wait = compute();
_single_wait.wait();
}
}
///////////////////
//// Decoder
///////////////////
template <typename In_It, typename Fwd_It>
Decoder<In_It, Fwd_It>::~Decoder ()
{
lock.lock();
if (waiting.size() == 0) {
lock.unlock();
_cond.wait (lock);
lock.unlock();
} while (waiting.size() != 0);
Decoder<In_It, Fwd_It>::Decoder (const Block_Size symbols,
:_symbols (static_cast<uint16_t> (symbols)), _symbol_size (symbol_size),
_type (type), dec (symbols, symbol_size)
IS_INPUT(In_It, "RaptorQ__v1::Decoder");
IS_FORWARD(Fwd_It, "RaptorQ__v1::Decoder");
// check for proper initialization
uint16_t idx;
for (idx = 0; idx < (*blocks).size(); ++idx) {
if ((*blocks)[idx] == symbols)
break;
}
// check that the user did not try some cast trickery,
// and maximum size is ssize_t::max.
if (idx == (*blocks).size() || symbol_size >=
std::numeric_limits<ssize_t>::max()) {
if (type != Dec_Report::PARTIAL_FROM_BEGINNING &&
type != Dec_Report::PARTIAL_ANY &&
type != Dec_Report::COMPLETE) {
last_reported.store (0);
symbols_tracker = std::deque<std::atomic<bool>> (2 * _symbols);
symbols_tracker[idx] = false;
work = RaptorQ__v1::Work_State::KEEP_WORKING;
template <typename Rnd_It, typename Fwd_It>
Decoder<Rnd_It, Fwd_It>::operator bool() const
{ return symbols_tracker.size() > 0; }
template <typename In_It, typename Fwd_It>
uint16_t Decoder<In_It, Fwd_It>::symbols() const
{
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It>
return RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> (this, 0);
RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It>
return RaptorQ__v1::It::Decoder::Symbol_Iterator<In_It, Fwd_It> (nullptr,
_symbols);
template <typename In_It, typename Fwd_It>
uint16_t Decoder<In_It, Fwd_It>::needed_symbols() const
{
Error Decoder<In_It, Fwd_It>::add_symbol (In_It &from, const In_It to,
if (symbols_tracker.size() == 0)
return Error::INITIALIZATION;
if (ret == Error::NONE) {
if (esi < _symbols)
symbols_tracker [2 * esi].store (true);
std::unique_lock<std::mutex> lock (_mtx);
}
template <typename In_It, typename Fwd_It>
if (symbols_tracker.size() == 0)
return {Error::INITIALIZATION, 0};
uint32_t idx;
uint32_t last;
bool expected = false;
switch (_type) {
// report the number of symbols that are known, starting from
// the beginning.
last = last_reported.load();
idx = last;
for (; idx < symbols_tracker.size(); idx += 2) {
if (symbols_tracker[idx].load() == true) {
++idx;
if (symbols_tracker[idx].load() == false)
symbols_tracker[idx].store (true);
--idx;
} else {
break;
}
}
idx /= 2;
if (idx > last) {
while (!last_reported.compare_exchange_weak (last, idx)) {
// expected is now "last_reported.load()"
if (last >= idx) {
// other thread already reported more than us.
// do not report things twice.
if (dec.ready()) {
last_reported.store (_symbols);
return {Error::NONE, _symbols};
}
return {Error::WORKING, 0};
return {Error::NEED_DATA, 0};
}
// else we can report the new stuff
}
}
// nothing to report
if (dec.ready()) {
last_reported.store (_symbols);
return {Error::NONE, _symbols};
}
return {Error::WORKING, 0};
return {Error::NEED_DATA, 0};
// report the first available, not yet reported.
// or return {NONE, _symbols} if all have been reported
if (dec.ready())
return {Error::NONE, _symbols};
for (idx = 0; idx < static_cast<uint32_t> (symbols_tracker.size());
idx += 2) {
if (symbols_tracker[idx].load() == true) {
++idx;
if (symbols_tracker[idx].load() == false) {
expected = false;
if (symbols_tracker[idx].
compare_exchange_strong (expected, true)) {
} // else some other thread raced us, keep trying other
// symbols
}
}
}
if (dec.ready())
return {Error::NONE, _symbols};
return {Error::WORKING, 0};
return {Error::NEED_DATA, 0};
idx = init * 2;
for (; idx < symbols_tracker.size(); idx += 2) {
if (symbols_tracker[idx].load() == false) {
idx /= 2;
while (!last_reported.compare_exchange_weak(init, idx))
idx = std::max(init, idx);
if (dec.threads() > 0)
return {Error::WORKING, 0};
return {Error::NEED_DATA, 0};
}
}
last_reported.store (_symbols);
return {Error::NONE, 0};
}
return {Error::WORKING, 0};
}
template <typename In_It, typename Fwd_It>
void Decoder<In_It, Fwd_It>::waiting_thread (Decoder<In_It, Fwd_It> *obj,
while (obj->work == RaptorQ__v1::Work_State::KEEP_WORKING) {
bool compute = obj->dec.add_concurrent (obj->_max_threads);
if (compute) {
obj->decode_once();
std::unique_lock<std::mutex> lock (obj->_mtx);
obj->dec.drop_concurrent();
obj->_cond.notify_all(); // notify other waiting threads
}
std::unique_lock<std::mutex> lock (obj->_mtx);
// poll() does not actually need to be locked, but we use the
// lock-wait mechanism to signal the arrival of new symbols,
// so that we retry only when we get new data.
auto res = obj->poll();
if (res.error == Error::NONE || (obj->dec.end_of_input == true &&
break;
}
obj->_cond.wait (lock);
lock.unlock();
}
if (obj->work != RaptorQ__v1::Work_State::KEEP_WORKING && !promise_set)
p.set_value ({Error::EXITING, 0});
std::unique_lock<std::mutex> lock (obj->_mtx);
RQ_UNUSED (lock);
for (auto th = obj->waiting.begin(); th != obj->waiting.end(); ++th) {
if (std::this_thread::get_id() == th->get_id()) {
th->detach();
obj->waiting.erase (th);
break;
}
}
obj->_cond.notify_all(); // notify exit to destructor
}
template <typename In_It, typename Fwd_It>
if (symbols_tracker.size() == 0)
return {Error::INITIALIZATION, 0};
auto fut = p.get_future();
waiting_thread (this, std::move(p));
fut.wait();
return fut.get();
std::future<struct Decoder_wait_res> Decoder<In_It, Fwd_It>::wait ()
if (symbols_tracker.size() == 0) {
p.set_value ({Error::INITIALIZATION, 0});
return p.get_future();
}
std::unique_lock<std::mutex> lock (_mtx);
RQ_UNUSED (lock);
waiting.emplace_back (waiting_thread, this, std::move(p));
return f;
std::vector<bool> Decoder<In_It, Fwd_It>::end_of_input (
const Fill_With_Zeros fill)
if (symbols_tracker.size() != 0) {
if (fill == Fill_With_Zeros::YES)
return dec.fill_with_zeros();
template <typename In_It, typename Fwd_It>
bool Decoder<In_It, Fwd_It>::can_decode() const
{
if (symbols_tracker.size() == 0)
return false;
return dec.can_decode();
}
void Decoder<In_It, Fwd_It>::set_max_concurrency (const uint16_t max_threads)
{
if (symbols_tracker.size() != 0)
_max_threads = max_threads;
}
Decoder_Result Decoder<In_It, Fwd_It>::decode_once()
auto res = dec.decode (&work);
if (res == Decoder_Result::DECODED) {
std::unique_lock<std::mutex> lock (_mtx);
RQ_UNUSED (lock);
uint32_t id = last_reported.load();
for (; id < symbols_tracker.size(); id += 2)
symbols_tracker[id].store (true);
}
last_reported.store(_symbols);
lock.unlock();
}
return res;
template <typename In_It, typename Fwd_It>
bool Decoder<In_It, Fwd_It>::ready() const
{
if (symbols_tracker.size() == 0)
return false;
return dec.ready();
}
template <typename In_It, typename Fwd_It>
void Decoder<In_It, Fwd_It>::stop()
{
std::unique_lock<std::mutex> lock (_mtx);
RQ_UNUSED (lock);
template <typename In_It, typename Fwd_It>
void Decoder<In_It, Fwd_It>::clear_data()
{
if (symbols_tracker.size() == 0)
return;
std::unique_lock<std::mutex> lock (_mtx);
RQ_UNUSED (lock);
dec.clear_data();
last_reported.store(0);
for (auto it = symbols_tracker.begin(); it != symbols_tracker.end(); ++it)
*it = false;
}
Decoder_written Decoder<In_It, Fwd_It>::decode_bytes (Fwd_It &start,
const Fwd_It end,
const size_t from_byte,
const size_t skip)
{
using T = typename std::iterator_traits<Fwd_It>::value_type;
if (symbols_tracker.size() == 0 || skip >= sizeof(T) || from_byte >=
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
static_cast<size_t> (_symbols * _symbol_size)) {
return {0, 0};
}
auto decoded = dec.get_symbols();
uint16_t esi = static_cast<uint16_t> (from_byte /
static_cast<size_t> (_symbol_size));
uint16_t byte = static_cast<uint16_t> (from_byte %
static_cast<size_t> (_symbol_size));
size_t offset_al = skip;
T element = static_cast<T> (0);
if (skip != 0) {
uint8_t *p = reinterpret_cast<uint8_t *> (&*start);
for (size_t keep = 0; keep < skip; ++keep) {
element += static_cast<T> (*(p++)) << keep * 8;
}
}
size_t written = 0;
while (start != end && esi < _symbols && dec.has_symbol (esi)) {
element += static_cast<T> (static_cast<uint8_t> ((*decoded)(esi, byte)))
<< offset_al * 8;
++offset_al;
if (offset_al == sizeof(T)) {
*start = element;
++start;
written += offset_al;
offset_al = 0;
element = static_cast<T> (0);
}
++byte;
if (byte == decoded->cols()) {
byte = 0;
++esi;
}
}
if (start != end && offset_al != 0) {
// we have more stuff in "element", but not enough to fill
// the iterator. Do not overwrite additional data of the iterator.
uint8_t *out = reinterpret_cast<uint8_t *> (&*start);
uint8_t *in = reinterpret_cast<uint8_t *> (&element);
for (size_t idx = 0; idx < offset_al; ++idx, ++out, ++in)
*out = *in;
written += offset_al;
}
return {written, offset_al};
Error Decoder<In_It, Fwd_It>::decode_symbol (Fwd_It &start, const Fwd_It end,
if (symbols_tracker.size() == 0)
return Error::INITIALIZATION;
auto start_copy = start;
size_t esi_byte = esi * _symbol_size;
auto out = decode_bytes (start_copy, end, esi_byte, 0);
if (out.written == _symbol_size) {
start = start_copy;
return Error::NONE;
}
return Error::NEED_DATA;