BrookPlatform.cpp 9.08 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
Mark Friedrichs committed
34
35
#include "OpenMMException.h"
#include "kernels.h"
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
36
#include "../../reference/src/SimTKUtilities/SimTKOpenMMRealType.h"
Mark Friedrichs's avatar
Mark Friedrichs committed
37
38
39
40
41
#include <brook/brook.hpp>
#include <stdlib.h>
#include <sstream>
#include <cctype>
#include <algorithm>
Mark Friedrichs's avatar
Mark Friedrichs committed
42
43
44

using namespace OpenMM;

Mark Friedrichs's avatar
Mark Friedrichs committed
45
46
47
48
49
50
51
/** 
 * Register BrookPlatform
 *
 * @return BrookPlatform instance
 *
 */

52
BrookPlatform* registerBrookPlatform( void ){
Mark Friedrichs's avatar
Mark Friedrichs committed
53
54
55
56
57
58
59

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

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

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

60
61
   BrookPlatform* platform = new BrookPlatform();
   Platform::registerPlatform(platform);
Mark Friedrichs's avatar
Mark Friedrichs committed
62

63
   return platform;
Mark Friedrichs's avatar
Mark Friedrichs committed
64
65
}

66
BrookPlatform* staticPlatform = registerBrookPlatform( );
Mark Friedrichs's avatar
Mark Friedrichs committed
67

Mark Friedrichs's avatar
Mark Friedrichs committed
68
69
70
71
72
/** 
 * BrookPlatform constructor
 *
 */

73
BrookPlatform::BrookPlatform( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
74
75
76
77
78
79
80
81
82
83
84
85

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

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

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

   _atomStreamWidth  = DefaultAtomStreamWidth;
   _log              = NULL;

   // get Brook runtime

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
86
87
88
89
90
#ifdef WIN32
   char* runtime;
   size_t numberOfEnv;
    _dupenv_s( &runtime, &numberOfEnv, "brt_runtime" );
#else
Mark Friedrichs's avatar
Mark Friedrichs committed
91
   char* runtime     = getenv( "brt_runtime" );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
92
#endif
Mark Friedrichs's avatar
Mark Friedrichs committed
93
94
95

   _initializeKernelFactory( );
   _setBrookRuntime( runtime );
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
96
97
98
99

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

Mark Friedrichs's avatar
Mark Friedrichs committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/** 
 * BrookPlatform constructor
 *
 * @param defaultAtomStreamWidth  stream width
 * @param runtime                 Brook runtime (cal/cpu)
 * @param log                     log file reference
 *
 */

BrookPlatform::BrookPlatform( int atomStreamWidth, const std::string& runtime, FILE* log ){

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

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

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

   _log              = log;
   _atomStreamWidth  = atomStreamWidth;
   _initializeKernelFactory( );
   _setBrookRuntime( runtime );

124
125
}

Mark Friedrichs's avatar
Mark Friedrichs committed
126
127
128
129
130
/** 
 * BrookPlatform destructor
 *
 */

131
BrookPlatform::~BrookPlatform( ){
Mark Friedrichs's avatar
Mark Friedrichs committed
132
133
134
135
136
137
138

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

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

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

139
140
}

Mark Friedrichs's avatar
Mark Friedrichs committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/** 
 * Initialize kernel factory
 *
 */

void BrookPlatform::_initializeKernelFactory( void ){

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

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

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

   BrookKernelFactory* factory = new BrookKernelFactory();

156
   registerKernelFactory( CalcNonbondedForceKernel::Name(), factory);
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
157
   registerKernelFactory( CalcGBSAOBCForceFieldKernel::Name(),    factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
158
   registerKernelFactory( IntegrateVerletStepKernel::Name(),      factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
159
   registerKernelFactory( IntegrateLangevinStepKernel::Name(),    factory);
160
   registerKernelFactory( IntegrateBrownianStepKernel::Name(),    factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
161
   //registerKernelFactory( ApplyAndersenThermostatKernel::Name(),  factory);
162
   registerKernelFactory( CalcKineticEnergyKernel::Name(),        factory);
163
   registerKernelFactory( RemoveCMMotionKernel::Name(),           factory);
Mark Friedrichs's avatar
Mark Friedrichs committed
164
   
165
166
}

Mark Friedrichs's avatar
Mark Friedrichs committed
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
/** 
 * 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;
   std::transform( _runtime.begin(), _runtime.end(), _runtime.begin(), tolower);
   if(  _runtime != "cal" && _runtime != "cpu" ){
      std::stringstream message;
      message << methodName << " Brook runtime=" << _runtime << " not recognized.";
      throw OpenMMException( message.str() );
   }

   if( getLog() ){
Mark Friedrichs's avatar
Mark Friedrichs committed
194
      (void) fprintf( getLog(), "%s Brook initializing to runtime=<%s>\n", methodName.c_str(), _runtime.c_str() ); 
Mark Friedrichs's avatar
Mark Friedrichs committed
195
196
197
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
225
226
227
      (void) fflush( getLog() );
   }

   brook::initialize( _runtime.c_str(), NULL );

}

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

/** 
 * Return platform speed
 *
 * @return speed
 */
    
double BrookPlatform::getSpeed() const {
  return 10.0;
}   

/** 
 * Return true if BrookPlatform supports double precison
 *
 * @return true if BrookPlatform supports double precison
 */

228
bool BrookPlatform::supportsDoublePrecision( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
229
230
231
232
233
234
235

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

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

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

Mark Friedrichs's avatar
Mark Friedrichs committed
236
237
238
    return (sizeof(RealOpenMM) >= sizeof(double));
}

Mark Friedrichs's avatar
Mark Friedrichs committed
239
240
241
242
243
/** 
 * Return Stream factory
 *
 */

244
const StreamFactory& BrookPlatform::getDefaultStreamFactory( void ) const {
Mark Friedrichs's avatar
Mark Friedrichs committed
245
    return _defaultStreamFactory;
Mark Friedrichs's avatar
Mark Friedrichs committed
246
}
247

Mark Friedrichs's avatar
Mark Friedrichs committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/** 
 *
 * 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";

// ---------------------------------------------------------------------------------------
269

Mark Friedrichs's avatar
Mark Friedrichs committed
270
   if( streamWidth < 1 || size < 1){
271
272
273
274
275
276
277
278
279
280
281
282
      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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

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