Scientific Programming for(i=0; i<100000; i++) { if(a > b) { a = func1(c,d,i*10); } else if(a < b) { a = func2(e,f,i*10); } else { a = func3(g,h,i*10);

Slides:



Advertisements
Similar presentations
Which is the most important thing about a friend? a. You can trust them. b. You like the same things. c. They spend time with you. d. Something else. Why?
Advertisements

Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
What is Physical Science?. Does it have to do with this?
Selection Process If … then … else.... Condition Process 2 Process 1 Y.
Joe Hummel, PhD UC-Irvine
Code: from cisc106 import * def myfunc(value1,value2): return (value1 + value2) assertEqual(myfunc(3,2),5) assertEqual(myfunc(7,4),11) assertEqual(myfunc(9,8),17)
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Quadrature. 二阶 : 中点公式 : 梯形公式 : 四阶公式 : Simpson’s 1/3 rd Rule.
Solution to Problem Recitation #1 (Week 2) ECEN350 Prof. Choi.
The Scientific Method: Scientific Questions
Cache Review Review the performance with different accessing method. Here, My L2 cache is 256K with cache line size of 64 bytes. It means the system will.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 15: Procedures as Arguments.
How do I get started? Start with a question about a topic that interests you.
 To add numbers in scientific notation: 1) Add the constants 2) Keep the exponent the same  Example: (2.1 x 10 5 ) + (3.2 x 10 5 ) = ( ) x 10.
ARC node name/location Host institute: Full name Structure Size Areas of expertise ARC node representative: Full name ARC node Expertise: ARC node key.
WORD Use your word in a sentence DefinitionFind an image related to your word. What else do I know about this word? NameTeacher.
Steps of the Scientific Method. Decide on a Topic What are you interested in? What do you want to find out more about?
Draw me a table as complete as possible to represent the following data: Time in seconds: Temperature in o C
Flat Model 56 Human Circulatory System LT-G30 3B Scientific® 57.
Scientific Method Vocabulary. Scientific Method  Research Question: You see something happen and you ask “why?”  Hypothesis: An “if…then” statement.
Multiplying and Dividing with Scientific Notation.
EXAMPLE 4 Multiplying Numbers in Scientific Notation Find the product ( ) ( ). = ( ) ( ) = =
Scientific Notation Practice Problems #3.
Science Words. Scientific Inquiry The ways scientists study the natural world and propose explanations based on gathered evidence.
Scientific Notation. Writing Scientific Notation Writing 1.A number between 0 and 10.
Scientific Notation and Significant Figures. When it says to write your answers in the form: Then it means put your answer in scientific notation! Scientific.
Scientific Method  Hypothesis  Experiment  Observations  Conclusion.
Types of Variables in a Scientific Experiment Cornell Notes Page 37.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Change to scientific notation: A. B. C. 289,800, x x x
Qualitative or quantitative?. Today’s session You will learn how to...You will learn about... Describe research methods Assess the strengths and weaknesses.
The Scientific Method ♫ A Way to Solve a Problem ♫ Created by Mrs. Vredenburg July, 2001.
Solve It Convert This Say What? Compare Us Final Jeopardy.
Adding Numbers in Scientific Notation
The Scientific Method A Way to Solve a Problem
إعداد د/زينب عبد الحافظ أستاذ مساعد بقسم الاقتصاد المنزلي
Standard and Scientific Notation
A short description of your planet
دورة حياة تطوير النظم system development life cycle
The Scientific Method:
The Scientific Method ♫A Way to Solve a Problem♫
معاونت درمان وزارت بهداشت، درمان و آموزش پزشكي
Ichthyology Lab Test III
نسبت علم و تکنولوژی با دین
Scientific Processes Scientific Method.
زيادة الوعي بأخلاقيات البحث العلمي لدى الباحثين
منهج البحث العلمي ( Scientific Research Method )
“Everyone Else Does It!” Ethics Project
Testing: Monday: 2nd hr & 5/6th hr Tuesday: 4th hr & 7th hr
روش تحقيق در تربيت بدني انسان داراي پيشرفته ترين نظام عصبي در بين موجودات جهان است تحقيق فرايند رسيدن به پاسخ سئوال پژوهشي است روشهاي دستيابي انسان به.
Which is the most important thing about a friend?
Chapter 20: Mixed-Methods Research
1. Heart Model a 6a 2 22 Classic Heart, 2-part LT-G08 3B Scientific®
Кезеңдер “Өзім туралы” “Болашақ компьютері” “Жарнама”
Asking Questions.
Chapter 1 Scientific Research.
When a function is called...
The Scientific Method A Way to Solve a Problem
What connections can you make? What else do you notice?
Main() { int fact; fact = Factorial(4); } main fact.
Houseplants Project By:.
Dept of Mechanical Engineering
The Scientific Method.
“Everyone Else Does It!” Ethics Project
Calculate 81 ÷ 3 = 27 3 x 3 x 3 3 x 3 x 3 x 3 ÷ 3 = This could be written as
Chapter Ten Exponents and Scientific Notation
Presented by, Mr. Satish Pise
Plenary
ENGLISH FOR BUSINESS LIFE Congress, symposiums, conferences, workshops
What are our homes made of?
Presentation transcript:

