BrookFloatStreamInternal.cpp 19.3 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
62
63
64
65

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

   // set base type (currently only FLOAT supported)

   switch( type ){

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

Mark Friedrichs's avatar
Mark Friedrichs committed
79
          _baseType = BrookStreamInternal::Double;
80
81
82
83
84
85
86
          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
87
88
   }

89
   // set _width (FLOAT, FLOAT2, ... )
Mark Friedrichs's avatar
Mark Friedrichs committed
90

91
   switch( type ){
Mark Friedrichs's avatar
Mark Friedrichs committed
92

Mark Friedrichs's avatar
Mark Friedrichs committed
93
94
      case BrookStreamInternal::Float:
      case BrookStreamInternal::Double:
Mark Friedrichs's avatar
Mark Friedrichs committed
95

96
97
98
          _width = 1;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
99
100
      case BrookStreamInternal::Float2:
      case BrookStreamInternal::Double2:
101
102
103
104

          _width = 2;
          break;

Mark Friedrichs's avatar
Mark Friedrichs committed
105
106
      case BrookStreamInternal::Float3:
      case BrookStreamInternal::Double3:
Mark Friedrichs's avatar
Mark Friedrichs committed
107

108
109
          _width = 3;
          break;
Mark Friedrichs's avatar
Mark Friedrichs committed
110

Mark Friedrichs's avatar
Mark Friedrichs committed
111
112
      case BrookStreamInternal::Float4:
      case BrookStreamInternal::Double4:
Mark Friedrichs's avatar
Mark Friedrichs committed
113

114
115
116
117
          _width = 4;
          break;
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
118
   _defaultDangleValue = (float) inputDefaultDangleValue;
119
120
121

   // set stream height based on specified stream _width

Mark Friedrichs's avatar
Mark Friedrichs committed
122
   if( streamWidth < 1 ){
123

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

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

131
   switch( _width ){
Mark Friedrichs's avatar
Mark Friedrichs committed
132

133
      case 1:
Mark Friedrichs's avatar
Mark Friedrichs committed
134

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

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
154
   int streamSize = getStreamSize();
Mark Friedrichs's avatar
Mark Friedrichs committed
155

Mark Friedrichs's avatar
Mark Friedrichs committed
156
   // allocate memory for data buffer
Mark Friedrichs's avatar
Mark Friedrichs committed
157

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

160
161
162
//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 );

163
164
165
}

/** 
Mark Friedrichs's avatar
Mark Friedrichs committed
166
 * BrookFloatStreamInternal destructor
167
168
169
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
170
BrookFloatStreamInternal::~BrookFloatStreamInternal( ){
171
172
173

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

174
   static const std::string methodName      = "BrookFloatStreamInternal::~BrookFloatStreamInternal";
175
176
177

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

178
179
180
//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 );

181
182
   delete[] _data;

Mark Friedrichs's avatar
Mark Friedrichs committed
183
184
185
186
187
188
189
}

/** 
 * Get dangle value
 * 
 * @return  dangle value
 */
190

Mark Friedrichs's avatar
Mark Friedrichs committed
191
192
double BrookFloatStreamInternal::getDangleValue( void ) const {
   return _defaultDangleValue;
193
194
195
}

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

Mark Friedrichs's avatar
Mark Friedrichs committed
207
void BrookFloatStreamInternal::loadFromArray( const void* array ){
208
209
210

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

Mark Friedrichs's avatar
Mark Friedrichs committed
211
   //static const std::string methodName      = "BrookFloatStreamInternal::loadFromArray";
212
213
214

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

Mark Friedrichs's avatar
Mark Friedrichs committed
215
   return loadFromArray( array, getBaseDataType() );
216

Mark Friedrichs's avatar
Mark Friedrichs committed
217
218
}

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

Mark Friedrichs's avatar
Mark Friedrichs committed
231
void BrookFloatStreamInternal::loadFromArray( const void* array, BrookStreamInternal::DataType baseType ){
232
233
234

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

Mark Friedrichs's avatar
Mark Friedrichs committed
235
   static const std::string methodName      = "BrookFloatStreamInternal::loadFromArray";
236
237
238

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

Mark Friedrichs's avatar
Mark Friedrichs committed
239
   int totalSize                           = getSize()*getWidth();
240
   //int totalSize                           = getSize();
241

Mark Friedrichs's avatar
Mark Friedrichs committed
242
243
244
245
246
247
248
   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
249
      }
Mark Friedrichs's avatar
Mark Friedrichs committed
250
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
251

Mark Friedrichs's avatar
Mark Friedrichs committed
252
   } else if( baseType == BrookStreamInternal::Double ){
Mark Friedrichs's avatar
Mark Friedrichs committed
253
254

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

Mark Friedrichs's avatar
Mark Friedrichs committed
259
   } else if( baseType == BrookStreamInternal::Integer ){
260
261

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
272
273
274
275
276
277
   }

   // set dangling values

   _loadDanglingValues();

278
   // write to GPU
Mark Friedrichs's avatar
Mark Friedrichs committed
279

280
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
281
282
}

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

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

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

