# Call the work() method

**URL:** <https://www.ruby-forum.com/t/call-the-work-method/230117>\
**Category:** GNU Radio\
**Created:** [May 31, 2014, 10:50am UTC](https://www.ruby-forum.com/t/call-the-work-method/230117 "2014-05-31T10:50:14Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason\_sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jason\_sam](https://www.ruby-forum.com/u/jason_sam)\
**Post date:** [May 31, 2014, 10:50am UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/1 "2014-05-31T10:50:14Z")

</div>

Hi all:  
I know every block should have it’s work or general\_work  
function.And i know it’s used by scheduler.  
Now I want to write my c++ code just call work function.Is  
right to do it?  
If ok,can someone explain how to call work function?  
Thanks.  
Best regards

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 11:58am UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/2 "2014-05-31T11:58:03Z")

</div>

Hi Xianda,

> ```
> I know every block should have it's work or general_work function.And i 
> 
> ```

know it’s used by scheduler.  
Right!

> ```
> Now I want to write my c++ code just call work function.Is right to do 
> 
> ```

it?  
I hope I understand you correctly:  
You want to write a C++ program that calls your work function?

As usual, it would be quite helpful if you explained _why_ you would  
want to do this!

Short Answer:  
_Usually_ you don’t do that, so it’s not right to do.  
Long Answer:  
You can not generally do this because a block can only exist within GNU  
Radio, and relies on GNU Radio scheduler mechanisms.  
If you however, wrote a work() that only uses the the input\_items,  
output\_items buffers and only relies on noutput\_items as runtime  
information,  
than that’s technically possible. I would _not_ recommend that, though.  
Better write a new function, e.g. “signal\_processing(in,out, howmany)”  
in a non-block class  
and call that from your work() as well as from your new C++ program.

> ```
> If ok,can someone explain how to call work function?
> 
> ```

