BrookStreamInternal.h 10.4 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_BROOK_STREAM_INTERNAL_H_
#define OPENMM_BROOK_STREAM_INTERNAL_H_

/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
12
13
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
Mark Friedrichs's avatar
Mark Friedrichs committed
14
15
 * Contributors:                                                              *
 *                                                                            *
16
17
18
19
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
20
 *                                                                            *
21
22
23
24
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
Mark Friedrichs's avatar
Mark Friedrichs committed
25
 *                                                                            *
26
27
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
Mark Friedrichs's avatar
Mark Friedrichs committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 * -------------------------------------------------------------------------- */

#include <brook/brook.hpp>

namespace OpenMM {

/**
 * This is the implementation of Float and Double streams in the Brook Platform.
 */

class BrookStreamInternal {

   public:

      /** 
        * This is an enumeration of the allowed data types for a Stream.
        */
      enum DataType { Float, Float2, Float3, Float4, Double, Double2, Double3, Double4, Integer, Integer2, Integer3, Integer4, Unknown };

47
48
49
50
51
52
53
      // return values

      static const int DefaultReturnValue = 0;
      static const int ErrorReturnValue   = -1; 

      // ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      /**
       * BrookStreamInternal constructor
       * 
       * @param name        name of the stream to create
       * @param size        number of elements in the stream
       * @param streamWidth stream width
       * @param type        data type of each element in the stream
       *
       */
  
      BrookStreamInternal( const std::string& name, int size, int streamWidth, BrookStreamInternal::DataType type );
  
      /**
       * BrookStreamInternal destructor
       * 
       */

      ~BrookStreamInternal( );
  
      /**
       * Get the name of this stream.
       */
  
      const std::string& getName( void ) const;
  
      /**
       * Get the number of elements in this stream.
       */
  
      int getSize( void ) const;
  
      /**
       * Get the data type of each element in the stream.
       */
  
      BrookStreamInternal::DataType getDataType( void ) const;
  
      /**
       * Get base data type of each element in the stream ( float, double, int )
       */
  
      BrookStreamInternal::DataType getBaseDataType( void ) const;
  
      /**
       * Copy the contents of an array into this stream.
       * 
       * @param  array a pointer to the start of the array.  The array is assumed to have the same length as this stream,
       * and to contain elements of the correct data type for this stream.  If the stream has a compound data type, all
       * the values should be packed into a single array: all the values for the first element, followed by all the values
       * for the next element, etc.
       */
105
      virtual void loadFromArray( const void* array ) = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  
      /**
       * Copy the contents of an array into this stream.
       * 
       * @param  array a pointer to the start of the array.  The array is assumed to have the same length as this stream,
       * and to contain elements of the correct data type for this stream.  If the stream has a compound data type, all
       * the values should be packed into a single array: all the values for the first element, followed by all the values
       * for the next element, etc.
       */
      virtual void loadFromArray( const void* array, BrookStreamInternal::DataType baseType ) = 0;
  
      /**
       * Copy the contents of this stream into an array.
       * 
       * @param  array a pointer to the start of the array.  The array is assumed to have the same length as this stream,
       * and to contain elements of the correct data type for this stream.  If the stream has a compound data type, all
       * the values should be packed into a single array: all the values for the first element, followed by all the values
       * for the next element, etc.
       */
125
      virtual void saveToArray( void* array ) = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
126
127
128
129
130
131
  
      /**
       * Set every element of this stream to the same value.
       * 
       * @param a pointer to the value.  It is assumed to be of the correct data type for this stream.
       */
132
      virtual void fillWithValue( void* value ) = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
133
  
Mark Friedrichs's avatar
Mark Friedrichs committed
134
135
136
137
138
139
140
      /**
       * Get data
       * 
       * @return data array
       */
      virtual void* getData( void ) = 0;
  
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
      /**
       * Get data
       *
       * @param readFromBoard  read data from board
       * 
       * @return data array
       */
      virtual void* getData( int readFromBoard ) = 0;
  
      /**
       * Get array of appropritate size for loading data
       *
       * @return data array -- user's responsibility to free
       */
      virtual void* getDataArray( void ) = 0;
  
Mark Friedrichs's avatar
Mark Friedrichs committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
      /** 
       * Get type string
       *
       * @param type        BrookStreamInternal data type (float, float2, ...)
       *
       * @return string matching type or "Unknown"
       * 
       */
      
