Coder Social home page Coder Social logo

tnagler / quickpool Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 4.0 672 KB

Fast and easy parallel computing in C++11

CMake 3.16% C++ 96.84%
thread pool stealing async lock-free threadpool cpp concurrency cpp11 header-only multithreading parallelfor parallel-computing

quickpool's Introduction

quickpool

build status Codacy Badge codecov License: MIT Documentation DOI

Fast and easy parallel computing in C++11

Why quickpool?

Developer friendly

The library consists of a single header file with permissive license. It requires only C++11 and is otherwise self-contained. Just drop quickpool.hpp in your project folder and enjoy.

User friendly API

Loops can be nested, see the examples below. All functions dispatch to a global thread pool instantiated only once with as many threads as there are cores. Optionally, one can create a local ThreadPool exposing the functions above. See also the API documentation.

Cutting edge algorithms

All scheduling uses work stealing synchronized by cache-aligned atomic operations.

The thread pool assigns each worker thread a task queue. The workers process first their own queue and then steal work from others. The algorithm is lock-free in the standard case where only a single thread pushes work to the pool.

Parallel loops assign each worker part of the loop range. When a worker completes its own range, it steals half the range of another worker. This perfectly balances the load and only requires a logarithmic number of steals (= points of contention). The algorithm uses double-wide compare-and-swap, which is lock-free on most modern processor architectures.

Examples

Thread pool

push([] { /* some work */ });
wait(); // wait for current jobs to finish

auto f = async([] { return 1 + 1; }); // get future for result
// do something else ...
auto result = f.get(); // waits until done and returns result

Both push() and async() can be called with extra arguments passed to the function.

auto work = [] (const std::string& title, int i) { 
  std::cout << title << ": " << i << std::endl; 
};
push(work, "first title", 5);
async(work, "other title", 99);
wait();

Parallel loops

Existing sequential loops are easy to parallelize:

std::vector<double> x(10, 1);

// sequential version
for (int i = 0; i < x.size(); ++i) 
  x[i] *= 2;

