
Setting up the computing environment • I will be demonstrating on MAC. • Linux environment is very similar to MAC and should be easy to follow • Windows might be hardest to set up In the past we provided virtual machine, light linux ubuntu, with all software installed. Such virtual machine can be run on any operating system. If there is sufficient interest for a virtual machine, we will create it. Native installation is more efficient, and probably a good idea to try it out. Essential Software: • Python, and its packages : numpy, scipy, matplotlib (easy installation with Anaconda www.anaconda.com) • C++ compiler, such as gcc, but other C++ compilers should be fine too. • Text editor for coding (anaconda includes Spyder, which I am learning, Emacs, Aquamacs, or similar) • make to execute makefiles Recommended Hardware: It is impossible to demonstrate this algorithms on different platforms, therefore we need to choose one of the common platforms. I will be demonstrating algorithms on Mac platform, and if you have Windows or linux machine, you will need to translate installation to your platform, which might be technically demanding at times. If you want to follow the course on similar system, you can choose, for example: MacBook Pro, Intel i7, 16GB DDR RAM, and 512 GB hard drive. Recommended Software: • Fortran compiler, such as gfortran, or intel fortran (it is getting harder to install nowadays). • blas&lapack libraries. They come preinstalled in most computers or one needs to install vendors libraries (intel mkl for linux). On mac it is contained in Xcode • openMP enabled C++ compiler (native gcc rather than apple clang, which is invoked by gcc,g++) • gnuplot for fast plotting. Installation on MAC: • Install Xcode package from App Store (Essential) • We will need specific part of Xcode namely “Command Line Tools”, which might be already installed in your distribution. To check if they are, type: xcode-select --print-path If you do not find them, install by xcode-select —install Xcode contains C/C++ compiler (gcc/g++). It is just a link to apples native Clang. Xcode also contains many libraries. For example, it should include BLAS and LAPACK libraries. To use these libraries, we will use a linker option: -framework Accelerate. For more information see (https://developer.apple.com/accelerate/) Xcode also includes make. Installation on MAC: Multicore openMP execution: However, Xcode does not come anymore with gnu compilers (such as gnu-c==gcc and gnu-c++==g++). Instead gcc and g++ point to apple’s own Clang compiler. Unfortunately Clang does not yet support openMP (multicore) instructions. Moreover, Clang does not include fortran compiler, such as gnu compiler (gfortran). Recommended: To install openMP, fortran & gnuplot we will use homebrew. The long instructions of how to install the homebrew are available at http://brew.sh In summary, you need to paste the following into the terminal prompt: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/ install.sh)" and follow the instructions. After installing homebrew, you can check for any issues with the install by typing brew doctor Installation on MAC: On some OSX versions you may run into file permission issues with OSX’s SIP process. If you see permission issues that does not allow you to install homebrew, you might need to change ownership on the homebrew directory by typing: sudo chown -R $(whoami):admin /usr/local Recommended: Now that homebrew is installed, we can install gcc and gfrortran First check that gcc package exists, you can type in the Terminal prompt brew search gcc To actually install gcc and gfortran, please type: brew install gcc --without-multilib The extra option “--without-multilib” adds open mp support. You no longer need to include --without-multilib on 64-bit-only systems as it is automatically disabled but it doesn’t hurt to add it. If you arelady installed gcc before, but you do not have openMP support, you will need to rather type: brew reinstall gcc --without-multilib Warning: the installation of gcc will take a lot of time. Check installation after complete: gcc-10 --version gfortran --version Installation on MAC: Recommended: Installing gnuplot using homebrew: brew install gnuplot --with-x11 If error is returned, just type: brew install gnuplot If you previously unsuccessfully installed a package such as gnuplot, then first type: brew uninstall gnuplot followed by command brew install gnuplot --with-x11 Installation for any platform: Essential: Finally, we will install Python with its packages. It should always be installed after Xcode was installed. Xcode comes with a stripped-down version of Python, however, this native version of python usually does not contain scipy support. To check the current python installation, you can type python import scipy scipy.test() If it survives a minute or or more, the installation is OK. If not, follow the instructions below. As of this writing, the Anaconda distribution is most user friendly and easy to install, available for all operating systems: https://www.anaconda.com “Get Started”, “Download Anaconda installers”, “Graphical installer” Once installed, open: It already includes all packages we need: scipy, numpy, matplotlib, ipython,jupyter Installation for MAC: Recommended: Installing pybin11 in anaconda: conda install -c conda-forge pybind11 This is one of the easier methods to speed-up python codes through C++ More information : https://pybind11.readthedocs.io/en/latest/index.html Installing numba in anaconda: Usually it is already installed. Check: conda list|grep numba In nothing is listed, type: conda install numba Testing of our installation with a concrete example Which language is most popular? https://www.tiobe.com/tiobe-index/ Python is climbing up fast C/C++ are basis of operating systems and core of most software Comparison of languages by generating Mandelbrot set: KH Computational Physics- 2019 Introduction 2 Wikipedia: The Mandelbrot set M is defined by a family of complex quadratic polynomials f(z) = z + z0 where z0 is a complex parameter. For each z0, one considers the behavior of the sequence (0, f (0), f (f (0)), f (f (f (0))), · · ·) obtained by iterating f (z) starting at z = 0, which either escapes to infinity or stays within a disk of someComparison finite radius. The Mandelbrot of languages set is defined as the set by of all generating points z0 such that Mandelbrot set: the above sequence does not escape to infinity. Im z0 Re z0 black: sequence stays finite Wikipedia: The Mandelbrot set M is defined 2 by a family of complex quadratic polynomials f(z)=z + z0 where z0 is a complex parameter. For each z0,oneconsidersthebehaviorofthesequence (0,f(0),f(f(0)),f(f(f(0))), ···) obtained by iterating f(z) starting at z =0,which either escapes to infinity or stays within a disk of some finite radius. The Mandelbrot set is defined as the set of all points z0 such that the above sequence does not escape to infinity. Kristjan Haule, 2019 –23– Implementation in Fortran (mandf.f90) INTEGER Function Mandelb(z0, max_steps) IMPLICIT NONE ! Every variable needs to be declared. It is very prudent to use that. COMPLEX*16, intent(in) :: z0 INTEGER, intent(in) :: max_steps ! locals COMPLEX*16 :: z INTEGER :: i z=0. do i=1,max_steps if (abs(z)>2.) then Mandelb = i-1 return end if z = z*z + z0 end do Mandelb = max_steps return END Function Mandelb Main function which defines how many steps are needed before the value f(f(….f(z0))) explodes. program mand use omp_lib IMPLICIT NONE ! external function INTEGER :: Mandelb ! Need to declare the external function ! locals INTEGER :: i, j REAL*8 :: x, y COMPLEX*16 :: z0 INTEGER, parameter :: Nx = 1000 INTEGER, parameter :: Ny = 1000 This is how we use the above INTEGER, parameter :: max_steps = 1000 function in the main part of the REAL*8 :: ext(4) = (/-2., 1., -1., 1./) ! The limits of plotting REAL*8 :: mande(Nx,Ny) program. REAL :: start, finish, startw, finishw call cpu_time(start) Note !$OMP directives for startw = OMP_get_wtime() multicore execution. !$OMP PARALLEL DO PRIVATE(j,x,y,z0) do i=1,Nx do j=1,Ny We print 2D array to the x = ext(1) + (ext(2)-ext(1))*(i-1.)/(Nx-1.) y = ext(3) + (ext(4)-ext(3))*(j-1.)/(Ny-1.) standard output, which contains z0 = dcmplx(x,y) mande(i,j) = Mandelb(z0, max_steps) 1/#steps needed before value enddo explodes. enddo !$OMP END PARALLEL DO finishw = OMP_get_wtime() call cpu_time(finish) WRITE(0, '("clock time : ",f6.3,"s wall time=",f6.3,"s")') finish-start, finishw-startw do i=1,Nx do j=1,Ny x = ext(1) + (ext(2)-ext(1))*(i-1.)/(Nx-1.) y = ext(3) + (ext(4)-ext(3))*(j-1.)/(Ny-1.) print *, x, y, 1./mande(i,j) enddo enddo end program mand Testing example gfortran -O3 -fopenmp -o mandf mandf.f90 Execute and check the time: ./mandf > mand.dat clock time : 1.30075s with wall time=0.218432s Finally plot: gnuplot gnu.sh The codes produce three column output x, y, color and need a plotting program to display results. In gnuplot the following command plots the output: set view map splot ’mand.dat’ with p ps 3 pt 5 palette or call the script by gnuplot gnu.sh Why do we still bother with Fortran? ↑ A lot of scientific code written in fortran → we need to use it and be able to occasionally adapt it. ↑ It is very easily integrated with Python/numpy through f2py/f2py3, hence useful in combination with Python. ↓ For todays standards, it is obsolete, i.e, developed by IBM in the 1950s (John W. Backus 1953). ↓ Many releases (Fortran, Fortran II, Fortran III, Fortran 66, Fortran 77, Fortran 90, Fortran 95, Fortran 2003, Fortran 2008).
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages75 Page
-
File Size-