Simbody  3.7
ParallelExecutor.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_PARALLEL_EXECUTOR_H_
2 #define SimTK_SimTKCOMMON_PARALLEL_EXECUTOR_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2008-12 Stanford University and the Authors. *
13  * Authors: Peter Eastman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include "PrivateImplementation.h"
28 #include <thread>
29 #include <iostream>
30 
31 namespace SimTK {
32 
33 class ParallelExecutor;
34 class ParallelExecutorImpl;
35 
36 // We only want the template instantiation to occur once. This symbol is defined in the SimTK core
37 // compilation unit that defines the ParallelExecutor class but should not be defined any other time.
38 
39 #ifndef SimTK_SIMTKCOMMON_DEFINING_PARALLEL_EXECUTOR
40  extern template class PIMPLHandle<ParallelExecutor, ParallelExecutorImpl>;
41 #endif
42 
98 public:
99  class Task;
105  explicit ParallelExecutor();
112  explicit ParallelExecutor(int maxThreads);
118  ParallelExecutor* clone() const;
125  void execute(Task& task, int times);
131  static int getNumProcessors();
136  static bool isWorkerThread();
141  int getMaxThreads() const;
142 };
143 
149 public:
150  virtual ~Task() {
151  }
156  virtual void execute(int index) = 0;
161  virtual void initialize() {
162  }
168  virtual void finish() {
169  }
170 };
171 
172 } // namespace SimTK
173 
174 #endif // SimTK_SimTKCOMMON_PARALLEL_EXECUTOR_H_
#define SimTK_SimTKCOMMON_EXPORT
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:224
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
virtual ~Task()
Definition: ParallelExecutor.h:150
This class is used for performing multithreaded computations.
Definition: ParallelExecutor.h:97
virtual void finish()
This method is invoked once by each worker thread after all invocations of the task on that thread ar...
Definition: ParallelExecutor.h:168
virtual void initialize()
This method is invoked once by each worker thread before the task is executed.
Definition: ParallelExecutor.h:161
This header provides declarations of the user-visible portion of the PIMPLHandle template classes tha...
Concrete subclasses of this abstract class represent tasks that can be executed by a ParallelExecutor...
Definition: ParallelExecutor.h:148