Using multicores with OpenMP and Coroutine2

Open
#49 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
20/100
Issue type
Documentation
Clarity
Needs clarification
Activity status
Stale
Tech stack
cpp

Research direction

Start with the simplified program in main and its OpenMP parallel loop, then review the Boost.Coroutine2 and OpenMP interaction described in the issue. A useful result would document whether this combination is supported, explain the observed single-core behavior, and provide an example or clear guidance using the shown compilation setup.

Written by the indexing model from the issue text.

Description

Hello,

I am currently modeling multiple producers using Boost.Coroutine2 and storing them in a container (std::vector). My goal is to leverage OpenMP to accelerate the calls of these coroutines on multiple cores.

However, I am encountering an issue where only one core is running the program. I have been searching for documentation on the combination of OpenMP and Coroutine2 but have not found examples or advice.

Below is a simplified version of my code:

#include <iostream>
#include <boost/coroutine2/all.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#define N 10000000

using namespace boost::coroutines2;
using CoroutinePush = coroutine<void>::push_type;
using CoroutinePull = coroutine<void>::pull_type;
using Funptr = boost::function<void(CoroutinePush&)>;

class Env{
        public:
        long x=0;
};


void producer(CoroutinePush& yield, Env* env) {
    yield(); // init
    for (int i = 0; i < N; ++i) {
        env->x++; // some work here
        yield();  // Yield the produced value
    }
}


int main() {
    Env* env=new Env();
    Funptr producerFunction = boost::bind(&producer, _1, env);

    std::vector<CoroutinePull*> l;
    for(int i=0;i<N;i++){
        l.push_back(new CoroutinePull(producerFunction));
    }


    #pragma omp parallel for
    for(int i=0;i<N;i++){
            // #pragma omp critical // if it's uncommented  x becomes predictable but 1 thread at a time
            {
            (*l[i])();
            }
    }

    std::cout << "x:" << env->x << std::endl;


    for(int i = 0; i < N; i++) {
        delete l[i];
    }
    delete env;
    return 0;
}

My compilation line: g++ -std=c++20 -fopenmp ./g.cpp -lboost_context -lboost_coroutine

I would greatly appreciate any guidance, examples, or advice on how to use Boost.Coroutine2 and OpenMP for multicore execution.

Thank you

Dominant language
C++
Stars
140
Forks
44
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/coroutine2

All issues in boostorg/coroutine2

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.