Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/target-cpu/fragments/seq-sarr-dual.in
ViewVC logotype

View of /branches/vis15/src/compiler/target-cpu/fragments/seq-sarr-dual.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4551 - (download) (annotate)
Fri Sep 9 13:56:32 2016 UTC (2 years, 11 months ago) by jhr
File size: 5745 byte(s)
  working on merge: add tear-down code for strand states
// forward declarations of strand methoda
#ifdef DIDEROT_HAS_START_METHOD
static diderot::strand_status @STRAND@_start (@START_PARAMS@
    @STRAND@_local *selfLocal, @STRAND@_shared *selfIn, @STRAND@_shared *selfOut);
#endif // DIDEROT_HAS_START_METHOD
static diderot::strand_status @STRAND@_update (@UPDATE_PARAMS@
    @STRAND@_local *selfLocal, @STRAND@_shared *selfIn, @STRAND@_shared *selfOut);
#ifdef DIDEROT_HAS_STABILIZE_METHOD
static void @STRAND@_stabilize (@STABILIZE_PARAMS@
    @STRAND@_local *selfLocal, @STRAND@_shared *selfIn, @STRAND@_shared *selfOut);
#endif // DIDEROT_HAS_STABILIZE_METHOD

// strand_array for BSP/DUAL STATE/DIRECT ACCESS
//
struct strand_array {
    typedef @STRANDTY@ strand_t;
    typedef uint32_t index_t;

    uint8_t		*_status;	// the array of status information for the strands
    char		*_storage;	// points to array of @STRANDTY@ structs
    uint32_t            _inIdx;         // index of shared input state (either 0 or 1)
    uint32_t		_nItems;	// number of items in the _storage and _status arrays
    uint32_t		_nStable;	// stable strands (in locations 0.._nStable-1)
    uint32_t		_nActive;	// active strands (in locations _nStable.._nStable+_nActive-1)

    strand_array () : _status(nullptr), _storage(nullptr), _nItems(0) { }
    ~strand_array ();

    uint32_t in_state_index () const { return this->_inIdx; }

    uint32_t num_active () const { return this->_nActive; }
    uint32_t num_stable () const { return this->_nStable; }
    uint32_t num_alive () const { return this->_nActive+this->_nStable; }

  // return a pointer to the given strand
    @STRANDTY@ *strand (index_t ix) const
    {
	return reinterpret_cast<@STRANDTY@ *>(this->_storage + ix * sizeof(@STRANDTY@));
    }

  // return a pointer to the local state of strand ix
    @STRAND@_local *local_state (index_t ix) const
    {
        return &(this->strand()->_local);
    }
  // return a pointer to the in-state of strand ix
    const @STRAND@_shared *in_state (index_t ix) const
    {
        return &(this->strand()->_shared[this->_inIdx]);
    }
  // return a pointer to the out-state of strand ix
    @STRAND@_shared *out_state (index_t ix) const
    {
        return &(this->strand()->_shared[this->_inIdx ^ 1]);
    }

  // allocate space for nItems
    bool alloc (uint32_t nItems)
    {
	this->_storage = static_cast<char *>(std::malloc (nItems * sizeof(@STRANDTY@)));
	if (this->_storage == nullptr) {
	    return true;
	}
	this->_status = static_cast<uint8_t *>(std::malloc (nItems * sizeof(uint8_t)));
	if (this->_status == nullptr) {
	    std::free (this->_storage);
	    return true;
	}
	this->_inIdx = 0;
	this->_nItems = nItems;
	this->_nActive = 0;
	this->_nStable = 0;
	return false;
    }

  // initialize the first nStrands locations as new active strands
    void create_strands (uint32_t nStrands)
    {
	assert (this->_nActive == 0);
	assert (this->_nItems == nStrands);
	for (uint32_t ix = 0;  ix < nStrands;  ix++) {
	    new(this->strand(ix)) @STRANDTY@;
	}
	this->_nActive = nStrands;
    }

  // swap in and out states
    void swap ()
    {
        tihs->_inIdx ^= 1;
    }

#ifdef DIDEROT_HAS_START_METHOD
  // invoke strand's start method
    diderot::strand_status strand_start (@START_PARAMS@index_t ix)
    {
	return @STRAND@_start (@START_ARGS@
	    this->local_state(ix),
	    this->in_state(ix),
	    this->out_state(ix));
    }
#endif // DIDEROT_HAS_START_METHOD

  // invoke strand's update method
    diderot::strand_status strand_update (@UPDATE_PARAMS@index_t ix)
    {
	return @STRAND@_update (@UPDATE_ARGS@
	    this->local_state(ix),
	    this->in_state(ix),
	    this->out_state(ix));
    }

  // invoke strand's stabilize method
    void strand_stabilize (@STABILIZE_PARAMS@index_t ix)
    {
#ifdef DIDEROT_HAS_STABILIZE_METHOD
	@STRAND@_shared *inState = this->in_state(ix);
	@STRAND@_shared *outState = this->out_state(ix);
        @STRAND@_stabilize (@STABILIZE_ARGS@
	    this->local_state(ix), inState, outState);
      // copy output state to input state
	std::memcpy (inState, outState, sizeof(@STRAND@_shared));
#endif // DIDEROT_HAS_STABILIZE_METHOD
	this->_status[ix] = diderot::kStable;
	this->_nActive--;
	this->_nStable++;
    }

  // finish a step (NOP)
    void finish_step ()
    {
    }

  // iterator over alive (active+stable) strands
    index_t begin_alive () const { return 0; }
    index_t end_alive () const { return this->_nItems; }
    index_t next_alive (index_t &ix) const
    {
        ix++;
#ifdef DIDEROT_HAS_STRAND_DIE
        while ((ix < this->_nItems) && (this->_status[ix] == diderot::kDead)) {
            ix++;
        }
#endif
        return ix;
    }

  // iterator over active strands
    index_t begin_active () const { return 0; }
    index_t end_active () const { return this->_nItems; }
    index_t next_active (index_t &ix) const
    {
        ix++;
        while ((ix < this->_nItems) && (this->_status[ix] != diderot::kActive)) {
            ix++;
        }
        return ix;
    }

  // iterator over stable strands
    index_t begin_stable () const { return 0; }
    index_t end_stable () const { return this->_nItems; }
    index_t next_stable (index_t &ix) const
    {
        ix++;
        while ((ix < this->_nItems) && (this->_status[ix] != diderot::kStable)) {
            ix++;
        }
        return ix;
    }

}; // struct strand_array

strand_array::~strand_array ()
{
  // run destructors to reclaim any dynamic memory attached to the strand state
    for (auto ix = this->begin_alive();  ix != this->end_alive();  ix = this->next_alive(ix)) {
	this->strand(ix)->~@STRANDTY@();
    }
    if (this->_status != nullptr) std::free (this->_status);
    if (this->_storage != nullptr) std::free (this->_storage);
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0