Writing Fast MATLAB Code

所需积分/C币:10 2017-05-29 14:33:04 518KB PDF
4
收藏 收藏
举报

matlab加速编程
e Save frequent console commands as a script If you find yourself repeating certain commands on the console, save them as a script. Less typing means fewer opportunities for typos A void losing data Dont use clear all in a script. This is an unfortunate common practicc--any important variables in the basc workspace will bo irretrievably lost Beware of clobber File clobber"refers to the kind of data loss when a file is accidenta lly overwritten with another one having the saIlle filenaine. This phenoinelon call occur with variables as well ation (input1)i resu Variable result was clobbered and the first output was lost Bcwarc of what can crash matlab While matlab is generally reliable, crashes are possible when using third-party meX functions or extremely memory-intensive operations, for example, with video and very large arrays Now with good working habits covered, we begin our discussion of writing fast MATLAB code. The rest of this article is organized by topic, first on techniques that are useful in general application, and next on specific computational topics(table of contents is on the first page 2>The Profiler MATLAB 5.0(R10) and ncwcr versions includc a tool called thc profilor that helps identify bottlenecks in a program. Use it with the profile command profile on Turn the profiler on profile off Turn it back off file clear Clear profile st atistics profile report view the results froIn the profiler For exaMple, Consider profiling the following functio function result example(Count) k 1: Count if result(k)<-09 result(k)= gammaln(k end To analyze the efficiency this function, first enable and clear the profiler, run the function, and then view the profile report > profile on, profile clear > exar.p1e1(5000) > profile report There is a slight parsing overhead when running code for the first time; run the test code twice and time the second run. The profiler report command shows a report. Depending on the system, profiler results may differ from this example. MATLAB Profile Report: Summary Report generated 30-Jwl-2004 16: 57: 01 Total recorded time 3.09s Number of m-functions 4 Clock precision 0.016s Function List Name Time Time Time/call Self time Location example13.09100.0% 3094000236763%/ example1.m 0.7323.7% 3562 0.000206 0.73 23.77./toolbox/matlab/specfun/gammalnm profile 0.00 0.0% 0.0000000000.0% /toolbox/matlab/general/profile. m profreport 0.00 0.070 1 0.000000 0.00 0.0% ./toolbox /matlab/general/profreportm Clicking the cxamplc1" link givcs morc details Lines where the most time was spent Line Number Code Calls Total Time %c Time result(k)= sin(k/50) 50002.11s % result(k) gammaln(k); 721 0.84s 27% 6 if result(k)<-09 50000.14s 5% Totals 3.09s 100% The most time-consuming lines are displayed, along with time. time percentage, and line number. The Illost costly lines are the conputations ol lines 4 and 7 Another helpful section of the profile report is" M-Lint Results, "which gives feedback from the M-Lint code analyzer. Possible errors and suggestions are listed here M-Lint results Line number Message 4 result'might be growing inside a loop. Consider preallocating for speed result' might be growing inside a loop. Consider prcallocating for speed (Preallocation is discussed in the next section. The profiler has limited time resolution, so to profile a piece of code that runs too quickly. run the test code multiple times with a loop. Adjust the number of loop iterations so that the time it takes to run is noticeable. More iterations yields better time resolution The profiler is an essential tool for identifying bottlenecks and per-statement analysis, however, for more accurate timing of a piece of code, use the tic/toc stopwatch timer > -ic; example(5000)i toci Elapsed time is 3.082055 seconds For serious benchmarking, also closc your web browser, anti-virus, and other background proccsscs that may be taking cpu cycles 3> Array Preallocation MATLAB'S matrix variables have the ability to dynamically augment rows and columns. For example, 0 MATLAB automatically resizes the matrix. Internally, the matrix data memory must be reallocated with larger size. If a matrix is resized repeatedly-like within a loop- this overhead can be significant To avoid frequent reallocations, preallocate the matrix with the zeros command Consider the code a(1)=1; for k (k)=0.99803xa(k-1)-0.06279*b(k b(k)=0.06279xa(k-1)+0.99803*(k-1) This code takes 0.47 seconds to run. After the for loop, both arrays are row vectors of length 8000 t hus to prea.lloca te, create empty a and b row vectors each with 8000 elements a= zeros(1,8000); 号 Preal1 ocation b= zeros(1,8000) a(1)=1 b(1) Ork=2:8000 a(k)=0.99803xa(k-1)-0.06279*b(k-1); b(k)=0.06279xa(k-1)+0.99803+b(k-1) With this modification, the code takes only 0. 14 seconds (over three times faster). Preallocation is often easy to do, in this case it was only necessary to determine the right preallocation size and add two lines Whlat if the final array size call vary? Here is all exaMple a= zeros(1,10000); Preallocate count = 0; fork=1:10000 V=exp(rand大rand); if v>0.5 o Conditionally add to array court count 1: a(count) end end a= a(: count) o Trim extra zeros rom the results The average run time of this program is 0. 42 seconds without preallocation and 0. 18 seconds with it Preallocation is also beneficial for cell arrays, using the cell command to create a cell array of the desired size 4>JIT Acceleration MATLAB 6.5(R13)and lator fcaturc the Just-In- Timc ( JIT)Accclcrator for improving the spccd of M-functions, particularly with loops. By knowing a few things about the accelerator, you can improve its performance Thc JIT Accclcrator is enabled by default. To disable it typcfeature accel off in the console and"feature accel on"to enable it again s of MaTlab R2008b, only a subset of the MaTlab language is supported for acceleration. Upon encountering an unsupported feature, acceleration processing falls back to non-accelerated evaluation Acceleration is most effective when significant contiguous portions of code are supported Data types: Code must use supported data types for acceleration: double(both real and complex), logical, char, int8-32, uint8-32. Some struct, cell, classdef, and function handle usage is supported Sparse arrays are not accelerated Array shapes: Array shapes of any size with 3 or fewer dimensions are supported Changing the shape or data type of an array interrupts acceleration. a few limited situations with 4D array are accelerated Function calls: Calls to built-in functions and M-functions are accelerated. Calling meX func tions and Java interrupts acceleration. (See also page 14 on inlining simple functions. Conditionals and loops: The conditional statements if, elseif, and simple switch statements tre supported if the conditional expression evaluates to a scalar. Loops of the form for k=a: b for k=a: b:c, and while loops arc accclcrated if all codc within thc loop is supported In-place computation ntroduced in MATLAB 7. 3(R2006b ), the element-wise operators(+,*, etc. and some other functions can be computed in-place. That is, a computation like =5 sgrt(x.2+-); is handled internally without needing temporary storage for accumulating the result. An M-function can also be computed in-place if its output argument matches one of the input arguments x my fun(x)i function x my fun(x) x = 5*sgrt(x.2 +-); eturn To enable ill-pliace cOmputatiOn, the iil-place operatiOn Illust be within an M-function (and for all ill- place function, the function itself must be called within an M-function). Currently there is no support for in-place computation with meX-functions Multithreaded Computation MATLAB 7. 4(R2007a) introduced multithreaded computation for multicore and multiprocessor com puters. Multithreaded computation accelerates some per-element functions when applied to large arrays for example,", sin, exp)and certain linear algebra functions in the BLAS library. To enable it, select File Preferences -General- Multithreading and select "Enable multithreaded computation. Fu ther control over parallel computation is possible with the Parallel Computing Toolbox M using parfor and spmd IIT-Accelerated Example For example, the following loop-heavy code is supported for acceleration function R= bilateral(A, sd, sr r) o The bilateral image denoising filter B- zeros(size(A) 士ori=1:size(A,1) f for m =-R: R if i+m 1&&i+m for n R: R if j+n >>=1 &&3+n <=size(A, 2 -n(i,j))^2/(2*sd^2)) B(i,j)=B(l,3)+ z*A(l+m, jtn)i end B(1,3)=B(i,-)/zsum For a 128x 128 input image and R= 3, the run time is 53.3 seconds without acceleration and 0.68 seconds with acceleration 5》 Vectorization A computation is vectorized by taking advantage of vector operations. A variety of programming situations can be vectorized, and often improving speed to 10 times faster or even better. Vectorization is one of the Inost general alld effective techliques for writing fast M-code 5.1 Vectorized Computations Many standard MATlab functions are "vectorized": they can operate on an array as if the function had b plied individually to cvcry clement >>sqrt(1,4;9,15) ans 2 3 4 Consider the following function function d= minDist o Find the min distance between a sct of points and the origin nPoints lengt(x)i d= zeros(nPoints,-) te distance f in七 grt(x(k) y(k)^2 d= min(d)i o Get the minimum distance For every point, its distance from the origin is computed and stored in d. For speed, array d is preallocated (see Section 3). The minimum distance is then found with min. To vectorize the distance computation, replace the for loop with vector operations function d= mi Distance(x,y, z o ind the min distance between a set of points and the origin d=sqrt(x.2+y.2+z.-2); Compute distance for every point d- min(d)i g Get the minimum distance The modified code performs the distance computation with vector operations. The x, y and z arrays are first squared using the per-element power operator,"(the per-element operators for multiplication and division are . and./). The squared components are added with vector addition. Fina ly, the square root of the vector suIn is computed per eleInent, yielding all array of distances. (A further improvement: it is equivalent to compute d sqrt(min(x. 2+y. 2+z. 2))).

...展开详情
试读 33P Writing Fast MATLAB Code
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
您会向同学/朋友/同事推荐我们的CSDN下载吗?
谢谢参与!您的真实评价是我们改进的动力~
上传资源赚钱or赚积分
最新推荐
Writing Fast MATLAB Code 10积分/C币 立即下载
1/33
Writing Fast MATLAB Code第1页
Writing Fast MATLAB Code第2页
Writing Fast MATLAB Code第3页
Writing Fast MATLAB Code第4页
Writing Fast MATLAB Code第5页
Writing Fast MATLAB Code第6页
Writing Fast MATLAB Code第7页

试读结束, 可继续读3页

10积分/C币 立即下载 >