      std::string getTypeString( BrookStreamInternal::DataType type ) const;
      
      /** 
       * Get Brook stream reference
       * 
       * @return  Brook stream reference
       */
      
      brook::stream& getBrookStream( void );

      /** 
       * Get width
       * 
       * @return width
       */

      int getWidth( void ) const;
    
      /** 
       * Get stream width
       * 
       * @return stream width
       */

      int getStreamWidth( void ) const;

      /** 
       * Get stream height
       * 
       * @return stream height
       */

      int getStreamHeight( void ) const;

      /** 
       * Get stream size
       * 
       * @return stream size
       */

      int getStreamSize( void ) const;

      /* 
       * Get contents of object
       *
       *
       * @param level   level of dump
       *
       * @return string containing contents
       *
       * */

      const std::string getContentsString( int level = 0 ) const;

220
221
222
      /* 
       * Print to file
       *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
223
224
       * @param log         log file
       * @param maxPrint    max values to print; if < 0, then all values printed; default value is -1
225
226
227
228
229
       *
       * @return  DefaultReturnValue
       *
       * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
230
      int printToFile( FILE* log, int maxPrint = -1 );
231

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
232
233
234
235
236
237
238
239
240
241
242
243
      /** 
       * Sum over stream dimensions
       * 
       * @param stopIndex                 index to stop sum
       * @param sum                       array of size=getWidth()
       *
       * @return DefaultReturnValue
       *
       * @throw exception if stopIndex is too large
       */
      
      int sumByDimension( int stopIndex, double* sum );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
244
245
246
247
  
      /*  
       * Get stats
       *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
248
249
250
       * @param statistics  output vector of stats
       * @param maxScan     number of points to use in computing stats
       *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
251
252
253
254
       * @return statistics vector
       *
       * */
     
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
255
      virtual int getStatistics( std::vector<std::vector<double>>& statVector, int maxScan );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
256
257
258
259
260
261
262
263
264

      /* 
       * Get stat string
       *
       * @param         tag    id tag
       * @param  statistics    stat vector
       *
       * @return stat string
       *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
265
       **/
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
266
267
      
      std::string printStatistics( std::string tag, std::vector<std::vector<double> >& statistics ) const;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
268
      
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
      /* 
       * Read stream from file
       *
       * @param fileName     file name
       *
       * @return DefaultReturnValue or ErrorReturnValue if problems
       *
       **/
      
      int loadStreamGivenFileName( std::string& filename );
      
      /* 
       * Print streams to file
       *
       * @param fileName     file name
       * @param streams      streams to print
       *
       * @return DefaultReturnValue
       *
       **/
       
      static int printStreamsToFile( std::string fileName, std::vector<BrookStreamInternal*>& streams );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

      /* 
       * Check for NANs
       *
       * @return number of Nans found
       *
       **/
      
      int checkForNans( void );
      
      /* 
       * Sum columns
       *
       * @param  sums   output vector of column sums
       *
       * @return DefaultReturnValue
       *
       **/
      
      int sumColumns( std::vector<float>& sums );
      
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
312
      
Mark Friedrichs's avatar
Mark Friedrichs committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
   protected:

      std::string _name;

      BrookStreamInternal::DataType _type;
      BrookStreamInternal::DataType _baseType;

      int _size;
      int _width;
      int _streamWidth;
      int _streamHeight;
      int _streamSize;
  
      brook::stream _aStream;
  
      /* 
       * Get contents of object
       *
       * @param tab         tab
       * @param description description
       * @param value       value
       *
       * @return string containing contents
       *
       * */
      
      std::string _getLine( const std::string& tab, const std::string& description, 
                            const std::string& value ) const;
      
342
343
344
345
346
347
348
349
350
      /* 
       * Print array to file
       *
       * @param log  log file
       *
       * @return  DefaultReturnValue
       *
       * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
351
      virtual int _bodyPrintToFile( FILE* log,  int maxPrint ) = 0;
Mark Friedrichs's avatar
Mark Friedrichs committed
352
353
354
355
356
};

} // namespace OpenMM

#endif /* OPENMM_BROOK_STREAM_INTERNAL_H_ */