L – Modeling and Simulating Social Systems with MATLAB

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Programming Languages Structure
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Parallel Computing with Matlab CBI Lab Parallel Computing Toolbox TM An Introduction Oct. 27, 2011 By: CBI Development Team.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: Φ Fax:
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
NETW3005 Virtual Memory. Reading For this lecture, you should have read Chapter 9 (Sections 1-7). NETW3005 (Operating Systems) Lecture 08 - Virtual Memory2.
December 13, G raphical A symmetric P rocessing Prototype Presentation December 13, 2004.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Bill Tucker Austin Community College COSC 1315
L – Modeling and Simulating Social Systems with MATLAB
Using Ada-C/C++ Changer as a Converter Automatically convert to C/C++ to reuse or redeploy your Ada code Eliminate the need for a costly and.
Code Optimization.
User-Written Functions
Introduction to the C Language
L – Modeling and Simulating Social Systems with MATLAB
Repetition Structures Chapter 9
L – Modeling and Simulating Social Systems with MATLAB
Operating Systems (CS 340 D)
GC101 Introduction to computers and programs
Topics Introduction to Repetition Structures
L – Modeling and Simulating Social Systems with MATLAB
Lecture: MATLAB Chapter 1 Introduction
MatLab Programming By Kishan Kathiriya.
L – Modeling and Simulating Social Systems with MATLAB
Representation, Syntax, Paradigms, Types
L – Modeling and Simulating Social Systems with MATLAB
L – Modeling and Simulating Social Systems with MATLAB
L – Modeling and Simulating Social Systems with MATLAB
ADVANTAGES OF SIMULATION
Lecture 3 of Computer Science II
Other Kinds of Arrays Chapter 11
Java programming lecture one
Lecture 1 Runtime environments.
Scripts & Functions Scripts and functions are contained in .m-files
CS190/295 Programming in Python for Life Sciences: Lecture 1
Chapter 5 - Functions Outline 5.1 Introduction
Introduction to the C Language
Chapter 10 Programming Fundamentals with JavaScript
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Machine Independent Features
Representation, Syntax, Paradigms, Types
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Loops CIS 40 – Introduction to Programming in Python
Matlab tutorial course
Threads Chapter 4.
CMP 131 Introduction to Computer Programming
Representation, Syntax, Paradigms, Types
Loop Statements & Vectorizing Code
Introduction to MATLAB
Mastering Memory Modes
Representation, Syntax, Paradigms, Types
Tonga Institute of Higher Education IT 141: Information Systems
Lecture 1 Runtime environments.
Tonga Institute of Higher Education IT 141: Information Systems
Module Recognition Algorithms
Loop Statements & Vectorizing Code
SPL – PS1 Introduction to C++.
Presentation transcript:

851-0585-04L – Modeling and Simulating Social Systems with MATLAB 19.05.2018 851-0585-04L – Modeling and Simulating Social Systems with MATLAB Lecture 8 – Simulation Speed & Parallelization Karsten Donnay and Stefano Balietti Chair of Sociology, in particular of Modeling and Simulation © ETH Zürich | © ETH Zürich |

Schedule of the course Introduction to MATLAB 19.05.2018 Schedule of the course Introduction to MATLAB 21.02. 28.02. 07.03. 14.03. 21.03. 28.03. 04.04. 18.04. 02.05. 09.05. 16.05. 23.05. 30.05. Introduction to social-science modeling and simulation Working on projects (seminar theses) Handing in seminar thesis and giving a presentation (final deadlines to be communicated)

Schedule of the course Introduction to MATLAB 19.05.2018 Schedule of the course Introduction to MATLAB 21.02. 28.02. 07.03. 14.03. 21.03. 28.03. 04.04. 18.04. 02.05. 09.05. 16.05. 23.05. 30.05. last two regular lectures of the term Working on projects (seminar theses) Speed & Parallelization Scientific Writing Handing in seminar thesis and giving a presentation (final deadlines to be communicated)

Goals of Lecture 8: students will 19.05.2018 Goals of Lecture 8: students will Refresh knowledge on continuous simulations acquired in lecture 7, through brief repetition of the main points. Get an overview of potential issues that might affect computational performance/speed in MATLAB and learn strategies to avoid performance loss. This lecture emphasizes both the importance of efficient program design and strategies to avoid MATLAB- specific performance issues. Understand the basics of parallelization and get to know some of the options available through MATLAB’s parallel computing toolbox.

Repetition transition from discrete spatial models to continuous spatial models where computational agents are treated analogous to particles in Physics random walk introduced as one generic microscopic agent dynamic that microscopically leads to diffusion of the whole ensemble of agents (micro-macro link) concept of random walk is not limited to spatial dynamics; successfully used for example also in modeling of financial markets continuous models vs. discrete models (CA, lattice): depending on context one or the other can be more suitable, both approaches are used in the literature!

How To Optimize Your Program General Remarks plan your program/ work flow before implementing the program only perform the minimal number of computational steps necessary to obtain results store intermediate results and re-use them use global variables; do not return and store every variable, only those you need as output do not visualize in real time if not necessary

