Skip to content

Commit

Permalink
fixing memory leaks (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: jsandham <james.sandham@amd.com>
  • Loading branch information
jsandham and jsandham authored Feb 24, 2021
1 parent bca444e commit 24fae7c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 88 deletions.
65 changes: 45 additions & 20 deletions clients/include/rocsparse_matrix_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ struct rocsparse_matrix_utils
bsrilu0_step step = bsrilu0_all)
{

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
if(!buffer)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
CHECK_ROCSPARSE_ERROR(rocsparse_bsrilu0_buffer_size<T>(handle,
that.block_direction,
that.mb,
Expand All @@ -306,8 +306,15 @@ struct rocsparse_matrix_utils
that.row_block_dim,
info,
p_buffer_size));
CHECK_ROCSPARSE_ERROR(rocsparse_destroy_handle(handle));

return;
}

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));

switch(step)
{
case bsrilu0_all:
Expand Down Expand Up @@ -381,11 +388,11 @@ struct rocsparse_matrix_utils
void* buffer,
bsric0_step step = bsric0_all)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
if(!buffer)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
CHECK_ROCSPARSE_ERROR(rocsparse_bsric0_buffer_size<T>(handle,
that.block_direction,
that.mb,
Expand All @@ -397,8 +404,14 @@ struct rocsparse_matrix_utils
that.row_block_dim,
info,
p_buffer_size));
CHECK_ROCSPARSE_ERROR(rocsparse_destroy_handle(handle));
return;
}

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));

switch(step)
{
case bsric0_all:
Expand Down Expand Up @@ -470,11 +483,11 @@ struct rocsparse_matrix_utils
void* buffer,
csric0_step step = csric0_all)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
if(!buffer)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
CHECK_ROCSPARSE_ERROR(rocsparse_csric0_buffer_size<T>(handle,
that.m,
that.nnz,
Expand All @@ -484,8 +497,14 @@ struct rocsparse_matrix_utils
that.ind,
info,
p_buffer_size));
CHECK_ROCSPARSE_ERROR(rocsparse_destroy_handle(handle));
return;
}

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));

switch(step)
{
case csric0_all:
Expand Down Expand Up @@ -556,12 +575,11 @@ struct rocsparse_matrix_utils
void* buffer,
csrilu0_step step = csrilu0_all)
{

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
if(!buffer)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));
CHECK_ROCSPARSE_ERROR(rocsparse_csrilu0_buffer_size<T>(handle,
that.m,
that.nnz,
Expand All @@ -571,8 +589,14 @@ struct rocsparse_matrix_utils
that.ind,
info,
p_buffer_size));
CHECK_ROCSPARSE_ERROR(rocsparse_destroy_handle(handle));
return;
}

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
CHECK_ROCSPARSE_ERROR(rocsparse_set_pointer_mode(handle, rocsparse_pointer_mode_host));

switch(step)
{
case csrilu0_all:
Expand Down Expand Up @@ -1614,13 +1638,6 @@ struct rocsparse_matrix_factory : public rocsparse_matrix_factory_base<T, I, J>
void init_hyb(
rocsparse_hyb_mat hyb, I& M, I& N, I& nnz, rocsparse_index_base base, bool& conform)
{
rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
rocsparse_mat_descr descr;
CHECK_ROCSPARSE_ERROR(rocsparse_create_mat_descr(&descr));
// Set matrix index base
CHECK_ROCSPARSE_ERROR(rocsparse_set_mat_index_base(descr, base));

conform = true;
rocsparse_hyb_partition part = this->m_arg.part;
rocsparse_int user_ell_width = this->m_arg.algo;
Expand Down Expand Up @@ -1656,6 +1673,14 @@ struct rocsparse_matrix_factory : public rocsparse_matrix_factory_base<T, I, J>
}

device_csr_matrix<T, I, I> dA(hA);

rocsparse_handle handle;
CHECK_ROCSPARSE_ERROR(rocsparse_create_handle(&handle));
rocsparse_mat_descr descr;
CHECK_ROCSPARSE_ERROR(rocsparse_create_mat_descr(&descr));
// Set matrix index base
CHECK_ROCSPARSE_ERROR(rocsparse_set_mat_index_base(descr, base));

// Convert CSR matrix to HYB
CHECK_ROCSPARSE_ERROR(rocsparse_csr2hyb<T>(
handle, M, N, descr, dA.val, dA.ptr, dA.ind, hyb, user_ell_width, part));
Expand Down
22 changes: 7 additions & 15 deletions clients/include/testing_csx2dense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void testing_csx2dense_bad_arg(const Arguments& arg, FUNC& csx2dense)
static constexpr rocsparse_int N = 10;
static constexpr rocsparse_int LD = M;
rocsparse_local_handle handle;
rocsparse_local_mat_descr descr;

device_vector<T> d_dense_val(safe_size);
device_vector<rocsparse_int> d_csx_row_col_ptr(2);
Expand All @@ -61,8 +62,6 @@ void testing_csx2dense_bad_arg(const Arguments& arg, FUNC& csx2dense)
return;
}

