1 / 29

Programming

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 ‵

glenda
Download Presentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming

  2. Steps and Stages • Unix: aliases, scripts, emacs • Programming: languages • Algorithms • Running codes and handling data • Analysis • Astrophysics: interpretation

  3. Making life more comfortable • 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‵ • aliases are a great way to get organized and to make convenient short-cuts for system-independentcommands

  4. 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'

  5. Connecting to other computer(s): • Use ssh and scp • To 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 computer • 3)add it to a file .ssh/authorized-keys(2) • 4)run ssh-agent bash on your computer • 5)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.

  6. 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

  7. (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) Edit file .emacs and add those lines there

  8. 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

  9. 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

  10. Style and rules • Statements • Examples • Compilers and options • Programming with Fortran

  11. 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.

  12. 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.

  13. Example: Declarations: Main body: Finish:

  14. The same example in Fortran-90 and C

  15. Example: Main program Declarations are in file nbody1.h Open files Set parameters Read data Analyze and print

  16. Example: subroutine for direct summation

  17. 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.

  18. 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.

  19. 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’.

  20. 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

  21. 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.

  22. 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.

  23. 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,c 10 format(f8.2,f9.2,f10.5) Format can be given as a separate statement or as an in-line specification write (*,’(f8.2,f9.2,f10.5)’) a,b,c

  24. 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)

  25. 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

  26. Files • Formatted and Unformatted • Sequential and direct access • position=’append’ Unit 20 is attached to sequential, formatted file myfile.dat Open(20,file=’myfile.dat’) Open(20,file=’myfile.dat’, & form=’unformatted) Unit 20 is attached to sequential, unformatted file myfile.dat Write x,y,z in to Unit 20. Because it is attached to file.dat, the write command modifies myfile.dat write(20,*) x,y,z read(20,*,error=10,end=10) x,y,z Read from Unit 20. If error of end of file happen, go to statement labeled 10 Close(20)

  27. Compilation Compile and link: ifort -O2 file1.f file2.f -o file.exe Compile: ifort -O2 -c file1.f ifort -O2 -c file2.f link: ifort -O2 file1.o file2.o -o file.exe 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

More Related