Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyflamegpu comparison #9

Draft
wants to merge 20 commits into
base: FLAMEGPU2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Agents/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ using Pkg; Pkg.instantiate()
using Agents
using Test
using Statistics
using Random

# Does not use @bencmark, due to jobs being OOM killed for long-running models, with a higher maximum runtime to allow the required repetitions.
# Does not use @benchmark, due to jobs being OOM killed for long-running models, with a higher maximum runtime to allow the required repetitions.
# enabling the gc between samples did not resolve this BenchmarkTools.DEFAULT_PARAMETERS.gcsample = false
# Runs each model SAMPLE_COUNT + 1 times, discarding hte first timing (which includes compilation)
# Runs each model SAMPLE_COUNT + 1 times, discarding the first timing (which includes compilation)
SAMPLE_COUNT = 10
SEED = 12

# Boids
Random.seed!(SEED)
times = []
for i in 0:SAMPLE_COUNT
Comment on lines +6 to 17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the changes to this file can be removed when #10 is merged

(model, agent_step!, model_step!) = Models.flocking(
Expand All @@ -20,6 +23,7 @@ for i in 0:SAMPLE_COUNT
match_factor = 0.05,
visual_distance = 5.0,
extent = (400, 400),
seed = rand(Int),
)
step_stats = @timed step!(model, agent_step!, model_step!, 100)
if i > 0
Expand All @@ -30,9 +34,14 @@ println("Agents.jl Flocking times (ms)", map(x -> x * 1e3, times))
println("Agents.jl Flocking (mean ms): ", (Statistics.mean(times)) * 1e3)

# Schelling
Random.seed!(SEED)
times = []
for i in 0:SAMPLE_COUNT
(model, agent_step!, model_step!) = Models.schelling(griddims = (500, 500), numagents = 200000)
(model, agent_step!, model_step!) = Models.schelling(
griddims = (500, 500),
numagents = 200000,
seed = rand(Int),
)
step_stats = @timed step!(model, agent_step!, model_step!, 100)
if i > 0
append!(times, step_stats.time)
Expand Down
8 changes: 6 additions & 2 deletions FLAMEGPU2/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import re
import math
import statistics
import random

SCRIPT_PATH = pathlib.Path(__file__).parent
BUILD_DIR = "build"
CONFIG = "Release"
REPETITIONS = 10
SEED = 12


def extract_times(lines):
Expand Down Expand Up @@ -59,8 +61,9 @@ def extract_times(lines):
pop_gen_times = []
main_times = []
sim_times = []
random.seed(SEED)
for i in range(0, REPETITIONS):
result = subprocess.run([str(flocking_binary_path), "-s", "100"], stdout=subprocess.PIPE)
result = subprocess.run([str(flocking_binary_path), "-s", "100", "-r", str(random.randint(0, 999999999))], stdout=subprocess.PIPE)
# @todo make this less brittle
lines = result.stdout.decode('utf-8').splitlines()
pre_pop_time, pop_gen_time, main_time, sim_time = extract_times(lines)
Expand Down Expand Up @@ -90,8 +93,9 @@ def extract_times(lines):
pop_gen_times = []
main_times = []
sim_times = []
random.seed(SEED)
for i in range(0, REPETITIONS):
result = subprocess.run([str(schelling_binary_path), "-s", "100"], stdout=subprocess.PIPE)
result = subprocess.run([str(schelling_binary_path), "-s", "100", "-r", str(random.randint(0, 999999999))], stdout=subprocess.PIPE)
# @todo make this less brittle
lines = result.stdout.decode('utf-8').splitlines()
pre_pop_time, pop_gen_time, main_time, sim_time = extract_times(lines)
Expand Down
13 changes: 6 additions & 7 deletions FLAMEGPU2/src/boids2D.cu
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ FLAMEGPU_HOST_DEVICE_FUNCTION void vec3Normalize(float &x, float &y) {
}

