Skip to content

Commit

Permalink
Duplicated changes + test for vertex.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robadob committed Jan 18, 2025
1 parent 499a667 commit c685f50
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class HostEnvironmentDirectedGraph {

public:
class Vertex;
struct Iterator;
/**
* Return a view into the vertex map of the specified vertex
* If the vertex has not already been created, it will be created
Expand All @@ -136,11 +137,12 @@ class HostEnvironmentDirectedGraph {
class Vertex {
std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> directed_graph;
const cudaStream_t stream;
id_t vertex_id;
unsigned int vertex_index;
friend Vertex VertexMap::operator[](id_t);
friend struct Iterator;

public:
Vertex(std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> _directed_graph, cudaStream_t _stream, id_t _vertex_id);
Vertex(std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> _directed_graph, cudaStream_t _stream, id_t _vertex_id, bool is_index = false);
/**
* Set the id for the current vertex
* @param vertex_id The value to set the current vertex's id
Expand Down Expand Up @@ -186,6 +188,48 @@ class HostEnvironmentDirectedGraph {
std::vector<T> getPropertyArray(const std::string& property_name) const;
#endif
};

/**
* Iterator for VertexMap
* @see HostEnvironmentDirectedGraph::VertexMap::begin()
* @see HostEnvironmentDirectedGraph::VertexMap::end()
*/
struct Iterator {

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.6.0, 3.12, ON, Release, windows-2022)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.6.0, 3.12, OFF, Release, windows-2022)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.6.0, 3.12, OFF, Beltsoff, windows-2022)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.0.0, 3.12, OFF, Release, windows-2019)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (11.8.0, 3.12, ON, Release, windows-2019)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (11.8.0, 3.12, OFF, Release, windows-2019)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 197 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (11.0.3, 3.12, OFF, Release, windows-2019)

'Iterator' is multiply defined in the generated target language module. [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]
using iterator_category = std::forward_iterator_tag;
using difference_type = unsigned int;
using value_type = Vertex;
using pointer = Vertex*;
using reference = Vertex&;

Iterator(std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> _directed_graph, const cudaStream_t _stream, difference_type _index)
: directed_graph(std::move(_directed_graph))
, stream(_stream)
, vertex(directed_graph, _stream, _index, true) {}
reference operator*() const { return vertex; }
pointer operator->() const { return &vertex; }
Iterator& operator++() { vertex.vertex_index++; return *this; }
Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }

bool operator==(const Iterator& other) const { return vertex.vertex_index == other.vertex.vertex_index; }
bool operator!=(const Iterator& other) const { return vertex.vertex_index != other.vertex.vertex_index; }

Iterator begin() { return { directed_graph, stream, 0 }; }
Iterator end() { return { directed_graph, stream, directed_graph->getVertexCount() }; }

private:
std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> directed_graph;
cudaStream_t stream;
mutable Vertex vertex;
};

/**
* Returns an iterator to the first vertex in the vertex map
*/
Iterator begin() { return {directed_graph, stream, 0}; }
/**
* Returns an iterator to one beyond the last vertex in the vertex map
*/
Iterator end() { return { directed_graph, stream, directed_graph->getVertexCount() }; }
};
/**
* A map for accessing edge storage via vertex ID
Expand Down Expand Up @@ -279,6 +323,11 @@ class HostEnvironmentDirectedGraph {
#endif
};

/**
* Iterator for EdgeMap
* @see HostEnvironmentDirectedGraph::EdgeMap::begin()
* @see HostEnvironmentDirectedGraph::EdgeMap::end()
*/
struct Iterator {

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.6.0, 3.12, ON, Release, windows-2022)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.6.0, 3.12, OFF, Release, windows-2022)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.6.0, 3.12, OFF, Beltsoff, windows-2022)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (12.0.0, 3.12, OFF, Release, windows-2019)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (11.8.0, 3.12, ON, Release, windows-2019)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (11.8.0, 3.12, OFF, Release, windows-2019)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]

Check failure on line 331 in include/flamegpu/runtime/environment/HostEnvironmentDirectedGraph.cuh

View workflow job for this annotation

GitHub Actions / build (11.0.3, 3.12, OFF, Release, windows-2019)

