BrookIntStreamInternal.cpp 11.4 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
9
10
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
Mark Friedrichs's avatar
Mark Friedrichs committed
11
12
 * Contributors:                                                              *
 *                                                                            *
13
14
15
16
 * 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
17
 *                                                                            *
18
19
20
21
 * 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
22
 *                                                                            *
23
24
 * 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
25
26
 * -------------------------------------------------------------------------- */

Mark Friedrichs's avatar
Mark Friedrichs committed
27
#include "BrookIntStreamInternal.h"
28
#include "openmm/OpenMMException.h"
29
#include <sstream>
Mark Friedrichs's avatar
Mark Friedrichs committed
30
31
32

using namespace OpenMM;

33
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
34
 * BrookIntStreamInternal constructor
35
36
 * 
 * @param name                      stream name
Mark Friedrichs's avatar
Mark Friedrichs committed
37
38
39
40
 * @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
41
42
43
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
44
45
46
47
BrookIntStreamInternal::BrookIntStreamInternal( std::string name, int size, int streamWidth,
                                                BrookStreamInternal::DataType type,
                                                int dangleValue ) : 
                        BrookStreamInternal( name, size, streamWidth, type ){
48
49
50

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

Mark Friedrichs's avatar
Mark Friedrichs committed
51
   static const std::string methodName      = "BrookIntStreamInternal::BrookIntStreamInternal";
52
53
54

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

Mark Friedrichs's avatar
Mark Friedrichs committed
55
56
   _dangleValue = dangleValue;

57
58
   switch( type ){

Mark Friedrichs's avatar
Mark Friedrichs committed
59
      case BrookStreamInternal::Integer:
Mark Friedrichs's avatar
Mark Friedrichs committed
60
61

          _width = 1;
62
63
          break;

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

          _width = 2;
67
68
          break;

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

          _width = 3;
72
73
          break;

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

          _width = 4;
77
78
79
          break;

      default:
Mark Friedrichs's avatar
Mark Friedrichs committed
80

81
82
83
84
85
         std::stringstream message;
         message << methodName << " type=" << type << " not recognized.";
         throw OpenMMException( message.str() );
   }

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

Mark Friedrichs's avatar
Mark Friedrichs committed
88
89
}

90
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
91
 * BrookIntStreamInternal destructor
92
93
94
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
95
BrookIntStreamInternal::~BrookIntStreamInternal() {
Mark Friedrichs's avatar
Mark Friedrichs committed
96
   delete[] _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
97
98
}

99
100
101
102
103
104
105
/** 
 * Load data from array into stream
 * 
 * @param array                     array to load (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
106
void BrookIntStreamInternal::loadFromArray( const void* array ){
107
108
109

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

Mark Friedrichs's avatar
Mark Friedrichs committed
110
   // static const std::string methodName      = "BrookIntStreamInternal::loadFromArray";
111
112
113
114
   // static const int debug                   = 1;

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

Mark Friedrichs's avatar
Mark Friedrichs committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
   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() );
   }

139
   int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
140
141
142
143
   int totalSize  = getSize()*getWidth();

   for( int ii = 0; ii < totalSize; ii++ ){
      _data[ii] = arrayData[ii];
144
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
145
146
}

147
148
149
150
151
152
153
/** 
 * Save data from stream to array 
 * 
 * @param array                     array to save data to (length=size*width)
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
154
void BrookIntStreamInternal::saveToArray( void* array ){
155
156
157

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

Mark Friedrichs's avatar
Mark Friedrichs committed
158
   // static const std::string methodName      = "BrookIntStreamInternal::saveToArray";
159
160
161
162

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

   int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
163
164
165
   int totalSize  = getSize()*getWidth();
   for( int ii = 0; ii < totalSize; ii++ ){
      arrayData[ii] = _data[ii];
166
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
167
168
}

169
170
171
172
173
174
175
/** 
 * Set all stream entries to input value
 * 
 * @param value                     value to load into stream
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
176
void BrookIntStreamInternal::fillWithValue( void* value ){
177
178
179

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

Mark Friedrichs's avatar
Mark Friedrichs committed
180
   // static const std::string methodName      = "BrookIntStreamInternal::fillWithValue";
181
182
183
184

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
187
188
189
   for( int ii = 0; ii < totalSize; ii++ ){
         _data[ii] = valueData;
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
190
191
}

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/** 
 * 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];
}  

210
211
212
213
214
215
216
/** 
 * Get data
 * 
 * @return data ptr
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
217
218
void* BrookIntStreamInternal::getData( void ){
   return _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
219
220
}

221
222
223
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
/** 
 * 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
254
int BrookIntStreamInternal::_bodyPrintToFile( FILE* log, int maxPrint ){
255
256
257
258
259
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

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

   //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   = "   ";

312
#ifdef _MSC_VER
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#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
336
337
338
339
340
341
342
343
344
345
346
347
/** 
 * 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 ){
348

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
349
350
351
352
353
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
// ---------------------------------------------------------------------------------------

   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;
}