C++ OpenMP 程序

发布于 2024-10-11 08:08:02 字数 850 浏览 0 评论 0原文

我试图使用以下代码在 C++ 程序中获得并行效果:

#include<iostream>
using namespace std;

int main()
{

#pragma omp parallel sections
    {
#pragma omp section
        {
        cout<<"Hello";  
        cout<<" ";  
        cout<<"World";  
        cout<<endl;

        }
#pragma omp section
        {
        cout<<"H";
        cout<<"ello";
        cout<<" W";
        cout<<"orld";
        cout<<endl;
        }
#pragma omp section
        cout<<"Hello"<<" "<<"World"<<endl;
#pragma omp section
{       cout<<"Hello ";
        cout<<"World"<<endl;
}
    }
    return 0;
}

我已经多次运行该程序。由于并行性,我期望交错输出。

但是,每次运行该程序时,输出都是:

Hello World
Hello World
Hello World
Hello World

我在做什么吗有事吗?

谢谢

I am trying to get a parallel effect in C++ program using the following code:

#include<iostream>
using namespace std;

int main()
{

#pragma omp parallel sections
    {
#pragma omp section
        {
        cout<<"Hello";  
        cout<<" ";  
        cout<<"World";  
        cout<<endl;

        }
#pragma omp section
        {
        cout<<"H";
        cout<<"ello";
        cout<<" W";
        cout<<"orld";
        cout<<endl;
        }
#pragma omp section
        cout<<"Hello"<<" "<<"World"<<endl;
#pragma omp section
{       cout<<"Hello ";
        cout<<"World"<<endl;
}
    }
    return 0;
}

I had run this program many times.. I was expecting interleaved output due to parallelism..

However, every time I run this program the output is:

Hello World
Hello World
Hello World
Hello World

Am I doing something wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-10-18 08:08:02

你是如何编译该程序的? OpenMP 是否已激活?

除此之外,如果你想获得交错字符,一个更简单的 Hello World 看起来像这样:

int main() {
    char const* str = "Hello world";
    unsigned const len = std::strlen(str);
    #pragma omp parallel for num_threads(4)
    for (unsigned thread = 0; thread < 4; ++thread)
        for (unsigned i = 0; i < len; ++i)
            std::cout << str[i] << std::endl;
}

How have you compiled the program? Is OpenMP activated?

Apart from that, a much simpler Hello World looks like this, if you want to get interleaving characters:

int main() {
    char const* str = "Hello world";
    unsigned const len = std::strlen(str);
    #pragma omp parallel for num_threads(4)
    for (unsigned thread = 0; thread < 4; ++thread)
        for (unsigned i = 0; i < len; ++i)
            std::cout << str[i] << std::endl;
}
半暖夏伤 2024-10-18 08:08:02

代码是正确的,但是很难从这样的小程序中获得交错输出。尝试在输出语句之间放置一些对 sleep 或类似的调用,并进行一些刷新。

(您是否使用 -openmp-fopenmp 或您的编译器想要听到的任何内容进行编译和链接?)

The code is correct, but interleaved output can be hard to get from such small programs. Try putting some calls to sleep or similar between output statements and do some flushing.

(Did you compile and link with -openmp, -fopenmp, or whatever your compiler want to hear?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文