Wednesday, May 23, 2018

Steps for Profiling with Oracle SolarisStudio Performance Analyzer:




 

Steps for Profiling with Oracle SolarisStudio Performance Analyzer:

 
  • Compile with optimization flags and the ā€˜-g’ flag
      
  • Set up the environment:
 
setenv PATH "/path_to_profiler/solarisstudio-12.5b/bin:/path_to_jdk/jdk1.8.0_71/bin:$PATH"
 
setenv LD_LIBRARY_PATH "/path_to_jdk/jdk1.8.0_71/lib:~/jdk1.8.0_71/jre/lib"
 
  • Run the application and collect samples in an experiment: The name of the experiment is printed in the console when the application starts, say /tmp/test.0.er. Running collect by itself would show the range of values for some of its parameters on the given machine.
 
For a serial application:
                                collect -p 1 -d /tmp
 
For profiling only the master of a MPI job, run it like this:
mpirun -f ./hosts -perhost 1 -np 4 ./prof.sh
 
where prof.sh is a script with content:
 
#! /bin/csh
if ($PMI_RANK == 0) then
   collect -p 1 -d /tmp $*
else
    $*
endif
 
  • Analyze the experiment:
    analyzer
                                (In the GUI, load the experiment by browsing to it)
 
References:

Thursday, May 17, 2018

Useful Intel Compiler debugging options

---------------------------------------------------------------------------------
Useful Intel Compiler debugging options:
---------------------------------------------------------------------------------
https://www.nas.nasa.gov/hecc/support/kb/recommended-intel-compiler-debugging-options_92.html

C++: -check-uninit -ftrapuv
Fortran: -check uninit -ftrapuv

Dump and load and diff

---------------------------------------------------------------------------------
To dump binary data from application, and then see it
---------------------------------------------------------------------------------
void dump_data (float *a, size_t bytes, char *root, int count, int rank)
{
    char filename[256];
    sprintf (filename, "%s_%d_%d", root, rank, count);
    int fd = open (filename, O_RDWR|O_CREAT|O_TRUNC, 00644);
    if (fd == -1) {
        printf ("Could not open file %s for writing\n", filename);
        return;
    }
    ucsl_printf ("Dumping data into %s\n", filename);
    fflush (_UCSL_LOG);
    write (fd, a, bytes);
    close (fd);
}

Call like this:
---------------
int iter=0;
func() {
    ...
    dump_data ((float *)array, n*sizeof(float), "/tmp/a", iter++);
}

To visualize:
---------------
Say a and b are the two dumps where n1=2001, then run the script:
./xdiff a b 2001


Content of xdiff is as follows:
-------------------------------
$ cat xdiff
#! /bin/csh -Xf

