+ All Categories
Home > Documents > Programming

Programming

Date post: 06-Jan-2016
Category:
Upload: glenda
View: 24 times
Download: 3 times
Share this document with a friend
Description:
Programming. Steps and Stages. Unix: aliases, scripts, emacs Programming: languages Algorithms Running codes and handling data Analysis Astrophysics: interpretation. Making life more comfortable. Examples: making ‘human’ commands: alias dir= ‵ ls -l ‵ - PowerPoint PPT Presentation
29
Programming
Transcript
Page 1: Programming

Programming

Page 2: Programming

Steps and Stages

Unix: aliases, scripts, emacsUnix: aliases, scripts, emacs

Programming: languages Programming: languages

AlgorithmsAlgorithms

Running codes and handling dataRunning codes and handling data

AnalysisAnalysis

Astrophysics: interpretationAstrophysics: interpretation

Page 3: Programming

Making life more comfortablealiases are a great way to get organized and to make convenient short-cuts

for system-independent commands Examples:

making ‘human’ commands: alias dir=‵ls -l‵

alias print=‵lpr‵alias copy=‵cp‵

almost mandatory: alias cp=‵cp -i‵alias mv=‵mv -i‵alias rm=‵rm -i‵

short-cuts: alias m=‵more‵alias x=‵emacs‵alias e=‵emacs -nw‵alias h=‵history‵

connections: alias leo1=‵ssh -X leo1.aip.de‵navigation: alias CODE=‵cd $HOME/CODE‵

alias RUN1=‵cd $scratch/RUN1‵

Page 4: Programming

Add lines with ‘alias’ statements to your shell profile.

For bash it is .bashrc:

export OMP_NUM_THREADS=4PS1="\[\033[1;31m\]\h.\w>\[\033[0m\] "alias m='more'alias rm='rm -i'alias cp='cp -i'alias mv='mv -i'alias dir='ls -l 'alias aphrodite='/usr/bin/ssh aphrodite.nmsu.edu -l yourUID'alias praesepe='/usr/bin/ssh -Y praesepe.nmsu.edu -l yourUID'alias CODE='cd $HOME/CODE'alias NBODY='cd $HOME/CODE/NBODY'

Page 5: Programming

Connecting to other computer(s):Use ssh and scpTo avoid typing you password again and again, use ssh-keygen and then ssh-add. In this case you type your pass only once. Steps: 1)ssh-keygen -t rsa

2)take ...pub file and scp it to another computer3)add it to a file .ssh/authorized-keys(2)4)run ssh-agent bash on your computer5)run ssh-add. You will be prompted to give passphrase.

Next time you login to your computer make (4) and (5). Your system may be setup so that you do not need (4).

Connection with the external computer may be slow. To edit files use ‘emacs -nw’, which makes editing possible with emacs even with very slow connection. You lose some functionality of emacs: cannot use ‘buttons’. Yet, it is better than using vi.

Page 6: Programming

EMACS

Emacs is a standard editor, which you can find in any unix or Mac OS system. It has many options and can be adjusted to make your editing more efficient.

The best way to customize emacs is to make changes to .emacs

Page 7: Programming

Edit file .emacs and add those lines there

(global-set-key "\C-w" 'save-buffer) (global-set-key "\C-d" 'delete-region) (global-set-key "\M-p" 'fill-paragraph) (global-set-key "\M-l" 'what-line) (global-set-key "\C-l" 'goto-line) (global-set-key "\C-e" 'save-buffers-kill-emacs) (global-set-key "\C-z" 'delete-char) (global-set-key "\M-k" 'copy-line-as-kill) (global-set-key "\M-%" 'query-replace) (global-set-key "\C-h" 'delete-backward-char) (global-set-key "\M-f" 'find-file-other-window) (global-set-key "\M-q" 'open-rectangle) (global-set-key "\M-k" 'kill-rectangle) (global-set-key "\M-c" 'clear-rectangle) (global-set-key "\M-y" 'yank-rectangle) (global-set-key "\M-o" 'other-window) (global-set-key "\M-1" 'delete-other-windows)(global-set-key "\C-o" 'open-line) (global-set-key "\M-r" 'redraw-display) (global-set-key "\C-s" 'isearch-forward) (global-set-key "\C-t" 'isearch-forward) (global-set-key "\M-n" 'find-file)

Page 8: Programming

Shell Scripts

• Simulations often create many files. For example, code may produce many snapshots.

• Analysis of many snapshots requires special tools of handling repetitive tasks

• Archiving and retrieving of many models and snapshots

Page 9: Programming

Simple example:

This script creates a list of snapshots (variables file1, 2,3). Then it calls executable PMgalaxy3 many times and feeds it with current snapshot number. It works fine given the number of the snapshots is not large.

Place the text in a file ‘run.bat’. Make it executable (chmod a+x run.bat). Then just run it as if it is a unix command:

run.bat

Page 10: Programming

Programming with Fortran

Style and rulesStatementsExamplesCompilers and options

Page 11: Programming

General Assessment

Fortran is designed for number-crunching. It is not for problems with sophisticated data structures or logic. If you follow rules, Fortran provides a very fast and easy to write code. Do not use archaic features in Fortran.

Fortran is about speed: fast codes is our goal.

Page 12: Programming

Before we start coding:• Check formulae and algorithms. It takes very long

time to develop and to debug a code. If at the end we find that there was a mistake in equations, we lose too much.

