Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lambdas for thread pool tasks #1741

Merged
merged 9 commits into from Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Expand Up @@ -108,6 +108,11 @@ before_install:
brew install doxygen fftw;
sudo easy_install pytest;
fi
- if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
CMAKE_URL="https://cmake.org/files/v3.7/cmake-3.7.2-Linux-x86_64.tar.gz";
mkdir $HOME/cmake && travis_retry wget --no-check-certificate --quiet -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C $HOME/cmake;
export PATH=${HOME}/cmake/bin:${PATH};
fi
- if [[ "$OPENCL" == "true" ]]; then
wget http://s3.amazonaws.com/omnia-ci/AMD-APP-SDKInstaller-v3.0.130.135-GA-linux64.tar.bz2;
tar -xjf AMD-APP-SDK*.tar.bz2;
Expand Down
25 changes: 6 additions & 19 deletions CMakeLists.txt
Expand Up @@ -21,10 +21,11 @@ IF( NOT PROJECT_NAME )
PROJECT (OpenMM)
ENDIF( NOT PROJECT_NAME )

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
if("${CMAKE_VERSION}" VERSION_GREATER "3.0" OR "${CMAKE_VERSION}" VERSION_EQUAL "3.0")
CMAKE_POLICY(SET CMP0042 OLD)
endif()
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
CMAKE_POLICY(SET CMP0042 OLD)
CMAKE_POLICY(SET CMP0003 NEW)
CMAKE_POLICY(SET CMP0005 NEW)
CMAKE_POLICY(SET CMP0011 NEW)

#SET(CMAKE_VERBOSE_MAKEFILE 1)

Expand All @@ -39,21 +40,6 @@ IF(NOT cmv EQUAL "2.4") # This whole file...
# We have custom cmake modules for FindOpenMM and running python tests
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")

# Older cmake versions do not have cmake_policy command
# Cmake 2.4.5, the default cmake on red hat linux, has the
# cmake_policy command, but it does not work
# "if(POLICY ..." does not work with cmake 2.4.[56] on red hat
# (cmake 2.4.7 is OK)
if(COMMAND cmake_policy)
if(CMAKE_MAJOR_VERSION GREATER 2 OR CMAKE_MINOR_VERSION GREATER 5)
cmake_policy(SET CMP0003 NEW)
cmake_policy(SET CMP0005 NEW)
endif(CMAKE_MAJOR_VERSION GREATER 2 OR CMAKE_MINOR_VERSION GREATER 5)
if(CMAKE_MAJOR_VERSION GREATER 2 OR CMAKE_MINOR_VERSION GREATER 6 OR CMAKE_PATCH_VERSION GREATER 2)
cmake_policy(SET CMP0011 NEW)
endif(CMAKE_MAJOR_VERSION GREATER 2 OR CMAKE_MINOR_VERSION GREATER 6 OR CMAKE_PATCH_VERSION GREATER 2)
endif(COMMAND cmake_policy)

# Where to install
IF(WIN32)
IF(NOT OPENMM_INSTALL_PREFIX)
Expand Down Expand Up @@ -117,6 +103,7 @@ ELSE( CMAKE_SIZEOF_VOID_P EQUAL 8 )
SET( LIB64 )
ENDIF( CMAKE_SIZEOF_VOID_P EQUAL 8 )

SET (CMAKE_CXX_STANDARD 11)

IF (APPLE AND (NOT PNACL))
# Build 64 bit binaries compatible with OS X 10.7
Expand Down
11 changes: 6 additions & 5 deletions appveyor.yml
@@ -1,12 +1,13 @@
os: Windows Server 2012 R2
os: Visual Studio 2015
platform: x64
configuration: Release
shallow_clone: true
install:

# Setup shell for VS2010, x64, release mode
- >
"%ProgramFiles%\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 /release
# Setup shell for VS2015, x64
- call "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
# Set path to python, git-bash tools.
- "set PATH=C:\\Python34-x64;C:\\Python34-x64\\Scripts;%PATH%"
- "set PATH=C:\\Python35-x64;C:\\Python35-x64\\Scripts;%PATH%"
- "set PATH=C:\\Program Files (x86)\\Git\\bin;%PATH%"
- pip install pytest

Expand Down
4 changes: 2 additions & 2 deletions docs-source/usersguide/library.rst
Expand Up @@ -404,7 +404,7 @@ version of Visual Studio.
Install CMake
=============

CMake is the build system used for OpenMM. You must install CMake version 2.8
CMake is the build system used for OpenMM. You must install CMake version 3.1
or higher before attempting to build OpenMM from source. You can get CMake from
http://www.cmake.org/. If you choose to build CMake from source on Linux, make
sure you have the curses library installed beforehand, so that you will be able
Expand Down Expand Up @@ -509,7 +509,7 @@ Windows

