Skip to content

Commit

Permalink
Fixes for wait_for_windows()
Browse files Browse the repository at this point in the history
Conway pretty example
  • Loading branch information
ggcrunchy committed Dec 18, 2015
1 parent b54d860 commit 8f2979e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 93 deletions.
46 changes: 28 additions & 18 deletions scripts/lib/misc/program.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,42 +193,52 @@ void read_idx(std::vector<dim_t> &dims, std::vector<ty> &data, const char *name)
--
wait_for_windows = function(how, w1, w2, w3)
--
local function any_closed ()
if w3 then
return w1:close() or w2:close() or w3:close()
elseif w2 then
return w1:close() or w2:close()
return function()
local is_open

if w3 and w3:close() then
is_open = false
elseif w2 and w2:close() then
is_open = false
else
return w1:close()
is_open = not w1:close()
end
end

--
if how == "until" then
return any_closed
else
return function()
return not any_closed()
if how == "until" then
return not is_open
else
return is_open
end
end
end,

--
wait_for_windows_close = function(how, w1, w2, w3)
local done = GetLib().wait_for_windows(how, w1, w2, w3)
local going = GetLib().wait_for_windows(how, w1, w2, w3)

return function()
if done() then
w1:destroy() -- TODO: Do this right... how?
local is_going = going()
local is_open = is_going

if w2 then
if how == "until" then
is_open = not is_open
end

if not is_open then
if not w1:close() then
w1:destroy() -- TODO: Do this right... how?
end

if w2 and not w2:close() then
w2:destroy()
end

if w3 then
if w3 and not w3:close() then
w3:destroy()
end
end

return is_going
end
--
end
Expand Down
5 changes: 1 addition & 4 deletions scripts/tests/graphics/conway.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,5 @@ AF.main(function()
local C1 = Comp(nHood == _3)
-- Update state
state = env(state * C0 + C1)
end, function()
return not myWindow:close()
end, "normal_gc") -- evict old states every now and then
myWindow:destroy()
end, AF.wait_for_windows_close("while", myWindow), "normal_gc") -- evict old states every now and then
end)
124 changes: 67 additions & 57 deletions scripts/tests/graphics/conway_pretty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,73 @@
]]

-- Modules --
local lib = require("lib.af_lib")
local AF = require("lib.af_lib")

