From a55ddfa2d9c2938ffad92b77b44fda0cf655c733 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 5 May 2009 00:29:20 +0000 Subject: [PATCH] Add "arg" global value to provide command line arguments. --- src/j2se/lua.java | 20 +++++-- src/test/perf/fannkuch.lua | 20 +++---- src/test/perf/mandelbrot.lua | 109 +++++++++++++++++++++++++---------- src/test/perf/nbody.lua | 8 +-- version.properties | 2 +- 5 files changed, 108 insertions(+), 51 deletions(-) diff --git a/src/j2se/lua.java b/src/j2se/lua.java index 7175cac2..7ce08c7d 100644 --- a/src/j2se/lua.java +++ b/src/j2se/lua.java @@ -31,6 +31,8 @@ import org.luaj.compiler.LuaC; import org.luaj.lib.DebugLib; import org.luaj.platform.J2sePlatform; import org.luaj.vm.LFunction; +import org.luaj.vm.LString; +import org.luaj.vm.LTable; import org.luaj.vm.Lua; import org.luaj.vm.LuaState; import org.luaj.vm.Platform; @@ -40,7 +42,7 @@ import org.luaj.vm.Platform; * lua command for use in java se environments. */ public class lua { - private static final String version = Lua._VERSION + "Copyright (C) 2008 luaj.org"; + private static final String version = Lua._VERSION + " Copyright (C) 2009 luaj.org"; private static final String usage = "usage: java -cp luaj-j2se.jar lua [options] [script [args]].\n" + @@ -116,10 +118,16 @@ public class lua { processing = true; for ( int i=0; i maxflips then maxflips = flips end -- New maximum? break end q[q1] = q1 + if q1 >= 4 then + local i, j = 2, q1 - 1 + repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j + end q1 = qq end end @@ -36,7 +37,7 @@ local function fannkuch(n) p[2], p[3] = p[3], p[2]; odd = true -- Rotate 1<-2 and 1<-2<-3. for i=3,n do local sx = s[i] - if sx ~= 1 then sx = sx - 1; s[i] = sx; break end + if sx ~= 1 then s[i] = sx-1; break end if i == n then return maxflips end -- Out of permutations. s[i] = i -- Rotate 1<-...<-i+1. @@ -46,6 +47,5 @@ local function fannkuch(n) until false end -local arg = {...} -local n = tonumber(arg and arg[1]) or 5 -print("Pfannkuchen("..n..") = "..fannkuch(n)) \ No newline at end of file +local n = tonumber(arg and arg[1]) or 1 +io.write("Pfannkuchen(", n, ") = ", fannkuch(n), "\n") diff --git a/src/test/perf/mandelbrot.lua b/src/test/perf/mandelbrot.lua index ad2a0a7a..b0a35a99 100644 --- a/src/test/perf/mandelbrot.lua +++ b/src/test/perf/mandelbrot.lua @@ -1,40 +1,91 @@ --- The Computer Language Shootout +-- The Computer Language Benchmarks Game -- http://shootout.alioth.debian.org/ -- contributed by Mike Pall --- modified for luaj to remove dependency on io +-- modified by Rob Kendrick to be parallel +-- modified by Isaac Gouy + +-- called with the following arguments on the command line; +-- 1: size of mandelbrot to generate +-- 2: number of children to spawn (defaults to 6, which works well on 4-way) +-- If this is a child, then there will be additional parameters; +-- 3: start row +-- 4: end row +-- +-- Children buffer up their output and emit it to stdout when +-- finished, to avoid stalling due to a full pipe. -local arg = {...} local width = tonumber(arg and arg[1]) or 100 +local children = tonumber(arg and arg[2]) or 6 +local srow = tonumber(arg and arg[3]) +local erow = tonumber(arg and arg[4]) + local height, wscale = width, 2/width local m, limit2 = 50, 4.0 -local write, char = print, string.char +local write, char = io.write, string.char -write("P4\n", width, " ", height, "\n") +if not srow then + -- we are the parent process. emit the header, and then spawn children -for y=0,height-1 do - local Ci = 2*y / height - 1 - for xb=0,width-1,8 do - local bits = 0 - local xbb = xb+7 - for x=xb,xbb < width and xbb or width-1 do - bits = bits + bits - local Zr, Zi, Zrq, Ziq = 0.0, 0.0, 0.0, 0.0 - local Cr = x * wscale - 1.5 - for i=1,m do - local Zri = Zr*Zi - Zr = Zrq - Ziq + Cr - Zi = Zri + Zri + Ci - Zrq = Zr*Zr - Ziq = Zi*Zi - if Zrq + Ziq > limit2 then - bits = bits + 1 - break + local workunit = math.floor(width / (children + 1)) + local handles = { } + + write("P4\n", width, " ", height, "\n") + + children = children - 1 + + for i = 0, children do + local cs, ce + + if i == 0 then + cs = 0 + ce = workunit + elseif i == children then + cs = (workunit * i) + 1 + ce = width - 1 + else + cs = (workunit * i) + 1 + ce = cs + workunit - 1 + end + + handles[i + 1] = io.popen(("%s %s %d %d %d %d"):format( + arg[-1], arg[0], width, children + 1, cs, ce)) + end + + -- collect answers, and emit + for i = 0, children do + write(handles[i + 1]:read "*a") + end + +else + -- we are a child process. do the work allocated to us. + local obuff = { } + for y=srow,erow do + local Ci = 2*y / height - 1 + for xb=0,width-1,8 do + local bits = 0 + local xbb = xb+7 + for x=xb,xbb < width and xbb or width-1 do + bits = bits + bits + local Zr, Zi, Zrq, Ziq = 0.0, 0.0, 0.0, 0.0 + local Cr = x * wscale - 1.5 + for i=1,m do + local Zri = Zr*Zi + Zr = Zrq - Ziq + Cr + Zi = Zri + Zri + Ci + Zrq = Zr*Zr + Ziq = Zi*Zi + if Zrq + Ziq > limit2 then + bits = bits + 1 + break + end end end - end - if xbb >= width then - for x=width,xbb do bits = bits + bits + 1 end - end - write(char(255-bits)) - end + if xbb >= width then + for x=width,xbb do bits = bits + bits + 1 end + end + obuff[#obuff + 1] = char(255 - bits) + end + end + + write(table.concat(obuff)) end diff --git a/src/test/perf/nbody.lua b/src/test/perf/nbody.lua index d688717f..91d7d5c6 100644 --- a/src/test/perf/nbody.lua +++ b/src/test/perf/nbody.lua @@ -1,7 +1,6 @@ -- The Great Computer Language Shootout -- http://shootout.alioth.debian.org/ -- contributed by Isaac Gouy, tuned by Mike Pall --- modified for luaj to remove dependency on io local sqrt = math.sqrt @@ -114,12 +113,11 @@ local function offsetMomentum(b, nbody) b[1].vz = -pz / SOLAR_MASS end -local arg = {...} local N = tonumber(arg and arg[1]) or 1000 local bodies = { Sun, Jupiter, Saturn, Uranus, Neptune } -local nbody = #bodies +local nbody = table.getn(bodies) offsetMomentum(bodies, nbody) -print( tostring(energy(bodies, nbody))) +io.write( string.format("%0.9f",energy(bodies, nbody)), "\n") for i=1,N do advance(bodies, nbody, 0.01) end -print( tostring(energy(bodies, nbody))) +io.write( string.format("%0.9f",energy(bodies, nbody)), "\n") diff --git a/version.properties b/version.properties index 3855fefd..ec47a948 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -version: 0.96 +version: 0.97