如何完成所有过程,然后使用MPI在C中进行串行工作?
我已经使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对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.