Optimizing and Accelerating Your MATLAB Code

Similar documents
Speeding up MATLAB Applications Sean de Wolski Application Engineer

Parallel and Distributed Computing with MATLAB Gerardo Hernández Manager, Application Engineer

Parallel and Distributed Computing with MATLAB The MathWorks, Inc. 1

Mit MATLAB auf der Überholspur Methoden zur Beschleunigung von MATLAB Anwendungen

Speeding up MATLAB Applications The MathWorks, Inc.

개발과정에서의 MATLAB 과 C 의연동 ( 영상처리분야 )

MATLAB to iphone Made Easy

Mit MATLAB auf der Überholspur Methoden zur Beschleunigung von MATLAB Anwendungen

Multicore Computer, GPU 및 Cluster 환경에서의 MATLAB Parallel Computing 기능

컴퓨터비전의최신기술 : Deep Learning, 3D Vision and Embedded Vision

Accelerating System Simulations

Scaling MATLAB. for Your Organisation and Beyond. Rory Adams The MathWorks, Inc. 1

Modeling a 4G LTE System in MATLAB

Parallel Computing with MATLAB

Integrate MATLAB Analytics into Enterprise Applications

2015 The MathWorks, Inc. 1

Integrate MATLAB Analytics into Enterprise Applications

Modeling a 4G LTE System in MATLAB

Getting Started with MATLAB Francesca Perino

Integrate MATLAB Analytics into Enterprise Applications

2^48 - keine Angst vor großen Datensätzen in MATLAB

Technical Computing with MATLAB

MATLAB AND PARALLEL COMPUTING

Using Intel Math Kernel Library with MathWorks* MATLAB* on Intel Xeon Phi Coprocessor System

INTRODUCTION TO MATLAB PARALLEL COMPUTING TOOLBOX

MATLAB Based Optimization Techniques and Parallel Computing

2015 The MathWorks, Inc. 1

MATLAB. Senior Application Engineer The MathWorks Korea The MathWorks, Inc. 2

Deep learning in MATLAB From Concept to CUDA Code

Daniel D. Warner. May 31, Introduction to Parallel Matlab. Daniel D. Warner. Introduction. Matlab s 5-fold way. Basic Matlab Example

Embarquez votre Intelligence Artificielle (IA) sur CPU, GPU et FPGA

Large Data in MATLAB: A Seismic Data Processing Case Study U. M. Sundar Senior Application Engineer

HPC with Multicore and GPUs

System Requirements & Platform Availability by Product for R2016b

Introduction to C and HDL Code Generation from MATLAB

MATLAB Distributed Computing Server Release Notes

What s New in MATLAB and Simulink The MathWorks, Inc. 1

High Performance and GPU Computing in MATLAB

Scaling up MATLAB Analytics Marta Wilczkowiak, PhD Senior Applications Engineer MathWorks

Using Parallel Computing Toolbox to accelerate the Video and Image Processing Speed. Develop parallel code interactively

What's New in MATLAB for Engineering Data Analytics?

Moving MATLAB Algorithms into Complete Designs with Fixed-Point Simulation and Code Generation

CMSC 714 Lecture 6 MPI vs. OpenMP and OpenACC. Guest Lecturer: Sukhyun Song (original slides by Alan Sussman)

GPU LIBRARY ADVISOR. DA _v8.0 September Application Note

Parallel Computing with Matlab and R

What s New in MATLAB and Simulink

Approaches to acceleration: GPUs vs Intel MIC. Fabio AFFINITO SCAI department

Computational Methods in Statistics with Applications A Numerical Point of View. Large Data Sets. L. Eldén. March 2016

MATLAB 7. The Language of Technical Computing KEY FEATURES

Introduction to MATLAB

Parallel Cosegmentation via Submodular Optimization on Anisotropic Diffusion

Data Analytics with MATLAB. Tackling the Challenges of Big Data

2015 The MathWorks, Inc. 1

Practical Introduction to CUDA and GPU

Accelerated Machine Learning Algorithms in Python

High-Performance Data Loading and Augmentation for Deep Neural Network Training

Speeding up Simulink. Murali Yeddanapudi The MathWorks, Inc. 1

Intel Math Kernel Library 10.3

A MATLAB Interface to the GPU

Accelerating Finite Element Analysis in MATLAB with Parallel Computing

Quantum ESPRESSO on GPU accelerated systems

Speedup Altair RADIOSS Solvers Using NVIDIA GPU

MATLAB 7 Getting Started Guide

ACCELERATING CFD AND RESERVOIR SIMULATIONS WITH ALGEBRAIC MULTI GRID Chris Gottbrath, Nov 2016

Cell Processor and Playstation 3

MATLAB 에서작업한응용프로그램의공유 : App 에서부터웹서비스까지

What s New in MATLAB and Simulink

What s New in MATLAB and Simulink Young Joon Lee Principal Application Engineer

Audio Signal Processing in MATLAB Youssef Abdelilah Senior Product Manager

