1
1
openmpi/src/mca/lam/base/mca_base_cmd_line.c

118 строки
2.9 KiB
C
Исходник Обычный вид История

/*
* $HEADER$
*/
#include "lam_config.h"
#include <stdio.h>
#include <string.h>
#include "lam/constants.h"
#include "lam/mem/malloc.h"
#include "lam/util/cmd_line.h"
#include "lam/util/argv.h"
#include "mca/lam/base/base.h"
/*
* Private variables
*/
static int mca_param_argc = 0;
static char **mca_param_argv = NULL;
static int mca_value_argc = 0;
static char **mca_value_argv = NULL;
/*
* Add -mca to the possible command line options list
*/
int
mca_base_cmd_line_setup(lam_cmd_line_t *cmd)
{
return lam_cmd_line_set_opt(cmd, "m", "mca", 2,
"General mechanism to pass MCA parameters");
}
/*
* Look for and handle any -mca options on the command line
*/
int
mca_base_cmd_line_process_args(lam_cmd_line_t *cmd)
{
int i, num_insts;
char *buf = 0;
int buflen = 0;
/* If no "-mca" parameters were given, just return */
if (!lam_cmd_line_is_taken(cmd, "mca"))
return LAM_SUCCESS;
/* Otherwise, assemble them into an argc/argv */
num_insts = lam_cmd_line_get_ninsts(cmd, "mca");
for (i = 0; i < num_insts; ++i)
mca_base_cmd_line_process_arg(lam_cmd_line_get_param(cmd, "mca", i, 0),
lam_cmd_line_get_param(cmd, "mca", i, 1));
/* Now put that argc/argv in the environment */
if (NULL == mca_param_argv)
return LAM_SUCCESS;
/* Loop through all the -mca args that we've gotten and make env
vars of the form LAM_MPI_MCA_*=value. This is a memory leak, but
that's how putenv works. :-( */
for (i = 0; NULL != mca_param_argv[i]; ++i) {
buflen = strlen(mca_param_argv[i]) + strlen(mca_value_argv[i]) + 32;
buf = LAM_MALLOC(buflen);
if (NULL == buf)
return LAM_ERR_OUT_OF_RESOURCE;
snprintf(buf, buflen, "LAM_MPI_MCA_%s=%s", mca_param_argv[i],
mca_value_argv[i]);
putenv(buf);
}
return LAM_SUCCESS;
}
/*
* Process a single MCA argument. Done as a separate function so that
* top-level applications can directly invoke this to effect MCA
* command line arguments.
*/
int
mca_base_cmd_line_process_arg(const char *param, const char *value)
{
int i, len;
char *new_str;
/* Look to see if we've already got an -mca argument for the same
param. Check against the list of MCA param's that we've already
saved arguments for. */
for (i = 0; NULL != mca_param_argv && NULL != mca_param_argv[i]; ++i) {
if (0 == strcmp(param, mca_param_argv[i])) {
len = strlen(value) + strlen(mca_param_argv[i]);
new_str = LAM_MALLOC(len);
snprintf(new_str, len, "%s,%s", mca_value_argv[i], value);
LAM_FREE(mca_value_argv[i]);
mca_value_argv[i] = new_str;
return LAM_SUCCESS;
}
}
/* If we didn't already have an value for the same param, save this
one away */
lam_argv_add(&mca_param_argc, &mca_param_argv, param);
lam_argv_add(&mca_value_argc, &mca_value_argv, value);
return LAM_SUCCESS;
}