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.in
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4631 - (download) (annotate)
Sun Sep 25 00:15:30 2016 UTC (2 years, 9 months ago) by jhr
File size: 6751 byte(s)
  Working on merge: bug fixes
// forward declarations of strand methods
#ifdef DIDEROT_HAS_START_METHOD
static diderot::strand_status @STRAND@_start (@START_PARAMS@@STRANDTY@ *self);
#endif // DIDEROT_HAS_START_METHOD
static diderot::strand_status @STRAND@_update (@UPDATE_PARAMS@@STRANDTY@ *self);
#ifdef DIDEROT_HAS_STABILIZE_METHOD
static void @STRAND@_stabilize (@STABILIZE_PARAMS@@STRANDTY@ *self);
#endif // DIDEROT_HAS_STABILIZE_METHOD

// strand_array for NO BSP/SINGLE STATE/DIRECT ACCESS
//
struct strand_array {
    typedef @STRANDTY@ strand_t;
    typedef uint32_t index_t;
    typedef index_t sid_t;              // strand ID (index into strand-state storage)

    uint8_t             *_status;       // the array of status information for the strands
    char                *_storage;      // points to array of @STRANDTY@ structs
    char                *_inStateBase;  // base pointer for accessing varying state
    char                *_outStateBase; // the other base pointer for accessing varying state
    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 0; /* dummy */ }

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

  // return the ID of a strand, which is the value of the _idx array
    sid_t id (index_t ix) const
    {
        assert (ix < this->_nItems);
        return ix;
    }
  // return a pointer to the strand with the given ID
    @STRANDTY@ *id_to_strand (sid_t id) const
    {
        assert (id < this->_nItems);
        return reinterpret_cast<@STRANDTY@ *>(this->_storage + id * sizeof(@STRANDTY@));
    }

  // return a strand's status
    diderot::strand_status status (index_t ix) const
    {
        return static_cast<diderot::strand_status>(this->_status[ix]);
    }
  // return a pointer to the given strand
    @STRANDTY@ *strand (index_t ix) const
    {
        return this->id_to_strand(this->id(ix));
    }
  // return a pointer to the local state of strand ix
    @STRANDTY@ *local_state (index_t ix) const
    {
        return this->strand(ix);
    }
  // return a pointer to the local state of strand with the given ID
    @STRANDTY@ *id_to_local_state (sid_t id) const
    {
        return this->id_to_strand(id);
    }

  // 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->_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++) {
            this->_status[ix] = diderot::kActive;
            new(this->strand(ix)) @STRANDTY@;
        }
        this->_nActive = nStrands;
    }

  // swap in and out states (NOP for this version)
    void swap () { }

#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->strand(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->strand(ix));
    }

  // invoke strand's stabilize method
    index_t strand_stabilize (@STABILIZE_PARAMS@index_t ix)
    {
#ifdef DIDEROT_HAS_STABILIZE_METHOD
        @STRAND@_stabilize (@STABILIZE_ARGS@this->strand(ix));
#endif // DIDEROT_HAS_STABILIZE_METHOD
        this->_status[ix] = diderot::kStable;
        this->_nActive--;
        this->_nStable++;
        return ix+1;
    }

  // mark the given strand as dead
    index_t kill (index_t ix)
    {
        this->_status[ix] = diderot::kDead;
        this->_nActive--;
        return ix+1;
    }

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

  // iterator over alive (active+stable) strands
    index_t begin_alive () const
    {
        index_t ix = 0;
#ifdef DIDEROT_HAS_STRAND_DIE
        while ((ix < this->_nItems) && (this->_status[ix] == diderot::kDead)) {
            ix++;
        }
#endif
        return ix;
    }
    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
    {
        index_t ix = 0;
        while ((ix < this->_nItems) && (this->_status[ix] != diderot::kActive)) {
            ix++;
        }
        return ix;
    }
    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
    {
        index_t ix = 0;
        while ((ix < this->_nItems) && (this->_status[ix] != diderot::kStable)) {
            ix++;
        }
        return ix;
    }
    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