Scientific Programming for(i=0; i<100000; i++) { if(a > b) { a = func1(c,d,i*10); } else if(a < b) { a = func2(e,f,i*10); } else { a = func3(g,h,i*10); } for(i=0; i<100000; i++) { if(a > b) { a = func1(c,d,i*10); } else if(a < b) { a = func2(e,f,i*10); } else { a = func3(g,h,i*10); } if(a > b) { for(i=0; i< ; i+=10) { a = func1(c,d,i); } else if(a < b) { for(i=0; i< ; i+=10) { a = func2(e,f,i); } else { for(i=0; i< ; i+=10) { a = func3(g,h,i); } if(a > b) { for(i=0; i< ; i+=10) { a = func1(c,d,i); } else if(a < b) { for(i=0; i< ; i+=10) { a = func2(e,f,i); } else { for(i=0; i< ; i+=10) { a = func3(g,h,i); }

 The code is too slow, like fundamentally slow  The code does not reach the precision required  The code does not fit into the memory  In any other case focus on functionality and let compilers handle optimization

 Start by writing a clean, well structured and documented code  Debug this code to make sure it works and does what it is supposed to do  Only now you can start optimization

 Assign names to constants and use those names throughout the code. E.g. REAL(8), PARAMETER :: Pi=3.1415D0  In every assignment statement try to combine variables and constants of the same type  For integers for integer power operation: use y = x**6 instead of y = x**6.0  Assign repeated operations to a temporary variable or at least keep the sequence of such operations; instead of a=atan(33./(1.-y**2))+(1.-y)*(1.+y)/2. use: onemy2=1.-y*y a=atan(33./onemy2)+onemy2/2.

 Operation timing: - multiplication -- division --- addition ---- subtraction power elementary functions (sqrt, exp/log, trigonometry)  This means that amean=(a1+a2+a3+a4)*0.25 will be faster than amean=(a1+a2+a3+a4)/4.  Do integer math instead of the floating point whenever it is possible

 If you can do the loop header to create the right index values, do it  Pre-compute all parts not changing inside the loop  If you use accumulators, do not touch them until the end of the loop p=0.0; x=1.0; y=0; for(i=s=0; i<n; i++,s+=10) {p=p+func(x,y,i); x=y; y=s;}  Help memory pre-fetching: for(i=s=0; i<n;) { p=p+func(x,y,i++); x=y; y=s; s+=10; p=p+func(x,y,i++); x=y; y=s; s+=10; }  Avoid if- and goto-statements inside the loop

if(x.gt.3) goto 11 a=11+b+c+d b=c c=d d=x goto a=12+b+c+d b=x c=b d=c if(x.gt.3) then a=11+b+c+d b=c c=d d=x else a=12+b+c+d b=x c=b d=c endif

subroutine qroots(a,b,c,x1,x2,flag)... C C Finding roots of a quadratic equation C b1=0.5*b/a det=b1*b1-c if(det.lt.0.) then x1=0. x2=0. flag=2 return else if(det.eq.0.) then x1=-b1 x2=x1 flag=1 return endif det=sqrt(det) x1=-b1-det x2=-b1+det flag=0 return end subroutine qroots(a,b,c,x1,x2,flag)... C C Finding roots of a quadratic equation C b1=0.5*b/a det=b1*b1-c if(det.lt.0.) then x1=0. x2=0. flag=2 return else if(det.eq.0.) then x1=-b1 x2=x1 flag=1 return endif det=sqrt(det) x1=-b1-det x2=-b1+det flag=0 return end At least try to minimize the work in each case

 Find the best algorithm for your task and look for its professional implementation. For example, the LAPACK implementation of the LU decomposition beats your simple-minded Gauss elimination by a factor of 3÷5×N/logN  Even if you do not find a readily-available library look around for the source code of a better algorithm: Marquardt-Levenberg is vastly superior to the gradient search method in optimization problems of a moderate size.

Tabulate complex functions in the initialization section of the code. Function longa(x) real xx(10000),yy(10000),yy2(10000) integer i logical first save first,xx,yy,yy2 data first/.true./ if(first) then first=.false. do i=1,10000 xx(i)=i* yy(i)=sin(xx(i))**2+exp(-(xx(i)/10.)**2) enddo call spline_init(xx,yy,yy2) endif call spline_interp(xx,yy,yy2,x,longa) return end Tabulate complex functions in the initialization section of the code. Function longa(x) real xx(10000),yy(10000),yy2(10000) integer i logical first save first,xx,yy,yy2 data first/.true./ if(first) then first=.false. do i=1,10000 xx(i)=i* yy(i)=sin(xx(i))**2+exp(-(xx(i)/10.)**2) enddo call spline_init(xx,yy,yy2) endif call spline_interp(xx,yy,yy2,x,longa) return end

 Find where precision is lost: subtraction/addition of comparable or largly different numbers  Try to fix the problem by increasing precision  Try to fix the problem by centering variables xm=mean(xx) call spline_init(xx-xm,yy,yy2) call spline_interp(xx-xm,yy,yy2,x-xm,y)  Create generic interface to your tools

MODULE SPLINES INTERFACE SPLINE_INIT MODULE PROCEDURE SPLINE_INIT8, SPLINE_INIT4 END INTERFACE INTERFACE SPLINE_INTER MODULE PROCEDURE SPLINE_INTER8, SPLINE_INTER4 END INTERFACE INTERFACE BEZIER_INIT MODULE PROCEDURE BEZIER_INIT8, BEZIER_INIT4 END INTERFACE INTERFACE BEZIER_INTER MODULE PROCEDURE BEZIER_INTER8, BEZIER_INTER4 END INTERFACE CONTAINS SUBROUTINE SPLINE_INIT8(X,Y,Y2) ! ! Computes second derivative approximations for cubic spline ! IMPLICIT NONE REAL (KIND=8) :: Y(:),Y2(:) REAL (KIND=8) :: X(:) INTEGER :: N,I REAL (KIND=8) :: U(SIZE(X)),SIG,P,YY1,YY2,YY3...

 Recycle arrays and variables  If you have elements of an array only sequentially (e.g. element i is only used after element i-1 and before element i+1 ) it can be replaced by a single variable  Use less memory consuming algorithms: conjugate gradients instead of Marquardt-Levenberg optimization  Quite often reducing memory means reducing performance but not always: if you can squeeze the whole memory of a computationally heavy routine to cache memory it will go much faster!

1. Start by writing a clear and well debugged code 2. Run a few tests – this will be your reference 3. Identify parts that represent bottlenecks. It is a good idea to separate these into subroutines/modules 4. Concentrate on optimization of only crucial parts 5. Start by find the best algorithm. 6. Next, help the compiler of doing small code restructuring 7. Finally, us the compiler optimization flags to do automatic optimization. Verify the optimized code against the reference version.

The lecture is on Tuesday October 13 th at 10:15. Home Work Part II