Docsity

Optimizing Monte-Carlo Simulation Code in C/C++ using Matlab Compiler - Prof. Jian-Guo Liu, Study Guides, Projects, Research of Mathematics

Toggle sidebar
bg1
Optimization for C/C++ Generated by Matlab Compiler
Qing Xia
Applied Mathematics and Scientific Computation
University of Maryland
Nov. 3, 2003
1. Introduction
In this project, I’ll try to optimize the Monte-Carlo simulation code written by
Matlab6.1. I also want to compare the efficiency of corresponding C/C++
codes generated by Matlab Complier Toolbox (Version 2.1). And I use a bar
chart to show difference of running time with and without optimization.
2. Monte-Carlo Simulation
Monte-Carlo is a technique to calculate the uncertainty in a forecast of future
events. It’s a numerical procedure that uses random numbers to simulate the
stochastic process. Usually, Monte-Carlo Methods are used to overcome the
properties of stochastic variable that prohibits the deterministic methods.
In this project, I’m trying to simulate a stochastic process : Throw a coin into a
unit square and count the number of falling into the quarter circle. Divide it with
the total number of times it falls into the unit square. Thus, I’ll get the expected
value of the coin falling into the quarter circle.
We can find the expected value by calculating a stochastic integration as
below:
∫Sdxdyyxgyxf),(),(
where S is the unit square, D is the unit circle, and
∈
=otherwise
Syx
yxf,0
),(,1
),( ,
∈
=otherwise
Dyx
yxg,0
),(,1
),(
pf3
pf4
pf5

Partial preview of the text

Download Optimizing Monte-Carlo Simulation Code in C/C++ using Matlab Compiler - Prof. Jian-Guo Liu and more Study Guides, Projects, Research Mathematics in PDF only on Docsity!

Optimization for C/C++ Generated by Matlab Compiler

Qing Xia

Applied Mathematics and Scientific Computation

University of Maryland

Nov. 3, 2003

1. Introduction

In this project, I’ll try to optimize the Monte-Carlo simulation code written by Matlab6.1. I also want to compare the efficiency of corresponding C/C++ codes generated by Matlab Complier Toolbox (Version 2.1). And I use a bar chart to show difference of running time with and without optimization.

2. Monte-Carlo Simulation

Monte-Carlo is a technique to calculate the uncertainty in a forecast of future events. It’s a numerical procedure that uses random numbers to simulate the stochastic process. Usually, Monte-Carlo Methods are used to overcome the properties of stochastic variable that prohibits the deterministic methods.

In this project, I’m trying to simulate a stochastic process : Throw a coin into a unit square and count the number of falling into the quarter circle. Divide it with the total number of times it falls into the unit square. Thus, I’ll get the expected value of the coin falling into the quarter circle.

We can find the expected value by calculating a stochastic integration as below:

∫ S f^ ( x , y ) g ( x , y ) dxdy

where S is the unit square, D is the unit circle, and

otherwise

xy S f x y 0 ,

otherwise

x y D g x y 0 ,

Actually, the integral above is equal to pi/4.

The process is shown as Figure 1.

(^00) 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

1 Throw a coin into a unit square

Figure 1. Throw a coin into a unit square

Some words about generating the random values. Since Monte-Carlo method is basic to simulate the stochastic process, it’s important to have REAL random variables. And almost all random variables in Monte -Carlo Simulation are in the end generated by manipulating hypothetical independent uniformly distributed random number on [0,1]. There’re some ready-made algorithms to generate random number. One can find them in the Numerical Recipe or just call the function in the standard math library in C or call the built-in function in Matlab.

3. The Algorithm and Implementation

3. 1 The Algorithm

  1. Generate a random point P(x,y) in the unit square [0,1] [0,1]
  2. If the point lies in the unit circle, then add one to the counter

Toolbox and the C Math Library. Generally, the resulting C code will not be very editable, but it will be much faster than the original m-file code.

Now we use the Matlab Compiler Toolbox (Version 2.1) to compile two codes respectively. (With compiler flag –x) The result (Figure 2) shows that there’s some improvement for the second code, but not obvious. While for the first code, there’s no improvement at all. Actually, even worse, there’s a negati ve effect for the complied m-file. This result is surprising. Since the compiled file will not do the type checking, array bounds checking and dynamic resizing, so it’ll be efficient than the un-compiled m-file.

5. Further Improvement

Now, I’m trying to do some further improvement. And I made some changes for both of two codes.

For the second code (Monte_2.m), I use a nested for-loop instead of a single for-loop. Keep all the other part and I have the third code(Monte_3.m). And you’ll see from Figure 2 , the running time for Monte_3.m is a little bit more than Monte_2.m, but the efficiency of the corresponding compiled m-file is more obvious than that of the second code.

For the first code (Monte_1.m), I changed the structure of the Random Matrix. Now I have two random matrices. One is for x-coordinate, the other for y-coordinate. Also, keep all the other part and I have the fourth code (Monte_4.m). And the amazing thing happens. For the original m-file Monte_4, the running is 53% of the Monte_1, 64% of the Monte_2. And there’s a sharp contrast between the compiled m-file and the un-compiled m-file. We almost boost the efficiency by 57%.

Figure 2. Difference of running time with and without optimization

Publication:

[1] A MATLAB to Fortran 90 Translator and its Effectiveness (1996) , Luiz De Rose, David Padua International Conference on Supercomputing. [2] High-Level Semantic Optimization of Numerical Codes (1999), Vijay Menon, Keshav Pingali, International Conference on Supercomputing. [3] Compiler Techniques For Matlab Programs (1996), Luiz Antônio De Rose. [4] MATLAB Compiler Help Document – the MATHWORKS – 1995. [5] A Case for Source-Level Transformations in MATLAB , vijay Menon, Keshav Pingali.