Commit 7dc2ff9e authored by peastman's avatar peastman
Browse files

Catch nan coordinates before they can produce a segfault

parent a9c75e52
...@@ -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) 2013-2014 Stanford University and the Authors. * * Portions copyright (c) 2013-2015 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -140,7 +140,7 @@ public: ...@@ -140,7 +140,7 @@ public:
class CpuCalcForcesAndEnergyKernel::InitForceTask : public ThreadPool::Task { class CpuCalcForcesAndEnergyKernel::InitForceTask : public ThreadPool::Task {
public: public:
InitForceTask(int numParticles, ContextImpl& context, CpuPlatform::PlatformData& data) : numParticles(numParticles), context(context), data(data) { InitForceTask(int numParticles, ContextImpl& context, CpuPlatform::PlatformData& data) : numParticles(numParticles), positionsValid(true), context(context), data(data) {
} }
void execute(ThreadPool& threads, int threadIndex) { void execute(ThreadPool& threads, int threadIndex) {
// Convert the positions to single precision and apply periodic boundary conditions // Convert the positions to single precision and apply periodic boundary conditions
...@@ -167,6 +167,12 @@ public: ...@@ -167,6 +167,12 @@ public:
posq[4*i+2] = (float) posData[i][2]; posq[4*i+2] = (float) posData[i][2];
} }
// Check for invalid positions.
for (int i = 4*start; i < 4*end; i++)
if (posq[i] != posq[i])
positionsValid = false;
// Clear the forces. // Clear the forces.
fvec4 zero(0.0f); fvec4 zero(0.0f);
...@@ -174,6 +180,7 @@ public: ...@@ -174,6 +180,7 @@ public:
zero.store(&data.threadForce[threadIndex][j*4]); zero.store(&data.threadForce[threadIndex][j*4]);
} }
int numParticles; int numParticles;
bool positionsValid;
ContextImpl& context; ContextImpl& context;
CpuPlatform::PlatformData& data; CpuPlatform::PlatformData& data;
}; };
...@@ -198,6 +205,8 @@ void CpuCalcForcesAndEnergyKernel::beginComputation(ContextImpl& context, bool i ...@@ -198,6 +205,8 @@ void CpuCalcForcesAndEnergyKernel::beginComputation(ContextImpl& context, bool i
InitForceTask task(context.getSystem().getNumParticles(), context, data); InitForceTask task(context.getSystem().getNumParticles(), context, data);
data.threads.execute(task); data.threads.execute(task);
data.threads.waitForThreads(); data.threads.waitForThreads();
if (!task.positionsValid)
throw OpenMMException("Particle coordinate is nan");
} }
double CpuCalcForcesAndEnergyKernel::finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups) { double CpuCalcForcesAndEnergyKernel::finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups) {
......
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