1 © 2012 The MathWorks, Inc. Speeding up MATLAB Applications.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
Tutorial on Matlab and OpenCV Rui Ma TA of CMPT 414 May 14, 2013 Office hours: Fridays 11:00-12:00, CSIL TA Office 1 (ASB 9838)
OpenMP Optimization National Supercomputing Service Swiss National Supercomputing Center.
Introduction to Matlab
Matlab Programming Huajun Wang Department of Earth Sciences, Zhejiang University Jan 25,2012.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
CMPE 421 Parallel Computer Architecture MEMORY SYSTEM.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Optimization of Sparse Matrix Kernels for Data Mining Eun-Jin Im and Katherine Yelick U.C.Berkeley.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
C++ for Engineers and Scientists Third Edition
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
© 2004 The MathWorks, Inc. 1 MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Mex. Introduction to MEX MEX = Matlab EXecutable – Dynamically Linked Libraries – Used like a.m function – Written in C (or Fortran)
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CS 598 Scripting Languages Design and Implementation 2. MATLAB 1.
Introduction to C & C++ Lecture 10 – library JJCAO.
Efficiency and Flexibility of Jagged Arrays Geir Gundersen Department of Informatics University of Bergen Norway Joint work with Trond Steihaug.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
41 - 4/24/2000AME 150L1 Solving Engineering Problems, Integrating Equations.
JAVA AND MATRIX COMPUTATION
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: Φ Fax:
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Working with Arrays in MATLAB
Introduction to C and CMex E177 April 1, Copyright , Andy Packard. This work is licensed under the.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Matlab Programming for Engineers
Radix Sort and Hash-Join for Vector Computers Ripal Nathuji 6.893: Advanced VLSI Computer Architecture 10/12/00.
Linear Algebra Libraries: BLAS, LAPACK, ScaLAPACK, PLASMA, MAGMA
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Improving Matlab Performance CS1114
PIDX PIDX - a parallel API to capture the data models used by HPC application and write it out in an IDX format. PIDX enables simulations to write out.
1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
6.S093: Visual Recognition through Machine Learning Competition MATLAB tutorial.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
“Moh’d Sami” AshhabSummer 2008University of Jordan MATLAB By (Mohammed Sami) Ashhab University of Jordan Summer 2008.
An Introduction to Programming in Matlab Emily Blumenthal
1 Introduction to Matlab. 2 What is Matlab? Matlab is basically a high level language which has many specialized toolboxes for making things easier for.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrix Multiplication in CUDA Kyeo-Reh Park Kyeo-Reh Park Nuclear & Quantum EngineeringNuclear & Quantum Engineering.
Optimizing the Performance of Sparse Matrix-Vector Multiplication
Cache Memories CSE 238/2038/2138: Systems Programming
Sparse Matrix-Vector Multiplication (Sparsity, Bebop)
7 Arrays.
Matlab tutorial course
Loop Statements & Vectorizing Code
MATLAB Programming Basics Copyright © Software Carpentry 2011
Loop Statements & Vectorizing Code
Working with Arrays in MATLAB
Presentation transcript:

1 © 2012 The MathWorks, Inc. Speeding up MATLAB Applications

2 Agenda  Leveraging the power of vector and matrix operations in MATLAB ® - Demonstration: Preallocation and vectorization  How does MATLAB ® store and provide access to its’ variables? - Demonstration: Row- vs. Column-major indexing - Demonstration: Logical indexing  Addressing bottlenecks - Demonstration: Profiling code in MATLAB ®  Memory considerations - Demonstration: Copy-on-write  Summary and Q&A

3 Example: Block Processing Images  Evaluate function at grid points  Reevaluate function over larger blocks  Compare the results  Evaluate code performance

4 Summary of Example  Used built-in timing functions >> tic >> toc  Used Code Analyzer to find suboptimal code  Preallocated arrays  Vectorized code

5 Effect of Not Preallocating Memory >> x = 4 >> x(2) = 7 >> x(3) = 12 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x0038 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x0038 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x X(3) = 12X(2) = 7 Resizing Arrays is Expensive

6 Benefit of Preallocation >> x = zeros(3,1) >> x(1) = 4 >> x(2) = 7 >> x(3) = 12 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x0038 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x Reduced Memory Operations

7 Agenda  Leveraging the power of vector and matrix operations in MATLAB ® - Demonstration: Preallocation and vectorization  How does MATLAB ® store and provide access to its’ variables? - Demonstration: Row- vs. Column-major indexing - Demonstration: Logical indexing  Addressing bottlenecks - Demonstration: Profiling code in MATLAB ®  Memory considerations - Demonstration: Copy-on-write  Summary and Q&A

