Class CUDASimulation

Inheritance Relationships

Base Type

Class Documentation

class flamegpu::CUDASimulation : public flamegpu::Simulation

CUDA runner for Simulation interface Executes a FLAMEGPU2 model using GPU

Public Types

typedef std::vector<NewAgentStorage> AgentDataBuffer
typedef std::unordered_map<std::string, AgentDataBuffer> AgentDataBufferStateMap
typedef std::unordered_map<std::string, VarOffsetStruct> AgentOffsetMap
typedef std::unordered_map<std::string, AgentDataBufferStateMap> AgentDataMap

Public Functions

explicit CUDASimulation(const ModelDescription &model, int argc = 0, const char **argv = nullptr)

Initialise cuda runner Allocates memory for agents/messages, copies environment properties to device etc If provided, you can pass runtime arguments to this constructor, to automatically call inititialise() This is not required, you can call inititialise() manually later, or not at all.

Parameters
  • model – The model description to initialise the runner to execute

  • argc – Runtime argument count

  • argv – Runtime argument list ptr

virtual ~CUDASimulation()

Inverse operation of contructor

virtual void initFunctions() override

Run the initFunctions of Simulation

virtual bool step() override

Steps the simulation once

Returns

False if an exit condition was triggered

virtual void exitFunctions() override

Run the exitFunctions of the Simulation..

Returns

False if an exit condition was triggered

virtual void simulate() override

Execute the simulation until config.steps have been executed, or an exit condition trips Includes init and exit functions calls.

void simulate(const RunPlan &plan)

Execute the simulation using the configuration and environment properties from the provided RunPlan

Note

The config.steps and config.random_seed values will be ignored, in favour of those provided by the RunPlan

Parameters

plan – The RunPlan to use to execute the simulation

Throws

exception::InvalidArgument – If the provided RunPlan does not match the CUDASimulation’s ModelDescription

virtual void setPopulationData(AgentVector &population, const std::string &state_name = ModelData::DEFAULT_STATE) override

Replaces internal population data for the specified agent

Parameters
  • population – The agent type and data to replace agents with

  • state_name – The agent state to add the agents to

Throws

exception::InvalidCudaAgent – If the agent type is not recognised

virtual void getPopulationData(AgentVector &population, const std::string &state_name = ModelData::DEFAULT_STATE) override

Returns the internal population data for the specified agent

Parameters
  • population – The agent type and data to fetch

  • state_name – The agent state to get the agents from

Throws

exception::InvalidCudaAgent – If the agent type is not recognised

template<typename T>
void setEnvironmentProperty(const std::string &property_name, const T &value)

Update the current value of the named environment property

Parameters
  • property_name – Name of the environment property to be updated

  • value – New value for the named environment property

Template Parameters

T – Type of the environment property

Throws
template<typename T, unsigned int N>
void setEnvironmentProperty(const std::string &property_name, const std::array<T, N> &value)

Update the current value of the named environment property array

Parameters
  • property_name – Name of the environment property to be updated

  • value – New value for the named environment property

Template Parameters

T – Type of the elements of the environment property array

Throws
template<typename T>
T getEnvironmentProperty(const std::string &property_name)

Return the current value of the named environment property

Parameters

property_name – Name of the environment property to be updated

Template Parameters

T – Type of the environment property

Throws

exception::InvalidEnvPropertyType – If the named environment property does not exist with the specified type

template<typename T, unsigned int N>
std::array<T, N> getEnvironmentProperty(const std::string &property_name)

Return the current value of the named environment property array

Parameters

property_name – Name of the environment property to be updated

Template Parameters

T – Type of the elements of the environment property array

Throws

exception::InvalidEnvPropertyType – If the named environment property does not exist with the specified type

CUDAAgent &getCUDAAgent(const std::string &agent_name) const

Returns the manager for the specified agent

virtual AgentInterface &getAgent(const std::string &name) override
CUDAMessage &getCUDAMessage(const std::string &message_name) const

Returns the manager for the specified agent

Config &CUDAConfig()

See

Simulation::applyConfig() Should be called afterwards to apply changes

Returns

A mutable reference to the cuda model specific configuration struct

virtual unsigned int getStepCounter() override

Returns the number of times step() has been called since the simulation was last reset/init

virtual void resetStepCounter() override

Manually resets the step counter

const Config &getCUDAConfig() const
Returns

An immutable reference to the cuda model specific configuration struct

void setStepLog(const StepLoggingConfig &stepConfig)

