summaryrefslogtreecommitdiff
path: root/lib/block_task.cpp
blob: cc9d6af3f1d1efb5ca3132f2b6f681e09f810206 (plain)
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
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include <gras_impl/block_actor.hpp>
#include "tag_handlers.hpp"

using namespace gras;

void BlockActor::mark_done(void)
{
    if (this->block_state == BLOCK_STATE_DONE) return; //can re-enter checking done first

    this->block_ptr->stop();

    //flush partial output buffers to the downstream
    for (size_t i = 0; i < this->get_num_outputs(); i++)
    {
        if (not this->output_queues.ready(i)) continue;
        SBuffer &buff = this->output_queues.front(i);
        if (buff.length == 0) continue;
        InputBufferMessage buff_msg;
        buff_msg.buffer = buff;
        this->post_downstream(i, buff_msg);
        this->output_queues.pop(i);
    }

    this->interruptible_thread.reset();

    //mark down the new state
    this->block_state = BLOCK_STATE_DONE;

    //release upstream, downstream, and executor tokens
    this->token_pool.clear();

    //release all buffers in queues
    this->input_queues.flush_all();
    this->output_queues.flush_all();

    //tell the upstream and downstram to re-check their tokens
    //this is how the other blocks know who is interested,
    //and can decide based on interest to set done or not
    for (size_t i = 0; i < this->get_num_inputs(); i++)
    {
        this->post_upstream(i, OutputCheckMessage());
    }
    for (size_t i = 0; i < this->get_num_outputs(); i++)
    {
        this->post_downstream(i, InputCheckMessage());
    }

    if (ARMAGEDDON) std::cerr
        << "==================================================\n"
        << "== The " << block_ptr->to_string() << " is done...\n"
        << "==================================================\n"
        << std::flush;
}

void BlockActor::input_fail(const size_t i)
{
    //input failed, accumulate and try again
    if (not this->input_queues.is_accumulated(i))
    {
        this->input_queues.accumulate(i);
        this->Push(SelfKickMessage(), Theron::Address());
        return;
    }

    //otherwise check for done, else wait for more
    if (this->inputs_done[i])
    {
        this->mark_done();
        return;
    }

    //check that the input is not already maxed
    if (this->input_queues.is_front_maximal(i))
    {
        throw std::runtime_error("input_fail called on maximum_items buffer");
    }

    //mark fail: not ready until a new buffer appears
    this->input_queues.fail(i);
}

void BlockActor::output_fail(const size_t i)
{
    SBuffer &buff = this->output_queues.front(i);

    //check that the input is not already maxed
    const size_t front_items = buff.length/this->output_items_sizes[i];
    if (front_items >= this->output_configs[i].maximum_items)
    {
        throw std::runtime_error("output_fail called on maximum_items buffer");
    }

    //force pop so next work() gets a new buffer
    this->flush_output(i, true);
}

GRAS_FORCE_INLINE bool BlockActor::is_work_allowed(void)
{
    return (
        this->block_state == BLOCK_STATE_LIVE and
        this->input_queues.all_ready() and
        this->inputs_available.any() and
        this->output_queues.all_ready()
    );
}

