# numerical Lua?

15 messages
Open this post in threaded view
|

## numerical Lua?

 I am learning Lua and find it has no sciLua or numLua projects. (Python has many. And I don't know why python is a good language to do matrix computing.). I am wondering if no array in Lua is a problem to manipulate matrixes and vectors. Does the following description imply that Lua is a good languge for scietific problems?====================Copied from http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf ====================Until Lua 4.0, tables were implemented strictly as hash tables: all pairs wereexplicitly stored. Lua 5.0 brought a new algorithm to optimize the use of tablesas arrays: it optimizes pairs with integer keys by not storing the keys and storing the values in an actual array. More precisely, in Lua 5.0, tables are implementedas hybrid data structures: they contain a hash part and an array part. Figure 2shows a possible con guration for a table with the pairs "x" ! 9:3, 1 ! 100, 2 ! 200, 3 ! 300. Note the array part on the right: it does not store the integerkeys. This division is made only at a low implementation level; access to table elds is transparent, even to the virtual machine. Tables automatically and dynamically adapt their two parts according to their contents: the array part triesto store the values corresponding to integer keys from 1 to some limit n. Valuescorresponding to non-integer keys or to integer keys outside the array range are stored in the hash part.==========================
Open this post in threaded view
|

## Re: numerical Lua?

 > I am learning Lua and find it has no sciLua or numLua projects. (Python has > many. And I don't know why python is a good language to do matrix > computing.). I am wondering if no array in Lua is a problem to manipulate > matrixes and vectors. I beg to differ: http://luaforge.net/projects/numluaIt's not as maintained as I'd wanted it to be, but it's fairly usable. > Does the following description imply that Lua is a good languge for > scietific problems? Definitely. Cheers, luis. -- A mathematician is a device for turning coffee into theorems.         -- P. Erdos -- Luis Carvalho Applied Math PhD Student - Brown University PGP Key: E820854A <[hidden email]>
Open this post in threaded view
|

## Re: numerical Lua?

 Hi,   I downloaded the code but don't know how to use it. Sorry. I could not compile the code successfully under Windows.   If numlua can deliver matlab or fortran like matrix manipulation syntax, it definitely rules.   Ben  On 4/22/06, Luis Carvalho <[hidden email]> wrote: > I am learning Lua and find it has no sciLua or numLua projects. (Python has> many. And I don't know why python is a good language to do matrix > computing.). I am wondering if no array in Lua is a problem to manipulate> matrixes and vectors.I beg to differ:http://luaforge.net/projects/numlua It's not as maintained as I'd wanted it to be, but it's fairly usable.> Does the following description imply that Lua is a good languge for> scietific problems?Definitely.Cheers, luis.--A mathematician is a device for turning coffee into theorems.       -- P. Erdos--Luis CarvalhoApplied Math PhD Student - Brown UniversityPGP Key: E820854A <[hidden email]>
Open this post in threaded view
|

## Re: numerical Lua?

 > I downloaded the code but don't know how to use it. Sorry. I could not > compile the code successfully under Windows. Ah, don't worry: it's not your fault. I haven't fixed the win32 settings in the Makefiles. Anyway, I've just released a win32-binary version of numlua. Please check http://luaforge.net/projects/numlua again. > If numlua can deliver matlab or fortran like matrix manipulation syntax, it > definitely rules.   Well, it does. And it is faster than Matlab in many respects -- loops, for instance -- thanks to Lua. I'll take some time this summer to write some documentation and fix some bugs. Any comments are welcome! Cheers, luis. -- A mathematician is a device for turning coffee into theorems.         -- P. Erdos -- Luis Carvalho Applied Math PhD Student - Brown University PGP Key: E820854A <[hidden email]>
Open this post in threaded view
|

## Re: numerical Lua?

 I have a few questions about numerical lua. 1)  I am on a 32 bit x86 windows box and I'd like to link in tuned version of BLAS such as atlas. It's not clear how to do this with the current source.  What would be nice is to simply have a separate .dll containing the standard BLAS and perhaps LAPACK libraries that could be swapped out.  Is there any chance luamatrix.dll does this?  I notice that you have Fortran source which probably means you are not using the standard cblas wrappers for blas. In fact I did a dumpbin on luamatrix.dll and confirmed this.  Apparently luamatrix.dll includes additional routines other than the standard blas, which means swapping out  different blas libraries is not possible in the current setup. 2)  Is there a convenient C or maybe C++ API for the library?  The thought here is to facilitate the ability to extend the library in a straightforward manner, perhaps with the help of tolua or SWIG.  Normally anything that needs to iterate over individual matrix elements should probably be written in C and then the interface extended to LUA.  The high level logic, gui's etc. could then remain in LUA. I would dearly like to see a standard numerical library like this for LUA and it looks like you have a pretty good start here just playing around with it.  I actually implemented an interface like this between LUA blas and additional user defined libraries for a client, but alas can not release it since it's proprietary.
