Skip to content

Commit

Permalink
Tests of ID->index conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Oct 16, 2023
1 parent 946118a commit d0402c7
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ def run(self, FLAMEGPU):
for i in range(ID_AGENT_COUNT):
vertex = vertices[i + 1];
vertex.setPropertyInt("foo", i);
edges[i + 1, i + 1];
edge = edges[i + 1, i + 1];
edge.setPropertyUInt("id", i);

class EnvironmentDirectedGraphTest(TestCase):
def test_HostGetResetGet(self):
Expand Down Expand Up @@ -1271,6 +1272,7 @@ def test_EdgeSameSrcDest(self):
model = pyflamegpu.ModelDescription("GraphTest");
graph = model.Environment().newDirectedGraph("graph");
graph.newVertexPropertyInt("foo");
graph.newEdgePropertyUInt("id");
agent = model.newAgent("agent");
agent.newVariableInt("result1", 0);
agent.newVariableInt("result2", 0);
Expand All @@ -1295,3 +1297,88 @@ def test_EdgeSameSrcDest(self):
assert result_agent.getVariableInt("result1") == 1
assert result_agent.getVariableInt("result2") == 1
assert result_agent.getVariableInt("result3") == 1


CheckGraphVertexFromID_func = """
FLAMEGPU_AGENT_FUNCTION(CheckGraphVertexFromID, flamegpu::MessageNone, flamegpu::MessageNone) {
auto graph = FLAMEGPU->environment.getDirectedGraph("graph");
const unsigned int vertex_index = graph.getVertexIndex(FLAMEGPU->getIndex() + 1);
const unsigned int vertex_val = graph.getVertexProperty<int>("foo", vertex_index);
if (vertex_val == FLAMEGPU->getIndex()) {
FLAMEGPU->setVariable<int>("result1", 1);
}
return flamegpu::ALIVE;
}
"""

def test_VertexFromID(self):
# Graph can have no edges
model = pyflamegpu.ModelDescription("GraphTest");
graph = model.Environment().newDirectedGraph("graph");
graph.newVertexPropertyInt("foo");
graph.newEdgePropertyUInt("id");
agent = model.newAgent("agent");
agent.newVariableInt("result1", 0);
a = agent.newRTCFunction("fn2", self.CheckGraphVertexFromID_func);
# Init graph with known data
model.newLayer().addHostFunction(InitGraph_SameSrcDest());
model.newLayer().addAgentFunction(a);

# Each agent checks 1 ID
pop = pyflamegpu.AgentVector(agent, ID_AGENT_COUNT);

sim = pyflamegpu.CUDASimulation(model);
sim.setPopulationData(pop);

sim.step();

# Check results
sim.getPopulationData(pop);

for result_agent in pop:
assert result_agent.getVariableInt("result1") == 1

CheckGraphEdgeFromIDs_func = """
FLAMEGPU_AGENT_FUNCTION(CheckGraphEdgeFromIDs, flamegpu::MessageNone, flamegpu::MessageNone) {
auto graph = FLAMEGPU->environment.getDirectedGraph("graph");
const unsigned int edge_index = graph.getEdgeIndex(FLAMEGPU->getIndex(), FLAMEGPU->getIndex());
const unsigned int edge_val = graph.getEdgeProperty<unsigned int>("id", edge_index);
if (edge_val == graph.getVertexID(FLAMEGPU->getIndex()) - 1) {
FLAMEGPU->setVariable<int>("result1", 1);
}
return flamegpu::ALIVE;
}
"""

def test_EdgeFromIDs(self):
# Graph can have no edges
model = pyflamegpu.ModelDescription("GraphTest");
graph = model.Environment().newDirectedGraph("graph");
graph.newVertexPropertyInt("foo");
graph.newEdgePropertyUInt("id");
agent = model.newAgent("agent");
agent.newVariableInt("result1", 0);
a = agent.newRTCFunction("fn2", self.CheckGraphEdgeFromIDs_func);
# Init graph with known data
model.newLayer().addHostFunction(InitGraph_SameSrcDest());
model.newLayer().addAgentFunction(a);

# Each agent checks 1 ID
pop = pyflamegpu.AgentVector(agent, ID_AGENT_COUNT);

sim = pyflamegpu.CUDASimulation(model);
sim.setPopulationData(pop);

sim.step();

# Check results
sim.getPopulationData(pop);