rocsparse_mat_descr descr = nullptr;
rocsparse_create_mat_descr(&descr);
//
// Testing invalid handle.
//
Expand Down Expand Up @@ -163,20 +162,18 @@ void testing_csx2dense_bad_arg(const Arguments& arg, FUNC& csx2dense)
(T*)d_dense_val,
M - 1),
rocsparse_status_invalid_size);
rocsparse_destroy_mat_descr(descr);
}

template <rocsparse_direction DIRA, typename T, typename FUNC1, typename FUNC2>
void testing_csx2dense(const Arguments& arg, FUNC1& csx2dense, FUNC2& dense2csx)

{
rocsparse_int M = arg.M;
rocsparse_int N = arg.N;
rocsparse_int LD = arg.denseld;
rocsparse_int DIMDIR = (rocsparse_direction_row == DIRA) ? M : N;
rocsparse_local_handle handle;
rocsparse_mat_descr descr;
rocsparse_create_mat_descr(&descr);
rocsparse_int M = arg.M;
rocsparse_int N = arg.N;
rocsparse_int LD = arg.denseld;
rocsparse_int DIMDIR = (rocsparse_direction_row == DIRA) ? M : N;
rocsparse_local_handle handle;
rocsparse_local_mat_descr descr;
rocsparse_set_mat_index_base(descr, arg.baseA);

//
Expand Down Expand Up @@ -420,11 +417,6 @@ void testing_csx2dense(const Arguments& arg, FUNC1& csx2dense, FUNC2& dense2csx)
<< std::endl;
// clang-format on
}

//
// Destroy the matrix descriptor.
//
rocsparse_destroy_mat_descr(descr);
}

#endif // TESTING_CSX2DENSE_HPP
7 changes: 7 additions & 0 deletions clients/include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ class rocsparse_local_mat_info
rocsparse_destroy_mat_info(this->info);
}

// Sometimes useful to reset local info
void reset()
{
rocsparse_destroy_mat_info(this->info);
rocsparse_create_mat_info(&this->info);
}

