BrookPlatform.cpp 15.7 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
32
33
/* -------------------------------------------------------------------------- *
 *                                   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.                                     *
 * -------------------------------------------------------------------------- */

#include "BrookPlatform.h"
#include "BrookKernelFactory.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
34
#include "OpenMMBrookInterface.h"
35
36
37
#include "openmm/internal/OpenMMContextImpl.h"
#include "openmm/OpenMMException.h"
#include "openmm/kernels.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
38
#include "../../reference/src/SimTKUtilities/SimTKOpenMMRealType.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
39
40
41
42
43
#include <brook/brook.hpp>
#include <stdlib.h>
#include <sstream>
#include <cctype>
#include <algorithm>
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
44
//#include "/cygdrive/c/Program\ Files/Amd/AMD\ CAL\ 1.2.1_beta/include/cal.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
45
46
47

using namespace OpenMM;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
48
49
50
51
52
53
54
55
56
57
extern "C" void initOpenMMPlugin() {
/*
   CALuint numberOfDevices = 0;
   // validateCALRuntime()
   if( calDeviceGetCount( &numberOfDevices ) == CAL_RESULT_OK && numberOfDevices > 0 ){
      Platform::registerPlatform(new BrookPlatform());
   }
*/
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/** 
 * BrookPlatformData constructor
 *
 */

BrookPlatformData::BrookPlatformData( void ){

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

   // static const std::string methodName      = "BrookPlatformData::BrookPlatformData";

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

      //_forceKernel                 = NULL;

      _removeCOM                   = 0;
      _useOBC                      = 0;
      _hasBonds                    = 0;
      _hasAngles                   = 0;
      _hasPeriodicTorsions         = 0;
      _hasRB                       = 0;
      _hasNonbonded                = 0;
      _cmMotionFrequency           = 0;

}

/** 
 * Get _removeCOM flag
 * 
 * @return  _removeCOM
 *
 */

int BrookPlatformData::removeCOM( void ) const {
   return _removeCOM;
}

/** 
 * Get _useOBC flag
 * 
 * @return  _useOBC
 *
 */

int BrookPlatformData::useOBC( void ) const {
   return _useOBC;
}

/** 
 * Get _hasBonds flag
 * 
 * @return  _hasBonds 
 *
 */

int BrookPlatformData::hasBonds( void ) const {
   return _hasBonds;
}

/** 
 * Get _hasAngles
 * 
 * @return  _hasAngles
 *
 */

int BrookPlatformData::hasAngles( void ) const {
   return _hasAngles;
}

/** 
 * Get _hasPeriodicTorsions
 * 
 * @return  _hasPeriodicTorsions
 *
 */

int BrookPlatformData::hasPeriodicTorsions( void ) const {
   return _hasPeriodicTorsions;
}

/** 
 * Get _hasRB
 * 
 * @return  _hasRB
 *
 */

int BrookPlatformData::hasRB( void ) const {
   return _hasRB;
}

/** 
 * Get _hasNonbonded
 * 
 * @return  _hasNonbonded
 *
 */

int BrookPlatformData::hasNonbonded( void ) const {
   return _hasNonbonded;
}

/** 
 * Get _cmMotionFrequency
 * 
 * @return  _cmMotionFrequency
 *
 */

int BrookPlatformData::cmMotionFrequency( void ) const {
   return _cmMotionFrequency;
}

Mark Friedrichs's avatar
Mark Friedrichs committed
172
173
174
175
176
177
178
/** 
 * Register BrookPlatform
 *
 * @return BrookPlatform instance
 *
 */

179
BrookPlatform* registerBrookPlatform( void ){
Mark Friedrichs's avatar
Mark Friedrichs committed
180
181
182
183
184
185
186

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

   // static const std::string methodName      = "BrookPlatform::registerBrookPlatform";

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

187
188
   BrookPlatform* platform = new BrookPlatform();
   Platform::registerPlatform(platform);
Mark Friedrichs's avatar
Mark Friedrichs committed
189

190
   return platform;
Mark Friedrichs's avatar
Mark Friedrichs committed
191
192
}

193
BrookPlatform* staticPlatform = registerBrookPlatform( );
Mark Friedrichs's avatar
Mark Friedrichs committed
194

Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
197
198
199
/** 
 * BrookPlatform constructor
 *
 */

200
BrookPlatform::BrookPlatform( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
201
202
203
204
205
206
207

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

   // static const std::string methodName      = "BrookPlatform::BrookPlatform(0)";

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

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
208
   _particleStreamWidth  = DefaultParticleStreamWidth;
209
   _minSuggestedThreads  = -1;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
210
   _log                  = NULL;
Mark Friedrichs's avatar
Mark Friedrichs committed
211
212
213

   // get Brook runtime

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
214
215
216
217
218
#ifdef WIN32
   char* runtime;
   size_t numberOfEnv;
    _dupenv_s( &runtime, &numberOfEnv, "brt_runtime" );
#else
Mark Friedrichs's avatar
Mark Friedrichs committed
219
   char* runtime     = getenv( "brt_runtime" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
220
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
221

Mark Friedrichs's avatar
Mark Friedrichs committed
222
   // if environment variable 'brt_runtime' not set, default to 'cal' settinh
223
224
225
226
227

   if( runtime == NULL ){
      runtime = _strdup( "cal" );
   }

Mark Friedrichs's avatar
Mark Friedrichs committed
228
229
   _initializeKernelFactory( );
   _setBrookRuntime( runtime );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
230
231
232
233

#ifdef WIN32
   free( runtime );
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
234
235
}

Mark Friedrichs's avatar
Mark Friedrichs committed
236
237
238
/** 
 * BrookPlatform constructor
 *
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
239
240
241
 * @param defaultParticleStreamWidth  stream width
 * @param runtime                     Brook runtime (cal/cpu)
 * @param log                         log file reference
Mark Friedrichs's avatar
Mark Friedrichs committed
242
243
244
 *
 */

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
245
BrookPlatform::BrookPlatform( int particleStreamWidth, const std::string& runtime, FILE* log ) : _particleStreamWidth( particleStreamWidth ), _log( log ){
Mark Friedrichs's avatar
Mark Friedrichs committed
246
247
248
249
250
251
252
253
254
255

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

   // static const std::string methodName      = "BrookPlatform::BrookPlatform(2)";

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

   _initializeKernelFactory( );
   _setBrookRuntime( runtime );

256
257
}

Mark Friedrichs's avatar
Mark Friedrichs committed
258
259
260
261
262
/** 
 * BrookPlatform destructor
 *
 */

263
BrookPlatform::~BrookPlatform( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
264
265
266
267
268
269
270

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

   // static const std::string methodName      = "BrookPlatform::BrookPlatform";

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

271
272
}

Mark Friedrichs's avatar
Mark Friedrichs committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/** 
 * Initialize kernel factory
 *
 */

void BrookPlatform::_initializeKernelFactory( void ){

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

   // static const std::string methodName      = "BrookPlatform::_initializeKernelFactory";

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

   BrookKernelFactory* factory = new BrookKernelFactory();

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
288
289
290
291
292
293
294
295
296
   registerKernelFactory( InitializeForcesKernel::Name(),         factory );
   registerKernelFactory( CalcHarmonicBondForceKernel::Name(),    factory );
   registerKernelFactory( CalcHarmonicAngleForceKernel::Name(),   factory );
   registerKernelFactory( CalcPeriodicTorsionForceKernel::Name(), factory );
   registerKernelFactory( CalcRBTorsionForceKernel::Name(),       factory );
   registerKernelFactory( CalcNonbondedForceKernel::Name(),       factory );
   registerKernelFactory( CalcGBSAOBCForceKernel::Name(),         factory );
   registerKernelFactory( IntegrateVerletStepKernel::Name(),      factory );
   registerKernelFactory( IntegrateLangevinStepKernel::Name(),    factory );
Mark Friedrichs's avatar
Mark Friedrichs committed
297
   // registerKernelFactory( IntegrateBrownianStepKernel::Name(),    factory );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
298
299
300
   //registerKernelFactory( ApplyAndersenThermostatKernel::Name(),  factory );
   registerKernelFactory( CalcKineticEnergyKernel::Name(),        factory );
   registerKernelFactory( RemoveCMMotionKernel::Name(),           factory );
Mark Friedrichs's avatar
Mark Friedrichs committed
301
   
302
303
}

Mark Friedrichs's avatar
Mark Friedrichs committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/** 
 * Set & validate runtime
 *
 * @param runtime    Brook runtime (cal/cpu)
 *
 * @throws exception if runtime is invalid
 */

void BrookPlatform::_setBrookRuntime( const std::string& runtime ){

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

   static const std::string methodName      = "BrookPlatform::_setBrookRuntime";

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

   // set & validate runtime

   _runtime = runtime;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
323
324
325
326

   // lower case

   std::transform( _runtime.begin(), _runtime.end(), _runtime.begin(), tolower );
Mark Friedrichs's avatar
Mark Friedrichs committed
327
328
329
330
331
332
   if(  _runtime != "cal" && _runtime != "cpu" ){
      std::stringstream message;
      message << methodName << " Brook runtime=" << _runtime << " not recognized.";
      throw OpenMMException( message.str() );
   }

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
333
   if( 1 ){
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356

      //When compiling with cygwin/cl combo, doesn't
      //always work from the environment, so I'm 
      //hardcoding it here. An alternative might be to getenv() in
      //the gromacs code and pass it here. The cygwin getenv() hopefully
      //will work more deterministically.

      char* info_string = NULL;
      int minSuggestedThreads;

      brook::initialize(  _runtime.c_str(), NULL, &info_string, &minSuggestedThreads );

      FILE* log = getLog() ? getLog() : stderr;
      (void) fprintf( log, "Using runtime %s; initializing Brook\n", _runtime.c_str() );
      fprintf( log, "############\n\nBrook info_string:\n%s\n############\n", info_string );
      (void) fflush( log );

      if( minSuggestedThreads > 0 ){
         _minSuggestedThreads = minSuggestedThreads;
      }
   
   } else {

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
357
358
359
      FILE* log = getLog() ? getLog() : stderr;
      (void) fprintf( log, "%s Brook initializing to runtime=<%s>\n", methodName.c_str(), _runtime.c_str() ); 
      (void) fflush( log );
360
      brook::initialize( _runtime.c_str(), NULL );
Mark Friedrichs's avatar
Mark Friedrichs committed
361

362
   }
Mark Friedrichs's avatar
Mark Friedrichs committed
363
364
365
366
367
368
369
370
371
372
373
374
375

}

/** 
 * Return platform name
 *
 * @return "Brook"
 */
    
std::string BrookPlatform::getName() const {
  return "Brook";
}   

376
377
378
379
380
381
382
383
384
385
386
387
/** 
 * Get DuplicationFactor 
 *
 * @param numberOfParticles number of particles
 *
 * @return DuplicationFactor
 */
    
int BrookPlatform::getDuplicationFactor( int numberOfParticles ) const {

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

388
   static const std::string methodName      = "BrookPlatform::getDuplicationFactor";
389
390
391
392
393
394

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

   // default value

   int duplicationFactor = 4;
395
396
   if( numberOfParticles < 65 ){
      duplicationFactor = 1;
397
      (void) fprintf( stderr, "%s forcing optimization parameter 'duplicationFactor' to %d since number of particles=%d < 65\n",
398
399
                      methodName.c_str(), duplicationFactor, numberOfParticles );
   }
400
401

   // set only if _minSuggestedThreads is available from board
402

403
404
405
406
407
408
409
410
411
412
413
414
415
416
   if( _minSuggestedThreads > 0 ){
      float threads   = static_cast<float>( _minSuggestedThreads );
      float numP      = static_cast<float>( numberOfParticles );
      float iUnroll   = 4.0f;
      float factor    = (threads*iUnroll)/numP;
      if( (factor*numP) < (threads*iUnroll) ){
         factor += 1.0f;
      }
      if( factor <= 1.0f ){
         duplicationFactor   = 1;
      } else {
         duplicationFactor   = static_cast<int>( ceil( factor*0.25f ) ); 
         duplicationFactor  *= 4; 
      }
417
      // (void) fprintf( stderr, "getDuplicationFactor %.1f numiP=%.1f factor=%.1f duplicationFactor=%d\n", threads, numP, factor, duplicationFactor );
418
   }
419

420
421
422
   return duplicationFactor;
}   

Mark Friedrichs's avatar
Mark Friedrichs committed
423
424
425
426
427
428
429
430
431
432
/** 
 * Return platform speed
 *
 * @return speed
 */
    
double BrookPlatform::getSpeed() const {
  return 10.0;
}   

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
433
434
435
436
437
438
439
440
441
442
/** 
 * Return particle stream width
 *
 * @return particle stream width
 */
    
int BrookPlatform::getParticleStreamWidth() const {
  return _particleStreamWidth;
}   

Mark Friedrichs's avatar
Mark Friedrichs committed
443
444
445
446
447
448
/** 
 * Return true if BrookPlatform supports double precison
 *
 * @return true if BrookPlatform supports double precison
 */

449
bool BrookPlatform::supportsDoublePrecision( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
450
451
452
453
454
455
456

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

   // static const std::string methodName      = "BrookPlatform::supportsDoublePrecision";

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

Mark Friedrichs's avatar
Mark Friedrichs committed
457
458
459
    return (sizeof(RealOpenMM) >= sizeof(double));
}

Mark Friedrichs's avatar
Mark Friedrichs committed
460
461
462
463
464
/** 
 * Return Stream factory
 *
 */

465
const StreamFactory& BrookPlatform::getDefaultStreamFactory( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
466
    return _defaultStreamFactory;
Mark Friedrichs's avatar
Mark Friedrichs committed
467
}
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
/** 
 *
 * Static method
 *
 * Return stream size and height given size of array and stream width
 *
 * @param size           size of array
 * @param streamWidth    stream width
 * @param outputHeight   output stream height
 *
 * @return stream size; -1 if streamWidth < 1 || size < 1
 *
 */

int BrookPlatform::getStreamSize( int size, int streamWidth, int* outputHeight ){

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

   // static const std::string methodName      = "BrookPlatform::getStreamSize";

// ---------------------------------------------------------------------------------------
490

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
491
   if( streamWidth < 1 || size < 1 ){
492
493
494
495
496
497
498
499
500
501
502
503
      return -1;
   }

   int height = size/streamWidth;
   if( streamWidth*height < size ){
      height++;
   }
   if( outputHeight ){
      *outputHeight = height;
   }
   return height*streamWidth;
}
Mark Friedrichs's avatar
Mark Friedrichs committed
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
529

/** 
 * Get log file reference
 * 
 * @return  log file reference
 *
 */

FILE* BrookPlatform::getLog( void ) const {
   return _log;
}

/** 
 * Set log file reference
 * 
 * @param  log file reference
 *
 * @return  DefaultReturnValue
 *
 */

int BrookPlatform::setLog( FILE* log ){
   _log = log;
   return BrookPlatform::DefaultErrorValue;
}

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/** 
 *
 * This is called whenever a new OpenMMContext is created.  It gives the Platform a chance to initialize
 * the context and store platform-specific data in it.
 *
 */
void BrookPlatform::contextCreated( OpenMMContextImpl& context ) const {

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

   // static const std::string methodName      = "BrookPlatform::contextCreated";

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

544
545
   int particles = context.getSystem().getNumParticles();
   OpenMMBrookInterface* openMMBrookInterface = new OpenMMBrookInterface( getParticleStreamWidth(), getDuplicationFactor( particles ) );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
546
//   openMMBrookInterface->setLog( stderr );
547
 
Mark Friedrichs's avatar
Mark Friedrichs committed
548
   context.setPlatformData( openMMBrookInterface );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
}

/** 
 *
 * This is called whenever an OpenMMContext is deleted.  It gives the Platform a chance to clean up
 * any platform-specific data that was stored in it.
 *
 */

void BrookPlatform::contextDestroyed( OpenMMContextImpl& context ) const {

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

   // static const std::string methodName      = "BrookPlatform::contextDestroyed";

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

}