4
Astroinformatics Proceedings IAU Symposium No. 325, 2017 M. Brescia, S. G. Djorgovski, E. Feigelson, G. Longo & S. Cavuoti. c 2017 International Astronomical Union DOI: 00.0000/X000000000000000X What can the programming language Rust do for astrophysics? Sergi Blanco-Cuaresma 1 and Emeline Bolmont 2 1 Observatoire de Gen` eve, Universit´ e de Gen` eve, CH-1290 Versoix, Switzerland. email: [email protected] 2 NaXys, Department of Mathematics, University of Namur, 8 Rempart de la Vierge, 5000 Namur, Belgium. email: [email protected] Abstract. The astrophysics community uses different tools for computational tasks such as complex systems simulations, radiative transfer calculations or big data. Programming languages like Fortran, C or C++ are commonly present in these tools and, generally, the language choice was made based on the need for performance. However, this comes at a cost: safety. For instance, a common source of error is the access to invalid memory regions, which produces random execution behaviors and affects the scientific interpretation of the results. In 2015, Mozilla Research released the first stable version of a new programming language named Rust. Many features make this new language attractive for the scientific community, it is open source and it guarantees memory safety while offering zero-cost abstraction. We explore the advantages and drawbacks of Rust for astrophysics by re-implementing the fundamental parts of Mercury-T, a Fortran code that simulates the dynamical and tidal evolution of multi-planet systems. Keywords. Rust, programming languages, N-Body, simulations, exoplanets 1. Introduction Plenty of tools in astrophysics are developed using system programming languages such as Fortran, C or C++. These languages are known to provide high performance and fast executions but they rely heavily on the developer for concurrency and memory control, which may lead to common errors as shown in Fig. 1: a) access to invalid memory regions, b) dangling pointers and attempts to free already freed memory, c) memory leaks and, d) race conditions. This can produce random behaviors and affect the scientific interpretation of the results. The recently created language Rust prevents such problems and fields like bioinfor- matics (K¨ oster, 2015) have already started to take advantage of it. Astroinformatics can benefit from it too. We first discuss the general principles behind this new language and what makes it attractive when compared to more traditional languages such as C or Fortran. We then show that this language can reach the same performance as a Fortran N-Body simulator, Mercury-T(Bolmont et al., 2015), designed for the study of the tidal evolution of multi-planet systems. 2. Rust Mozilla Research, motivated by the development of a new web browser engine (An- derson et al., 2015), released in 2015 the first stable version of a new open source pro- http://www.emelinebolmont.com 1 arXiv:1702.02951v1 [astro-ph.IM] 9 Feb 2017

What can the programming language Rust do for … · What can the programming language Rust do for astrophysics? ... fundamental parts of Mercury-T, ... 2014) and it is designed

Embed Size (px)

Citation preview

Page 1: What can the programming language Rust do for … · What can the programming language Rust do for astrophysics? ... fundamental parts of Mercury-T, ... 2014) and it is designed

AstroinformaticsProceedings IAU Symposium No. 325, 2017M. Brescia, S. G. Djorgovski, E. Feigelson, G. Longo & S. Cavuoti.

c© 2017 International Astronomical UnionDOI: 00.0000/X000000000000000X

What can the programming languageRust do for astrophysics?

Sergi Blanco-Cuaresma1 and Emeline Bolmont2

1Observatoire de Geneve, Universite de Geneve,CH-1290 Versoix, Switzerland.email: [email protected]

2 NaXys, Department of Mathematics, University of Namur,8 Rempart de la Vierge, 5000 Namur, Belgium.

email: [email protected]

Abstract. The astrophysics community uses different tools for computational tasks such ascomplex systems simulations, radiative transfer calculations or big data. Programming languageslike Fortran, C or C++ are commonly present in these tools and, generally, the language choicewas made based on the need for performance. However, this comes at a cost: safety. For instance,a common source of error is the access to invalid memory regions, which produces randomexecution behaviors and affects the scientific interpretation of the results.

In 2015, Mozilla Research released the first stable version of a new programming languagenamed Rust. Many features make this new language attractive for the scientific community, itis open source and it guarantees memory safety while offering zero-cost abstraction.

We explore the advantages and drawbacks of Rust for astrophysics by re-implementing thefundamental parts of Mercury-T, a Fortran code that simulates the dynamical and tidal evolutionof multi-planet systems.

Keywords. Rust, programming languages, N-Body, simulations, exoplanets

1. Introduction

Plenty of tools in astrophysics are developed using system programming languagessuch as Fortran, C or C++. These languages are known to provide high performanceand fast executions but they rely heavily on the developer for concurrency and memorycontrol, which may lead to common errors as shown in Fig. 1: a) access to invalid memoryregions, b) dangling pointers and attempts to free already freed memory, c) memory leaksand, d) race conditions. This can produce random behaviors and affect the scientificinterpretation of the results.

