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

SCM Repository

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

View of /branches/pure-cfg/src/lib/common/output.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1517 - (download) (as text) (annotate)
Fri Oct 7 19:52:25 2011 UTC (9 years, 11 months ago) by jhr
File size: 5435 byte(s)
  Fix compiler warnings
/*! \file output.c
 *
 * \author Nick Seltzer
 */

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

#include <stdio.h>
#include "Diderot/diderot.h"
#include "Diderot/output.h"
#include <teem/nrrd.h>
#include <teem/air.h>

/*! \brief Print the strand output state to a text file.
 */
void Diderot_Print (Output_Args_t *args)
{
    airArray *mop = airMopNew();

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

    uint8_t *strandPtr = (uint8_t *)args->inState;
    for (int i = 0;  i < args->numStrands;  i++, strandPtr += args->outputSzb) {

        if ((args->isStatusInt && (args->status[i * 4] == DIDEROT_STABLE))
        || (args->status[i] == DIDEROT_STABLE)) {
        /*if (args->status[i] == DIDEROT_STABLE) {*/
            Diderot_Strands[0]->print (outS, strandPtr);
        }
    }

    airMopOkay(mop);
}


/*! \brief Output the strand output state to a nrrd or text file.
 */
void Diderot_Output (Output_Args_t *args)
{
    airArray *mop = airMopNew();
    int status;
    char *err;

  // get correct output file name
    int outFileNameLen = strlen(args->name) + 6;
    char *outFileName = (char *)CheckedAlloc(outFileNameLen);
    airMopAdd(mop, outFileName, airFree, airMopAlways);
    snprintf (outFileName, outFileNameLen, "%s.nrrd", args->name);
    fprintf(stderr, "%d", 1);
  // allocate the correct amount of space for output
    int numStabilized = 0;
    for (int i = 0;  i < args->numStrands;  i++) {
// FIXME: this conditional is not portable because of endianess issues.
        if ((args->isStatusInt && (args->status[i * 4] == DIDEROT_STABLE))
        || (args->status[i] == DIDEROT_STABLE))
            numStabilized++;
    }
    void *outData = CheckedAlloc(numStabilized * args->outputSzb);
    airMopAdd(mop, outData, airFree, airMopAlways);
    fprintf(stderr, "%d", 2);

  // copy data into outData
    void *outPos = outData;
    for (int i = 0;  i < args->numStrands;  i++) {
        if ((args->isStatusInt && (args->status[i * 4] == DIDEROT_STABLE))
        || (args->status[i] == DIDEROT_STABLE)) {
            Diderot_Strands[0]->output (outPos, &args->inState[i]);
            outPos += args->outputSzb;
        }
    }

  // make a new nrrd
    Nrrd *outNrrd;
    outNrrd = nrrdNew();
    if (outNrrd == NULL) {
        fprintf(stderr, "Trouble allocating nrrd struct:\n%s", err);
        airMopError(mop);
        exit(1);
    }
    airMopAdd(mop, outNrrd, (airMopper)nrrdNix, airMopAlways);
    fprintf(stderr, "%d", 4);

  // wrap the data (outData) with the nrrd (outNrrd)
    if (args->isArray) {
        size_t *sizes = NEWVEC(size_t, args->nDims + 1);
        airMopAdd(mop, sizes, airFree, airMopAlways);
        switch(Diderot_Strands[0]->nrrdType) {
            case nrrdTypeInt:
            case nrrdTypeFloat:
                sizes[0] = args->outputSzb >> 2;
                break;
            case nrrdTypeLLong:
            case nrrdTypeDouble:
                sizes[0] = args->outputSzb >> 3;
                break;
            default:
                fprintf(stderr, "Bad nrrdType\n");
                airMopError(mop);
                exit(1);
                break;
        }
        for (int i = 1; i <= args->nDims; i++) {
            sizes[i] = args->size[args->nDims - i];
        }
        status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, args->nDims + 1, sizes);
        if (status) {
            err = biffGetDone(NRRD);
            fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
            airMopError(mop);
            free(err);
            exit(1);
        }
    }
    else {
        size_t *sizes = NEWVEC(size_t, 2);
        airMopAdd(mop, sizes, airFree, airMopAlways);
        switch(Diderot_Strands[0]->nrrdType) {
            case nrrdTypeInt:
            case nrrdTypeFloat:
                sizes[0] = args->outputSzb >> 2;
                break;
            case nrrdTypeLLong:
            case nrrdTypeDouble:
                sizes[0] = args->outputSzb >> 3;
                break;
            default:
                fprintf(stderr, "Bad nrrdType\n");
                airMopError(mop);
                exit(1);
                break;
        }
        sizes[1] = numStabilized;
        status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, args->nDims, sizes);
        if (status != 0) {
            err = biffGetDone(NRRD);
            fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
            airMopError(mop);
            free(err);
            exit(1);
        }
    }
    fprintf(stderr, "%d", 5);

  // save the data
    status = nrrdSave(outFileName, outNrrd, NULL);
    if(status) {
        err = biffGetDone(NRRD);
        fprintf(stderr, "Trouble saving nrrd struct:\n%s", err);
        airMopError(mop);
        free(err);
        exit(1);
    }
    fprintf(stderr, "%d", 6);

    airMopOkay(mop);

}

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