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

SCM Repository

[diderot] View of /trunk/src/lib/c-target/main.c
ViewVC logotype

View of /trunk/src/lib/c-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1380 - (download) (as text) (annotate)
Thu Jun 23 19:20:07 2011 UTC (8 years ago) by jhr
File size: 7078 byte(s)
  merging changes from pure-cfg
/*! \file main.c
 *
 * \author John Reppy
 */

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

#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <Diderot/diderot.h>

// NOTE: we probably should put this in a file that supports runtime printing
static bool     VerboseFlg = false;
static bool     TimingFlg = false;

struct struct_world {
    const char          *name;          // the program name
    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
    uint32_t            numStrands;     // number of strands in the world
    void                **inState;
    void                **outState;
    uint8_t             *status;        // array of strand status flags
};

extern float getOutf (void *self);

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

    Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
    Diderot_OptAddFlag (opts, "timing", "enable execution timing", &TimingFlg);
    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 = GetTime();
    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) {
                  // copy out to in so that both copies are the stable state
                    memcpy (wrld->inState[i], wrld->outState[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 = GetTime() - 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);

  // here we have the final state of all of the strands in the "in" buffer
    int outFileNameLen = strlen(wrld->name) + 5;
    char *outFileName = (char *)malloc(outFileNameLen);
    snprintf (outFileName, outFileNameLen, "%s.txt", wrld->name);
    FILE *outS = fopen(outFileName, "w");
    if (outS == NULL) {
        fprintf (stderr, "Cannot open output file %s\n", outFileName);
        exit (8);
    }

    for (int i = 0;  i < wrld->numStrands;  i++) {
        if (wrld->status[i] == DIDEROT_STABLE)
            Diderot_Strands[0]->print (outS, wrld->inState[i]);
    }
    fclose (outS);

    Diderot_Shutdown (wrld);

    return 0;

}


// this should be the part of the scheduler
void *Diderot_AllocStrand (Strand_t *strand)
{
    return malloc(strand->stateSzb);
}

// 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 = (Diderot_World_t *) malloc (sizeof(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 = (int32_t *) malloc (nDims * sizeof(int32_t));
    wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
    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 = (void **) malloc (numStrands * sizeof(void *));
    wrld->outState = (void **) malloc (numStrands * sizeof(void *));
    wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
    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] = Diderot_AllocStrand (strand);
        wrld->outState[i] = Diderot_AllocStrand (strand);
        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];
}

bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
{
    assert (i < wrld->numStrands);
    return !wrld->status[i];
}

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