openmp - Getting multiple outputs on Open MPI C++ Code - Stack Overflow

The following code gives multiple outputsSum of first 100000 natural numbers: 5000050000Sum of first 1

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
  • 4 You are confusing MPI (Open MPI is an implementation of the MPI standard) and OpenMP. Choose one paradigm (before going hybrid if you want). When asking a question, also includes your run command line. – Gilles Gouaillardet Commented Mar 25 at 5:26
  • 3 Cannot reproduce on MSVC (VS2022) - I get only one output line. Which compiler are you using ? – wohlstad Commented Mar 25 at 5:35
  • 1 Side note : C++ also has multithreading support out of the box (std::thread), and there is a set of algorithms like std::accumulate that can be called with a parallel execution policy. So make clear in your question WHAT you are investigating. Is it the use of OMP or are you just interested in parallelization in general? – Pepijn Kramer Commented Mar 25 at 5:51
  • 1 @wohlstad he likely ran his program via mpirun, that's why you did not reproduce the output – Gilles Gouaillardet Commented Mar 25 at 6:44
  • 1 because you are not running an MPI program, you are simply running 6 independant instances of the same program. 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
 |  Show 3 more comments

1 Answer 1

Reset to default 2

As 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744216904a4563596.html

相关推荐

  • openmp - Getting multiple outputs on Open MPI C++ Code - Stack Overflow

    The following code gives multiple outputsSum of first 100000 natural numbers: 5000050000Sum of first 1

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信