Professional Documents
Culture Documents
2006 Optimization of The Monte Carlo Code For
2006 Optimization of The Monte Carlo Code For
a r t i c l e i n f o a b s t r a c t
Article history: The Monte Carlo method is frequently used to simulate light transport in turbid media
Received 12 December 2005 because of its simplicity and flexibility, allowing to analyze complicated geometrical struc-
Received in revised form tures. Monte Carlo simulations are, however, time consuming because of the necessity to
27 July 2006 track the paths of individual photons. The time consuming computation is mainly asso-
Accepted 27 July 2006 ciated with the calculation of the logarithmic and trigonometric functions as well as the
generation of pseudo-random numbers. In this paper, the Monte Carlo algorithm was devel-
Keywords: oped and optimized, by approximation of the logarithmic and trigonometric functions. The
Monte Carlo approximations were based on polynomial and rational functions, and the errors of these
Light transport approximations are less than 1% of the values of the original functions. The proposed algo-
Time-resolved reflectance rithm was verified by simulations of the time-resolved reflectance at several source-detector
Distribution of time of flight of separations. The results of the calculation using the approximated algorithm were compared
photons with those of the Monte Carlo simulations obtained with an exact computation of the log-
arithm and trigonometric functions as well as with the solution of the diffusion equation.
The errors of the moments of the simulated distributions of times of flight of photons (total
number of photons, mean time of flight and variance) are less than 2% for a range of optical
properties, typical of living tissues. The proposed approximated algorithm allows to speed
up the Monte Carlo simulations by a factor of 4. The developed code can be used on parallel
machines, allowing for further acceleration.
© 2006 Elsevier Ireland Ltd. All rights reserved.
1. Introduction than the average mean free path of photon in the tissue [8].
The number of the time steps, before the photon is absorbed
Monte Carlo simulations are frequently used in modeling of or scattered, becomes very big with highly scattering media.
light transport in turbid media [1–6]. The method does not To reduce the computation time, certain improvements were
require assumptions on the boundary conditions, which are proposed. In the variable stepsize technique, the stepsize is
necessary in case of the finite element and finite differences chosen to be equal to the distance at which the photon is either
solutions of the diffusion equation and radiative transport scattered or absorbed.
equation [7,4]. To reduce the number of photons required to achieve the
In Monte Carlo simulations of light transport, photons are desired accuracy of the simulation, the variance reduction
traced until they are detected or absorbed. The classical Monte technique [9–11] was proposed. In the classical Monte Carlo
Carlo method, based on the random walk, used fixed step- method, the probability of the absorption per length of the
size of the photon movement, which had to be much shorter photon’s travel is defined by the absorption coefficient. The
∗
Corresponding author. Tel.: +48 226599143; fax: +48 226597030.
E-mail address: norbert@ibib.waw.pl (N.S. Żołek).
0169-2607/$ – see front matter © 2006 Elsevier Ireland Ltd. All rights reserved.
doi:10.1016/j.cmpb.2006.07.007
c o m p u t e r m e t h o d s a n d p r o g r a m s i n b i o m e d i c i n e 8 4 ( 2 0 0 6 ) 50–57 51
absorbed photon disappears. In the variance reduction tech- The path of the photon package consists of straight seg-
nique, a weight is assigned to each photon (or a photon ments between the consecutive scattering events. The dis-
package) as it enters the tissue. After each propagation step, tance between two scattering events, called the free path-
the photon’s weight is reduced, according to the probability length, is calculated using formula (1):
of absorption. A number of Monte Carlo algorithms based
on these techniques was developed [12–14], including par- − ln()
s= , (1)
allel computations [15]. In order to simulate light transport a + s
in respect of different absorption and scattering coefficients,
scaling of results from a single Monte Carlo simulation was where ∈ (0;1) is the uniformly distributed random number,
proposed [16–18]. However, a single Monte Carlo process is and a and s are the absorption and the scattering coeffi-
time consuming, especially with a large source-detector sepa- cients, respectively. If the photon package crosses the bound-
ration, when the considered times of flight of photons are long ary between the media with different optical properties, the
and when the reduced scattering coefficient is large. This long refraction or the reflection is taken into account. At the ini-
computation is the main disadvantage of this method. tial moment, the weight of each photon package is set to 1.
This paper describes optimization of a Monte Carlo algo- The weight decreases at each ith scattering event, according to
rithm in simulation of the time-resolved reflectance. In the the Beer–Lambert law for turbid media, which can be approx-
parts of the code that are most critical for the computation imated by the following formula:
time, approximated logarithmic and trigonometric functions
which allowed to speed up the simulations were used. The s
wi+1 = wi (2)
proposed algorithm was verified by simulations of the time- a + s
resolved reflectance at several source-detector separations.
The results of the application of the approximated algorithm The photon package is treated as absorbed, when its weight
were compared with the results of the Monte Carlo simula- becomes lower than the prescribed threshold (in our case, the
tions obtained with an exact computation of the logarithm threshold is set to be 10−20 ).
and trigonometric functions and with the solution of the dif- At every scattering event, a new direction of the photon
fusion equation. package movement, represented by the directional cosines, is
determined. Simulation of anisotropic scattering can be done
using the Henyey–Greenstein phase function, which satisfac-
2. Monte Carlo simulations of light torily approximates anisotropic scattering in tissues [19] and
transport in turbid media allows to omit the calculation of the explicit value of the
polar deflection angle [13,11]. Only the cosine of this angle
Photon packages enter the medium under investigation at the needs to be generated using random number and anisotropy
emission point and are traced until they are absorbed or exit factor g defined as the mean cosine of the scattering angle.
from the medium. The tissue is regarded as a structure con- The azimuthal angle is determined by the random number
sisting of layers parallel to the surface, with a fixed thickness with the uniform distribution in the range [0;2]. In the pre-
and optical properties (a , absorption coefficient; s , scatter- sented simulations, the isotropic scattering is assumed with
ing coefficient; n, refractive index) in each layer. This assump- the cosine of the polar deflection angle represented by the ran-
tion is fulfilled for measurements on head, in which the skin, dom number between −1 and 1.
skull, cerebrospinal fluid and grey matter layers should be con-
sidered. The radial symmetry (around the axis perpendicular
to the surface) of the structure (Fig. 1) allows to assume that the 3. Code optimization
detector forms a ring located around the emission point. Infor-
mation about the time, spent by the photon package in the For further optimization, weighted Monte Carlo code of light
tissue, is collected in respect to each detected photon package. transport in layered media – MCML [13] – was used. In the first
After simulation of many photon packages, the distribution of step the code was modified in such a way that the analysis of
times of flight of photons (DTOF) is calculated. times of the flight of photons is possible. Then the analysis of
the computation was performed, showing that the calculation
of the directional cosines of the azimuthal and polar deflec-
tion angles as well as the free pathlength and generation of the
random numbers were the most time consuming operations.
The tests on MCML code (Table 1) showed that most of the
computation time was taken for calculation of the new photon
package direction after the scattering event (37% of the total
simulation time) and for calculation of the free pathlength of
the photon package (34% of the total simulation time), which is
in agreement with Ref. [14]. These estimations suggest that an
acceleration of Monte Carlo calculations needs faster approx-
Fig. 1 – Layered structure and path of an exemplary photon imations of the logarithmic and trigonometric functions than
package. The azimuthal angle (˛) and polar deflection angle those used in the most programming languages and applica-
(ˇ) at single scattering event are shown. tion of the fastest pseudo-random number generator.
52 c o m p u t e r m e t h o d s a n d p r o g r a m s i n b i o m e d i c i n e 8 4 ( 2 0 0 6 ) 50–57
was used:
Table 1 – The percentage of the total running time used
by most time consuming computations ⎧
⎨ a + bx2 + cx4 , for x ∈ 0; − 0.12
Type of computations The total running time
cos(x) ≈ appcos(x) = 2
(%) ⎩ − x, for x ∈ − 0.12;
2 2 2
Calculation of new direction after 37.45 (3)
scattering event
Calculation of free pathlength 34.57
Random number generator “ran3” 20.01
The approximating function (3) was found by searching for
Calculation of new position of 6.04
photon package the best fit to the exact values of the cosine function. With
arguments from the range of the (0 to /2 − 0.12) for calculation
Monte Carlo simulations performed for homogeneous semi-infinite of appcos only five floating point operations (three multiplica-
medium (n = 1.4, a = 0.18 cm−1 , and s = 19 cm−1 ).
tions and two additions) are required. At least the same num-
ber of operations is needed, when floating point sine/cosine
calculations are performed using lookup tables [28]. For argu-
Calculation of the new direction of the photon package ments from the range of [/2 − 0.12,/2], the cosine function
after the scattering event requires calculation of trigonomet- becomes better approximated when the linear function is
ric functions of the azimuthal and polar deflection angles. used. An analysis of the relative error of the approximation
Cosine of the polar deflection angle is generated directly from defined by:
the random number and Henyey–Greenstein phase function.
Calculation of the sine is performed using the cosine and the cos(x) − appcos(x)
square root function which, in most computers, is realized by cc = × 100% (4)
cos(x)
the hardware. Though, one cosine of the azimuthal angle has
to be calculated each time when a scattering event occurs. was carried out, showing that the error was less than 1% in the
As it was noticed, the current Java releases (Version 1.4.2 whole range of the arguments. The parameters of the poly-
and higher) are, at least, as fast as the programs written in nomial are as follows: a = 0.999, b = −0.497, and c = 0.0376. The
C/C++ [20,21] in most situations. In the optimization process accuracy of the approximation in the percentage of the exact
described here, the MCML algorithm [13] was rewritten in the values of the cosine function is shown in Fig. 2.
Java programming language using the suggestion given in Ref.
[22], and it was optimized in the most time consuming opera- 3.3. Free path calculation
tions.
In most compilers, implementation of the numerical loga-
3.1. Pseudo-random number generator rithm calculation is based on the polynomial approximation
of degree 14. The maximum error of this polynomial approx-
The pseudo-random number generator chosen here (“ranmar” imation is not larger than 2−58.45 . In our case, for arguments
[23,24]) delivers a sequence of 32-bit floating point pseudo- near zero, values of the logarithmic function are not required
random numbers, uniformly distributed in the interval [0;1). because the smallest non-zero number generated by the “ran-
The period of the generated sequence (2144 ) is longer than the mar” generator is about 5.96e−8. In the interval [5.96e−8;1], an
period needed in the practical Monte Carlo simulation of light approximation of the logarithm using a rational function can
transport. Pseudo-random generator “ran3” [25] used in MCML be applied:
code has a shorter period of 255 . Moreover, “ranmar” is about
12% faster than “ran3” [26]. With this algorithm, several inde- a + cx0.5 + ex
, for x ∈ [5.5e−8; 0.99)
pendent sequences can be initialized and used in the same ln(x) ≈ appln(x)= 1 + bx0.5 +dx + fx1.5
run. This property is especially important for parallelization x − 1, for x ∈ [0.99; 1]
of the algorithm. “Ranmar” gives an identical sequence on all (5)
machines having at least 24-bit mantissas in the floating point
representation, which may be important in comparisons of the
results between the runs performed on different machines.
The algorithm has adequate statistical properties [26], but this
class of generators is known to have poor lattice structures in
high dimensions [27]. Nevertheless, this property is negligible
in the Monte Carlo simulation of light transport because only
one adequate dimensional quality is required.
a −18.7 −8.77
b 1.6e3 15.7
c −1.8e5 −42.3
d 9.3e5 15.2
e −4.8e6 51.0
f 3.5e5 −1.95
ln(x) − appln(x)
cl = × 100% (6)
ln(x)
where t0 = 1 ns and t1 = 2 ns. the Monte Carlo simulation using the approximated logarithm
Comparison of DTOFs obtained by the Monte Carlo sim- and cosine functions and with the solution of the diffusion
ulation using an exact logarithmic and cosine function with equation is shown in Fig. 4 for the homogeneous semi-infinite
medium with assumed optical properties of the skin (n = 1.4,
a = 0.18 cm−1 , and s = 19 cm−1 [29]). The internal radius of
the detection ring was 28 mm and the ring thickness was
4 mm.
The number of the detected photons as a function of the
optical properties is shown in Fig. 5. Comparison of the mean
time of flight and variance of DTOFs is shown in Figs. 6 and 7,
respectively, in respect of different optical properties of the
modeled structure. In Figs. 5A, 6A and 7A, the absorption coef-
ficient was set to 0.1 cm−1 . In Figs. 5B, 6B and 7B, the scattering
coefficient was 10 cm−1 .
A similar analysis was performed for the two-layered
structure. A simplified model of the head was considered,
Fig. 3 – Error of approximation of logarithmic function. containing only the skin layer overlaying the brain tissue.
54 c o m p u t e r m e t h o d s a n d p r o g r a m s i n b i o m e d i c i n e 8 4 ( 2 0 0 6 ) 50–57
5. Discussion
6. Conclusions
references
us allowed to compensate for this property of the new Java
version.
If a higher accuracy is required, it is possible to approximate
[1] J. Steinbrink, H. Wabnitz, H. Obrig, A. Villringer, H.
the cosine function by a polynomial of order 6 or 8. In these
Rinneberg, Determining changes in nir absorption using a
cases, the approximation error is lower than 0.01% and 0.001% layered model of the human head, Phys. Med. Biol. 46 (2001)
of the cosine value, respectively. The logarithmic function can 879–896.
be approximated by a more complicated rational function in [2] M.H. Niemz, Laser-tissue Interactions: Fundamentals and
the form of Applications (Biological and Medical Physics, Biomedical
Engineering), Springer-Verlag, 2003.
⎧ [3] A. Liebert, H. Wabnitz, D. Grosenick, M. Moller, R. Macdonald,
⎪
⎨ a + cx 0.5 + ex + gx1.5 + ix 2+ kx 2.5 , for x ∈ [5.5e − 8; 0.01)
0.5 1.5 2 2.5
H. Rinneberg, Evaluation of optical properties of highly
1 + bx + dx + fx + hx + jx
scattering media by moments of distributions of times of
⎪
⎩ a + cx 0.5 + ex + gx1.5 ,
0.5 1.5
for x ∈ [0.01; 1) flight of photons, Appl. Opt. 42 (28) (2003) 5785–5792.
1 + bx + fx + hx
[4] E. Okada, M. Firbank, M. Schweiger, S.R. Arridge, M. Cope,
(8)
D.T. Delpy, Theoretical and experimental investigation of
near-infrared light propagation in a model of the adult head,
Appl. Opt. 36 (1997) 21–31.
For this approximation, the error is not larger than 0.2% of [5] J.M. Maarek, G. Jarry, B. de Cosnac, A. Lansiart, H. Bui Mong,
the logarithm value in the interval [5.9e−8;1]. These more time A simulation method for the study of laser transillumination
of biological tissues, Ann. Biomed. Eng. 12 (3) (1984) 281–304.
consuming approximations are still faster than the original
[6] S.T. Flock, M.S. Patterson, B.C. Wilson, D.R. Wyman, Monte
calculation of the logarithm and cosine functions in the Java
carlo modeling of light propagation in highly scattering
language. tissue. i. Model predictions and comparison with diffusion
We developed a fast Monte Carlo code for which the errors theory, IEEE Trans. Biomed. Eng. 36 (12) (1989) 1162–1168.
of the analyzed moments of the simulated DTOFs are less than [7] A.H. Hielscher, R.E. Alcouffe, R.L. Barbour, Comparison of
2% for a range of optical properties, typical of living tissues. finite-difference transport and diffusion calculations for
Speed of this code is very important in many applications. photon migration in homogeneous and heterogeneous
tissues, Phys. Med. Biol. 43 (1998) 1285–1302.
A fast calculation of the sensitivity factors used to estimate
[8] S. Prahl, Light transport in tissue, Ph.D. Thesis, University of
changes in absorption coefficients in layered media [30] or for
Texas, Austin, 1998.
inverse problems and tomographic reconstructions [31–33] of [9] J.S. Hendricks, T.E. Booth (Eds.), Mcvp Variance Reduction
the optical properties of the non-homogeneous medium may Overview, Springer-Verlag, New York, 1983.
be crucial for a success of these techniques in clinical appli- [10] H. Kahn, Random sampling monte carlo techniques in
cations. The proposed here approximations affected only the neutron attenuation problems, Nucleonics 6 (1950) 27–37,
main engine of the code-calculation of the photon package 61–65.
[11] A.N. Witt, Multiple scattering in reflection nebulae. i. A
trajectory. Thus, such approximations can be applied also in
monte carlo approach, Astrophys. J. S35 (1977) 1–6.
the codes in which fluorescence [34] or the Doppler scattering [12] D.A. Boas, J.P. Culver, J.J. Stott, A.K. Dunn, Three dimensional
[35] are considered. monte carlo code for photon migration through complex
c o m p u t e r m e t h o d s a n d p r o g r a m s i n b i o m e d i c i n e 8 4 ( 2 0 0 6 ) 50–57 57
heterogeneous media including the adult human head, Opt. [25] D.E. Knuth, The art of computer programming,
Express 10 (3) (2002) 159–170. Addison-Wesley, Reading, Massachusetts, 1981.
[13] L. Wang, S.L. Jacques, l. Zheng, Mcml—monte carlo [26] I. Vattulainen, K. Kankaala, J. Saarinen, T. Ala-Nissila, A
modeling of light transport in multi-layered tissues, comprehesive study of some pseudorandom number
Comput. Methods Programs Biomed. 47 (1995) 131– generators, Comput. Phys. Commun. 86 (1995) 209–226.
146. [27] S. Tezuka, P. L’Ecuyer, R. Couture, On the lattice structure of
[14] L. Wang, S.L. Jacques, Monte Carlo Modeling of Light the add-with-carry and subtract-with-borrow random
Transport in Multi-layered Tissues in Standard c, University number generators, ACM Trans. Comp. Sci. 3 (4) (1993)
of Texas, Texas, 1992. 315–331.
[15] A. Colasanti, G. Guida, A. Kisslinger, R. Liuzzi, M. Quarto, P. [28] R. Davies, Really fast x86 floating point sin/cos.
Riccio, G. Roberti, F. Villani, Multiple processor version of a http://www.musicdsp.org/showone.php?id=158, 2003.
monte carlo code for photon transport in turbid media, [29] E. Okada, D. Delpy, Near-infrared light propagation in an
Comp. Phys. Commun. 132 (1) (2000) 84–93. adult head model. I. Modeling of low-level scattering in the
[16] R. Graff, M.H. Koelink, F.F.M. deMul, W.G. Zijlstra, C.M. cerebrospinal fluid layer, Appl. Opt. 42 (16) (2003) 2906–2914.
Dassel, J.G. Aarnoudse, Condensed monte carlo simulations [30] A. Liebert, H. Wabnitz, J. Steinbrink, H. Obrig, M. Moller, R.
for the description of light transport, Appl. Opt. 32 (4) (1993) Macdonald, A. Villringer, H. Rinneberg, Time-resolved
426–434. multidistance near-infrared spectroscopy of the adult head:
[17] A. Kienle, M.S. Patterson, Determination of the optical intracerebral and extracerebralabsorption changes from
properties of turbid media from single monte carlo moments of distribution of times of flight of photons, Appl.
simulation, Phys. Med. Biol. 41 (1996) 2221–2227. Opt. 43 (15) (2004) 3037–3047.
[18] A. Pifferi, P. Taroni, G. Valentini, S. Anderson-Engels, [31] S.R. Arridge, J.C. Hebden, Optical imaging in medicine. Ii.
Real-time method for fitting time resolved reflectance and Modelling and reconstruction, Phys. Med. Biol. 42 (5) (1997)
transmittance measurements with a monte carlo model, 841–853.
Appl. Opt. 37 (13) (1998) 2774–2780. [32] C.K. Hayakawa, J. Spanier, F. Bevilacqua, A.K. Dunn, J.S. You,
[19] M.J.C. VanGemert, S.L. Jacques, J.C.M. Sterenborg, W.M. Star, B.J. Tromberg, V. Venugopalan, Perturbation monte carlo
Skin optics, IEEE Trans. Biomed. Eng. 36 (12) (1989) methods to solve inverse photon migration problems in
1146–1154. heterogeneous tissues, Opt. Lett. 26 (17) (2001) 1335–1337.
[20] C.W. Cowell-Shah, Nine language performance round-up: [33] P. Kumar, R.M. Vasu, Reconstruction of optical properties of
benchmarking math and file i/o, OSNews, 2004, low-scattering tissue using derivative estimated though
(http://www.osnews.com/story.php?news id=5602). perturbation monte-carlo method, J. Biomed. Opt. 9 (5)
[21] S.R. Ladd, Linux Number Crunching: Benchmarking (2004) 1002–1012.
Compilers and Languages for ia32, Coyote Gulch [34] J. Swartling, A. Pifferi, A.M.K. Enejder, S. Anderson-Engels,
Productions, 2003. Accelerated monte carlo models to simulate fluorescence
[22] J. Bloch, Effective Java Programming Language Guide, spectra from layered tissues, J. Opt. Soc. Am. 20 (4) (2003)
Addison-Wesley Professional, 2001. 714–727.
[23] G. Marsaglia, A. Zaman, Toward a Universal Random [35] F.F.M.d. Mul, W. Steenbergen, J. Greve, Doppler monte carlo
Number Generator, Florida State University, 1987. simulations of light scattering in tissue to support
[24] F. James, A review of pseudorandom number generators, laser-doppler perfusion measurements, Technol. Health
Comput. Phys. Commun. 60 (1990) 329–344. Care 7 (2–3) (1999) 171–183.