Blame src/pmi/pmi2/simple/simple_pmiutil.h

Packit Service c5cf8c
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *  (C) 2001 by Argonne National Laboratory.
Packit Service c5cf8c
 *      See COPYRIGHT in top-level directory.
Packit Service c5cf8c
 */
Packit Service c5cf8c
#ifndef SIMPLE_PMIUTIL_H_INCLUDED
Packit Service c5cf8c
#define SIMPLE_PMIUTIL_H_INCLUDED
Packit Service c5cf8c
Packit Service c5cf8c
#include "mpichconf.h"
Packit Service c5cf8c
Packit Service c5cf8c
/* maximum sizes for arrays */
Packit Service c5cf8c
#define PMI2U_MAXLINE 1024
Packit Service c5cf8c
#define PMI2U_IDSIZE    32
Packit Service c5cf8c
Packit Service c5cf8c
/* prototypes for PMIU routines */
Packit Service c5cf8c
void PMI2U_Set_rank(int PMI_rank);
Packit Service c5cf8c
void PMI2U_SetServer(void);
Packit Service c5cf8c
void PMI2U_printf(int print_flag, const char *fmt, ...);
Packit Service c5cf8c
int PMI2U_readline(int fd, char *buf, int max);
Packit Service c5cf8c
int PMI2U_writeline(int fd, char *buf);
Packit Service c5cf8c
int PMI2U_parse_keyvals(char *st);
Packit Service c5cf8c
void PMI2U_dump_keyvals(void);
Packit Service c5cf8c
char *PMI2U_getval(const char *keystr, char *valstr, int vallen);
Packit Service c5cf8c
void PMI2U_chgval(const char *keystr, char *valstr);
Packit Service c5cf8c
Packit Service c5cf8c
#ifdef HAVE__FUNCTION__
Packit Service c5cf8c
#define PMI2U_FUNC __FUNCTION__
Packit Service c5cf8c
#elif defined(HAVE_CAP__FUNC__)
Packit Service c5cf8c
#define PMI2U_FUNC __FUNC__
Packit Service c5cf8c
#elif defined(HAVE__FUNC__)
Packit Service c5cf8c
#define PMI2U_FUNC __func__
Packit Service c5cf8c
#else
Packit Service c5cf8c
#define PMI2U_FUNC __FILE__
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
extern int PMI2_pmiverbose;     /* Set this to true to print PMI debugging info */
Packit Service c5cf8c
#define printf_d(x...)  do { if (PMI2_pmiverbose) printf(x); } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
/* error reporting macros */
Packit Service c5cf8c
Packit Service c5cf8c
#define PMI2U_ERR_POP(err) do { pmi2_errno = err; printf_d("ERROR: %s (%d)\n", PMI2U_FUNC, __LINE__); goto fn_fail; } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP(err, class, str) do {                              \
Packit Service c5cf8c
        printf_d("ERROR: "str" in %s (%d)\n", PMI2U_FUNC, __LINE__);     \
Packit Service c5cf8c
        pmi2_errno = class;                                                      \
