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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1130 - (download) (as text) (annotate)
Thu May 5 18:39:29 2011 UTC (8 years, 3 months ago) by jhr
File size: 5091 byte(s)
  Runtime system updates to support Die and to eliminate race for stabilizing
/*! \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;
    uint8_t		*status;	// array of strand status 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
	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;
		}
	    }
	}
	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;
    }

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++) {
	if (wrld->status[i] == DIDEROT_STABLE)
	    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->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