Code Search for Developers
 
 
  

ship_example.cpp from GreenSocs at Krugle


Show ship_example.cpp syntax highlighted

/*
Copyright (c) 2006 : Technical University of Braunschweig, Germany

All rights reserved.

Authors: Wolfgang Klingauf, TU Braunschweig, E.I.S.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.  Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.  Neither the name of
the Technical University of Braunschweig, Germany nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// make GreenBus communicative
#define VERBOSE 

// enable DUST transaction recording
//#define DUST_ENABLE

#include <systemc.h>
#include "gstlm/userAPI/shipAPI.h"
#include "sys/time.h"

#include "xml_log/static_model/model_builder.h"

   #define USE_PVT

// make ship example communicative
//#define DEBUG

// print comma separated benchmark results
//#define CSV_MODE

/// how many objects are transferred sequentially for each measurement?
#define OBJ_COUNT 10
/// how many benchmark cycles?
#define LOOPS 20

/// initial size of ship objects (64 byte aligned) - will be doubled after each cycle
#define OBJ_SIZE 10


// time measure functions 
timeval tv_start, tv_stop;
inline void startMeasure() {
  gettimeofday(&tv_start, NULL);
}

inline void stopMeasure() {
  gettimeofday(&tv_stop, NULL);
#ifndef CSV_MODE
  cout << "+++ Duration +++ " << tv_stop.tv_sec-tv_start.tv_sec << "s " 
       << (((tv_stop.tv_usec-tv_start.tv_usec)<0) ? 
           (tv_start.tv_usec-tv_stop.tv_usec) :
           (tv_stop.tv_usec-tv_start.tv_usec)) << "us." << endl;
#else
  cout << tv_stop.tv_sec-tv_start.tv_sec;
#endif
}



/// This is the datatype which is transferred through the channel under test
class shipobj : ship_serializable_if {
public:
  /// default constructor, creates an empty ship object
  shipobj() 
    : m_pdata(NULL), 
      m_length(0)
  {}

  /// create a new shipobj with given size and fill it with test data
  shipobj(uint size) : m_length(size)
  {
    m_pdata = new sc_uint<64>[m_length];  

    // fill with test data
    for (uint i=0; i<m_length; i++)
      m_pdata[i] = shipobj::fill++;
  }

  /// copy constructor
  shipobj(const shipobj& obj) 
    : m_pdata(NULL) 
  {
    *this = obj; // use copy-operator
  }
  
  /// destructor
  virtual ~shipobj() {
    if (m_pdata != NULL)
      delete [] m_pdata;
  }
  
  /// copy-operator
  shipobj & operator = (const shipobj& obj) {
    if (m_pdata != NULL)
      delete [] m_pdata;
    m_pdata = new sc_uint<64>[obj.m_length];
    if (this != &obj) { // !self-assignment?
      m_length = obj.m_length;
      for (uint i=0; i<obj.m_length; i++)
        m_pdata[i] = obj.m_pdata[i];
    }
    return *this;
  }
  
  /// return a serial presentation of this ship object
  sc_uint<64> * serialize() { 
    return m_pdata; 
  }
  
  /// copy serial data array into this ship object
  unsigned int deserialize(sc_uint<64> *values, unsigned int length) { 
    if (m_pdata != NULL)
      delete [] m_pdata;
    m_pdata = new sc_uint<64>[length];
    m_length = length;
      
    for (uint i=0; i<length; i++)
      m_pdata[i] = values[i];
    return length;
  }
  
  /// get the serial data stream length of this ship object
  unsigned int getSerialLength() { return m_length; }

  /// print contents of this SHIP object
  void print() {
    cout << "shipobj [ ";
    for (uint i=0; i<m_length; i++)
      cout << m_pdata[i] << " ";
    cout << "]" << endl;
  }
  
  sc_uint<64> *m_pdata;
  uint m_length;

  static int fill;
};

int shipobj::fill = 0;


/// The producer continuously sends data to the channel under test.
SC_MODULE(producer) {
  shipMasterAPI<shipobj> out_port;
  
#ifdef USE_PVT
  SC_CTOR(producer) : out_port("producer_port", true) // use PVT mode 
#else
  SC_CTOR(producer) : out_port("producer_port") // use PV mode 
#endif
    {
#ifdef USE_PVT
      cout << name() << " I am a SHIP master running in PVT mode." << endl;
#else
      cout << name() << " I am a SHIP master running in PV mode." << endl;
#endif

      SC_THREAD(play);
    }    
  
    void play() {
      for (int c=0; c<LOOPS; c++) {

#ifndef CSV_MODE
        cout << endl << name() << " sending " << OBJ_COUNT << " SHIP objects of " << m_size << " size... " << endl;      
#else
        cout << m_size << ", ";
#endif

        //        shipobj obj(m_size);
        
        {
          startMeasure();
          for(uint i=0; i<OBJ_COUNT; i++) {
            shipobj obj(m_size);           // this slows down things SIGNIFICANTLY
            out_port.send(obj);
            wait(1, SC_NS);
          }
          stopMeasure();
        }

#ifndef CSV_MODE
        cout << endl << name() << " requesting " << OBJ_COUNT << " SHIP objects..." << endl;
#else
        cout << ", ";
#endif
        
        {
          startMeasure();
          for(uint i=0; i<OBJ_COUNT; i++) {
            shipobj obj;
            out_port.request(obj);
            
#ifdef DEBUG
            cout << name() << " got ";
            obj.print();
#endif
            
            wait(1, SC_NS);
          }
          stopMeasure();
        }

        cout << endl;

        // double obj size for next cycle
        m_size *= 2;
      }
    }  
    
    static uint m_size;
};

uint producer::m_size = OBJ_SIZE;

/// The consumer eats data objects from the channel under test.
SC_MODULE(consumer) {
  shipSlaveAPI<shipobj> in_port;

#ifdef USE_PVT
  SC_CTOR(consumer) : in_port("consumer_port", true)  // use PVT mode
#else
  SC_CTOR(consumer) : in_port("consumer_port")  // use PV mode
#endif
    {
#ifdef USE_PVT
      cout << name() << " I am a SHIP slave running in PVT mode." << endl;
#else
      cout << name() << " I am a SHIP slave running in PV mode." << endl;
#endif

      SC_THREAD(play);
    }
 
  void play() {
    for(;;) {
      ship_command cmd = in_port.waitEvent();

      if (cmd.cmd == SHIP_SEND) {
        shipobj obj;
        in_port.recv(obj);

#ifdef DEBUG
        cout << name() << " got ";
        obj.print();
#endif

      }
      else if (cmd.cmd == SHIP_REQUEST) {
        shipobj obj(producer::m_size);
        in_port.reply(obj);
      }
    }
  }  
};


int sc_main(int argc, char *argv[]) {
  producer p("Producer");
  consumer c("Consumer");
  
  // port-to-port binding
  p.out_port(c.in_port);

  sc_report_handler::set_actions(SC_INFO, SC_DISPLAY);
  sc_report_handler::set_actions(SC_ERROR, SC_DISPLAY);
  sc_report_handler::set_actions(SC_WARNING, SC_DISPLAY);

#ifdef CSV_MODE
  cout << endl << "size, send-sec, request-sec" << endl;
#endif

  // DUST structure analysis
  model_builder xml(true);
  
  sc_start(1000, SC_NS);
  
  return EXIT_SUCCESS;
}  






See more files for this project here

GreenSocs

To develop SystemC infrustructure, basic IP, patches and add on library code for eventual standerdization.\r\nThe GreenSocs project is made up of a number of contributions (sub projects). Please visit www.greensocs.com for more information.

Project homepage: http://sourceforge.net/projects/greensocs
Programming language(s): C,C++,Java,Perl,XML
License: other

  Makefile
  pv.txt
  pvt.txt
  ship_example.cpp
  ship_performance.ods
  ship_performance.png