《计算物理ComputationalPhysics计算物理 (10).pdf》由会员分享,可在线阅读,更多相关《计算物理ComputationalPhysics计算物理 (10).pdf(31页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、Computational physiCsHigh-performance computing PC,cluster,supercomputer Parallelism by OpenMP Parallelism by MPI GPU programming Numerical librariesPC:Personal Computer Usually 1 CPU(Central processing unit)per computer Even though,it is already very powerful.x86(x86-64)-compatible microprocessors:
2、the most used CPUs in PCs.PentiumCore i3 i5,i7Athlon,PhenomA4,A6,A10,FXworkstation-powerfultower workstation4U workstation2U workstation1U workstationU=Rack unit1U rack Usually more than 1 CPU per node.Intel:Xeon AMD:OpteronNon-x86/x86-64 compatible CPUs Intel:ItaniumIntel architecture 64 IBM:Power(
3、Performance Optimization With Enhanced RISC)总参56所:ShenweiRISC architechture IOCT:CAS:LoongsonMIPS architechture Nvidia:TegraARM architectureCluster More than 1 computer(node)Connect by network Work together.More Powerful than PCCheaper than supercomputerCan be small:BladesTransformers The philosophy
4、 of PC cluster.Devastator挖地虎-大力神SupercomputerMore CpusFaster connectionvery expensiveParallelismBig problem?needs long CPU-time?The solution:Simplify the problem!Use a faster CPU!Use more than 1 CPU!-ParallelismProverb:Many hands make light work!Code example for(int i=0;i1000000;i+)ai=i;If you only
5、have one cpu:The process is:1.a0=0;2.a1=1;3.a2=2;.If you have two cpus:You can divide the task to two cpus:1.a0=0;a1=1;2.a2=2;a3=3;3.a4=4;a5=5;.(even)(odd)Save half time!automatically parallelized by compilersOne process,several threadsExample:9.1.openmp.cpp#include#include using namespace std;int m
6、ain()const int n=8;#pragma omp parallel forfor(int i=0;in;i+)couti;sleep(1);coutendl;return 0;g+9.1.openmp.cpptime./a.outg+-fopenmp 9.1.openmp.cpptime./a.outexport OMP_NUM_THREADS=29.2.openmp2.cppMPI-Message Passing Interface MPI is a language-independent communications protocol used to program para
7、llel computersSeveral software implementations:Open MPIMPICH/MPICH2LAM/MPIIntel MPIMicrosoft MPIcode example:9.3.mpi.cpp#include#include using namespace std;int main(int argc,char*argv)MPI_Init(&argc,&argv);int mpi_procs,mpi_rank;MPI_Comm_size(MPI_COMM_WORLD,&mpi_procs);MPI_Comm_rank(MPI_COMM_WORLD,
8、&mpi_rank);coutIt is the mpi_rank of total mpi_procsendl;MPI_Finalize();return 0;Compile and run a MPI program Install one MPI implementation,e.g.OpenMPI mpic+9.3.mpi.cpp mpirun-np 10 a.outMPI for molecular dynamics If you have N degrees of freedom during the molecular dynamics simulation,you can us
9、e m processors to share the task.e.g.100 atoms in a 3D box,300 degrees of freedom.We can use 20 cpus together,each of which deals with 5 atoms.Darts method powered by MPICircle:pr2/4=p/4Square:r2=1Darts in Circle p-=-Total Darts 4MPI DartsMaybe,it is the earliest MPI darts invented by Mr.Liang Zhuge
10、The modern MPI dartsKatyusha,Soviet UnionMPI Darts 9.4.mpidarts.cpp Another method:9.5.mpidarts2.cppThe working mechansim of MPI Make n copy of program.Each copy holds one process All copies(processes)run parallel Processes can communicate between each otherTo use GPU GPU:Graphic Processing UnitVery
11、 powerfulfor molecular dynamics simulationsGPU Programming The use of Graphics Processing Units for rendering is well known,but their power for general parallel computation has only recently been explored.Parallel algorithms running on GPUs can often achieve up to 100 x speedup over similar CPU algo
12、rithms,with many existing applications for physics simulations,signal processing,financial modeling,neural networks,and countless other fields.Power of GPUGPU computing language Open general-purpose GPU computing language:OpenCL(Open Computing Language)Proprietary framework:Nvidias CUDA since 2006.C
13、UDA(Compute Unified Devices Architecture)Learn by yourselfNumerical libraries fftw:Fastest Fourier transfer in the West blas:Basic Linear Algebra Subprograms lapack:Linear Algebra Package mkl:Math Kernel Libraries acml:AMD Core Math Libraries.some basic knowledge source file main.cc func.cc object f
14、ile main.o func.o (func.obj)dynamic library libfunc.so (libfunc.dll)static library libfunc.a (libfunc.lib)exectuable program a.out (a.exe)compile&linkcode example:9.6.lib.cpp double dabs(double d)if(d0)d=-d;return d;g+-c 9.6.lib.cpp g+-shared-fPIC 9.6.lib.o-o libabs.so ar crv libabs.a 9.6.lib.ocode
15、example:9.6.link.cpp#include using namespace std;double dabs(double d);int main()double a=0;couta;coutThe absolute value is:tdabs(a)endl;return 0;g+9.6.link.cppg+9.6.link.cpp-L.-labsg+9.6.link.cpp libabs.aldd a.outenvironment variableLD_LIBRARY_PATHUsing lapack 9.7.Diagonalization.cppCall a Fortran
16、function in C/C+code.extern C void dsyev_(char*jobz,char*uplo,int*n,double*a,int*lda,double*w,double*work,int*lwork,int*info);g+9.6.Diagonalization.cpp-llapackenvironment variableLD_LIBRARY_PATHTips Find the usage of lapack functions in the website:http:/lib.org/lapack/or the manual of MKL.For symmetric/hermitian matrix,only upper/lower triangular matrix elements are used.But take care the difference between C/C+and Fortran,especially for the hermitian matrix.