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

SCM Repository

[diderot] View of /branches/pure-cfg/src/lib/c-target/main.c
ViewVC logotype

View of /branches/pure-cfg/src/lib/c-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1668 - (download) (as text) (annotate)
Sun Dec 4 10:13:05 2011 UTC (7 years, 9 months ago) by jhr
File size: 5647 byte(s)
  code formating
/*! \file main.c
 *
 * \author John Reppy
 */

/*
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#include <Diderot/diderot.h>
#include <teem/nrrd.h>

struct struct_world {
    STRUCT_WORLD_PREFIX
    void                **inState;
    void                **outState;
};

extern float getOutf (void *self);

int main (int argc, const char **argv)
{
    Diderot_Options_t *opts = Diderot_OptNew ();
    Diderot_RegisterGlobalOpts (opts);
    Diderot_OptProcess (opts, argc, argv);
    Diderot_OptFree (opts);

  // run the generated global initialization code
    if (VerboseFlg)
        fprintf (stderr, "initializing globals ...\n");
    Diderot_InitGlobals ();

  // FIXME: we need to figure out how initialization should be handled.
    if (VerboseFlg) fprintf (stderr, "initializing strands ...\n");
    Diderot_World_t *wrld = Diderot_Initially ();
    for (int i = 0;  i < wrld->numStrands;  i++) {
      // hack to make the invariant part of the state the same in both copies
        memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
    }

  // iterate until all strands are stable
    if (VerboseFlg) fprintf(stderr, "run with %d strands ...\n", wrld->numStrands);
    double t0 = airTime();
    int nSteps = 0, nUpdates = 0;
    int nActive = wrld->numStrands;
    while (nActive > 0) {
        nSteps++;
      // update strands
        bool existsStabilizing = false;
        for (int i = 0;  i < wrld->numStrands;  i++) {
            if (! wrld->status[i]) {
                nUpdates++;
                StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]);
                switch (sts) {
                  case DIDEROT_STABILIZE:
                    existsStabilizing = true;
                    wrld->status[i] = DIDEROT_STABILIZE;
                    break;
                  case DIDEROT_DIE:
                    wrld->status[i] = DIDEROT_DIE;
                    nActive--;
                    break;
                  default:
                    break;
                }
            }
        }
        if (existsStabilizing) {
            for (int i = 0;  i < wrld->numStrands;  i++) {
// NOTE: we may want to compact the array of strands
                if (wrld->status[i] == DIDEROT_STABILIZE) {
                  // stabilize the strand's state.  Note that the outState has been set by
                  // the last call to update, so we make the inState be the target of the
                  // stabilize method.
                    Diderot_Strands[0]->stabilize(wrld->outState[i], wrld->inState[i]);
                    memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
                    wrld->status[i] = DIDEROT_STABLE;
                    nActive--;
                }
            }
        }
      // swap in and out
        void **tmp = wrld->inState;
        wrld->inState = wrld->outState;
        wrld->outState = tmp;
    }
    double totalTime = airTime() - t0;

    if (VerboseFlg)
        fprintf (stderr, "done: %d updates, %d steps, in %f seconds\n", nUpdates, nSteps, totalTime);
    else if (TimingFlg)
        printf ("usr=%f\n", totalTime);

  // output the final strand states
    if (NrrdOutputFlg)
        Diderot_Output (wrld, Diderot_Strands[0]->outputSzb);
    else
        Diderot_Print (wrld);

    Diderot_Shutdown (wrld);

    return 0;

}

// block allocation of an initial collection of strands
Diderot_World_t *Diderot_AllocInitially (
    const char *name,           // the name of the program
    Strand_t *strand,           // the type of strands being allocated
    bool isArray,               // is the initialization an array or collection?
    uint32_t nDims,             // depth of iteration nesting
    int32_t *base,              // nDims array of base indices
    uint32_t *size)             // nDims array of iteration sizes
{
    Diderot_World_t *wrld = NEW(Diderot_World_t);
    if (wrld == 0) {
        fprintf (stderr, "unable to allocate world\n");
        exit (1);
    }

    wrld->name = name; /* NOTE: we are assuming that name is statically allocated! */
    wrld->isArray = isArray;
    wrld->nDims = nDims;
    wrld->base = NEWVEC(int32_t, nDims);
    wrld->size = NEWVEC(uint32_t, nDims);
    size_t numStrands = 1;
    for (int i = 0;  i < wrld->nDims;  i++) {
        numStrands *= size[i];
        wrld->base[i] = base[i];
        wrld->size[i] = size[i];
    }

    if (VerboseFlg) {
        fprintf(stderr, "AllocInitially: %d", size[0]);
        for (int i = 1;  i < nDims;  i++) fprintf(stderr, " x %d", size[i]);
        fprintf(stderr, "\n");
    }

  // allocate the strand state pointers
    wrld->numStrands = numStrands;
    wrld->inState = NEWVEC(void *, numStrands);
    wrld->outState = NEWVEC(void *, numStrands);
    wrld->status = NEWVEC(uint8_t, numStrands);
    if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
        fprintf (stderr, "unable to allocate strand states\n");
        exit (1);
    }

  // initialize strand state pointers etc.
    for (size_t i = 0;  i < numStrands;  i++) {
        wrld->inState[i] = CheckedAlloc (strand->stateSzb);
        wrld->outState[i] = CheckedAlloc (strand->stateSzb);
        wrld->status[i] = DIDEROT_ACTIVE;
    }

    return wrld;

}

// get strand state pointers
void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
{
    assert (i < wrld->numStrands);
    return wrld->inState[i];
}

void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
{
    assert (i < wrld->numStrands);
    return wrld->outState[i];
}

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