使用 MPI_Scather 后打印每个等级数组的好方法
A good way to print array of each rank after using MPI_Scather
我想以适当的方式打印每个等级的数组。首先,这段代码的问题是输出没有顺序,其次,我只是想取消使用 if 条件来打印每个等级的数据。可能吗?
#include <fstream>
#include <iostream>
#include <vector>
#include "mpi.h"
using namespace std;
const int N = 3;
int main()
{
MPI_Init(NULL, NULL);
int rank;
int size;
int root = 0;
vector<int> x(N);
vector<int> receive_data(N);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
const int leng = size * N;
vector<int> single_arr(leng);
for (int i = 0; i < N;i++) {
x[i] = 1000*rank + i;
}
MPI_Gather(x.data(), N, MPI_INT, single_arr.data(), N, MPI_INT, root, MPI_COMM_WORLD);
if (rank == root) {
cout << " After the gathering data inside single array at root is : " << endl;
for (int i = 0; i < single_arr.size();i++) {
cout << i << "\t" << single_arr[i] << endl;
}
}
MPI_Scatter(single_arr.data(),N, MPI_INT, receive_data.data(), N,
MPI_INT, root, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
// now checks for data of each rank:
if (rank == root) {
//cout << " After scattering the data: " << endl;
cout << " My rank is : " << rank << endl;
for (int i = 0; i < receive_data.size(); i++) {
cout << i << "\t" << receive_data[i] << endl;
cout << endl;
}
}
if (rank == 1) {
//cout << " After scattering the data: " << endl;
cout << " My rank is : " << rank<<"\t"<< endl;
for (int i = 0; i < receive_data.size(); i++) {
cout << i << "\t" << receive_data[i] << endl;
cout << endl;
}
}
MPI_Finalize();
}
结果是:
0 0
1 1
2 2
3 1000
4 1001
5 1002
6 2000
7 2001
8 2002
9 3000
10 3001
11 3002
My rank is : 0
0 0
My rank is : 1
1 1
0 1000
2 2
1 1001
2 1002
但是,我想在不对每个等级使用 if 条件的情况下看到这个:
My rank is : 0
0 0
1 1
2 2
My rank is: 1
0 1000
1 1001
2 1002
First, the problem with this code is that output is not ordered, and
second, I just want to eliminate the use of if the condition for
printing the data of each rank. Is it possible?
这不是 MPI 的用途,实际上是一般 IMO 中的并行性。在进程之间将输出打印到控制台的协调将大大降低并行版本的性能,这违背了并行的目的之一,即减少整体执行时间。
大多数时候最好让一个进程负责将输出打印到控制台(通常是 master 进程 即 处理 rank = 0
).
引用@Gilles Gouaillardet:
Your best bet is to have all the data sent to one rank and have this
rank print everything.
您可以尝试使用 MPI_Barrier 以打印您想要的输出的方式协调流程,但是(引用 Hristo Iliev):
Using barriers like that only works for local launches when (and if)
the processes share the same controlling terminal. Otherwise, it is
entirely to the discretion of the I/O redirection mechanism of the MPI
implementation.
我想以适当的方式打印每个等级的数组。首先,这段代码的问题是输出没有顺序,其次,我只是想取消使用 if 条件来打印每个等级的数据。可能吗?
#include <fstream>
#include <iostream>
#include <vector>
#include "mpi.h"
using namespace std;
const int N = 3;
int main()
{
MPI_Init(NULL, NULL);
int rank;
int size;
int root = 0;
vector<int> x(N);
vector<int> receive_data(N);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
const int leng = size * N;
vector<int> single_arr(leng);
for (int i = 0; i < N;i++) {
x[i] = 1000*rank + i;
}
MPI_Gather(x.data(), N, MPI_INT, single_arr.data(), N, MPI_INT, root, MPI_COMM_WORLD);
if (rank == root) {
cout << " After the gathering data inside single array at root is : " << endl;
for (int i = 0; i < single_arr.size();i++) {
cout << i << "\t" << single_arr[i] << endl;
}
}
MPI_Scatter(single_arr.data(),N, MPI_INT, receive_data.data(), N,
MPI_INT, root, MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
// now checks for data of each rank:
if (rank == root) {
//cout << " After scattering the data: " << endl;
cout << " My rank is : " << rank << endl;
for (int i = 0; i < receive_data.size(); i++) {
cout << i << "\t" << receive_data[i] << endl;
cout << endl;
}
}
if (rank == 1) {
//cout << " After scattering the data: " << endl;
cout << " My rank is : " << rank<<"\t"<< endl;
for (int i = 0; i < receive_data.size(); i++) {
cout << i << "\t" << receive_data[i] << endl;
cout << endl;
}
}
MPI_Finalize();
}
结果是:
0 0
1 1
2 2
3 1000
4 1001
5 1002
6 2000
7 2001
8 2002
9 3000
10 3001
11 3002
My rank is : 0
0 0
My rank is : 1
1 1
0 1000
2 2
1 1001
2 1002
但是,我想在不对每个等级使用 if 条件的情况下看到这个:
My rank is : 0
0 0
1 1
2 2
My rank is: 1
0 1000
1 1001
2 1002
First, the problem with this code is that output is not ordered, and second, I just want to eliminate the use of if the condition for printing the data of each rank. Is it possible?
这不是 MPI 的用途,实际上是一般 IMO 中的并行性。在进程之间将输出打印到控制台的协调将大大降低并行版本的性能,这违背了并行的目的之一,即减少整体执行时间。
大多数时候最好让一个进程负责将输出打印到控制台(通常是 master 进程 即 处理 rank = 0
).
引用@Gilles Gouaillardet:
Your best bet is to have all the data sent to one rank and have this rank print everything.
您可以尝试使用 MPI_Barrier 以打印您想要的输出的方式协调流程,但是(引用 Hristo Iliev):
Using barriers like that only works for local launches when (and if) the processes share the same controlling terminal. Otherwise, it is entirely to the discretion of the I/O redirection mechanism of the MPI implementation.