This week I’m playing with MPI and small Raspberry Pi cluster. Parallel coding was fun! The problem that I’m trying to solve is a BFS pathfinding algorithm. I want to make the solution done in bi-directional finding with the help of parallel computing.

I’ve wrote the code some years ago in Javascript and now need it run in Python with mpi4py. Since I’m too lazy to port the code, I seek for an automate solution and found this js2py library. This is awesome.

>> import js2py
>> js2py.eval_js6('console.log("hello world")')

Even it can transpile the entire JS file to Python

>> js2py.translate_file('foo.js', 'foo.py')

The transpiled code looks like this :

    def PyJsHoisted_determine_path_(this, arguments, var=var):
        var = Scope({u'this':this, u'arguments':arguments}, var)
        var.registers([u'yy', u'aa', u'xx', u'bb', u'zz'])
        pass
        pass
        pass
        var.put(u'xx', var.get(u'target'))
        pass
        var.put(u'aa', (var.get(u'pohon').get(u'length')-Js(1.0)))
        #for JS loop
        var.put(u'bb', var.get(u'aa'))
        while (var.get(u'bb')>Js(0.0)):
            try:
                if (var.get(u'pohon').get(var.get(u'bb')).get(u'anak')==var.get(u'xx')):
                    var.put(u'xx', var.get(u'pohon').get(var.get(u'bb')).get(u'ortu'))
                    var.get(u'path').callprop(u'push', var.get(u'xx'))
            finally:
                    (var.put(u'bb',Js(var.get(u'bb').to_number())-Js(1))+Js(1))
        #for JS loop
        var.put(u'yy', Js(0.0))
        while (var.get(u'yy')<var.get(u'path').get(u'length')):
            try:
                var.get(u'land').get((var.get(u'parseInt')(var.get(u'path').get(var.get(u'yy')))-Js(1.0))).put(u'state', Js(u'white'))
            finally:
                    (var.put(u'yy',Js(var.get(u'yy').to_number())+Js(1))-Js(1))
        var.get(u'console').callprop(u'log', (Js(u'result : ')+var.get(u'path')))

Hardly unnreadable, of course.

But,

The performance of the result script is too slow! And I ended up with porting the code manually for an hour. u_u