 |
Shiokaze Framework
A research-oriented fluid solver for computer graphics
|
|
Go to the documentation of this file.
25 #ifndef SHKZ_PARALLEL_DRIVER_H
26 #define SHKZ_PARALLEL_DRIVER_H
38 #define shkz_default_parallel_name "stdthread"
39 #define shkz_default_splitter_name "sequential_splitter"
59 parallel_driver (
recursive_configurable *parent, std::
string parallel_name=shkz_default_parallel_name, std::
string splitter_name=shkz_default_splitter_name ) : m_parallel_name(parallel_name), m_splitter_name(splitter_name) {
60 if( parent ) parent->add_child(
this);
71 parallel_driver ( std::string parallel_name=shkz_default_parallel_name, std::string splitter_name=shkz_default_splitter_name ) :
parallel_driver(nullptr,parallel_name,splitter_name) {
81 return m_maximal_threads;
90 m_maximal_threads = maximal_threads;
99 return m_parallel_dispatcher.get();
107 void run(
const std::vector<std::function<
void()> > &functions ) {
108 if( m_maximal_threads > 1 ) {
109 m_parallel_dispatcher->run(functions);
111 for(
auto f : functions ) f();
123 size_t size_div_8 = (size+7) / 8;
124 for_each( size_div_8, [&](
size_t n_div_8,
int thread_index ) {
125 for(
char i=0; i<8; ++i ) {
126 size_t n = 8*n_div_8+i;
128 func(n,thread_index);
141 void for_each(
size_t size, std::function<
void(
size_t n,
int thread_index)> func )
const {
144 int num_threads = m_maximal_threads;
145 if( num_threads > size ) num_threads = size;
146 if( num_threads > 1 ) {
148 assert(m_loop_splitter.get());
150 const void *context = m_loop_splitter->new_context(size,num_threads);
151 auto start_func = m_loop_splitter->get_start_func(context);
152 auto advance_func = m_loop_splitter->get_advance_func(context);
154 m_parallel_dispatcher->for_each(func,
155 [&](
int q) {
return start_func(context,q); },
156 [&](
size_t &n,
int q){
return advance_func(context,n,q); },
159 m_loop_splitter->delete_context(context);
161 for(
size_t n=0; n<size; ++n ) {
175 void for_each(
size_t size, std::function<
void(
size_t n)> func )
const {
176 for_each(size,[&](
size_t n,
int thread_n ) {
190 void for_each(
const shape2 &shape, std::function<
void(
int i,
int j,
int thread_index)> func )
const {
192 vec2i pi = shape.decode(n);
193 func(pi[0],pi[1],thread_index);
204 void for_each(
const shape2 &shape, std::function<
void(
int i,
int j)> func )
const {
205 for_each(shape,[&](
int i,
int j,
int thread_index ) {
217 void for_each(
const shape3 &shape, std::function<
void(
int i,
int j,
int k,
int thread_index)> func )
const {
219 vec3i pi = shape.decode(n);
220 func(pi[0],pi[1],pi[2],thread_index);
231 void for_each(
const shape3 &shape, std::function<
void(
int i,
int j,
int k)> func )
const {
232 for_each(shape,[&](
int i,
int j,
int k,
int thread_index ) {
239 std::string m_parallel_name, m_splitter_name;
240 parallel_ptr m_parallel_dispatcher;
241 loop_splitter_ptr m_loop_splitter;
242 int m_maximal_threads {NUM_THREAD};
246 m_parallel_dispatcher = parallel_core::quick_load_module(config,m_parallel_name);
247 m_loop_splitter = loop_splitter::quick_load_module(config,m_splitter_name);
252 config.
get_integer(
"Threads",m_maximal_threads,
"Number of maximal threads");
253 m_parallel_dispatcher->recursive_configure(config);
254 m_loop_splitter->recursive_configure(config);
Class that facilitates the use of parallel_core class for parallel loop.
Definition: parallel_driver.h:44
void for_each(const shape2 &shape, std::function< void(int i, int j)> func) const
Perform a two dimensional parallel loop operation.
Definition: parallel_driver.h:204
virtual void setup_now(configuration &config=get_global_configuration())
Run load - configure - initialize processes.
Definition: configurable.h:95
bool get_integer(std::string name, int &value, std::string description=std::string())
Get the integer parameter.
size_t count() const
Count the number of cells of the grid of this shape.
Definition: shape.h:857
Structure that defines a three dimensional shape such as width, height and depth.
Definition: shape.h:478
#define ARGUMENT_NAME(argument_name)
Macro that defines the argument name.
Definition: credit.h:40
size_t count() const
Count the number of cells of the grid of this shape.
Definition: shape.h:385
Class for managing the workflow of load - configure - initialize.
Definition: configurable.h:40
const parallel_core * get() const
Get a pointer to the internal instance of parallel_core.
Definition: parallel_driver.h:98
Abstract class that handles parallel operations. Used with loop_splitter. "stdthread" and "tbbthread"...
Definition: parallel_core.h:40
void for_each(size_t size, std::function< void(size_t n)> func) const
Perform a parallel loop operation.
Definition: parallel_driver.h:175
int get_thread_num() const
Get the number of maximal threads set.
Definition: parallel_driver.h:80
#define SHKZ_BEGIN_NAMESPACE
Name space beggining definition for shiokaze.
Definition: common.h:39
void run(const std::vector< std::function< void()> > &functions)
Run operations in parallel.
Definition: parallel_driver.h:107
void set_thread_num(int maximal_threads)
Set the number of maximal threads set.
Definition: parallel_driver.h:89
#define LONG_NAME(long_name)
Macro that defines the full name.
Definition: credit.h:37
void for_each(const shape3 &shape, std::function< void(int i, int j, int k, int thread_index)> func) const
Perform a three dimensional parallel loop operation.
Definition: parallel_driver.h:217
Extended configurable class that holds multiple children of configurable.
Definition: configurable.h:126
Class that automates the push and pop groups.
Definition: configuration.h:107
void for_each_byte_safe(size_t size, std::function< void(size_t n, int thread_index)> func) const
Perform a parallel loop operation. Thread is guaranteed the same if (n mod 8) is the same.
Definition: parallel_driver.h:122
Class that defines the name, argument name, author's name, email address, date and the version of the...
Definition: credit.h:47
Class that controls the settings of the program.
Definition: configuration.h:39
void for_each(const shape2 &shape, std::function< void(int i, int j, int thread_index)> func) const
Perform a two dimensional parallel loop operation.
Definition: parallel_driver.h:190
Structure that defines shape such as width, height.
Definition: shape.h:42
#define SHKZ_END_NAMESPACE
Name space end definition for shiokaze.
Definition: common.h:44
void for_each(size_t size, std::function< void(size_t n, int thread_index)> func) const
Perform a parallel loop operation.
Definition: parallel_driver.h:141
parallel_driver(std::string parallel_name=shkz_default_parallel_name, std::string splitter_name=shkz_default_splitter_name)
Constructor for parallel_driver.
Definition: parallel_driver.h:71
void for_each(const shape3 &shape, std::function< void(int i, int j, int k)> func) const
Perform a three dimensional parallel loop operation.
Definition: parallel_driver.h:231