Skip to content

Commit

Permalink
Foundation for including example source code
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Hursey <[email protected]>
  • Loading branch information
jjhursey committed Sep 4, 2020
1 parent c2d00f2 commit 717b52c
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
*.synctex.gz
*.DS_Store
*.xwm
*.mw
datetime-defaults.sty
datetime.sty
check-openpmix
figs/~$drawings.pptx
*.loc
*.soc
_minted*
46 changes: 46 additions & 0 deletions Chap_Examples.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chapter: Examples
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Examples}
\label{chap:examples}

This chapter contains various code examples that \ac{PMIx} users may find helpful.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Playing Around}

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}%

Putting a couple of examples together

\pmixCodeJoinStart{}%
\pmixCodeImportC[firstline=41, lastline=42]{sources/hello.c}%
\pmixCodeJoin{}%
\pmixCodeImportC[firstline=49, lastline=55, firstnumber=last]{sources/hello.c}%
\pmixCodeJoinEnd{}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29 changes: 14 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Makefile for the PMIx Standard document in LaTex format.
# For more information, see the master document, pmix-standard.tex.

LATEX_C=pdflatex -shell-escape -file-line-error

version=4.0
default: pmix-standard.pdf

Expand All @@ -27,11 +29,7 @@ CHAPTERS= \
App_Python.tex \
Acknowledgements.tex

