Home > front end >  How can I execute two for loops in parallel in C ?
How can I execute two for loops in parallel in C ?

Time:01-31

In C I would like two for loops to execute at the same time and not have one wait for the other one to go first or wait for it to end.
I would like the two for loops (or more) to finish the loops in the same speed it would take one loop of the same size to finish.

I know it's been asked and answered, but not in an example this simple. I'm hoping to solve this specific problem. I worked combinations of pragma omp code examples and couldn't get the result.

#include <iostream>
using namespace std;

#define N 5

int main(void) { 
    int i;
    for (i = 0; i < N; i  ) {
        cout << "This is line ONE \n";
    };

    #pragma omp parallel
    #pragma omp for             
    for (i = 0; i < N; i  ) {
        cout << "This is line TWO \n";
    };
};

Compiling
$ g parallel.cpp -fopenmp && ./a.out

The output of the code is this, in the time it takes to run two loops...

This is line ONE
This is line ONE
This is line ONE
This is line ONE
This is line ONE
This is line TWO
This is line TWO
This is line TWO
This is line TWO
This is line TWO  

The output I would like is this They don't have to print one after the other like this, but I would think they would if they were both getting to the print part of the loops at the same times. What I really need is for the loops to start and finish at the same time (with the loops being equal).

This is line ONE
This is line TWO
This is line ONE
This is line TWO
This is line ONE
This is line TWO
This is line ONE
This is line TWO
This is line ONE
This is line TWO

There's this Q&A here, but I don't quite understand the undeclared foo and the //do stuff with item parts. What kinda stuff? What item? I have not been able to extrapolate from examples online to make what I need happen.

CodePudding user response:

As already mentioned in the comments that OpenMP may not be the best solution to do so, but if you wish to do it with OpenMP, I suggest the following:

Use sections to start 2 threads, and communicate between the threads by using shared variables. The important thing is to use atomic operation to read (#pragma omp atomic read seq_cst) and to write (#pragma omp atomic write seq_cst) these variables. Here is an example:

#pragma omp parallel num_threads(2)
#pragma omp sections
{                     
    #pragma omp section
    {
        //This is the sensor controlling part
            
        while(exit_condition)
        {
            sensor_state = read_sensor(); 
            
            // Read the currect state of motor from other thread
            #pragma omp atomic read seq_cst
            motor_state=shared_motor_state;
            
            // Based on the motor_state and sensor state send
            // a command to the other thread to control the motor
            // or wait for the motor to be ready in a loop, etc.
            
            #pragma omp atomic write seq_cst
            shared_motor_command= //whaterver you wish ;
        }
    }

    #pragma omp section
    {
        //This is the motor controlling part           
        while(exit_condition)
        {
            // read motor command form other thread
            #pragma omp atomic read seq_cst
            motor_command = shared_motor_command;

            // Do whatewer you have to to based on motor command and
            // You can set the state of motor by the following line

            #pragma omp atomic write seq_cst
            shared_motor_state= //what you need to pass to the other thread


        }
    }
}

CodePudding user response:

I think the issue is that you are not trying to parallelize two loops but instead you try to parallelize the work of one loop. If you would add std::cout << "Hello from thread: " << omp_get_thread_num() << "\n"; to your second loop you would see:

This is line TWO
Hello from thread: 0
This is line TWO
Hello from thread: 1
This is line TWO
Hello from thread: 2
This is line TWO
Hello from thread: 3
This is line TWO  
Hello from thread: 0

Depending on the assignment to threads, with four threads being the default amount of threads (often number of cores), the order might vary: example (0,1,2,3,0) could be (0,2,3,1,0)

So what you do is that the first loop is run in serial and then (4 or more/less) threads run the second loop in parallel.

The question is if you REALLY want to use OpenMP to parallelize your code. If so you could do something similar to:

#include <iostream>
#include <omp.h>
#include <String.h>


int main() {

    #pragma omp parallel for schedule(static)
    for(int i = 0; i < 10; i  ){
        int tid = omp_get_thread_num();
        if (tid%2==0) {
            std::cout << "This is line ONE" << "\n";
        } else {
            std::cout << "This is line TWO" << "\n";
        }
    }
    return 0;
}

Where based on the threadID - if it is an even thread it will do task 1, if it is an uneven thread it will do task 2. But as many other commenters have commented, maybe you should consider using p_threads depending on the task.

  •  Tags:  
  • Related