Open this post in threaded view
|

## Re: numerical Lua?

 In reply to this post by Luis Carvalho-2 I am pleased at how complete your port is.  You've overloaded arithmetic operators and have even included support for N-d arrays. It is almost as easy to use as Matlab!  However there is still a performance issue which surprised me.  On my windows xp box  (amd x2 3800) I ran a simple benchmark that inverts some matrices and performs some other calculations.  I then ran the equivalent code in Matlab 6.5. The results are NumLua 24 sec.s Matlab. 15.2 sec.s here's the code for each LUA u0 = os.time() x = matrix(100,100) I = matrix.eye(100) for i,j in x:entries() do                 x[i][j] = math.random() end for q=1,1000 do         x = I + matrix.inv(x) end u1 = os.time() print(u1-u0) Matlab tic ; x = rand(100,100) ; I = eye(100) ; for q=1:1000     x = I + inv(x) ; end toc Now since I used your binary distribution I'm not using tuned BLAS libraries, but then again neither is Matlab.  (I think 6.5 uses Atlas but I'm not sure).  Your code does a lot of type checking, but then again so does Matlabs.  I would have expected Matlab to incur more overhead and therefore the LUA code to be more competitive.   One of the big attractions for using LUA for numerical applications is it's easy interface to C, (with the help of maybe tolua or SWIG).  With C doing the heavy number crunching, one can get a factor of 5 or 6 improvment in speed over Matlab.  (Contrary to the claims of some speed is still of huge importance for numerical applications.  I have Matlab scripts that take 2 days to run, even on my overclocked dual core athlon) LUA is great for configuration and high level logic.  I have used it this way before with great success.  The problem now with the NumLua approach,  with all the syntactic sugar, is that an overhead penalty is being paid that defeats the purpose of using LUA.  If we want free numerical software,  SciPy is a lot more mature and also has a nice C interface.
Open this post in threaded view
|

## Re: numerical Lua?

Open this post in threaded view
|

## Re: numerical Lua?

Open this post in threaded view
|

## Re: numerical Lua?

 In reply to this post by SevenThunders Hi, Two possibilities for the speed difference.  Here are my guesses. > for q=1,1000 do >         x = I + matrix.inv(x) > end Is the above code generating a new 100x100 matrix at each iteration?  I'd be surprized if Matlab didn't optmize this to operate on the matrix 'x' itself, instead of creating a new matrix and copying it over 'x'.  This eliminates a lot of memory allocation, copying, and a lot of garbage collection. It woudl be *very* hard to optimize this in Lua. You'd need at least two changes. One is on the semantics of something like      x = matrix(100, 100)      y = x I don't know if NumericLua does it, but you'd have to create a copy of x and place a reference to it on y, instead of just pointing y to x.  It would be nice if you did this by copy-on-write.  Matlab does something similar to that with function arguments. The problem is that you'd still need a change to Lua metametods. Perhaps Lua 5.2 could have it. We'd need access to an optional l-value. In expressions such as      x = a  +  b + c      x = (a + b) + c the first operation would have changed x. The second would receive x both as a target and a source. This means you'd have to implement your operations in such a way that the source and target can be the *same*, which is not trivial for things like matrix multiplication. > for i,j in x:entries() do > x[i][j] = math.random() > end I didn't look at the code, but the syntax above suggests you are creating a temporary object for x[i], which then receives the [j] indexing. This has got to be slower than doing something like x(i, j). I know the syntax looks better, but I x(i, j) is also fine and could be *much* faster.