The recently created language Rust prevents such problems and fields like bioinfor-matics (Koster, 2015) have already started to take advantage of it. Astroinformatics canbenefit from it too. We first discuss the general principles behind this new language andwhat makes it attractive when compared to more traditional languages such as C orFortran. We then show that this language can reach the same performance as a FortranN-Body simulator, Mercury-T† (Bolmont et al., 2015), designed for the study of the tidalevolution of multi-planet systems.

2. Rust

Mozilla Research, motivated by the development of a new web browser engine (An-derson et al., 2015), released in 2015 the first stable version of a new open source pro-

† http://www.emelinebolmont.com

1

arX

iv:1

702.

0295

1v1

[as

tro-

ph.I

M]

9 F

eb 2

017

Page 2: What can the programming language Rust do for … · What can the programming language Rust do for astrophysics? ... fundamental parts of Mercury-T, ... 2014) and it is designed

2 Sergi Blanco-Cuaresma & Emeline Bolmont

1 2 3

Read / Write 4th element

Neighboor's memoryYour vector

Allocate memoryFreeFree a 2nd time

Allocate memoryForget to freeAllocate memory Out of memory!

Access freed memory

Shared data

Task 1 ReadRead

WriteOverwrite

Task 2

a) b) c) d)

Figure 1. Typical memory and concurrency problems with system programming languagessuch as Fortran, C or C++.

gramming language named Rust. It uses patterns coming from functional programminglanguages (Poss, 2014) and it is designed not only for performance and concurrency, butalso for safety. Rust introduces concepts like ownership, borrowing and variable lifetime,which:

- facilitates the automatic control of the lifetime of objects during compilation time.There is no need for manually freeing resources or for an automated garbage collectorlike in Java or Go;

- prevents the access to invalid memory regions;- enforces thread-safety (race conditions cannot occur). These zero-cost abstraction

features make Rust very attractive for the scientific community with high performanceneeds.

In Rust, variables are non-mutable by default (unless the mutable keyword is used)and they are bound to their content (i.e, they own it or they have ownership of it). Whenyou assign one variable to another (case ’a’ in Fig. 2), you are not copying the contentbut transferring the ownership†, so that the previous variable does not have any content(like when we give a book to a friend, we stop having access to it). This transfer takesalso place when we call functions (case ’b’ in Fig. 2), and it is important to note thatRust will free the bound resource when the variable binding goes out of scope (at the endof the function call for case ’b’). Hence, we do not have to worry about freeing memoryand the compiler will validate for us that we are not accessing a memory region that hasalready been freed (errors are caught at compilation time, before execution time).

Additionally, apart from transferring ownership, we can borrow the content of a vari-able (case ’c’ in Fig. 2). In this case, two variables have the same content but none of themcan be modified, thus protecting us from race conditions. Alternatively, we can borrowin a more traditional way (like when we borrow a book from a friend, he is expecting toget it back when we stop using it) like in case ’d’ in Fig. 2, where the function borrowsthe content of a variable, operates with it (in this case, it could modify its content) andreturns it to the original owner (not destroying it as shown in case ’b’).

Exceptionally, all these rules can be violated if we make use of unsafe blocks, whichis strongly discouraged but necessary in certain situation (e.g., dealing with externallibraries written in Fortran, C or C++). If present, unsafe blocks allow us to clearlyidentify parts of the code which should be carefully audited, keeping it isolated and notmaking the whole program unsafe by default like in Fortran, C or C++.

3. Assessment

We explored the advantages and drawbacks of Rust for astrophysics by re-implementingthe fundamental parts of Mercury-T (Bolmont et al., 2015), a Fortran code that simulatesthe dynamical and tidal evolution of multi-planet systems.

† Except primitive types or types that implement the Copy trait.

Page 3: What can the programming language Rust do for … · What can the programming language Rust do for astrophysics? ... fundamental parts of Mercury-T, ... 2014) and it is designed

What can Rust do for astrophysics? 3

let v1 = vec![1, 2, 3];let v2 = v1;println!("{}", v1[0]);

owne

rship v1

v2v2

fn calc(v: Vec<i32>) {...}let v1 = vec![1, 2, 3];calc(v1);println!("{}", v1[0]);

owne

rship v1

vv Lifetime ends

(i.e., vector is freed)error: v1 doesn't own anything

error: v1 doesn't own anything

Lifetime starts

let mut v1 = vec![1, 2, 3];let v2 = &v1;println!("{} {}", v1[0], v2[0]);v1[0] = 10;error: v1/v2 can be read but none can be modified