It’s a C++ method, and the function signature is  
int work  
[http://gnuradio.org/doc/doxygen/classgr\_1\_1sync\_\_block.html#a38942c5d8bee5232404ab71ada9a3956](http://gnuradio.org/doc/doxygen/classgr_1_1sync__block.html#a38942c5d8bee5232404ab71ada9a3956)  
(int noutput\_items, gr\_vector\_const\_void\_star  
[http://gnuradio.org/doc/doxygen/gnuradio-runtime\_2include\_2gnuradio\_2types\_8h.html#a9852a3d906d823cb70eb9c29e7f2a43d](http://gnuradio.org/doc/doxygen/gnuradio-runtime_2include_2gnuradio_2types_8h.html#a9852a3d906d823cb70eb9c29e7f2a43d)  
&input\_items, gr\_vector\_void\_star  
[http://gnuradio.org/doc/doxygen/gnuradio-runtime\_2include\_2gnuradio\_2types\_8h.html#acd3f499576e769966ea44554b359d949](http://gnuradio.org/doc/doxygen/gnuradio-runtime_2include_2gnuradio_2types_8h.html#acd3f499576e769966ea44554b359d949)  
&output\_items)  
(from the official documentation,  
[GNU Radio Manual and C++ API Reference: gr::sync\_block Class Reference](http://gnuradio.org/doc/doxygen/classgr_1_1sync__block.html))  
You could call it like you would call every other method. Since I assume  
you already know how input\_ and output\_items should look like from  
writing a work() function,  
I don’t understand where the “how to call question” comes from.

Best Regards,  
Marcus M?ller

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 12:34pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/3 "2014-05-31T12:34:37Z")

</div>

On 31 May 2014 11:57, “Marcus Müller” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Xianda,
> 
> > ```
> > I know every block should have it's work or general_work
> > 
> > ```

function.And i know it’s used by scheduler

For the record, a block that only uses message passing doesn’t need a  
work  
function - not that that’s what you’re looking for here.

M

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 1:04pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/4 "2014-05-31T13:04:15Z")

</div>

Hi:  
Thank you so much.  
For example,I write a simple c++ program which can use the  
“gnuradio/fft/fft.h”.But i also want to use  
\<gnuradio/digital/ofdm\_cyclic\_prefixer.h\>.  
But the ofdm\_cyclic\_prefixer is a block.It has work function.  
I attached my document.  
#include   
#include   
#include \<gnuradio/gr\_complex.h\>  
#include \<gnuradio/fft/fft.h\>  
#include \<gnuradio/digital/ofdm\_cyclic\_prefixer.h\>

std::vector\<std::complex \*\> buff1(64);//read data  
std::vector\<std::complex \*\> buff2(64);//write data

std::ifstream infile(“a.dat”,std::ifstream::binary);  
std::ofstream outfile(“b.dat”,std::ofstream::binary);

int main(){  
while(not infile.eof())  
{  
infile.read((char\*)&buff1.front(),buff1.size()\*sizeof(std::complex));

gr::fft::fft\_complex \*buff=new gr::fft::fft\_complex(64,0);  
gr\_complex \*bu=buff-\>get\_inbuf();  
memcpy(bu,&buff1.front(),buff1.size()\*sizeof(std::complex));  
buff-\>execute();  
gr\_complex \*out=buff-\>get\_outbuf();  
memcpy(&buff2.front(),out,buff2.size()\*sizeof(std::complex));

outfile.write((char\*)&buff2.front(),buff2.size()\*sizeof(std::complex));  
}

infile.close();  
outfile.close();  
}  
Can you help me?  
Thanks  
Best regards,  
xianda

At 2014-05-31 05:56:29, “Marcus M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Hi Xianda,

```
     I know every block should have it's work or general_work 

```

function.And i know it’s used by scheduler.  
Right!

```
     Now I want to write my c++ code just call work function.Is 

```

right to do it?  
I hope I understand you correctly:  
You want to write a C++ program that calls your work function?

As usual, it would be quite helpful if you explained _why_ you would  
want to do this!

Short Answer:  
_Usually_ you don’t do that, so it’s not right to do.  
Long Answer:  
You can not generally do this because a block can only exist within GNU  
Radio, and relies on GNU Radio scheduler mechanisms.  
If you however, wrote a work() that only uses the the input\_items,  
output\_items buffers and only relies on noutput\_items as runtime  
information,  
than that’s technically possible. I would _not_ recommend that, though.  
Better write a new function, e.g. “signal\_processing(in,out, howmany)”  
in a non-block class  
and call that from your work() as well as from your new C++ program.

```
     If ok,can someone explain how to call work function?

```

It’s a C++ method, and the function signature is  
int work (int noutput\_items, gr\_vector\_const\_void\_star &input\_items,  
gr\_vector\_void\_star &output\_items)  
(from the official documentation,  
[GNU Radio Manual and C++ API Reference: gr::sync\_block Class Reference](http://gnuradio.org/doc/doxygen/classgr_1_1sync__block.html))  
You could call it like you would call every other method. Since I assume  
you already know how input\_ and output\_items should look like from  
writing a work() function,  
I don’t understand where the “how to call question” comes from.

Best Regards,  
Marcus M.

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 1:23pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/5 "2014-05-31T13:23:11Z")

</div>

Hi:  
Thank you so much.  
I just want to write c++ code to realize (“fft” block+“OFDM Cyclic  
Prefixer”).  
And I attached my code which just realize ifft but the “OFDM Cyclic  
Prefixer” has it’s work function.  
#include   
#include   
#include \<gnuradio/gr\_complex.h\>  
#include \<gnuradio/fft/fft.h\>  
#include \<gnuradio/digital/ofdm\_cyclic\_prefixer.h\>

std::vector\<std::complex \*\> buff1(64);//read data  
std::vector\<std::complex \*\> buff2(64);//write data

std::ifstream infile(“a.dat”,std::ifstream::binary);  
std::ofstream outfile(“b.dat”,std::ofstream::binary);

int main(){  
while(not infile.eof())  
{  
infile.read((char\*)&buff1.front(),buff1.size()\*sizeof(std::complex));

gr::fft::fft\_complex \*buff=new gr::fft::fft\_complex(64,0);  
gr\_complex \*bu=buff-\>get\_inbuf();  
memcpy(bu,&buff1.front(),buff1.size()\*sizeof(std::complex));  
buff-\>execute();  
gr\_complex \*out=buff-\>get\_outbuf();  
memcpy(&buff2.front(),out,buff2.size()\*sizeof(std::complex));

outfile.write((char\*)&buff2.front(),buff2.size()\*sizeof(std::complex));  
}

infile.close();  
outfile.close();  
}

Thank you.  
Best regards,  
xianda

At 2014-05-31 06:33:35, “Martin B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

On 31 May 2014 11:57, “Marcus Müller” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Xianda,
> 
> > ```
> > I know every block should have it's work or general_work function.And 
> > 
> > ```

i know it’s used by scheduler

For the record, a block that only uses message passing doesn’t need a  
work function - not that that’s what you’re looking for here.

M

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 2:14pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/6 "2014-05-31T14:14:27Z")

</div>

Hi:  
 Thank you so much.  
 What I want to do is that  
read symbols from file and perform fft and insert cp and then transmit  
to the usrp.Just as the “fft” and “OFDM Cyclic Prefixer” do!  
  
The easy way is use the\<gnuradio/fft/fft.h\> and  
\<gnuradio/digital/ofdm\_cyclic\_prefixer.h\>.But as you said  
fft\_complex\_vcc is a block.But now how can i call it in my code?(It’s a  
block.It has work function.)Can i add it’s work function in my  
code?Thank you.  
Best regards,  
xianda

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 2:29pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/7 "2014-05-31T14:29:46Z")

</div>

Hi Xianda,

some of your emails are really hard to read due to your email client  
writing non-standard-compliant HTML mail. Could you disable  
HTML/multimedia mail?

> What I want to do is that read symbols from file and perform fft and insert cp  
> and then transmit to the usrp.  
> ok, thanks for clarifying that!

> Just as the “fft” and “OFDM Cyclic Prefixer” do!

Well, the fft block doesn’t read from a file, and ofdm cyclic prefixer  
does not transmit, but I get your point.

> The easy way is use the \<gnuradio/fft/fft.h\> and  
> \<gnuradio/digital/ofdm\_cyclic\_prefixer.h\>;

No!  
In GNU Radio, you build flow graphs out of existing blocks, whenever  
possible. So you want to use

[file source] → [(I)FFT] → [cyclic prefixer] → [USRP sink]

by instantiating these blocks, and connecting them to a GNU Radio flow  
graph. Then you let GNU Radio run that flow graph.  
There’s no need to write your own block, so far. Actually, you can do  
this without writing a single line of code just by constructing  
above flow graph in gnuradio-companion. That will generate the flowgraph  
setup and starting program in python.

If you need to add more functionality, you usually just add another  
block to this chain, but from what you’ve said there will be no need to  
do this.

> But as you said fft\_complex\_vcc is a block. But now how can i call it in my  
> code?

You don’t, see my previous post on calling work().

Greetings,  
Marcus

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 1:37pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/8 "2014-05-31T13:37:55Z")

</div>

Hi Xianda,

> I just want to write c++ code to realize (“fft” block+“OFDM Cyclic  
> Prefixer”).

Sorry, I still did not understand. I only understand your C++, so here’s  
my comments:

The code you posted has really nothing to do with gr::block;  
gr::fft:fft\_complex is _not_ a gr::block, and has no work()!  
[http://gnuradio.org/doc/doxygen/classgr\_1\_1fft\_1\_1fft\_\_complex.html](http://gnuradio.org/doc/doxygen/classgr_1_1fft_1_1fft__complex.html)

Also, I don’t see the need to #include  
\<gnuradio/digital/ofdm\_cyclic\_prefixer.h\>. Is this a mistake or was it  
intentional?

fft\_complex really just a convenience wrapper around FFTW.  
I haven’t tried to test your code, and you could read() directly into  
the get\_inbuf() buffer, and write directly from your get\_outbuf()  
buffer, but it looks ok.

Again: gr::fft:fft\_complex is not a block! it is used from within  
fft\_complex\_vcc (which is a block).  
All it does is use the FFTW library to perform FFTs, you can do that  
yourself without using GNU Radio at all. Only use it when you are  
developing a GNU Radio program!

Greetings,  
Marcus

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 3:16pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/9 "2014-05-31T15:16:19Z")

</div>

Hi!

The gr-uhd USRP sink can represent multiple USRPs at once.  
If you have a recent GNU Radio version, in gnuradio-companion your “UHD:  
USRP Sink” block will have a “Documentation” tab.  
There is an example that specifies two USRP addresses. This will  
automatically enable you to transmit the contents of both files  
simultaneously [1].  
You should start with the gnuradio-companion, which will generate  
easy-to-read Python code.  
Usually, you don’t need to write your own blocks to interface with  
USRPs.

Greetings,  
Marcus

[1] “Simultaneously” does not mean “coherently”!

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 3:23pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/10 "2014-05-31T15:23:58Z")

