BrookFloatStreamImpl.cpp 14.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
#include "BrookFloatStreamImpl.h"
34
#include "OpenMMException.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
35
36
37

using namespace OpenMM;

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/** 
 * BrookFloatStreamImpl constructor
 * 
 * @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
 *
 */

BrookFloatStreamImpl::BrookFloatStreamImpl( std::string name, int size, Stream::DataType type,
                                            const Platform& platform, int inputStreamWidth,
                                            double inputDefaultDangleValue = 0.0 ) : StreamImpl( name, size, type, platform ){

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

   static const std::string methodName      = "BrookFloatStreamImpl::BrookFloatStreamImpl";
   // static const int debug                   = 1;

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

   // set base type (currently only FLOAT supported)

   switch( type ){

      case Stream::Float:
      case Stream::Float2:
      case Stream::Float3:
      case Stream::Float4:
   
          _baseType = Stream::Float;
          break;
   
      case Stream::Double:
      case Stream::Double2:
      case Stream::Double3:
      case Stream::Double4:

          _baseType = Stream::Float;
          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
86
87
   }

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

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

92
93
      case Stream::Float:
      case Stream::Double:
Mark Friedrichs's avatar
Mark Friedrichs committed
94

95
96
97
98
99
100
101
102
103
104
105
          _width = 1;
          break;

      case Stream::Float2:
      case Stream::Double2:

          _width = 2;
          break;

      case Stream::Float3:
      case Stream::Double3:
Mark Friedrichs's avatar
Mark Friedrichs committed
106

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

110
111
      case Stream::Float4:
      case Stream::Double4:
Mark Friedrichs's avatar
Mark Friedrichs committed
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
          _width = 4;
          break;
   }

   _defaultDangleValue = (BrookOpenMMFloat) inputDefaultDangleValue;

   // set stream height based on specified stream _width

   if( inputStreamWidth < 1 ){
      std::stringstream message;
      message << methodName << " stream=" << name << " input stream width=" << type << " is less than 1.";
      throw OpenMMException( message.str() );
   }

   _streamWidth    = inputStreamWidth;
   _streamHeight   = size/_streamWidth + ((size % _streamWidth) ? 1 : 0);
   int streamSize  = getStreamSize();

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

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

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
      case 1:
         _aStream      = brook::stream::create<float>(  _streamHeight, _streamWidth );
         break;
   
      case 2:
         _aStream      = brook::stream::create<float2>( _streamHeight, _streamWidth );
         break;
   
      case 3:
         _aStream      = brook::stream::create<float3>( _streamHeight, _streamWidth );
         break;
   
      case 4:
         _aStream      = brook::stream::create<float4>( _streamHeight, _streamWidth );
         break;
Mark Friedrichs's avatar
Mark Friedrichs committed
150
151
152
153
   }

   // allocate memory for data buffer

154
155
156
   _data = new float*[streamSize];
   for( int ii = 0; ii < streamSize; ii++ ){
       _data[ii] = new float[_width];
Mark Friedrichs's avatar
Mark Friedrichs committed
157
158
   }

159
160
   // check if we are using float or double

Mark Friedrichs's avatar
Mark Friedrichs committed
161
162
   if( sizeof( float ) != sizeof( RealOpenMM ) ){
   
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
      _realOpenMMData = new RealOpenMM*[streamSize];
      for( int ii = 0; ii < streamSize; ii++ ){
         _realOpenMMData[ii] = new RealOpenMM[_width];
      }
   } else {
      _realOpenMMData = NULL;
   }
}

/** 
 * BrookFloatStreamImpl destructor
 * 
 */

BrookFloatStreamImpl::~BrookFloatStreamImpl( ){

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

   //static const std::string methodName      = "BrookFloatStreamImpl::~BrookFloatStreamImpl";
   // static const int debug                   = 1;

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

   //delete _aStream;

   int streamSize = getStreamSize();

   for( int ii = 0; ii < streamSize; ii++ ){
      delete[] _data[ii];
   }
   delete[] _data;

   if( _realOpenMMData ){
      for( int ii = 0; ii < streamSize; ii++ ){
         delete[] _realOpenMMData[ii];
Mark Friedrichs's avatar
Mark Friedrichs committed
198
      }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
      delete[] _realOpenMMData;
   }

}

/** 
 * Load _data from 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.
 *
 */

void BrookFloatStreamImpl::loadFromArray( const void* array ){

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

   // static const std::string methodName      = "BrookFloatStreamImpl::loadFromArray";
   // static const int debug                   = 1;

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

   if( _baseType == Stream::Float ){
      loadFromArray( array, Stream::Float );
Mark Friedrichs's avatar
Mark Friedrichs committed
225
   } else {
226
      loadFromArray( array, Stream::Double );
Mark Friedrichs's avatar
Mark Friedrichs committed
227
   }
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

}

