Reference Guide Appendix: A4

A4. Source code for diagraph cell (digcell) model

//
// dctest.C
//

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <strio.h>
#include <num_ent.h>
#include <block.h>
#include <digraph.h>
#include <digcell.h>
#include “genr.h”
#include “transd.h”
#include “pcell.h”
#include “layer.h”

int main(int argc, char ** argv) {
    int i, j;
    float amount;
    pcell *nc;
    digcell *tmpdig;

    // **************************************************
    // relationship between genr & transd
    // **************************************************
    genr * g;
    g = new genr(“g”,1);                                         // int_arr_time
    transd * t;
    t = new transd(“t”,10);
    block * top;
    top = new block(“top”);
    digraph * efsim = new digraph(“efsim”);
    efsim->add(g);
    efsim->add(t);
    efsim->add(top);

    int rowmin, rownum, colmin, colnum;
    rowmin = 1;
    rownum = 1;
    colmin = 1;
    colnum = 2;
    for (i=rownum;i>=rowmin;i–) {
        for (j=colnum;j>=colmin;j–) {
            if ((i+j)%2 ==0) {
                top->add(nc = new pcell(name_gen(“c”,i,j,1),new addrclass(i,j,1), 1));
            }
            else {
                top->add(tmpdig=new digcell(name_gen(“dc”,i,j,1), new addrclass(i,j,1)));
                tmpdig->get_inports()->add(“in”);
                tmpdig->get_inports()->add(“in”);
                tmpdig->get_outports()->add(“out”);
                tmpdig->get_outports()->add(“out”);
int k=1;
                layer *tl_1, *tl_2;
                tmpdig->add(tl_1 = new layer(name_gen(“layer”, k),1));         // 1: proc time
                k++;
                tmpdig->add(tl_2 = new layer(name_gen(“layer”, k),1));        // 1: proc time
                tmpdig->add_coupling(tmpdig,”in”,tl_1,”in”);
                tl_1->add_coupling(tl_1,”out”,tl_2,”in”);
                tl_2->add_coupling(tl_2,”out”,tmpdig,”out”);
            }
        }
    }
    top->add_coupling(“out”, “in”);
    g->add_coupling(g, “out”, t, “ariv”);
    g->add_coupling(g, “out”, top, “in”);
    t->add_coupling(t,”out”,g,”stop”);
    top->add_coupling(top, “out”, t, “solved”);
    efsim->get_inports()->add(“start”);
    efsim->get_outports()->add(“stop”);
    efsim->get_outports()->add(“result”);
    efsim->add_coupling(efsim, “start”, g, “start”);
    t->add_coupling(t, “out”, top, “stop”);
    efsim->initialize();
    amount = 1000;
    efsim->inject(“start”, new float_ent(amount));
    efsim->simulate(10);
    return 0;
}

//
// genr.h
//

#ifndef _GENRH_
#define _GENRH_

#include <atomic.h>

class genr : public atomic {
protected:
    timetype processing_time;
    float amount;
    int count;
public:
    genr(char * name, timetype int_arr_time) ;
    void deltext(timetype e,message * x) ;
    void deltint();
    message * out();
    void show_state();
    void show_output();
};
#endif

//
//genr.C
//

#include “genr.h”
#include <num_ent.h>

genr::genr(char * name,timetype int_arr_time) : atomic(name) {
    inports->add(“stop”);
    inports->add(“start”);
    outports->add(“out”);
    phases->add(“busy”);
    this->processing_time = int_arr_time ;
    count = 0;
    hold_in(“busy”, processing_time);
}

void genr::deltext(timetype e,message * x) {
    if(x->empty()) return;                                             // for timing sync
    Continue();
    element *el, *elnext;
    for (el=x->get_head(); el!=NULL;el=elnext) {
        elnext = el->get_right();
        x->remove(el);
        content *con;
        con = (content *)el->get_ent();
        if(strcmp(con->p,”stop”)==0) passivate();
        if(strcmp(con->p,”start”)==0) {
            amount = ((float_ent *)con->val)->getv();
            hold_in(“busy”,processing_time);
        }
        delete con;
        delete el;
    }
}

void genr::deltint() {
    count++;
    passivate();
}

message * genr::out() {
    message * m = new message();
    float_ent * amt = new float_ent(amount);
    content * con = make_content_address(“out”, (entity *) amt,
    new addrclass(1,1,1));
    m->add(con);
    m->show_message();
    return m;
}

void genr::show_state() {
    cout << “\nstate of ” << name << “: ” ;
    cout << “phase, sigma,count : “ <<phase << ” ” << sigma << ” ” << count <<endl;
}

//
// layer.h
//

#ifndef _LAYER_H_
#define _LAYER_H_

#include <atomic.h>

class layer : public atomic {
protected:
    entity *store;
    int processing_time;
public:
    layer(char * name, int pt);
    void deltint();
    void deltext(timetype e, message * x);
    message * out();
};
#endif

//
// layer.C
//

#include “layer.h”
#include <atomic.h>
#include <num_ent.h>
#include <digcell.h>

layer::layer(char * name, int pt)  : atomic(name) {
    inports->add(“in”);
    inports->add(“stop”);
    outports->add(“out”);
    this->processing_time = pt ;
    store = NULL;
}

/*
 * External Transition Function
 */

void layer::deltext(timetype e, message * x) {
    if(x->empty()) return;                                         // for timing sync
    Continue();
    element *el, *elnext;
    for (el=x->get_head(); el!=NULL;el=elnext) {
        elnext = el->get_right();
        x->remove(el);
        content *con;
        con = (content *)el->get_ent();
        if(strcmp(con->p,”stop”)==0) passivate();
        if(strcmp(con->p,”in”)==0) {
            store = con->val;
            hold_in(“busy”,processing_time);
        }
        delete con;
        delete el;
    }
}