if ($#argv != 3) then
    echo "Usage: $0 file1 file2 n1"
    exit 0
endif
diff $1 $2
cp -f $1 aaaaaa
cat $2 >> aaaaaa
cat diff.bin >> aaaaaa
ximage perc=90 n1=$3 < aaaaaa
exit 0
---------------------------------------------------------------------------------


diff is an executable produced from the following C code:

#include
#include
#include
#include
#include
#include
#include
#include




Using python to do the same:

---------------------------------------------------------------------------------
Using python: python2.7.9 is a script that loads python/2.7.9 and PrgEnv-gnu
---------------------------------------------------------------------------------
% source python2.7.9
This modulefile provides python (2.7.9)

More information about python can be found at:
    http://docs.python.org/2/

% which ipython
% ipython --pylab
Python 2.7.9 (default, Jan  7 2016, 09:44:23)
Type "copyright", "credits" or "license" for more information.

IPython 4.0.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: TkAgg

In [1]: run ./compute.py

In [2]: dd = data1-data2

In [4]: dd.min()
Out[4]: -12.869473

---------------------------------------------------------------------------------
To check diff between two binary files
---------------------------------------------------------------------------------
$ python --version
Python 2.6.6
$ python
Python 2.6.6 (r266:84292, Dec  7 2011, 20:48:22)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.fromfile('/tmp/a',dtype=np.float32)
array([ 1536.7668457 ,  1534.49963379,  1528.29431152, ...,  3819.53222656,
        3827.30517578,  3833.81079102], dtype=float32)
>>> aa = np.fromfile('/tmp/a',dtype=np.float32)
>>> aa.shape
(40330178,)
>>> bb = np.fromfile('/tmp/a',dtype=np.float32)
>>> cc = aa - bb
>>> cc.sum

>>> cc.sum()
0.0
>>>

compute.py looks like this:
----------------------------------------------
import matplotlib.pylab as plt
import numpy as np
import time

fname1    = '/tmp/a'
fname2    = '/tmp/b'

nt = 2001 # number of time pts
nc = 190  # receiver samples (number of channel)

data1 = np.fromfile(fname1,dtype=np.float32)
data1 = data1.reshape((nc,nt)).T
data2 = np.fromfile(fname2,dtype=np.float32)
data2 = data2.reshape((nc,nt)).T

figlabel1 = fname1
figlabel2 = fname2

## plot parameters
#~ cmin = data.min()
#~ cmax = data.max()

#~ pct = 5.0 ## set the color range
#~ dmi = data.min()
#~ dmx = data.max()
#~ cmin = 0.5*(dmi+dmx) - pct/100.0*(dmx-dmi)
#~ cmax = 0.5*(dmi+dmx) + pct/100.0*(dmx-dmi)

pct = 10.0 ## set the color range
cmin = pct/100 * min(data1.min(), data2.min())
cmax = pct/100 * max(data1.max(), data2.max())

# # show data
cmap = 'jet'
#~ cmap = 'gray'
#~ cmap = 'bone'

fig1 = plt.figure()
dshow = plt.imshow(data1, vmin = cmin, vmax = cmax, cmap = cmap)
plt.colorbar(dshow)
#~ plt.axis('off')
plt.title(figlabel1, fontsize=16)
#~
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()

fig2 = plt.figure()
dshow = plt.imshow(data2, vmin = cmin, vmax = cmax, cmap = cmap)
plt.colorbar(dshow)
#~ plt.axis('off')
plt.title(figlabel2, fontsize=16)
#~
mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()



# # show models
fig3 = plt.figure()

gfig = fig3.add_subplot(1,3,1)
gshow = plt.imshow(data1, vmin = cmin, vmax = cmax, cmap = cmap , interpolation='none')
plt.colorbar(gshow)
gfig.set_title('data1')

kfig = fig3.add_subplot(1,3,2)
kshow = plt.imshow(data2, vmin = cmin, vmax = cmax, cmap = cmap , interpolation='none')
plt.colorbar(kshow)
kfig.set_title('data2')

dfig = fig3.add_subplot(1,3,3)
dshow = plt.imshow(data1 - data2,  cmap = cmap , interpolation='none')
plt.colorbar(dshow)
dfig.set_title('difference')

mng = plt.get_current_fig_manager()
mng.resize(*mng.window.maxsize())
plt.show()

Useful commands

To disable automatic comment insertion in blank lines
-------------------------------------------------------
:set formatoptions-=r formatoptions-=c formatoptions-=o

or insert this in .vimrc
------------------------
au FileType * setlocal fo-=c fo-=r fo-=o

For 4 spaces instead of tab in vi, add this to ~.vimrc:
------------------------------------------------------- 
set expandtab
set tabstop=4
set shiftwidth=4
au FileType * setlocal fo-=c fo-=r fo-=o



" To read fortran code

--------------------------------------------------------
let fortran_free_source=1
let fortran_do_enddo=1
"filetype plugin indent on
syntax on

highlight! link DiffText Todo
 


To create a zero 11G file:
--------------------------
dd if=/dev/zero of=/tmp/dummy_gina count=10 bs=1G

---------------------------------------------------------------------------------
To avoid typing password for ssh login to a mach:
---------------------------------------------------------------------------------
vi ~/.ssh/id_rsa.pub (copy the line here)
ssh
vi ~/.ssh/authorized_keys, add the public key from the machine you are logging
in from.
Make sure permissions on this file is 600


---------------------------------------------------------------------------------
Bash scripts
---------------------------------------------------------------------------------
while true
do
    echo "Hi"
    sleep 1
done

(or)

while sleep 30; do pmap -x ; done

for host in `cat ~/jlt`; do echo $host; done

for i in `cat /tmp/nodes`; do ssh $i dmesg|grep OOM; done
for i in `cat /tmp/nodes`; do ssh $i dmesg|grep segfaul; donefor i in `cat /tmp/nodes`; do ssh $i dmesg|grep segfaul; done


Script to run a list of commands in a file called "cmdlist":

#!/bin/bash

#------------------------------------------------------------------------
# Set the field separator to a newline
IFS="
"

if [ "$#" -ne 1 ]; then
    echo "Usage: $0  "
    exit
fi

# Loop through the file
for line in `cat "$1"`; do
    a=(eval "$line" ">& /dev/null")
    "${a[@]}"
    let status=$?
    if [ $status != 0 ]; then
        echo "${a[1]} ================> FAILED"
    fi
done
 


Thursday, February 11, 2016

Get available system memory

Useful Linux tips
//*****************************************************************
// Get available system memory

//*****************************************************************
float get_available_sysmem_mb ()
{
  size_t sysmem = (size_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGESIZE );
  return ((float)sysmem/1048576.0f);
}

Monday, July 08, 2013

Memory Mapped Files

Wednesday, July 03, 2013

Shell Scripts

Shell Scripts Links

How to pass arguments to bash script:
https://stackoverflow.com/questions/18568706/check-number-of-arguments-passed-to-a-bash-script?rq=1

String manipulation in bash scripts:
https://gist.github.com/magnetikonline/90d6fe30fc247ef110a1

Parse command line arguments:
https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash


---------------------------------------------------------------------------------------------------

Script to execute a bunch of commands:

#!/bin/bash
#
# Invoke commands stored in a file, say "cmdfile",  by running this script:
#       ./run_script cmdfile
#
#------------------------------------------------------------------------

# Set the field separator to a newline
IFS="
"

if [ "$#" -ne 1 ]; then
    echo "Usage: $0  "
    exit
fi

# Loop through the file
for line in `cat "$1"`; do
    a=(eval "$line" ">& /dev/null")
    "${a[@]}"
    let status=$?
    if [ $status != 0 ]; then
        echo "${a[1]} ================> FAILED"
    fi
done



-------------------------------------------------------------------------------------------------

  #!/bin/bash

#------------------------------------------------------------------------
#
# This script expects a command line argument: the name of a file that
# contains a sequence of jobnames, a pattern to replace and a pattern to replace with.
#
# For instance, the following commands are saved in a file called "jobs":
#   job1_v03i
#   job2_v03i
#   job3_v03i
#
#------------------------------------------------------------------------

# Set the field separator to a newline
IFS="
"

if [ "$#" -lt 3 ]; then
    echo "Usage: $0  [ ]"
    echo "For example:"
    echo "             $0 jobs v03i v04 /path/to/binary/v03i /different/path/to/binary/v04"
    echo "             $0 jobs v03i v03j"
    exit
fi

# Loop through the file
for job in `cat "$1"`; do
    inputjobxml="${job}.jobxml"
    outputjobxml="${inputjobxml//"$2"/$3}"
    tempfile="/tmp/a"
    echo "$inputjobxml"
    echo "$outputjobxml"
    if [ "$#" -eq 5 ]; then
        eval "sed 's+$4+$5+g' $inputjobxml > $tempfile"
    fi
    eval "sed 's/$2/$3/g' $tempfile > $outputjobxml"
    echo "--------"
  done



--------------------------------------------------------------------------------------------------

#!/bin/bash

#------------------------------------------------------------------------
#
# This script expects a command line argument: the name of a file that
# contains a sequence of filenames, a pattern to replace and a pattern to replace with.
#
# For instance, the following commands are saved in a file called "list":
#   filename1.ext
#   filename2.ext
#   filename3.ext
#
#
#------------------------------------------------------------------------

# Set the field separator to a newline
IFS="
"

# Extract versions from command line arguments
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -f)
    fileslist="$2"
    shift # past argument
    shift # past value
    ;;
    -o|--oldver)
    oldver="$2"
    shift # past argument
    shift # past value
    ;;
    -n|--newver)
    newver="$2"
    shift # past argument
    shift # past value
    ;;

    -h|--help)
    echo ""
    echo "Usage: $0 "
    echo ""
    echo "-f {name}                             file containing  filenames"
    echo "-o {ver} | --oldver {ver}             string representing old version"
    echo "-n {ver} | --newver {ver}             string representing new version"
    echo ""
    echo "For example: "
    echo "             $0 -f parfiles --oldver v03i --newver v04"
    echo "             $0 -f parfiles -o v03i -n v03j"
    echo ""
    exit
    ;;

    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

