gpt4 book ai didi

c++ - 将非 NULL argv 传递给 MPI_Comm_spawn

转载 作者:行者123 更新时间:2023-11-30 02:40:53 26 4
gpt4 key购买 nike

假设我的程序(我们称它为 prog_A)作为单个 MPI 进程启动。后来我希望程序 prog_A 使用 MPI_Comm_spawn 生成 n MPI 进程(我们称它们为 prog_B)我传递给 prog_A 的参数。

例如,如果我使用参数 200 100 10

运行 prog_A
mpiexec -n 1 prog_A 200 100 10

我希望为 prog_B 提供相同的参数 200 100 10

我该怎么做?我尝试了以下但它不起作用。

  char ** newargv = new char*[3];//create new argv for childs

newargv[0] = new char[50];
newargv[1] = new char[50];
newargv[2] = new char[50];

strcpy(newargv[0],argv[1]);//copy argv to newargv
strcpy(newargv[1],argv[2]);
strcpy(newargv[2],argv[3]);

MPI_Comm theother;

MPI_Init(&argc, &argv);
MPI_Comm_spawn("prog_B",newargv,numchildprocs,
MPI_INFO_NULL, 0, MPI_COMM_SELF, &theother,
MPI_ERRCODES_IGNORE);

MPI_Finalize();

最佳答案

你的问题是你没有 NULL 终止你的 argv 列表。这是 MPI standard 的重要部分(强调):

The argv argument argv is an array of strings containing arguments that are passed to the program. The first element of argv is the first argument passed to command, not, as is conventional in some contexts, the command itself. The argument list is terminated by NULL in C and C++ and an empty string in Fortran. In Fortran, leading and trailing spaces are always stripped, so that a string consisting of all spaces is considered an empty string. The constant MPI_ARGV_NULL may be used in C, C++ and Fortran to indicate an empty argument list. In C and C++, this constant is the same as NULL.

您只需在列表末尾添加一个 NULL。这是更正后的代码(翻译成 C,因为我的笔记本电脑上没有安装 C++ 绑定(bind)):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpi.h"

int main(int argc, char ** argv) {
char ** newargv = malloc(sizeof(char *)*4);//create new argv for childs
int numchildprocs = 1;
MPI_Comm theother;

MPI_Init(&argc, &argv);
MPI_Comm_get_parent(&theother);

if (MPI_COMM_NULL != theother) {
fprintf(stderr, "SPAWNED!\n");
} else {
newargv[0] = (char *) malloc(sizeof(char)*50);
newargv[1] = (char *) malloc(sizeof(char)*50);
newargv[2] = (char *) malloc(sizeof(char)*50);
newargv[3] = NULL;

strncpy(newargv[0],argv[1], 50);//copy argv to newargv
strncpy(newargv[1],argv[2], 50);
strncpy(newargv[2],argv[3], 50);

fprintf(stderr, "SPAWNING!\n");
MPI_Comm_spawn("./prog_B",newargv,numchildprocs,
MPI_INFO_NULL, 0, MPI_COMM_SELF, &theother,
MPI_ERRCODES_IGNORE);
}

MPI_Comm_free(&theother);

MPI_Finalize();
}

关于c++ - 将非 NULL argv 传递给 MPI_Comm_spawn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28586240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com