/*
* Internal transition Function
*/

void layer::deltint() {
    passivate();
}

message * layer::out() {
    message * m = new message();
    addrclass * addr;
    content * con;
    int ii, jj, kk;
    ii = ((digcell *)get_parent())->get_my_location()->i;
    jj = ((digcell *)get_parent())->get_my_location()->j;
    kk = ((digcell *)get_parent())->get_my_location()->k;
    addr = new addrclass(ii, jj+1, kk);
    con = make_content_address(“out”,(entity *)store, addr);
    m->add(con);
    store = NULL;
    m->show_message();
    return m;
}

//
// pcell.h
//

#ifndef _PCELL_H_
#define _PCELL_H_

#include <cell.h>

class pcell : public cell {
protected:
    entity *store;
    int processing_time;
public:
    pcell(char * name, addrclass * location, int pt);
    void deltint();
    void deltext(timetype e,message * x);
    message * out();
};

#endif

//
// pcell.C
//

#include “pcell.h”
#include <atomic.h>
#include <num_ent.h>

extern int rownum,colnum, rowmin, colmin;

pcell::pcell(char * name, addrclass * location, int pt) :cell(name,location) {
    inports->add(“in”);
    inports->add(“stop”);
    outports->add(“out”);
    this->processing_time = pt ;
    store = NULL;
}

 /*
* External Transition Function
*/

void pcell::deltext(timetype e,message * x) {
    if(x->empty()) return; // for timing sync
    Continue();
    element *el, *elnext;
    for (el=x->get_head(); el!=NULL;el=elnext) {
        elnext = el->get_right();
        x->remove(el);
        content *con;
        con = (content *)el->get_ent();
        if(strcmp(con->p,”stop”)==0) passivate();
        if(strcmp(con->p,”in”)==0) {
            store = con->val;
            hold_in(“busy”,processing_time);
        }
    }
}

/*
* Internal transition Function
*/

void pcell::deltint() {
    passivate();
}

message * pcell::out() {
    message * m = new message();
    addrclass * addr;
    content * con;
    addr = new addrclass(my_location->i, my_location->j+1, 1);
    con = make_content_address(“out”,(entity *)store, addr);
    m->add(con);
    store = NULL;
    m->show_message();
    return m;
}

//
// trnasd.h
//

#ifndef _TRANSDH_
#define _TRANSDH_

#include <atomic.h>
#include <function.h>

class transd : public atomic {
protected:
    function *arrived, *solved;
    timetype clock,total_ta,observation_time;
public:
    transd(char * name,timetype observation_time);
    void deltext(timetype e,message * x);
    void deltint();
    message * out();
    void show_state();
    void show_output();
};

#endif

//
// transd.C
//

#include “transd.h”
#include <mess.h>
#include <num_ent.h>

transd::transd(char * name,timetype observation_time):atomic(name) {
    inports->add(“null”);
    inports->add(“ariv”);
    inports->add(“solved”);
    outports->add(“out”);
    phases->add(“active”);
    arrived = new function();
    solved = new function();
    phase = “active”;
    this->observation_time = observation_time;
    hold_in(“active”, observation_time);
    clock = 0.0;
    total_ta = 0.0;
}

void transd::deltext(timetype e,message * x) {
    timetype clock;
    clock = tL + e;
 
    if(x->empty()) return;                                         // for timing sync
    Continue();
    element *el, *elnext;
    for (el=x->get_head(); el!=NULL;el=elnext) {
        elnext = el->get_right();
        x->remove(el);
        content *con;
        con = (content *)el->get_ent();
        if(strcmp(con->p,”ariv”)==0) {
            arrived->add(con->val,new float_ent(clock));
        }
        else if(strcmp(con->p,”solved”)==0) {
            entity * ent = arrived->assoc(con->val);
            timetype arrival_time = ((float_ent *)ent)->getv();
            timetype turn_around_time = clock – arrival_time;
            total_ta = total_ta + turn_around_time;
            solved->add(con->val, new float_ent(clock));
        }
        delete con;
        delete el;
    }
}

void transd::deltint() {
    passivate();
}

message * transd::out() {
    timetype avg_ta_time = 0;
    if (!solved->empty()) avg_ta_time = total_ta/solved->get_length();
    float throughput;
    throughput = solved->get_length()/tN;

    char a[10], b[10];
    strcpy(a, arrived->get_head()->get_ent()->get_name());
    strcpy(b, solved->get_head()->get_ent()->get_name());
    if ( strcmp(a,b)==0 ) {
        cout << endl;
        cout << “*** TEST SATISFIED ***” << endl;
        cout << endl;
    }
    else {
        cout << endl;
        cout << “test NOT satisfied” << endl;
        cout << endl;
    }
    cout << “jobs arrived :” ;
    arrived->print();
    cout << “jobs solved :” ;
    solved->print();
    cout << endl;
    cout << “AVG TA = ” << avg_ta_time <<endl;
    cout << “THROUGHPUT = ” << throughput <<endl;
    cout << endl;
    message * m = new message();
    content * con = make_content(“out”,new entity(“finished”));
    m->add(con);
    m->show_message();
    return m;
}

void transd::show_state() {
    cout << “state of ” << name << “: ” ;
    cout << “phase, sigma,arrived,solved: ” <<phase << ” ” << sigma << ” “;
}