Skip to content

Commit

Permalink
Reset CUDAFatAgent _nextID when purging agent populations.
Browse files Browse the repository at this point in the history
Mostly so submodels always reset agent id for internal pops.
  • Loading branch information
Robadob committed Apr 30, 2021
1 parent 5d84bcc commit abfc2d1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion include/flamegpu/gpu/CUDAFatAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ class CUDAFatAgent {
* Resets the flag agent_ids_have_init
*/
void markIDsUnset() { agent_ids_have_init = false; }
/**
* Resets the ID counter (_nextID) to the first valid ID
* Useful for submodels which will keep resetting an agent population
* @note This will fail silently if it called if any state contains agents
*/
void resetIDCounter();

private:
/**
Expand Down Expand Up @@ -198,7 +204,7 @@ class CUDAFatAgent {
unsigned int mappedAgentCount;
/**
* Holds the next ID to be assigned
* IDs are assigned in consecutive order, however it cannot be guarnteed that IDs will always be consecutive due to agent death, parallel birth and multiple states.
* IDs are assigned in consecutive order, however it cannot be guaranteed that IDs will always be consecutive due to agent death, parallel birth and multiple states.
*/
id_t _nextID;
/**
Expand Down
5 changes: 5 additions & 0 deletions src/flamegpu/gpu/CUDAAgent.cu
Original file line number Diff line number Diff line change
Expand Up @@ -704,16 +704,21 @@ void CUDAAgent::initExcludedVars(const std::string &state, const unsigned int&co
sm->second->initExcludedVars(count, offset, scatter, streamId, stream);
}
void CUDAAgent::cullUnmappedStates() {
int i = 0;
for (auto &s : state_map) {
if (!s.second->getIsSubStatelist()) {
s.second->clear();
++i;
}
}
if (i == state_map.size())
fat_agent->resetIDCounter();
}
void CUDAAgent::cullAllStates() {
for (auto &s : state_map) {
s.second->clear();
}
fat_agent->resetIDCounter();
}
std::list<std::shared_ptr<VariableBuffer>> CUDAAgent::getUnboundVariableBuffers(const std::string& state) {
const auto& sm = state_map.find(state);
Expand Down
9 changes: 8 additions & 1 deletion src/flamegpu/gpu/CUDAFatAgent.cu
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CUDAFatAgent::CUDAFatAgent(const AgentData& description)
: mappedAgentCount(0)
, _nextID(ID_NOT_SET + 1)
, d_nextID(nullptr)
, hd_nextID(0) {
, hd_nextID(ID_NOT_SET) {
for (const std::string &s : description.states) {
// allocate memory for each state list by creating a new Agent State List
AgentState state = {mappedAgentCount, s};
Expand Down Expand Up @@ -381,3 +381,10 @@ void CUDAFatAgent::assignIDs() {

agent_ids_have_init = true;
}
void CUDAFatAgent::resetIDCounter() {
// Resetting ID whilst agents exist is a bad idea, so fail silently
for (auto& s : states_unique)
if (s->getSize())
return;
_nextID = ID_NOT_SET + 1;
}

0 comments on commit abfc2d1

Please sign in to comment.