/**
* Ensure that the x and y position are withini the defined boundary area, wrapping to the far side if out of bounds.
* Ensure that the x and y position are within the defined boundary area, wrapping to the far side if out of bounds.
* Performs the operation in place
* @param x x component of the vector
* @param y y component of the vector
Expand Down Expand Up @@ -126,7 +126,7 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, flamegpu::MessageSpatial2D, flamegpu::Message
float agent_fx = FLAMEGPU->getVariable<float>("fx");
float agent_fy = FLAMEGPU->getVariable<float>("fy");

// Boids percieved center
// Boids perceived center
float perceived_centre_x = 0.0f;
float perceived_centre_y = 0.0f;
int perceived_count = 0;
Expand Down Expand Up @@ -234,7 +234,7 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, flamegpu::MessageSpatial2D, flamegpu::Message
agent_x += agent_fx * TIME_SCALE;
agent_y += agent_fy * TIME_SCALE;

// Wramp position
// Wrap position
const float MIN_POSITION = FLAMEGPU->environment.getProperty<float>("MIN_POSITION");
const float MAX_POSITION = FLAMEGPU->environment.getProperty<float>("MAX_POSITION");
wrapPosition(agent_x, agent_y, MIN_POSITION, MAX_POSITION);
Expand Down Expand Up @@ -296,7 +296,6 @@ int main(int argc, const char ** argv) {
// Spatial 2D messages implicitly have float members x and y, so they do not need to be defined
message.newVariable<float>("fx");
message.newVariable<float>("fy");
message.newVariable<float>("fz");

// Boid agent
flamegpu::AgentDescription agent = model.newAgent("Boid");
Expand All @@ -319,7 +318,7 @@ int main(int argc, const char ** argv) {
// Create Model Runner
flamegpu::CUDASimulation simulator(model);

// If enabled, define the visualsiation for the model
// If enabled, define the visualisation for the model
#ifdef FLAMEGPU_VISUALISATION
flamegpu::visualiser::ModelVis visualisation = simulator.getVisualisation();
{
Expand Down Expand Up @@ -405,10 +404,10 @@ int main(int argc, const char ** argv) {
// Execute the simulation
simulator.simulate();

// Print the exeuction time to stdout
// Print the execution time to stdout
fprintf(stdout, "simulate (s): %.6f\n", simulator.getElapsedTimeSimulation());

// Join the visualsition if required
// Join the visualisation if required
#ifdef FLAMEGPU_VISUALISATION
visualisation.join();
#endif
Expand Down
7 changes: 4 additions & 3 deletions FLAMEGPU2/src/schelling.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fstream>
#include <array>
#include <chrono>
#include <numeric>

#include "flamegpu/flamegpu.h"
#include "flamegpu/detail/SteadyClockTimer.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ FLAMEGPU_AGENT_FUNCTION(determine_status, flamegpu::MessageArray2D, flamegpu::Me
diff_type_neighbours += (my_type != message_type) && (message_type != UNOCCUPIED);
}

int isHappy = (static_cast<float>(same_type_neighbours) / (same_type_neighbours + diff_type_neighbours)) > THRESHOLD;
int isHappy = same_type_neighbours ? (static_cast<float>(same_type_neighbours) / (same_type_neighbours + diff_type_neighbours)) > THRESHOLD : false;
FLAMEGPU->setVariable<unsigned int>("happy", isHappy);
unsigned int my_next_type = ((my_type != UNOCCUPIED) && isHappy) ? my_type : UNOCCUPIED;
FLAMEGPU->setVariable<unsigned int>("next_type", my_next_type);
Expand Down Expand Up @@ -149,12 +150,12 @@ int main(int argc, const char ** argv) {


// Define a submodel for conflict resolution for agent movement
// This is neccesary for parlalel random movement of agents, to resolve conflict between agents moveing to the same location
// This is necessary for parallel random movement of agents, to resolve conflict between agents moving to the same location
flamegpu::ModelDescription submodel("plan_movement");
// Submodels require an exit condition function, so they do not run forever
submodel.addExitCondition(movement_resolved);
{
// Define the submodel environemnt
// Define the submodel environment
flamegpu::EnvironmentDescription env = submodel.Environment();
env.newProperty<unsigned int>("spaces_available", 0);

Expand Down
18 changes: 12 additions & 6 deletions Mesa/Flocking/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
import timeit
import gc
import statistics
import random

setup = f"""
REPETITIONS = 3
SEED = 12

random.seed(SEED)
a = []
for i in range(0, REPETITIONS):
setup=f"""
gc.enable()
import os, sys
sys.path.insert(0, os.path.abspath("."))
Expand All @@ -21,13 +28,12 @@ def runthemodel(flock):
flock = BoidFlockers(
population=80000,
width=400,
height=400
height=400,
seed={random.randint(0, 999999999)}
)
"""

tt = timeit.Timer('runthemodel(flock)', setup=setup)
SAMPLES=3
a = tt.repeat(SAMPLES, 1)
tt = timeit.Timer('runthemodel(flock)', setup=setup)
a.append(tt.timeit(1))
print("Mesa Flocking times (ms):", list(map(lambda x: x * 1e3, a)))
print("Mesa Flocking (mean ms):", statistics.mean(a)*1e3)

21 changes: 12 additions & 9 deletions Mesa/Schelling/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@
import timeit
import gc
import statistics
import random

REPETITIONS = 3
SEED = 12

setup = f"""
random.seed(SEED)
a = []
for i in range(0, REPETITIONS):
setup = f"""
gc.enable()
import os, sys
sys.path.insert(0, os.path.abspath("."))

from model import SchellingModel

import random
random.seed(2)

def runthemodel(schelling):
for i in range(0, 100):
schelling.step()

schelling = SchellingModel(
height=500,
width=500,
density=0.8
density=0.8,
seed={random.randint(0, 999999999)}
)
"""

tt = timeit.Timer('runthemodel(schelling)', setup=setup)
SAMPLES=3
a = tt.repeat(SAMPLES, 1)
tt = timeit.Timer('runthemodel(schelling)', setup=setup)
a.append(tt.timeit(1))
print("Mesa schelling times (ms):", list(map(lambda x: x * 1e3, a)))
print("Mesa schelling (mean ms):", statistics.mean(a)*1e3)

Expand Down
Loading