Previous declaration of 'Iterator' [D:\a\FLAMEGPU2\FLAMEGPU2\build\swig\python\pyflamegpu_swig.vcxproj]
using iterator_category = std::forward_iterator_tag;
using difference_type = unsigned int;
Expand All @@ -295,8 +344,8 @@ class HostEnvironmentDirectedGraph {
Iterator& operator++() { edge.edge_index++; return *this; }
Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }

bool operator== (const Iterator& other) const { return edge.edge_index == other.edge.edge_index; }
bool operator!= (const Iterator& other) const { return edge.edge_index != other.edge.edge_index; }
bool operator==(const Iterator& other) const { return edge.edge_index == other.edge.edge_index; }
bool operator!=(const Iterator& other) const { return edge.edge_index != other.edge.edge_index; }

Iterator begin() { return { directed_graph, stream, 0 }; }
Iterator end() { return { directed_graph, stream, directed_graph->getEdgeCount() }; }
Expand All @@ -309,12 +358,12 @@ class HostEnvironmentDirectedGraph {

/**
* Returns an iterator to the first edge in the edge map
* @note The order of edges is not stable and is likely to change whenever the graph is rebuilt
* @note The order of edges is not stable and is likely to change when the graph is rebuilt
*/
Iterator begin() { return {directed_graph, stream, 0}; }
/**
* Returns an iterator to one beyond the last edge in the edge map
* @note The order of edges is not stable and is likely to change whenever the graph is rebuilt
* @note The order of edges is not stable and is likely to change when the graph is rebuilt
*/
Iterator end() { return { directed_graph, stream, directed_graph->getEdgeCount() }; }
};
Expand All @@ -323,8 +372,6 @@ class HostEnvironmentDirectedGraph {
#ifdef SWIG
template<typename T>
void HostEnvironmentDirectedGraph::VertexMap::Vertex::setPropertyArray(const std::string& property_name, const std::vector<T>& property_value) const {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
size_type n = property_value.size();
T* val = directed_graph->getVertexPropertyBuffer<T>(property_name, n, stream);
if (!property_value.size()) {
Expand All @@ -336,8 +383,6 @@ void HostEnvironmentDirectedGraph::VertexMap::Vertex::setPropertyArray(const std
}
template<typename T>
std::vector<T> HostEnvironmentDirectedGraph::VertexMap::Vertex::getPropertyArray(const std::string& property_name) const {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
return directed_graph->getVertexPropertyArray<T>(property_name, vertex_index, stream);
}
template<typename T>
Expand All @@ -358,16 +403,12 @@ std::vector<T> HostEnvironmentDirectedGraph::EdgeMap::Edge::getPropertyArray(con

template<typename T>
void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::string& property_name, T property_value) {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
size_type element_ct = 1;
T* rtn = directed_graph->getVertexPropertyBuffer<T>(property_name, element_ct, stream);
rtn[vertex_index] = property_value;
}
template<typename T, flamegpu::size_type N>
void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::string& property_name, flamegpu::size_type element_index, T property_value) {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
size_type element_ct = N;
T* rtn = directed_graph->getVertexPropertyBuffer<T>(property_name, element_ct, stream);
if (element_index > element_ct) {
Expand All @@ -378,25 +419,19 @@ void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::str
}
template<typename T, flamegpu::size_type N>
void HostEnvironmentDirectedGraph::VertexMap::Vertex::setProperty(const std::string& property_name, const std::array<T, N>& property_value) {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
size_type element_ct = N;
T* rtn = directed_graph->getVertexPropertyBuffer<T>(property_name, element_ct, stream);
std::array<T, N>* rtn2 = reinterpret_cast<std::array<T, N>*>(rtn);
rtn2[vertex_index] = property_value;
}
template<typename T>
T 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 = 1;
const T* rtn = const_cast<const detail::CUDAEnvironmentDirectedGraphBuffers *>(directed_graph.get())->getVertexPropertyBuffer<T>(property_name, element_ct, stream);
return rtn[vertex_index];
}
template<typename T, size_type N>
T HostEnvironmentDirectedGraph::VertexMap::Vertex::getProperty(const std::string& property_name, unsigned int element_index) const {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
size_type element_ct = N;
const T* rtn = const_cast<const detail::CUDAEnvironmentDirectedGraphBuffers*>(directed_graph.get())->getVertexPropertyBuffer<T>(property_name, element_ct, stream);
if (element_index > element_ct) {
Expand All @@ -407,8 +442,6 @@ T HostEnvironmentDirectedGraph::VertexMap::Vertex::getProperty(const std::string
}
template<typename T, size_type N>
std::array<T, N> 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;
const T* rtn = const_cast<const detail::CUDAEnvironmentDirectedGraphBuffers*>(directed_graph.get())->getVertexPropertyBuffer<T>(property_name, element_ct, stream);
const std::array<T, N>* rtn2 = reinterpret_cast<const std::array<T, N>*>(rtn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,13 @@ Vertex VertexMap::operator[](id_t vertex_id) {
return Vertex{directed_graph, stream, vertex_id};
}

Vertex::Vertex(std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> _directed_graph, const cudaStream_t _stream, id_t _vertex_id)
Vertex::Vertex(std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> _directed_graph, const cudaStream_t _stream, id_t _vertex_id, bool is_index)
: directed_graph(std::move(_directed_graph))
, stream(_stream)
, vertex_id(_vertex_id) { }
, vertex_index(is_index ? _vertex_id : directed_graph->getVertexIndex(_vertex_id)) { }
void Vertex::setID(id_t vertex_identifier) {
// Get index
const unsigned int vertex_index = directed_graph->getVertexIndex(vertex_id);
// Update ID
directed_graph->setVertexID(vertex_index, vertex_identifier, stream);
// Update local copy of ID
vertex_id = vertex_identifier;
}
id_t Vertex::getID() const {
return getProperty<id_t>(ID_VARIABLE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ void CUDAEnvironmentDirectedGraphBuffers::setVertexID(unsigned int vertex_index,
h_vertex_index_map.erase(static_cast<id_t*>(vb.h_ptr)[vertex_index]);
}

// Add new vertex ID to host map (validate it's not already in us)
// Add new vertex ID to host map (validate it's not already in use)
const auto find = h_vertex_index_map.find(vertex_id);
if (find != h_vertex_index_map.end()) {
THROW exception::IDCollision("ID collision, %u has already been assigned to vertex at index %u, "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ FLAMEGPU_HOST_FUNCTION(HostIteration) {
auto vertices = graph.vertices();
auto edges = graph.edges();

// No vertices allocated, so no iteration, but no error
for (auto a : vertices) {
FAIL();
}
// No edges allocated, so no iteration, but no error
for (auto a : edges) {
FAIL();
Expand All @@ -418,14 +422,29 @@ FLAMEGPU_HOST_FUNCTION(HostIteration) {
graph.setVertexCount(vertex_count);

// Set a few edges to test stuff
vertices[4];
vertices[5];
vertices[6];
edges[{4,5}].setProperty<int>("edge_int", 9);
vertices[4].setProperty<int>("vertex_int", 0);
vertices[5].setProperty<int>("vertex_int", 1);
vertices[6].setProperty<int>("vertex_int", 2);
edges[{4, 5}].setProperty<int>("edge_int", 9);

// Test iteration works
unsigned int it_count = 0;
for (auto edge : edges) {
for (const auto &vertex : vertices) {
if (it_count < 3) {
// Set vertices should be in order
EXPECT_EQ(vertex.getID(), it_count + 4u);
EXPECT_EQ(vertex.getProperty<int>("vertex_int"), static_cast<int>(it_count));
} else {
// Others are default
EXPECT_EQ(vertex.getID(), ID_NOT_SET);
EXPECT_EQ(vertex.getProperty<int>("vertex_int"), 21);
}
++it_count;
}
EXPECT_EQ(it_count, vertex_count);

it_count = 0;
for (const auto &edge : edges) {
if (it_count == 0) {
// First edge is the one we set
EXPECT_EQ(edge.getDestinationVertexID(), 5u);
Expand All @@ -439,6 +458,7 @@ FLAMEGPU_HOST_FUNCTION(HostIteration) {
}
++it_count;
}
EXPECT_EQ(it_count, edge_count);

// Allocate remaining edges to avoid exception from incomplete graph
vertices[7];
Expand Down

0 comments on commit c685f50

Please sign in to comment.