v1v1 v2v1 v2

Transfer

owne

rship

fn calc(v: &mut Vec<i32>) { v[0] = 10;}

let mut v1 = vec![1, 2, 3];calc(&mut v1);println!("{}", v1[0]);

owne

rship v1

vv1

Lifetime starts

TransferBorrowsWith mutability

Gives backv1 Lifetime ends

(i.e., vector is freed)

BorrowsWithout mutability

v1 and v2 access the same memory

Borrowing rulesTwo kinds of borrows, never both:1. One or more non-mutable reference2. Just one mutable reference

Transfer ownership Borrowing

CompilerAll errors are caught at compilation time.

a) c)

b) d)

Figure 2. Rust’s ownership and borrowing concepts help us overcome the problems oftraditional system programming languages.

We developed a simple N-Body dynamical simulator‡ (without tidal effects) based ona leapfrog integrator in Rust, Fortran, C and Go (which provide a garbage collector formemory management). The software design and implementation does not include anylanguage-specific optimization that a developer with basic knowledge would not do.

We compiled the four implementations with an optimization level 3 (rustc/gfortran/gcccompiler) and the standard compilation arguments in the case of Go. We selected thebest execution time out of five for an integration of 1 million years and the results are inTable 1. For this particular problem, Rust is as efficient as Fortran, and both surpass Cand Go implementations

Table 1. Best execution times of pure N-body simulations, for an integration time of 1 millionyears using a leap-frog integrator.

Rust Fortran C Go

0m13.660s 0m14.640s 2m32.910sa 4m26.240s

a Time might be improved if language-specificoptimizations were implemented.

Based on Mercury-T, we implemented the additional acceleration produced by tidalforces between the star and its planets into our Rust and Fortran leapfrog integrators.

To test the codes, we ran a simulation of 100 million years with the same initialconditions as the case 3 described in the Mercury-T article (Bolmont et al., 2015), hencea single planet with a rotation period of 24 hours, orbiting a brown dwarf (0.08 M�) at0.018 AU with an eccentricity of 0.1.

The results are shown in Fig. 3, the Rust and Fortran code are practically identicaland they reproduce a similar behavior to what is shown in the Mercury-T article. Nev-ertheless, leapfrog is a very simple integrator and not very accurate. This can be seenin the eccentricity evolution, which is slightly different from the Mercury-T article andappears noisy. As an additional exercise, we implemented the WHFast integrator (Reinand Tamayo, 2015) in Rust (black line in Fig. 3). This better integrator leads to a betteragreement with Mercury-T thus demonstrating that a high level of accuracy can also beachieved with Rust.

‡ http://www.blancocuaresma.com/s/

Page 4: What can the programming language Rust do for … · What can the programming language Rust do for astrophysics? ... fundamental parts of Mercury-T, ... 2014) and it is designed

4 Sergi Blanco-Cuaresma & Emeline Bolmont

0.016

0.018

0.020

0.022

0.024

0.026

0.028

Sem

i-m

ajo

r axis

(AU

)

Leapfrog Fortran

Leapfrog Rust

WHFast Rust

60

80

100

120

140

Pla

net

rota

tion

peri

od (

hours

)

102 103 104 105 106 107 108

Time (years)

10-3

10-2

10-1

Ecc

entr

icit

y

Figure 3. Tidal evolution of a single planet orbiting a brown-dwarf with the three integrators.Top: evolution of the semi-major axis of the planet. Middle: evolution of the planet’s rotationperiod. Bottom: evolution of the eccentricity of the planet.

4. Conclusions

We have shown the reliability of Rust as a programming language as opposed to For-tran, C or even Go. Rust allows the user to avoid common mistakes such as the access toinvalid memory regions and race conditions. We have also shown that it is a competitivelanguage in terms of speed and accuracy.

The main challenge we experienced was the initial learning curve, it was necessaryto really understand and get used to the ownership and borrowing concepts. Once theparadigm shift is done, the benefits are immediate. We therefore encourage the commu-nity to consider Rust as a language that will help us produce good quality, memory safe,concurrent and high-performance scientific code.

References

Anderson, B., Herman, D., Matthews, J., McAllister, K., Goregaokar, M., Moffitt, J. and Sapin,S. 2015, arXiv, 1505.07383

Bolmont, E. and Raymond, S. N. and Leconte, J. and Hersant, F. and Correia, A. C. M. 2015,A&A, 583, A116

Koster, J. 2015, Bioinformatics, 32, 444Poss, R. 2014, arXiv, 1407.5670Rein, H. and Tamayo, D. 2015, MNRAS, 452, 376-388