echo "${fileslist}"
echo "${oldver}"
echo "${newver}"

# Loop through the file
for inputfile in `cat "$fileslist"`; do
    outputfile="${inputfile//"$oldver"/$newver}"
    echo "$inputfile"
    echo "$outputfile"
    eval "sed 's/$oldver/$newver/g' $inputfile > $outputfile"
    echo "--------"
done



A similar loop:


# Loop through the file

 for line in `cat "$fileslist"`; do    
    inputfile="$(cut -d' ' -f 1 <<< $line)"
    np="$(awk '{print $2}' <<< $line)"
    outputfile="${inputfile//"$oldver"/$newver}"
    eval "sed 's/$oldver/$newver/g' $inputfile > $outputfile"

    cmd="mpirun -f $hostfile -perhost 1 -np $np $binary $args"
    echo "Running $cmd ... "
    eval $cmd
    let status=$?
    if [ $status != 0 ]; then
        echo "FAILED"
    else
        echo "SUCCESSFUL"
    fi
done


Note: Can also be done using, but this did not execute the "eval $cmd" line each time.

while read line; do
done < $fileslist

Friday, June 14, 2013

Buffer Cache, Page Cache, etc.

Tuesday, February 07, 2012

Semaphores

Unix semaphore example

/* semabinit.c - initialize a semaphore for use by programs sema and semb */

