Skip to content

Commit 1a2b50b

Browse files
committed
Fixed memory exceptions related to matrix allocation and vector deallocation
1 parent a37dff4 commit 1a2b50b

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Common/include/linear_algebra/CSysMatrix.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ class CSysMatrix {
148148
ScalarType* d_matrix; /*!< \brief Device Pointer to store the matrix values on the GPU. */
149149
const unsigned long* d_row_ptr; /*!< \brief Device Pointers to the first element in each row. */
150150
const unsigned long* d_col_ind; /*!< \brief Device Column index for each of the elements in val(). */
151+
bool useCuda; /*!< \brief Boolean that indicates whether user has enabled CUDA or not.
152+
Mainly used to conditionally free GPU memory in the class destructor. */
151153

152154
ScalarType* ILU_matrix; /*!< \brief Entries of the ILU sparse matrix. */
153155
unsigned long nnz_ilu; /*!< \brief Number of possible nonzero entries in the matrix (ILU). */

Common/include/linear_algebra/CSysVector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class CSysVector : public VecExpr::CVecExpr<CSysVector<ScalarType>, ScalarType>
7272
unsigned long nElmDomain = 0; /*!< \brief Total number of elements without Ghost cells. */
7373
unsigned long nVar = 1; /*!< \brief Number of elements in a block. */
7474

75-
ScalarType* d_vec_val; /*!< \brief Device Pointer to store the vector values on the GPU. */
75+
ScalarType* d_vec_val = nullptr; /*!< \brief Device Pointer to store the vector values on the GPU. */
7676

7777
/*!
7878
* \brief Generic initialization from a scalar or array.

Common/src/linear_algebra/CSysMatrix.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ CSysMatrix<ScalarType>::~CSysMatrix() {
6868
MemoryAllocation::aligned_free(matrix);
6969
MemoryAllocation::aligned_free(invM);
7070

71-
GPUMemoryAllocation::gpu_free(d_matrix);
72-
GPUMemoryAllocation::gpu_free(d_row_ptr);
73-
GPUMemoryAllocation::gpu_free(d_col_ind);
71+
if (useCuda) {
72+
GPUMemoryAllocation::gpu_free(d_matrix);
73+
GPUMemoryAllocation::gpu_free(d_row_ptr);
74+
GPUMemoryAllocation::gpu_free(d_col_ind);
75+
}
7476

7577
#ifdef USE_MKL
7678
mkl_jit_destroy(MatrixMatrixProductJitter);
@@ -140,10 +142,12 @@ void CSysMatrix<ScalarType>::Initialize(unsigned long npoint, unsigned long npoi
140142
ptr = MemoryAllocation::aligned_alloc<ScalarType, true>(64, num * sizeof(ScalarType));
141143
};
142144

143-
if (config->GetCUDA()) {
144-
/*--- Allocate GPU data. ---*/
145-
allocAndInit(matrix, nnz * nVar * nEqn);
145+
allocAndInit(matrix, nnz * nVar * nEqn);
146146

147+
useCuda = config->GetCUDA();
148+
149+
if (useCuda) {
150+
/*--- Allocate GPU data. ---*/
147151
auto GPUAllocAndInit = [](ScalarType*& ptr, unsigned long num) {
148152
ptr = GPUMemoryAllocation::gpu_alloc<ScalarType, true>(num * sizeof(ScalarType));
149153
};

0 commit comments

Comments
 (0)