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 1526 - (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 : lamonts 1526 Diderot_Strands[0]->print (outS, Diderot_OutState(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 :    
65 :     // calculate the amount of space needed for the output array
66 :     int numStabilized;
67 :     if (hdr->isArray) {
68 :     numStabilized = hdr->numStrands;
69 : jhr 1481 }
70 : jhr 1519 else {
71 :     numStabilized = 0;
72 :     for (int i = 0; i < hdr->numStrands; i++) {
73 :     if (hdr->status[i] == DIDEROT_STABLE)
74 :     numStabilized++;
75 :     }
76 :     }
77 :    
78 :     // allocate the output object
79 :     void *outData = CheckedAlloc(numStabilized * outStateSzb);
80 : jhr 1481 airMopAdd(mop, outData, airFree, airMopAlways);
81 : nseltzer 1479
82 : jhr 1481 // copy data into outData
83 :     void *outPos = outData;
84 : jhr 1519 for (int i = 0; i < hdr->numStrands; i++) {
85 :     if (hdr->status[i] == DIDEROT_STABLE) {
86 : lamonts 1526 Diderot_Strands[0]->output (outPos, Diderot_OutState(wrld, i));
87 : jhr 1519 outPos += outStateSzb;
88 : nseltzer 1479 }
89 : jhr 1481 }
90 : nseltzer 1479
91 : jhr 1481 // make a new nrrd
92 :     Nrrd *outNrrd;
93 :     outNrrd = nrrdNew();
94 :     if (outNrrd == NULL) {
95 :     fprintf(stderr, "Trouble allocating nrrd struct:\n%s", err);
96 :     airMopError(mop);
97 :     exit(1);
98 :     }
99 :     airMopAdd(mop, outNrrd, (airMopper)nrrdNix, airMopAlways);
100 : nseltzer 1479
101 : jhr 1481 // wrap the data (outData) with the nrrd (outNrrd)
102 : jhr 1519 if (hdr->isArray) {
103 :     size_t *sizes = NEWVEC(size_t, hdr->nDims + 1);
104 : jhr 1481 airMopAdd(mop, sizes, airFree, airMopAlways);
105 :     switch(Diderot_Strands[0]->nrrdType) {
106 : jhr 1483 case nrrdTypeInt:
107 :     case nrrdTypeFloat:
108 : jhr 1519 sizes[0] = outStateSzb >> 2;
109 : jhr 1481 break;
110 : jhr 1483 case nrrdTypeLLong:
111 :     case nrrdTypeDouble:
112 : jhr 1519 sizes[0] = outStateSzb >> 3;
113 : jhr 1481 break;
114 :     default:
115 :     fprintf(stderr, "Bad nrrdType\n");
116 : nseltzer 1479 airMopError(mop);
117 :     exit(1);
118 : jhr 1481 break;
119 : nseltzer 1479 }
120 : jhr 1519 for (int i = 1; i <= hdr->nDims; i++) {
121 :     sizes[i] = hdr->size[hdr->nDims - i];
122 : jhr 1481 }
123 : jhr 1519 status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, hdr->nDims + 1, sizes);
124 : jhr 1481 if (status) {
125 :     err = biffGetDone(NRRD);
126 :     fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
127 :     airMopError(mop);
128 :     free(err);
129 :     exit(1);
130 :     }
131 :     }
132 :     else {
133 :     size_t *sizes = NEWVEC(size_t, 2);
134 :     airMopAdd(mop, sizes, airFree, airMopAlways);
135 :     switch(Diderot_Strands[0]->nrrdType) {
136 :     case nrrdTypeInt:
137 :     case nrrdTypeFloat:
138 : jhr 1519 sizes[0] = outStateSzb >> 2;
139 : jhr 1481 break;
140 :     case nrrdTypeLLong:
141 :     case nrrdTypeDouble:
142 : jhr 1519 sizes[0] = outStateSzb >> 3;
143 : jhr 1481 break;
144 :     default:
145 :     fprintf(stderr, "Bad nrrdType\n");
146 : nseltzer 1479 airMopError(mop);
147 :     exit(1);
148 : jhr 1481 break;
149 : nseltzer 1479 }
150 : jhr 1481 sizes[1] = numStabilized;
151 : jhr 1519 status = nrrdWrap_nva(outNrrd, outData, Diderot_Strands[0]->nrrdType, hdr->nDims, sizes);
152 : jhr 1481 if (status != 0) {
153 :     err = biffGetDone(NRRD);
154 :     fprintf(stderr, "Trouble wrapping nrrd struct:\n%s", err);
155 :     airMopError(mop);
156 :     free(err);
157 :     exit(1);
158 :     }
159 : jhr 1482 }
160 : nseltzer 1479
161 : jhr 1482 // save the data
162 :     status = nrrdSave(outFileName, outNrrd, NULL);
163 :     if(status) {
164 :     err = biffGetDone(NRRD);
165 :     fprintf(stderr, "Trouble saving nrrd struct:\n%s", err);
166 :     airMopError(mop);
167 :     free(err);
168 :     exit(1);
169 : nseltzer 1479 }
170 :    
171 :     airMopOkay(mop);
172 : jhr 1481
173 : nseltzer 1479 }

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