"platforms/brook/vscode:/vscode.git/clone" did not exist on "cfb9c98a0e90f0c2fc3d0e9636066fa8f10b1739"
Commit 5eb439d8 authored by peastman's avatar peastman
Browse files

Optimized processing of expressions with tabulated functions

parent 97b972e9
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2014-2016 Stanford University and the Authors. * * Portions copyright (c) 2014-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "openmm/TabulatedFunction.h" #include "openmm/TabulatedFunction.h"
#include "openmm/internal/windowsExport.h" #include "openmm/internal/windowsExport.h"
#include "lepton/CustomFunction.h" #include "lepton/CustomFunction.h"
#include <memory>
#include <vector> #include <vector>
namespace OpenMM { namespace OpenMM {
...@@ -146,6 +147,22 @@ private: ...@@ -146,6 +147,22 @@ private:
std::vector<double> values; std::vector<double> values;
}; };
/**
* This is a lightweight wrapper around an immutable CustomFunction. It makes
* cloning very inexpensive since nothing needs to be copied except a single
* pointer.
*/
class OPENMM_EXPORT SharedFunctionWrapper : public Lepton::CustomFunction {
public:
SharedFunctionWrapper(std::shared_ptr<const CustomFunction> pointer);
int getNumArguments() const;
double evaluate(const double* arguments) const;
double evaluateDerivative(const double* arguments, const int* derivOrder) const;
CustomFunction* clone() const;
private:
std::shared_ptr<const CustomFunction> pointer;
};
} // namespace OpenMM } // namespace OpenMM
#endif /*OPENMM_REFERENCETABULATEDFUNCTION_H_*/ #endif /*OPENMM_REFERENCETABULATEDFUNCTION_H_*/
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2014-2016 Stanford University and the Authors. * * Portions copyright (c) 2014-2019 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -56,19 +56,22 @@ using namespace std; ...@@ -56,19 +56,22 @@ using namespace std;
using Lepton::CustomFunction; using Lepton::CustomFunction;
extern "C" OPENMM_EXPORT CustomFunction* createReferenceTabulatedFunction(const TabulatedFunction& function) { extern "C" OPENMM_EXPORT CustomFunction* createReferenceTabulatedFunction(const TabulatedFunction& function) {
CustomFunction* fn;
if (dynamic_cast<const Continuous1DFunction*>(&function) != NULL) if (dynamic_cast<const Continuous1DFunction*>(&function) != NULL)
return new ReferenceContinuous1DFunction(dynamic_cast<const Continuous1DFunction&>(function)); fn = new ReferenceContinuous1DFunction(dynamic_cast<const Continuous1DFunction&>(function));
if (dynamic_cast<const Continuous2DFunction*>(&function) != NULL) else if (dynamic_cast<const Continuous2DFunction*>(&function) != NULL)
return new ReferenceContinuous2DFunction(dynamic_cast<const Continuous2DFunction&>(function)); fn = new ReferenceContinuous2DFunction(dynamic_cast<const Continuous2DFunction&>(function));
if (dynamic_cast<const Continuous3DFunction*>(&function) != NULL) else if (dynamic_cast<const Continuous3DFunction*>(&function) != NULL)
return new ReferenceContinuous3DFunction(dynamic_cast<const Continuous3DFunction&>(function)); fn = new ReferenceContinuous3DFunction(dynamic_cast<const Continuous3DFunction&>(function));
if (dynamic_cast<const Discrete1DFunction*>(&function) != NULL) else if (dynamic_cast<const Discrete1DFunction*>(&function) != NULL)
return new ReferenceDiscrete1DFunction(dynamic_cast<const Discrete1DFunction&>(function)); fn = new ReferenceDiscrete1DFunction(dynamic_cast<const Discrete1DFunction&>(function));
if (dynamic_cast<const Discrete2DFunction*>(&function) != NULL) else if (dynamic_cast<const Discrete2DFunction*>(&function) != NULL)
return new ReferenceDiscrete2DFunction(dynamic_cast<const Discrete2DFunction&>(function)); fn = new ReferenceDiscrete2DFunction(dynamic_cast<const Discrete2DFunction&>(function));
if (dynamic_cast<const Discrete3DFunction*>(&function) != NULL) else if (dynamic_cast<const Discrete3DFunction*>(&function) != NULL)
return new ReferenceDiscrete3DFunction(dynamic_cast<const Discrete3DFunction&>(function)); fn = new ReferenceDiscrete3DFunction(dynamic_cast<const Discrete3DFunction&>(function));
else
throw OpenMMException("createReferenceTabulatedFunction: Unknown function type"); throw OpenMMException("createReferenceTabulatedFunction: Unknown function type");
return new SharedFunctionWrapper(shared_ptr<const CustomFunction>(fn));
} }
ReferenceContinuous1DFunction::ReferenceContinuous1DFunction(const Continuous1DFunction& function) : function(function) { ReferenceContinuous1DFunction::ReferenceContinuous1DFunction(const Continuous1DFunction& function) : function(function) {
...@@ -298,3 +301,22 @@ double ReferenceDiscrete3DFunction::evaluateDerivative(const double* arguments, ...@@ -298,3 +301,22 @@ double ReferenceDiscrete3DFunction::evaluateDerivative(const double* arguments,
CustomFunction* ReferenceDiscrete3DFunction::clone() const { CustomFunction* ReferenceDiscrete3DFunction::clone() const {
return new ReferenceDiscrete3DFunction(function); return new ReferenceDiscrete3DFunction(function);
} }
SharedFunctionWrapper::SharedFunctionWrapper(shared_ptr<const CustomFunction> pointer) : pointer(pointer) {
}
int SharedFunctionWrapper::getNumArguments() const {
return pointer->getNumArguments();
}
double SharedFunctionWrapper::evaluate(const double* arguments) const {
return pointer->evaluate(arguments);
}
double SharedFunctionWrapper::evaluateDerivative(const double* arguments, const int* derivOrder) const {
return pointer->evaluateDerivative(arguments, derivOrder);
}
CustomFunction* SharedFunctionWrapper::clone() const {
return new SharedFunctionWrapper(pointer);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment