Wiki

Clone wiki

jCODE-public / Linux_Tips

Linux Tips

VTune - Code Performance Analyzer

Use VTune by Intel to determine which parts of your code run slowly.

Running code on multiple processors

Application to launch:          mpirun
Optional Application arguments: -np 6 /home/user/jcode/bin/jcode
Working directory:              /path/to/project

Command Line

Search for a string within all f90 files in a directory

grep -inr 'string' *.f90

Sum numbers in a column

Replace # with column number and file with filename

awk '{sum += $#} END { printf "%s\n", sum}' file

Monitor processes running on computer

top

or

htop

Kill a job Use top or htop to find process id of job that might be lagging

kill -9 <process id>

Monitor processes linked to current terminal

ps

Stack memory to unlimited to avoid segmentation fault

ulimit -a unlimited

Show size of directories

du -skh ./*

Find replace in file

sed -i 's/expression/replacement/'

Back-search through previously executed commands

Ctrl & r

Loop through files and perform action Example with convert command

for i in(*.eps)
do
convert $i $i.jpg 
done

Gnuplot

Plot column 2 vs. 1

p 'file' u 1:2

Skip first two lines of data file using every

p "file" u 1:2 every ::3

Set x limit 0 to 10

set xrange[0:10]

Use linux command from gnuplot

Example using paste

plot "< paste file_1 file_2" using 2:8

Column Arithemitic

Example, divide second column by 5

p file u 1:($2/5)

Logical plotting

Example, plot second column if it is greater than zero

p file u 1:($2<0 ? 1/0 : $2)

Make high quality png or eps images

Save the following code in a file say plot.gnu. Make it an executable using chmod +x plot.gnu and run using ./plot.gnu Script:

#!/bin/bash
gnuplot << TOEND
#Setup output
set term postscript eps color enhanced "Helvectica" 20
set output "image.eps"

#Format plot
set title 'This is a title'
set xlabel 'x'
set ylabel 'Fancy y: {/Times-Italic v}_{/Symbol q}'
set key left top
set pointsize 1

#Path to file and a variable
file1='curvature_dg.txt'
file2='curvature_acls.txt'

#Plot results
p file1 w l t 'DG-CLS'\
, file2 w l t 'ACLS'

TOEND
epstopdf image.eps
rm image.eps

SSH

Setup ~/.ssh/config

SSH to other computers by typing for example

ssh -Y <username>@titan.ccs.ornl.gov

Shortcut

ssh titan

This is accomplished by adding the following to '~/.ssh/config' (may have to create it)

Host titan titan.ccs.ornl.gov HostName titan.ccs.ornl.gov User <username> ForwardX11 yes

Emacs

Compilation Mode

The typical workflow when testing changes to jCODE looks something like:

  1. Save changes to source code in editor (easy to forget).
  2. Navigate to terminal open to jcode-src-directory.
  3. Run make.
  4. The compilation failed at some point and the compiler emitted a very large number of error messages. Scroll back through the terminal and find the first one.
  5. Memorize the file-name and line-number where the error occurred.
  6. Go back to editor, and go to the appropriate line-number in the appropriate file and repeat

Thankfully we don't have to live like this! There is a wonderful emacs mode: Compilation mode which does all of this for us. You are encouraged to view the documentation for Compilation mode to make full use of this powerful tool but to get started there are really only 2 things that you need to know:

  1. Invoking compilation mode
  2. Navigating the compiler errors in compilation mode

Invoking compilation mode

Note that in emacs-land "M" stands for the Meta key (typically Alt), "C" is shorthand for the control key and a-b means press the 'b' key while holding down the 'a' key.

By default, compilation mode can be invoked with M-x compile. However, this command is invoked in the directory containing the file you are editing. Thus, to properly compile jCODE one would have to visit a file, jcode-install-dir/src/somefile and run M-x compile which is not particularly convenient. To get around around this, paste the lisp code below into your emacs initialization file (typically HOME/.emacs)

(defvar default-jcode-compile-mode "opt")

(defun compile-jcode (mode)
  "Compile jcode via make"
  (interactive (list (read-from-minibuffer 
                      (format "Compilation mode [%s]: " default-jcode-compile-mode))))
  (let ( 
        (cc-jcode (cond ((equal mode "") (format "make -w %s" default-jcode-compile-mode))
                      (t (format "make -w %s" mode))))
        (source-file (split-string buffer-file-name "/"))
      )
  ;; set the default compile mode to current mode
  (unless (equal mode "") 
    (setq default-jcode-compile-mode mode))

    (setq file-dir (mapconcat 'identity (butlast source-file) "/" ))
  ;; pop last element
  (let ( 
        (last-link (car (last source-file)))
        )
    (while (and last-link 
                (equal (equal last-link "src") nil))
        (nbutlast source-file)
        (setq last-link (car (last source-file)))
      )
    (setq src-dir (mapconcat 'identity source-file "/"))
    ;; go to src-dir and run compile
    (cd src-dir)
    (compile cc-jcode)
    (cd file-dir)
    )
  ))

(add-hook 'f90-mode-hook
          (lambda () (local-set-key (kbd "C-c C-c") 'compile-jcode)))

Upon restarting emacs there will now be a new command available: compile-jcode. When invoked from any file in any subfolder of 'src', compile-jcode will run make from the jcode-src-directory. Note that the default option for make is initialized to opt. Thus to compile in opt mode you can simply press enter. Other options would be debug or clean. After the first invocation, the default option will change to the previous choice. In the above snippet compile-jcode is bound to the shortcut key C-c C-c. Depending on other choices for key bindings this may have to be changed. Upon invocation, you will be asked to save any unsaved files.

Navigating the compiler errors in compilation mode

M-g n will take you to the next (first) error. This will immediately take you to the output of the compiler in the compilation-buffer where the error occured. By pressing RET emacs will then open the offending file and take you directly to the line which caused the error.

Updated