On Windows, perform the following steps:

#. Click Start->All Programs->CMake 2.8->CMake
#. Click Start->All Programs->CMake 3.1->CMake
#. In the box labeled "Where is the source code:" browse to OpenMM src directory
(containing top CMakeLists.txt)
#. In the box labeled "Where to build the binaries" browse to your build_openmm
Expand Down
9 changes: 8 additions & 1 deletion openmmapi/include/openmm/internal/ThreadPool.h
Expand Up @@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2013 Stanford University and the Authors. *
* Portions copyright (c) 2013-2017 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand All @@ -34,6 +34,7 @@

#define NOMINMAX
#include "windowsExport.h"
#include <functional>
#include <pthread.h>
#include <vector>

Expand Down Expand Up @@ -69,6 +70,10 @@ class OPENMM_EXPORT ThreadPool {
* Execute a Task in parallel on the worker threads.
*/
void execute(Task& task);
/**
* Execute a function in parallel on the worker threads.
*/
void execute(std::function<void (ThreadPool&, int)> task);
/**
* This is called by the worker threads to block until all threads have reached the same point
* and the master thread instructs them to continue by calling resumeThreads().
Expand All @@ -90,6 +95,8 @@ class OPENMM_EXPORT ThreadPool {
std::vector<ThreadData*> threadData;
pthread_cond_t startCondition, endCondition;
pthread_mutex_t lock;
Task* currentTask;
std::function<void (ThreadPool& pool, int)> currentFunction;
};

/**
Expand Down
22 changes: 17 additions & 5 deletions openmmapi/src/ThreadPool.cpp
Expand Up @@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2013-2014 Stanford University and the Authors. *
* Portions copyright (c) 2013-2017 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand Down Expand Up @@ -40,10 +40,17 @@ class ThreadPool::ThreadData {
public:
ThreadData(ThreadPool& owner, int index) : owner(owner), index(index), isDeleted(false) {
}
void executeTask() {
if (owner.currentTask != NULL)
owner.currentTask->execute(owner, index);
else
owner.currentFunction(owner, index);
}
ThreadPool& owner;
int index;
bool isDeleted;
Task* currentTask;
function<void (ThreadPool& pool, int)> currentFunction;
};

static void* threadBody(void* args) {
Expand All @@ -54,13 +61,13 @@ static void* threadBody(void* args) {
data.owner.syncThreads();
if (data.isDeleted)
break;
data.currentTask->execute(data.owner, data.index);
data.executeTask();
}
delete &data;
return 0;
}

ThreadPool::ThreadPool(int numThreads) {
ThreadPool::ThreadPool(int numThreads) : currentTask(NULL) {
if (numThreads <= 0)
numThreads = getNumProcessors();
this->numThreads = numThreads;
Expand Down Expand Up @@ -99,8 +106,13 @@ int ThreadPool::getNumThreads() const {
}

void ThreadPool::execute(Task& task) {
for (int i = 0; i < (int) threadData.size(); i++)
threadData[i]->currentTask = &task;
currentTask = &task;
resumeThreads();
}

void ThreadPool::execute(function<void (ThreadPool&, int)> task) {
currentTask = NULL;
currentFunction = task;
resumeThreads();
}

Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuBondForce.h
Expand Up @@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2014 Stanford University and the Authors. *
* Portions copyright (c) 2014-2017 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand Down Expand Up @@ -46,7 +46,6 @@ namespace OpenMM {
*/
class OPENMM_EXPORT_CPU CpuBondForce {
public:
class ComputeForceTask;
CpuBondForce();
/**
* Analyze the set of bonds and decide which to compute with each thread.
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuCustomGBForce.h
@@ -1,5 +1,5 @@

/* Portions copyright (c) 2009-2016 Stanford University and Simbios.
/* Portions copyright (c) 2009-2017 Stanford University and Simbios.
* Contributors: Peter Eastman
*
* Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -39,7 +39,6 @@ namespace OpenMM {

class CpuCustomGBForce {
private:
class ComputeForceTask;
class ThreadData;

bool cutoff;
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuCustomManyParticleForce.h
@@ -1,5 +1,5 @@

/* Portions copyright (c) 2009-2014 Stanford University and Simbios.
/* Portions copyright (c) 2009-2017 Stanford University and Simbios.
* Contributors: Peter Eastman
*
* Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -48,7 +48,6 @@ class CpuCustomManyParticleForce {
class DistanceTermInfo;
class AngleTermInfo;
class DihedralTermInfo;
class ComputeForceTask;
class ThreadData;
int numParticles, numParticlesPerSet, numPerParticleParameters, numTypes;
bool useCutoff, usePeriodic, triclinic, centralParticleMode;
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuCustomNonbondedForce.h
@@ -1,5 +1,5 @@

/* Portions copyright (c) 2009-2016 Stanford University and Simbios.
/* Portions copyright (c) 2009-2017 Stanford University and Simbios.
* Contributors: Peter Eastman
*
* Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -122,7 +122,6 @@ class CpuCustomNonbondedForce {
RealOpenMM* fixedParameters, const std::map<std::string, double>& globalParameters,
std::vector<AlignedArray<float> >& threadForce, bool includeForce, bool includeEnergy, double& totalEnergy, double* energyParamDerivs);
private:
class ComputeForceTask;
class ThreadData;

bool cutoff;
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuGBSAOBCForce.h
@@ -1,5 +1,5 @@

/* Portions copyright (c) 2006-2013 Stanford University and Simbios.
/* Portions copyright (c) 2006-2017 Stanford University and Simbios.
* Contributors: Pande Group
*
* Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -36,7 +36,6 @@ namespace OpenMM {

class CpuGBSAOBCForce {
public:
class ComputeTask;
CpuGBSAOBCForce();

/**
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuGayBerneForce.h
Expand Up @@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2016 Stanford University and the Authors. *
* Portions copyright (c) 2016-2017 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand Down Expand Up @@ -45,7 +45,6 @@ namespace OpenMM {
class CpuGayBerneForce {
public:
struct Matrix;
class ComputeTask;

/**
* Constructor.
Expand Down
2 changes: 0 additions & 2 deletions platforms/cpu/include/CpuKernels.h
Expand Up @@ -54,8 +54,6 @@ namespace OpenMM {
*/
class CpuCalcForcesAndEnergyKernel : public CalcForcesAndEnergyKernel {
public:
class InitForceTask;
class SumForceTask;
CpuCalcForcesAndEnergyKernel(std::string name, const Platform& platform, CpuPlatform::PlatformData& data, ContextImpl& context);
/**
* Initialize the kernel.
Expand Down
5 changes: 1 addition & 4 deletions platforms/cpu/include/CpuLangevinDynamics.h
@@ -1,5 +1,5 @@

/* Portions copyright (c) 2013-2016 Stanford University and Simbios.
/* Portions copyright (c) 2013-2017 Stanford University and Simbios.
* Authors: Peter Eastman
* Contributors:
*
Expand Down Expand Up @@ -35,9 +35,6 @@ namespace OpenMM {

class CpuLangevinDynamics : public ReferenceStochasticDynamics {
public:
class Update1Task;
class Update2Task;
class Update3Task;
/**
* Constructor.
*
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuNeighborList.h
Expand Up @@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2013-2016 Stanford University and the Authors. *
* Portions copyright (c) 2013-2017 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand Down Expand Up @@ -45,7 +45,6 @@ namespace OpenMM {

class OPENMM_EXPORT_CPU CpuNeighborList {
public:
class ThreadTask;
class Voxels;
CpuNeighborList(int blockSize);
void computeNeighborList(int numAtoms, const AlignedArray<float>& atomLocations, const std::vector<std::set<int> >& exclusions,
Expand Down
3 changes: 1 addition & 2 deletions platforms/cpu/include/CpuNonbondedForce.h
@@ -1,5 +1,5 @@

/* Portions copyright (c) 2006-2015 Stanford University and Simbios.
/* Portions copyright (c) 2006-2017 Stanford University and Simbios.
* Contributors: Pande Group
*
* Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -39,7 +39,6 @@ namespace OpenMM {

class CpuNonbondedForce {
public:
class ComputeDirectTask;

/**---------------------------------------------------------------------------------------

Expand Down
4 changes: 1 addition & 3 deletions platforms/cpu/include/CpuSETTLE.h
Expand Up @@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. *
* *
* Portions copyright (c) 2013 Stanford University and the Authors. *
* Portions copyright (c) 2013-2017 Stanford University and the Authors. *
* Authors: Peter Eastman *
* Contributors: *
* *
Expand Down Expand Up @@ -45,8 +45,6 @@ namespace OpenMM {
*/
class OPENMM_EXPORT_CPU CpuSETTLE : public ReferenceConstraintAlgorithm {
public:
class ApplyToPositionsTask;
class ApplyToVelocitiesTask;
CpuSETTLE(const System& system, const ReferenceSETTLEAlgorithm& settle, ThreadPool& threads);
~CpuSETTLE();

Expand Down