Upload
trancong
View
221
Download
3
Embed Size (px)
Citation preview
1
IntroductionUse-cases
DemoBenchmarks!Conclusion
LuaFor Science!
...you monster
Leo Antunes
HPAC
January 2014
Leo Antunes Lua
2
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
What is Lua?
I Interpreted language
I ”Teenage”: developed in the early 90s in Brasil (yay!)
I Reference implementation interpreted; also JITed, LLVM
I How many Python users out there?
Leo Antunes Lua
3
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
The Language
I Dynamically typedI Imperative, but multi-paradigm(ish):
I Functional: currying, higher-order functions(however: side-effects!)
I Object-oriented: some syntactic sugar, but mostly bare-bones(everything can be made into an object)
I Implemented in pure ANSI C (extremely portable)I Types:
I Usual: numbers, strings, bools, functions and nil
I Higher-level: tables, tables and - did I mention - tables?I Object orientation through tables: metatables (cf. Python)
I Nothing very new, so we’ll focus on API
Leo Antunes Lua
4
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
The Language
I Dynamically typedI Imperative, but multi-paradigm(ish):
I Functional: currying, higher-order functions(however: side-effects!)
I Object-oriented: some syntactic sugar, but mostly bare-bones(everything can be made into an object)
I Implemented in pure ANSI C (extremely portable)I Types:
I Usual: numbers, strings, bools, functions and nilI Higher-level: tables, tables and - did I mention - tables?
I Object orientation through tables: metatables (cf. Python)
I Nothing very new, so we’ll focus on API
Leo Antunes Lua
5
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
The Language
I Dynamically typedI Imperative, but multi-paradigm(ish):
I Functional: currying, higher-order functions(however: side-effects!)
I Object-oriented: some syntactic sugar, but mostly bare-bones(everything can be made into an object)
I Implemented in pure ANSI C (extremely portable)I Types:
I Usual: numbers, strings, bools, functions and nilI Higher-level: tables, tables and - did I mention - tables?I Object orientation through tables: metatables (cf. Python)
I Nothing very new, so we’ll focus on API
Leo Antunes Lua
6
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
A Bit of Syntax (not our focus, but...)
-- some examples
function factorial(n)
local x = 1
for i = 2,n do
x = x * i
end
return x
end
some_table = {
name="whatever",
func=factorial
}
some_table.func(3) --> 6
I No () for single argument(allows currying like Haskel’s)
I Can return multiple results(no packing/unpacking)
I Table indices start at 1(convention; no actual arrays)
I Scoping: globals per default(cf. Python; real closures)
I No named arguments(but can pass table)
Leo Antunes Lua
7
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
A Bit of Syntax (not our focus, but...)
-- some examples
function factorial(n)
local x = 1
for i = 2,n do
x = x * i
end
return x
end
some_table = {
name="whatever",
func=factorial
}
some_table.func(3) --> 6
I No () for single argument(allows currying like Haskel’s)
I Can return multiple results(no packing/unpacking)
I Table indices start at 1(convention; no actual arrays)
I Scoping: globals per default(cf. Python; real closures)
I No named arguments(but can pass table)
Leo Antunes Lua
8
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
A Bit of Syntax (not our focus, but...)
-- some examples
function factorial(n)
local x = 1
for i = 2,n do
x = x * i
end
return x
end
some_table = {
name="whatever",
func=factorial
}
some_table.func(3) --> 6
I No () for single argument(allows currying like Haskel’s)
I Can return multiple results(no packing/unpacking)
I Table indices start at 1(convention; no actual arrays)
I Scoping: globals per default(cf. Python; real closures)
I No named arguments(but can pass table)
Leo Antunes Lua
9
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
A Bit of Syntax (not our focus, but...)
-- some examples
function factorial(n)
local x = 1
for i = 2,n do
x = x * i
end
return x
end
some_table = {
name="whatever",
func=factorial
}
some_table.func(3) --> 6
I No () for single argument(allows currying like Haskel’s)
I Can return multiple results(no packing/unpacking)
I Table indices start at 1(convention; no actual arrays)
I Scoping: globals per default(cf. Python; real closures)
I No named arguments(but can pass table)
Leo Antunes Lua
10
IntroductionUse-cases
DemoBenchmarks!Conclusion
What is Lua?The LanguageA Bit of Syntax
A Bit of Syntax (not our focus, but...)
-- some examples
function factorial(n)
local x = 1
for i = 2,n do
x = x * i
end
return x
end
some_table = {
name="whatever",
func=factorial
}
some_table.func(3) --> 6
I No () for single argument(allows currying like Haskel’s)
I Can return multiple results(no packing/unpacking)
I Table indices start at 1(convention; no actual arrays)
I Scoping: globals per default(cf. Python; real closures)
I No named arguments(but can pass table)
Leo Antunes Lua
11
IntroductionUse-cases
DemoBenchmarks!Conclusion
BackgroundGeneral Pros/ConsWhat about science?
Use-cases
That’s all nice and good, but what is it for?
Application LuaembedableC Library
extensible
I Same stack-API on both cases (interpreter is thin-wrapper for lib)
I Focus: simplicity and performance over features(remember tables?)
I What seemed like limitations are now elegance
I Can wrap lib in C module or use Alien/FFI (cf. ctypes)
Leo Antunes Lua
12
IntroductionUse-cases
DemoBenchmarks!Conclusion
BackgroundGeneral Pros/ConsWhat about science?
Use-cases
That’s all nice and good, but what is it for?
Application LuaembedableC Library
extensible
I Same stack-API on both cases (interpreter is thin-wrapper for lib)
I Focus: simplicity and performance over features(remember tables?)
I What seemed like limitations are now elegance
I Can wrap lib in C module or use Alien/FFI (cf. ctypes)
Leo Antunes Lua
13
IntroductionUse-cases
DemoBenchmarks!Conclusion
BackgroundGeneral Pros/ConsWhat about science?
Use-cases
That’s all nice and good, but what is it for?
Application LuaembedableC Library
extensible
I Same stack-API on both cases (interpreter is thin-wrapper for lib)
I Focus: simplicity and performance over features(remember tables?)
I What seemed like limitations are now elegance
I Can wrap lib in C module or use Alien/FFI (cf. ctypes)
Leo Antunes Lua
14
IntroductionUse-cases
DemoBenchmarks!Conclusion
BackgroundGeneral Pros/ConsWhat about science?
General Pros/Cons...or: when should I use Lua instead of X?
I Not-so-rapid prototyping:
7 Boilerplate for OO7 Standard library is paltry7 LuaRocks still a bit lacking3 But: performance!
I Embedding:
3 Very easy with C (cf. Python), theoretically easy with all3 Handle configuration, customization, abstraction (DSLs)
I As Glue Language:
3 Also very easy to extend (remember: same API)3 ”Selective” Rapid Prototyping
Leo Antunes Lua
15
IntroductionUse-cases
DemoBenchmarks!Conclusion
BackgroundGeneral Pros/ConsWhat about science?
What about science?
I Lua Libraries:
3 Numerical: NumLua (FFTW, BLAS, LAPACK)3 Plotting: PLPlot, GNUPlot7 MPI: very basic wrapper7 Many missing, e.g.: Linear Programing
I Parallelization
7 No threads from Lua’s side (ANSI C; no actual GIL)3 But of course: C code can multithread3 Elegant and easy coroutines3 Easy to implement message-passing multi-threading in C
I Misc:
7 Only one number type: double; So: no boolean ops3 debug.debug(): interactive breakpoint for Lua code
Leo Antunes Lua
16
IntroductionUse-cases
DemoBenchmarks!Conclusion
BackgroundGeneral Pros/ConsWhat about science?
What about science?
I Lua Libraries:
3 Numerical: NumLua (FFTW, BLAS, LAPACK)3 Plotting: PLPlot, GNUPlot7 MPI: very basic wrapper7 Many missing, e.g.: Linear Programing
I Parallelization
7 No threads from Lua’s side (ANSI C; no actual GIL)3 But of course: C code can multithread3 Elegant and easy coroutines3 Easy to implement message-passing multi-threading in C
I Misc:
7 Only one number type: double; So: no boolean ops3 debug.debug(): interactive breakpoint for Lua code
Leo Antunes Lua
17
IntroductionUse-cases
DemoBenchmarks!Conclusion
I Up til now: tough sell
I Where it shines: performance (later) and simplicity forembedding/extending
Leo Antunes Lua
18
IntroductionUse-cases
DemoBenchmarks!Conclusion
I Up til now: tough sell
I Where it shines: performance (later) and simplicity forembedding/extending
Leo Antunes Lua
19
IntroductionUse-cases
DemoBenchmarks!Conclusion
Subject allocator
Lua Program1
Lua as DSL2
C/C++ module4
C++ lib
3
5
Leo Antunes Lua
20
IntroductionUse-cases
DemoBenchmarks!Conclusion
Figure : from http://attractivechaos.github.io/plb (independent)
Leo Antunes Lua
21
IntroductionUse-cases
DemoBenchmarks!Conclusion
Conclusion
I Lua is cool!
I Will I use it again? Probably.
I Should you use it? Definitely! For science? Maybe...
I Is it a silver bullet? No. But I don’t think that exists anyway
Leo Antunes Lua
22
IntroductionUse-cases
DemoBenchmarks!Conclusion
Conclusion
I Lua is cool!
I Will I use it again? Probably.
I Should you use it? Definitely! For science? Maybe...
I Is it a silver bullet? No. But I don’t think that exists anyway
Leo Antunes Lua
23
IntroductionUse-cases
DemoBenchmarks!Conclusion
Thanks!Questions?
Leo Antunes Lua