/** 
 * Get width
 * 
 * @return width
 */

int BrookFloatStreamImpl::getWidth( void ) const {
   return _width;
}

/** 
 * Get stream width
 * 
 * @return stream width
 */

int BrookFloatStreamImpl::getStreamWidth( void ) const {
   return _streamWidth;
Mark Friedrichs's avatar
Mark Friedrichs committed
249
250
}

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/** 
 * Get stream height
 * 
 * @return stream height
 */

int BrookFloatStreamImpl::getStreamHeight( void ) const {
   return _streamHeight;
}

/** 
 * Get Brook stream
 * 
 * @return Brook stream 
 */

brook::stream& BrookFloatStreamImpl::getBrookStream( void ){
   return _aStream;
}

/** 
 * Get stream size
 * 
 * @return stream size
 */

int BrookFloatStreamImpl::getStreamSize( void ) const {
   return _streamWidth*_streamHeight;
Mark Friedrichs's avatar
Mark Friedrichs committed
279
280
}

281
282
283
284
285
286
287
288
289
290
291
292
/** 
 * Load _data from 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.
 *
 * @param inputType  type of input array (Stream::Float, Stream::Double, Stream::Integer)
 *
 * @throw if input type not recognized, an exception is thrown
 */
Mark Friedrichs's avatar
Mark Friedrichs committed
293

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
void BrookFloatStreamImpl::loadFromArray( const void* array, Stream::DataType inputType ){

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

   static const std::string methodName      = "BrookFloatStreamImpl::loadFromArray";
   // static const int debug                   = 1;

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

   int index = 0;
   if( inputType == Stream::Float ){

   float* arrayData = (float*) array;
      for( int ii = 0; ii < getSize(); ii++ ){
         for( int jj = 0; jj < getWidth(); jj++ ){
            _data[ii][jj] = (BrookOpenMMFloat) arrayData[index++];
Mark Friedrichs's avatar
Mark Friedrichs committed
310
311
312
         }
      }

313
   } else if( inputType == Stream::Double ){
Mark Friedrichs's avatar
Mark Friedrichs committed
314
315

      double* arrayData = (double*) array;
316
317
318
319
320
321
322
323
324
325
326
327
328
      for( int ii = 0; ii < getSize(); ii++ ){
         for( int jj = 0; jj < getWidth(); jj++ ){
            _data[ii][jj] = (BrookOpenMMFloat) arrayData[index++];
         }
      }

   } else if( inputType == Stream::Integer ){

      int* arrayData = (int*) array;
 
      for( int ii = 0; ii < getSize(); ii++ ){
         for( int jj = 0; jj < getWidth(); jj++ ){
            _data[ii][jj] = (BrookOpenMMFloat) arrayData[index++];
Mark Friedrichs's avatar
Mark Friedrichs committed
329
330
         }
      }
331
332
333
334
335
336
337

   } else {
      
      std::stringstream message;
      message << methodName << " stream=" << getName() << " input type=" << inputType << " not recognized.";
      throw OpenMMException( message.str() );

Mark Friedrichs's avatar
Mark Friedrichs committed
338
339
340
341
342
343
   }

   // set dangling values

   _loadDanglingValues();

344
   // write to GPU
Mark Friedrichs's avatar
Mark Friedrichs committed
345

346
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
347
348
}

349
350
351
void BrookFloatStreamImpl::saveToArray( void* array ){

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

353
354
   //static const std::string methodName      = "BrookFloatStreamImpl::saveToArray";
   // static const int debug                   = 1;
Mark Friedrichs's avatar
Mark Friedrichs committed
355

356
357
358
359
360
// ---------------------------------------------------------------------------------------

  // get _data from GPU

  _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
361
362
363

  // load into array

364
365
366
  int index = 0;
  if( _baseType == Stream::Float ){

Mark Friedrichs's avatar
Mark Friedrichs committed
367
     float* arrayData = (float*) array;
368
369
370
     for( int ii = 0; ii < getSize(); ii++ ){
        for( int jj = 0; jj < getWidth(); jj++ ){
           arrayData[index++] = (float) _data[ii][jj];
Mark Friedrichs's avatar
Mark Friedrichs committed
371
372
        }
     }
373

Mark Friedrichs's avatar
Mark Friedrichs committed
374
   } else {
375

Mark Friedrichs's avatar
Mark Friedrichs committed
376
      double* arrayData = (double*) array;
377
378
379
      for( int ii = 0; ii < getSize(); ii++ ){
         for( int jj = 0; jj < getWidth(); jj++ ){
            arrayData[index++] = _data[ii][jj];
Mark Friedrichs's avatar
Mark Friedrichs committed
380
381
         }
      }
382

Mark Friedrichs's avatar
Mark Friedrichs committed
383
384
385
   }
}

