GNU Radio 3.6.4.1 C++ API
gr_block.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004,2007,2009,2010 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_BLOCK_H
00024 #define INCLUDED_GR_BLOCK_H
00025 
00026 #include <gr_core_api.h>
00027 #include <gr_basic_block.h>
00028 #include <gr_tags.h>
00029 
00030 /*!
00031  * \brief The abstract base class for all 'terminal' processing blocks.
00032  * \ingroup base_blk
00033  *
00034  * A signal processing flow is constructed by creating a tree of
00035  * hierarchical blocks, which at any level may also contain terminal nodes
00036  * that actually implement signal processing functions. This is the base
00037  * class for all such leaf nodes.
00038 
00039  * Blocks have a set of input streams and output streams.  The
00040  * input_signature and output_signature define the number of input
00041  * streams and output streams respectively, and the type of the data
00042  * items in each stream.
00043  *
00044  * Although blocks may consume data on each input stream at a
00045  * different rate, all outputs streams must produce data at the same
00046  * rate.  That rate may be different from any of the input rates.
00047  *
00048  * User derived blocks override two methods, forecast and general_work,
00049  * to implement their signal processing behavior. forecast is called
00050  * by the system scheduler to determine how many items are required on
00051  * each input stream in order to produce a given number of output
00052  * items.
00053  *
00054  * general_work is called to perform the signal processing in the block.
00055  * It reads the input items and writes the output items.
00056  */
00057 
00058 class GR_CORE_API gr_block : public gr_basic_block {
00059 
00060  public:
00061 
00062   //! Magic return values from general_work
00063   enum {
00064     WORK_CALLED_PRODUCE = -2,
00065     WORK_DONE = -1
00066   };
00067 
00068   enum tag_propagation_policy_t {
00069     TPP_DONT = 0,
00070     TPP_ALL_TO_ALL = 1,
00071     TPP_ONE_TO_ONE = 2
00072   };
00073 
00074   virtual ~gr_block ();
00075 
00076   /*!
00077    * Assume block computes y_i = f(x_i, x_i-1, x_i-2, x_i-3...)
00078    * History is the number of x_i's that are examined to produce one y_i.
00079    * This comes in handy for FIR filters, where we use history to
00080    * ensure that our input contains the appropriate "history" for the
00081    * filter.   History should be equal to the number of filter taps.
00082    */
00083   unsigned history () const { return d_history; }
00084   void  set_history (unsigned history) { d_history = history; }
00085 
00086   /*!
00087    * \brief Return true if this block has a fixed input to output rate.
00088    *
00089    * If true, then fixed_rate_in_to_out and fixed_rate_out_to_in may be called.
00090    */
00091   bool fixed_rate() const { return d_fixed_rate; }
00092 
00093   // ----------------------------------------------------------------
00094   //            override these to define your behavior
00095   // ----------------------------------------------------------------
00096 
00097   /*!
00098    * \brief  Estimate input requirements given output request
00099    *
00100    * \param noutput_items           number of output items to produce
00101    * \param ninput_items_required   number of input items required on each input stream
00102    *
00103    * Given a request to product \p noutput_items, estimate the number of
00104    * data items required on each input stream.  The estimate doesn't have
00105    * to be exact, but should be close.
00106    */
00107   virtual void forecast (int noutput_items,
00108                          gr_vector_int &ninput_items_required);
00109 
00110   /*!
00111    * \brief compute output items from input items
00112    *
00113    * \param noutput_items       number of output items to write on each output stream
00114    * \param ninput_items        number of input items available on each input stream
00115    * \param input_items         vector of pointers to the input items, one entry per input stream
00116    * \param output_items        vector of pointers to the output items, one entry per output stream
00117    *
00118    * \returns number of items actually written to each output stream, or -1 on EOF.
00119    * It is OK to return a value less than noutput_items.  -1 <= return value <= noutput_items
00120    *
00121    * general_work must call consume or consume_each to indicate how many items
00122    * were consumed on each input stream.
00123    */
00124   virtual int general_work (int noutput_items,
00125                             gr_vector_int &ninput_items,
00126                             gr_vector_const_void_star &input_items,
00127                             gr_vector_void_star &output_items);
00128 
00129   /*!
00130    * \brief Called to enable drivers, etc for i/o devices.
00131    *
00132    * This allows a block to enable an associated driver to begin
00133    * transfering data just before we start to execute the scheduler.
00134    * The end result is that this reduces latency in the pipeline when
00135    * dealing with audio devices, usrps, etc.
00136    */
00137   virtual bool start();
00138 
00139   /*!
00140    * \brief Called to disable drivers, etc for i/o devices.
00141    */
00142   virtual bool stop();
00143 
00144   // ----------------------------------------------------------------
00145 
00146   /*!
00147    * \brief Constrain the noutput_items argument passed to forecast and general_work
00148    *
00149    * set_output_multiple causes the scheduler to ensure that the noutput_items
00150    * argument passed to forecast and general_work will be an integer multiple
00151    * of \param multiple  The default value of output multiple is 1.
00152    */
00153   void set_output_multiple (int multiple);
00154   int  output_multiple () const { return d_output_multiple; }
00155   bool  output_multiple_set () const { return d_output_multiple_set; }
00156 
00157   /*!
00158    * \brief Constrains buffers to work on a set item alignment (for SIMD)
00159    *
00160    * set_alignment_multiple causes the scheduler to ensure that the noutput_items
00161    * argument passed to forecast and general_work will be an integer multiple
00162    * of \param multiple  The default value is 1.
00163    *
00164    * This control is similar to the output_multiple setting, except
00165    * that if the number of items passed to the block is less than the
00166    * output_multiple, this value is ignored and the block can produce
00167    * like normal. The d_unaligned value is set to the number of items
00168    * the block is off by. In the next call to general_work, the
00169    * noutput_items is set to d_unaligned or less until
00170    * d_unaligned==0. The buffers are now aligned again and the aligned
00171    * calls can be performed again.
00172    */
00173   void set_alignment (int multiple);
00174   int  alignment () const { return d_output_multiple; }
00175 
00176   void set_unaligned (int na);
00177   int unaligned () const { return d_unaligned; }
00178   void set_is_unaligned (bool u);
00179   bool is_unaligned () const { return d_is_unaligned; }
00180 
00181   /*!
00182    * \brief Tell the scheduler \p how_many_items of input stream \p which_input were consumed.
00183    */
00184   void consume (int which_input, int how_many_items);
00185 
00186   /*!
00187    * \brief Tell the scheduler \p how_many_items were consumed on each input stream.
00188    */
00189   void consume_each (int how_many_items);
00190 
00191   /*!
00192    * \brief Tell the scheduler \p how_many_items were produced on output stream \p which_output.
00193    *
00194    * If the block's general_work method calls produce, \p general_work must return WORK_CALLED_PRODUCE.
00195    */
00196   void produce (int which_output, int how_many_items);
00197 
00198   /*!
00199    * \brief Set the approximate output rate / input rate
00200    *
00201    * Provide a hint to the buffer allocator and scheduler.
00202    * The default relative_rate is 1.0
00203    *
00204    * decimators have relative_rates < 1.0
00205    * interpolators have relative_rates > 1.0
00206    */
00207   void  set_relative_rate (double relative_rate);
00208 
00209   /*!
00210    * \brief return the approximate output rate / input rate
00211    */
00212   double relative_rate () const { return d_relative_rate; }
00213 
00214   /*
00215    * The following two methods provide special case info to the
00216    * scheduler in the event that a block has a fixed input to output
00217    * ratio.  gr_sync_block, gr_sync_decimator and gr_sync_interpolator
00218    * override these.  If you're fixed rate, subclass one of those.
00219    */
00220   /*!
00221    * \brief Given ninput samples, return number of output samples that will be produced.
00222    * N.B. this is only defined if fixed_rate returns true.
00223    * Generally speaking, you don't need to override this.
00224    */
00225   virtual int fixed_rate_ninput_to_noutput(int ninput);
00226 
00227   /*!
00228    * \brief Given noutput samples, return number of input samples required to produce noutput.
00229    * N.B. this is only defined if fixed_rate returns true.
00230    * Generally speaking, you don't need to override this.
00231    */
00232   virtual int fixed_rate_noutput_to_ninput(int noutput);
00233 
00234   /*!
00235    * \brief Return the number of items read on input stream which_input
00236    */
00237   uint64_t nitems_read(unsigned int which_input);
00238 
00239   /*!
00240    * \brief  Return the number of items written on output stream which_output
00241    */
00242   uint64_t nitems_written(unsigned int which_output);
00243 
00244   /*!
00245    * \brief Asks for the policy used by the scheduler to moved tags downstream.
00246    */
00247   tag_propagation_policy_t tag_propagation_policy();
00248 
00249   /*!
00250    * \brief Set the policy by the scheduler to determine how tags are moved downstream.
00251    */
00252   void set_tag_propagation_policy(tag_propagation_policy_t p);
00253 
00254   /*!
00255    * \brief Return the minimum number of output items this block can
00256    * produce during a call to work.
00257    *
00258    * Should be 0 for most blocks.  Useful if we're dealing with packets and
00259    * the block produces one packet per call to work.
00260   */
00261   int min_noutput_items() const { return d_min_noutput_items; }
00262 
00263   /*!
00264    * \brief Set the minimum number of output items this block can
00265    * produce during a call to work.
00266    *
00267    * \param m the minimum noutput_items this block can produce.
00268    */
00269   void set_min_noutput_items(int m) { d_min_noutput_items = m; }
00270 
00271   /*!
00272    * \brief Return the maximum number of output items this block will
00273    * handle during a call to work.
00274    */
00275   int max_noutput_items();
00276 
00277   /*!
00278    * \brief Set the maximum number of output items this block will
00279    * handle during a call to work.
00280    *
00281    * \param m the maximum noutput_items this block will handle.
00282    */
00283   void set_max_noutput_items(int m);
00284 
00285   /*!
00286    * \brief Clear the switch for using the max_noutput_items value of this block.
00287    *
00288    * When is_set_max_noutput_items() returns 'true', the scheduler
00289    * will use the value returned by max_noutput_items() to limit the
00290    * size of the number of items possible for this block's work
00291    * function. If is_set_max_notput_items() returns 'false', then the
00292    * scheduler ignores the internal value and uses the value set
00293    * globally in the top_block.
00294    *
00295    * Use this value to clear the 'is_set' flag so the scheduler will
00296    * ignore this. Use the set_max_noutput_items(m) call to both set a
00297    * new value for max_noutput_items and to reenable its use in the
00298    * scheduler.
00299    */
00300   void unset_max_noutput_items();
00301 
00302   /*!
00303    * \brief Ask the block if the flag is or is not set to use the
00304    * internal value of max_noutput_items during a call to work.
00305    */
00306   bool is_set_max_noutput_items();
00307 
00308   /*
00309    * Used to expand the vectors that hold the min/max buffer sizes.
00310    *
00311    * Specifically, when -1 is used, the vectors are just initialized
00312    * with 1 value; this is used by the flat_flowgraph to expand when
00313    * required to add a new value for new ports on these blocks.
00314    */
00315   void expand_minmax_buffer(int port) {
00316     if((size_t)port >= d_max_output_buffer.size())
00317       set_max_output_buffer(port, -1);
00318     if((size_t)port >= d_min_output_buffer.size())
00319       set_min_output_buffer(port, -1);
00320   }
00321 
00322   /*!
00323    * \brief Returns max buffer size on output port \p i.
00324    */
00325   long max_output_buffer(size_t i) {
00326     if(i >= d_max_output_buffer.size())
00327       throw std::invalid_argument("gr_basic_block::max_output_buffer: port out of range.");
00328     return d_max_output_buffer[i];
00329   }
00330 
00331   /*!
00332    * \brief Sets max buffer size on all output ports.
00333    */
00334   void set_max_output_buffer(long max_output_buffer) { 
00335     for(int i = 0; i < output_signature()->max_streams(); i++) {
00336       set_max_output_buffer(i, max_output_buffer);
00337     }
00338   }
00339 
00340   /*!
00341    * \brief Sets max buffer size on output port \p port.
00342    */
00343   void set_max_output_buffer(int port, long max_output_buffer) {
00344     if((size_t)port >= d_max_output_buffer.size())
00345       d_max_output_buffer.push_back(max_output_buffer);
00346     else
00347       d_max_output_buffer[port] = max_output_buffer; 
00348   }
00349 
00350   /*!
00351    * \brief Returns min buffer size on output port \p i.
00352    */
00353   long min_output_buffer(size_t i) {
00354     if(i >= d_min_output_buffer.size())
00355       throw std::invalid_argument("gr_basic_block::min_output_buffer: port out of range.");
00356     return d_min_output_buffer[i];
00357   }
00358 
00359   /*!
00360    * \brief Sets min buffer size on all output ports.
00361    */
00362   void set_min_output_buffer(long min_output_buffer) {
00363     for(int i=0; i<output_signature()->max_streams(); i++) {
00364       set_min_output_buffer(i, min_output_buffer);
00365     }
00366   }
00367 
00368   /*!
00369    * \brief Sets min buffer size on output port \p port.
00370    */
00371   void set_min_output_buffer(int port, long min_output_buffer) {
00372     if((size_t)port >= d_min_output_buffer.size())
00373       d_min_output_buffer.push_back(min_output_buffer);
00374     else
00375       d_min_output_buffer[port] = min_output_buffer; 
00376   }
00377 
00378   // --------------- Performance counter functions -------------
00379 
00380   /*!
00381    * \brief Gets average noutput_items performance counter.
00382    */
00383   float pc_noutput_items();
00384 
00385   /*!
00386    * \brief Gets variance of noutput_items performance counter.
00387    */
00388   float pc_noutput_items_var();
00389 
00390   /*!
00391    * \brief Gets average num items produced performance counter.
00392    */
00393   float pc_nproduced();
00394 
00395   /*!
00396    * \brief Gets variance of  num items produced performance counter.
00397    */
00398   float pc_nproduced_var();
00399 
00400   /*!
00401    * \brief Gets average fullness of \p which input buffer.
00402    */
00403   float pc_input_buffers_full(int which);
00404 
00405   /*!
00406    * \brief Gets variance of fullness of \p which input buffer.
00407    */
00408   float pc_input_buffers_full_var(int which);
00409 
00410   /*!
00411    * \brief Gets average fullness of all input buffers.
00412    */
00413   std::vector<float> pc_input_buffers_full();
00414 
00415   /*!
00416    * \brief Gets variance of fullness of all input buffers.
00417    */
00418   std::vector<float> pc_input_buffers_full_var();
00419 
00420   /*!
00421    * \brief Gets average fullness of \p which input buffer.
00422    */
00423   float pc_output_buffers_full(int which);
00424 
00425   /*!
00426    * \brief Gets variance of fullness of \p which input buffer.
00427    */
00428   float pc_output_buffers_full_var(int which);
00429 
00430   /*!
00431    * \brief Gets average fullness of all output buffers.
00432    */
00433   std::vector<float> pc_output_buffers_full();
00434   /*!
00435    * \brief Gets variance of fullness of all output buffers.
00436    */
00437   std::vector<float> pc_output_buffers_full_var();
00438 
00439   /*!
00440    * \brief Gets average clock cycles spent in work.
00441    */
00442   float pc_work_time();
00443 
00444   /*!
00445    * \brief Gets average clock cycles spent in work.
00446    */
00447   float pc_work_time_var();
00448 
00449   /*!
00450    * \brief Resets the performance counters
00451    */
00452   void reset_perf_counters();
00453 
00454 
00455   // ----------------------------------------------------------------------------
00456   // Functions to handle thread affinity
00457 
00458   /*!
00459    * \brief Set the thread's affinity to processor core \p n.
00460    *
00461    * \param mask a vector of unsigned ints of the core numbers available to this block.
00462    */
00463   void set_processor_affinity(const std::vector<unsigned int> &mask);
00464 
00465   /*!
00466    * \brief Remove processor affinity to a specific core.
00467    */
00468   void unset_processor_affinity();
00469 
00470   /*!
00471    * \brief Get the current processor affinity.
00472    */
00473   std::vector<unsigned int> processor_affinity() { return d_affinity; }
00474 
00475   // ----------------------------------------------------------------------------
00476 
00477  private:
00478 
00479   int                   d_output_multiple;
00480   bool                  d_output_multiple_set;
00481   int                   d_unaligned;
00482   bool                  d_is_unaligned;
00483   double                d_relative_rate;        // approx output_rate / input_rate
00484   gr_block_detail_sptr  d_detail;               // implementation details
00485   unsigned              d_history;
00486   bool                  d_fixed_rate;
00487   bool                  d_max_noutput_items_set;     // if d_max_noutput_items is valid
00488   int                   d_max_noutput_items;         // value of max_noutput_items for this block
00489   int                   d_min_noutput_items;
00490   tag_propagation_policy_t d_tag_propagation_policy; // policy for moving tags downstream
00491   std::vector<unsigned int> d_affinity;              // thread affinity proc. mask
00492 
00493  protected:
00494   gr_block (void){} //allows pure virtual interface sub-classes
00495   gr_block (const std::string &name,
00496             gr_io_signature_sptr input_signature,
00497             gr_io_signature_sptr output_signature);
00498 
00499   void set_fixed_rate(bool fixed_rate){ d_fixed_rate = fixed_rate; }
00500 
00501 
00502   /*!
00503    * \brief  Adds a new tag onto the given output buffer.
00504    *
00505    * \param which_output an integer of which output stream to attach the tag
00506    * \param abs_offset   a uint64 number of the absolute item number
00507    *                     assicated with the tag. Can get from nitems_written.
00508    * \param key          the tag key as a PMT symbol
00509    * \param value        any PMT holding any value for the given key
00510    * \param srcid        optional source ID specifier; defaults to PMT_F
00511    */
00512   inline void add_item_tag(unsigned int which_output,
00513                     uint64_t abs_offset,
00514                     const pmt::pmt_t &key,
00515                     const pmt::pmt_t &value,
00516                     const pmt::pmt_t &srcid=pmt::PMT_F)
00517     {
00518         gr_tag_t tag;
00519         tag.offset = abs_offset;
00520         tag.key = key;
00521         tag.value = value;
00522         tag.srcid = srcid;
00523         this->add_item_tag(which_output, tag);
00524     }
00525 
00526  /*!
00527    * \brief  Adds a new tag onto the given output buffer.
00528    *
00529    * \param which_output an integer of which output stream to attach the tag
00530    * \param tag the tag object to add
00531    */
00532   void add_item_tag(unsigned int which_output, const gr_tag_t &tag);
00533 
00534   /*!
00535    * \brief  Removes a tag from the given input buffer.
00536    *
00537    * \param which_input an integer of which input stream to remove the tag from
00538    * \param abs_offset   a uint64 number of the absolute item number
00539    *                     assicated with the tag. Can get from nitems_written.
00540    * \param key          the tag key as a PMT symbol
00541    * \param value        any PMT holding any value for the given key
00542    * \param srcid        optional source ID specifier; defaults to PMT_F
00543    *
00544    * If no such tag is found, does nothing.
00545    */
00546   inline void remove_item_tag(unsigned int which_input,
00547                     uint64_t abs_offset,
00548                     const pmt::pmt_t &key,
00549                     const pmt::pmt_t &value,
00550                     const pmt::pmt_t &srcid=pmt::PMT_F)
00551   {
00552       gr_tag_t tag;
00553       tag.offset = abs_offset;
00554       tag.key = key;
00555       tag.value = value;
00556       tag.srcid = srcid;
00557       this->remove_item_tag(which_input, tag);
00558   }
00559 
00560  /*!
00561    * \brief  Removes a tag from the given input buffer.
00562    *
00563    * If no such tag is found, does nothing.
00564    *
00565    * \param which_input an integer of which input stream to remove the tag from
00566    * \param tag the tag object to remove
00567    */
00568   void remove_item_tag(unsigned int which_input, const gr_tag_t &tag);
00569 
00570   /*!
00571    * \brief Given a [start,end), returns a vector of all tags in the range.
00572    *
00573    * Range of counts is from start to end-1.
00574    *
00575    * Tags are tuples of:
00576    *      (item count, source id, key, value)
00577    *
00578    * \param v            a vector reference to return tags into
00579    * \param which_input  an integer of which input stream to pull from
00580    * \param abs_start    a uint64 count of the start of the range of interest
00581    * \param abs_end      a uint64 count of the end of the range of interest
00582    */
00583   void get_tags_in_range(std::vector<gr_tag_t> &v,
00584                          unsigned int which_input,
00585                          uint64_t abs_start,
00586                          uint64_t abs_end);
00587 
00588   /*!
00589    * \brief Given a [start,end), returns a vector of all tags in the range
00590    * with a given key.
00591    *
00592    * Range of counts is from start to end-1.
00593    *
00594    * Tags are tuples of:
00595    *      (item count, source id, key, value)
00596    *
00597    * \param v            a vector reference to return tags into
00598    * \param which_input  an integer of which input stream to pull from
00599    * \param abs_start    a uint64 count of the start of the range of interest
00600    * \param abs_end      a uint64 count of the end of the range of interest
00601    * \param key          a PMT symbol key to filter only tags of this key
00602    */
00603   void get_tags_in_range(std::vector<gr_tag_t> &v,
00604                          unsigned int which_input,
00605                          uint64_t abs_start,
00606                          uint64_t abs_end,
00607                          const pmt::pmt_t &key);
00608 
00609   std::vector<long>    d_max_output_buffer;
00610   std::vector<long>    d_min_output_buffer;
00611 
00612   /*! Used by block's setters and work functions to make
00613    * setting/resetting of parameters thread-safe.
00614    *
00615    * Used by calling gruel::scoped_lock l(d_setlock);
00616    */ 
00617   gruel::mutex d_setlock;
00618 
00619   // These are really only for internal use, but leaving them public avoids
00620   // having to work up an ever-varying list of friend GR_CORE_APIs
00621 
00622  public:
00623   gr_block_detail_sptr detail () const { return d_detail; }
00624   void set_detail (gr_block_detail_sptr detail) { d_detail = detail; }
00625 };
00626 
00627 typedef std::vector<gr_block_sptr> gr_block_vector_t;
00628 typedef std::vector<gr_block_sptr>::iterator gr_block_viter_t;
00629 
00630 inline gr_block_sptr cast_to_block_sptr(gr_basic_block_sptr p)
00631 {
00632   return boost::dynamic_pointer_cast<gr_block, gr_basic_block>(p);
00633 }
00634 
00635 
00636 std::ostream&
00637 operator << (std::ostream& os, const gr_block *m);
00638 
00639 #endif /* INCLUDED_GR_BLOCK_H */