Merge pull request #2600 from manveru/pony-language

Add Pony language
This commit is contained in:
Arfon Smith
2015-11-05 08:08:38 -08:00
10 changed files with 657 additions and 0 deletions

30
samples/Pony/circle.pony Normal file
View File

@@ -0,0 +1,30 @@
use "collections"
class Circle
var _radius: F32
new create(radius': F32) =>
_radius = radius'
fun ref get_radius(): F32 =>
_radius
fun ref get_area(): F32 =>
F32.pi() * _radius.pow(2)
fun ref get_circumference(): F32 =>
2 * _radius * F32.pi()
actor Main
new create(env: Env) =>
for i in Range[F32](1.0, 101.0) do
let c = Circle(i)
var str =
"Radius: " + c.get_radius().string() + "\n" +
"Circumference: " + c.get_circumference().string() + "\n" +
"Area: " + c.get_area().string() + "\n"
env.out.print(str)
end

32
samples/Pony/counter.pony Normal file
View File

@@ -0,0 +1,32 @@
use "collections"
actor Counter
var _count: U32
new create() =>
_count = 0
be increment() =>
_count = _count + 1
be get_and_reset(main: Main) =>
main.display(_count)
_count = 0
actor Main
var _env: Env
new create(env: Env) =>
_env = env
var count: U32 = try env.args(1).u32() else 10 end
var counter = Counter
for i in Range[U32](0, count) do
counter.increment()
end
counter.get_and_reset(this)
be display(result: U32) =>
_env.out.print(result.string())

261
samples/Pony/gups-opt.pony Normal file
View File

@@ -0,0 +1,261 @@
use "options"
use "time"
use "collections"
class Config
var logtable: U64 = 20
var iterate: U64 = 10000
var logchunk: U64 = 10
var logactors: U64 = 2
fun ref apply(env: Env): Bool =>
var options = Options(env)
options
.add("logtable", "l", I64Argument)
.add("iterate", "i", I64Argument)
.add("chunk", "c", I64Argument)
.add("actors", "a", I64Argument)
for option in options do
match option
| ("table", var arg: I64) => logtable = arg.u64()
| ("iterate", var arg: I64) => iterate = arg.u64()
| ("chunk", var arg: I64) => logchunk = arg.u64()
| ("actors", var arg: I64) => logactors = arg.u64()
| let err: ParseError =>
err.report(env.out)
env.out.print(
"""
gups_opt [OPTIONS]
--table N log2 of the total table size. Defaults to 20.
--iterate N number of iterations. Defaults to 10000.
--chunk N log2 of the chunk size. Defaults to 10.
--actors N log2 of the actor count. Defaults to 2.
"""
)
return false
end
end
env.out.print(
"logtable: " + logtable.string() +
"\niterate: " + iterate.string() +
"\nlogchunk: " + logchunk.string() +
"\nlogactors: " + logactors.string()
)
true
actor Main
let _env: Env
let _config: Config = Config
var _updates: U64 = 0
var _confirm: U64 = 0
let _start: U64
var _actors: Array[Updater] val
new create(env: Env) =>
_env = env
if _config(env) then
let actor_count = 1 << _config.logactors
let loglocal = _config.logtable - _config.logactors
let chunk_size = 1 << _config.logchunk
let chunk_iterate = chunk_size * _config.iterate
_updates = chunk_iterate * actor_count
_confirm = actor_count
var updaters = recover Array[Updater](actor_count) end
for i in Range[U64](0, actor_count) do
updaters.push(Updater(this, actor_count, i, loglocal, chunk_size,
chunk_iterate * i))
end
_actors = consume updaters
_start = Time.nanos()
for a in _actors.values() do
a.start(_actors, _config.iterate)
end
else
_start = 0
_actors = recover Array[Updater] end
end
be done() =>
if (_confirm = _confirm - 1) == 1 then
for a in _actors.values() do
a.done()
end
end
be confirm() =>
_confirm = _confirm + 1
if _confirm == _actors.size() then
let elapsed = (Time.nanos() - _start).f64()
let gups = _updates.f64() / elapsed
_env.out.print(
"Time: " + (elapsed / 1e9).string() +
"\nGUPS: " + gups.string()
)
end
actor Updater
let _main: Main
let _index: U64
let _updaters: U64
let _chunk: U64
let _mask: U64
let _loglocal: U64
let _output: Array[Array[U64] iso]
let _reuse: List[Array[U64] iso] = List[Array[U64] iso]
var _others: (Array[Updater] val | None) = None
var _table: Array[U64]
var _rand: U64
new create(main:Main, updaters: U64, index: U64, loglocal: U64, chunk: U64,
seed: U64)
=>
_main = main
_index = index
_updaters = updaters
_chunk = chunk
_mask = updaters - 1
_loglocal = loglocal
_rand = PolyRand.seed(seed)
_output = _output.create(updaters)
let size = 1 << loglocal
_table = Array[U64].undefined(size)
var offset = index * size
try
for i in Range[U64](0, size) do
_table(i) = i + offset
end
end
be start(others: Array[Updater] val, iterate: U64) =>
_others = others
iteration(iterate)
be apply(iterate: U64) =>
iteration(iterate)
fun ref iteration(iterate: U64) =>
let chk = _chunk
for i in Range(0, _updaters) do
_output.push(
try
_reuse.pop()
else
recover Array[U64](chk) end
end
)
end
for i in Range(0, _chunk) do
var datum = _rand = PolyRand(_rand)
var updater = (datum >> _loglocal) and _mask
try
if updater == _index then
_table(i) = _table(i) xor datum
else
_output(updater).push(datum)
end
end
end
try
let to = _others as Array[Updater] val
repeat
let data = _output.pop()
if data.size() > 0 then
to(_output.size()).receive(consume data)
else
_reuse.push(consume data)
end
until _output.size() == 0 end
end
if iterate > 1 then
apply(iterate - 1)
else
_main.done()
end
be receive(data: Array[U64] iso) =>
try
for i in Range(0, data.size()) do
let datum = data(i)
var j = (datum >> _loglocal) and _mask
_table(j) = _table(j) xor datum
end
data.clear()
_reuse.push(consume data)
end
be done() =>
_main.confirm()
primitive PolyRand
fun apply(prev: U64): U64 =>
(prev << 1) xor if prev.i64() < 0 then _poly() else 0 end
fun seed(from: U64): U64 =>
var n = from % _period()
if n == 0 then
return 1
end
var m2 = Array[U64].undefined(64)
var temp = U64(1)
try
for i in Range(0, 64) do
m2(i) = temp
temp = this(temp)
temp = this(temp)
end
end
var i: U64 = 64 - n.clz()
var r = U64(2)
try
while i > 0 do
temp = 0
for j in Range(0, 64) do
if ((r >> j) and 1) != 0 then
temp = temp xor m2(j)
end
end
r = temp
i = i - 1
if ((n >> i) and 1) != 0 then
r = this(r)
end
end
end
r
fun _poly(): U64 => 7
fun _period(): U64 => 1317624576693539401

View File

@@ -0,0 +1,3 @@
actor Main
new create(env: Env) =>
env.out.print("Hello, world.")

View File

@@ -0,0 +1,188 @@
use "files"
use "options"
use "collections"
actor Worker
new mandelbrot(main: Main, x: U64, y: U64, width: U64, iterations: U64,
limit: F32, real: Array[F32] val, imaginary: Array[F32] val)
=>
var view: Array[U8] iso =
recover
Array[U8]((y - x) * (width >> 3))
end
let group_r = Array[F32].undefined(8)
let group_i = Array[F32].undefined(8)
var row = x
try
while row < y do
let prefetch_i = imaginary(row)
var col: U64 = 0
while col < width do
var j: U64 = 0
while j < 8 do
group_r.update(j, real(col + j))
group_i.update(j, prefetch_i)
j = j + 1
end
var bitmap: U8 = 0xFF
var n = iterations
repeat
var mask: U8 = 0x80
var k: U64 = 0
while k < 8 do
let r = group_r(k)
let i = group_i(k)
group_r.update(k, ((r * r) - (i * i)) + real(col + k))
group_i.update(k, (2.0 * r * i) + prefetch_i)
if ((r * r) + (i * i)) > limit then
bitmap = bitmap and not mask
end
mask = mask >> 1
k = k + 1
end
until (bitmap == 0) or ((n = n - 1) == 1) end
view.push(bitmap)
col = col + 8
end
row = row + 1
end
main.draw(x * (width >> 3), consume view)
end
actor Main
var iterations: U64 = 50
var limit: F32 = 4.0
var chunks: U64 = 16
var width: U64 = 16000
var actors: U64 = 0
var header: U64 = 0
var real: Array[F32] val = recover Array[F32] end
var imaginary: Array[F32] val = recover Array[F32] end
var outfile: (File | None) = None
new create(env: Env) =>
try
arguments(env)
let length = width
let recip_width = 2.0 / width.f32()
var r = recover Array[F32](length) end
var i = recover Array[F32](length) end
for j in Range(0, width) do
r.push((recip_width * j.f32()) - 1.5)
i.push((recip_width * j.f32()) - 1.0)
end
real = consume r
imaginary = consume i
spawn_actors()
create_outfile()
end
be draw(offset: U64, pixels: Array[U8] val) =>
match outfile
| var out: File =>
out.seek_start(header + offset)
out.write(pixels)
if (actors = actors - 1) == 1 then
out.dispose()
end
end
fun ref create_outfile() =>
match outfile
| var f: File =>
f.print("P4\n " + width.string() + " " + width.string() + "\n")
header = f.size()
f.set_length((width * (width >> 3)) + header)
end
fun ref spawn_actors() =>
actors = ((width + (chunks - 1)) / chunks)
var rest = width % chunks
if rest == 0 then rest = chunks end
var x: U64 = 0
var y: U64 = 0
for i in Range(0, actors - 1) do
x = i * chunks
y = x + chunks
Worker.mandelbrot(this, x, y, width, iterations, limit, real, imaginary)
end
Worker.mandelbrot(this, y, y + rest, width, iterations, limit, real,
imaginary)
fun ref arguments(env: Env) ? =>
let options = Options(env)
options
.add("iterations", "i", I64Argument)
.add("limit", "l", F64Argument)
.add("chunks", "c", I64Argument)
.add("width", "w", I64Argument)
.add("output", "o", StringArgument)
for option in options do
match option
| ("iterations", var arg: I64) => iterations = arg.u64()
| ("limit", var arg: F64) => limit = arg.f32()
| ("chunks", var arg: I64) => chunks = arg.u64()
| ("width", var arg: I64) => width = arg.u64()
| ("output", var arg: String) =>
outfile = try File(FilePath(env.root, arg)) end
| let err: ParseError => err.report(env.out) ; usage(env) ; error
end
end
fun tag usage(env: Env) =>
env.out.print(
"""
mandelbrot [OPTIONS]
The binary output can be converted to a BMP with the following command
(ImageMagick Tools required):
convert <output> JPEG:<output>.jpg
Available options:
--iterations, -i Maximum amount of iterations to be done for each pixel.
Defaults to 50.
--limit, -l Square of the limit that pixels need to exceed in order
to escape from the Mandelbrot set.
Defaults to 4.0.
--chunks, -c Maximum line count of chunks the image should be
divided into for divide & conquer processing.
Defaults to 16.
--width, -w Lateral length of the resulting mandelbrot image.
Defaults to 16000.
--output, -o File to write the output to.
"""
)

130
samples/Pony/mixed.pony Normal file
View File

@@ -0,0 +1,130 @@
use "collections"
actor Worker
var _env: Env
new create(env: Env) =>
_env = env
var a: U64 = 86028157
var b: U64 = 329545133
var result = factorize(a*b)
var correct =
try
(result.size() == 2) and
(result(0) == 86028157) and
(result(1) == 329545133)
else
false
end
fun ref factorize(bigint: U64) : Array[U64] =>
var factors = Array[U64](2)
if bigint <= 3 then
factors.push(bigint)
else
var d: U64 = 2
var i: U64 = 0
var n = bigint
while d < n do
if (n % d) == 0 then
i = i + 1
factors.push(d)
n = n / d
else
d = if d == 2 then 3 else (d + 2) end
end
end
factors.push(d)
end
factors
actor Ring
var _env: Env
var _size: U32
var _pass: U32
var _repetitions: U32
var _next: Ring
new create(env: Env, size: U32, pass: U32, repetitions: U32) =>
_env = env
_size = size
_pass = pass
_repetitions = repetitions
_next = spawn_ring(_env, _size, _pass)
run()
new neighbor(env: Env, next: Ring) =>
_env = env
_next = next
_size = 0
_pass = 0
_repetitions = 0
be apply(i: U32) =>
if i > 0 then
_next(i - 1)
else
run()
end
fun ref run() =>
if _repetitions > 0 then
_repetitions = _repetitions - 1
_next(_pass * _size)
Worker(_env)
end
fun tag spawn_ring(env: Env, size: U32, pass': U32) : Ring =>
var next: Ring = this
for i in Range[U32](0, size) do
next = Ring.neighbor(env, next)
end
next
actor Main
var _size: U32 = 50
var _count: U32 = 20
var _pass: U32 = 10000
var _repetitions: U32 = 5
var _env: Env
new create(env: Env) =>
_env = env
try
arguments()
start_benchmark()
else
usage()
end
fun ref arguments() ? =>
_count = _env.args(1).u32()
_size = _env.args(2).u32()
_pass = _env.args(3).u32()
_repetitions = _env.args(4).u32()
fun ref start_benchmark() =>
for i in Range[U32](0, _count) do
Ring(_env, _size, _pass, _repetitions)
end
fun ref usage() =>
_env.out.print(
"""
mixed OPTIONS
N number of actors in each ring"
N number of rings"
N number of messages to pass around each ring"
N number of times to repeat"
"""
)