Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadPool implemented with OpenMP

I need to implement a thread pool executor for generic tasks. My idea is to use OpenMP for thread management. The problem is that I'm not familar yet with OpenMP..

I tried to find an existing implementation of the generic ThreadPool with OpenMP, but I haven't find one so far. What I would like to have in the end is something very similar to java.util.concurrent.ThreadPoolExecutor:

template <typename Return, typename Task>
class ThreadPoolExecutor
{
public:
    ThreadPoolExecutor(int threadCount);

    // asyncronous invoke
    boost::unique_future<Return> submit(const TaskPtr & task);

    // blocking call
    std::vector<Return> invokeAll(const std::vector<TaskPtr> & tasks)
    {
        // submit all tasks + wait for completion
        #pragma omp parallel default(shared)
        {
            // call tasks here
        }
    }
};

I have several questions about this approach:

  1. Is there existing implementation of thread pool with OpenMP for C++? [I know I can implement threadpool with boost::asio::io_service, but I would prefer not depend on it]

  2. With my design - how can we guarantee that the OpenMP threads will be 'owned' by the concrete ThreadPoolExecutor instance? They should not be static for all instances.

Thank you for any advice & constructive critics of this approach & proposals of other implementations.

like image 943
nogard Avatar asked Nov 04 '22 16:11

nogard


1 Answers

Just to sum up: as described in the comments OpenMP is not an option for implementing generic thread pool or executor, because:

  1. OpenMP is strictly a fork/join threading model.
  2. You can't assign the owner for OpenMP threads
  3. No control over threads and internal threadpool
like image 185
nogard Avatar answered Nov 15 '22 06:11

nogard