如何完成所有过程,然后使用MPI在C中进行串行工作?

发布于 2025-01-23 18:37:41 字数 744 浏览 2 评论 0原文

我已经使用MPI编写了一个多线程C程序,以在2D char阵列中查找palidromes。我开始4个线程。现在,在所有线程完成后,我想杀死线程并继续串行工作吗?我该如何实现?该代码看起来像这样:

int main(int argc, char *argv[]) {
    // define some varibles
    int foo;

    // Kick off parallel work
    MPI_Init (&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    if (rank == 0) {
        // some work ...
    }
    if (rank == 1) {
        // some work ...
    }
    if (rank == 2) {
        // some work ...
    }
    if (rank == 3) {
        // some work ...
    }

    MPI_Finalize();

    // Here I want to print some results AFTER all threads are finished. E.g deallocate memory etc
    printf("Found: %d", foo);

    return 0;
}

I have written a multithreaded C program using MPI to find palidromes in a 2D char array. I start 4 threads. Now after all threads are finished, I want to kill the threads and continue serial work? How do I achieve this? The code looks something like this:

int main(int argc, char *argv[]) {
    // define some varibles
    int foo;

    // Kick off parallel work
    MPI_Init (&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 

    if (rank == 0) {
        // some work ...
    }
    if (rank == 1) {
        // some work ...
    }
    if (rank == 2) {
        // some work ...
    }
    if (rank == 3) {
        // some work ...
    }

    MPI_Finalize();

    // Here I want to print some results AFTER all threads are finished. E.g deallocate memory etc
    printf("Found: %d", foo);

    return 0;
}

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

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

发布评论

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

评论(1

江南烟雨〆相思醉 2025-01-30 18:37:41

您对MPI的工作原理感到困惑。 MPI不使用线程,而是使用独立的过程。每个过程都执行整个主要程序。

*脚注:该标准实际上允许不同的模型,但实际上这是真的。

You're confused about how MPI works. MPI does not use threads, it uses independent processes. Each process executes the whole of the main program.* So if you want only one process to do work, you have put a condition if (i_am_working_process) around that code.

*footnote: The standard actually allows different models, but in practice this is true.

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