Although, many scientists and engineers use Octave or MATLAB as their preferred programming language, dynamic nature of these languages can lead to slower running-time of programs written in these languages compared to programs written in languages which are not as dynamic, like C, C++ and Fortran. In this work we developed a translator for a new programming language (tym) which tries to address performance issues, common in scientific programs, by adding new constructs to a subset of Octave/MATLAB language. Our translator compiles programs written in tym, to efficient C++ code.
Deep Dive into tym: Typed Matlab.
Although, many scientists and engineers use Octave or MATLAB as their preferred programming language, dynamic nature of these languages can lead to slower running-time of programs written in these languages compared to programs written in languages which are not as dynamic, like C, C++ and Fortran. In this work we developed a translator for a new programming language (tym) which tries to address performance issues, common in scientific programs, by adding new constructs to a subset of Octave/MATLAB language. Our translator compiles programs written in tym, to efficient C++ code.
Many scientists and engineers use Octave or MATLAB as their preferred programming language. However, dynamic nature of these languages can lead to slower running-time of programs written in these languages compared to programs written in languages which are not as dynamic, like C, C++ and Fortran.
Two dynamic features that contributes to performance issues in major ways are:
Dynamic typing: Types of variables can change during run-time and generally they are not known before run-time.
Dynamic resizing: Size of arrays and matrices can change during runtime. Pre-allocation of arrays is not mandatory in Octave/MATLAB and whenever a value is assigned to a location that is not within the range of the array indexes, the array is resized to store the new value.
For example, consider the following function in Octave/MATLAB: function z = mmt(x, y) z=x*y; end Type of variables x and y can be any allowable type. If we are supposed to compile this function statically, we may have to choose the widest possible type Figure 1: tym compiler architecture for x and y. This type is an array of complex numbers. The result would be very inefficient code when parameters are of narrower type (e.g. Integers or even arrays of integers). In these cases they are actually wrapped as arrays of complex numbers. One way to tackle this problem is to use JIT compilers to compile the program or choose the best previously compiled version at runtime. However, this requires run-time overhead and can be really non-trivial. We would like to follow another path in this paper.
We have developed a new language which is similar to Octave/MATLAB in many ways but has a less dynamic nature. In particular, variables must be declared (with their type) before they are defined or used and arrays must be allocated explicitly. We have called this language, tym (Typed MATLAB). Programs written in tym are translated into C++, the generated C++ program uses Octave library and can be called from the Octave [1] interpreter. To do this, an oct-file should be created by using mkoctfile. Cython [4] also takes a similar approach, but, it is based on Python which might not be as common as Octave/MATLAB in scientific and engineering communities. OMPC [3] is another compiler that translates MATLAB to Python. We have used some of their routines and their grammatical rules in our code.
You can see different components that are necessary to translate a program in tym to its equivalent module in C++ in Figure 1. Currently, a program should be written as a function in tym. Later, this function is transformed to a C++ module and is compiled to an oct-file. The resulted oct-file can be called from the octave interpreter as a function. This has the advantage that the user can change or write parts of her MATLAB program that is computation-intensive as a function in tym then call it from Octave so she has access to all packages in octave-forge (Neural networks, Image processing, Signal processing, …) while she programs in tym. Resulted C++ module uses Octave library classes and routines to manipulate matrices and other objects in a way that is compatible with Octave. However, since Octave library does not depend on the Octave interpreter and any C++ program can use it independent of the rest of the Octave, it is also possible to convert a program which is written in tym to an executable that can be run without relying on the octave interpreter.
To implement our tym to C++ compiler, we use PLY [2] which is a Python implementation of compiler construction tools lex and yacc. PLY supports LALR(1) parsing. All grammatical rules should be written in a python module (tymply.py in our compiler). There is a function for every tym programming construct in this module. The grammatical rule which corresponds to a construct is defined in the function’s document string. In the body of the function is the action code which is done upon parsing the construct. PLY generates a parser based on the file tymply.py. Since the generated parser is a bottom up LALR(1) parser, we can assume that it does a post-order traversal on Abstract Syntax Tree (AST) and performs some action code upon visiting each AST node. Every AST node is an instance of a tym construct in input text. In our compiler, action codes are responsible for transforming the input program in tym to its equivalent form in C++. AST is an abstract form of parse-tree which is constructed during parsing the input text.
Octave can be dynamically linked with functions which are written in C/C++. Upon linking any of the linked functions can be called from Octave interpreter. Writing Octave compatible functions requires either following the C-Mex interface or using Octave library to manipulate matrices and other objects you would like to pass to Octave. This is necessary to comply to Octave’s memory representation of different objects (like matrices). Octave itself uses the Octave library to manipulate matrices and other objects
…(Full text truncated)…
This content is AI-processed based on ArXiv data.