How To Optimize Your Program Strategy the design and structure of your program is the place to most easily gain performance use functions to run your program, they are best performance-optimized in MATLAB! subroutines both improve performance and readability of your code in a second step optimize each routine for individual speed & test its performance

How To Optimize Your Program Possible Program Structure define and initialize all variables in a script file that serves as the main of your program execute the actual program as function with the above variables as inputs automated analysis, visualization etc. of the simulation output may then be implemented in the main file using the outputs of the function that runs the actual program

MATLAB-specific Performance Issues Memory Preallocation data structures (arrays, cell arrays) in MATLAB have a default memory allocated when created without specified size resizing data structures is both bad for performance and memory efficiency! whenever possible initialize a data structure such that it does not have to be resized while the program is running use commands zeros(n,m), cell(n,m)

MATLAB-specific Performance Issues Limit the Complexity of The Program MATLAB has a limit on the complexity of program code it can interpret! subdivide your program in routines of which each is independently implemented as a function avoid excessive use of if… else… statements, in particular multiply nested expressions “minimalistic” programming will not only improve performance but also greatly improve readability!

MATLAB-specific Performance Issues Variable Casting do not recast your data structures during program execution in particular, avoid storing the “wrong” data type in a data structure (for example a complex number or a string in an array of type double) MATLAB is otherwise forced to recast the data structure! the MATLAB default number format is double; if you explicitly need another number format, use for example zeros(10, ‘int32’) to initialize the array

MATLAB-specific Performance Issues Short-circuit Operators in logical operations make use of the short-circuit operators that MATLAB provides, i.e. use && and || instead of & and | advantage: in a complicated logical statement MATLAB only evaluates the first expression if this already returns false, e.g. in (x >= 3) && (y > 4) it stops after the first argument if x < 3 evaluating as few Boolean expressions as possible is a direct performance boost

MATLAB-specific Performance Issues Overloading Build-in Functions MATLAB is optimized for performance of its build-in functions other than for example in C++, overloading a build in function can lead to performance losses since you might interfere with particular optimizations in the build-in function MATLAB is a closed source software, you usually do not know what you are tempering with our advice: stay away from overloading build-in functions!

MATLAB-specific Performance Issues MATLAB Specifics MATLAB is best optimized for the execution of functions, use them over scripts whenever possible if you need to store data structures outside your program, use the MATLAB ‘save’ and ‘load’ commands they are superior to routines like ‘fread’ or ‘fwrite’ (faster and less memory fragmentation) avoid running CPU and/or memory intensive programs at the same time as MATLAB OR prioritize MATLAB

MATLAB-specific Performance Issues Vectorizing a large performance gain can achieved by vectorizing ‘for’ or ‘while’ loops instead of iteratively calculating the results, the loops are expressed as matrix operations for which MATLAB is optimized e.g. instead of simply use for i=1:10 indices=1:10 x(i)=i^2 x=indices.^2 end

MATLAB-specific Performance Issues Vectorizing there are a number of functions specifically used in the context of vectorized computations, examples are meshgrid or reshape there is an exhaustive documentation of vectorizing techniques available on the Internet a quick overview and a list of useful functions in MATLAB when vectorizing computations may be found here, a more detailed introduction here

A word of caution… the fact that MATLAB is optimized for matrix operations does not in every case mean that such a routine is faster!! vectorizing a computation can be quite costly and greatly affect the performance MATLAB uses an just-in-time (JIT) compiler that recognizes high level commands and replaces them with native machine instructions This can for example greatly accelerate loops! in the end you have to test your code to see what is faster!!

Measuring performance MATLAB has a profiler that tracks the performance of your code at the resolution of lines it may be called with profile on, the command profile viewer stops the profiler and displays the results using the profiler make sure to first optimize the slowest sections of your code as the performance gain is the largest it is important to test the code for the full load of the program, it might perform differently for small/ large data structures! the MATLAB documentation of the profiler may be found here

Measuring performance alternatively use the tic and toc commands to test a particular routine tic starts the time counter and toc stops the timer & displays the elapsed time it is often convenient to store the timer results in a variable, the MATLAB default is elapsedTime = toc you can also test several routines simultaneously using the ticID and the the specific timing command toc(ticID) documentation may be found here

Parallelization in MATLAB When Is It Useful? when performing large number of independent operations MATLAB’s parallel computing features are useful e.g when the same simulation has to be run for multiple parameter combinations parallelization boosts performance note that the data structures and program routines have to be parallelizable you also require multiple cores on your computer or access to a computational cluster

Parallelization in MATLAB The Toolbox the toolbox comes with a number of high level programming constructs such as the parfor loop a number of build-in routines automatically parallelize their work stream if possible with your hardware build-in distributed computing interface supports various schedulers like Platform LSF®, Microsoft® Windows® Compute Cluster Server & HPC Server 2008 or Altair PBS Pro® newest MATLAB version also supports GPU computing

Projects There are no exercises today, please work on your projects! We would like to remind you that the oral project presentations will start in the week of the 23rd of May, the written project reports will be due 48 hours before your presentation

References MATLAB documentation on performance Manual on writing fast MATLAB code MATLAB Technical Note on code vectorization MATLAB Parallel Computing Toolbox Short-circuit operators in MATLAB