• More efficient algorithm has higher priority over pretty code writing.

• Start with clearly defining data structures: arrays, parameters, files.

• Estimate time needed for code developing: 10% - design, 10% - write, 80% - debug

• High price for making errors: write code nice and clear; learn how find bugs.

Page 13: Programming

Example:

Declarations:

Main body:

Finish:

Page 14: Programming

The same example in Fortran-90 and C

Page 15: Programming

Example: Main program

Declarations are in file nbody1.h

Open files

Set parameters

Read data

Analyze and print

Page 16: Programming

Example: subroutine for direct summation

Page 17: Programming

Style and rules• Write Fortran code as C++: objects and

data flow are first, arithmetics is second.

• Top down design: start with the top level of the code. Write the logic of calls of different top-level routines. Then fill the gaps by writing routines of lower and lower levels.

• Routines should be short. No side effects. If a routine is assigning density of particles to some array, do not modify potential.

Page 18: Programming

Archaic Features, which should be avoided

• Labels or line numbers should be avoided. Code can jump to any label (goto N). Because compiler does not know from where you can jump in to the labeled statement, it does not do many optimizations. This makes code very slow.

• Compilers try to optimize pieces of the code, which do not contain labels.

• Do not abuse the rule: sometimes it is easier to use labels.

Page 19: Programming

Implicit statement• It is often recommended (in strong words) not to use

“Implicit” statement. The main motivation is that one can make a typo in a name of a variable, which is difficult to find. ‘Implicit none’ is recommended for the beginners.

• This recommendation comes from people, who do not write large and complicated codes.

• ‘Implicit’ is a powerful tool, which makes coding more efficient. Yet, one should always use it or never use it. Do not mix explicit declarations and implicit style.

• Always follow implicit rules: variables and arrays starting with letters (i-n) are INTEGER. Do not define Mass as real. ‘aMass’ is as clear as ‘Mass’.

Page 20: Programming

Writing comments•Rule Number One: write text in such a

way that just looking at it one sees the structure of the code. No comments are needed to understand what is written.

•Rule Number Two: follow rule N1, but then write short comments: Always. Write them as you type. You will not come back to add a comment.

•Rule Number Three: comment every subroutine or function by putting dividing lines, which clearly separate the text.

•Rule Number Four: no dividing lines in the main text. Comments should not obstruct the text

Page 21: Programming

More on Style: calling names

•Use long and natural names for global variables and arrays. E.g., Nparticles, Niterations.

•For local temporary variables use short names: x,y,i,k0

•In a long subroutine one should be careful not to use names, which are already in use. You may give names, which are obviously temporary: tmp, dummy

•Use ‘grab’ to check whether name is used.

Page 22: Programming

Formal parameters and common blocks

•Data can be passed to a subroutine either using formal arguments ( e.g. CALL MySUB(x,y,z) ) or by as global variable( e.g. COMMON /A/x,y,z) or in module)

•Those are very different mechanisms. Formal arguments are handled through ‘stack’ and common blocks (also SAVE and ALLOCATE) go to the ‘heap’. Most of the computer RAM is allocated to the ‘heap’ because by design stack is for small things.

•Use formal arguments for small arrays, few arguments, and for constructs, which cannot be handled by common blocks.

Page 23: Programming

Formatting I/O•Fortran has extensive tools and options

to read and write files. There is nothing like that in C.

write (*,10) a,b,c10 format(f8.2,f9.2,f10.5)

write (*,’(f8.2,f9.2,f10.5)’) a,b,c

Format can be given as a separate statement or as an in-line specification

Page 24: Programming

Implicit Do loops in formats •You can write in a file n=1 an array Z

of length N in two ways:

write(5) Z or write(5) (Z(i),i=1,N)

The first form is faster, but the second is more flexible if we want to modify it:

write(5) (Z(i),i=1,N-5,3)

• More complicated example:

write(5) a,b,c,((Z(i,j),i=1,N,10),j=2,M/2)

Page 25: Programming

Formatting outputs

•To produce nice-looking output tables it is convenient to use ‘T’ format. For example,

write(*,’(f8.2,T30,a,g12.3)’) r,’Density=’,d

starts text ‘Density’ at the position 30. This is useful for wide tables with many headers

Page 26: Programming

Files •Formatted and Unformatted

•Sequential and direct access

•position=’append’

Open(20,file=’myfile.dat’) Unit 20 is attached to sequential, formatted file myfile.dat

Open(20,file=’myfile.dat’, & form=’unformatted)

Unit 20 is attached to sequential, unformatted file myfile.dat

Close(20)

write(20,*) x,y,z

read(20,*,error=10,end=10) x,y,z

Write x,y,z in to Unit 20. Because it is attached to file.dat, the write command

modifies myfile.dat

Read from Unit 20. If error of end of file happen, go to statement labeled 10

Page 27: Programming

Compilation

ifort -O2 file1.f file2.f -o file.exe

ifort -O2 -c file1.f

ifort -O2 -c file2.f

ifort -O2 file1.o file2.o -o file.exe

Compile and link:

Compile:

link:

Compile for parallel execution with OpenMP:

ifort -O2 -openmp -parallel file1.f file2.f -o file.exe Parallel execution with OpenMP:

setenv OMP_NUM_THREADS 16

file.exe export OMP_NUM_THREADS=16

file.exe

Page 28: Programming
Page 29: Programming

QuickTime™ and aSorenson Video 3 decompressorare needed to see this picture.


Recommended