Open this post in threaded view
|

## Re: numerical Lua?

 In reply to this post by Luis Carvalho-2 Again I want to emphasize that this is a real nice effort.  I just would like to see some things improved, primarily in the area of performance.  Unfortunately it's not a trivial exercise.  You do a lot of type checking, and I don't think you are, or can do lazy evaluation of vector or matrix expressions.  (Maybe the LUA experts could weigh in here.  Is there any way to do lazy evaluation inside lua?) My approach to this problem was not nearly as elegant as yours, but it was a heck of a lot faster. The first thing I did was generate a C include file filled with Macros that handled special case calls to the BLAS and LAPACK libraries, that are commonly used, such as matrix multiplies,  triangular matrix back substitutions etc. etc.  I had a set of wrapper routines that worked directly off of my main matrix structure. If you use a garbage collector at the C level it makes the C level matrix operations a lot easier to do.  Thus I modified lua and the C code to use the Hans Boehm garbage collector (only two lines of code need to be changed to do this as discussed elsewhere).  Now I don't have to worry about memory leaks and the macros make it fairly straightforward to implement any matrix math in C.   My philosophy was that anything that had to loop over matrix elements or loop over multiple matrix operations should be in C and should link to BLAS or routines optimized for BLAS (e.g. Lapack) whenever possible.  Thus having a good C api is even more important than the LUA interface.   On the LUA side I want to be able to do things like view the matrices or submatrices and perhaps graph the results out using gnuplot and control the C code, and insert LUA tables of parameters into the C code.  Thus one wants to automate the interface to the extent possible (tolua and swig are great for this.) As a final point, on a windows box, there is a library interface issue.  f2c.exe appends an underscore to all fortran routines.  However on windows all the BLAS libraries I've used do not do this.  Atlas leaves the fortran names unchanged and AMDs acml libraries does the same for their C stubs, but capitilizes the names of the Fortran routines so that every routine has two entry points. I might be able to help, but some more thought would have to go into how to address the performance issues in a reasonable manner.
Open this post in threaded view
|

## Re: numerical Lua?

 Hallo, On 5/7/06, SevenThunders <[hidden email]> wrote: > expressions.  (Maybe the LUA experts could weigh in here.  Is there any way > to do lazy evaluation inside lua?) >      It's possible attaching to a table a metatable with a __index metamethod set. -- -alex http://www.ventonegro.org/
Open this post in threaded view
|

## Re: numerical Lua?

 In reply to this post by Diego Nehab-3 Yes that's why I was wondering if it is possible at all to do lazy evaluation in LUA.  I know C++ does it using ugly template hacks.   Such things are normally  in the purview of functional languages.  I am not complaining though, I like LUA nice and simple.  I hardly ever use the more advanced features anyway. No doubt Matlab no longer copies the matrix for sub-expressions that are just thrown away.  It has gotten a bit better.  It is stil very slow when it comes to loops however.  It is encouraging to hear that Louis is seeing some performance gains on his benchmarks.  I should try figuring out how to link it to acml BLAS and see if that improves things. By the way changing x and I to local in my code had no effect on the overall time (at least within a second).  I wonder if it would be faster if run in luajit?
Open this post in threaded view
|