Packit Service c5cf8c
        goto fn_fail;                                                           \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP1(err, class, str, str1, arg) do {                          \
Packit Service c5cf8c
        printf_d("ERROR: "str1" in %s (%d)\n", arg, PMI2U_FUNC, __LINE__);       \
Packit Service c5cf8c
        pmi2_errno = class;                                                              \
Packit Service c5cf8c
        goto fn_fail;                                                                   \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP2(err, class, str, str1, arg1, arg2) do {                           \
Packit Service c5cf8c
        printf_d("ERROR: "str1" in %s (%d)\n", arg1, arg2, PMI2U_FUNC, __LINE__);        \
Packit Service c5cf8c
        pmi2_errno = class;                                                                      \
Packit Service c5cf8c
        goto fn_fail;                                                                           \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP3(err, class, str, str1, arg1, arg2, arg3) do {                     \
Packit Service c5cf8c
        printf_d("ERROR: "str1" in %s (%d)\n", arg1, arg2, arg3, PMI2U_FUNC, __LINE__);  \
Packit Service c5cf8c
        pmi2_errno = class;                                                                      \
Packit Service c5cf8c
        goto fn_fail;                                                                           \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP4(err, class, str, str1, arg1, arg2, arg3, arg4) do {                       \
Packit Service c5cf8c
        printf_d("ERROR: "str1" in %s (%d)\n", arg1, arg2, arg3, arg4, PMI2U_FUNC, __LINE__);    \
Packit Service c5cf8c
        pmi2_errno = class;                                                                              \
Packit Service c5cf8c
        goto fn_fail;                                                                                   \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP5(err, class, str, str1, arg1, arg2, arg3, arg4, arg5) do {                         \
Packit Service c5cf8c
        printf_d("ERROR: "str1" in %s (%d)\n", arg1, arg2, arg3, arg4, arg5, PMI2U_FUNC, __LINE__);      \
Packit Service c5cf8c
        pmi2_errno = class;                                                                                      \
Packit Service c5cf8c
        goto fn_fail;                                                                                           \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_SETANDJUMP6(err, class, str, str1, arg1, arg2, arg3, arg4, arg5, arg6) do {                           \
Packit Service c5cf8c
        printf_d("ERROR: "str1" in %s (%d)\n", arg1, arg2, arg3, arg4, arg5, arg6, PMI2U_FUNC, __LINE__);        \
Packit Service c5cf8c
        pmi2_errno = class;                                                                                              \
Packit Service c5cf8c
        goto fn_fail;                                                                                                   \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP(cond, err, class, str) do {        \
Packit Service c5cf8c
        if (cond)                                               \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP(err, class, str);              \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP1(cond, err, class, str, str1, arg) do {    \
Packit Service c5cf8c
        if (cond)                                                       \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP1(err, class, str, str1, arg);          \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP2(cond, err, class, str, str1, arg1, arg2) do {     \
Packit Service c5cf8c
        if (cond)                                                               \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP2(err, class, str, str1, arg1, arg2);           \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP3(cond, err, class, str, str1, arg1, arg2, arg3) do {       \
Packit Service c5cf8c
        if (cond)                                                                       \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP3(err, class, str, str1, arg1, arg2, arg3);             \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP4(cond, err, class, str, str1, arg1, arg2, arg3, arg4) do { \
Packit Service c5cf8c
        if (cond)                                                                       \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP4(err, class, str, str1, arg1, arg2, arg3, arg4);       \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP5(cond, err, class, str, str1, arg1, arg2, arg3, arg4, arg5) do {   \
Packit Service c5cf8c
        if (cond)                                                                               \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP5(err, class, str, str1, arg1, arg2, arg3, arg4, arg5);         \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_ERR_CHKANDJUMP6(cond, err, class, str, str1, arg1, arg2, arg3, arg4, arg5, arg6) do {     \
Packit Service c5cf8c
        if (cond)                                                                                       \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP6(err, class, str, str1, arg1, arg2, arg3, arg4, arg5, arg6);           \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
#if (!defined(NDEBUG) && defined(HAVE_ERROR_CHECKING))
Packit Service c5cf8c
#define PMI2U_AssertDecl(a_) a_
Packit Service c5cf8c
#define PMI2U_AssertDeclValue(_a, _b) _a = _b
Packit Service c5cf8c
#else
Packit Service c5cf8c
/* Empty decls not allowed in C */
Packit Service c5cf8c
#define PMI2U_AssertDecl(a_) a_
Packit Service c5cf8c
#define PMI2U_AssertDeclValue(_a, _b) _a ATTRIBUTE((unused))
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
#ifdef HAVE_ERROR_CHECKING
Packit Service c5cf8c
#define PMI2U_CHKMEM_SETERR(rc_, nbytes_, name_) do {                                           \
Packit Service c5cf8c
        rc_ = PMI2_ERR_NOMEM;                                                                   \
Packit Service c5cf8c
        printf_d("ERROR: memory allocation of %lu bytes failed for %s in %s (%d)\n",     \
Packit Service c5cf8c
                (size_t)nbytes_, name_, PMI2U_FUNC, __LINE__);                                  \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#else
Packit Service c5cf8c
#define PMI2U_CHKMEM_SETERR(rc_, nbytes_, name_) rc_ = PMI2_ERR_NOMEM
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
#if defined(HAVE_ALLOCA) && defined(USE_ALLOCA)
Packit Service c5cf8c
#ifdef HAVE_ALLOCA_H
Packit Service c5cf8c
#include <alloca.h>
Packit Service c5cf8c
#endif
Packit Service c5cf8c
/* Define decl with a dummy definition to allow us to put a semi-colon
Packit Service c5cf8c
   after the macro without causing the declaration block to end (restriction
Packit Service c5cf8c
   imposed by C) */
Packit Service c5cf8c
#define PMI2U_CHKLMEM_DECL(n_) int dummy_ ATTRIBUTE((unused))
Packit Service c5cf8c
#define PMI2U_CHKLMEM_FREEALL()
Packit Service c5cf8c
#define PMI2U_CHKLMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,stmt_) do {        \
Packit Service c5cf8c
        pointer_ = (type_)alloca(nbytes_);                                              \
Packit Service c5cf8c
        if (!(pointer_)) {                                                              \
Packit Service c5cf8c
            PMI2U_CHKMEM_SETERR(rc_,nbytes_,name_);                                     \
Packit Service c5cf8c
            stmt_;                                                                      \
Packit Service c5cf8c
        }                                                                               \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#else
Packit Service c5cf8c
#define PMI2U_CHKLMEM_DECL(n_)                                  \
Packit Service c5cf8c
    void *(pmi2u_chklmem_stk_[n_]) = {0};                       \
Packit Service c5cf8c
    int pmi2u_chklmem_stk_sp_=0;                                \
Packit Service c5cf8c
    PMI2U_AssertDeclValue(const int pmi2u_chklmem_stk_sz_,n_)
Packit Service c5cf8c
Packit Service c5cf8c
#define PMI2U_CHKLMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,stmt_) do {        \
Packit Service c5cf8c
        pointer_ = (type_)PMI2U_Malloc(nbytes_);                                        \
Packit Service c5cf8c
        if (pointer_) {                                                                 \
Packit Service c5cf8c
            PMI2U_Assert(pmi2u_chklmem_stk_sp_
Packit Service c5cf8c
            pmi2u_chklmem_stk_[pmi2u_chklmem_stk_sp_++] = pointer_;                     \
Packit Service c5cf8c
        } else {                                                                        \
Packit Service c5cf8c
            PMI2U_CHKMEM_SETERR(rc_,nbytes_,name_);                                     \
Packit Service c5cf8c
            stmt_;                                                                      \
Packit Service c5cf8c
        }                                                                               \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_CHKLMEM_FREEALL()                                         \
Packit Service c5cf8c
    while (pmi2u_chklmem_stk_sp_ > 0) {                                 \
Packit Service c5cf8c
        PMI2U_Free(pmi2u_chklmem_stk_[--pmi2u_chklmem_stk_sp_]); }
Packit Service c5cf8c
#endif /* HAVE_ALLOCA */
Packit Service c5cf8c
#define PMI2U_CHKLMEM_MALLOC(pointer_,type_,nbytes_,rc_,name_) \
Packit Service c5cf8c
    PMI2U_CHKLMEM_MALLOC_ORJUMP(pointer_,type_,nbytes_,rc_,name_)
Packit Service c5cf8c
#define PMI2U_CHKLMEM_MALLOC_ORJUMP(pointer_,type_,nbytes_,rc_,name_) \
Packit Service c5cf8c
    PMI2U_CHKLMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,goto fn_fail)
Packit Service c5cf8c
Packit Service c5cf8c
/* In some cases, we need to allocate large amounts of memory. This can
Packit Service c5cf8c
   be a problem if alloca is used, as the available stack space may be small.
Packit Service c5cf8c
   This is the same approach for the temporary memory as is used when alloca
Packit Service c5cf8c
   is not available. */
Packit Service c5cf8c
#define PMI2U_CHKLBIGMEM_DECL(n_)                                       \
Packit Service c5cf8c
    void *(pmi2u_chklbigmem_stk_[n_]);                                  \
Packit Service c5cf8c
    int pmi2u_chklbigmem_stk_sp_ = 0;                                   \
Packit Service c5cf8c
    PMI2U_AssertDeclValue(const int pmi2u_chklbigmem_stk_sz_,n_)
Packit Service c5cf8c
Packit Service c5cf8c
#define PMI2U_CHKLBIGMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,stmt_) do {     \
Packit Service c5cf8c
        pointer_ = (type_)PMI2U_Malloc(nbytes_);                                        \
Packit Service c5cf8c
        if (pointer_) {                                                                 \
Packit Service c5cf8c
            PMI2U_Assert(pmi2u_chklbigmem_stk_sp_
Packit Service c5cf8c
            pmi2u_chklbigmem_stk_[pmi2u_chklbigmem_stk_sp_++] = pointer_;               \
Packit Service c5cf8c
        } else {                                                                        \
Packit Service c5cf8c
            PMI2U_CHKMEM_SETERR(rc_,nbytes_,name_);                                     \
Packit Service c5cf8c
            stmt_;                                                                      \
Packit Service c5cf8c
        }                                                                               \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_CHKLBIGMEM_FREEALL()                                              \
Packit Service c5cf8c
    while (pmi2u_chklbigmem_stk_sp_ > 0) {                                      \
Packit Service c5cf8c
        PMI2U_Free(pmi2u_chklbigmem_stk_[--pmi2u_chklbigmem_stk_sp_]); }
Packit Service c5cf8c
Packit Service c5cf8c
#define PMI2U_CHKLBIGMEM_MALLOC(pointer_,type_,nbytes_,rc_,name_)       \
Packit Service c5cf8c
    PMI2U_CHKLBIGMEM_MALLOC_ORJUMP(pointer_,type_,nbytes_,rc_,name_)
Packit Service c5cf8c
#define PMI2U_CHKLBIGMEM_MALLOC_ORJUMP(pointer_,type_,nbytes_,rc_,name_)                \
Packit Service c5cf8c
    PMI2U_CHKLBIGMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,goto fn_fail)
Packit Service c5cf8c
Packit Service c5cf8c
/* Persistent memory that we may want to recover if something goes wrong */
Packit Service c5cf8c
#define PMI2U_CHKPMEM_DECL(n_)                                  \
Packit Service c5cf8c
    void *(pmi2u_chkpmem_stk_[n_]) = {0};                       \
Packit Service c5cf8c
    int pmi2u_chkpmem_stk_sp_=0;                                \
Packit Service c5cf8c
    PMI2U_AssertDeclValue(const int pmi2u_chkpmem_stk_sz_,n_)
Packit Service c5cf8c
#define PMI2U_CHKPMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,stmt_) do {        \
Packit Service c5cf8c
        pointer_ = (type_)PMI2U_Malloc(nbytes_);                                        \
Packit Service c5cf8c
        if (pointer_) {                                                                 \
Packit Service c5cf8c
            PMI2U_Assert(pmi2u_chkpmem_stk_sp_
Packit Service c5cf8c
            pmi2u_chkpmem_stk_[pmi2u_chkpmem_stk_sp_++] = pointer_;                     \
Packit Service c5cf8c
        } else {                                                                        \
Packit Service c5cf8c
            PMI2U_CHKMEM_SETERR(rc_,nbytes_,name_);                                     \
Packit Service c5cf8c
            stmt_;                                                                      \
Packit Service c5cf8c
        }                                                                               \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_CHKPMEM_REGISTER(pointer_) do {                           \
Packit Service c5cf8c
        PMI2U_Assert(pmi2u_chkpmem_stk_sp_
Packit Service c5cf8c
        pmi2u_chkpmem_stk_[pmi2u_chkpmem_stk_sp_++] = pointer_;         \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
#define PMI2U_CHKPMEM_REAP()                                            \
Packit Service c5cf8c
    while (pmi2u_chkpmem_stk_sp_ > 0) {                                 \
Packit Service c5cf8c
        PMI2U_Free(pmi2u_chkpmem_stk_[--pmi2u_chkpmem_stk_sp_]); }
Packit Service c5cf8c
#define PMI2U_CHKPMEM_COMMIT() pmi2u_chkpmem_stk_sp_ = 0
Packit Service c5cf8c
#define PMI2U_CHKPMEM_MALLOC(pointer_,type_,nbytes_,rc_,name_)          \
Packit Service c5cf8c
    PMI2U_CHKPMEM_MALLOC_ORJUMP(pointer_,type_,nbytes_,rc_,name_)
Packit Service c5cf8c
#define PMI2U_CHKPMEM_MALLOC_ORJUMP(pointer_,type_,nbytes_,rc_,name_)           \
Packit Service c5cf8c
    PMI2U_CHKPMEM_MALLOC_ORSTMT(pointer_,type_,nbytes_,rc_,name_,goto fn_fail)
Packit Service c5cf8c
Packit Service c5cf8c
/* A special version for routines that only allocate one item */
Packit Service c5cf8c
#define PMI2U_CHKPMEM_MALLOC1(pointer_,type_,nbytes_,rc_,name_,stmt_) do {      \
Packit Service c5cf8c
        pointer_ = (type_)PMI2U_Malloc(nbytes_);                                \
Packit Service c5cf8c
        if (!(pointer_)) {                                                      \
Packit Service c5cf8c
            PMI2U_CHKMEM_SETERR(rc_,nbytes_,name_);                             \
Packit Service c5cf8c
            stmt_;                                                              \
Packit Service c5cf8c
        }                                                                       \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
/* Provides a easy way to use realloc safely and avoid the temptation to use
Packit Service c5cf8c
 * realloc unsafely (direct ptr assignment).  Zero-size reallocs returning NULL
Packit Service c5cf8c
 * are handled and are not considered an error. */
Packit Service c5cf8c
#define PMI2U_REALLOC_OR_FREE_AND_JUMP(ptr_,size_,rc_) do {                                     \
Packit Service c5cf8c
        void *realloc_tmp_ = PMI2U_Realloc((ptr_), (size_));                                    \
Packit Service c5cf8c
        if ((size_) && !realloc_tmp_) {                                                         \
Packit Service c5cf8c
            PMI2U_Free(ptr_);                                                                   \
Packit Service c5cf8c
            PMI2U_ERR_SETANDJUMP2(rc_,PMI2U_CHKMEM_ISFATAL,                                     \
Packit Service c5cf8c
                                  "**nomem2","**nomem2 %d %s",(size_),PMI2U_QUOTE(ptr_));       \
Packit Service c5cf8c
        }                                                                                       \
Packit Service c5cf8c
        (ptr_) = realloc_tmp_;                                                                  \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
/* this version does not free ptr_ */
Packit Service c5cf8c
#define PMI2U_REALLOC_ORJUMP(ptr_,size_,rc_) do {                                               \
Packit Service c5cf8c
        void *realloc_tmp_ = PMI2U_Realloc((ptr_), (size_));                                    \
Packit Service c5cf8c
        if (size_)                                                                              \
Packit Service c5cf8c
            PMI2U_ERR_CHKANDJUMP2(!realloc_tmp_,rc_,PMI2U_CHKMEM_ISFATAL,\                      \
Packit Service c5cf8c
                                  "**nomem2","**nomem2 %d %s",(size_),PMI2U_QUOTE(ptr_));       \
Packit Service c5cf8c
        (ptr_) = realloc_tmp_;                                                                  \
Packit Service c5cf8c
    } while (0)
Packit Service c5cf8c
Packit Service c5cf8c
#endif /* SIMPLE_PMIUTIL_H_INCLUDED */