BrookFloatStreamInternal.cpp 22.4 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
Mark Friedrichs committed
245
246
247
248
249
250
251
   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
252
      }
Mark Friedrichs's avatar
Mark Friedrichs committed
253
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
254

Mark Friedrichs's avatar
Mark Friedrichs committed
255
   } else if( baseType == BrookStreamInternal::Double ){
Mark Friedrichs's avatar
Mark Friedrichs committed
256
257

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

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
275
276
277
278
279
280
   }

   // set dangling values

   _loadDanglingValues();

281
   // write to GPU
Mark Friedrichs's avatar
Mark Friedrichs committed
282

283
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
284
285
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
286
287
288
289
290
291
292
293
294
295
296
297
/** 
 * 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
298
void BrookFloatStreamInternal::saveToArray( void* array ){
299
300

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

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

304
305
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
306
   // get _data from GPU
307

Mark Friedrichs's avatar
Mark Friedrichs committed
308
   _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
309

Mark Friedrichs's avatar
Mark Friedrichs committed
310
   // load into array
Mark Friedrichs's avatar
Mark Friedrichs committed
311

Mark Friedrichs's avatar
Mark Friedrichs committed
312
313
   int totalSize                             = getSize()*getWidth();
   BrookStreamInternal::DataType  baseType   = getBaseDataType();
314

Mark Friedrichs's avatar
Mark Friedrichs committed
315
   if( baseType == BrookStreamInternal::Float ){
316

317
318
//printf( "%s Basetype is float\n", methodName.c_str() );
//fflush( stdout );
Mark Friedrichs's avatar
Mark Friedrichs committed
319
320
321
322
323
324
325
326
327
      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 ){
328

329
330
331
//printf( "%s Basetype is double\n", methodName.c_str() );
//fflush( stdout );

Mark Friedrichs's avatar
Mark Friedrichs committed
332
      double* arrayData = (double*) array;
Mark Friedrichs's avatar
Mark Friedrichs committed
333
334
      for( int ii = 0; ii < totalSize; ii++ ){
         arrayData[ii] = (double) _data[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
335
      }
336

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

339
340
341
//printf( "%s Basetype is int\n", methodName.c_str() );
//fflush( stdout );

Mark Friedrichs's avatar
Mark Friedrichs committed
342
343
344
345
346
347
348
349
350
351
      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
352
353
354
   }
}

355
356
357
358
359
360
361
/** 
 * Fill stream w/ specified value
 * 
 * @param value                     value to fill stream w/
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
362
void BrookFloatStreamInternal::fillWithValue( void* value ){
Mark Friedrichs's avatar
Mark Friedrichs committed
363

364
365
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
366
   // static const std::string methodName      = "BrookFloatStreamInternal::fillWithValue";
367
368
369
370
371
   // static const int debug                   = 1;

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

   BrookOpenMMFloat valueData;
Mark Friedrichs's avatar
Mark Friedrichs committed
372
   if( _baseType == BrookStreamInternal::Float) {
373
      valueData = (BrookOpenMMFloat) *((float*) value);
Mark Friedrichs's avatar
Mark Friedrichs committed
374
   } else {
375
376
      valueData = (BrookOpenMMFloat) *((double*) value);
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
377
378
379
380
381
   //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
382
   }
383

Mark Friedrichs's avatar
Mark Friedrichs committed
384
   _loadDanglingValues();
385
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
386

Mark Friedrichs's avatar
Mark Friedrichs committed
387
388
}

Mark Friedrichs's avatar
Mark Friedrichs committed
389
390
391
392
393
394
395
396
/** 
 * Get data
 * 
 * @param readFromBoard if set, read values on board 
 *
 * @return data array
 *
 */
397

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

Mark Friedrichs's avatar
Mark Friedrichs committed
400
// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
401

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

Mark Friedrichs's avatar
Mark Friedrichs committed
404
// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
405

Mark Friedrichs's avatar
Mark Friedrichs committed
406
407
   if( readFromBoard ){
      _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
408
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
409
410

   return (void*) _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
411
412
}

413
414
415
416
417
418
419
/** 
 * Get data
 * 
 * @return data array
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
420
void* BrookFloatStreamInternal::getData( void ){
421
422
423

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

Mark Friedrichs's avatar
Mark Friedrichs committed
424
   // static const std::string methodName      = "BrookFloatStreamInternal::getData";
425
426
427

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

Mark Friedrichs's avatar
Mark Friedrichs committed
428
   return getData( 0 );
Mark Friedrichs's avatar
Mark Friedrichs committed
429
430
}

431
432
433
434
435
436
437
/** 
 * Load dangling value into stream
 * 
 * @param danglingValue   dangling value to load
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
438
void BrookFloatStreamInternal::_loadDanglingValues( float danglingValue ){
Mark Friedrichs's avatar
Mark Friedrichs committed
439

440
441
// ---------------------------------------------------------------------------------------

442
    static const std::string methodName      = "BrookFloatStreamInternal::_loadDanglingValues";
443
444
445

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

Mark Friedrichs's avatar
Mark Friedrichs committed
446
447
448
449
450
   int width      = getWidth();

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

451
452
453
//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
454
455
456
   if( arraySize < streamSize ){
      for( int ii = arraySize; ii < streamSize; ii++ ){
          _data[ii] = danglingValue;
Mark Friedrichs's avatar
Mark Friedrichs committed
457
458
459
460
       }
    }
}

461
462
463
464
465
/** 
 * Load default dangling value into stream
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
466
void BrookFloatStreamInternal::_loadDanglingValues( void ){
467
   _loadDanglingValues( _defaultDangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
468
}
Mark Friedrichs's avatar
Mark Friedrichs committed
469
470
471
472
473
474
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

/* 
 * 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();
}

523
524
525
526
527
528
529
530
531
/* 
 * Print array contents of object to file
 *
 * @param log         file to print to
 *
 * @return DefaultReturnValue
 *
 * */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
532
int BrookFloatStreamInternal::_bodyPrintToFile( FILE* log, int maxPrint ){
533
534
535
536

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

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

540
541
542

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
543
   assert( log );
544

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
545
   void* dataArrayV = getData( 1 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
546
547
548
549
550
551

#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
552
553
554
555

   int streamSize         = getStreamSize();
   int width              = getWidth();
   int index              = 0;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
556
557
   maxPrint               = maxPrint < 0 ? streamSize : maxPrint;
   maxPrint              *= width;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
558
   const float* dataArray = (float*) dataArrayV;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
559
560
   for( int ii = 0; ii < streamSize && ii < maxPrint; ii++ ){

561
      std::stringstream message;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
562
563
564
565
566
567
568
      (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;
      }   
569
      message << "]\n";
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
570
571
572
      if( index == (_size+1)*width ){
         (void) fprintf( log, "\n" );
      }
573
574
575
      (void) fprintf( log, "%s", message.str().c_str() );    
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
576
577
578
579
580
581
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
   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 ) );
         }
      }
   }
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684

   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
685
686
687
688
689
690
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
/** 
 * 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;
}