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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1519 - (view) (download) (as text)

1 : jhr 1481 /*! \file output.c
2 :     *
3 :     * \author Nick Seltzer
4 :     */
5 : nseltzer 1479
6 : jhr 1481 /*
7 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     * All rights reserved.
9 :     */
10 :    
11 : nseltzer 1479 #include <stdio.h>
12 :     #include "Diderot/diderot.h"
13 :     #include <teem/nrrd.h>
14 :     #include <teem/air.h>
15 :    
16 : jhr 1519 // FIXME: to make this compatible with OpenCL (and CUDA), we should be copying the
17 :     // output state to the output array on the GPU and then just uploading the output
18 :     // array to the CPU. This will require changing the interface of these functions.
19 :    
20 : jhr 1481 /*! \brief Print the strand output state to a text file.
21 :     */
22 : jhr 1519 void Diderot_Print (Diderot_World_t *wrld)
23 : jhr 1481 {
24 : jhr 1519 WorldPrefix_t *hdr = (WorldPrefix_t *)wrld;
25 : jhr 1481 airArray *mop = airMopNew();
26 :    
27 :     // here we have the final state of all of the strands in the "in" buffer
28 : jhr 1519 int outFileNameLen = strlen(hdr->name) + 5;
29 : jhr 1481 char *outFileName = (char *)CheckedAlloc(outFileNameLen);
30 :     airMopAdd(mop, outFileName, airFree, airMopAlways);
31 : jhr 1519 snprintf (outFileName, outFileNameLen, "%s.txt", hdr->name);
32 : jhr 1481 FILE *outS = fopen(outFileName, "w");
33 :     if (outS == NULL) {
34 :     fprintf (stderr, "Cannot open output file %s\n", outFileName);
35 :     airMopError(mop);
36 :     exit (8);
37 :     }
38 :     airMopAdd(mop, outS, (airMopper)airFclose, airMopAlways);
39 :    
40 : jhr 1519 // print the strand output states
41 :     for (int i = 0; i < hdr->numStrands; i++) {
42 :     if (hdr->status[i] == DIDEROT_STABLE) {
43 :     Diderot_Strands[0]->print (outS, Diderot_InState(wrld, i));
44 : lamonts 1513 }
45 : jhr 1481 }
46 :    
47 :     airMopOkay(mop);
48 :     }
49 :    
50 : jhr 1519 /*! \brief Output the strand output state to a nrrd file.
51 : jhr 1480 */
52 : jhr 1519 void Diderot_Output (Diderot_World_t *wrld, size_t outStateSzb)
53 : nseltzer 1479 {
54 : jhr 1519 WorldPrefix_t *hdr = (WorldPrefix_t *)wrld;
55 : nseltzer 1479 airArray *mop = airMopNew();
56 : jhr 1481 int status;
57 :     char *err;
58 : nseltzer 1479
59 : jhr 1481 // get correct output file name
60 : jhr 1519 int outFileNameLen = strlen(hdr->name) + 6;
61 : jhr 1481 char *outFileName = (char *)CheckedAlloc(outFileNameLen);
62 :     airMopAdd(mop, outFileName, airFree, airMopAlways);
63 : jhr 1519 snprintf (outFileName, outFileNameLen, "%s.nrrd", hdr->name);
64 : lamonts 1513 fprintf(stderr, "%d", 1);
65 : jhr 1519
66 :     // calculate the amount of space needed for the output array
67 :     int numStabilized;
68 :     if (hdr->isArray) {
69 :     numStabilized = hdr->numStrands;
70 : jhr 1481 }
71 : jhr 1519 else {
72 :     numStabilized = 0;
73 :     for (int i = 0; i < hdr->numStrands; i++) {
74 :     if (hdr->status[i] == DIDEROT_STABLE)
75 :     numStabilized++;
76 :     }
77 :     }
78 :    
79 :     // allocate the output object
80 :     void *outData = CheckedAlloc(numStabilized * outStateSzb);
81 : jhr 1481 airMopAdd(mop, outData, airFree, airMopAlways);
82 : lamonts 1513 fprintf(stderr, "%d", 2);
83 : nseltzer 1479
84 : jhr 1481 // copy data into outData
85 :     void *outPos = outData;
86 : jhr 1519 for (int i = 0; i < hdr->numStrands; i++) {
87 :     if (hdr->status[i] == DIDEROT_STABLE) {
88 :     Diderot_Strands[0]->output (outPos, Diderot_InState(wrld, i));
89 :     outPos += outStateSzb;
90 : nseltzer 1479 }
91 : jhr 1481 }
92 : nseltzer 1479
93 : jhr 1481 // make a new nrrd
94 :     Nrrd *outNrrd;
95 :     outNrrd = nrrdNew();
96 :     if (outNrrd == NULL) {
97 :     fprintf(stderr, "Trouble allocating nrrd struct:\n%s", err);
98 :     airMopError(mop);
99 :     exit(1);
100 :     }
101 :     airMopAdd(mop, outNrrd, (airMopper)nrrdNix, airMopAlways);
102 : lamonts 1513 fprintf(stderr, "%d", 4);
103 : nseltzer 1479
104 : jhr 1481 // wrap the data (outData) with the nrrd (outNrrd)
105 : jhr 1519 if (hdr->isArray) {
106 :     size_t *sizes = NEWVEC(size_t, hdr->nDims + 1);
107 : jhr 1481 airMopAdd(mop, sizes, airFree, airMopAlways);
108 :     switch(Diderot_Strands[0]->nrrdType) {
109 : jhr 1483 case nrrdTypeInt:
110 :     case nrrdTypeFloat:
111 : jhr 1519 sizes[0] = outStateSzb >> 2;
112 : jhr 1481 break;
113 : jhr 1483 case nrrdTypeLLong:
114 :     case nrrdTypeDouble:
115 : jhr 1519 sizes[0] = outStateSzb >> 3;
116 : jhr 1481 break;
117 :     default:
118 :     fprintf(stderr, "Bad nrrdType\n");
119 : nseltzer 1479 airMopError(mop);
120 :     exit(1);
121 : jhr 1481 break;
122 : nseltzer 1479 }
123 : jhr 1519 for (int i = 1; i <= hdr->nDims; i++) {
124 :     sizes[i] = hdr->size[hdr->nDims - i];
125 : jhr 1481 }
126 : jhr 1519 status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, hdr->nDims + 1, sizes);
127 : jhr 1481 if (status) {
128 :     err = biffGetDone(NRRD);
129 :     fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
130 :     airMopError(mop);
131 :     free(err);
132 :     exit(1);
133 :     }
134 :     }
135 :     else {
136 :     size_t *sizes = NEWVEC(size_t, 2);
137 :     airMopAdd(mop, sizes, airFree, airMopAlways);
138 :     switch(Diderot_Strands[0]->nrrdType) {
139 :     case nrrdTypeInt:
140 :     case nrrdTypeFloat:
141 : jhr 1519 sizes[0] = outStateSzb >> 2;
142 : jhr 1481 break;
143 :     case nrrdTypeLLong:
144 :     case nrrdTypeDouble:
145 : jhr 1519 sizes[0] = outStateSzb >> 3;
146 : jhr 1481 break;
147 :     default:
148 :     fprintf(stderr, "Bad nrrdType\n");
149 : nseltzer 1479 airMopError(mop);
150 :     exit(1);
151 : jhr 1481 break;
152 : nseltzer 1479 }
153 : jhr 1481 sizes[1] = numStabilized;
154 : jhr 1519 status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, hdr->nDims, sizes);
155 : jhr 1481 if (status != 0) {
156 :     err = biffGetDone(NRRD);
157 :     fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
158 :     airMopError(mop);
159 :     free(err);
160 :     exit(1);
161 :     }
162 : jhr 1482 }
163 : lamonts 1513 fprintf(stderr, "%d", 5);
164 : nseltzer 1479
165 : jhr 1482 // save the data
166 :     status = nrrdSave(outFileName, outNrrd, NULL);
167 :     if(status) {
168 :     err = biffGetDone(NRRD);
169 :     fprintf(stderr, "Trouble saving nrrd struct:\n%s", err);
170 :     airMopError(mop);
171 :     free(err);
172 :     exit(1);
173 : nseltzer 1479 }
174 : lamonts 1513 fprintf(stderr, "%d", 6);
175 : nseltzer 1479
176 :     airMopOkay(mop);
177 : jhr 1481
178 : nseltzer 1479 }

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