C 错误输出中的简单 OpenMP For 循环
Simple OpenMP For Loop in C wrong output
试图让一个简单的 OpenMP 循环运行,但我总是得到奇怪的输出。它没有直接从 1 到 1000 列出,而是从 501 到 750,然后从 1 到 1000。我猜这是线程问题?我正在 VS2013 上编译和运行。
#include <stdio.h>
#include <math.h>
int main(void)
{
int counter = 0;
double root = 0;
// OPEN MP SECTION
printf("Open MP section: \n\n");
getchar(); //Pause
#pragma omp parallel for
for (counter = 0; counter <= 1000; counter++)
{
root = sqrt(counter);
printf("The root of %d is %.2f\n", counter, root);
}
return(0);
}
将结果存储在一个数组中,并使 printf
退出循环。它必须序列化到显示器。
OpenMP 的全部 要点 是 运行 并行,将工作分配给不同的执行引擎。
因此,您的循环的各个迭代很可能是乱序完成的,因为这是多线程的本质。
虽然 计算 并行完成可能有意义(因此可能会乱序),但这并不是您真正想要的 打印 个结果。
确保结果按正确顺序打印的一种方法是将打印推迟到 并行执行完成之后。换句话说,并行化计算但串行化输出。
这当然意味着能够将信息存储在例如数组中,而并行操作是 运行ning。
换句话说,类似于:
// Make array instead of single value.
double root[1001];
// Parallelise just the calculation bit.
#pragma omp parallel for
for (counter = 0; counter <= 1000; counter++)
root[counter] = sqrt(counter);
// Leave the output as a serial operation,
// once all parallel operations are done.
for (counter = 0; counter <= 1000; counter++)
printf("The root of %d is %.2f\n", counter, root[counter]);
您的代码不会运行顺序。
OpenMP 并行编译指示:
#pragma omp parallel
{
// Code inside this region runs in parallel.
printf("Hello!\n");
}
'This code creates a team of threads, and each thread executes the same code. It prints the text "Hello!" followed by a newline, as many times as there are threads in the team created. For a dual-core system, it will output the text twice. (Note: It may also output something like "HeHlellolo", depending on system, because the printing happens in parallel.) At the }, the threads are joined back into one, as if in non-threaded program."'
试图让一个简单的 OpenMP 循环运行,但我总是得到奇怪的输出。它没有直接从 1 到 1000 列出,而是从 501 到 750,然后从 1 到 1000。我猜这是线程问题?我正在 VS2013 上编译和运行。
#include <stdio.h>
#include <math.h>
int main(void)
{
int counter = 0;
double root = 0;
// OPEN MP SECTION
printf("Open MP section: \n\n");
getchar(); //Pause
#pragma omp parallel for
for (counter = 0; counter <= 1000; counter++)
{
root = sqrt(counter);
printf("The root of %d is %.2f\n", counter, root);
}
return(0);
}
将结果存储在一个数组中,并使 printf
退出循环。它必须序列化到显示器。
OpenMP 的全部 要点 是 运行 并行,将工作分配给不同的执行引擎。
因此,您的循环的各个迭代很可能是乱序完成的,因为这是多线程的本质。
虽然 计算 并行完成可能有意义(因此可能会乱序),但这并不是您真正想要的 打印 个结果。
确保结果按正确顺序打印的一种方法是将打印推迟到 并行执行完成之后。换句话说,并行化计算但串行化输出。
这当然意味着能够将信息存储在例如数组中,而并行操作是 运行ning。
换句话说,类似于:
// Make array instead of single value.
double root[1001];
// Parallelise just the calculation bit.
#pragma omp parallel for
for (counter = 0; counter <= 1000; counter++)
root[counter] = sqrt(counter);
// Leave the output as a serial operation,
// once all parallel operations are done.
for (counter = 0; counter <= 1000; counter++)
printf("The root of %d is %.2f\n", counter, root[counter]);
您的代码不会运行顺序。
OpenMP 并行编译指示:
#pragma omp parallel
{
// Code inside this region runs in parallel.
printf("Hello!\n");
}
'This code creates a team of threads, and each thread executes the same code. It prints the text "Hello!" followed by a newline, as many times as there are threads in the team created. For a dual-core system, it will output the text twice. (Note: It may also output something like "HeHlellolo", depending on system, because the printing happens in parallel.) At the }, the threads are joined back into one, as if in non-threaded program."'