Example implemented from "The computer Language Benchmarks Game" - Fannkuch-Redux

http://benchmarksgame.alioth.debian.org/

Complete description of the fannkuch-redux : https://benchmarksgame.alioth.debian.org/u64q/fannkuchredux-description.html#fannkuchredux

Introduced classes

Redefined classes

redef class Sys

actors :: fannkuchredux $ Sys

The main class of the program.

All class definitions

redef class Sys

actors :: fannkuchredux $ Sys

The main class of the program.
package_diagram actors::fannkuchredux fannkuchredux actors actors actors::fannkuchredux->actors pthreads pthreads actors->pthreads ...pthreads ... ...pthreads->pthreads a_star-m a_star-m a_star-m->actors::fannkuchredux

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module concurrent_collections

pthreads :: concurrent_collections

Introduces thread-safe concurrent collections
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module extra

pthreads :: extra

Offers some POSIX threads services that are not available on all platforms
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module pthreads

pthreads :: pthreads

Main POSIX threads support and intro the classes Thread, Mutex and Barrier
module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module actors

actors :: actors

Abstraction of the actors concepts

Children

module a_star-m

a_star-m

# Example implemented from "The computer Language Benchmarks Game" - Fannkuch-Redux
# http://benchmarksgame.alioth.debian.org/
#
# Complete description of the fannkuch-redux :
# https://benchmarksgame.alioth.debian.org/u64q/fannkuchredux-description.html#fannkuchredux
module fannkuchredux is example, no_warning("missing-doc")

import actors

class FannkuchRedux
	actor

	var p: Array[Int] is noautoinit
	var pp: Array[Int] is noautoinit
	var count: Array[Int] is noautoinit

	fun print_p do
		for i in [0..p.length[ do printn p[i] + 1
		print ""
	end

	fun first_permutation(idx: Int) do
		for i in [0..p.length[ do p[i] = i

		for i in [0..count.length[.reverse_iterator do
			var d = idx / fact[i]
			count[i] = d
			idx = idx % fact[i]

			p.copy_to(0, i+1, pp, 0)

			for j in [0..i] do p[j] = if j + d <= i then pp[j+d] else pp[j+d-i-1]
		end
	end

	fun next_permutation do
		var first = p[1]
		p[1] = p[0]
		p[0] = first

		var i = 1
		count[i] += 1
		while count[i] > i do
			count[i] = 0
			i += 1

			p[0] = p[1]
			var next = p[0]

			for j in [1..i[ do p[j] = p[j+1]
			p[i] = first
			first = next

			count[i] += 1
		end
	end

	fun count_flips: Int do
		var flips = 1
		var first = p[0]
		if p[first] != 0 then
			p.copy_to(0, pp.length, pp, 0)
			while pp[first] != 0 do
				flips += 1
				var lo = 1
				var hi = first - 1
				while lo < hi do
					var t = pp[lo]
					pp[lo] = pp[hi]
					pp[hi] = t
					lo += 1
					hi -= 1
				end
				var t = pp[first]
				pp[first] = first
				first = t
			end
		end
		return flips
	end

	fun run_task(task: Int) do
		var idx_min = task * chunk_sz
		var idx_max = fact[n].min(idx_min + chunk_sz)

		first_permutation(idx_min)

		var maxflips = 1
		var chk_sum = 0

		for i in [idx_min..idx_max[ do
			if p[0] != 0 then
				var flips = count_flips
				maxflips = maxflips.max(flips)
				chk_sum += if i % 2 == 0 then flips else -flips
			end
			if i + 1 != idx_max then next_permutation
		end

		max_flips[task] = maxflips
		chk_sums[task] = chk_sum
	end

	fun run do
		p = new Array[Int].with_capacity(n)
		for i in [0..n[ do p.add(0)
		pp = new Array[Int].with_capacity(n)
		for i in [0..n[ do pp.add(0)
		count = new Array[Int].with_capacity(n)
		for i in [0..n[ do count.add(0)

		var task = 0
		loop
			task = task_id.get_and_increment
			if task < n_tasks then run_task(task) else break
		end
	end
end

redef class Sys
	var n_chunks = 150
	var chunk_sz: Int is noautoinit
	var n_tasks: Int is noautoinit
	var n: Int is noautoinit
	var fact: Array[Int] is noautoinit
	var max_flips: Array[Int] is noautoinit
	var chk_sums: Array[Int] is noautoinit
	var task_id = new AtomicInt(0)
	var nb_actors = 8
end

fun print_result(n, res, chk: Int) do
	print chk.to_s + "\nPfannfuchen(" + n.to_s + ") = " + res.to_s

end

n = if args.is_empty then 7 else args[0].to_i

fact = new Array[Int].with_capacity(n+1)
fact[0] = 1
for i in [1..n] do fact.add(fact[i-1] * i)

chunk_sz = (fact[n] + n_chunks - 1) / n_chunks
n_tasks = (fact[n] + chunk_sz - 1) / chunk_sz
max_flips = new Array[Int].with_capacity(n_tasks)
for i in [0..n_tasks[ do max_flips.add(0)
chk_sums = new Array[Int].with_capacity(n_tasks)
for i in [0..n_tasks[ do chk_sums.add(0)

var actors = new Array[FannkuchRedux].with_capacity(nb_actors)
for i in [0..nb_actors[ do
	var a = new FannkuchRedux
	actors.add(a)
	a.async.run
end

for i in [0..nb_actors[ do
	actors[i].async.terminate
	actors[i].async.wait_termination
end

var res = 0
for i in max_flips do res = res.max(i)

var chk = 0
for i in chk_sums do
	chk += i
end

print_result(n, res, chk)
lib/actors/examples/fannkuchredux/fannkuchredux.nit:15,1--184,25