Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/flamegpu/runtime/agent/DeviceAgentVector_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,21 @@ class DeviceAgentVector_impl : protected AgentVector {
*/
explicit VariableBufferPair(const std::shared_ptr<detail::VariableBuffer>& _device)
: device(_device) { }
VariableBufferPair(const VariableBufferPair& other) {
this->device = other.device;
this->host_sz = other.host_sz;
if (other.host) {
this->host = static_cast<char*>(malloc(this->host_sz));
memcpy(this->host, other.host, this->host_sz);
}
}
VariableBufferPair(VariableBufferPair&& other) {
*this = std::move(other);
}
VariableBufferPair& operator=(VariableBufferPair&& other) {
std::swap(this->host, other.host);
std::swap(this->device, other.device);
std::swap(this->host_sz, other.host_sz);
return *this;
}
/**
Expand All @@ -311,6 +320,10 @@ class DeviceAgentVector_impl : protected AgentVector {
* nullptr until required to be allocated
*/
char* host = nullptr;
/**
* Size of the allocated host ptr in bytes
*/
size_t host_sz = 0;
/**/
std::shared_ptr<detail::VariableBuffer> device;
};
Expand Down
7 changes: 5 additions & 2 deletions src/flamegpu/runtime/agent/DeviceAgentVector_impl.cu
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void DeviceAgentVector_impl::initUnboundBuffers() {
}
// Alloc
const size_t var_size = buff.device->type_size * buff.device->elements;
buff.host = static_cast<char*>(malloc(_capacity * var_size));
buff.host_sz = _capacity * var_size;
buff.host = static_cast<char*>(malloc(buff.host_sz));
// DtH memcpy
gpuErrchk(cudaMemcpyAsync(buff.host, buff.device->data, copy_len * var_size, cudaMemcpyDeviceToHost, stream));
// Not sure this will ever happen, but better safe
Expand Down Expand Up @@ -131,7 +132,8 @@ void DeviceAgentVector_impl::reinitUnboundBuffers() {
if (unbound_host_buffer_capacity < _capacity) {
free(buff.host);
// Alloc
buff.host = static_cast<char*>(malloc(_capacity * var_size));
buff.host_sz = _capacity * var_size;
buff.host = static_cast<char*>(malloc(buff.host_sz));
}
// DtH memcpy
gpuErrchk(cudaMemcpyAsync(buff.host, buff.device->data, copy_len * var_size, cudaMemcpyDeviceToHost, stream));
Expand Down Expand Up @@ -162,6 +164,7 @@ void DeviceAgentVector_impl::resizeUnboundBuffers(const unsigned int new_capacit
// Free old
free(buff.host);
// Replace old ptr
buff.host_sz = new_capacity * var_size;
buff.host = t;
if (init) {
for (unsigned int i = unbound_host_buffer_capacity; i < new_capacity; ++i) {
Expand Down
Loading