# Should return the number of input or the number of output?

**URL:** <https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652>\
**Category:** GNU Radio\
**Created:** [March 9, 2014, 5:00am UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652 "2014-03-09T05:00:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [March 9, 2014, 5:00am UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/1 "2014-03-09T05:00:07Z")

</div>

Dear Sir,

A python sink block created by gr\_modtool has below work() function, it  
returns the number of input items.

```
def work(self, input_items, output_items):
    in0 = input_items[0]
    # <+signal processing here+>
    return len(input_items[0])

```

But a c++ sink block created by gr\_modtool has the work() function  
return  
the number of output items (noutput\_items).

It sounds contradict. Is there any error happening?

Regards,  
Activecat

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 10, 2014, 10:43am UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/2 "2014-03-10T10:43:54Z")

</div>

On 03/09/2014 04:58 AM, Activecat wrote:

> But a c++ sink block created by gr\_modtool has the work() function  
> return the number of output items (noutput\_items).
> 
> It sounds contradict. Is there any error happening?

No contradiction. Sinks are always syncs (haha), so noutput\_items == the  
number of input items (by definition). The actual output buffer,  
however, is empty – so we can’t use the length of the output buffer to  
tell the scheduler how much we consumed.

In a sync block, there is no argument “ninput\_items”, so we can’t use  
that, either.

M

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 10, 2014, 2:10pm UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/3 "2014-03-10T14:10:39Z")

</div>

> > ```
> > return len(input_items[0])
> > 
> > ```
> 
> however, is empty – so we can’t use the length of the output buffer to  
> tell the scheduler how much we consumed.
> 
> In a sync block, there is no argument “ninput\_items”, so we can’t use  
> that, either.
> 
> M

Dear Sir,

If the return value of work() is used to tell the scheduler how much we  
have consumed, than it is very clear!

In general\_work() of new templates generated by gr\_modtool, we always  
see  
this, even for sink blocks:

```
  // Tell runtime system how many output items we produced.
  return noutput_items;

```

The comment sounds like the return value of general\_work() is to tell  
the  
scheduler how many output items have produced.  
If this is true, for sink block, we should actually returned zero  
because  
sink block doesn’t produce any output item.

To avoid confusion, and to be more consistent, I suggest to put this  
instead into work() of new templates of sink block:

```
// Tell runtime system how many input items have been consumed.
return noutput_items;

```

Because when I look into sync\_block.cc, the return value of work() is  
actually used for consume\_each(r).

Just a silly suggestion. Thanks.

Regards,  
Activecat

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 11, 2014, 1:36am UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/4 "2014-03-11T01:36:22Z")

</div>

Dear Martin,

Apparently the consistency of code across different source files is more  
emphasized than its clarity in individual file.  
Nevertheless that is clear now, thanks.

Regards,  
Activecat

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 10, 2014, 2:20pm UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/5 "2014-03-10T14:20:24Z")

</div>

OK, I admit I was unclear. Let’s try from scratch:

- The return function from work() or general\_work() is the amount of  
items that were _produced_.
- In a sync block, the number of items produced is the number of items  
consumed, so we can use that to save the developer from manually  
consuming(). In other words, in a sync block, the return value is _both_  
the number of produced and consumed items.
- Sinks and sources are syncs, therefore the mechanic is always the  
same.
- Sinks are a special case because they don’t produce anything. However,  
the scheduler knows the block’s io signature, and knows what to do. The  
consume/produce mechanic stays the same, for consistency’s sake, as with  
all other sync blocks.

Hope this clears things up!

M

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 11, 2014, 10:09am UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/6 "2014-03-11T10:09:43Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Well, it’s basically the idea of not writing a special case for a  
single type of block; and if you are aware that sync blocks are only  
subclasses of normal blocks that implement a general\_work which  
consumes _and_ produces the return value of work, than everything is  
clear and readable 🙂  
Greetings,  
Marcus

On 11.03.2014 01:34, Activecat wrote:

> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:
> 
> > case because they don’t produce anything. However, the scheduler
> > 
> > > If the return value of work() is used to tell the scheduler how  
> > > this is true, for sink block, we should actually returned zero
> 
> [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1  
Comment: Using GnuPG with Thunderbird - [http://www.enigmail.net/](http://www.enigmail.net/)

iQEcBAEBAgAGBQJTHtKDAAoJEBQ6EdjyzlHtKhsIAJaKRWiOkmZ8/icO07FjTDCP  
M7DFTEvaWXl/WFRSVfGYkccE513iXU/wTmud4gF8ywYHK+YWJbEOmPV3aEKNNTU5  
8C1GNa6a0yPfGz7l4+WK0oKt8vIklViSKqt+sOJ3G/FGu/bllSoStbOvGnzw2ubI  
mJDfE4BzAuBIGSRr6Ar11UJ7xRNwLg9pcGsDYEAy12HUKpuJSqKP9rrb1wb1Kwco  
t/Ov+KvmgPiNnP8w6V2VZ5FIWtE77BsdALtDZy4dKsQaSCiOPb+AxZyOZUP1XDYD  
rV0POlRWuQ4kPqRH8MhzxUn6h7yKs31q+9GSkWOCO8YiCC9EvYDYvi3Jm/oZndY=  
=4uZc  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![activecat\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@activecat\_k](https://www.ruby-forum.com/u/activecat_k)\
**Post date:** [March 11, 2014, 10:17am UTC](https://www.ruby-forum.com/t/should-return-the-number-of-input-or-the-number-of-output/239652/7 "2014-03-11T10:17:02Z")

</div>

Yes I agree with you all.  
Everything are now clear and readable.  
Thank you very much.

Regards,  
Activecat
