如何在 C++ 中并行执行两个 for 循环?
How can I execute two for loops in parallel in C++?
在 C++ 中,我希望同时执行两个 for 循环,而不是让一个等待另一个先执行或等待它结束。
我希望两个 for 循环(或更多)以相同的速度完成循环,它需要一个相同大小的循环才能完成。
我知道有人问过也有人回答过,但不是在这么简单的例子中。我希望能解决这个具体问题。我使用了 pragma omp 代码示例的组合,但无法得到结果。
#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";
};
};
正在编译
$ g++ parallel.cpp -fopenmp && ./a.out
代码的输出是这样的,在 运行 两个循环所花费的时间内...
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
我想要的输出是这样的
他们不必像这样一个接一个地打印,但我认为如果他们同时进入循环的打印部分,他们会这样做。我真正需要的是让循环同时开始和结束(循环相等)。
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
有这个问答 ,但我不太明白未声明的 foo
和 //do stuff with item
部分。什么样的东西?什么项目?我无法从在线示例中推断出我需要的东西。
我认为问题在于您没有尝试并行化两个循环,而是尝试并行化一个循环的工作。如果将 std::cout << "Hello from thread: " << omp_get_thread_num() << "\n";
添加到第二个循环中,您会看到:
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
根据对线程的分配,四个线程是默认的线程数(通常是核心数),顺序可能会有所不同:例如 (0,1,2,3,0)
可能是 (0,2,3,1,0)
所以你要做的是第一个循环 运行 串行,然后(4 或 more/less)线程 运行 第二个并行循环。
问题是您是否真的想要使用 OpenMP 来并行化您的代码。如果是这样,你可以做类似的事情:
#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;
}
基于 threadID 的地方 - 如果它是偶数线程,它将执行任务 1,如果它是不均匀线程,它将执行任务 2。但是正如许多其他评论者评论的那样,也许您应该考虑使用 p_threads
取决于任务。
正如评论中已经提到的那样,OpenMP 可能不是这样做的最佳解决方案,但如果您希望使用 OpenMP 来实现,我建议如下:
使用sections
启动2个线程,线程之间使用共享变量进行通信。重要的是使用原子操作来读取(#pragma omp atomic read seq_cst
)和写入(#pragma omp atomic write seq_cst
)这些变量。这是一个例子:
#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
}
}
}
在 C++ 中,我希望同时执行两个 for 循环,而不是让一个等待另一个先执行或等待它结束。
我希望两个 for 循环(或更多)以相同的速度完成循环,它需要一个相同大小的循环才能完成。
我知道有人问过也有人回答过,但不是在这么简单的例子中。我希望能解决这个具体问题。我使用了 pragma omp 代码示例的组合,但无法得到结果。
#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";
};
};
正在编译
$ g++ parallel.cpp -fopenmp && ./a.out
代码的输出是这样的,在 运行 两个循环所花费的时间内...
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
我想要的输出是这样的 他们不必像这样一个接一个地打印,但我认为如果他们同时进入循环的打印部分,他们会这样做。我真正需要的是让循环同时开始和结束(循环相等)。
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
有这个问答 foo
和 //do stuff with item
部分。什么样的东西?什么项目?我无法从在线示例中推断出我需要的东西。
我认为问题在于您没有尝试并行化两个循环,而是尝试并行化一个循环的工作。如果将 std::cout << "Hello from thread: " << omp_get_thread_num() << "\n";
添加到第二个循环中,您会看到:
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
根据对线程的分配,四个线程是默认的线程数(通常是核心数),顺序可能会有所不同:例如 (0,1,2,3,0)
可能是 (0,2,3,1,0)
所以你要做的是第一个循环 运行 串行,然后(4 或 more/less)线程 运行 第二个并行循环。
问题是您是否真的想要使用 OpenMP 来并行化您的代码。如果是这样,你可以做类似的事情:
#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;
}
基于 threadID 的地方 - 如果它是偶数线程,它将执行任务 1,如果它是不均匀线程,它将执行任务 2。但是正如许多其他评论者评论的那样,也许您应该考虑使用 p_threads
取决于任务。
正如评论中已经提到的那样,OpenMP 可能不是这样做的最佳解决方案,但如果您希望使用 OpenMP 来实现,我建议如下:
使用sections
启动2个线程,线程之间使用共享变量进行通信。重要的是使用原子操作来读取(#pragma omp atomic read seq_cst
)和写入(#pragma omp atomic write seq_cst
)这些变量。这是一个例子:
#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
}
}
}