lib.main(function()
static const float h_kernel[] = {1, 1, 1, 1, 0, 1, 1, 1, 1};
static const int reset = 500;
static const int game_w = 128, game_h = 128;
-- Shorthands --
local Comp, WC = AF.CompareResult, AF.WrapConstant

std::cout << "This example demonstrates the Conway's Game of Life using ArrayFire" << std::endl
<< "There are 4 simple rules of Conways's Game of Life" << std::endl
<< "1. Any live cell with fewer than two live neighbours dies, as if caused by under-population." << std::endl
<< "2. Any live cell with two or three live neighbours lives on to the next generation." << std::endl
<< "3. Any live cell with more than three live neighbours dies, as if by overcrowding." << std::endl
<< "4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction." << std::endl
<< "Each white block in the visualization represents 1 alive cell, black space represents dead cells" << std::endl
;
std::cout << "The conway_pretty example visualizes all the states in Conway" << std::endl
<< "Red : Cells that have died due to under population" << std::endl
<< "Yellow: Cells that continue to live from previous state" << std::endl
<< "Green : Cells that are new as a result of reproduction" << std::endl
<< "Blue : Cells that have died due to over population" << std::endl
;
std::cout << "This examples is throttled so as to be a better visualization" << std::endl;
af::Window simpleWindow(512, 512, "Conway's Game Of Life - Current State");
af::Window prettyWindow(512, 512, "Conway's Game Of Life - Visualizing States");
simpleWindow.setPos(25, 25);
prettyWindow.setPos(125, 15);
int frame_count = 0;
// Initialize the kernel array just once
const af::array kernel(3, 3, h_kernel, afHost);
array state;
state = (af::randu(game_h, game_w, f32) > 0.4).as(f32);
array display = tile(state, 1, 1, 3, 1);
while(!simpleWindow.close() && !prettyWindow.close()) {
af::timer delay = timer::start();
if(!simpleWindow.close()) simpleWindow.image(state);
if(!prettyWindow.close()) prettyWindow.image(display);
frame_count++;
// Generate a random starting state
if(frame_count % reset == 0)
state = (af::randu(game_h, game_w, f32) > 0.5).as(f32);
// Convolve gets neighbors
af::array nHood = convolve(state, kernel);
// Generate conditions for life
// state == 1 && nHood < 2 ->> state = 0
// state == 1 && nHood > 3 ->> state = 0
// else if state == 1 ->> state = 1
// state == 0 && nHood == 3 ->> state = 1
af::array C0 = (nHood == 2);
af::array C1 = (nHood == 3);
array a0 = (state == 1) && (nHood < 2); // Die of under population
array a1 = (state != 0) && (C0 || C1); // Continue to live
array a2 = (state == 0) && C1; // Reproduction
array a3 = (state == 1) && (nHood > 3); // Over-population
display = join(2, a0 + a1, a1 + a2, a3).as(f32);
// Update state
state = state * C0 + C1;
double fps = 5;
while(timer::stop(delay) < (1 / fps)) { }
}
AF.main(function()
local h_kernel = {1, 1, 1, 1, 0, 1, 1, 1, 1}
local reset = 500
local game_w, game_h = 128, 128

print("This example demonstrates the Conway's Game of Life using ArrayFire")
print("There are 4 simple rules of Conways's Game of Life")
print("1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.")
print("2. Any live cell with two or three live neighbours lives on to the next generation.")
print("3. Any live cell with more than three live neighbours dies, as if by overcrowding.")
print("4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.")
print("Each white block in the visualization represents 1 alive cell, black space represents dead cells")

print("The conway_pretty example visualizes all the states in Conway")
print("Red : Cells that have died due to under population")
print("Yellow: Cells that continue to live from previous state")
print("Green : Cells that are new as a result of reproduction")
print("Blue : Cells that have died due to over population")

print("This examples is throttled so as to be a better visualization")
local simpleWindow = AF.Window(512, 512, "Conway's Game Of Life - Current State")
local prettyWindow = AF.Window(512, 512, "Conway's Game Of Life - Visualizing States")
simpleWindow:setPos(25, 25)
prettyWindow:setPos(125, 15)
local frame_count = 0
-- Initialize the kernel array just once
local kernel = AF.array(3, 3, h_kernel, "afHost")
local state
state = Comp(AF.randu(game_h, game_w, "f32") > WC(0.4)):as("f32")
local AND, OR = AF["and"], AF["or"]
local _0_5, _0, _1, _2, _3 = WC(0.5), WC(0), WC(1), WC(2), WC(3)
local display = AF.tile(state, 1, 1, 3, 1)
AF.EnvLoopWhile_Mode(function(env)
local delay = AF.timer_start()
if not simpleWindow:close() then
simpleWindow:image(state)
end
if not prettyWindow:close() then
prettyWindow:image(display)
end
frame_count = frame_count + 1
-- Generate a random starting state
if frame_count % reset == 0 then
state = Comp(AF.randu(game_h, game_w, "f32") > _0_5):as("f32")
end
-- Convolve gets neighbors
local nHood = AF.convolve(state, kernel)
-- Generate conditions for life
-- state == 1 && nHood < 2 ->> state = 0
-- state == 1 && nHood > 3 ->> state = 0
-- else if state == 1 ->> state = 1
-- state == 0 && nHood == 3 ->> state = 1
local C0 = Comp(nHood == _2)
local C1 = Comp(nHood == _3)
local a0 = AND(Comp(state == _1), Comp(nHood < _2)) -- Die of under population
local a1 = AND(Comp(state ~= _0), OR(C0, C1)) -- Continue to live
local a2 = AND(Comp(state == _0), C1) -- Reproduction
local a3 = AND(Comp(state == _1), Comp(nHood > _3)) -- Over-population
display = env(AF.join(2, a0 + a1, a1 + a2, a3):as("f32"))
-- Update state
state = env(state * C0 + C1)
local fps = 5
while AF.timer_stop(delay) < (1 / fps) do end
end, AF.wait_for_windows_close("while", simpleWindow, prettyWindow), "normal_gc") -- evict old states every now and then
end)
7 changes: 1 addition & 6 deletions scripts/tests/graphics/histogram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,5 @@ AF.main(function()
AF.EnvLoopWhile(function()
myWindow:hist(hist_out, 0, 255)
imgWnd:image(img:as("u8"))
end, function()
return not myWindow:close() and not imgWnd:close()
end)
-- :/
myWindow:destroy()
imgWnd:destroy()
end, AF.wait_for_windows_close("while", myWindow, imgWnd))
end)
5 changes: 1 addition & 4 deletions scripts/tests/graphics/plot2d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,5 @@ AF.main(function()
elseif val < -pi then
sign = 1
end
end, function()
return not myWindow:close()
end, "normal_gc") -- evict old states every now and then
myWindow:destroy()
end, AF.wait_for_windows_close("while", myWindow), "normal_gc") -- evict old states every now and then
end)
5 changes: 1 addition & 4 deletions scripts/tests/graphics/plot3d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@ AF.main(function()
--or in the flattened xyz-triplet array with size 3n x 1
myWindow:plot3(Pts)
t=t+0.01
end, function()
return myWindow:close()
end)
myWindow:destroy()
end, AF.wait_for_windows_close("until", myWindow))
end)
1 change: 1 addition & 0 deletions scripts/tests/pde/swe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ AF.main(function(argc, arv)
end, function()
return AF.progress(iter, t, time_total)
end, "normal_gc") -- evict old states every now and then
-- win:destroy() -- err...
end

print("Simulation of shallow water equations")
Expand Down

0 comments on commit 8f2979e

Please sign in to comment.