// parallel version
parallel_for(0, x.size(), [&] (int i) { x[i] *= 2; };


// sequential version
for (auto& xx : x) 
  xx *= 2;

// parallel version
parallel_for_each(x, [] (double& xx) { xx *= 2; };

The loop functions automatically wait for all jobs to finish, but only when called from the main thread.

Nested parallel loops

It is possible to nest parallel for loops, provided that we don't need to wait for inner loops.

std::vector<double> x(10, 1);

// sequential version
for (int i = 0; i < x.size(); ++i) {
  for (int j = 4; j < 9; j++) {
    x[i] *= j;
  }
}

// parallel version
parallel_for(0, x.size(), [&] (int i) { 
  // *important*: capture i by copy
  parallel_for(4, 9, [&, i] (int j) {  x[i] *= j; }); // doesn't wait
}; // does wait

Local thread pool

A ThreadPool can be set up manually, with an arbitrary number of threads. When the pool goes out of scope, all threads joined.

ThreadPool pool(2); // thread pool with two threads

pool.push([] {});
pool.async([] {});
pool.wait();

pool.parallel_for(2, 5, [&] (int i) {});
auto x = std::vector<double>{10};
pool.parallel_for_each(x, [] (double& xx) {});

quickpool's People

Contributors

davidebeatrici avatar krzmbrzl avatar tnagler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

quickpool's Issues

This is a great library, but it can't be compiled normally in visual studio

Error reported after include quickpool.hpp

`Build started

1> ------ build started: Project: basic, configuration: debug Win32------

1>basic_ form. cpp

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (714,27): error c2440: "static_cast": unable to convert from "quickpool:: MEM:: aligned:: allocator < quickpool:: sched:: taskqueue, 64 >" to "quickpool:: mem:: aligned:: allocator < u, 64 >"

1> with

1> [

1> U=std::_ Container_ proxy

1> ]

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (714,27): Message: no constructor can accept the source type, or the constructor overload decision is unclear

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (711): Message: when compiling class template member functions "STD:: vector < quickpool:: sched:: taskqueue, quickpool:: MEM:: aligned:: allocator < quickpool:: sched:: taskqueue, 64 > >: ~ vector (void) noexcept"

1>F:\CppPro\test\02-basic\quickpool. HPP (553): Message: view the reference to the function template being compiled instantiating "STD:: vector < quickpool:: sched:: taskqueue, quickpool:: MEM:: aligned:: allocator < quickpool:: sched:: taskqueue, 64 > >: ~ vector (void) noexcept"

1>F:\CppPro\test\02-basic\quickpool. HPP (729): Message: view the reference to the class template being compiled instantiating "STD:: vector < quickpool:: sched:: taskqueue, quickpool:: MEM:: aligned:: allocator < quickpool:: sched:: taskqueue, 64 > >"

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (714,25): error c2530: "_alproxy": reference must be initialized

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (715,1): error c3536: "_alproxy": cannot be used before initialization

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (715,9): error c2672: "_delete_plain_internal": no matching overloaded function found

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (715,1): error c2893: failed to specialize the function template "void STD:: _delete_plain_internal (_alloc &, _alloc:: value_type * const) noexcept"

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ xmemory (998): Message: see the statement of "STD:: _delete_plain_internal"

1> F: \ CPP \ VisualStudio \ ide \ VC \ tools \ MSVC \ 14.29.30133 \ include \ vector (715,1): Message: use the following template parameters:

1>F:\CPP\VisualStudio\IDE\VC\Tools\MSVC\14.29.30133\include\vector(715,1): message : β€œ_Alloc=int”

1> Finished building project 'basic. Vcxproj' - failed.

==========Generation: 0 successes, 1 failures, 0 latest, 0 skipped==========`

MSVC warnings

From https://github.com/tnagler/quickpool/runs/5404950620:

test.cpp(90,42): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
test.cpp(104,47): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
test.cpp(125,1): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
test.cpp(126,14): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
test.cpp(141,1): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
test.cpp(142,14): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
quickpool.hpp(919,31): warning C4244: 'argument': conversion from '__int64' to 'int', possible loss of data [build\quickpool_test.vcxproj]
test.cpp(174): message : see reference to function template instantiation 'void quickpool::ThreadPool::parallel_for_each<std::vector<size_t,std::allocator<size_t>>,main::<lambda_37fca2606d1d166ca50f88b50c00b954>>(Items &,UnaryFunction)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              Items=std::vector<size_t,std::allocator<size_t>>,
              UnaryFunction=main::<lambda_37fca2606d1d166ca50f88b50c00b954>
          ]
quickpool.hpp(182,1): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data [build\quickpool_test.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.31.31103\include\xmemory(572): message : see reference to function template instantiation 'void quickpool::mem::aligned::allocator<quickpool::loop::Worker<UnaryFunction>,64>::construct<_Ty,unsigned __int64,unsigned __int64,const Function&>(U *,unsigned __int64 &&,unsigned __int64 &&,const Function &)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              UnaryFunction=main::<lambda_911885787203d9d13cac60ed82b30719>,
              _Ty=quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>,
              U=quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>
          ]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.31.31103\include\xmemory(572): message : see reference to function template instantiation 'void quickpool::mem::aligned::allocator<quickpool::loop::Worker<UnaryFunction>,64>::construct<_Ty,unsigned __int64,unsigned __int64,const Function&>(U *,unsigned __int64 &&,unsigned __int64 &&,const Function &)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              UnaryFunction=main::<lambda_911885787203d9d13cac60ed82b30719>,
              _Ty=quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>,
              U=quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>
          ]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.31.31103\include\vector(607): message : see reference to function template instantiation 'void std::_Normal_allocator_traits<_Alloc>::construct<_Ty,unsigned __int64,unsigned __int64,const Function&>(_Alloc &,_Ty *,unsigned __int64 &&,unsigned __int64 &&,const Function &)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              _Alloc=quickpool::mem::aligned::allocator<quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>,64>,
              _Ty=quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>
          ]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.31.31103\include\vector(607): message : see reference to function template instantiation 'void std::_Normal_allocator_traits<_Alloc>::construct<_Ty,unsigned __int64,unsigned __int64,const Function&>(_Alloc &,_Ty *,unsigned __int64 &&,unsigned __int64 &&,const Function &)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              _Alloc=quickpool::mem::aligned::allocator<quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>,64>,
              _Ty=quickpool::loop::Worker<main::<lambda_911885787203d9d13cac60ed82b30719>>,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>
          ]
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.31.31103\include\vector(625): message : see reference to function template instantiation 'void std::vector<quickpool::loop::Worker<UnaryFunction>,quickpool::mem::aligned::allocator<quickpool::loop::Worker<UnaryFunction>,64>>::_Emplace_back_with_unused_capacity<_Ty,_Ty,const Function&>(_Ty &&,_Ty &&,const Function &)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              UnaryFunction=main::<lambda_911885787203d9d13cac60ed82b30719>,
              _Ty=size_t,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>
          ]
quickpool.hpp(384): message : see reference to function template instantiation 'void std::vector<quickpool::loop::Worker<UnaryFunction>,quickpool::mem::aligned::allocator<quickpool::loop::Worker<UnaryFunction>,64>>::emplace_back<size_t,size_t,const Function&>(size_t &&,size_t &&,const Function &)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              UnaryFunction=main::<lambda_911885787203d9d13cac60ed82b30719>,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>
          ]
quickpool.hpp(898): message : see reference to function template instantiation 'std::shared_ptr<std::vector<quickpool::loop::Worker<UnaryFunction>,quickpool::mem::aligned::allocator<quickpool::loop::Worker<UnaryFunction>,64>>> quickpool::loop::create_workers<UnaryFunction>(const Function &,int,int,size_t)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              UnaryFunction=main::<lambda_911885787203d9d13cac60ed82b30719>,
              Function=main::<lambda_911885787203d9d13cac60ed82b30719>
          ]
test.cpp(104): message : see reference to function template instantiation 'void quickpool::ThreadPool::parallel_for<main::<lambda_911885787203d9d13cac60ed82b30719>>(int,int,UnaryFunction)' being compiled [build\quickpool_test.vcxproj]
          with
          [
              UnaryFunction=main::<lambda_911885787203d9d13cac60ed82b30719>
          ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.