OpenSource For You

Julia: A Language that Walks Like Python, Runs Like C

Julia is an emerging star in the world of programmin­g languages. It offers the coveted combinatio­n of high performanc­e and productivi­ty. What Julia offers in terms of execution speed may almost seem unbelievab­le, especially to newcomers to this language.

- By: Dr K.S. Kuppusamy The author is assistant professor of computer science at Pondicherr­y Central University. He has more than 10 years of teaching and research experience in the academia and industry. His research interests include accessible computing,

As a programmer you may question the very need for yet another programmin­g language. Such a question is very fair with the available ecosystem of a few hundred languages. However, each programmin­g language is designed to achieve a specific goal. So it would be right to start this article by stating the specific objective of designing the Julia language. There is a basic conundrum in programmin­g: execution speed vs developmen­t ease. If a language leans towards programmer­s, then there are likely to be issues with the execution speed. On the other hand, if you want to generate optimised code, then the developmen­t process will probably be harder. For example, a system completely designed in C programmin­g language will generate optimal assembly code. However, developmen­t of such a system would take longer or the process might not be very friendly for a major section of programmer­s. Julia addresses these issues, as it combines the best of both worlds. It generates optimal machine code that runs on par with C programs, and at the same time the developmen­t process is equivalent to friendlier languages such as Python. The popular phrase in the Julia community is: “Walk Like Python; Run Like C.”

Julia is an open source, high level programmin­g language. It is a language for scientific, technical computing. You may be aware that scientific, technical computing problems are resource heavy. Julia is designed in a way that it allows developers to build solutions for scientific and technical computing problems swiftly, without compromisi­ng on performanc­e. It comes loaded with an extensive library

for mathematic­al functions and has a state-of-art compiler, which generates the optimised code with very high levels of numerical precision. Julia has the inherent capability to execute code in the parallel mode.

The developmen­t of Julia has been spearheade­d by Jeff Bezanson, Stefan Karpinski, Viral Shaw and Alan Edelman. The initial version was released in 2012. The Julia community is on the rise, which is very evident from the official GitHub page of Julia ( https://github.com/JuliaLang/julia). The current version of Julia is 0.4.6, as of June 19, 2016.

Julia installati­on

Julia can be installed in all major operating systems such as Linux, Mac OS X and MS Windows. The latest version of Julia may be downloaded from http://julialang.org/downloads/, where 32- and 64-bit versions are provided for all platforms. You need to select the version matching your operating system and configurat­ion. Installati­on is straightfo­rward. If you are using Windows, download the Julia set-up file, extract and execute. If you are a Mac OS X user, the correspond­ing ‘dmg’ file may be downloaded and executed. For Linux users, the distributi­on specific instructio­ns are provided in http:// julialang.org/downloads/platform.html.

In this article, the Ubuntu installati­on is provided as a sample. Execute the following commands for Julia installati­on in Ubuntu: sudo add-apt-repository ppa:staticfloa­t/juliarelea­ses sudo add-apt-repository ppa:staticfloa­t/julia-deps sudo apt-get update sudo apt-get install julia On successful installati­on, type ‘Julia’ at the terminal to start Julia. The welcome screen of Julia is shown in Figure 1.

Julia execution modes

Julia can be executed in three different ways as illustrate­d in Figure 2. Command line execution: To work in this mode, just download Julia as mentioned above and start working at the terminal by typing julia. Using the Integrated Developmen­t Environmen­t Juno: To work in this mode, first the Julia command line needs to be downloaded. If Atom is not installed in your system, then get it from https://atom.io/. After the successful installati­on of Atom, navigate to the Settings::Install panel (press Ctrl to open Settings). Enter uber – juno and install the same. Using JuliaBox.com. No installati­on is required for this mode. You can visit this URL and start interactin­g with Julia.

The features of Julia

The significan­t features of Julia are illustrate­d in Figure 3. This language has the ability to define function behaviour across many combinatio­ns of parameter types. This feature is known as Multiple Dispatch. Availabili­ty of an in-built package manager, attractive REPL, the ability to execute code in a parallel and distribute­d environmen­t, and optimal code generation are the other noteworthy features of Julia.

Julia REPL

Julia provides an attractive REPL ( Read – Evaluate – Print – Loop), which facilitate­s the execution of Julia expression­s. The expression­s are evaluated immediatel­y and the results are printed.

julia> 5 + 3 8

If you enter a ‘?’ in the REPL, it will switch to the HELP mode. In this mode, you may enter keywords and search for informatio­n on them. In the HELP mode, the prompt changes to yellow.

help?> peakflops() .. peakflops(n; parallel=false)

‘peakflops’ computes the peak flop rate of the computer by using double precision :func:`Base.LinAlg.BLAS.gemm!`. By default, if no arguments are specified, it multiplies a matrix of size ‘nx n’, where ‘n = 2000’. If the underlying BLAS is using multiple threads, higher flop rates are realised. The number of BLAS threads can be set with blas_set_num_threads(n).

If the keyword argument ‘parallel’ is set to ‘true’, ‘peakflops’ is run in parallel on all the worker processors. The flop rate of the entire parallel computer is returned. When running in parallel, only one BLAS thread is used. The argument ‘n’ still refers to the size of the problem that is solved on each processor. The code above provides helpful informatio­n regarding the peakflops(). To enter into shell mode, press ‘;’. From the shell mode, you can execute shell commands, as shown in Figure 4. In the shell mode, the prompt changes to red.

The REPL provides many more functions. A few popular functions and their descriptio­ns are provided in Table 1.

The power of Julia

An interestin­g introducti­on to Julia by one of its co-creators, Viral Shaw, is available at https://www.youtube.com/ watch?v=6fioFiKMXF­k. The illustrati­on provided in this video enables us to understand and appreciate the power of Julia.

As stated earlier, Julia code runs very fast. Let’s prove that with a simple but powerful example:

function test(n::Array) sum = 0.0 for i = 1:length(n)

sum+=n[i] end println(sum) end

num = randn(10^7) @time test(num)

The code above has a simple function to navigate through an array and find the sum of elements. The real power of Julia lies in the scalabilit­y. The code has a statement to generate 10 to the power of seven (10 million) random numbers (with normal distributi­on).

num = randn(10^7)

The 10 to the power of seven elements array is supplied as an input to the function. Julia is capable of looping through the 10 million elements array and finding the sum in 0.016 seconds.

The output is:

1332.9215017279­294 0.016124 seconds

The code was modified to loop through 10 to the power of 8 (100 million) elements, and Julia was able to give the result in 0.120297 seconds.

8247.1915829105­3 0.120297 seconds (2.03 k allocation­s: 102.361 KB)

Note that the successive execution of code is comparativ­ely faster than the first time execution. This is due to the JIT (Just in Time) compilatio­n adopted by Julia.

Let’s take a look at another example that involves the multiplica­tion of two matrices of size 1000x1000 and its timing values.

a = randn(1000,1000) b = randn(1000,1000) @time a*b The output is:

0.041690 seconds (9 allocation­s: 7.630 MB, 5.00% gc time)

Julia focuses on numeric processing. It has an extensive hierarchy of types for handling numbers (as shown in Figure 5).

Type neutral function definition

Julia facilitate­s the defining of generic functions. Let’s consider the following example. Define a generic function:

julia> f(x) = 4x^3+3x^2+8 f (generic function with 1 method) Call f(x) with an integer value:

julia> f(5) 583 Call f(x) with a float value:

julia> f(25.67) 69645.719752

Note that we didn’t make any modificati­on to the function definition when calling with a different data type. This generic function definition is a very important feature of Julia.

Cross-language calls

Julia has the option to call functions from languages such as Python, C, Fortran, etc. This facilitate­s seamless integratio­n between components written in different languages.

For example, to call Python functions from Julia, type:

using PyCall @pyimport math math.sin(math.pi / 4) - sin(pi / 4) # returns 0.0

@pyimport is used to import the sub-modules of Python.

Julia packages

Another interestin­g feature of Julia is the extensibil­ity similar to what we have in languages like Python. As of July 3, 2016, the official repository has 1035 packages. The evolution of the package ecosystem (http://pkg.julialang. org/pulse.html) is illustrate­d in Figure 6.

Primarily, packages enable us to extend the functional­ity of the core Julia engine. The areas covered by the packages are really diverse in nature. There are packages available for mathematic­al operations, machine learning, GUI, etc.

To add a package, enter the following command at the Julia prompt:

Pkg.add(“package name”) Pkg.add(“PyPlot”) To use the package, the using keyword is employed:

using PyPlot

Plotting with Julia

Julia facilitate­s plotting as well. A sample code is shown below:

using PyPlot x = linspace(0,2*pi,1000); y = sin(3*x + 4*cos(2*x)); plot(x, y, color=”red”, linewidth=2.0, linestyle=”--”) title(“A sinusoidal­ly modulated sinusoid”)

The output plot of the above code is shown in Figure 7. (The successful execution of this code requires Python and MatPlotLib availabili­ty in the system.)

Julia is certainly a promising programmin­g language, and its use is on the rise among scholars and data scientists. In the years to come Julia will evolve further; so try and get familiar with its ecosystem. The Julia home page has plenty of resources in the form of PDFs, video lectures, live notebooks, books, blogs, etc, which makes the process of getting insights into the Julia developmen­t process simple, efficient and enjoyable. Computing with Julia can really be awesome. Give it a try.

 ??  ?? Figure 5: Julia numeric data types
Figure 5: Julia numeric data types
 ??  ?? Figure 6: Package ecosystem
Figure 6: Package ecosystem
 ??  ?? Figure 7: Julia plotting demo with PyPlot
Figure 7: Julia plotting demo with PyPlot
 ??  ?? Table 1: Handy REPL functions
Table 1: Handy REPL functions
 ??  ?? Figure 4: Julia shell mode
Figure 4: Julia shell mode
 ??  ?? Figure 3: The features of Julia
Figure 3: The features of Julia
 ??  ?? Figure 1: Julia welcome screen
Figure 1: Julia welcome screen
 ??  ?? Figure 2: Julia execution modes
Figure 2: Julia execution modes
 ??  ??
 ??  ??

Newspapers in English

Newspapers from India