From a2fd081a8ab94244ecced96277c09459756999fb Mon Sep 17 00:00:00 2001 From: Robert Chisholm Date: Tue, 10 Oct 2023 11:22:20 +0100 Subject: [PATCH] VertexMap test changes. --- .../HostEnvironmentDirectedGraph.cuh | 12 +- .../CUDAEnvironmentDirectedGraphBuffers.cu | 11 +- .../test_environment_directed_graph.cu | 258 ++++++++++-------- 3 files changed, 158 insertions(+), 123 deletions(-) diff --git a/include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh b/include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh index 24257664a..c22d199a7 100644 --- a/include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh +++ b/include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh @@ -250,7 +250,7 @@ class HostEnvironmentDirectedGraph { template T getProperty(const std::string& property_name, unsigned int element_index) const; template - std::array getVertexProperty(const std::string& property_name) const; + std::array getProperty(const std::string& property_name) const; }; }; }; @@ -421,7 +421,7 @@ void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::str // Get index const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id); size_type element_ct = 1; - const T* rtn = const_cast(directed_graph.get())->getVertexPropertyBuffer(property_name, element_ct, stream); + T* rtn = directed_graph->getVertexPropertyBuffer(property_name, element_ct, stream); rtn[vertex_index] = property_value; } template @@ -429,7 +429,7 @@ void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::str // Get index const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id); size_type element_ct = N; - const T* rtn = const_cast(directed_graph.get())->getVertexPropertyBuffer(property_name, element_ct, stream); + T* rtn = directed_graph->getVertexPropertyBuffer(property_name, element_ct, stream); if (element_index > element_ct) { THROW exception::OutOfBoundsException("Element index %u is out of range %u, in VertexMap::Vertex::setProperty()\n", element_index, element_ct); @@ -441,8 +441,8 @@ void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::str // Get index const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id); size_type element_ct = N; - const T* rtn = const_cast(directed_graph.get())->getVertexPropertyBuffer(property_name, element_ct, stream); - const std::array* rtn2 = reinterpret_cast*>(rtn); + T* rtn = directed_graph->getVertexPropertyBuffer(property_name, element_ct, stream); + std::array* rtn2 = reinterpret_cast*>(rtn); rtn2[vertex_index] = property_value; } template @@ -466,7 +466,7 @@ T HostEnvironmentDirectedGraph::VertexMap::Vertex::getProperty(const std::string return rtn[vertex_index * element_ct + element_index]; } template -std::array HostEnvironmentDirectedGraph::VertexMap::Vertex::getVertexProperty(const std::string& property_name) const { +std::array HostEnvironmentDirectedGraph::VertexMap::Vertex::getProperty(const std::string& property_name) const { // Get index const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id); size_type element_ct = N; diff --git a/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu b/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu index ebf2dfa3b..ba8019d1c 100644 --- a/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu +++ b/src/flamegpu/simulation/detail/CUDAEnvironmentDirectedGraphBuffers.cu @@ -575,6 +575,9 @@ void CUDAEnvironmentDirectedGraphBuffers::setVertexID(unsigned int vertex_index, if (vertex_index >= vertex_count) { THROW exception::OutOfBoundsException("Vertex index exceeds bounds %u >= %u, " "in CUDAEnvironmentDirectedGraphBuffers::setVertexID()\n", vertex_index, vertex_count); + } else if (vertex_id == ID_NOT_SET) { + THROW exception::IDOutOfBounds("Vertex ID of %u is not valid, " + "in CUDAEnvironmentDirectedGraphBuffers::setVertexID()\n", ID_NOT_SET); } // Purge old vertex ID from host map auto& vb = vertex_buffers.at(ID_VARIABLE_NAME); @@ -601,7 +604,7 @@ void CUDAEnvironmentDirectedGraphBuffers::setVertexID(unsigned int vertex_index, } unsigned int CUDAEnvironmentDirectedGraphBuffers::getVertexIndex(id_t vertex_id) const { const auto find = h_vertex_index_map.find(vertex_id); - if (find != h_vertex_index_map.end()) { + if (find == h_vertex_index_map.end()) { THROW exception::InvalidID("No vertex found with ID %u, in CUDAEnvironmentDirectedGraphBuffers::getVertexIndex()\n", vertex_id); } return find->second; @@ -649,11 +652,15 @@ unsigned int CUDAEnvironmentDirectedGraphBuffers::getEdgeIndex(id_t src_vertex_i } unsigned int CUDAEnvironmentDirectedGraphBuffers::createIfNotExistVertex(id_t vertex_id, const cudaStream_t stream) { + if (vertex_id == ID_NOT_SET) { + THROW exception::IDOutOfBounds("Vertex ID of %u is not valid, " + "in CUDAEnvironmentDirectedGraphBuffers::setVertexID()\n", ID_NOT_SET); + } const auto it = h_vertex_index_map.find(vertex_id); if (it != h_vertex_index_map.end()) { return it->second; } - if (h_vertex_index_map.size() + 1 < vertex_count) { + if (h_vertex_index_map.size() < vertex_count) { const unsigned int vertex_index = static_cast(h_vertex_index_map.size()); h_vertex_index_map.emplace(vertex_id, vertex_index); // Update vertex's ID in buffer diff --git a/tests/test_cases/runtime/environment/test_environment_directed_graph.cu b/tests/test_cases/runtime/environment/test_environment_directed_graph.cu index ad8058541..5aecab6d6 100644 --- a/tests/test_cases/runtime/environment/test_environment_directed_graph.cu +++ b/tests/test_cases/runtime/environment/test_environment_directed_graph.cu @@ -14,12 +14,13 @@ namespace test_environment_directed_graph { FLAMEGPU_HOST_FUNCTION(InitGraph) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); graph.setVertexCount(10); - for (unsigned int i = 0; i < 10; ++i) { - graph.setVertexID(i, i + 1); - graph.setVertexProperty("vertex_float", i, static_cast(i)); - graph.setVertexProperty("vertex_double2", i, 0, static_cast(i + 11)); - graph.setVertexProperty("vertex_double2", i, 1, static_cast(i + 21)); - graph.setVertexProperty("vertex_int3", i, {static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3)}); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 11; ++i) { + auto vertex = vertices[i]; + vertex.setProperty("vertex_float", static_cast(i)); + vertex.setProperty("vertex_double2", 0, static_cast(i + 11)); + vertex.setProperty("vertex_double2", 1, static_cast(i + 21)); + vertex.setProperty("vertex_int3", {static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3)}); } graph.setEdgeCount(20); // Edge source dest pairs are carefully set to ensure the defined order matches the sorted order @@ -43,12 +44,13 @@ FLAMEGPU_HOST_FUNCTION(InitGraph) { FLAMEGPU_HOST_FUNCTION(InitGraph2) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); graph.setVertexCount(10); - for (unsigned int i = 0; i < 10; ++i) { - graph.setVertexID(i, i + 1); - graph.setVertexProperty("vertex_float", i, static_cast(0)); - graph.setVertexProperty("vertex_double2", i, 0, static_cast(0)); - graph.setVertexProperty("vertex_double2", i, 1, static_cast(0)); - graph.setVertexProperty("vertex_int3", i, { static_cast(0), static_cast(0) , static_cast(0) }); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 11; ++i) { + auto vertex = vertices[i]; + vertex.setProperty("vertex_float", static_cast(0)); + vertex.setProperty("vertex_double2", 0, static_cast(0)); + vertex.setProperty("vertex_double2", 1, static_cast(0)); + vertex.setProperty("vertex_int3", { static_cast(0), static_cast(0) , static_cast(0) }); } graph.setEdgeCount(20); for (unsigned int i = 0; i < 10; ++i) { @@ -70,12 +72,13 @@ FLAMEGPU_HOST_FUNCTION(InitGraph2) { FLAMEGPU_HOST_FUNCTION(InitGraph3) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); graph.setVertexCount(30); - for (unsigned int i = 0; i < 30; ++i) { - graph.setVertexID(i, i + 1); - graph.setVertexProperty("vertex_float", i, static_cast(i)); - graph.setVertexProperty("vertex_double2", i, 0, static_cast(i + 11)); - graph.setVertexProperty("vertex_double2", i, 1, static_cast(i + 21)); - graph.setVertexProperty("vertex_int3", i, { static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3) }); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 31; ++i) { + auto vertex = vertices[i]; + vertex.setProperty("vertex_float", static_cast(i)); + vertex.setProperty("vertex_double2", 0, static_cast(i + 11)); + vertex.setProperty("vertex_double2", 1, static_cast(i + 21)); + vertex.setProperty("vertex_int3", { static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3) }); } graph.setEdgeCount(60); // Edge source dest pairs are carefully set to ensure the defined order matches the sorted order @@ -98,12 +101,13 @@ FLAMEGPU_HOST_FUNCTION(InitGraph3) { // Set graph, it assumes vertice/edge counts are correct FLAMEGPU_HOST_FUNCTION(SetGraph) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); - for (unsigned int i = 0; i < 10; ++i) { - graph.setVertexID(i, i + 1); - graph.setVertexProperty("vertex_float", i, static_cast(i)); - graph.setVertexProperty("vertex_double2", i, 0, static_cast(i + 11)); - graph.setVertexProperty("vertex_double2", i, 1, static_cast(i + 21)); - graph.setVertexProperty("vertex_int3", i, { static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3) }); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 11; ++i) { + auto vertex = vertices[i]; + vertex.setProperty("vertex_float", static_cast(i)); + vertex.setProperty("vertex_double2", 0, static_cast(i + 11)); + vertex.setProperty("vertex_double2", 1, static_cast(i + 21)); + vertex.setProperty("vertex_int3", { static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3) }); } // Edge source dest pairs are carefully set to ensure the defined order matches the sorted order // Furthermore no edges have a matching source/dest (assuming vcount=10, ecount=20) @@ -126,13 +130,14 @@ FLAMEGPU_HOST_FUNCTION(HostCheckSetGraph) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); // Vertices EXPECT_EQ(graph.getVertexCount(), 10u); - for (unsigned int i = 0; i < 10; ++i) { - EXPECT_EQ(graph.getVertexID(i), i + 1); - EXPECT_EQ(graph.getVertexProperty("vertex_float", i), static_cast(i)); - EXPECT_EQ((graph.getVertexProperty("vertex_double2", i, 0)), static_cast(i + 11)); - EXPECT_EQ(graph.getVertexProperty("vertex_double2", i, 1), static_cast(i + 21)); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 11; ++i) { + auto vertex = vertices[i]; + EXPECT_EQ(vertex.getProperty("vertex_float"), static_cast(i)); + EXPECT_EQ((vertex.getProperty("vertex_double2", 0)), static_cast(i + 11)); + EXPECT_EQ(vertex.getProperty("vertex_double2", 1), static_cast(i + 21)); std::array result = { static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3) }; - EXPECT_EQ((graph.getVertexProperty("vertex_int3", i)), result); + EXPECT_EQ((vertex.getProperty("vertex_int3")), result); } // Edges EXPECT_EQ(graph.getEdgeCount(), 20u); @@ -161,13 +166,14 @@ FLAMEGPU_HOST_FUNCTION(HostCheckGraph) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); // Vertices EXPECT_EQ(graph.getVertexCount(), 10u); - for (unsigned int i = 0; i < 10; ++i) { - EXPECT_EQ(graph.getVertexID(i), i + 1); - EXPECT_EQ(graph.getVertexProperty("vertex_float", i), static_cast(i)); - EXPECT_EQ((graph.getVertexProperty("vertex_double2", i, 0)), static_cast(i + 11)); - EXPECT_EQ(graph.getVertexProperty("vertex_double2", i, 1), static_cast(i + 21)); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 11; ++i) { + auto vertex = vertices[i]; + EXPECT_EQ(vertex.getProperty("vertex_float"), static_cast(i)); + EXPECT_EQ((vertex.getProperty("vertex_double2", 0)), static_cast(i + 11)); + EXPECT_EQ(vertex.getProperty("vertex_double2", 1), static_cast(i + 21)); std::array result = { static_cast(i + 1), static_cast(i + 2) , static_cast(i + 3) }; - EXPECT_EQ((graph.getVertexProperty("vertex_int3", i)), result); + EXPECT_EQ((vertex.getProperty("vertex_int3")), result); } EXPECT_EQ(graph.getEdgeCount(), 20u); for (unsigned int i = 0; i < 10; ++i) { @@ -194,13 +200,14 @@ FLAMEGPU_HOST_FUNCTION(HostCheckGraph3) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); // Vertices EXPECT_EQ(graph.getVertexCount(), 30u); - for (unsigned int i = 0; i < 30; ++i) { - EXPECT_EQ(graph.getVertexID(i), i + 1); - EXPECT_EQ(graph.getVertexProperty("vertex_float", i), static_cast(i)); - EXPECT_EQ((graph.getVertexProperty("vertex_double2", i, 0)), static_cast(i + 11)); - EXPECT_EQ(graph.getVertexProperty("vertex_double2", i, 1), static_cast(i + 21)); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 31; ++i) { + auto vertex = vertices[i]; + EXPECT_EQ(vertex.getProperty("vertex_float"), static_cast(i)); + EXPECT_EQ((vertex.getProperty("vertex_double2", 0)), static_cast(i + 11)); + EXPECT_EQ(vertex.getProperty("vertex_double2", 1), static_cast(i + 21)); std::array result = { static_cast(i + 1), static_cast(i + 2), static_cast(i + 3) }; - EXPECT_EQ((graph.getVertexProperty("vertex_int3", i)), result); + EXPECT_EQ((vertex.getProperty("vertex_int3")), result); } // Edges EXPECT_EQ(graph.getEdgeCount(), 60u); @@ -279,79 +286,80 @@ FLAMEGPU_HOST_FUNCTION(HostException) { EXPECT_THROW(FLAMEGPU->environment.getDirectedGraph("does not exist"), exception::InvalidGraphName); HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); - EXPECT_THROW(graph.getVertexID(0), exception::OutOfBoundsException); + + auto vertices = graph.vertices(); + + EXPECT_THROW(vertices[1], exception::OutOfBoundsException); EXPECT_THROW(graph.getEdgeSource(0), exception::OutOfBoundsException); - EXPECT_NO_THROW(graph.setVertexCount(10)); + EXPECT_NO_THROW(graph.setVertexCount(1)); EXPECT_NO_THROW(graph.setEdgeCount(10)); + auto vertex = vertices[1]; + // Name - EXPECT_THROW(graph.setVertexProperty("does not exist", 0, static_cast(0)), exception::InvalidGraphProperty); - EXPECT_THROW((graph.setVertexProperty("does not exist", 0, 0, static_cast(0))), exception::InvalidGraphProperty); - EXPECT_THROW(graph.setVertexProperty("does not exist", 0, 0, static_cast(0)), exception::InvalidGraphProperty); - EXPECT_THROW((graph.setVertexProperty("does not exist", 0, { })), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.setProperty("does not exist", static_cast(0)), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.setProperty("does not exist", 0, static_cast(0))), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.setProperty("does not exist", 0, static_cast(0)), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.setProperty("does not exist", { })), exception::InvalidGraphProperty); EXPECT_THROW(graph.setEdgeProperty("does not exist", 0, static_cast(0)), exception::InvalidGraphProperty); EXPECT_THROW((graph.setEdgeProperty("does not exist", 0, 0, static_cast(0))), exception::InvalidGraphProperty); EXPECT_THROW(graph.setEdgeProperty("does not exist", 0, 0, static_cast(0)), exception::InvalidGraphProperty); EXPECT_THROW((graph.setEdgeProperty("does not exist", 0, { })), exception::InvalidGraphProperty); - EXPECT_THROW(graph.getVertexProperty("does not exist", 0), exception::InvalidGraphProperty); - EXPECT_THROW((graph.getVertexProperty("does not exist", 0, 0)), exception::InvalidGraphProperty); - EXPECT_THROW(graph.getVertexProperty("does not exist", 0, 0), exception::InvalidGraphProperty); - EXPECT_THROW((graph.getVertexProperty("does not exist", 0)), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.getProperty("does not exist"), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.getProperty("does not exist", 0)), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.getProperty("does not exist", 0), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.getProperty("does not exist")), exception::InvalidGraphProperty); EXPECT_THROW(graph.getEdgeProperty("does not exist", 0), exception::InvalidGraphProperty); EXPECT_THROW((graph.getEdgeProperty("does not exist", 0, 0)), exception::InvalidGraphProperty); EXPECT_THROW(graph.getEdgeProperty("does not exist", 0, 0), exception::InvalidGraphProperty); EXPECT_THROW((graph.getEdgeProperty("does not exist", 0)), exception::InvalidGraphProperty); // Type - EXPECT_THROW(graph.setVertexProperty("vertex_float", 0, static_cast(0)), exception::InvalidGraphProperty); - EXPECT_THROW((graph.setVertexProperty("vertex_double2", 0, 0, static_cast(0))), exception::InvalidGraphProperty); - EXPECT_THROW(graph.setVertexProperty("vertex_double2", 0, 0, static_cast(0)), exception::InvalidGraphProperty); - EXPECT_THROW((graph.setVertexProperty("vertex_int3", 0, { })), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.setProperty("vertex_float", static_cast(0)), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.setProperty("vertex_double2", 0, static_cast(0))), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.setProperty("vertex_double2", 0, static_cast(0)), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.setProperty("vertex_int3", { })), exception::InvalidGraphProperty); EXPECT_THROW(graph.setEdgeProperty("edge_int", 0, static_cast(0)), exception::InvalidGraphProperty); EXPECT_THROW((graph.setEdgeProperty("edge_double2", 0, 0, static_cast(0))), exception::InvalidGraphProperty); EXPECT_THROW(graph.setEdgeProperty("edge_double2", 0, 0, static_cast(0)), exception::InvalidGraphProperty); EXPECT_THROW((graph.setEdgeProperty("edge_float3", 0, { })), exception::InvalidGraphProperty); - EXPECT_THROW(graph.getVertexProperty("vertex_float", 0), exception::InvalidGraphProperty); - EXPECT_THROW((graph.getVertexProperty("vertex_double2", 0, 0)), exception::InvalidGraphProperty); - EXPECT_THROW(graph.getVertexProperty("vertex_double2", 0, 0), exception::InvalidGraphProperty); - EXPECT_THROW((graph.getVertexProperty("vertex_int3", 0)), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.getProperty("vertex_float"), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.getProperty("vertex_double2", 0)), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.getProperty("vertex_double2", 0), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.getProperty("vertex_int3")), exception::InvalidGraphProperty); EXPECT_THROW(graph.getEdgeProperty("edge_int", 0), exception::InvalidGraphProperty); EXPECT_THROW((graph.getEdgeProperty("edge_double2", 0, 0)), exception::InvalidGraphProperty); EXPECT_THROW(graph.getEdgeProperty("edge_double2", 0, 0), exception::InvalidGraphProperty); EXPECT_THROW((graph.getEdgeProperty("edge_float3", 0)), exception::InvalidGraphProperty); // Length - EXPECT_THROW(graph.setVertexProperty("vertex_int3", 0, static_cast(0)), exception::InvalidGraphProperty); - EXPECT_THROW((graph.setVertexProperty("vertex_int3", 0, 0, static_cast(0))), exception::InvalidGraphProperty); - EXPECT_THROW((graph.setVertexProperty("vertex_int3", 0, { })), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.setProperty("vertex_int3", static_cast(0)), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.setProperty("vertex_int3", 0, static_cast(0))), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.setProperty("vertex_int3", { })), exception::InvalidGraphProperty); EXPECT_THROW(graph.setEdgeProperty("edge_float3", 0, static_cast(0)), exception::InvalidGraphProperty); EXPECT_THROW((graph.setEdgeProperty("edge_float3", 0, 0, static_cast(0))), exception::InvalidGraphProperty); EXPECT_THROW((graph.setEdgeProperty("edge_float3", 0, { })), exception::InvalidGraphProperty); - EXPECT_THROW(graph.getVertexProperty("vertex_int3", 0), exception::InvalidGraphProperty); - EXPECT_THROW((graph.getVertexProperty("vertex_int3", 0, 0)), exception::InvalidGraphProperty); - EXPECT_THROW((graph.getVertexProperty("vertex_int3", 0)), exception::InvalidGraphProperty); + EXPECT_THROW(vertex.getProperty("vertex_int3"), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.getProperty("vertex_int3", 0)), exception::InvalidGraphProperty); + EXPECT_THROW((vertex.getProperty("vertex_int3")), exception::InvalidGraphProperty); EXPECT_THROW(graph.getEdgeProperty("edge_float3", 0), exception::InvalidGraphProperty); EXPECT_THROW((graph.getEdgeProperty("edge_float3", 0, 0)), exception::InvalidGraphProperty); EXPECT_THROW((graph.getEdgeProperty("edge_float3", 0)), exception::InvalidGraphProperty); // Array Index - EXPECT_THROW(graph.setVertexProperty("vertex_double2", 0, 3, static_cast(0)), exception::OutOfBoundsException); + EXPECT_THROW(vertex.setProperty("vertex_double2", 3, static_cast(0)), exception::OutOfBoundsException); EXPECT_THROW(graph.setEdgeProperty("edge_double2", 0, 3, static_cast(0)), exception::OutOfBoundsException); - EXPECT_THROW(graph.getVertexProperty("vertex_double2", 0, 3), exception::OutOfBoundsException); + EXPECT_THROW(vertex.getProperty("vertex_double2", 3), exception::OutOfBoundsException); EXPECT_THROW(graph.getEdgeProperty("edge_double2", 0, 3), exception::OutOfBoundsException); - EXPECT_THROW((graph.setVertexProperty("vertex_double2", 0, 3, static_cast(0))), exception::OutOfBoundsException); + EXPECT_THROW((vertex.setProperty("vertex_double2", 3, static_cast(0))), exception::OutOfBoundsException); EXPECT_THROW((graph.setEdgeProperty("edge_double2", 0, 3, static_cast(0))), exception::OutOfBoundsException); - EXPECT_THROW((graph.getVertexProperty("vertex_double2", 0, 3)), exception::OutOfBoundsException); + EXPECT_THROW((vertex.getProperty("vertex_double2", 3)), exception::OutOfBoundsException); EXPECT_THROW((graph.getEdgeProperty("edge_double2", 0, 3)), exception::OutOfBoundsException); // Vertex/Edge Index - EXPECT_THROW(graph.setVertexID(11, 3), exception::OutOfBoundsException); - EXPECT_THROW(graph.setVertexID(0, 0), exception::IDOutOfBounds); - EXPECT_THROW(graph.setVertexProperty("vertex_float", 11, static_cast(0)), exception::OutOfBoundsException); - EXPECT_THROW((graph.setVertexProperty("vertex_double2", 11, 0, static_cast(0))), exception::OutOfBoundsException); - EXPECT_THROW(graph.setVertexProperty("vertex_double2", 11, 0, static_cast(0)), exception::OutOfBoundsException); - EXPECT_THROW((graph.setVertexProperty("vertex_int3", 11, { })), exception::OutOfBoundsException); + EXPECT_THROW(vertices[0], exception::IDOutOfBounds); + EXPECT_THROW(vertex.setID(0), exception::IDOutOfBounds); EXPECT_THROW(graph.setEdgeSourceDestination(11, 1, 1), exception::OutOfBoundsException); EXPECT_THROW(graph.setEdgeSourceDestination(0, 1, ID_NOT_SET), exception::IDOutOfBounds); EXPECT_THROW(graph.setEdgeSourceDestination(0, ID_NOT_SET, 1), exception::IDOutOfBounds); @@ -359,11 +367,6 @@ FLAMEGPU_HOST_FUNCTION(HostException) { EXPECT_THROW((graph.setEdgeProperty("edge_double2", 11, 0, static_cast(0))), exception::OutOfBoundsException); EXPECT_THROW(graph.setEdgeProperty("edge_double2", 11, 0, static_cast(0)), exception::OutOfBoundsException); EXPECT_THROW((graph.setEdgeProperty("edge_float3", 11, { })), exception::OutOfBoundsException); - EXPECT_THROW(graph.getVertexID(11), exception::OutOfBoundsException); - EXPECT_THROW(graph.getVertexProperty("vertex_float", 11), exception::OutOfBoundsException); - EXPECT_THROW((graph.getVertexProperty("vertex_double2", 11, 0)), exception::OutOfBoundsException); - EXPECT_THROW(graph.getVertexProperty("vertex_double2", 11, 0), exception::OutOfBoundsException); - EXPECT_THROW((graph.getVertexProperty("vertex_int3", 11)), exception::OutOfBoundsException); EXPECT_THROW(graph.getEdgeSource(11), exception::OutOfBoundsException); EXPECT_THROW(graph.getEdgeDestination(11), exception::OutOfBoundsException); EXPECT_THROW(graph.getEdgeSourceDestination(11), exception::OutOfBoundsException); @@ -371,6 +374,9 @@ FLAMEGPU_HOST_FUNCTION(HostException) { EXPECT_THROW((graph.getEdgeProperty("edge_double2", 11, 0)), exception::OutOfBoundsException); EXPECT_THROW(graph.getEdgeProperty("edge_double2", 11, 0), exception::OutOfBoundsException); EXPECT_THROW((graph.getEdgeProperty("edge_float3", 11)), exception::OutOfBoundsException); + + // Out of bounds + EXPECT_THROW(vertices[2], exception::OutOfBoundsException); } TEST(TestEnvironmentDirectedGraph, TestHostException) { ModelDescription model("GraphTest"); @@ -435,9 +441,9 @@ FLAMEGPU_HOST_FUNCTION(HostDeviceCheckGraph) { DeviceAgentVector agent = FLAMEGPU->agent("agent").getPopulationData(); for (unsigned int i = 0; i < 20; ++i) { if (i < 10) { - EXPECT_EQ(agent[i].getVariable("vertex_id"), static_cast(i + 1)); - EXPECT_EQ(agent[i].getVariable("vertex_float"), static_cast(i)); - std::array result = { static_cast(i + 11), static_cast(i + 21) }; + EXPECT_EQ(agent[i].getVariable("vertex_id"), static_cast((i + 1))); + EXPECT_EQ(agent[i].getVariable("vertex_float"), static_cast(i + 1)); + std::array result = { static_cast(i + 12), static_cast(i + 22) }; EXPECT_EQ((agent[i].getVariable("vertex_double2")), result); EXPECT_EQ(agent[i].getVariable("edge_dest"), static_cast((i + 2) % 10) + 1); } else { @@ -455,8 +461,8 @@ FLAMEGPU_HOST_FUNCTION(HostDeviceCheckGraph3) { for (unsigned int i = 0; i < 60; ++i) { if (i < 30) { EXPECT_EQ(agent[i].getVariable("vertex_id"), static_cast(i + 1)); - EXPECT_EQ(agent[i].getVariable("vertex_float"), static_cast(i)); - std::array result = { static_cast(i + 11), static_cast(i + 21) }; + EXPECT_EQ(agent[i].getVariable("vertex_float"), static_cast(i + 1)); + std::array result = { static_cast(i + 12), static_cast(i + 22) }; EXPECT_EQ((agent[i].getVariable("vertex_double2")), result); EXPECT_EQ(agent[i].getVariable("edge_dest"), static_cast((i + 2) % 30) + 1); } else { @@ -741,8 +747,9 @@ FLAMEGPU_HOST_FUNCTION(HostTestEdgesOut) { graph.setVertexCount(5); graph.setEdgeCount(15); int k = 0; + auto vertices = graph.vertices(); for (int i = 0; i < 5; ++i) { - graph.setVertexID(i, i + 1); + vertices[i + 1]; for (int j = 0; j <= i; ++j) { graph.setEdgeProperty("src_copy", k, j + 1); graph.setEdgeSourceDestination(k++, j + 1, i + 1); @@ -770,8 +777,9 @@ FLAMEGPU_HOST_FUNCTION(HostTestEdgesIn) { graph.setVertexCount(5); graph.setEdgeCount(15); int k = 0; + auto vertices = graph.vertices(); for (int i = 0; i < 5; ++i) { - graph.setVertexID(i, i + 1); + vertices[i + 1]; for (int j = 0; j <= i; ++j) { graph.setEdgeProperty("dest_copy", k, j + 1); graph.setEdgeSourceDestination(k++, i + 1, j + 1); @@ -804,21 +812,32 @@ FLAMEGPU_HOST_FUNCTION(LoadCheckGraph3) { // Load graph.importGraph("graph.json"); // Check - for (unsigned int i = 0; i < 60; ++i) { - if (i < 30) { - EXPECT_EQ(graph.getVertexID(i), static_cast(i + 1)); - EXPECT_EQ(graph.getVertexProperty("vertex_float", i), static_cast(i)); + auto vertices = graph.vertices(); + for (unsigned int i = 1; i < 31; ++i) { + auto vertex = vertices[i]; + EXPECT_EQ(vertex.getProperty("vertex_float"), static_cast(i)); + EXPECT_EQ((vertex.getProperty("vertex_double2", 0)), static_cast(i + 11)); + EXPECT_EQ(vertex.getProperty("vertex_double2", 1), static_cast(i + 21)); + std::array result = { static_cast(i + 1), static_cast(i + 2), static_cast(i + 3) }; + EXPECT_EQ((vertex.getProperty("vertex_int3")), result); + } + + for (unsigned int i = 1; i < 61; ++i) { + if (i < 31) { + auto vertex = vertices[i]; + EXPECT_EQ(vertex.getID(), static_cast(i)); + EXPECT_EQ(vertex.getProperty("vertex_float"), static_cast(i)); std::array result = { static_cast(i + 11), static_cast(i + 21) }; - EXPECT_EQ((graph.getVertexProperty)("vertex_double2", i), result); - EXPECT_EQ(graph.getEdgeDestination(i), static_cast((i + 2) % 30) + 1); + EXPECT_EQ((vertex.getProperty)("vertex_double2"), result); + EXPECT_EQ(graph.getEdgeDestination(i-1), static_cast(((i-1) + 2) % 30) + 1); } else { - EXPECT_EQ(graph.getEdgeDestination(i), static_cast((i + 18) % 30) + 1); + EXPECT_EQ(graph.getEdgeDestination(i-1), static_cast(((i-1) + 18) % 30) + 1); } // Edges - EXPECT_EQ(graph.getEdgeSource(i), static_cast(i / 2) + 1); - EXPECT_EQ(graph.getEdgeProperty("edge_int", i), static_cast(i + 70)); - std::array result = { static_cast(i + 61), static_cast(i + 51) }; - EXPECT_EQ((graph.getEdgeProperty)("edge_double2", i), result); + EXPECT_EQ(graph.getEdgeSource(i-1), static_cast((i-1) / 2) + 1); + EXPECT_EQ(graph.getEdgeProperty("edge_int", i-1), static_cast((i-1) + 70)); + std::array result = { static_cast((i-1) + 61), static_cast((i-1) + 51) }; + EXPECT_EQ((graph.getEdgeProperty)("edge_double2", i-1), result); } // Cleanup std::filesystem::remove("graph.json"); @@ -938,10 +957,11 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_ContiguousIDs) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { - graph.setVertexID(i, OFFSET + i); - graph.setVertexProperty("vertex_index", i, i); - graph.setVertexProperty("vertex_ID", i, OFFSET + i); + auto vertex = vertices[OFFSET + i]; + vertex.setProperty("vertex_index", i); + vertex.setProperty("vertex_ID", OFFSET + i); // Test does not care about edges, but add some some so that it generates properly graph.setEdgeSourceDestination(i, OFFSET + i, OFFSET + ((i + 5) * 3) % ID_AGENT_COUNT); } @@ -1003,11 +1023,12 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_NonContiguousIDs) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { const unsigned int my_id = OFFSET + i + ((i * ID_GAP2) - ((i * ID_GAP1) % ID_GAP2)); - graph.setVertexID(i, my_id); - graph.setVertexProperty("vertex_index", i, i); - graph.setVertexProperty("vertex_ID", i, my_id); + auto vertex = vertices[my_id]; + vertex.setProperty("vertex_index", i); + vertex.setProperty("vertex_ID", my_id); // Test does not care about edges, but add some some so that it generates properly const unsigned int j = ((i + 5) * 3) % ID_AGENT_COUNT; const unsigned int my_dest_id = OFFSET + j + ((j * ID_GAP2) - ((j * ID_GAP1) % ID_GAP2)); @@ -1072,9 +1093,10 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_MissingIDs1) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT/2; ++i) { const unsigned int my_id = OFFSET + i*2; - graph.setVertexID(i, my_id); + auto vertex = vertices[my_id]; // Test does not care about edges, but add some some so that it generates properly const unsigned int j = i + 1 % (ID_AGENT_COUNT/2); graph.setEdgeSourceDestination(i, my_id, OFFSET + j * 2); @@ -1102,14 +1124,15 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_MissingIDs2) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT/2; ++i) { const unsigned int my_id = OFFSET + i*5; - graph.setVertexID(i, my_id); + auto vertex = vertices[my_id]; // Test does not care about edges, but add some some so that it generates properly const unsigned int j = i + 1 % (ID_AGENT_COUNT/2); graph.setEdgeSourceDestination(i, my_id, OFFSET + j * 5); } - graph.setVertexID(0, 1); + vertices[OFFSET].setID(1); } TEST(TestEnvironmentDirectedGraph, TestAllVerticesRequireID2) { // Assign vertices an ID and check they are accessible correctly via map @@ -1135,9 +1158,10 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_InvalidEdgeSource) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { const unsigned int my_id = OFFSET + i*5; - graph.setVertexID(i, my_id); + auto vertex = vertices[my_id]; const unsigned int j = i + 1 % (ID_AGENT_COUNT/2); graph.setEdgeSourceDestination(i, my_id, OFFSET + j * 5); } @@ -1149,9 +1173,10 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_InvalidEdgeDest) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { const unsigned int my_id = OFFSET + i*5; - graph.setVertexID(i, my_id); + auto vertex = vertices[my_id]; const unsigned int j = i + 1 % (ID_AGENT_COUNT/2); graph.setEdgeSourceDestination(i, my_id, OFFSET + j * 5); } @@ -1197,9 +1222,10 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_NoEdges) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(0); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { - graph.setVertexID(i, i + 1); - graph.setVertexProperty("foo", i, i); + auto vertex = vertices[i + 1]; + vertex.setProperty("foo", i); } } FLAMEGPU_AGENT_FUNCTION(CheckGraphNoEdges, MessageNone, MessageNone) { @@ -1249,9 +1275,10 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_SameSrcDest) { HostEnvironmentDirectedGraph graph = FLAMEGPU->environment.getDirectedGraph("graph"); graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { - graph.setVertexID(i, i + 1); - graph.setVertexProperty("foo", i, i); + auto vertex = vertices[i + 1]; + vertex.setProperty("foo", i); graph.setEdgeSourceDestination(i, i+1, i+1); } } @@ -1541,9 +1568,10 @@ FLAMEGPU_HOST_FUNCTION(InitGraph_NonContiguousIDs_SEATBELTS) { const unsigned int OFFSET = ID_OFFSET; graph.setVertexCount(ID_AGENT_COUNT); graph.setEdgeCount(ID_AGENT_COUNT); + auto vertices = graph.vertices(); for (unsigned int i = 0; i < ID_AGENT_COUNT; ++i) { const unsigned int my_id = OFFSET + (i * 2); - graph.setVertexID(i, my_id); + auto vertex = vertices[my_id]; // Test does not care about edges, but add some some so that it generates properly const unsigned int j = OFFSET + ((i + 1) % ID_AGENT_COUNT) * 2; graph.setEdgeSourceDestination(i, my_id, j);