MPI C 逐行向所有进程子进程发送一个矩阵 (MPI_COMM_SPAWN)
MPI C send a matrix line by line to all process childrens (MPI_COMM_SPAWN)
我有一个父进程和一个矩阵,我想为每一行创建一个子进程并将相应的行发送给进程。
父进程代码:
int tag = 0;
MPI_Status status;
int random(int n) {
return rand() % n;
}
float** generate_matrix(int n, int m) {
int i, j;
float **x;
x = (float **) malloc(m * sizeof(float));
for (i = 0; i < m; i++) {
x[i] = (float *) malloc(n * sizeof(float));
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
x[i][j] = random(100);
}
}
return x;
}
int main(int argc, char** argv) {
int my_rank;
int num_procs;
MPI_Comm workercomm;
int n = 4, m = 5;
float**matrix = generate_matrix(n, m);
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
MPI_Comm_spawn("C:/Users/colegnou/workspace/worker/Debug/worker.exe",
MPI_ARGV_NULL, m,
MPI_INFO_NULL, 0, MPI_COMM_SELF, &workercomm, MPI_ERRCODES_IGNORE);
for (int i = 0; i < m; i++) {
MPI_Bcast(matrix[i], n, MPI_FLOAT, MPI_ROOT, workercomm);
}
MPI_Finalize();
return 0;
}
和工人代码:
int tag = 0;
MPI_Status status;
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
MPI_Comm parent;
MPI_Comm_get_parent(&parent);
int myid;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
int n = 4;
float*vector = (float *) malloc(n * sizeof(float));
if (parent != MPI_COMM_NULL) {
MPI_Bcast(vector, n, MPI_FLOAT, MPI_ROOT, parent);
}
printf("%d ->", myid);
for (int i = 0; i < n; i++) {
printf("%f ", vector[i]);
}
printf("\n");
MPI_Comm_free(&parent);
free(vector);
MPI_Finalize();
return 0;
}
但我希望每个子进程都在矩阵中逐行打印其对应的输出,而不是输出:
..................................................... ...................................
4 ->0.000000 0.000000 0.000000 0.000000
1 ->0.000000 0.000000 0.000000 0.000000
3 ->0.000000 0.000000 0.000000 0.000000
0 ->0.000000 0.000000 0.000000 0.000000
2 ->0.000000 0.000000 0.000000 0.000000
谢谢!!
在工人代码中,您应该使用root=0
而不是MPI_ROOT
。
当使用 inter-communicator 时,请随意 re-read MPI_Bcast()
的定义
https://www.open-mpi.org/doc/v2.1/man3/MPI_Bcast.3.php
注意矩阵的分配不正确,你应该malloc(m * sizeof(float *))
代替。
您还应该在 worker 中执行 m
广播,除非 MPI_Scatter()
是您要查找的内容(在这种情况下,您应该分配一个连续的二维矩阵)
我有一个父进程和一个矩阵,我想为每一行创建一个子进程并将相应的行发送给进程。
父进程代码:
int tag = 0;
MPI_Status status;
int random(int n) {
return rand() % n;
}
float** generate_matrix(int n, int m) {
int i, j;
float **x;
x = (float **) malloc(m * sizeof(float));
for (i = 0; i < m; i++) {
x[i] = (float *) malloc(n * sizeof(float));
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
x[i][j] = random(100);
}
}
return x;
}
int main(int argc, char** argv) {
int my_rank;
int num_procs;
MPI_Comm workercomm;
int n = 4, m = 5;
float**matrix = generate_matrix(n, m);
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
MPI_Comm_spawn("C:/Users/colegnou/workspace/worker/Debug/worker.exe",
MPI_ARGV_NULL, m,
MPI_INFO_NULL, 0, MPI_COMM_SELF, &workercomm, MPI_ERRCODES_IGNORE);
for (int i = 0; i < m; i++) {
MPI_Bcast(matrix[i], n, MPI_FLOAT, MPI_ROOT, workercomm);
}
MPI_Finalize();
return 0;
}
和工人代码:
int tag = 0;
MPI_Status status;
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
MPI_Comm parent;
MPI_Comm_get_parent(&parent);
int myid;
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
int n = 4;
float*vector = (float *) malloc(n * sizeof(float));
if (parent != MPI_COMM_NULL) {
MPI_Bcast(vector, n, MPI_FLOAT, MPI_ROOT, parent);
}
printf("%d ->", myid);
for (int i = 0; i < n; i++) {
printf("%f ", vector[i]);
}
printf("\n");
MPI_Comm_free(&parent);
free(vector);
MPI_Finalize();
return 0;
}
但我希望每个子进程都在矩阵中逐行打印其对应的输出,而不是输出: ..................................................... ...................................
4 ->0.000000 0.000000 0.000000 0.000000
1 ->0.000000 0.000000 0.000000 0.000000
3 ->0.000000 0.000000 0.000000 0.000000
0 ->0.000000 0.000000 0.000000 0.000000
2 ->0.000000 0.000000 0.000000 0.000000
谢谢!!
在工人代码中,您应该使用root=0
而不是MPI_ROOT
。
当使用 inter-communicator 时,请随意 re-read MPI_Bcast()
的定义
https://www.open-mpi.org/doc/v2.1/man3/MPI_Bcast.3.php
注意矩阵的分配不正确,你应该malloc(m * sizeof(float *))
代替。
您还应该在 worker 中执行 m
广播,除非 MPI_Scatter()
是您要查找的内容(在这种情况下,您应该分配一个连续的二维矩阵)