301
302
// ---------------------------------------------------------------------------------------

Mark Friedrichs's avatar
Mark Friedrichs committed
303
   // get _data from GPU
304

Mark Friedrichs's avatar
Mark Friedrichs committed
305
   _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
306

Mark Friedrichs's avatar
Mark Friedrichs committed
307
   // load into array
Mark Friedrichs's avatar
Mark Friedrichs committed
308

Mark Friedrichs's avatar
Mark Friedrichs committed
309
310
   int totalSize                             = getSize()*getWidth();
   BrookStreamInternal::DataType  baseType   = getBaseDataType();
311

Mark Friedrichs's avatar
Mark Friedrichs committed
312
   if( baseType == BrookStreamInternal::Float ){
313

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

326
327
328
//printf( "%s Basetype is double\n", methodName.c_str() );
//fflush( stdout );

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

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

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

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

352
353
354
355
356
357
358
/** 
 * Fill stream w/ specified value
 * 
 * @param value                     value to fill stream w/
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
359
void BrookFloatStreamInternal::fillWithValue( void* value ){
Mark Friedrichs's avatar
Mark Friedrichs committed
360

361
362
// ---------------------------------------------------------------------------------------

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
381
   _loadDanglingValues();
382
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
383

Mark Friedrichs's avatar
Mark Friedrichs committed
384
385
}

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
397
// ---------------------------------------------------------------------------------------
Mark Friedrichs's avatar
Mark Friedrichs committed
398

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
403
404
   if( readFromBoard ){
      _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
405
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
406
407

   return (void*) _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
408
409
}

410
411
412
413
414
415
416
/** 
 * Get data
 * 
 * @return data array
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
417
void* BrookFloatStreamInternal::getData( void ){
418
419
420

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

Mark Friedrichs's avatar
Mark Friedrichs committed
421
   // static const std::string methodName      = "BrookFloatStreamInternal::getData";
422
423
424

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

Mark Friedrichs's avatar
Mark Friedrichs committed
425
   return getData( 0 );
Mark Friedrichs's avatar
Mark Friedrichs committed
426
427
}

428
429
430
431
432
433
434
/** 
 * Load dangling value into stream
 * 
 * @param danglingValue   dangling value to load
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
435
void BrookFloatStreamInternal::_loadDanglingValues( float danglingValue ){
Mark Friedrichs's avatar
Mark Friedrichs committed
436

437
438
// ---------------------------------------------------------------------------------------

439
    static const std::string methodName      = "BrookFloatStreamInternal::_loadDanglingValues";
440
441
442

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

Mark Friedrichs's avatar
Mark Friedrichs committed
443
444
445
446
447
   int width      = getWidth();

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

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

458
459
460
461
462
/** 
 * Load default dangling value into stream
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
463
void BrookFloatStreamInternal::_loadDanglingValues( void ){
464
   _loadDanglingValues( _defaultDangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
465
}
Mark Friedrichs's avatar
Mark Friedrichs committed
466
467
468
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

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

520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* 
 * Print array contents of object to file
 *
 * @param log         file to print to
 *
 * @return DefaultReturnValue
 *
 * */

int BrookFloatStreamInternal::_bodyPrintToFile( FILE* log ){

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

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

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
537
   assert( log );
538

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
539
540
541
542
543
544
545
546
   void* dataArrayV = getData( 1 );
   //void* dataArrayV = getDataArray( );
   //saveToArray( dataArrayV );

   int streamSize         = getStreamSize();
   int width              = getWidth();
   int index              = 0;
   const float* dataArray = (float*) dataArrayV;
547
548
549
   for( int ii = 0; ii < streamSize; ii++ ){
      std::stringstream message;
      message.width( 15 );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
550
      message.precision( 8 );
551
552
553
554
555
556
557
558
      message << ii << " [ ";
      for( int jj = 0; jj < width; jj++ ){
         message << dataArray[index++] << " ";
      }
      message << "]\n";
      (void) fprintf( log, "%s", message.str().c_str() );    
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
559
   //delete[] dataArrayV;
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

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