Configure which step data should be logged

Note

This must be for the same model description hierarchy as the CUDASimulation

Parameters

stepConfig – The step logging config for the CUDASimulation

void setExitLog(const LoggingConfig &exitConfig)

Configure which exit data should be logged

Note

This must be for the same model description hierarchy as the CUDASimulation

Parameters

exitConfig – The logging config for the CUDASimulation

virtual const RunLog &getRunLog() const override

Returns a reference to the current exit log

visualiser::ModelVis &getVisualisation()

Creates (on first call) and returns the visualisation configuration options for this model instance

void RTCSafeCudaMemcpyToSymbol(const void *symbol, const char *rtc_symbol_name, const void *src, size_t count, size_t offset = 0) const

Performs a cudaMemCopyToSymbol in the runtime library and also updates the symbols of any RTC functions (which exist separately within their own cuda module) Will thrown an error if any of the calls fail.

Parameters
  • symbol – A device symbol

  • rtc_symbol_name – The name of the symbol

  • src – Source memory address

  • count – Size in bytes to copy

  • offset – Offset from start of symbol in bytes

void RTCSafeCudaMemcpyToSymbolAddress(void *ptr, const char *rtc_symbol_name, const void *src, size_t count, size_t offset = 0) const

Performs a cudaMemCopy to a pointer in the runtime library and also updates the symbols of any RTC functions (which exist separately within their own cuda module) Will thrown an error if any of the calls fail.

Parameters
  • ptr – a pointer to a symbol in device memory

  • rtc_symbol_name – The name of the symbol

  • src – Source memory address

  • count – Size in bytes to copy

  • offset – Offset from start of symbol in bytes

double getElapsedTimeRTCInitialisation() const

Get the duration of the last time RTC was iniitliased

Returns

elapsed time of last simulation call in seconds.

double getElapsedTimeSimulation() const

Get the duration of the last call to simulate() in seconds.

Returns

elapsed time of last simulation call in seconds.

double getElapsedTimeInitFunctions() const

Get the duration of the last call to initFunctions() in seconds.

Returns

elapsed time of last simulation call in seconds.

double getElapsedTimeExitFunctions() const

Get the duration of the last call to stepFunctions() in seconds.

Returns

elapsed time of last simulation call in seconds.

std::vector<double> getElapsedTimeSteps() const

Get the duration of each step() since the last call to reset

Returns

vector of step times

double getElapsedTimeStep(unsigned int step) const

Get the duration of an individual step in seconds.

Parameters

step – Index of step, must be less than the number of steps executed.

Returns

elapsed time of required step in seconds

inline unsigned int getInstanceID() const

Returns the unique instance id of this CUDASimulation instance

Note

This value is used internally for environment property storage

Public Static Attributes

static bool AUTO_CUDA_DEVICE_RESET = true

If changed to false, will not auto cudaDeviceReset when final CUDASimulation instance is destructed

Protected Functions

virtual void reset(bool submodelReset) override

Returns the model to a clean state This clears all agents and message lists, resets environment properties and reseeds random generation. Also calls resetStepCounter();

Note

If triggered on a submodel, agent states and environment properties mapped to a parent agent, and random generation are not affected.

Note

If random was manually seeded, it will return to it’s original state. If random was seeded from time, it will return to a new random state.

Parameters

submodelReset – This should only be set to true when called automatically when a submodel reaches it’s exit condition during execution. This performs a subset of the regular reset procedure.

virtual void applyConfig_derived() override

Called by Simulation::applyConfig() to trigger any runner specific configs

See

CUDASimulation::CUDAConfig()

virtual bool checkArgs_derived(int argc, const char **argv, int &i) override

Called by Simulation::checkArgs() to trigger any runner specific argument checking This only handles arg 0 before returning

See

Simulation::checkArgs()

Returns

True if the argument was handled successfully

virtual void printHelp_derived() override

Called by Simulation::printHelp() to print help for runner specific runtime args

See

Simulation::printHelp()

virtual void resetDerivedConfig() override

Called at the start of Simulation::initialise(int, const char**) to reset runner specific config struct

See

Simulation::initialise(int, const char**)

Protected Attributes

const bool isPureRTC

If true, the model is pureRTC, and hence does not use non-RTC curve

struct Config

CUDA runner specific config

Public Members

int device_id = 0

GPU to execute model on Defaults to device 0, this is most performant device as detected by CUDA

bool inLayerConcurrency = true

Enable / disable the use of concurrency within a layer. Defaults to enabled.