Skip to content

Instantly share code, notes, and snippets.

@jjhursey
Created November 12, 2020 16:31
Show Gist options
  • Save jjhursey/00b8d722567a6b71615b9e4c419edf21 to your computer and use it in GitHub Desktop.
Save jjhursey/00b8d722567a6b71615b9e4c419edf21 to your computer and use it in GitHub Desktop.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chapter: Examples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Examples}
\label{chap:examples}
This chapter contains various code examples that \ac{PMIx} users may find helpful.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Example C Code}
Example block of C code
\begin{pmixCodeC}
int main() {
printf("Hello World!\n");
return 0;
}
\end{pmixCodeC}
The inline code \pmixCodeInlineC{printf("Hello World");} prints ``Hello World''.
Example of a selection of a file:
\pmixCodeImportC[firstline=72, lastline=84]{sources/hello.c}%
Example with highlighting:
\pmixCodeImportC[firstline=72, lastline=84, highlightlines={2-5,7}]{sources/hello.c}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Example C Code from Preprocessor}
Example of a selection of a file:
\pmixCodeImportC{sources/_autogen_/hello-alt.c_pmix_init}%
Example with highlighting:
\pmixCodeImportC[highlightlines={4,7,14-15}]{sources/_autogen_/hello-alt.c_pmix_get}%
Another example
\pmixCodeImportC{sources/_autogen_/hello-alt.c_pmix_init}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Example Python Code from Preprocessor}
The inline code \pmixCodeInlinePy{print("Hello World")} prints ``Hello World''.
Another example
\pmixCodeImportPy{sources/_autogen_/hello.py_pmix_init}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Synchronizing Fence}
\label{chap:examples:syncfence}
This example shows how to call a synchronizing \refapi{PMIx_Fence} operation across the entire namespace.
To signify the entire namespace we are using the \refconst{PMIX_RANK_WILDCARD} constant.
\pmixCodeImportC[firstline=72, lastline=84, highlightlines={2-5,7}]{sources/hello.c}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
* Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2011 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2006-2013 Los Alamos National Security, LLC.
* All rights reserved.
* Copyright (c) 2009-2012 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2011 Oak Ridge National Labs. All rights reserved.
* Copyright (c) 2013-2018 Intel, Inc. All rights reserved.
* Copyright (c) 2015 Mellanox Technologies, Inc. All rights reserved.
* Copyright (c) 2020 IBM Corporation. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*
*/
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <pmix.h>
int main(int argc, char **argv)
{
//<EG BEGIN ID="pmix_init">
//<EG BEGIN ID="pmix_get">
pmix_status_t rc = PMIX_SUCCESS;
pmix_proc_t myproc;
//<EG END ID="pmix_init">
pmix_value_t *val;
uint16_t localrank;
//<EG END ID="pmix_get">
//<EG BEGIN ID="pmix_init">
rc = PMIx_Init(&myproc, NULL, 0);
if (PMIX_SUCCESS != rc) {
return 1;
}
//<EG END ID="pmix_init">
//<EG BEGIN ID="pmix_get">
/* Get our rank local to this node */
rc = PMIx_Get(&myproc, PMIX_LOCAL_RANK, NULL, 0, &val);
if (PMIX_SUCCESS != rc) {
return 2;
}
localrank = val->data.uint16;
PMIX_VALUE_RELEASE(val);
//<EG END ID="pmix_get">
/* Fence to hold all processes in this namespace */
pmix_proc_t wildproc;
PMIX_PROC_CONSTRUCT(&wildproc);
(void)strncpy(wildproc.nspace, myproc.nspace, PMIX_MAX_NSLEN);
wildproc.rank = PMIX_RANK_WILDCARD;
rc = PMIx_Fence(&wildproc, 1, NULL, 0);
if (PMIX_SUCCESS != rc) {
fprintf(stderr,
"Client ns %s rank %d: PMIx_Fence failed: %s\n",
myproc.nspace, myproc.rank, PMIx_Error_string(rc));
goto done;
}
done:
/* Finalize the PMIx library */
printf("Client ns %s rank %d: Finalizing\n", myproc.nspace, myproc.rank);
rc = PMIx_Finalize(NULL, 0);
if (PMIX_SUCCESS != rc) {
fprintf(stderr, "Client ns %s rank %d:PMIx_Finalize failed: %s\n",
myproc.nspace, myproc.rank, PMIx_Error_string(rc));
} else {
printf("Client ns %s rank %d:PMIx_Finalize successfully completed\n",
myproc.nspace, myproc.rank);
}
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment