Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Nov 28, 2022
1 parent 10ca148 commit 01f17ca
Showing 1 changed file with 18 additions and 203 deletions.
221 changes: 18 additions & 203 deletions tests/swig/python/pop/test_device_agent_vector.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import pytest
from unittest import TestCase
from pyflamegpu import *
import time

AGENT_COUNT = 10;
MODEL_NAME = "model";
SUBMODEL_NAME = "submodel";
AGENT_NAME = "agent";

class SetGet(pyflamegpu.HostFunctionCallback):
class setGet(pyflamegpu.HostFunctionCallback):
def run(self,FLAMEGPU):
# Accessing DeviceAgentVector like this would previously lead to an access violation (Issue #522, PR #751)
av = FLAMEGPU.agent(AGENT_NAME).getPopulationData();
for ai in av:
ai.setVariableInt("int", ai.getVariableInt("int") + 12);


class SetGetHalf(pyflamegpu.HostFunctionCallback):
class setGetHalf(pyflamegpu.HostFunctionCallback):
def run(self,FLAMEGPU):
agent = FLAMEGPU.agent(AGENT_NAME);
av = agent.getPopulationData();
Expand Down Expand Up @@ -253,7 +254,7 @@ def run(self,FLAMEGPU):
# Host agent birth 1 agent
agent.newAgent().setVariableInt("int", -12);
# Test again (need to test each with it's own update)
ASSERT_EQ(av.empty(), false);
assert av.empty() == False;


class HostAgentBirthAutoSync_step_size(pyflamegpu.HostFunctionCallback):
Expand Down Expand Up @@ -390,42 +391,6 @@ def run(self,FLAMEGPU):
else:
assert a.getVariableInt("int")== -(i - (AGENT_COUNT + 4)); # Host new agents



class HostAgentBirthAutoSync_step_insert2(pyflamegpu.HostFunctionCallback):
def run(self,FLAMEGPU):
agent = FLAMEGPU.agent(AGENT_NAME);
# It shouldn't matter whether this is called before/after
av = agent.getPopulationData();
# Agent begins with AGENT_COUNT agents, value 0- (AGENT_COUNT-1)
assert av.size() == AGENT_COUNT;
# Host agent birth 4 agents
for i in range(4):
a = agent.newAgent();
a.setVariableInt("int", -i);

# Insert 4 agents at the end of the initial list
it1 = av.begin();
for i in range(1):
it1 += 1;
it5 = av.begin();
for i in range(5):
it5 += 1;
av.insert(AGENT_COUNT, it1, it5);
# Check the size has changed correctly
assert av.size() == AGENT_COUNT + 8;
# Test again (need to test each with it's own update)
for i in range(AGENT_COUNT + 8):
a = av[i];
if i < AGENT_COUNT:
assert a.getVariableInt("int") == i;
elif i < AGENT_COUNT + 4:
assert a.getVariableInt("int") == 1 + (i - AGENT_COUNT); # We inserted copies of first items [1,4]
else:
assert a.getVariableInt("int") == -(i - (AGENT_COUNT + 4)); # Host new agents



class HostAgentBirthAutoSync_step_erase(pyflamegpu.HostFunctionCallback):
def run(self,FLAMEGPU):
agent = FLAMEGPU.agent(AGENT_NAME);
Expand Down Expand Up @@ -551,62 +516,17 @@ def run(self,FLAMEGPU):
t = vec_b[i];
t.setVariableID("id_copy", t.getID());


class AgentID_DeviceAgentVectorBirth2(pyflamegpu.HostFunctionCallback):
def run(self,FLAMEGPU):
agt_a = FLAMEGPU.agent("agent", "a");
agt_b = FLAMEGPU.agent("agent", "b");
vec_a = agt_a.getPopulationData();
vec_b = agt_b.getPopulationData();
birth_ct_a = vec_a.size();
birth_ct_b = vec_b.size();
copy_a = pyflamegpu.AgentInstance(vec_a[0]);
copy_b = pyflamegpu.AgentInstance(vec_a[0]);
it_a = vec_a.begin();
for i in range(birth_ct_a / 2):
it_a += 1;
it_b = vec_a.begin();
for i in range(birth_ct_b / 2):
it_b += 1;
vec_a.insert(it_a, birth_ct_a, copy_a);
vec_b.insert(it_b, birth_ct_b, copy_b);
for i in range(birth_ct_a, 2 * birth_ct_a):
t = vec_a[i];
t.setVariableID("id_copy", t.getID());
class DeviceAgentVectorTest(TestCase):

for i in range(birth_ct_b, 2 * birth_ct_b):
t = vec_b[i];
t.setVariableID("id_copy", t.getID());


class AgentID_DeviceAgentVectorBirth3(pyflamegpu.HostFunctionCallback):
def run(self,FLAMEGPU):
agt_a = FLAMEGPU.agent("agent", "a");
agt_b = FLAMEGPU.agent("agent", "b");
vec_a = agt_a.getPopulationData();
vec_b = agt_b.getPopulationData();
birth_pos_a = vec_a.size();
vec_a.insert(birth_pos_a / 2, vec_b.begin(), vec_b.end());
birth_pos_b = vec_b.size();
vec_b.insert(birth_pos_b / 2, vec_a.begin(), vec_a.end());
for t in vec_a:
t.setVariableID("id_copy", t.getID());

for t in vec_b:
t.setVariableID("id_copy", t.getID());


class AgentInstanceTest(TestCase):

def test_SetGet(self):
def test_setGet(self):
# Initialise an agent population with values in a variable [0,1,2..N]
# Inside a step function, retrieve the agent population as a DeviceAgentVector
# Update all agents by adding 12 to their value
# After model completion, retrieve the agent population and check their values are [12,13,14..N+12]
model = pyflamegpu.ModelDescription(MODEL_NAME);
agent = model.newAgent(AGENT_NAME);
agent.newVariableInt("int", 0);
model.addStepFunctionCallback(SetGet());
model.addStepFunctionCallback(setGet());

# Init agent pop
av = pyflamegpu.AgentVector(agent, AGENT_COUNT);
Expand All @@ -631,15 +551,15 @@ def test_SetGet(self):
for i in range(AGENT_COUNT):
assert av[i].getVariableInt("int") == i + 24;

def test_SetGetHalf(self):
def test_setGetHalf(self):
# Initialise an agent population with values in a variable [0,1,2..N]
# Inside a step function, retrieve the agent population as a DeviceAgentVector
# Update half agents (contiguous block) by adding 12 to their value
# After model completion, retrieve the agent population and check their values are [12,13,14..N+12]
model = pyflamegpu.ModelDescription(MODEL_NAME);
agent = model.newAgent(AGENT_NAME);
agent.newVariableInt("int", 0);
model.addStepFunctionCallback(SetGetHalf());
model.addStepFunctionCallback(setGetHalf());

# Init agent pop
av = pyflamegpu.AgentVector(agent, AGENT_COUNT);
Expand Down Expand Up @@ -696,7 +616,7 @@ def test_GetIndex(self):
FLAMEGPU_AGENT_FUNCTION(MasterIncrement, flamegpu::MessageNone, flamegpu::MessageNone) {
FLAMEGPU->setVariable<unsigned int>("uint", FLAMEGPU->getVariable<unsigned int>("uint") + 1);
return flamegpu::ALIVE;
}ggg
}
"""

def test_Resize(self):
Expand Down Expand Up @@ -738,6 +658,7 @@ def test_Resize(self):


def test_SubmodelResize(self):
time.sleep(15)
# In void CUDAFatAgentStateList::resize() (as of 2021-03-04)
# The minimum buffer len is 1024 and resize grows by 25%
# So to trigger resize, we can grow from 1024->2048
Expand Down Expand Up @@ -1142,24 +1063,6 @@ def test_HostAgentBirthAutoSync_insert1(self):
# Also confirm the agents weren't added twice
assert av.size() == AGENT_COUNT + 8;

def test_HostAgentBirthAutoSync_insert2(self):
# Test the templated insert method, it doesn't use the common insert
# This is kind of redundant, as begin() will be called before insert()
model = pyflamegpu.ModelDescription(MODEL_NAME);
agent = model.newAgent(AGENT_NAME);
agent.newVariableInt("int", 10);
model.addStepFunctionCallback(HostAgentBirthAutoSync_step_insert2());
av = pyflamegpu.AgentVector(agent, AGENT_COUNT);
for i in range(AGENT_COUNT):
av[i].setVariableInt("int", i);

sim = pyflamegpu.CUDASimulation(model);
sim.setPopulationData(av);
sim.step();
sim.getPopulationData(av);
# Also confirm the agents weren't added twice
assert av.size() == AGENT_COUNT + 8;

def test_HostAgentBirthAutoSync_erase(self):
model = pyflamegpu.ModelDescription(MODEL_NAME);
agent = model.newAgent(AGENT_NAME);
Expand Down Expand Up @@ -1271,7 +1174,7 @@ def test_AgentID_MultipleStatesUniqueIDs(self):
sim.getPopulationData(pop_out_a, "a");
sim.getPopulationData(pop_out_b, "b");

ids = {};
ids = set();
# Validate that there are no ID collisions
for a in pop_out_a:
ids.add(a.getID());
Expand All @@ -1285,7 +1188,7 @@ def test_AgentID_MultipleStatesUniqueIDs(self):
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth


assert ids.size() == 4 * POP_SIZE; # No collisions
assert len(ids) == 4 * POP_SIZE; # No collisions

def test_AgentID_MultipleAgents(self):
POP_SIZE = 100;
Expand Down Expand Up @@ -1319,108 +1222,20 @@ def test_AgentID_MultipleAgents(self):
sim.getPopulationData(pop_out_a);
sim.getPopulationData(pop_out_b);

ids_a = {};
ids_b = {};
ids_a = set();
ids_b = set();
# Validate that there are no ID collisions
for a in pop_out_a:
ids_a.add(a.getID());
if a.getVariableID("id_copy") != pyflamegpu.ID_NOT_SET:
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth

assert ids_a.size() == 2 * POP_SIZE; # No collisions
assert len(ids_a) == 2 * POP_SIZE; # No collisions
for a in pop_out_b:
ids_b.add(a.getID());
if a.getVariableID("id_copy") != pyflamegpu.ID_NOT_SET:
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth

assert ids_b.size() == 2 * POP_SIZE; # No collisions

def test_AgentID_MultipleStatesUniqueIDs2(self):
POP_SIZE = 100;
# Create agents via AgentVector to two agent states
# DeviceAgentVector Birth creates new agent in both states (in the middle of the current agents)
# Store agent IDs to an agent variable inside model
# Export agents and check their IDs are unique
# Also check that the id's copied during model match those at export

model = pyflamegpu.ModelDescription("test_agentid");
agent = model.newAgent("agent");
agent.newVariableID("id_copy", pyflamegpu.ID_NOT_SET);
agent.newState("a");
agent.newState("b");

layer_a = model.newLayer();
layer_a.addHostFunctionCallback(AgentID_DeviceAgentVectorBirth2());

pop_in = pyflamegpu.AgentVector(agent, POP_SIZE);

sim = pyflamegpu.CUDASimulation(model);
sim.setPopulationData(pop_in, "a");
sim.setPopulationData(pop_in, "b");

sim.step();

pop_out_a = pyflamegpu.AgentVector(agent);
pop_out_b = pyflamegpu.AgentVector(agent);

sim.getPopulationData(pop_out_a, "a");
sim.getPopulationData(pop_out_b, "b");

ids = {};
# Validate that there are no ID collisions
for a in pop_out_a:
ids.add(a.getID());
if a.getVariableID("id_copy") != pyflamegpu.ID_NOT_SET:
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth

for a in pop_out_b:
ids.add(a.getID());
if a.getVariableID("id_copy") != pyflamegpu.ID_NOT_SET:
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth

assert ids.size() == 4 * POP_SIZE; # No collisions
assert len(ids_b) == 2 * POP_SIZE; # No collisions

def test_AgentID_MultipleStatesUniqueIDs3(self):
POP_SIZE = 100;
# Create agents via AgentVector to two agent states
# DeviceAgentVector Birth creates new agent in both states (in the middle of the current agents)
# Store agent IDs to an agent variable inside model
# Export agents and check their IDs are unique
# Also check that the id's copied during model match those at export

model = pyflamegpu.ModelDescription("test_agentid");
agent = model.newAgent("agent");
agent.newVariableID("id_copy", pyflamegpu.ID_NOT_SET);
agent.newState("a");
agent.newState("b");

layer_a = model.newLayer();
layer_a.addHostFunctionCallback(AgentID_DeviceAgentVectorBirth3());

pop_in = pyflamegpu.AgentVector(agent, POP_SIZE);

sim = pyflamegpu.CUDASimulation(model);
sim.setPopulationData(pop_in, "a");
sim.setPopulationData(pop_in, "b");

sim.step();

pop_out_a = pyflamegpu.AgentVector(agent);
pop_out_b = pyflamegpu.AgentVector(agent);

sim.getPopulationData(pop_out_a, "a");
sim.getPopulationData(pop_out_b, "b");

ids ={};
# Validate that there are no ID collisions
for a in pop_out_a:
ids.add(a.getID());
if a.getVariableID("id_copy") != pyflamegpu.ID_NOT_SET:
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth

for a in pop_out_b:
ids.add(a.getID());
if a.getVariableID("id_copy") != pyflamegpu.ID_NOT_SET:
assert a.getID() == a.getVariableID("id_copy"); # ID is same as reported at birth

assert ids.size() == 5 * POP_SIZE; # No collisions

0 comments on commit 01f17ca

Please sign in to comment.