Add tests for gpu pvt classes

This commit is contained in:
Tobias Meyer Andersen
2024-11-08 14:41:34 +01:00
parent 2546d2b181
commit 78e01ad06f
5 changed files with 295 additions and 16 deletions
+20 -13
View File
@@ -84,22 +84,29 @@ GpuBuffer<T>::resize(size_t newSize)
if (newSize < 1) {
OPM_THROW(std::invalid_argument, "Setting a GpuBuffer size to a non-positive number is not allowed");
}
// Allocate memory for the new buffer
T* tmpBuffer = nullptr;
OPM_GPU_SAFE_CALL(cudaMalloc(&tmpBuffer, sizeof(T) * newSize));
// Move the data from the old to the new buffer with truncation
size_t sizeOfMove = std::min({m_numberOfElements, newSize});
OPM_GPU_SAFE_CALL(cudaMemcpy(tmpBuffer,
m_dataOnDevice,
sizeOfMove * sizeof(T),
cudaMemcpyDeviceToDevice));
if (m_numberOfElements == 0) {
// We have no data, so we can just allocate new memory
OPM_GPU_SAFE_CALL(cudaMalloc(&m_dataOnDevice, sizeof(T) * newSize));
}
else {
// Allocate memory for temporary buffer
T* tmpBuffer = nullptr;
OPM_GPU_SAFE_CALL(cudaMalloc(&tmpBuffer, sizeof(T) * m_numberOfElements));
// free the old buffer
OPM_GPU_SAFE_CALL(cudaFree(m_dataOnDevice));
// Move the data from the old to the new buffer with truncation
size_t sizeOfMove = std::min({m_numberOfElements, newSize});
OPM_GPU_SAFE_CALL(cudaMemcpy(tmpBuffer,
m_dataOnDevice,
sizeOfMove * sizeof(T),
cudaMemcpyDeviceToDevice));
// swap the buffers
m_dataOnDevice = tmpBuffer;
// free the old buffer
OPM_GPU_SAFE_CALL(cudaFree(m_dataOnDevice));
// swap the buffers
m_dataOnDevice = tmpBuffer;
}
// update size
m_numberOfElements = newSize;
+3 -3
View File
@@ -81,9 +81,9 @@ public:
explicit GpuBuffer(const std::vector<T>& data);
/**
* @brief Default constructor that will initialize cublas and allocate 0 bytes of memory
* @brief Default constructor not allocating any memory
*/
explicit GpuBuffer();
GpuBuffer() = default;
/**
* @brief GpuBuffer allocates new GPU memory of size numberOfElements * sizeof(T)
@@ -265,7 +265,7 @@ public:
private:
T* m_dataOnDevice = nullptr;
size_t m_numberOfElements;
size_t m_numberOfElements = 0;
void assertSameSize(const GpuBuffer<T>& other) const;
void assertSameSize(size_t size) const;