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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1449, Mon Jul 18 15:26:21 2011 UTC revision 1450, Mon Jul 18 18:45:43 2011 UTC
# Line 12  Line 12 
12  #include <string.h>  #include <string.h>
13  #include <stdio.h>  #include <stdio.h>
14  #include <assert.h>  #include <assert.h>
15    #include <teem/nrrd.h>
16    
17  // NOTE: we probably should put this in a file that supports runtime printing  // NOTE: we probably should put this in a file that supports runtime printing
18  static bool     VerboseFlg = false;  static bool     VerboseFlg = false;
19  static bool     TimingFlg = false;  static bool     TimingFlg = false;
20    static bool     NrrdOutputFlg = false;
21    
22  struct struct_world {  struct struct_world {
23      const char          *name;          //!< the program name      const char          *name;          //!< the program name
# Line 37  Line 39 
39    
40      Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);      Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
41      Diderot_OptAddFlag (opts, "timing", "enable execution timing", &TimingFlg);      Diderot_OptAddFlag (opts, "timing", "enable execution timing", &TimingFlg);
42        Diderot_OptAddFlag (opts, "nrrd", "enable nrrd output", &NrrdOutputFlg);
43      Diderot_RegisterGlobalOpts (opts);      Diderot_RegisterGlobalOpts (opts);
44      Diderot_OptProcess (opts, argc, argv);      Diderot_OptProcess (opts, argc, argv);
45      Diderot_OptFree (opts);      Diderot_OptFree (opts);
# Line 110  Line 113 
113      else if (TimingFlg)      else if (TimingFlg)
114          printf ("usr=%f\n", totalTime);          printf ("usr=%f\n", totalTime);
115    
116        if(!NrrdOutputFlg) {
117    // here we have the final state of all of the strands in the "in" buffer    // here we have the final state of all of the strands in the "in" buffer
118      int outFileNameLen = strlen(wrld->name) + 5;      int outFileNameLen = strlen(wrld->name) + 5;
119      char *outFileName = (char *)malloc(outFileNameLen);      char *outFileName = (char *)malloc(outFileNameLen);
# Line 125  Line 129 
129              Diderot_Strands[0]->print (outS, wrld->inState[i]);              Diderot_Strands[0]->print (outS, wrld->inState[i]);
130      }      }
131      fclose (outS);      fclose (outS);
132        }
133        else {
134            int status;
135            char *err;
136    
137            // get correct output file name
138            int outFileNameLen = strlen(wrld->name) + 6;
139            char *outFileName = (char *)malloc(outFileNameLen);
140            snprintf (outFileName, outFileNameLen, "%s.nrrd", wrld->name);
141    
142            // allocate the correct amount of space for output
143            int numStabilized = 0;
144            for (int i = 0;  i < wrld->numStrands;  i++) {
145                if (wrld->status[i] == DIDEROT_STABLE)
146                    numStabilized++;
147            }
148            void *outData = malloc(numStabilized * Diderot_Strands[0]->outputSzb);
149    
150            // copy data into outData
151            void *outPos = outData;
152            for (int i = 0;  i < wrld->numStrands;  i++) {
153                if (wrld->status[i] == DIDEROT_STABLE) {
154                    Diderot_Strands[0]->out (outPos, wrld->inState[i]);
155                    outPos += Diderot_Strands[0]->outputSzb;
156                }
157            }
158    
159            // make a new nrrd
160            Nrrd *outNrrd;
161            outNrrd = nrrdNew();
162            if(outNrrd == NULL) {
163                err = biffGetDone(NRRD);
164                fprintf(stderr, "Trouble allocating nrrd struct:\n%s", err);
165                free(err);
166                free(outData);
167                exit(1);
168              }
169    
170            // wrap the data (outData) with the nrrd (outNrrd)
171            if(wrld->isArray) {
172                size_t *sizes = malloc(sizeof(size_t) * wrld->nDims);
173                for(int i = 0; i < wrld->nDims; i++) {
174                    *(sizes + i) = *(wrld->size + i);
175                }
176                status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, wrld->nDims, sizes);
177                if(status) {
178                    err = biffGetDone(NRRD);
179                    fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
180                    free(err);
181                    nrrdNix(outNrrd);
182                    free(outData);
183                    free(sizes);
184                    exit(1);
185                }
186                free(sizes);
187            }
188            else {
189                size_t *sizes = malloc(sizeof(size_t));
190                *(sizes) = numStabilized;
191                status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, wrld->nDims, sizes);
192                if(status) {
193                    err = biffGetDone(NRRD);
194                    fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
195                    free(err);
196                    nrrdNix(outNrrd);
197                    free(outData);
198                    free(sizes);
199                    exit(1);
200                }
201                free(sizes);
202            }
203    
204            // save the data
205            status = nrrdSave(outFileName, outNrrd, NULL);
206            if(status) {
207                err = biffGetDone(NRRD);
208                fprintf(stderr, "Trouble saving nrrd struct:\n%s", err);
209                free(err);
210                nrrdNix(outNrrd);
211                free(outData);
212                exit(1);
213            }
214    
215            // cleanup output-related stuff
216            nrrdNix(outNrrd);
217            free(outData);
218        }
219    
220      Diderot_Shutdown (wrld);      Diderot_Shutdown (wrld);
221    

Legend:
Removed from v.1449  
changed lines
  Added in v.1450

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