SCM Repository
Annotation of /branches/cuda/src/lib/common/world.cxx
Parent Directory
|
Revision Log
Revision 5626 - (view) (download) (as text)
1 : | jhr | 5597 | /*! \file world.cxx |
2 : | * | ||
3 : | * \author John Reppy | ||
4 : | */ | ||
5 : | |||
6 : | /* | ||
7 : | * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu) | ||
8 : | * | ||
9 : | * COPYRIGHT (c) 2016 The University of Chicago | ||
10 : | * All rights reserved. | ||
11 : | */ | ||
12 : | |||
13 : | #include <cstdlib> | ||
14 : | adrianlehm | 5619 | #include "diderot/base.h" |
15 : | #include "diderot/world.h" | ||
16 : | jhr | 5597 | |
17 : | #ifdef DIDEROT_TARGET_PARALLEL | ||
18 : | adrianlehm | 5619 | #include "diderot/parallel.h" |
19 : | jhr | 5597 | #endif |
20 : | |||
21 : | namespace diderot { | ||
22 : | |||
23 : | world_base::world_base (std::string const &name, bool isArr, int nAxes) | ||
24 : | : _name(name), _errors(biffMsgNew("DIDEROT")), _stage(POST_NEW), | ||
25 : | _verbose(false), _is_array(isArr), _naxes(nAxes), | ||
26 : | _base(new uint32_t[nAxes]), _size(new uint32_t[nAxes]), | ||
27 : | _printTo(nullptr), _run_time(0) | ||
28 : | { | ||
29 : | #ifdef DIDEROT_TARGET_PARALLEL | ||
30 : | this->_sched = new diderot::scheduler(); | ||
31 : | #endif | ||
32 : | #ifdef DIDEROT_ENABLE_LOGGING | ||
33 : | this->_log_file = nullptr; | ||
34 : | #endif | ||
35 : | } | ||
36 : | |||
37 : | world_base::~world_base () | ||
38 : | { | ||
39 : | biffMsgNix (this->_errors); | ||
40 : | delete[] this->_base; | ||
41 : | delete[] this->_size; | ||
42 : | if (this->_printTo != nullptr) { | ||
43 : | this->_printTo->close(); | ||
44 : | delete this->_printTo; | ||
45 : | } | ||
46 : | #ifdef DIDEROT_TARGET_PARALLEL | ||
47 : | delete this->_sched; | ||
48 : | #endif | ||
49 : | #ifdef DIDEROT_ENABLE_LOGGING | ||
50 : | if (this->_log_file != nullptr) delete this->_log_file; | ||
51 : | #endif | ||
52 : | } | ||
53 : | |||
54 : | //! Check that a nrrd has the expected structure for loading into a dynamic sequence | ||
55 : | //! \param nin the nrrd to check | ||
56 : | //! \param nValuesPerElem the expected number of values per element | ||
57 : | //! \return true on error, false otherwise | ||
58 : | bool world_base::check_seq_nrrd (const Nrrd *nin, uint32_t nValuesPerElem) | ||
59 : | { | ||
60 : | // check the structure of the nrrd file | ||
61 : | if (nin->spaceDim != 0) { | ||
62 : | this->error ("expected nrrd-file space dimension of 0, but found %d\n", | ||
63 : | nin->spaceDim); | ||
64 : | return true; | ||
65 : | } | ||
66 : | |||
67 : | if (nValuesPerElem == 1) { | ||
68 : | // check sequence of scalars | ||
69 : | if (nin->dim != 1) { | ||
70 : | this->error ("expected nrrd dimension of 1, but found %d\n", nin->dim); | ||
71 : | return true; | ||
72 : | } | ||
73 : | } | ||
74 : | else { | ||
75 : | // check sequence of aggregates | ||
76 : | if (nin->dim != 2) { | ||
77 : | this->error ("expected nrrd dimension of 2, but found %d\n", nin->dim); | ||
78 : | return true; | ||
79 : | } | ||
80 : | else if (nin->axis[0].size != nValuesPerElem) { | ||
81 : | this->error ("expected %d values per sequence element, but found %d\n", | ||
82 : | nValuesPerElem, nin->axis[0].size); | ||
83 : | return true; | ||
84 : | } | ||
85 : | } | ||
86 : | |||
87 : | if (__details::nrrd_type_info[nin->type].sizeb == 0) { | ||
88 : | this->error ("bogus element type %d in nrrd\n", nin->type); | ||
89 : | return true; | ||
90 : | } | ||
91 : | |||
92 : | return false; | ||
93 : | } | ||
94 : | |||
95 : | // load a nrrd from a file | ||
96 : | Nrrd *world_base::load_nrrd_file (std::string const &filename) | ||
97 : | { | ||
98 : | /* create a nrrd; at this point it is just an empty container */ | ||
99 : | Nrrd *nin = nrrdNew(); | ||
100 : | |||
101 : | /* read in the nrrd from the file */ | ||
102 : | if (nrrdLoad(nin, filename.c_str(), nullptr) != 0) { | ||
103 : | char *msg = biffGetDone(NRRD); | ||
104 : | biffMsgAdd (this->_errors, msg); | ||
105 : | std::free (msg); | ||
106 : | return nullptr; | ||
107 : | } | ||
108 : | |||
109 : | return nin; | ||
110 : | |||
111 : | } | ||
112 : | |||
113 : | bool world_base::normalize_nrrd_meta_data (Nrrd *nin) | ||
114 : | { | ||
115 : | #ifdef HAVE_NRRDMETADATANORMALIZE | ||
116 : | int lostMeasureFrame = 0; | ||
117 : | int sts = nrrdMetaDataNormalize ( | ||
118 : | nin, nin, nrrdMetaDataCanonicalVersionAlpha, | ||
119 : | AIR_FALSE, AIR_FALSE, AIR_FALSE, 1.0, &lostMeasureFrame); | ||
120 : | if (sts != 0) { | ||
121 : | // propagate the error message from nrrdMetaDataNormalize | ||
122 : | char *msg = biffGetDone(NRRD); | ||
123 : | biffMsgAdd (this->_errors, msg); | ||
124 : | nrrdNuke (nin); | ||
125 : | return true; | ||
126 : | } | ||
127 : | else if (lostMeasureFrame != 0) { | ||
128 : | // FIXME: we should figure out a mechanism to pass warnings back up the call chain. | ||
129 : | std::cerr << "WARNING: input data measurement frame lost" << std::endl; | ||
130 : | } | ||
131 : | #else | ||
132 : | # error must have nrrdMetaDataNormalize | ||
133 : | #endif | ||
134 : | |||
135 : | return false; | ||
136 : | } | ||
137 : | |||
138 : | void world_base::error (const char *fmt, ...) | ||
139 : | { | ||
140 : | char buf[1024]; | ||
141 : | va_list ap; | ||
142 : | |||
143 : | // NOTE: if there was a biffMsg function that worked with va_lists, then we | ||
144 : | // could avoid the buffer | ||
145 : | va_start (ap, fmt); | ||
146 : | vsnprintf (buf, sizeof(buf), fmt, ap); | ||
147 : | va_end (ap); | ||
148 : | |||
149 : | biffMsgAdd (this->_errors, buf); | ||
150 : | } | ||
151 : | |||
152 : | } // namespace Diderot |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |