GithubHelp home page GithubHelp logo

mjjdick / ptl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jrmadsen/ptl

0.0 0.0 0.0 1018 KB

Parallel Tasking Library (PTL) - Lightweight C++11 mutilthreading tasking system featuring thread-pool, task-groups, and lock-free task queue

License: MIT License

C 0.35% Shell 1.19% CMake 16.52% C++ 78.41% Python 3.53%

ptl's Introduction

Parallel Tasking Library (PTL)

Lightweight C++11 multithreading tasking system featuring thread-pool, task-groups, and lock-free task queue

Basic Interface

#include "PTL/PTL.hh"

#include <cassert>

inline long
fibonacci(long n)
{
    return (n < 2) ? n : (fibonacci(n - 1) + fibonacci(n - 2));
}

int main()
{
    bool use_tbb     = false;
    auto num_threads = 4;
    auto run_manager = PTL::TaskRunManager(use_tbb);

    run_manager.Initialize(num_threads);

    auto* task_manager = run_manager.GetTaskManager();

    // add a task via the task manager
    auto baz = task_manager->async<long>(fibonacci, 40);

    // functor to combine results
    auto join = [](long& lhs, long rhs) { return lhs += rhs; };

    // create a task group for 10 fibonacci calculations
    PTL::TaskGroup<long> foo(join);
    for(uint64_t i = 0; i < 10; ++i)
        foo.exec(fibonacci, 40);

    // create a task group for 10 fibonacci calculations
    PTL::TaskGroup<void> bar{};

    long ret_bar = 0;
    auto run     = [&ret_bar](long n) { ret_bar += fibonacci(n); };
    for(uint64_t i = 0; i < 10; ++i)
        bar.exec(run, 40);

    auto ret_baz = baz->get();
    auto ret_foo = foo.join();
    bar.join();

    assert(ret_baz * 10 == ret_foo);
    assert(ret_baz * 10 == ret_bar);
    assert(ret_foo == ret_bar);
}

Explicit Thread-Pool

Using PTL::TaskRunManager is not necessary with task-groups. You can create new thread-pools directly and pass them to task-groups:

long example()
{
    // create a new thread-pool explicitly
    PTL::ThreadPool tp(4);

    // combines results
    auto join = [](long& lhs, long rhs) { return lhs += rhs; };

    // specify thread-pool explicitly
    PTL::TaskGroup<long> foo(join, &tp);

    for(int i = 0; i < 10; ++i)
        foo.exec(fibonacci, 40);

    // blocks until tasks in group are completed
    // thread-pool is destroyed after function returns
    return foo.get();
}

ptl's People

Contributors

amadio avatar badshah400 avatar carterbox avatar drbenmorgan avatar jrmadsen avatar slyon avatar stephanlachnit avatar

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.