Intel Performance Libraries

Summer 2009 REU: Introduction to Some Advanced Topics in Computational Mathematics

Data Analysis with MATLAB. Steve Lantz Senior Research Associate Cornell CAC

How to Write Fast Code , spring st Lecture, Jan. 14 th

Model-Based Design for High Integrity Software Development Mike Anthony Senior Application Engineer The MathWorks, Inc.

An Auto-Tuning Framework for Parallel Multicore Stencil Computations

Accelerating Simulink Optimization, Code Generation & Test Automation Through Parallelization

Georgia Institute of Technology Center for Signal and Image Processing Steve Conover February 2009

Code optimization techniques

Technology for a better society. hetcomp.com

MATLAB Parallel Computing Toolbox Benchmark for an Embarrassingly Parallel Application

MATLAB on BioHPC. portal.biohpc.swmed.edu Updated for

Parallel MATLAB at VT

Lecture x: MATLAB - advanced use cases

High-Performance and Parallel Computing

3. Replace any row by the sum of that row and a constant multiple of any other row.

A Fast GPU-Based Approach to Branchless Distance-Driven Projection and Back-Projection in Cone Beam CT

General Purpose GPU Computing in Partial Wave Analysis

Model-Based Design: Design with Simulation in Simulink

What s New in MATLAB May 16, 2017

Simulink 모델과 C/C++ 코드에대한매스웍스의정형검증툴소개 The MathWorks, Inc. 1

Parallel Computing with MATLAB

Deploying Deep Learning Networks to Embedded GPUs and CPUs

GPU Acceleration of SAR/ISAR Imaging Algorithms

Implementing MATLAB Algorithms in FPGAs and ASICs By Alexander Schreiber Senior Application Engineer MathWorks

Deep Learning: Transforming Engineering and Science The MathWorks, Inc.

Very fast simulation of nonlinear water waves in very large numerical wave tanks on affordable graphics cards