8 Data Storage of MATLAB Arrays What is the fastest way to process MATLAB matrices with for loops? (i.e. de-vectorize) a) Down the columns b) Along the rows c) Doesn't matter 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x0038 0x0040 0x0048 0x0050 0x0058 0x0060 0x0068 See the June 2007 article in “The MathWorks News and Notes”: >> x = magic(3) x = Column-Major Memory Storage

9 Indexing into MATLAB Arrays  Subscripted –Access elements by rows and columns  Linear –Access elements with a single number  Logical –Access elements with logical operations or mask ,11,21,3 2,12,22,3 3,13,23,3 Linear indexing Subscripted indexing ind2sub sub2ind

10 Speed and Memory Usage  Balance vectorization and memory usage –Use bsxfun instead of functions such as repmat –Reduce size of arrays to smaller blocks for block processing  Consider using sparse matrices –Less Memory: Store only nonzero elements and their indices –Faster: Eliminate operations on zero elements –Blog Post - Creating Sparse Finite Element Matrices element-matrices-in-matlab/ element-matrices-in-matlab/

11 MATLAB Underlying Technologies  Commercial libraries –BLAS:Basic Linear Algebra Subroutines (multithreaded) –LAPACK: Linear Algebra Package –etc.  JIT/Accelerator –Improves looping –Generates on-the-fly multithreaded code –Continually improving

12 Other Best Practices  Minimize dynamically changing path >> addpath(…) >> fullfile(…)  Use the functional load syntax >> x = load('myvars.mat') x = a: 5 b: 'hello'  Minimize changing variable class >> x = 1; >> xnew = 'hello'; instead of cd(…) instead of load('myvars.mat') instead of x = 'hello';

13 Agenda  Leveraging the power of vector and matrix operations in MATLAB ® - Demonstration: Preallocation and vectorization  How does MATLAB ® store and provide access to its’ variables? - Demonstration: Row- vs. Column-major indexing - Demonstration: Logical indexing  Addressing bottlenecks - Demonstration: Profiling code in MATLAB ®  Memory considerations - Demonstration: Copy-on-write  Summary and Q&A

14 Example: Fitting Data  Load data from multiple files  Extract a specific test  Fit a spline to the data  Write results to Microsoft Excel

15 Summary of Example  Used profiler to analyze code  Targeted significant bottlenecks  Reduced file I/O  Reused figure

16 Acceleration using MEX  Call C or Fortran code directly from MATLAB –Integrate existing code using MEX API –Auto-generate C-based MEX files from MATLAB code using MATLAB Coder  Speed-up factor will vary –May see speedup for state-based for-loops –May not see a speedup when MATLAB code is  Using multithreaded computations  Using optimized libraries (BLAS, FFTW, etc.) c = myFcn(a,b) myFcn.c void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* more C code... */ }

17 Agenda  Leveraging the power of vector and matrix operations in MATLAB ® - Demonstration: Preallocation and vectorization  How does MATLAB ® store and provide access to its’ variables? - Demonstration: Row- vs. Column-major indexing - Demonstration: Logical indexing  Addressing bottlenecks - Demonstration: Profiling code in MATLAB ®  Memory considerations - Demonstration: Copy-on-write  Summary and Q&A

18 MATLAB and Memory Lazy copying (copy-on-write): C{1} = rand(3000,3000); %72Mb C{2} = rand(3000,3000); %72Mb C_new = C; C{1}(1,1) = 2; Arrays are stored in contiguous memory %72Mb %0 Mb

19 In-place Operations When does MATLAB do calculations “in-place”? function y = myfunc(x) y = sin(2*x.^2+3*x+4); function x = myfuncInPlace(x) x = sin(2*x.^2+3*x+4); x = randn(3e7,1); y = myfunc(x); x = myfuncInPlace(x); % In-place x = myfunc(x); % Copy

20 Agenda  Leveraging the power of vector and matrix operations in MATLAB ® - Demonstration: Preallocation and vectorization  How does MATLAB ® store and provide access to its’ variables? - Demonstration: Row- vs. Column-major indexing - Demonstration: Logical indexing  Addressing bottlenecks - Demonstration: Profiling code in MATLAB ®  Memory considerations - Demonstration: Copy-on-write  Summary and Q&A

21 Summary  Consider performance benefit of vector and matrix operations in MATLAB  Preallocate memory and look at your indexing.  Analyze your code for bottlenecks and address most critical items  Never write hard-to-read code unless you have to.

22 Summary (continued…)  MATLAB passes data to functions by value using lazy copy.

23 Sample of Other Performance Resources  MATLAB documentation MATLAB  Programming Fundamentals  Performance  Memory Management Guide  The Art of MATLAB, Loren Shure’s blog blogs.mathworks.com/loren/  MATLAB Answers