// Allow rocsparse_local_mat_info to be used anywhere rocsparse_mat_info is expected
operator rocsparse_mat_info&()
{
Expand Down
13 changes: 4 additions & 9 deletions clients/testings/testing_bsrsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ void testing_bsrsv(const Arguments& arg)
rocsparse_local_mat_descr descr;

// Create matrix info
rocsparse_mat_info info;
CHECK_ROCSPARSE_ERROR(rocsparse_create_mat_info(&info));
rocsparse_local_mat_info info;

// Set matrix diag type
CHECK_ROCSPARSE_ERROR(rocsparse_set_mat_diag_type(descr, diag));
Expand Down Expand Up @@ -331,9 +330,7 @@ void testing_bsrsv(const Arguments& arg)
//
// RESET MAT INFO.
//
CHECK_ROCSPARSE_ERROR(rocsparse_destroy_mat_info(info));
CHECK_ROCSPARSE_ERROR(rocsparse_create_mat_info(&info));

info.reset();
{
device_scalar<rocsparse_int> d_analysis_pivot;
device_scalar<rocsparse_int> d_solve_pivot;
Expand Down Expand Up @@ -376,8 +373,7 @@ void testing_bsrsv(const Arguments& arg)
//
// RESET MAT INFO
//
CHECK_ROCSPARSE_ERROR(rocsparse_destroy_mat_info(info));
CHECK_ROCSPARSE_ERROR(rocsparse_create_mat_info(&info));
info.reset();

//
// A BIT MORE FOR CODE COVERAGE, WE ONLY DO ANALYSIS FOR INFO ASSIGNMENT.
Expand Down Expand Up @@ -418,8 +414,7 @@ void testing_bsrsv(const Arguments& arg)
CHECK_ROCSPARSE_ERROR(rocsparse_bsrsv_analysis<T>(PARAMS_ANALYSIS(dA)));
}

CHECK_ROCSPARSE_ERROR(rocsparse_destroy_mat_info(info));
CHECK_ROCSPARSE_ERROR(rocsparse_create_mat_info(&info));
info.reset();

//
// A BIT MORE FOR CODE COVERAGE, WE ONLY DO ANALYSIS FOR INFO ASSIGNMENT.
Expand Down
20 changes: 7 additions & 13 deletions clients/testings/testing_coo2dense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void testing_coo2dense_bad_arg(const Arguments& arg)
static constexpr rocsparse_int NNZ = 10;
static constexpr rocsparse_int LD = M;
rocsparse_local_handle handle;
rocsparse_local_mat_descr descr;

device_vector<T> d_dense_val(safe_size);
device_vector<rocsparse_int> d_coo_row_ind(2);
Expand All @@ -45,9 +46,6 @@ void testing_coo2dense_bad_arg(const Arguments& arg)
return;
}

rocsparse_mat_descr descr = nullptr;
rocsparse_create_mat_descr(&descr);

// Testing invalid handle.
EXPECT_ROCSPARSE_STATUS(
rocsparse_coo2dense(
Expand Down Expand Up @@ -159,18 +157,17 @@ void testing_coo2dense_bad_arg(const Arguments& arg)
(T*)d_dense_val,
M - 1),
rocsparse_status_invalid_size);
rocsparse_destroy_mat_descr(descr);
}

template <typename T>
void testing_coo2dense(const Arguments& arg)
{
rocsparse_int M = arg.M;
rocsparse_int N = arg.N;
rocsparse_int LD = arg.denseld;
rocsparse_local_handle handle;
rocsparse_mat_descr descr;
rocsparse_create_mat_descr(&descr);
rocsparse_int M = arg.M;
rocsparse_int N = arg.N;
rocsparse_int LD = arg.denseld;
rocsparse_local_handle handle;
rocsparse_local_mat_descr descr;

rocsparse_set_mat_index_base(descr, arg.baseA);

// Argument sanity check before allocating invalid memory
Expand Down Expand Up @@ -383,9 +380,6 @@ void testing_coo2dense(const Arguments& arg)
<< std::endl;
// clang-format on
}

// Destroy the matrix descriptor.
rocsparse_destroy_mat_descr(descr);
}

#define INSTANTIATE(TYPE) \
Expand Down
8 changes: 4 additions & 4 deletions clients/testings/testing_csrsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,9 @@ void testing_csrsm(const Arguments& arg)
// Create matrix descriptor
rocsparse_local_mat_descr descr;

// Create matrix info
rocsparse_local_mat_info info;

// Create matrix info

// Set matrix diag type
Expand All @@ -943,9 +946,6 @@ void testing_csrsm(const Arguments& arg)
// Set matrix index base
CHECK_ROCSPARSE_ERROR(rocsparse_set_mat_index_base(descr, base));

rocsparse_mat_info info = nullptr;
rocsparse_create_mat_info(&info);

// Argument sanity check before allocating invalid memory
if(M <= 0 || nrhs <= 0)
{
Expand Down Expand Up @@ -1198,7 +1198,7 @@ void testing_csrsm(const Arguments& arg)
//
// RESET MAT INFO.
//
rocsparse_create_mat_info(&info);
info.reset();

//
// CALL BUFFER SIZE WITH DEVICE MODE.
Expand Down
Loading

0 comments on commit 24fae7c

Please sign in to comment.