void BlockActor::handle_task(void)
{
    //------------------------------------------------------------------
    //-- Decide if its possible to continue any processing:
    //-- Handle task may get called for incoming buffers,
    //-- however, not all ports may have available buffers.
    //------------------------------------------------------------------
    if (not this->is_work_allowed()) return;

    #ifdef WORK_DEBUG
    WorkDebugPrinter WDP(block_ptr->to_string());
    #endif

    //------------------------------------------------------------------
    //-- Asynchronous notification through atomic variable
    //-- that the executor has instructed workers to stop.
    //------------------------------------------------------------------
    if (active_token.expired())
    {
        this->mark_done();
        return;
    }

    const size_t num_inputs = this->get_num_inputs();
    const size_t num_outputs = this->get_num_outputs();

    //------------------------------------------------------------------
    //-- initialize input buffers before work
    //------------------------------------------------------------------
    size_t output_inline_index = 0;
    this->input_items.min() = ~0;
    this->input_items.max() = 0;
    for (size_t i = 0; i < num_inputs; i++)
    {
        this->sort_tags(i);

        ASSERT(this->input_queues.ready(i));
        const SBuffer &buff = this->input_queues.front(i);
        const void *mem = buff.get();
        size_t items = buff.length/this->input_items_sizes[i];

        this->input_items[i].get() = mem;
        this->input_items[i].size() = items;
        this->input_items.min() = std::min(this->input_items.min(), items);
        this->input_items.max() = std::max(this->input_items.max(), items);

        //inline dealings, how and when input buffers can be inlined into output buffers
        continue; //FIXME to implement needs change
        /*
        if (
            buff.unique() and
            input_configs[i].inline_buffer and
            output_inline_index < num_outputs and
            buff.get_affinity() == this->buffer_affinity
        ){
            //copy buffer reference but push with zero length, same offset
            SBuffer new_obuff = buff;
            new_obuff.length = 0;
            this->flush_output(output_inline_index);
            this->output_queues.push_front(output_inline_index, new_obuff); //you got inlined!
            output_inline_index++; //done do this output port again
        }
        */
    }

    //------------------------------------------------------------------
    //-- initialize output buffers before work
    //------------------------------------------------------------------
    this->output_items.min() = ~0;
    this->output_items.max() = 0;
    for (size_t i = 0; i < num_outputs; i++)
    {
        ASSERT(this->output_queues.ready(i));
        SBuffer &buff = this->output_queues.front(i);
        ASSERT(buff.length == 0); //assumes it was flushed last call
        void *mem = buff.get();
        const size_t bytes = buff.get_actual_length() - buff.offset;
        size_t items = bytes/this->output_items_sizes[i];

        this->output_items[i].get() = mem;
        this->output_items[i].size() = items;
        this->output_items.min() = std::min(this->output_items.min(), items);
        this->output_items.max() = std::max(this->output_items.max(), items);
    }

    //------------------------------------------------------------------
    //-- the work
    //------------------------------------------------------------------
    if (this->interruptible_thread)
    {
        this->interruptible_thread->call();
    }
    else
    {
        this->task_work();
    }

    //------------------------------------------------------------------
    //-- Flush output buffers downstream
    //------------------------------------------------------------------
    for (size_t i = 0; i < num_outputs; i++)
    {
        this->flush_output(i);
    }

    //------------------------------------------------------------------
    //-- Message self based on post-work conditions
    //------------------------------------------------------------------
    //missing at least one upstream provider?
    //since nothing else is coming in, its safe to mark done
    for (size_t i = 0; i < num_inputs; i++)
    {
        const bool nothing = this->input_queues.empty(i) and this->input_tags[i].empty();
        this->inputs_available.set(i, not nothing);
        if (this->is_input_done(i)) this->mark_done();
    }

    //still have IO ready? kick off another task
    if (this->is_work_allowed())
    {
        this->Push(SelfKickMessage(), Theron::Address());
    }
}

void BlockActor::consume(const size_t i, const size_t items)
{
    #ifdef ITEM_CONSPROD
    std::cerr << "consume " << items << std::endl;
    #endif
    this->items_consumed[i] += items;
    const size_t bytes = items*this->input_items_sizes[i];
    this->input_queues.consume(i, bytes);
    this->trim_tags(i);
}

void BlockActor::produce(const size_t i, const size_t items)
{
    #ifdef ITEM_CONSPROD
    std::cerr << "produce " << items << std::endl;
    #endif
    SBuffer &buff = this->output_queues.front(i);
    this->items_produced[i] += items;
    const size_t bytes = items*this->output_items_sizes[i];
    buff.length += bytes;
}

void BlockActor::produce_buffer(const size_t i, const SBuffer &buffer)
{
    this->flush_output(i);
    const size_t items = buffer.length/output_items_sizes[i];
    this->items_produced[i] += items;
    InputBufferMessage buff_msg;
    buff_msg.buffer = buffer;
    this->post_downstream(i, buff_msg);
}

GRAS_FORCE_INLINE void BlockActor::flush_output(const size_t i, const bool force_pop)
{
    if (not this->output_queues.ready(i) or this->output_queues.front(i).length == 0) return;
    SBuffer &buff = this->output_queues.front(i);
    InputBufferMessage buff_msg;
    buff_msg.buffer = buff;
    this->post_downstream(i, buff_msg);

    //increment buffer for next use
    buff.offset += buff.length;
    buff.length = 0;

    //simply just pop
    this->output_queues.pop(i);

    /*
    //when to pop the buffer and give next work a new one
    const size_t reserve_bytes = this->output_configs[i].reserve_items/this->output_items_sizes[i];
    if (
        force_pop or (buff.offset*2 > buff.get_actual_length()) or
        (buff.get_actual_length() - buff.offset) < reserve_bytes
    )
    {
        this->output_queues.pop(i);
    }*/
}