#include 
#include 
#include 
#include 

/* The semaphore key is an arbitrary long integer which serves as an
   external identifier by which the semaphore is known to any program
   that wishes to use it. */

#define KEY (1492)

void main()
{
   int id; /* Number by which the semaphore is known within a program */

   /* The next thing is an argument to the semctl() function. Semctl() 
      does various things to the semaphore depending on which arguments
      are passed. We will use it to make sure that the value of the 
      semaphore is initially 0. */

 union semun {
  int val;
  struct semid_ds *buf;
  ushort * array;
 } argument;

   argument.val = 0;

   /* Create the semaphore with external key KEY if it doesn't already 
      exists. Give permissions to the world. */

   id = semget(KEY, 1, 0666 | IPC_CREAT);

   /* Always check system returns. */

   if(id < 0)
   {
      fprintf(stderr, "Unable to obtain semaphore.\n");
      exit(0);
   }

   /* What we actually get is an array of semaphores. The second 
      argument to semget() was the array dimension - in our case
      1. */

   /* Set the value of the number 0 semaphore in semaphore array
      # id to the value 0. */

   if( semctl(id, 0, SETVAL, argument) < 0)
   {
      fprintf( stderr, "Cannot set semaphore value.\n");
   }
   else
   {
      fprintf(stderr, "Semaphore %d initialized.\n", KEY);
   }
}

/* Semaphore example program a (sema.c) */
/* We have two programs, sema and semb. Semb may be initiated at any 
  time, but will be forced to wait until sema is executed. Sema and
  semb do not have to be executed by the same user! */

#include 
#include 
#include 
#include 

#define KEY (1492)
/* This is the external name by which the semaphore is known to any
   program that wishes to access it. */

void main()
{
   int id;  /* Internal identifier of the semaphore. */
   struct sembuf operations[1];
   /* An "array" of one operation to perform on the semaphore. */

   int retval; /* Return value from semop() */

   /* Get the index for the semaphore with external name KEY. */
   id = semget(KEY, 1, 0666);
   if(id < 0)
   /* Semaphore does not exist. */
   {
      fprintf(stderr, "Program sema cannot find semaphore, exiting.\n");
      exit(0);
   }

   /* Do a semaphore V-operation. */
   printf("Program sema about to do a V-operation. \n");

   /* Set up the sembuf structure. */
   /* Which semaphore in the semaphore array : */
    operations[0].sem_num = 0;
    /* Which operation? Add 1 to semaphore value : */
    operations[0].sem_op = 1;
    /* Set the flag so we will wait : */   
    operations[0].sem_flg = 0;

    /* So do the operation! */
    retval = semop(id, operations, 1);

    if(retval == 0)
    {
       printf("Successful V-operation by program sema.\n");
    }
    else
    {
       printf("sema: V-operation did not succeed.\n");
 perror("REASON");
    }
}

/* Think carefully about what the V-operation does. If sema is executed 
   twice, then semb can execute twice. */

/* Semaphore example program b (semb.c) */
/* We have two programs, sema and semb. Semb may be initiated at any 
  time, but will be forced to wait until sema is executed. Sema and
  semb do not have to be executed by the same user! */

/* HOW TO TEST:
   Execute semb &
   The & is important - otherwise you would have have to move to
   a different terminal to execute sema.

   Then execute sema.
*/

#include 
#include 
#include 
#include 

#define KEY (1492)
/* This is the external name by which the semaphore is known to any
   program that wishes to access it. */

void main()
{
   int id;  /* Internal identifier of the semaphore. */
   struct sembuf operations[1];
   /* An "array" of one operation to perform on the semaphore. */

   int retval; /* Return value from semop() */

   /* Get the index for the semaphore with external name KEY. */
   id = semget(KEY, 1, 0666);
   if(id < 0)
   /* Semaphore does not exist. */
   {
      fprintf(stderr, "Program semb cannot find semaphore, exiting.\n");
      exit(0);
   }

   /* Do a semaphore P-operation. */
   printf("Program semb about to do a P-operation. \n");
   printf("Process id is %d\n", getpid());

   /* Set up the sembuf structure. */
   /* Which semaphore in the semaphore array : */
    operations[0].sem_num = 0;
    /* Which operation? Subtract 1 from semaphore value : */
    operations[0].sem_op = -1;
    /* Set the flag so we will wait : */   
    operations[0].sem_flg = 0;

    /* So do the operation! */
    retval = semop(id, operations, 1);

    if(retval == 0)
    {
       printf("Successful P-operation by program semb.\n");
       printf("Process id is %d\n", getpid());
    }
    else
    {
       printf("semb: P-operation did not succeed.\n");
    }
}

/* Think carefully about what the V-operation does. If sema is executed 
   twice, then semb can execute twice. */