SOURCES=
# SOURCES=sources/*.c \
# sources/*.cpp \
# sources/*.f90 \
# sources/*.f
SOURCES=sources/*.c

INTERMEDIATE_FILES=pmix-standard.pdf \
pmix-standard.toc \
Expand All @@ -45,27 +43,28 @@ INTERMEDIATE_FILES=pmix-standard.pdf \
pmix-standard.blg \
pmix-standard.synctex.gz \
pmix-standard.xwm \
*.idx *.ilg *.ind
*.idx *.ilg *.ind \
_minted-*

all: pmix-standard.pdf

pmix-standard.pdf: $(CHAPTERS) $(SOURCES) pmix.sty pmix-standard.tex figs/pmix-logo.png
rm -f $(INTERMEDIATE_FILES)
rm -rf $(INTERMEDIATE_FILES)
@echo "-------------------------------------------------------------"
@echo "If error occurs check pmix-standard.log and pmix-standard.ind"
@echo "-------------------------------------------------------------"
@echo "====> Building 1/4"
pdflatex -interaction=batchmode -file-line-error pmix-standard.tex || \
pdflatex -interaction=errorstopmode -file-line-error pmix-standard.tex < /dev/null
$(LATEX_C) -interaction=batchmode pmix-standard.tex || \
$(LATEX_C) -interaction=errorstopmode pmix-standard.tex < /dev/null
@echo "====> Building 2/4 (bibtex)"
bibtex pmix-standard < /dev/null
@echo "====> Building 3/4"
pdflatex -interaction=batchmode -file-line-error pmix-standard.tex || \
pdflatex -interaction=errorstopmode -file-line-error pmix-standard.tex < /dev/null
$(LATEX_C) -interaction=batchmode pmix-standard.tex || \
$(LATEX_C) -interaction=errorstopmode pmix-standard.tex < /dev/null
@echo "====> Building 4/4"
pdflatex -interaction=batchmode -file-line-error pmix-standard.tex || \
pdflatex -interaction=errorstopmode -file-line-error pmix-standard.tex < /dev/null
pdflatex -interaction=batchmode -file-line-error pmix-standard.tex
$(LATEX_C) -interaction=batchmode pmix-standard.tex || \
$(LATEX_C) -interaction=errorstopmode pmix-standard.tex < /dev/null
$(LATEX_C) -interaction=batchmode pmix-standard.tex
@./bin/check-doc.sh
@echo "====> Success"
@cp pmix-standard.pdf pmix-standard-${version}.pdf
Expand All @@ -87,4 +86,4 @@ check-decl: pmix-standard.pdf FORCECHECK
@./bin/check-multi-declare.py

clean:
rm -f $(INTERMEDIATE_FILES) pmix-standard-*.pdf
rm -rf $(INTERMEDIATE_FILES) pmix-standard-*.pdf
3 changes: 3 additions & 0 deletions pmix-standard.tex
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@
% PMIx Tools and Debugger Support
\input{Chap_API_Tools.tex}

% Examples
\input{Chap_Examples}

%
% Appendix
%
Expand Down
62 changes: 62 additions & 0 deletions pmix.sty
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,65 @@
\newcounter{pycounter}
\newcommand{\pylabel}[1]{\refstepcounter{pycounter} \label{appB:#1}}
\newcommand{\refpy}[1]{\hyperref[appB:#1]{\code{#1}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code examples
% Install: (if using Python 3 then use pip3)
% sudo pip2 install --upgrade pip setuptools
% sudo pip2 install Pygments
% Ubuntu:
% sudo apt update
% sudo apt install python-pip
\usepackage{morewrites}
\usepackage{minted}
\usemintedstyle{default}
\usepackage{xcolor}

% Background: Light Gray
\definecolor{pmixbg}{rgb}{0.95,0.95,0.95}
% Highlight: Yellow
\definecolor{pmixhl}{rgb}{0.95,0.95,0}
% C Code block
% \begin{pmixCodeC}
% return 0;
% \end{pmixCodeC}
\newminted[pmixCodeC]{c}{fontsize=\small,
bgcolor=pmixbg,
highlightcolor=pmixhl,
breaklines,
autogobble,
linenos,
numbersep=3pt,
firstnumber=1}
% Inline C code
% The code \pmixCodeInlineC{printf("Hello World");} prints ``Hello World''.
\newmintinline[pmixCodeInlineC]{c}{bgcolor=pmixbg,
highlightcolor=pmixhl,
breaklines,
autogobble,
linenos,
firstnumber=1}
% Import C code from a file:
% - Whole file: \pmixCodeImportC{sources/hello.c}
% - Selection of a file: \pmixCodeImportC[firstline=73, lastline=84]{sources/hello.c}
% - Highlight selection of a file: \pmixCodeImportC[firstline=73, lastline=84, highlightlines={2-4,6}]{sources/hello.c}
\newmintedfile[pmixCodeImportC]{c}{fontsize=\small,
bgcolor=pmixbg,
highlightcolor=pmixhl,
breaklines,
autogobble,
linenos,
numbersep=3pt,
firstnumber=1}

% Putting a few code blocks together with a little space between the sections.
% \pmixCodeJoinStart{}%
% \pmixCodeImportC[firstline=41, lastline=42]{sources/hello.c}%
% \pmixCodeJoin{}%
% \pmixCodeImportC[firstline=49, lastline=55, firstnumber=last]{sources/hello.c}%
% \pmixCodeJoinEnd{}%
\newcommand{\pmixCodeJoinStart}{\nolinenumbers}
\newcommand{\pmixCodeJoin}{\vspace{-3.15em}}
\newcommand{\pmixCodeJoinEnd}{\linenumbers}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 changes: 99 additions & 0 deletions sources/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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)
{
pid_t pid;
char hostname[1024];
pmix_status_t rc = PMIX_SUCCESS;
pmix_proc_t myproc;
pmix_value_t *val;
uint16_t localrank;

pid = getpid();
gethostname(hostname, 1024);

/* Initialize PMIx - client role */
rc = PMIx_Init(&myproc, NULL, 0);
if (PMIX_SUCCESS != rc) {
fprintf(stderr, "Client ns %s rank %d: PMIx_Init failed: %s\n",
myproc.nspace, myproc.rank, PMIx_Error_string(rc));
return 1;
}

/* Get our rank local to this node */
rc = PMIx_Get(&myproc, PMIX_LOCAL_RANK, NULL, 0, &val);
if (PMIX_SUCCESS != rc) {
fprintf(stderr,
"Client ns %s rank %d: PMIx_Get local rank failed: %s\n",
myproc.nspace, myproc.rank, PMIx_Error_string(rc));
goto done;
}
localrank = val->data.uint16;
PMIX_VALUE_RELEASE(val);

printf("Client ns %s rank %d pid %lu: Running on host %s localrank %d\n",
myproc.nspace, myproc.rank, (unsigned long)pid, hostname ,
(int)localrank);

/* 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;
}

0 comments on commit 717b52c

Please sign in to comment.