admin管理员组文章数量:1399509
The following code gives multiple outputs
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
As per my understanding only the for loop will be run parallel and only one output should come. Why I am getting same output many times.
#include <iostream>
#include <omp.h>
int main() {
int n = 100000;
long long sum = 0;
#pragma omp parallel for reduction(+: sum)
for (int i = 1; i <= n; ++i) {
sum += i;
}
std::cout << "Sum of first " << n << " natural numbers: " << sum << std::endl;
return 0;
}
The following code gives multiple outputs
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
Sum of first 100000 natural numbers: 5000050000
As per my understanding only the for loop will be run parallel and only one output should come. Why I am getting same output many times.
#include <iostream>
#include <omp.h>
int main() {
int n = 100000;
long long sum = 0;
#pragma omp parallel for reduction(+: sum)
for (int i = 1; i <= n; ++i) {
sum += i;
}
std::cout << "Sum of first " << n << " natural numbers: " << sum << std::endl;
return 0;
}
Share
Improve this question
edited Mar 25 at 5:37
wohlstad
29.9k17 gold badges61 silver badges93 bronze badges
asked Mar 25 at 5:09
Anjeneya Swami KareAnjeneya Swami Kare
1
8
|
Show 3 more comments
1 Answer
Reset to default 2As many commenters have already pointed out, you are confusing MPI
(the Message Passing Interface), which is used for large scale parallelism between completely separate computers which do not share memory, and OpenMP which is used for parallelism within a single machine that can support multiple hardware threads. (You have maybe been confused by the fact that one of the MPI implementations is called "Open MPI" [for which blame the people who chose that name!]).
Since your code is solely OpenMP; you are not including <mpi.h>
or calling any MPI functions (which begin with the prefix MPI_
) you should not be executing your code using mpirun
but rather just treating it as a normal executable to run on the current node. So, just suing something like:-
% ./a.out
Because you are running with mpirun
(or one of the batch systems that support MPI), multiple copies of your code are being executed on multiple nodes, and each copy prints the output.
本文标签: openmpGetting multiple outputs on Open MPI C CodeStack Overflow
版权声明:本文标题:openmp - Getting multiple outputs on Open MPI C++ Code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744216904a2595680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
mpiexec -n 1 a.out
will output a single line, but this is unlikely what you are trying to achieve. – Gilles Gouaillardet Commented Mar 25 at 8:53