BrookIntStreamInternal.cpp 11.8 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors: Peter Eastman, Mark Friedrichs                                    *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
32
#include "BrookIntStreamInternal.h"
33
#include "openmm/OpenMMException.h"
34
#include <sstream>
Mark Friedrichs's avatar
Mark Friedrichs committed
35
36
37

using namespace OpenMM;

38
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
39
 * BrookIntStreamInternal constructor
40
41
 * 
 * @param name                      stream name
Mark Friedrichs's avatar
Mark Friedrichs committed
42
43
44
45
 * @param size                      array size
 * @param streamWidth               stream width
 * @param type                      stream type (Integer, Integer2, ...)
 * @param dangleValue               fill value for tail of stream beyond array size
46
47
48
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
49
50
51
52
BrookIntStreamInternal::BrookIntStreamInternal( std::string name, int size, int streamWidth,
                                                BrookStreamInternal::DataType type,
                                                int dangleValue ) : 
                        BrookStreamInternal( name, size, streamWidth, type ){
53
54
55

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

Mark Friedrichs's avatar
Mark Friedrichs committed
56
   static const std::string methodName      = "BrookIntStreamInternal::BrookIntStreamInternal";
57
58
59

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

Mark Friedrichs's avatar
Mark Friedrichs committed
60
61
   _dangleValue = dangleValue;

62
63
   switch( type ){

Mark Friedrichs's avatar
Mark Friedrichs committed
64
      case BrookStreamInternal::Integer:
Mark Friedrichs's avatar
Mark Friedrichs committed
65
66

          _width = 1;
67
68
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
69
      case BrookStreamInternal::Integer2:
Mark Friedrichs's avatar
Mark Friedrichs committed
70
71

          _width = 2;
72
73
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
74
      case BrookStreamInternal::Integer3:
Mark Friedrichs's avatar
Mark Friedrichs committed
75
76

          _width = 3;
77
78
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
79
      case BrookStreamInternal::Integer4:
Mark Friedrichs's avatar
Mark Friedrichs committed
80
81

          _width = 4;
82
83
84
          break;

      default:
Mark Friedrichs's avatar
Mark Friedrichs committed
85

86
87
88
89
90
         std::stringstream message;
         message << methodName << " type=" << type << " not recognized.";
         throw OpenMMException( message.str() );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
91
   _data = new int[size*_width];
92

Mark Friedrichs's avatar
Mark Friedrichs committed
93
94
}

95
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
96
 * BrookIntStreamInternal destructor
97
98
99
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
100
BrookIntStreamInternal::~BrookIntStreamInternal() {
Mark Friedrichs's avatar
Mark Friedrichs committed
101
   delete[] _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
102
103
}

104
105
106
107
108
109
110
/** 
 * Load data from array into stream
 * 
 * @param array                     array to load (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
111
void BrookIntStreamInternal::loadFromArray( const void* array ){
112
113
114

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

Mark Friedrichs's avatar
Mark Friedrichs committed
115
   // static const std::string methodName      = "BrookIntStreamInternal::loadFromArray";
116
117
118
119
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
   return loadFromArray( array, getBaseDataType() );
}

/** 
 * Load data from array into stream
 * 
 * @param array                     array to load (length=size*width)
 *
 */

void BrookIntStreamInternal::loadFromArray( const void* array, BrookStreamInternal::DataType baseType ){

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

   static const std::string methodName      = "BrookIntStreamInternal::loadFromArray(1)";

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

   if( baseType != BrookStreamInternal::Integer ){
      std::stringstream message;
      message << methodName << " stream=" << getName() << " base type=" << getTypeString( baseType ) << " not handled -- add code.";
      throw OpenMMException( message.str() );
   }

144
   int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
145
146
147
148
   int totalSize  = getSize()*getWidth();

   for( int ii = 0; ii < totalSize; ii++ ){
      _data[ii] = arrayData[ii];
149
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
150
151
}

152
153
154
155
156
157
158
/** 
 * Save data from stream to array 
 * 
 * @param array                     array to save data to (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
159
void BrookIntStreamInternal::saveToArray( void* array ){
160
161
162

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

Mark Friedrichs's avatar
Mark Friedrichs committed
163
   // static const std::string methodName      = "BrookIntStreamInternal::saveToArray";
164
165
166
167

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

   int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
168
169
170
   int totalSize  = getSize()*getWidth();
   for( int ii = 0; ii < totalSize; ii++ ){
      arrayData[ii] = _data[ii];
171
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
172
173
}

174
175
176
177
178
179
180
/** 
 * Set all stream entries to input value
 * 
 * @param value                     value to load into stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
181
void BrookIntStreamInternal::fillWithValue( void* value ){
182
183
184

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

Mark Friedrichs's avatar
Mark Friedrichs committed
185
   // static const std::string methodName      = "BrookIntStreamInternal::fillWithValue";
186
187
188
189

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

   int valueData = *((int*) value);
Mark Friedrichs's avatar
Mark Friedrichs committed
190
   int totalSize  = getSize()*getWidth();
191

Mark Friedrichs's avatar
Mark Friedrichs committed
192
193
194
   for( int ii = 0; ii < totalSize; ii++ ){
         _data[ii] = valueData;
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
}

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/** 
 * Get array of appropritate size for loading data
 *
 * @return data array -- user's responsibility to free
 */

void* BrookIntStreamInternal::getDataArray( void ){

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

   //static const std::string methodName      = "BrookIntStreamInternal::getDataArray";

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

   int totalSize                          = getStreamSize()*getWidth();
   return new int[totalSize];
}  

215
216
217
218
219
220
221
/** 
 * Get data
 * 
 * @return data ptr
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
222
223
void* BrookIntStreamInternal::getData( void ){
   return _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
224
225
}

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/** 
 * Get data
 * 
 * @param readFromBoard if set, read values on board 
 *
 * @return data array
 *
 */

void* BrookIntStreamInternal::getData( int readFromBoard ){

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

   // static const std::string methodName      = "BrookIntStreamInternal::getData";

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

   if( readFromBoard ){
      _aStream.write( _data );
   }

   return (void*) _data;
}

/* 
 * Print array contents of object to file
 *
 * @param log         file to print to
 *
 * @return DefaultReturnValue
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
259
int BrookIntStreamInternal::_bodyPrintToFile( FILE* log, int maxPrint ){
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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

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

   //static const std::string methodName      = "BrookIntStreamInternal::_bodyPrintToFile";

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

   void* dataArrayV = getDataArray( );
   saveToArray( dataArrayV );

   int streamSize   = getStreamSize();
   int width        = getWidth();
   int index        = 0;
   int* dataArray   = (int*) dataArrayV;
   for( int ii = 0; ii < streamSize; ii++ ){
      std::stringstream message;
      message.width( 10 );
      message << ii << " [ ";
      for( int jj = 0; jj < width; jj++ ){
         message << dataArray[index++] << " ";
      }   
      message << "]\n";
      (void) fprintf( log, "%s", message.str().c_str() );    
   }   

   delete[] dataArrayV;

   return DefaultReturnValue;

}

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

const std::string BrookIntStreamInternal::getContentsString( int level ) const {

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

   // static const std::string methodName      = "BrookIntStreamInternal::getContentsString";

   static const unsigned int MAX_LINE_CHARS = 256;
   char value[MAX_LINE_CHARS];
   //static const char* Set                   = "Set";
   //static const char* NotSet                = "Not set";


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

   std::stringstream message;
   std::string tab   = "   ";

#ifdef WIN32
#define LOCAL_SPRINTF(a,b,c) sprintf_s( (a), MAX_LINE_CHARS, (b), (c) );   
#else
#define LOCAL_SPRINTF(a,b,c) sprintf( (a), (b), (c) );   
#endif

   (void) LOCAL_SPRINTF( value, "%s", getName().c_str() );
   message << _getLine( tab, "Name:", value );

   (void) LOCAL_SPRINTF( value, "%d", getWidth() );
   message << _getLine( tab, "Width:", value );

   (void) LOCAL_SPRINTF( value, "%d", getStreamSize() );
   message << _getLine( tab, "Stream size:", value );

   (void) LOCAL_SPRINTF( value, "%d", getStreamWidth() );
   message << _getLine( tab, "Stream width:", value );

   (void) LOCAL_SPRINTF( value, "%d", getStreamHeight() );
   message << _getLine( tab, "Stream height:", value );

   return message.str();
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
341
342
343
344
345
346
347
348
349
350
351
352
/** 
 * BrookFloatStreamInternal constructor
 * 
 * @param stopIndex                 index to stop sum
 * @param sum                       array of size=getWidth()
 *
 * @return DefaultReturnValue
 *
 * @throw exception if stopIndex is too large
 */

int BrookIntStreamInternal::sumByDimension( int stopIndex, double* sum ){
353

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// ---------------------------------------------------------------------------------------

   static const std::string methodName      = "BrookIntStreamInternal::sumByDimension";

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

   if( stopIndex > getSize() ){
      std::stringstream message;
      message << methodName << " stream=" << getName() << " input topIndex" << stopIndex << " is too large: stream size=" << getSize();
      throw OpenMMException( message.str() );
   }   
   
   // get _data from GPU

   _aStream.write( _data );

   int width                                 = getWidth();
   int widthM1                               = getWidth() - 1;
   stopIndex                                *= width;

   for( int ii = 0; ii < width; ii++ ){
      sum[ii] = 0.0;
   }   

   int index = 0;
   for( int ii = 0; ii < stopIndex; ii++ ){
      sum[index] += (double) _data[ii];
      if( index == widthM1 ){
         index = 0;
      } else {
         index++;
      }   
   }   

   return DefaultReturnValue;
}