for result_agent in pop:
assert result_agent.getVariableInt("result1") == 1
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,8 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_SameSrcDest) {
for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) {
auto vertex = vertices[i + 1];
vertex.setProperty<int>("foo", i);
edges[{i + 1, i + 1}];
auto edge = edges[{i + 1, i + 1}];
edge.setProperty<unsigned int>("id", i);
}
}
FLAMEGPU_AGENT_FUNCTION(CheckGraphSameSrcDest, MessageNone, MessageNone) {
Expand Down Expand Up @@ -1346,6 +1347,7 @@ TEST(TestEnvironmentDirectedGraph, TestEdgeSameSrcDest) {
ModelDescription model("GraphTest");
EnvironmentDirectedGraphDescription graph = model.Environment().newDirectedGraph("graph");
graph.newVertexProperty<int>("foo");
graph.newEdgeProperty<unsigned int>("id");
AgentDescription agent = model.newAgent("agent");
agent.newVariable<int>("result1", 0);
agent.newVariable<int>("result2", 0);
Expand All @@ -1372,6 +1374,86 @@ TEST(TestEnvironmentDirectedGraph, TestEdgeSameSrcDest) {
EXPECT_EQ(result_agent.getVariable<int>("result3"), 1);
}
}
FLAMEGPU_AGENT_FUNCTION(CheckGraphVertexFromID, MessageNone, MessageNone) {
auto graph = FLAMEGPU->environment.getDirectedGraph("graph");

const unsigned int vertex_index = graph.getVertexIndex(FLAMEGPU->getIndex() + 1);
const unsigned int vertex_val = graph.getVertexProperty<int>("foo", vertex_index);

if (vertex_val == FLAMEGPU->getIndex()) {
FLAMEGPU->setVariable<int>("result1", 1);
}

return flamegpu::ALIVE;
}
TEST(TestEnvironmentDirectedGraph, VertexFromID) {
// Graph can have no edges
ModelDescription model("GraphTest");
EnvironmentDirectedGraphDescription graph = model.Environment().newDirectedGraph("graph");
graph.newVertexProperty<int>("foo");
graph.newEdgeProperty<unsigned int>("id");
AgentDescription agent = model.newAgent("agent");
agent.newVariable<int>("result1", 0);
agent.newFunction("fn2", CheckGraphVertexFromID);
// Init graph with known data
model.newLayer().addHostFunction(InitGraph_SameSrcDest);
model.newLayer().addAgentFunction(CheckGraphVertexFromID);

// Each agent checks 1 ID
AgentVector pop(agent, ID_AGENT_COUNT);

CUDASimulation sim(model);
sim.setPopulationData(pop);

EXPECT_NO_THROW(sim.step());

// Check results
sim.getPopulationData(pop);

for (const auto &result_agent : pop) {
EXPECT_EQ(result_agent.getVariable<int>("result1"), 1);
}
}
FLAMEGPU_AGENT_FUNCTION(CheckGraphEdgeFromIDs, MessageNone, MessageNone) {
auto graph = FLAMEGPU->environment.getDirectedGraph("graph");

Check failure on line 1419 in tests/test_cases/runtime/environment/test_environment_directed_graph.cu

View workflow job for this annotation

GitHub Actions / cpplint (11.8, ubuntu-20.04)

Line ends in whitespace. Consider deleting these extra spaces.
const unsigned int edge_index = graph.getEdgeIndex(FLAMEGPU->getIndex(), FLAMEGPU->getIndex());
const unsigned int edge_val = graph.getEdgeProperty<unsigned int>("id", edge_index);

if (edge_val == graph.getVertexID(FLAMEGPU->getIndex()) - 1) {
FLAMEGPU->setVariable<int>("result1", 1);
}

return flamegpu::ALIVE;
}
TEST(TestEnvironmentDirectedGraph, EdgeFromIDs) {
// Graph can have no edges
ModelDescription model("GraphTest");
EnvironmentDirectedGraphDescription graph = model.Environment().newDirectedGraph("graph");
graph.newVertexProperty<int>("foo");
graph.newEdgeProperty<unsigned int>("id");
AgentDescription agent = model.newAgent("agent");
agent.newVariable<int>("result1", 0);
agent.newFunction("fn2", CheckGraphEdgeFromIDs);
// Init graph with known data
model.newLayer().addHostFunction(InitGraph_SameSrcDest);
model.newLayer().addAgentFunction(CheckGraphEdgeFromIDs);

// Each agent checks 1 ID
AgentVector pop(agent, ID_AGENT_COUNT);

CUDASimulation sim(model);
sim.setPopulationData(pop);

EXPECT_NO_THROW(sim.step());

// Check results
sim.getPopulationData(pop);

for (const auto &result_agent : pop) {
EXPECT_EQ(result_agent.getVariable<int>("result1"), 1);
}
}

#if !defined(FLAMEGPU_SEATBELTS) || FLAMEGPU_SEATBELTS
FLAMEGPU_AGENT_FUNCTION(DeviceGetVertex_SEATBELTS1, MessageNone, MessageNone) {
Expand Down

0 comments on commit d0402c7

Please sign in to comment.