Skip to content

Commit

Permalink
Environment directed graph visualisation
Browse files Browse the repository at this point in the history
Visualisation will update if the graph is changed during the simulation.
Requires updates to the visualiser too.
  • Loading branch information
Robadob committed Oct 19, 2024
1 parent ed7d1d7 commit 107bb63
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
#include "flamegpu/simulation/detail/CUDAErrorChecking.cuh"
#include "flamegpu/detail/type_decode.h"
#include "flamegpu/util/StringPair.h"

namespace flamegpu {
#ifdef FLAMEGPU_VISUALISATION
namespace visualiser {
struct ModelVisData;
}
#endif
namespace detail {
class CUDAScatter;
namespace curve {
class HostCurve;
class CurveRTCHost;
}

/**
* This represents the equivalent of CUDAAgent, CUDAMessage for EnvironmentDirectedGraph
* As the graph cannot be modified on the device, the host buffers can be assumed to always holds the truth
Expand Down Expand Up @@ -66,6 +69,12 @@ class CUDAEnvironmentDirectedGraphBuffers {
std::map<std::string, Buffer> edge_buffers;
std::list<std::weak_ptr<detail::curve::HostCurve>> curve_instances;
std::list<std::weak_ptr<detail::curve::CurveRTCHost>> rtc_curve_instances;
#ifdef FLAMEGPU_VISUALISATION
/**
* Empty if getVisualisation() hasn't been called
*/
mutable std::weak_ptr<visualiser::ModelVisData> visualisation;
#endif
size_type vertex_count;
size_type edge_count;
bool requires_rebuild;
Expand Down Expand Up @@ -267,6 +276,11 @@ class CUDAEnvironmentDirectedGraphBuffers {
* @throws exception::InvalidID If the ID is not in use
*/
unsigned int getEdgeIndex(id_t src_vertex_id, id_t dest_vertex_id) const;
#ifdef FLAMEGPU_VISUALISATION
void setVisualisation(std::shared_ptr<visualiser::ModelVisData> &_visualisation) const {
this->visualisation = _visualisation;
}
#endif
};


Expand Down
73 changes: 73 additions & 0 deletions include/flamegpu/visualiser/EnvironmentGraphVis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef INCLUDE_FLAMEGPU_VISUALISER_ENVIRONMENTGRAPHVIS_H_
#define INCLUDE_FLAMEGPU_VISUALISER_ENVIRONMENTGRAPHVIS_H_
#ifdef FLAMEGPU_VISUALISATION

#include <memory>
#include <string>

#include "flamegpu/visualiser/color/Color.h"

namespace flamegpu {
struct EnvironmentDirectedGraphData;
namespace detail {
class CUDAEnvironmentDirectedGraphBuffers;
} // namespace detail
namespace visualiser {
struct LineConfig;
struct EnvironmentGraphVisData {
explicit EnvironmentGraphVisData(std::shared_ptr <EnvironmentDirectedGraphData> _graphData, std::shared_ptr<LineConfig>_lines);
void constructGraph(const std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers> &graph);
std::string x_varName = "";
std::string y_varName = "";
std::string z_varName = "";
std::string xy_varName = "";
std::string xyz_varName = "";
Color color;
const std::shared_ptr<EnvironmentDirectedGraphData> graphData;
const std::shared_ptr<LineConfig> lines;
};
class EnvironmentGraphVis {
public:
explicit EnvironmentGraphVis(std::shared_ptr<EnvironmentGraphVisData> data);

/**
* Set the name of the variable representing the agents x/y/z location coordinates
* @param var_name Name of the agent variable
* @note unnecessary if the variables are named "x", "y", "z" respectively
* @note Implicitly calls clearXYProperty(), clearXYZProperty()
* @throws InvalidEnvProperty If the variable is not type float[1]
*/
void setXProperty(const std::string &var_name);
void setYProperty(const std::string &var_name);
void setZProperty(const std::string &var_name);
/**
* Set the name of the array variable (length 2) representing the agents x/y location coordinates
* @param var_name Name of the agent variable
* @note Implicitly calls clearXProperty(), clearYProperty(), clearZProperty(),clearXYZProperty()
* @throws InvalidEnvProperty If the variable is not type float[2]
*/
void setXYProperty(const std::string &var_name);
/**
* Set the name of the array variable (length 3) representing the agents x/y/z location coordinates
* @param var_name Name of the agent variable
* @note Implicitly calls clearXProperty(), clearYProperty(), clearZProperty(),clearXYProperty()
* @throws InvalidEnvProperty If the variable is not type float[3]
*/
void setXYZProperty(const std::string &var_name);
/**
* Set the colour the graph will be rendered
* @param cf The colour to be used, default white
*/
void setColor(const Color& cf);

private:
/**
* Pointer to data struct
*/
std::shared_ptr<EnvironmentGraphVisData> data;
};
} // namespace visualiser
} // namespace flamegpu

#endif // FLAMEGPU_VISUALISATION
#endif // INCLUDE_FLAMEGPU_VISUALISER_ENVIRONMENTGRAPHVIS_H_
4 changes: 4 additions & 0 deletions include/flamegpu/visualiser/LineVis.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class LineVis {
* @note Y is considered the vertical axis
*/
void addVertex(float x, float y, float z = 0.0f);
/**
* Remove all sketch data to start drawing a replacement
*/
void clear();

private:
/**
Expand Down
30 changes: 30 additions & 0 deletions include/flamegpu/visualiser/ModelVis.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

// @todo - All vis headers should live in the vis repo.
#include "flamegpu/visualiser/AgentVis.h"
#include "flamegpu/visualiser/EnvironmentGraphVis.h"
#include "flamegpu/visualiser/StaticModelVis.h"
#include "flamegpu/visualiser/LineVis.h"
#include "flamegpu/visualiser/PanelVis.h"
Expand All @@ -33,6 +34,12 @@ struct ModelVisData {
* > On resize, also update textures
*/
explicit ModelVisData(const CUDASimulation& model/*TBD*/);
/**
* Pass the vis shared pointer to directed graphs being visualised so they can trigger updates
* @param vis pointer to this
* @param map the map of environment directed graph cuda buffers
*/
void hookVis(std::shared_ptr<visualiser::ModelVisData> &vis, std::unordered_map<std::string, std::shared_ptr<detail::CUDAEnvironmentDirectedGraphBuffers>> &map);
/**
* Main struct of visualisation configuration options for the model
*/
Expand All @@ -46,6 +53,10 @@ struct ModelVisData {
* Per agent, visualisation configuration options
*/
std::unordered_map<std::string, std::shared_ptr<AgentVisData>> agents;
/**
* Per environment direct graph, visualisation configuration options
*/
std::unordered_map<std::string, std::shared_ptr<EnvironmentGraphVisData>> graphs;
/**
* Reference back to the model to be visualised
*/
Expand Down Expand Up @@ -75,6 +86,14 @@ struct ModelVisData {
* Random seed has changed
*/
void updateRandomSeed();
/**
* Rebuild all environment graph sketches
*/
void buildEnvGraphs();
/**
* Rebuild a specific environment graph sketch
*/
void rebuildEnvGraph(const std::string& graph_name);
};

/**
Expand Down Expand Up @@ -110,6 +129,17 @@ class ModelVis {
* @see addAgent(const std::string&)
*/
AgentVis Agent(const std::string &agent_name);
/**
* Select a graph to be rendered
* @param graph_name The name of the environment directed graph to visualise
* @return A handle to configure the visualisation of the specified graph
*/
EnvironmentGraphVis addGraph(const std::string &graph_name);
/**
* Returns the configuration handler if the environment directed graph has been marked for visualisation
* @see addGraph(const std::string&)
*/
EnvironmentGraphVis Graph(const std::string& graph_name);
/**
* Set the title for the visualisation window
* This value defaults to the model's name
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ SET(SRC_INCLUDE_VISUALISER
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/ModelVis.h
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/AgentVis.h
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/AgentStateVis.h
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/EnvironmentGraphVis.h
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/StaticModelVis.h
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/LineVis.h
${FLAMEGPU_ROOT}/include/flamegpu/visualiser/PanelVis.h
Expand All @@ -411,6 +412,7 @@ set(SRC_FLAMEGPU_VISUALISER
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/ModelVis.cpp
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/AgentVis.cpp
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/AgentStateVis.cpp
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/EnvironmentGraphVis.cpp
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/StaticModelVis.cpp
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/LineVis.cpp
${FLAMEGPU_ROOT}/src/flamegpu/visualiser/PanelVis.cpp
Expand Down
9 changes: 8 additions & 1 deletion src/flamegpu/simulation/CUDASimulation.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,12 @@ void CUDASimulation::applyConfig_derived() {

// We init Random through submodel hierarchy after singletons
reseed(getSimulationConfig().random_seed);

#ifdef FLAMEGPU_VISUALISATION
if (visualisation) {
visualisation->hookVis(visualisation, directed_graph_map);
}
#endif
}

void CUDASimulation::reseed(const uint64_t seed) {
Expand Down Expand Up @@ -1709,8 +1715,9 @@ const CUDASimulation::Config &CUDASimulation::getCUDAConfig() const {
}
#ifdef FLAMEGPU_VISUALISATION
visualiser::ModelVis CUDASimulation::getVisualisation() {
if (!visualisation)
if (!visualisation) {
visualisation = std::make_shared<visualiser::ModelVisData>(*this);
}
return visualiser::ModelVis(visualisation, isSWIG);
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "flamegpu/simulation/detail/CUDAScatter.cuh"
#include "flamegpu/runtime/detail/curve/HostCurve.cuh"
#include "flamegpu/detail/cuda.cuh"
#ifdef FLAMEGPU_VISUALISATION
#include "flamegpu/visualiser/ModelVis.h"
#endif
#ifdef _MSC_VER
#pragma warning(push, 1)
#pragma warning(disable : 4706 4834)
Expand All @@ -18,6 +21,8 @@
#endif // __NVCC_DIAG_PRAGMA_SUPPORT__
#include <cub/cub.cuh>

#include "flamegpu/visualiser/FLAMEGPU_Visualisation.h"

#ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
#pragma nv_diag_default 1719
#else
Expand Down Expand Up @@ -379,13 +384,15 @@ __global__ void translateSrcDest(id_t *edgeSrcDest, unsigned int *idMap, const u
}
}
void CUDAEnvironmentDirectedGraphBuffers::syncDevice_async(detail::CUDAScatter& scatter, const unsigned int streamID, const cudaStream_t stream) {
bool has_changed = false;
// Copy variable buffers to device
if (vertex_count) {
for (auto& v : graph_description.vertexProperties) {
auto& vb = vertex_buffers.at(v.first);
if (vb.ready == Buffer::Host) {
gpuErrchk(cudaMemcpyAsync(vb.d_ptr, vb.h_ptr, vertex_count * v.second.type_size * v.second.elements, cudaMemcpyHostToDevice, stream));
vb.ready = Buffer::Both;
has_changed = true;
}
}
}
Expand All @@ -395,6 +402,7 @@ void CUDAEnvironmentDirectedGraphBuffers::syncDevice_async(detail::CUDAScatter&
if (eb.ready == Buffer::Host) {
gpuErrchk(cudaMemcpyAsync(eb.d_ptr, eb.h_ptr, edge_count * e.second.type_size * e.second.elements, cudaMemcpyHostToDevice, stream));
eb.ready = Buffer::Both;
has_changed = true;
}
}
}
Expand Down Expand Up @@ -571,7 +579,18 @@ void CUDAEnvironmentDirectedGraphBuffers::syncDevice_async(detail::CUDAScatter&
}
}
requires_rebuild = false;
has_changed = true;
}
#ifdef FLAMEGPU_VISUALISATION
if (has_changed) {
if (auto vis = visualisation.lock()) {
vis->visualiser->lockDynamicLinesMutex();
vis->rebuildEnvGraph(graph_description.name);
vis->visualiser->updateDynamicLine(std::string("graph_") + graph_description.name);
vis->visualiser->releaseDynamicLinesMutex();
}
}
#endif
}

void CUDAEnvironmentDirectedGraphBuffers::Buffer::updateHostBuffer(size_type edge_count, cudaStream_t stream) const {
Expand Down
Loading

0 comments on commit 107bb63

Please sign in to comment.