All Projects → yuki-koyama → parallel-util

yuki-koyama / parallel-util

Licence: MIT license
Simple header-only implementation of "parallel_for" and "parallel_map" for C++11

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to parallel-util

Arteries
A procedural modeling toolkit base on UE4 blueprint
Stars: ✭ 92 (+240.74%)
Mutual labels:  multi-thread
epump
ePump是一个基于I/O事件通知、非阻塞通信、多路复用、多线程等机制开发的事件驱动模型的 C 语言应用开发框架,利用该框架可以很容易地开发出高性能、大并发连接的服务器程序。
Stars: ✭ 26 (-3.7%)
Mutual labels:  multi-thread

parallel-util

Test

A single-header implementation of parallel_for, parallel_map, and parallel_exec using C++11.

This library is based on multi-threading on CPU (std::thread) and the default concurrency is set to the hardware concurrency (std::thread::hardware_concurrency()).

Usage of parallel_for

Suppose that you have a callable function that can be stored by an instance of std::function<void(int)>, for example, defined by C++11 lambda expression:

auto process = [](int i) { ... };

and want to parallelize the following for-loop procedure:

for (int i = 0; i < n; ++ i) { process(i); }

By using parallel-util, this can be easily parallelized by

parallelutil::parallel_for(n, process);

Usage of parallel_map

Suppose that you have a callable function that takes an instance of T1 as input and returns an instance of T2 as output, and thus can be stored by an instance of std::function<T2(T1)>. For example,

auto square = [](double x) { return x * x; };

In this case, T1 = T2 = double. Also suppose that you have an array of T1 and want to obtain an array of T2 by applying the function to each array element. For example, you have an array:

std::vector<double> input_array = { 0.2, 0.9, - 0.4, 0.5, 0.3 };

and want to their squares. By using parallel-util, this can be easily parallelized by

auto output_array = parallelutil::parallel_map(input_array, square);

where output_array is an array: { 0.04, 0.81, 0.16, 0.25, 0.09 }.

If you are using C++17 Parallel STL, std::transform has similar functionality.

Usage of parallel_exec

An arbitrary number of functions whose type is std::function<void()>, for example,

auto process_1 = [](){ ... };
auto process_2 = [](){ ... };
auto process_3 = [](){ ... };

can be executed in parallel by

parallelutil::parallel_exec({ process_1, process_2, process_3 });

Installation

parallel-util is a header-only, single-file library. It can be used by just copying parallel-util.hpp and pasting it into your project.

Alternatively, it can be installed using cmake. If your project is also managed using cmake, ExternalProject or add_subdirectory commands are useful for including parallel-util to your project.

If you want to install parallel-util to your system, use the typical cmake cycle:

git clone https://github.com/yuki-koyama/parallel-util.git
mkdir build
cd build
cmake ../parallel-util
make install

Dependencies

  • C++ Standard Library; Thread support library (require -pthread)

Persuing Further Performance

Please consider to use more sophisticated libraries such as Intel(R) Threading Building Blocks.

Projects using parallel-util

LICENSING

MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].