SCM Repository
View of /trunk/src/lib/main.c
Parent Directory
|
Revision Log
Revision 1115 -
(download)
(as text)
(annotate)
Thu May 5 04:42:18 2011 UTC (9 years, 11 months ago) by jhr
File size: 4911 byte(s)
Thu May 5 04:42:18 2011 UTC (9 years, 11 months ago) by jhr
File size: 4911 byte(s)
More merging of pure-cfg back into trunk
/*! \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> struct struct_world { 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; bool *isActive; // array of isActive flags }; extern float getOutf (void *self); int main (int argc, const char **argv) { // printf("initializing globals ...\n"); Diderot_InitGlobals (); // FIXME: we need to figure out how initialization should be handled. printf("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 printf("run with %d strands ...\n", wrld->numStrands); int nSteps = 0, nUpdates = 0; int nActive = wrld->numStrands; while (nActive > 0) { nSteps++; // update strands for (int i = 0; i < wrld->numStrands; i++) { if (wrld->isActive[i]) { nUpdates++; StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]); switch (sts) { case DIDEROT_STABILIZE: // copy out to in so that both copies are the stable state // FIXME: there is a race condition here, since other strands might query this strand memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb); wrld->isActive[i] = false; nActive--; break; case DIDEROT_DIE: // FIXME: need more than booleans here, since we have to differentiate between dead and stable strands wrld->isActive[i] = false; nActive--; break; } } } // swap in and out void **tmp = wrld->inState; wrld->inState = wrld->outState; wrld->outState = tmp; } printf("done: %d updates, %d steps\n", nUpdates, nSteps); // here we have the final state of all of the strands in the "in" buffer FILE *outS = fopen("mip.txt", "w"); if (outS == NULL) { fprintf (stderr, "Cannot open output file\n"); exit (8); } for (int i = 0; i < wrld->numStrands; i++) { Diderot_Strands[0]->print (outS, wrld->inState[i]); } fclose (outS); 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 ( 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->isArray = isArray; wrld->nDims = nDims; wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t)); wrld->size = (int32_t *) malloc (nDims * sizeof(int32_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]; } printf("AllocInitially: %d", size[0]); for (int i = 1; i < nDims; i++) printf(" x %d", size[i]); printf("\n"); // allocate the strand state pointers wrld->numStrands = numStrands; wrld->inState = (void **) malloc (numStrands * sizeof(void *)); wrld->outState = (void **) malloc (numStrands * sizeof(void *)); wrld->isActive = (bool *) malloc (numStrands * sizeof(bool)); if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->isActive == 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->isActive[i] = true; } 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->isActive[i]; }
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |