Sunday, July 15, 2012

Julia set in Julia

Julia programming language

Note: working with Julia commit 2013-07-26 (0.2.0-prerelease+2820)

Julia is a new high-level, high-performance dynamic programming language for technical computing.
High-performance assures fast execution, whereas dynamic languages enable fast development.

Both used to be contradictionary features of programming languages, since dynamic basically means no compilation before execution, and that means: More work at run-time. However, since Just-In-Time-Compilers got better and better, now there is a way to have dynamic and high perfomance languages. Julia is one of those languages. They use the LLVM as JIT-compiler - which I think is a pretty neat idea.

Installation is moderately easy as is described here . There are some precompiled packages here, but since Julia is pretty new language, it is best to do a git clone . For that, you might want to update your GCC, since I was not able to compile it with 4.1.2 which comes standard with OSX.

Julia sets


So somehow I could not resist in writing a program calculating a Julia set in Julia. Since there was already the mandelbrot example here, it was easy to change it to calculate Julia sets:

# the julia iteration function
function julia(z, maxiter::Int64)
    for n = 1:maxiter
        if abs(z) > 2
            return n-1
        z = z^2 + c
    return maxiter

Since Julia has built-in support for complex numbers, we can now calculate, for example julia(0.5+0.2im, 256)

Sequential Calculation

To calculate a whole picture, we need some for loops like this:
# load image support

# load our julia function
# create a 1000x500 Array for our picture
h = 500
w = 1000
m = Array(Int64, h, w)
# time measurements
# for every pixel
for y=1:h, x=1:w
    # translate numbers [1:w, 1:h] -> -2:2 + -1:1 im
    c = complex((x-w/2)/(h/2), (y-h/2)/(h/2))
    # call our julia function
    m[y,x] = julia(c, 256)
tStop = time()
# write the ppm-file
myppmwrite(m, "julia.ppm")
print("done. took ", tStop-tStart, " seconds\n");

Unfortuately, there is no image.jl anymore shipped with julia anymore, so here is a very easy version of it:
function myppmwrite(img, file::String)
    s = open(file, "w")
    write(s, "P6\n")    
    n, m = size(img)
    write(s, "$m $n 255\n")
    for i=1:n, j=1:m
        p = img[i,j]
        write(s, uint8(p))
        write(s, uint8(p))
        write(s, uint8(p))

Together with the julia.jl file, you can execute above script with
julia calcJulia.jl

There is an even easier way to calculate the array m:
m = [ julia(complex(r,i)) for i=-1.0:.01:1.0, r=-2.0:.01:0.5 ];
(taken from extras/image.jl). However, above approach yields more insight on the parallel algorithm.

Parallel Calculation 

Parallel programming is not as easy as in, for example OpenMP.  We are creating a distributed array here (darray) and initialize it with the function parJuliaInit, which has to calculate its local part of the array. Because every processor needs to know the init function parJuliaInit and julia, we need to use the @everywhere command for the load and the function definition (@everywhere is not explained in the docs yet):

# we need julia.jl everywhere
@everywhere include("julia.jl")

@everywhere w=1000
@everywhere h=500

# the parallel init function, needed everywhere
@everywhere function parJuliaInit(I)
    # create our local patch
    d=(size(I[1], 1), size(I[2], 1))
    m = Array(Int, d)

    # we need to shift our image

    for x=I[2], y=I[1]
        c = complex((x-w/2)/(h/2), (y-h/2)/(h/2))
        m[y-ymin+1, x-xmin+1] = julia(c, 256)
    return m
tStart = time()

# create a distributed array, initialize with julia values
Dm = DArray(parJuliaInit, (h, w))
tStop = time()
# convert into a local array
m = convert(Array, Dm)
# write the picture
myppmwrite(m, "julia.ppm")
# print out time
print("done. took ", tStop-tStart, " seconds\n");


You can run that code with
julia -p 4 parJulia.jl
Where you replace 4 with your number of processors.

Here's a comparison of different processor sizes and algorithms for different picture sizes calculated on a MacPro with 2x 2.26 Ghz Quad-Core Xeon processors (= 8 processors in total):
code #processors time 500x1000 time 2000x4000
calcJulia.jl 1 2.20 s 34.50 s
parJulia.jl 1 3.16 s 46.26 s
parJulia.jl 2 1.83 s 23.64 s
parJulia.jl 4 0.92 s 7.28 s
parJulia.jl 5 0.83 s 5.27 s
parJulia.jl 8 0.85 s 2.97 s
parJulia.jl 10 0.81 s 2.50 s

The table shows the difficulties in parallel computing: Even if the algorithm scales perfectly in theory, the execution on N processors is not always N times faster. This is because of not all parts of your implementation and your hardware might scale perfectly, especially not for all problem sizes (very large or very small). The drastically difference of access rates for the L1/L2/RAM memory can even lead to results like the parallel calculation of the 2000x4000 image, which is 15 times faster on 8 processors than on one.


Julia is a nice language with a MATLAB-style syntax which could have a big influence on scientific computing. Many applications are operating on the memory bandwith limit, or the communication bandwith limit, so with a JIT compiler, those would be just fine.

Note: Syntax highlighting is done with Syntax Highlighter and this little beta JavaScript configuration file for the Julia language.

Update 1:
Syntax Highlighting should now work.
Update 2:
Fixed sequential calculation.
Update 3:
Fixed parallel calculation. load() is not @everywhere by default.
Update 4:
Now working with Julia commit 2013-07-26, which changed DArray syntax and require.

Saturday, July 14, 2012

gcc name demangling

According to wikipedia, name mangling is a "way of encoding additional information in the name of a function, structure, class or another datatype in order to pass more semantic information from the compilers to linkers".

The need for name mangeling arises because the name of a symbol in a file is very restricted. It can not contain spaces, brackets or columns, for example. With name mangling, the linker is able to distinguish overloaded functions in C++ like
int bla();
int bla(double);
The wikipedia article and this describe name mangling quite well.

You'll see a lot of mangled names when you try
nm someexecutable

So I created a small C++ program demangles the names. Save below code in a file named mydemangle.cpp, and compile it with
g++ -o mydemangle mydemangle.cpp

Now you can try it with
mydemangle _ZN9wikipedia7article6formatEv
and the result is

But demangle can do more: It can read input from stdin. So you might try
nm someexecutable | mydemangle 

Now mydemangle demangles all names it can demangle and leaves the rest as it is.

There's a quite handy way in C++ to demangle names via the function
char * __cxa_demangle (const char *mangled_name, char *output_buffer, size_t *length, int *status);

Be aware that this will not be available for all processors/compilers.

Here's the code:


#include <iostream>
#include <cxxabi.h>
#include <stdlib.h>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
    int status;
    if(argc == 2)
        // we have one argument: demangle it
        char *realname = abi::__cxa_demangle(argv[1], 0, 0, &status);
            cout << "\n" << realname << "\n";
            cout << "\ncould not demangle " << argv[1] << "\n";
        // we have no argument: read from stdin
        char c, lastc=0x00;
        string s;
        while((c = getchar()) != EOF)
            // mangled names start with _ . consider only if last sign was space or tab or newline
            if(c == '_' && (lastc==' ' || lastc == '\n' || lastc == '\t'))
                s = "_";
                // add all characters to the string until space, tab or newline
                while((c = getchar()) != EOF)
                    if(c == ' ' || c == '\n' || c == '\t')
                    s += c;
                // some compilers add an additional _ in front. skip it.
                const char *p = s.c_str();
                if(s.length() > 2 && s[1] == '_') p = p+1;
                char *realname = abi::__cxa_demangle(p, 0, 0, &status);
                    cout << realname; // demangle successfull
                    cout<< s; // demangling failed, print normal string
            cout << c;
            lastc =c;
    return 0;

Of course, there is some easy way in bash to do it. This requires demangle (part of KDE Dev Kit) and is not as fast and convenient as above method.
while read LINE
    for WORD in $LINE
 echo -n $WORD | demangle

Wednesday, July 4, 2012

Ultimate bash prompt

The standard bash prompt on most systems is very unusable. Most of the time, it's just something like bash-3.2#, which doesn't give you much information. Knowing the machine you're working on and your current working directory is crucial, and if your prompt provides you this information this can make your life a lot easier.

So here it is: My bash prompt. It looks like this:


on mac (
Features are:
  • Display of hostname up to first '.'
  • Display of path
  • red SU in front when superuser/root
  • display of hostname up to first '.' and directory in xterm/mac title
In my opinion, that's all information you need most of the time (except for maybe some git/svn information). I added the shortened hostname in front because i'm working on different systems a lot and this just helps distinguish them from the window title.
You can add more to it, flashing colors and stuff, but it just gets unusable at some point especially when you have long directory names.
Here is the code: 
# this sed line returns everything up to the first . in hostname
# ex. -> linuxbox
SHORT_HOSTNAME=`hostname | sed 's/\([^\.]*\).*/\1/'`

#LONG_HOSTNAME=`hostname -f`


# add SU in red if root/superuser
[[ $EUID -eq 0 ]] && HOSTCOLOR="\[\033[1;31m\]SU $HOSTCOLOR"

# command executed just before bash displays a prompt
# here it is set to change xterm's (also mac terminal) title string to
# hostname currentDirectory
PROMPT_COMMAND='echo -ne "\033]0; $SHORT_HOSTNAME `dirs`\007"'

# the prompt
You can just copy it in your ~/.bashrc or save it as ~/ and add a source in your ~/.bashrc .

Note: Of course this is not the ultimate bash prompt, every advanced user will have an own customized prompt. But I hope it's a good place to start :-).

Here's some more information:

- Martin