BrookFloatStreamInternal.cpp 22.6 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.                                     *
 * -------------------------------------------------------------------------- */

32
#include <sstream>
Mark Friedrichs's avatar
Mark Friedrichs committed
33
34
#include "BrookFloatStreamInternal.h"
#include "BrookPlatform.h"
35
#include "OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
36
37
38

using namespace OpenMM;

39
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
40
 * BrookFloatStreamInternal constructor
41
42
43
44
45
46
47
48
 * 
 * @param name                      stream name
 * @param size                      stream size
 * @param type                      stream type (float, float2, ...)
 * @param platform                  platform
 * @param inputStreamWidth          stream width
 * @param inputDefaultDangleValue   default dangle value
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
49
50
 * @throw exception if stream type not recognized or stream width < 1
 *
51
52
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
53
54
BrookFloatStreamInternal::BrookFloatStreamInternal( const std::string& name, int size, int streamWidth, BrookStreamInternal::DataType type,
                                                    double inputDefaultDangleValue ) : BrookStreamInternal( name, size, streamWidth, type ){
55
56
57

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

Mark Friedrichs's avatar
Mark Friedrichs committed
58
   static const std::string methodName      = "BrookFloatStreamInternal::BrookFloatStreamInternal";
59
60
61

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
62
63
64
//fprintf( stderr,"%s %s\n", methodName.c_str(), getName().c_str() );
//fflush( stderr );

65
66
67
68
   // set base type (currently only FLOAT supported)

   switch( type ){

Mark Friedrichs's avatar
Mark Friedrichs committed
69
70
71
72
      case BrookStreamInternal::Float:
      case BrookStreamInternal::Float2:
      case BrookStreamInternal::Float3:
      case BrookStreamInternal::Float4:
73
   
Mark Friedrichs's avatar
Mark Friedrichs committed
74
          _baseType = BrookStreamInternal::Float;
75
76
          break;
   
Mark Friedrichs's avatar
Mark Friedrichs committed
77
78
79
80
      case BrookStreamInternal::Double:
      case BrookStreamInternal::Double2:
      case BrookStreamInternal::Double3:
      case BrookStreamInternal::Double4:
81

Mark Friedrichs's avatar
Mark Friedrichs committed
82
          _baseType = BrookStreamInternal::Double;
83
84
85
86
87
88
89
          break;

      default:
         std::stringstream message;
         message << methodName << " stream=" << name << " input type=" << type << " not recognized.";
         throw OpenMMException( message.str() );
         break;
Mark Friedrichs's avatar
Mark Friedrichs committed
90
91
   }

92
   // set _width (FLOAT, FLOAT2, ... )
Mark Friedrichs's avatar
Mark Friedrichs committed
93

94
   switch( type ){
Mark Friedrichs's avatar
Mark Friedrichs committed
95

Mark Friedrichs's avatar
Mark Friedrichs committed
96
97
      case BrookStreamInternal::Float:
      case BrookStreamInternal::Double:
Mark Friedrichs's avatar
Mark Friedrichs committed
98

99
100
101
          _width = 1;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
102
103
      case BrookStreamInternal::Float2:
      case BrookStreamInternal::Double2:
104
105
106
107

          _width = 2;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
108
109
      case BrookStreamInternal::Float3:
      case BrookStreamInternal::Double3:
Mark Friedrichs's avatar
Mark Friedrichs committed
110

111
112
          _width = 3;
          break;
Mark Friedrichs's avatar
Mark Friedrichs committed
113

Mark Friedrichs's avatar
Mark Friedrichs committed
114
115
      case BrookStreamInternal::Float4:
      case BrookStreamInternal::Double4:
Mark Friedrichs's avatar
Mark Friedrichs committed
116

117
118
119
120
          _width = 4;
          break;
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
121
   _defaultDangleValue = (float) inputDefaultDangleValue;
122
123
124

   // set stream height based on specified stream _width

Mark Friedrichs's avatar
Mark Friedrichs committed
125
   if( streamWidth < 1 ){
126

127
      std::stringstream message;
Mark Friedrichs's avatar
Mark Friedrichs committed
128
      message << methodName << " stream=" << name << " input stream width=" << streamWidth << " is less than 1.";
129
130
131
132
      throw OpenMMException( message.str() );
   }

   // create Brook stream handle
Mark Friedrichs's avatar
Mark Friedrichs committed
133

134
   switch( _width ){
Mark Friedrichs's avatar
Mark Friedrichs committed
135

136
      case 1:
Mark Friedrichs's avatar
Mark Friedrichs committed
137

138
139
140
141
         _aStream      = brook::stream::create<float>(  _streamHeight, _streamWidth );
         break;
   
      case 2:
Mark Friedrichs's avatar
Mark Friedrichs committed
142

143
144
145
146
         _aStream      = brook::stream::create<float2>( _streamHeight, _streamWidth );
         break;
   
      case 3:
Mark Friedrichs's avatar
Mark Friedrichs committed
147

148
149
150
151
         _aStream      = brook::stream::create<float3>( _streamHeight, _streamWidth );
         break;
   
      case 4:
Mark Friedrichs's avatar
Mark Friedrichs committed
152

153
154
         _aStream      = brook::stream::create<float4>( _streamHeight, _streamWidth );
         break;
Mark Friedrichs's avatar
Mark Friedrichs committed
155
156
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
157
   int streamSize = getStreamSize();
Mark Friedrichs's avatar
Mark Friedrichs committed
158

Mark Friedrichs's avatar
Mark Friedrichs committed
159
   // allocate memory for data buffer
Mark Friedrichs's avatar
Mark Friedrichs committed
160

Mark Friedrichs's avatar
Mark Friedrichs committed
161
   _data = new float[streamSize*_width];
162

163
164
165
//printf( "%s %s data=%u stream=%d [%d %d] width=%d\n", methodName.c_str(), getName().c_str(), (unsigned int) _data, streamSize, _streamHeight, _streamWidth, _width );
//fflush( stdout );

166
167
168
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
169
 * BrookFloatStreamInternal destructor
170
171
172
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
173
BrookFloatStreamInternal::~BrookFloatStreamInternal( ){
174
175
176

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

177
   static const std::string methodName      = "BrookFloatStreamInternal::~BrookFloatStreamInternal";
178
179
180

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

181
182
183
//printf( "%s %s data=%u stream=%d [%d %d] width=%d\n", methodName.c_str(), getName().c_str(), (unsigned int) _data, getStreamSize(), _streamHeight, _streamWidth, _width );
//fflush( stdout );

184
185
   delete[] _data;

Mark Friedrichs's avatar
Mark Friedrichs committed
186
187
188
189
190
191
192
}

/** 
 * Get dangle value
 * 
 * @return  dangle value
 */
193

Mark Friedrichs's avatar
Mark Friedrichs committed
194
195
double BrookFloatStreamInternal::getDangleValue( void ) const {
   return _defaultDangleValue;
196
197
198
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
199
 * Load data from input array
200
201
202
203
204
205
 * 
 * @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.
 *
Mark Friedrichs's avatar
Mark Friedrichs committed
206
207
 * @throw exception if baseType not recognized
 *
208
209
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
210
void BrookFloatStreamInternal::loadFromArray( const void* array ){
211
212
213

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

Mark Friedrichs's avatar
Mark Friedrichs committed
214
   //static const std::string methodName      = "BrookFloatStreamInternal::loadFromArray";
215
216
217

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

Mark Friedrichs's avatar
Mark Friedrichs committed
218
   return loadFromArray( array, getBaseDataType() );
219

Mark Friedrichs's avatar
Mark Friedrichs committed
220
221
}

222
/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
223
 * Load data from input array
224
225
226
227
228
229
 * 
 * @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.
 *
230
 * @throw exception if baseType not float, double, or integer
231
232
 *
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
233

Mark Friedrichs's avatar
Mark Friedrichs committed
234
void BrookFloatStreamInternal::loadFromArray( const void* array, BrookStreamInternal::DataType baseType ){
235
236
237

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

Mark Friedrichs's avatar
Mark Friedrichs committed
238
   static const std::string methodName      = "BrookFloatStreamInternal::loadFromArray";
239
240
241

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

Mark Friedrichs's avatar
Mark Friedrichs committed
242
   int totalSize                           = getSize()*getWidth();
243
   //int totalSize                           = getSize();
244

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
245
246
247
248
249
250
/*
fprintf( stderr, "%s %s data=%p stream=%d [%d %d] width=%d type=%d\n",
          methodName.c_str(), getName().c_str(), _data, getStreamSize(), _streamHeight, _streamWidth, _width, baseType );
fflush( stderr );
*/

Mark Friedrichs's avatar
Mark Friedrichs committed
251
252
253
254
255
256
257
   if( baseType == BrookStreamInternal::Float ){

      memcpy( _data, array, sizeof( float )*totalSize );
/*
      float* arrayData = (float*) array;
      for( int ii = 0; ii < totalSize; ii++ ){
         _data[ii] = (BrookOpenMMFloat) arrayData[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
258
      }
Mark Friedrichs's avatar
Mark Friedrichs committed
259
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
260

Mark Friedrichs's avatar
Mark Friedrichs committed
261
   } else if( baseType == BrookStreamInternal::Double ){
Mark Friedrichs's avatar
Mark Friedrichs committed
262
263

      double* arrayData = (double*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
264
265
      for( int ii = 0; ii < totalSize; ii++ ){
         _data[ii] = (BrookOpenMMFloat) arrayData[ii];
266
267
      }

Mark Friedrichs's avatar
Mark Friedrichs committed
268
   } else if( baseType == BrookStreamInternal::Integer ){
269
270

      int* arrayData = (int*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
271
272
      for( int ii = 0; ii < totalSize; ii++ ){
         _data[ii] = (BrookOpenMMFloat) arrayData[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
273
      }
274
275
276
277

   } else {
      
      std::stringstream message;
Mark Friedrichs's avatar
Mark Friedrichs committed
278
      message << methodName << " stream=" << getName() << " base type=" << getTypeString( baseType ) << " not recognized.";
279
280
      throw OpenMMException( message.str() );

Mark Friedrichs's avatar
Mark Friedrichs committed
281
282
283
284
285
286
   }

   // set dangling values

   _loadDanglingValues();

287
   // write to GPU
Mark Friedrichs's avatar
Mark Friedrichs committed
288

289
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
290
291
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
292
293
294
295
296
297
298
299
300
301
302
303
/** 
 * Save data to input 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.
 *
 * @throw exception if baseType not float, double, or integer
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
304
void BrookFloatStreamInternal::saveToArray( void* array ){
305
306

// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
307

Mark Friedrichs's avatar
Mark Friedrichs committed
308
   static const std::string methodName      = "BrookFloatStreamInternal::saveToArray";
Mark Friedrichs's avatar
Mark Friedrichs committed
309

310
311
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
312
   // get _data from GPU
313

Mark Friedrichs's avatar
Mark Friedrichs committed
314
   _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
315

Mark Friedrichs's avatar
Mark Friedrichs committed
316
   // load into array
Mark Friedrichs's avatar
Mark Friedrichs committed
317

Mark Friedrichs's avatar
Mark Friedrichs committed
318
319
   int totalSize                             = getSize()*getWidth();
   BrookStreamInternal::DataType  baseType   = getBaseDataType();
320

Mark Friedrichs's avatar
Mark Friedrichs committed
321
   if( baseType == BrookStreamInternal::Float ){
322

323
324
//printf( "%s Basetype is float\n", methodName.c_str() );
//fflush( stdout );
Mark Friedrichs's avatar
Mark Friedrichs committed
325
326
327
328
329
330
331
332
333
      memcpy( array, _data, sizeof( float )*totalSize );
/*
      float* arrayData = (float*) array;
      for( int ii = 0; ii < totalSize; ii++ ){
         arrayData[ii] = (float) _data[ii];
      }
*/

   } else if( baseType == BrookStreamInternal::Double ){
334

335
336
337
//printf( "%s Basetype is double\n", methodName.c_str() );
//fflush( stdout );

Mark Friedrichs's avatar
Mark Friedrichs committed
338
      double* arrayData = (double*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
339
340
      for( int ii = 0; ii < totalSize; ii++ ){
         arrayData[ii] = (double) _data[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
341
      }
342

Mark Friedrichs's avatar
Mark Friedrichs committed
343
344
   } else if( baseType == BrookStreamInternal::Integer ){

345
346
347
//printf( "%s Basetype is int\n", methodName.c_str() );
//fflush( stdout );

Mark Friedrichs's avatar
Mark Friedrichs committed
348
349
350
351
352
353
354
355
356
357
      int* arrayData = (int*) array;
      for( int ii = 0; ii < totalSize; ii++ ){
         arrayData[ii] = (int) _data[ii];
      }
   } else {
      
      std::stringstream message;
      message << methodName << " stream=" << getName() << " base type=" << getTypeString( baseType ) << " not recognized.";
      throw OpenMMException( message.str() );

Mark Friedrichs's avatar
Mark Friedrichs committed
358
359
360
   }
}

361
362
363
364
365
366
367
/** 
 * Fill stream w/ specified value
 * 
 * @param value                     value to fill stream w/
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
368
void BrookFloatStreamInternal::fillWithValue( void* value ){
Mark Friedrichs's avatar
Mark Friedrichs committed
369

370
371
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
372
   // static const std::string methodName      = "BrookFloatStreamInternal::fillWithValue";
373
374
375
376
377
   // static const int debug                   = 1;

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

   BrookOpenMMFloat valueData;
Mark Friedrichs's avatar
Mark Friedrichs committed
378
   if( _baseType == BrookStreamInternal::Float) {
379
      valueData = (BrookOpenMMFloat) *((float*) value);
Mark Friedrichs's avatar
Mark Friedrichs committed
380
   } else {
381
382
      valueData = (BrookOpenMMFloat) *((double*) value);
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
383
384
385
386
387
   //memset( _data, valueData, sizeof( float )*getSize()*getWidth() );

   int totalSize = getSize()*getWidth();
   for( int ii = 0; ii < totalSize; ii++ ){
      _data[ii] = valueData;
Mark Friedrichs's avatar
Mark Friedrichs committed
388
   }
389

Mark Friedrichs's avatar
Mark Friedrichs committed
390
   _loadDanglingValues();
391
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
392

Mark Friedrichs's avatar
Mark Friedrichs committed
393
394
}

Mark Friedrichs's avatar
Mark Friedrichs committed
395
396
397
398
399
400
401
402
/** 
 * Get data
 * 
 * @param readFromBoard if set, read values on board 
 *
 * @return data array
 *
 */
403

Mark Friedrichs's avatar
Mark Friedrichs committed
404
void* BrookFloatStreamInternal::getData( int readFromBoard ){
Mark Friedrichs's avatar
Mark Friedrichs committed
405

Mark Friedrichs's avatar
Mark Friedrichs committed
406
// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
407

Mark Friedrichs's avatar
Mark Friedrichs committed
408
   // static const std::string methodName      = "BrookFloatStreamInternal::getData";
Mark Friedrichs's avatar
Mark Friedrichs committed
409

Mark Friedrichs's avatar
Mark Friedrichs committed
410
// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
411

Mark Friedrichs's avatar
Mark Friedrichs committed
412
413
   if( readFromBoard ){
      _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
414
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
415
416

   return (void*) _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
417
418
}

419
420
421
422
423
424
425
/** 
 * Get data
 * 
 * @return data array
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
426
void* BrookFloatStreamInternal::getData( void ){
427
428
429

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

Mark Friedrichs's avatar
Mark Friedrichs committed
430
   // static const std::string methodName      = "BrookFloatStreamInternal::getData";
431
432
433

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

Mark Friedrichs's avatar
Mark Friedrichs committed
434
   return getData( 0 );
Mark Friedrichs's avatar
Mark Friedrichs committed
435
436
}

437
438
439
440
441
442
443
/** 
 * Load dangling value into stream
 * 
 * @param danglingValue   dangling value to load
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
444
void BrookFloatStreamInternal::_loadDanglingValues( float danglingValue ){
Mark Friedrichs's avatar
Mark Friedrichs committed
445

446
447
// ---------------------------------------------------------------------------------------

448
    static const std::string methodName      = "BrookFloatStreamInternal::_loadDanglingValues";
449
450
451

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

Mark Friedrichs's avatar
Mark Friedrichs committed
452
453
454
455
456
   int width      = getWidth();

   int arraySize  = getSize()*width;
   int streamSize = getStreamSize()*width;

457
458
459
//printf( "%s array=%d stream=%d width=%d %s\n", methodName.c_str(), arraySize, streamSize, width, getName().c_str() );
//fflush( stdout );

Mark Friedrichs's avatar
Mark Friedrichs committed
460
461
462
   if( arraySize < streamSize ){
      for( int ii = arraySize; ii < streamSize; ii++ ){
          _data[ii] = danglingValue;
Mark Friedrichs's avatar
Mark Friedrichs committed
463
464
465
466
       }
    }
}

467
468
469
470
471
/** 
 * Load default dangling value into stream
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
472
void BrookFloatStreamInternal::_loadDanglingValues( void ){
473
   _loadDanglingValues( _defaultDangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
474
}
Mark Friedrichs's avatar
Mark Friedrichs committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528

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

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

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

   // static const std::string methodName      = "BrookFloatStreamInternal::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 ); 

   (void) LOCAL_SPRINTF( value, "%3e", getDangleValue() );
   message << _getLine( tab, "Dangle value:", value ); 

   return message.str();
}

529
530
531
532
533
534
535
536
537
/* 
 * Print array contents of object to file
 *
 * @param log         file to print to
 *
 * @return DefaultReturnValue
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
538
int BrookFloatStreamInternal::_bodyPrintToFile( FILE* log, int maxPrint ){
539
540
541
542

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

   //static const std::string methodName      = "BrookStreamInternal::_bodyPrintToFile";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
543
544
545
   static const unsigned int MAX_LINE_CHARS = 256;
   char value[MAX_LINE_CHARS];

546
547
548

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
549
   assert( log );
550

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
551
   void* dataArrayV = getData( 1 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
552
553
554
555
556
557

#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
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
558
559
560
561

   int streamSize         = getStreamSize();
   int width              = getWidth();
   int index              = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
562
563
   maxPrint               = maxPrint < 0 ? streamSize : maxPrint;
   maxPrint              *= width;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
564
   const float* dataArray = (float*) dataArrayV;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
565
566
   for( int ii = 0; ii < streamSize && ii < maxPrint; ii++ ){

567
      std::stringstream message;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
568
569
570
571
572
573
574
      (void) LOCAL_SPRINTF( value, "%6d ", ii );
      message << value << " [ ";

      for( unsigned int jj = 0; jj < width; jj++ ){
         (void) LOCAL_SPRINTF( value, "%16.7e ", dataArray[index++] );
         message << value;
      }   
575
      message << "]\n";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
576
577
578
      if( index == (_size+1)*width ){
         (void) fprintf( log, "\n" );
      }
579
580
581
      (void) fprintf( log, "%s", message.str().c_str() );    
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
   return DefaultReturnValue;

}

/* 
 * Get stats
 *
 * @return statistics vector
 *
 * */

int  BrookFloatStreamInternal::getStatistics( std::vector<std::vector<double> >& statistics, int maxScan ){

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

   static const std::string methodName      = "BrookStreamInternal::getStatistics";
   static const int MinIndex                = 4;
   static const int MinIndexIndex           = 5;
   static const int MaxIndex                = 6;
   static const int MaxIndexIndex           = 7;
   static const int CountIndex              = 8;
   static const int vectorSize              = CountIndex + 1;
   static const double bigValue             = 1.0e+10;

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

   void* dataArrayV = getData( 1 );

   statistics.resize( vectorSize );
   int streamSize         = getStreamSize();
   int width              = getWidth();
   int index              = 0;
   const float* dataArray = (float*) dataArrayV;
   
   for( int ii = 0; ii < vectorSize; ii++ ){
      for( int jj = 0; jj < width; jj++ ){
         if( ii == MinIndex ){
            statistics[ii].push_back( bigValue );
         } else if( ii == MaxIndex ){
            statistics[ii].push_back( -bigValue );
         } else {
            statistics[ii].push_back( 0.0 );
         }
      }
   }

   for( int ii = 0; ii < streamSize && ii < maxScan; ii++ ){
      for( int jj = 0; jj < width; jj++ ){

         double value                  =  (double) dataArray[index++];

         statistics[0][jj]            += value;
         statistics[1][jj]            += value*value;

         statistics[2][jj]            += fabs( value );

         statistics[CountIndex][jj]   += 1.0;
         if( value < statistics[MinIndex][jj] ){
            statistics[MinIndex][jj]       = value;
            statistics[MinIndexIndex][jj]  = ii;
         }

         if( value > statistics[MaxIndex][jj] ){
            statistics[MaxIndex][jj]       = value;
            statistics[MaxIndexIndex][jj]  = ii;
         }
      }
   }


   for( int jj = 0; jj < width; jj++ ){
      if( statistics[CountIndex][jj] > 0.0 ){
         statistics[3][jj]   = statistics[0][jj];
         statistics[0][jj]  /= statistics[CountIndex][jj];
         statistics[2][jj]  /= statistics[CountIndex][jj];
         statistics[1][jj]   = statistics[1][jj] - statistics[0][jj]*statistics[0][jj]*statistics[CountIndex][jj];
         if( statistics[CountIndex][jj] > 1.0 ){
            statistics[1][jj] = sqrt( statistics[1][jj]/( statistics[CountIndex][jj] - 1.0 ) );
         }
      }
   }
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690

   return DefaultReturnValue;

}

/** 
 * Get array of appropritate size for loading data
 *
 * @return data array -- user's responsibility to free
 */

void* BrookFloatStreamInternal::getDataArray( void ){

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

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

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

   int totalSize                          = getStreamSize()*getWidth();
   BrookStreamInternal::DataType baseType = getBaseDataType();
   
   if( baseType == Double || baseType == Double2 ||  baseType == Double3 ||  baseType == Double4 ){
      totalSize *= 2;
   }
   return new float[totalSize];
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/** 
 * BrookFloatStreamInternal constructor
 * 
 * @param stopIndex                 index to stop sum
 * @param sum                       array of size=getWidth()
 *
 * @return DefaultReturnValue
 *
 * @throw exception if stopIndex is too large
 */

int BrookFloatStreamInternal::sumByDimension( int stopIndex, double* sum ){

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

   static const std::string methodName      = "BrookFloatStreamInternal::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;
}