Skip to content

Commit

Permalink
Update examples to work with namespaces
Browse files Browse the repository at this point in the history
Most examples use explict namespaces where required

boids_spatial3D demonstrates the using namespace flamegpu approach, which works but triggers a lint failure.
boids_rtc_spaticl3D demonstrates the using namespace flamegpu apprach, which doesn't quite work. also fails linting.

This will be addressed later, and then upadted to not use this appraoch when it works (and ideally a test is in place?)
  • Loading branch information
ptheywood committed Jun 5, 2021
1 parent 3e9f9c1 commit 79df23f
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 196 deletions.
37 changes: 18 additions & 19 deletions examples/boids_bruteforce/src/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ FLAMEGPU_HOST_DEVICE_FUNCTION void clampPosition(float &x, float &y, float &z, c


/**
* outputdata agent function for Boid agents, which outputs publicly visible properties to a message list
* outputdata agent funFLAMEGPU_AGENT_FUNCTIONction for Boid agents, which outputs publicly visible properties to a message list
*/
FLAMEGPU_AGENT_FUNCTION(outputdata, MsgNone, MsgBruteForce) {
FLAMEGPU_AGENT_FUNCTION(outputdata, flamegpu::MsgNone, flamegpu::MsgBruteForce) {
// Output each agents publicly visible properties.
FLAMEGPU->message_out.setVariable<id_t>("id", FLAMEGPU->getID());
FLAMEGPU->message_out.setVariable<float>("x", FLAMEGPU->getVariable<float>("x"));
Expand All @@ -124,12 +124,12 @@ FLAMEGPU_AGENT_FUNCTION(outputdata, MsgNone, MsgBruteForce) {
FLAMEGPU->message_out.setVariable<float>("fx", FLAMEGPU->getVariable<float>("fx"));
FLAMEGPU->message_out.setVariable<float>("fy", FLAMEGPU->getVariable<float>("fy"));
FLAMEGPU->message_out.setVariable<float>("fz", FLAMEGPU->getVariable<float>("fz"));
return ALIVE;
return flamegpu::ALIVE;
}
/**
* inputdata agent function for Boid agents, which reads data from neighbouring Boid agents, to perform the boid flocking model.
*/
FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
FLAMEGPU_AGENT_FUNCTION(inputdata, flamegpu::MsgBruteForce, flamegpu::MsgNone) {
// Agent properties in local register
const int id = FLAMEGPU->getID();
// Agent position
Expand Down Expand Up @@ -282,14 +282,14 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
FLAMEGPU->setVariable<float>("fy", agent_fy);
FLAMEGPU->setVariable<float>("fz", agent_fz);

return ALIVE;
return flamegpu::ALIVE;
}

int main(int argc, const char ** argv) {
ModelDescription model("Boids_BruteForce");
flamegpu::ModelDescription model("Boids_BruteForce");

{ // Location message
MsgBruteForce::Description &message = model.newMessage("location");
flamegpu::MsgBruteForce::Description &message = model.newMessage("location");
// A message to hold the location of an agent.
message.newVariable<int>("id");
message.newVariable<float>("x");
Expand All @@ -300,7 +300,7 @@ int main(int argc, const char ** argv) {
message.newVariable<float>("fz");
}
{ // Boid agent
AgentDescription &agent = model.newAgent("Boid");
flamegpu::AgentDescription &agent = model.newAgent("Boid");
agent.newVariable<float>("x");
agent.newVariable<float>("y");
agent.newVariable<float>("z");
Expand All @@ -316,7 +316,7 @@ int main(int argc, const char ** argv) {
* GLOBALS
*/
{
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();

// Population size to generate, if no agents are loaded from disk
env.newProperty("POPULATION_TO_GENERATE", 32768u);
Expand Down Expand Up @@ -347,27 +347,26 @@ int main(int argc, const char ** argv) {
* Control flow
*/
{ // Layer #1
LayerDescription &layer = model.newLayer();
flamegpu::LayerDescription &layer = model.newLayer();
layer.addAgentFunction(outputdata);
}
{ // Layer #2
LayerDescription &layer = model.newLayer();
flamegpu::LayerDescription &layer = model.newLayer();
layer.addAgentFunction(inputdata);
}


/**
* Create Model Runner
*/
CUDASimulation cuda_model(model);
flamegpu::CUDASimulation cuda_model(model);

/**
* Create visualisation
*/
#ifdef VISUALISATION
ModelVis &visualisation = cuda_model.getVisualisation();
flamegpu::visualiser::ModelVis &visualisation = cuda_model.getVisualisation();
{
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();
float envWidth = env.getProperty<float>("MAX_POSITION") - env.getProperty<float>("MIN_POSITION");
const float INIT_CAM = env.getProperty<float>("MAX_POSITION") * 1.25f;
visualisation.setInitialCameraLocation(INIT_CAM, INIT_CAM, INIT_CAM);
Expand All @@ -378,7 +377,7 @@ int main(int argc, const char ** argv) {
circ_agt.setForwardXVariable("fx");
circ_agt.setForwardYVariable("fy");
circ_agt.setForwardZVariable("fz");
circ_agt.setModel(Stock::Models::ICOSPHERE);
circ_agt.setModel(flamegpu::visualiser::Stock::Models::ICOSPHERE);
circ_agt.setModelScale(env.getProperty<float>("SEPARATION_RADIUS"));
}
visualisation.activate();
Expand All @@ -389,16 +388,16 @@ int main(int argc, const char ** argv) {

// If no xml model file was is provided, generate a population.
if (cuda_model.getSimulationConfig().input_file.empty()) {
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();
// Uniformly distribute agents within space, with uniformly distributed initial velocity.
std::mt19937 rngEngine(cuda_model.getSimulationConfig().random_seed);
std::uniform_real_distribution<float> position_distribution(env.getProperty<float>("MIN_POSITION"), env.getProperty<float>("MAX_POSITION"));
std::uniform_real_distribution<float> velocity_distribution(-1, 1);
std::uniform_real_distribution<float> velocity_magnitude_distribution(env.getProperty<float>("MIN_INITIAL_SPEED"), env.getProperty<float>("MAX_INITIAL_SPEED"));
const unsigned int populationSize = env.getProperty<unsigned int>("POPULATION_TO_GENERATE");
AgentVector population(model.Agent("Boid"), populationSize);
flamegpu::AgentVector population(model.Agent("Boid"), populationSize);
for (unsigned int i = 0; i < populationSize; i++) {
AgentVector::Agent instance = population[i];
flamegpu::AgentVector::Agent instance = population[i];

// Agent position in space
instance.setVariable<float>("x", position_distribution(rngEngine));
Expand Down
40 changes: 20 additions & 20 deletions examples/boids_bruteforce_dependency_graph/src/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,21 @@ FLAMEGPU_HOST_DEVICE_FUNCTION void clampPosition(float &x, float &y, float &z, c
/**
* outputdata agent function for Boid agents, which outputs publicly visible properties to a message list
*/
FLAMEGPU_AGENT_FUNCTION(outputdata, MsgNone, MsgBruteForce) {
FLAMEGPU_AGENT_FUNCTION(outputdata, flamegpu::MsgNone, flamegpu::MsgBruteForce) {
// Output each agents publicly visible properties.
FLAMEGPU->message_out.setVariable<id_t>("id", FLAMEGPU->getID());
FLAMEGPU->message_out.setVariable<flamegpu::id_t>("id", FLAMEGPU->getID());
FLAMEGPU->message_out.setVariable<float>("x", FLAMEGPU->getVariable<float>("x"));
FLAMEGPU->message_out.setVariable<float>("y", FLAMEGPU->getVariable<float>("y"));
FLAMEGPU->message_out.setVariable<float>("z", FLAMEGPU->getVariable<float>("z"));
FLAMEGPU->message_out.setVariable<float>("fx", FLAMEGPU->getVariable<float>("fx"));
FLAMEGPU->message_out.setVariable<float>("fy", FLAMEGPU->getVariable<float>("fy"));
FLAMEGPU->message_out.setVariable<float>("fz", FLAMEGPU->getVariable<float>("fz"));
return ALIVE;
return flamegpu::ALIVE;
}
/**
* inputdata agent function for Boid agents, which reads data from neighbouring Boid agents, to perform the boid flocking model.
*/
FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
FLAMEGPU_AGENT_FUNCTION(inputdata, flamegpu::MsgBruteForce, flamegpu::MsgNone) {
// Agent properties in local register
const int id = FLAMEGPU->getID();
// Agent position
Expand Down Expand Up @@ -163,7 +163,7 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
// Iterate location messages, accumulating relevant data and counts.
for (const auto &message : FLAMEGPU->message_in) {
// Ignore self messages.
if (message.getVariable<id_t>("id") != id) {
if (message.getVariable<flamegpu::id_t>("id") != id) {
// Get the message location and velocity.
const float message_x = message.getVariable<float>("x");
const float message_y = message.getVariable<float>("y");
Expand Down Expand Up @@ -282,14 +282,14 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
FLAMEGPU->setVariable<float>("fy", agent_fy);
FLAMEGPU->setVariable<float>("fz", agent_fz);

return ALIVE;
return flamegpu::ALIVE;
}

int main(int argc, const char ** argv) {
ModelDescription model("Boids_BruteForce_DependencyGraph");
flamegpu::ModelDescription model("Boids_BruteForce_DependencyGraph");

{ // Location message
MsgBruteForce::Description &message = model.newMessage("location");
flamegpu::MsgBruteForce::Description &message = model.newMessage("location");
// A message to hold the location of an agent.
message.newVariable<int>("id");
message.newVariable<float>("x");
Expand All @@ -301,20 +301,20 @@ int main(int argc, const char ** argv) {
}

// Boid agent
AgentDescription &agent = model.newAgent("Boid");
flamegpu::AgentDescription &agent = model.newAgent("Boid");
agent.newVariable<float>("x");
agent.newVariable<float>("y");
agent.newVariable<float>("z");
agent.newVariable<float>("fx");
agent.newVariable<float>("fy");
agent.newVariable<float>("fz");
AgentFunctionDescription& outputdataDescription = agent.newFunction("outputdata", outputdata);
flamegpu::AgentFunctionDescription& outputdataDescription = agent.newFunction("outputdata", outputdata);
outputdataDescription.setMessageOutput("location");
AgentFunctionDescription& inputdataDescription = agent.newFunction("inputdata", inputdata);
flamegpu::AgentFunctionDescription& inputdataDescription = agent.newFunction("inputdata", inputdata);
inputdataDescription.setMessageInput("location");

// Dependency specification
DependencyGraph dependencyGraph;
flamegpu::DependencyGraph dependencyGraph;
inputdataDescription.dependsOn(outputdataDescription);
dependencyGraph.addRoot(outputdataDescription);
dependencyGraph.generateLayers(model);
Expand All @@ -323,7 +323,7 @@ int main(int argc, const char ** argv) {
* GLOBALS
*/
{
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();

// Population size to generate, if no agents are loaded from disk
env.newProperty("POPULATION_TO_GENERATE", 32768u);
Expand Down Expand Up @@ -353,22 +353,22 @@ int main(int argc, const char ** argv) {
/**
* Create Model Runner
*/
CUDASimulation cuda_model(model);
flamegpu::CUDASimulation cuda_model(model);

/**
* Create visualisation
*/
#ifdef VISUALISATION
ModelVis &visualisation = cuda_model.getVisualisation();
flamegpu::visualiser::ModelVis &visualisation = cuda_model.getVisualisation();
{
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();
float envWidth = env.getProperty<float>("MAX_POSITION") - env.getProperty<float>("MIN_POSITION");
const float INIT_CAM = env.getProperty<float>("MAX_POSITION") * 1.25f;
visualisation.setInitialCameraLocation(INIT_CAM, INIT_CAM, INIT_CAM);
visualisation.setCameraSpeed(0.002f * envWidth);
auto &circ_agt = visualisation.addAgent("Boid");
// Position vars are named x, y, z; so they are used by default
circ_agt.setModel(Stock::Models::ICOSPHERE);
circ_agt.setModel(flamegpu::visualiser::Stock::Models::ICOSPHERE);
circ_agt.setModelScale(env.getProperty<float>("SEPARATION_RADIUS"));
}
visualisation.activate();
Expand All @@ -379,16 +379,16 @@ int main(int argc, const char ** argv) {

// If no xml model file was is provided, generate a population.
if (cuda_model.getSimulationConfig().input_file.empty()) {
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();
// Uniformly distribute agents within space, with uniformly distributed initial velocity.
std::mt19937 rngEngine(cuda_model.getSimulationConfig().random_seed);
std::uniform_real_distribution<float> position_distribution(env.getProperty<float>("MIN_POSITION"), env.getProperty<float>("MAX_POSITION"));
std::uniform_real_distribution<float> velocity_distribution(-1, 1);
std::uniform_real_distribution<float> velocity_magnitude_distribution(env.getProperty<float>("MIN_INITIAL_SPEED"), env.getProperty<float>("MAX_INITIAL_SPEED"));
const unsigned int populationSize = env.getProperty<unsigned int>("POPULATION_TO_GENERATE");
AgentVector population(model.Agent("Boid"), populationSize);
flamegpu::AgentVector population(model.Agent("Boid"), populationSize);
for (unsigned int i = 0; i < populationSize; i++) {
AgentVector::Agent instance = population[i];
flamegpu::AgentVector::Agent instance = population[i];

// Agent position in space
instance.setVariable<float>("x", position_distribution(rngEngine));
Expand Down
38 changes: 19 additions & 19 deletions examples/boids_rtc_bruteforce/src/main.cu
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ FLAMEGPU_HOST_DEVICE_FUNCTION void clampPosition(float &x, float &y, float &z, c
* outputdata agent function for Boid agents, which outputs publicly visible properties to a message list
*/
const char* outputdata = R"###(
FLAMEGPU_AGENT_FUNCTION(outputdata, MsgNone, MsgBruteForce) {
FLAMEGPU_AGENT_FUNCTION(outputdata, flamegpu::MsgNone, flamegpu::MsgBruteForce) {
// Output each agents publicly visible properties.
FLAMEGPU->message_out.setVariable<id_t>("id", FLAMEGPU->getID());
FLAMEGPU->message_out.setVariable<flamegpu::id_t>("id", FLAMEGPU->getID());
FLAMEGPU->message_out.setVariable<float>("x", FLAMEGPU->getVariable<float>("x"));
FLAMEGPU->message_out.setVariable<float>("y", FLAMEGPU->getVariable<float>("y"));
FLAMEGPU->message_out.setVariable<float>("z", FLAMEGPU->getVariable<float>("z"));
FLAMEGPU->message_out.setVariable<float>("fx", FLAMEGPU->getVariable<float>("fx"));
FLAMEGPU->message_out.setVariable<float>("fy", FLAMEGPU->getVariable<float>("fy"));
FLAMEGPU->message_out.setVariable<float>("fz", FLAMEGPU->getVariable<float>("fz"));
return ALIVE;
return flamegpu::ALIVE;
}
)###";
/**
Expand Down Expand Up @@ -172,7 +172,7 @@ FLAMEGPU_HOST_DEVICE_FUNCTION void clampPosition(float &x, float &y, float &z, c
z = (z > MAX_POSITION)? MAX_POSITION: z;
}
// Agent function
FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
FLAMEGPU_AGENT_FUNCTION(inputdata, flamegpu::MsgBruteForce, flamegpu::MsgNone) {
// Agent properties in local register
const int id = FLAMEGPU->getID();
// Agent position
Expand Down Expand Up @@ -206,7 +206,7 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
// Iterate location messages, accumulating relevant data and counts.
for (const auto &message : FLAMEGPU->message_in) {
// Ignore self messages.
if (message.getVariable<id_t>("id") != id) {
if (message.getVariable<flamegpu::id_t>("id") != id) {
// Get the message location and velocity.
const float message_x = message.getVariable<float>("x");
const float message_y = message.getVariable<float>("y");
Expand Down Expand Up @@ -325,15 +325,15 @@ FLAMEGPU_AGENT_FUNCTION(inputdata, MsgBruteForce, MsgNone) {
FLAMEGPU->setVariable<float>("fy", agent_fy);
FLAMEGPU->setVariable<float>("fz", agent_fz);
return ALIVE;
return flamegpu::ALIVE;
}
)###";

int main(int argc, const char ** argv) {
ModelDescription model("Boids_BruteForce");
flamegpu::ModelDescription model("Boids_BruteForce");

{ // Location message
MsgBruteForce::Description &message = model.newMessage("location");
flamegpu::MsgBruteForce::Description &message = model.newMessage("location");
// A message to hold the location of an agent.
message.newVariable<int>("id");
message.newVariable<float>("x");
Expand All @@ -344,7 +344,7 @@ int main(int argc, const char ** argv) {
message.newVariable<float>("fz");
}
{ // Boid agent
AgentDescription &agent = model.newAgent("Boid");
flamegpu::AgentDescription &agent = model.newAgent("Boid");
agent.newVariable<float>("x");
agent.newVariable<float>("y");
agent.newVariable<float>("z");
Expand All @@ -360,7 +360,7 @@ int main(int argc, const char ** argv) {
* GLOBALS
*/
{
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();

// Population size to generate, if no agents are loaded from disk
env.newProperty("POPULATION_TO_GENERATE", 32768u);
Expand Down Expand Up @@ -391,27 +391,27 @@ int main(int argc, const char ** argv) {
* Control flow
*/
{ // Layer #1
LayerDescription &layer = model.newLayer();
flamegpu::LayerDescription &layer = model.newLayer();
layer.addAgentFunction("Boid", "outputdata");
}
{ // Layer #2
LayerDescription &layer = model.newLayer();
flamegpu::LayerDescription &layer = model.newLayer();
layer.addAgentFunction("Boid", "inputdata");
}


/**
* Create Model Runner
*/
CUDASimulation cuda_model(model);
flamegpu::CUDASimulation cuda_model(model);

/**
* Create visualisation
*/
#ifdef VISUALISATION
ModelVis &visualisation = cuda_model.getVisualisation();
flamegpu::visualiser::ModelVis &visualisation = cuda_model.getVisualisation();
{
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();
float envWidth = env.getProperty<float>("MAX_POSITION") - env.getProperty<float>("MIN_POSITION");
const float INIT_CAM = env.getProperty<float>("MAX_POSITION") * 1.25f;
visualisation.setInitialCameraLocation(INIT_CAM, INIT_CAM, INIT_CAM);
Expand All @@ -422,7 +422,7 @@ int main(int argc, const char ** argv) {
circ_agt.setForwardXVariable("fx");
circ_agt.setForwardYVariable("fy");
circ_agt.setForwardZVariable("fz");
circ_agt.setModel(Stock::Models::ICOSPHERE);
circ_agt.setModel(flamegpu::visualiser::Stock::Models::ICOSPHERE);
circ_agt.setModelScale(env.getProperty<float>("SEPARATION_RADIUS"));
}
visualisation.activate();
Expand All @@ -433,16 +433,16 @@ int main(int argc, const char ** argv) {

// If no xml model file was is provided, generate a population.
if (cuda_model.getSimulationConfig().input_file.empty()) {
EnvironmentDescription &env = model.Environment();
flamegpu::EnvironmentDescription &env = model.Environment();
// Uniformly distribute agents within space, with uniformly distributed initial velocity.
std::mt19937 rngEngine(cuda_model.getSimulationConfig().random_seed);
std::uniform_real_distribution<float> position_distribution(env.getProperty<float>("MIN_POSITION"), env.getProperty<float>("MAX_POSITION"));
std::uniform_real_distribution<float> velocity_distribution(-1, 1);
std::uniform_real_distribution<float> velocity_magnitude_distribution(env.getProperty<float>("MIN_INITIAL_SPEED"), env.getProperty<float>("MAX_INITIAL_SPEED"));
const unsigned int populationSize = env.getProperty<unsigned int>("POPULATION_TO_GENERATE");
AgentVector population(model.Agent("Boid"), populationSize);
flamegpu::AgentVector population(model.Agent("Boid"), populationSize);
for (unsigned int i = 0; i < populationSize; i++) {
AgentVector_Agent instance = population[i];
flamegpu::AgentVector_Agent instance = population[i];

// Agent position in space
instance.setVariable<float>("x", position_distribution(rngEngine));
Expand Down
Loading

0 comments on commit 79df23f

Please sign in to comment.