Implementation of the finite-difference method for solving Maxwell`s equations in MATLAB language on a GPU

A Comprehensive Study on the Performance of Implicit LS-DYNA

The Use of Computing Clusters and Automatic Code Generation to Speed Up Simulation Tasks

What s New in MATLAB and Simulink

Transcription:

Optimizing and Accelerating Your MATLAB Code Sofia Mosesson Senior Application Engineer 2016 The MathWorks, Inc. 1

Agenda Optimizing for loops and using vector and matrix operations Indexing in different ways Finding and addressing bottlenecks Generating C code and incorporating it into your application Utilizing additional hardware and processing power 2

Example: Block Processing Images Calculate a function at grid points Take the mean of larger blocks Analyze and improve performance 3

Effect of Not Preallocating Memory x(1) = 4 x(2) = 7 x(3) = 12 4 4 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 X(2) = 7 0x0000 4 7 0x0008 0x0010 0x0018 0x0020 0x0028 X(3) = 12 0x0000 4 7 0x0008 0x0010 4 7 12 0x0018 0x0020 0x0028 0x0030 0x0038 0x0030 0x0038 0x0030 0x0038 4

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

MATLAB Underlying Technologies Execution Engine (>=R2015b) All MATLAB code is just-in-time compiled Improves Nth run performance Commercial Libraries BLAS: Basic Linear Algebra Subroutines LAPACK: Linear Algebra Package IPP: Intel Performance Primitives FFTW: Fastest Fourier Transform in the West 6

Other Best Practices Avoid clear all Use clear or clearvars instead Avoid introspection functions E.g. dbstack, inputname, exist, whos http://www.mathworks.com/help/releases/r2015b/matlab/matlab_prog/techniques-for-improving-performance.html 7

Agenda Optimizing for loops and using vector and matrix operations Indexing in different ways Finding and addressing bottlenecks Generating C code and incorporating it into your application Utilizing additional hardware and processing power 8

Exampel: Indexing Loop over rows versus columns x 0.123 0.469 0.811 0.532 0.103 0.775 y = x; if x(r,c) < 0.5 y(r,c) = 0 end y 0.123 0.469 0.811 0.532 0.103 0.775 Use find command 0.320 0.382 0.295 0.320 0.382 0.295 Use logical indexing 0 0 0.811 0.532 0 0.775 0 0 0 9

Matrix processing in memory 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 >> x = magic(3) x = 8 1 6 3 5 7 4 9 2 8 3 4 1 5 9 6 7 2 0x0000 0x0008 0x0010 0x0018 0x0020 0x0028 0x0030 0x0038 0x0040 0x0048 0x0050 10

Agenda Optimizing for loops and using vector and matrix operations Indexing in different ways Finding and addressing bottlenecks Generating C code and incorporating it into your application Utilizing additional hardware and processing power 12

Example: Addressing Bottlenecks Run and time program Identify bottlenecks Improve run time 13

Profiler Total number of function calls Time per function call Self time in a function call Statement coverage of code 14

Best Practices Minimize file I/O Reuse existing graphics components Avoid printing to Command Window 15

Agenda Optimizing for loops and using vector and matrix operations Indexing in different ways Finding and addressing bottlenecks Generating C code and incorporating it into your application Utilizing additional hardware and processing power 17

Why Engineers Translate MATLAB to C Implement C code on processors or hand off to software engineers Accelerate MATLAB algorithms.c MEX 18

Challenges with Manual Translation of MATLAB to C Separate functional and implementation specifications Leads to multiple implementations which are inconsistent Hard to modify requirements during development Difficult to keep MATLAB code and C code in sync Manual coding errors iterate.c.exe Time consuming and expensive process Algorithm Design in MATLAB Re-code in C/C++ MEX.lib.dll 19

Automatic Translation of MATLAB to C Maintain one design in MATLAB Design faster and get to C quickly Test more systematically and frequently iterate.c.exe Spend more time improving algorithms in MATLAB.lib.dll verify / accelerate MEX 20

Acceleration Using MEX Speedup factor will vary When you may see a speedup: Often for communications or signal processing Likely for loops with states or when vectorization is not possible Always for fixed point When you may not see a speedup: MATLAB implicitly multithreads computation Built in functions that call BLAS or IPP 21

Supported Language Features and Functions New functions and features are supported each release Matrices and Arrays Data Types Programming Constructs Functions Matrix operations N-dimensional arrays Subscripting Frames Persistent variables Global variables Complex numbers Integer math Double/single-precision Fixed-point arithmetic Characters Structures Cell arrays Numeric class Variable-sized data MATLAB Class System objects Arithmetic, relational, and logical operators Program control (if, for, while, switch) MATLAB functions and subfunctions Variable-length argument lists Function handles Supported algorithms More than 1100 MATLAB operators (R2015b), functions, and System objects for: Communications Computer vision Image processing Phased Array signal processing Robotics System Toolbox Signal processing Statistic & Machine Learning Toolbox http://www.mathworks.com/help/coder/language-supported-for-code-generation.html 22

Agenda Optimizing for loops and using vector and matrix operations Indexing in different ways Finding and addressing bottlenecks Generating C code and incorporating it into your application Utilizing additional hardware and processing power 23

Parallel Computing Paradigm Multicore Desktops Multicore Desktop MATLAB Desktop (client) Core 1 Core 2 Core 5 Core 6 MATLAB multicore 24

Parallel Computing Paradigm Cluster Hardware Cluster of computers Core 1 Core 2 Core 1 Core 2 Core 5 Core 6 Core 5 Core 6 MATLAB Desktop (client) Core 1 Core 2 Core 1 Core 2 Core 5 Core 6 Core 5 Core 6 25

Parallel Computing Paradigm NVIDIA GPUs Using NVIDIA GPUs MATLAB Desktop (client) GPU cores Device Memory 26

Explicit Parallelism: Independent Tasks or Iterations Parallel for loops Ideal problem for parallel computing No dependencies or communications between tasks Examples: parameter sweeps, Monte Carlo simulations MATLAB Desktop (Client) Time Time 27

Example: Parameter Sweep Parallel for-loops M x + C x + Kx = F Deflection of customizable truss Initial dynamic load, damping Parameters: Height of truss Cross sectional area of truss elements Convert for to parfor Use pool of MATLAB workers 28

Offloading Serial Computations job = batch(...); MATLAB Desktop (Client) Work Result 29

Offloading and Scaling Computations job = batch(..., 'Pool', n); MATLAB Desktop (Client) Work Result 30

Migrate to Cluster / Cloud Use MATLAB Distributed Computing Server Change hardware without changing algorithm 31

Speed-up using NVIDIA GPUs Ideal Problems Massively Parallel and/or Vectorized operations Computationally Intensive Algorithm consists of supported functions 300+ GPU-enabled MATLAB functions Additional GPU-enabled Toolboxes Neural Networks Image Processing Communications Signal Processing... Learn More 32

Example: Wave Equation Solve 2 nd order wave equation with spectral methods Use CPU and GPU 33

Agenda Optimizing for loops and using vector and matrix operations Indexing in different ways Finding and addressing bottlenecks Generating C code and incorporating it into your application Utilizing additional hardware and processing power 34

Key Takeaways Consider the performance benefits of vector and matrix operations Analyze your code for bottlenecks to address the critical areas Leverage MATLAB Coder to speed up functions with generated C code Leverage parallel computing tools to take advantage of additional hardware 35

Some Other Valuable Resources MATLAB Documentation MATLAB Advanced Software Development Performance and Memory Accelerating MATLAB algorithms and applications http://www.mathworks.com/company/newsletters/articles/accelerating-matlabalgorithms-and-applications.html Loren Shure s Blog: The Art of MATLAB http://blogs.mathworks.com/loren/ MATLAB Question and Answers Site: MATLAB Answers http://www.mathworks.com/matlabcentral/answers/ 36

MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. 2015 The MathWorks, Inc. 2016 The MathWorks, Inc. 37