386
387
388
389
390
391
392
/** 
 * Fill stream w/ specified value
 * 
 * @param value                     value to fill stream w/
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
393
394
void BrookFloatStreamImpl::fillWithValue( void* value ){

395
396
397
398
399
400
401
402
403
404
// ---------------------------------------------------------------------------------------

   // static const std::string methodName      = "BrookFloatStreamImpl::fillWithValue";
   // static const int debug                   = 1;

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

   BrookOpenMMFloat valueData;
   if( _baseType == Stream::Float) {
      valueData = (BrookOpenMMFloat) *((float*) value);
Mark Friedrichs's avatar
Mark Friedrichs committed
405
   } else {
406
407
408
409
410
      valueData = (BrookOpenMMFloat) *((double*) value);
   }
   for( int ii = 0; ii < getSize(); ii++ ){
      for (int jj = 0; jj < getWidth(); jj++ ){
         _data[ii][jj] = valueData;
Mark Friedrichs's avatar
Mark Friedrichs committed
411
412
      }
   }
413

Mark Friedrichs's avatar
Mark Friedrichs committed
414
   _loadDanglingValues();
415
   _aStream.read( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
416
417
}

418
419
420
421
422
423
424
const RealOpenMM* const * BrookFloatStreamImpl::getData() const {
   return NULL;
}

// problem w/ const here -- _data is modified (cast away?)

/*
Mark Friedrichs's avatar
Mark Friedrichs committed
425
426
const RealOpenMM* const * BrookFloatStreamImpl::getData() const {

427
   // retrieve _data from GPU
Mark Friedrichs's avatar
Mark Friedrichs committed
428

429
   _aStream.write( _data );
Mark Friedrichs's avatar
Mark Friedrichs committed
430
431
432
433
434
435

   // check if RealOpenMM is float; if not, then 
   // copy into realOpenMMData[][] array

   if( realOpenMMData ){
      for( int i = 0; i < getSize(); i++ ){
436
437
         for( int j = 0; j < _width; j++ ){
            realOpenMMData[i][j] = (RealOpenMM) _data[i][j];
Mark Friedrichs's avatar
Mark Friedrichs committed
438
439
440
441
         }
      }
      return realOpenMMData;
   } else {
442
      return _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
443
444
   }
}
445
*/
Mark Friedrichs's avatar
Mark Friedrichs committed
446

447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/** 
 * Get data
 * 
 * @return data array
 *
 */

RealOpenMM** BrookFloatStreamImpl::getData( void ){

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

   // static const std::string methodName      = "BrookFloatStreamImpl::getData";
   // static const int debug                   = 1;

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

   _aStream.write( _data );
   if( _realOpenMMData ){
   for( int ii = 0; ii < getSize(); ii++ ){
      for (int jj = 0; jj < getWidth(); jj++ ){
            _realOpenMMData[ii][jj] = (RealOpenMM) _data[ii][jj];
Mark Friedrichs's avatar
Mark Friedrichs committed
468
469
         }
      }
470
      return _realOpenMMData;
Mark Friedrichs's avatar
Mark Friedrichs committed
471
   } else {
472
      return _data;
Mark Friedrichs's avatar
Mark Friedrichs committed
473
474
475
   }
}

476
477
478
479
480
481
482
483

/** 
 * Load dangling value into stream
 * 
 * @param danglingValue   dangling value to load
 *
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
484
485
void BrookFloatStreamImpl::_loadDanglingValues( float danglingValue ){

486
487
488
489
490
491
492
493
494
495
496
// ---------------------------------------------------------------------------------------

   // static const std::string methodName      = "BrookFloatStreamImpl::_loadDanglingValues";
   // static const int debug                   = 1;

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

   int streamSize = getStreamSize();
   for( int ii = getSize(); ii < streamSize; ii++ ){
      for (int jj = 0; jj < getWidth(); jj++ ){
          _data[ii][jj] = danglingValue;
Mark Friedrichs's avatar
Mark Friedrichs committed
497
498
499
500
       }
    }
}

501
502
503
504
505
/** 
 * Load default dangling value into stream
 * 
 */

Mark Friedrichs's avatar
Mark Friedrichs committed
506
void BrookFloatStreamImpl::_loadDanglingValues( void ){
507
   _loadDanglingValues( _defaultDangleValue );
Mark Friedrichs's avatar
Mark Friedrichs committed
508
}