</div>

Hi  
Thank you so much for your patient instructions.  
Yes ,I know the “UHD:USRP Sink” can specifies two USRP  
addresses.But the symbol i try to transmit to the usrp is different.  
They may be should in two flow graph.  
[file source 1] --\> [(I)FFT] --\> [cyclic prefixer] --\> [ USRP sink  
1]  
[file source 2] --\> [(I)FFT] --\> [cyclic prefixer] --\> [ USRP sink  
2]  
Can i realize it in grc?Or should i write the c++ code?  
Thank you.Thank you.  
Best regards,  
xianda

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 2:51pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/11 "2014-05-31T14:51:35Z")

</div>

Hi:  
Thank you so much for your kindly reply.  
Sorry for my poor English.  
The reason why i directly write the c++ code is that I want to use  
the uhd to control my two usrps send simultaneously.  
The aim i want to reach is that read symbol from file and perform  
ifft and insert cp and then transmit.Just like you write:  
[file source 1] --\> [(I)FFT] --\> [cyclic prefixer] --\> [ USRP  
sink 1]  
[file source 2] --\> [(I)FFT] --\> [cyclic prefixer] --\> [ USRP sink  
2]  
I can control them simutanously.Use some class like  
uhd::usrp::multi\_usrp.  
Which is the best way to realize it?Use the c++ or write python flow  
graph?Thanks.  
Thank you so much.  
Best regards,  
xianda

---

<div class="post-metadata">

**Author:** ![xianda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/b38774/32.png) [@xianda](https://www.ruby-forum.com/u/xianda)\
**Post date:** [May 31, 2014, 3:26pm UTC](https://www.ruby-forum.com/t/call-the-work-method/230117/12 "2014-05-31T15:26:55Z")

</div>

GRC. You get 1 USRP sink, but it will have 2 independent inputs.
