Skip to content

Commit

Permalink
cpu: x64: gemm: fix memory leak
Browse files Browse the repository at this point in the history
If shared library is loaded/unloaded multiple times memory will be
leaked.
  • Loading branch information
aaraujom authored and tprimak committed Aug 19, 2021
1 parent b7d40a0 commit fd6d14c
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 232 deletions.
9 changes: 5 additions & 4 deletions src/cpu/x64/gemm/f32/jit_avx512_common_gemm_f32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <atomic>
#include <cmath>
#include <memory>
#include <mutex>

#include "common/dnnl_thread.hpp"
Expand Down Expand Up @@ -1740,7 +1741,7 @@ xbyak_gemm_t *get_xbyak_gemm(
};

// Kernel table [isTransA][isTransB][hasBias][beta (0, 1, other)]
static xbyak_gemm_t *kernel_table[2][2][2][3];
static std::unique_ptr<xbyak_gemm_t> kernel_table[2][2][2][3];
static std::once_flag initialized;
dnnl_status_t st = dnnl_success;
std::call_once(initialized, [&] {
Expand All @@ -1753,8 +1754,8 @@ xbyak_gemm_t *get_xbyak_gemm(
auto &kern = kernel_table[isTransA][isTransB][hasBias]
[beta_idx(beta)];

kern = new xbyak_gemm_t(
isTransA, isTransB, beta, hasBias);
kern.reset(new xbyak_gemm_t(
isTransA, isTransB, beta, hasBias));
if (kern->create_kernel() != dnnl_success) {
st = dnnl_runtime_error;
return;
Expand All @@ -1763,7 +1764,7 @@ xbyak_gemm_t *get_xbyak_gemm(
});

return (st == dnnl_success)
? kernel_table[isTransA][isTransB][hasBias][beta_idx(beta)]
? kernel_table[isTransA][isTransB][hasBias][beta_idx(beta)].get()
: nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/

#include <atomic>
#include <memory>
#include <mutex>

#include "common/dnnl_thread.hpp"
Expand Down Expand Up @@ -723,7 +724,7 @@ dnnl_status_t sgemm_smalln_tn(const dim_t m, const dim_t n, const dim_t k,
const dim_t ldb, const float beta, float *C, const dim_t ldc) {
using namespace avx512_core_gemm_smalln_tn_f32;

static xbyak_gemm_smalln_tn_t *kernels[4][3][3];
static std::unique_ptr<xbyak_gemm_smalln_tn_t> kernels[4][3][3];
static std::once_flag initialized;

dnnl_status_t st = dnnl_success;
Expand All @@ -732,7 +733,7 @@ dnnl_status_t sgemm_smalln_tn(const dim_t m, const dim_t n, const dim_t k,
for (float al : {0.0f, 1.0f, 2.0f}) {
for (float be : {0.0f, 1.0f, 2.0f}) {
auto &kern = kernels[N - 1][(dim_t)al][(dim_t)be];
kern = new xbyak_gemm_smalln_tn_t(N, be, al);
kern.reset(new xbyak_gemm_smalln_tn_t(N, be, al));
st = kern->create_kernel();
if (st != dnnl_success) return;
}
Expand Down
9 changes: 5 additions & 4 deletions src/cpu/x64/gemm/f32/jit_avx_gemm_f32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <atomic>
#include <cmath>
#include <memory>
#include <mutex>

#include "common/dnnl_thread.hpp"
Expand Down Expand Up @@ -2193,7 +2194,7 @@ xbyak_gemm_t *get_xbyak_gemm(
};

// Kernel table [isTransA][isTransB][hasBias][beta (0, 1, other)]
static xbyak_gemm_t *kernel_table[2][2][2][3];
static std::unique_ptr<xbyak_gemm_t> kernel_table[2][2][2][3];
static std::once_flag initialized;
dnnl_status_t st = dnnl_success;
std::call_once(initialized, [&] {
Expand All @@ -2206,8 +2207,8 @@ xbyak_gemm_t *get_xbyak_gemm(
auto &kern = kernel_table[isTransA][isTransB][hasBias]
[beta_idx(beta)];

kern = new xbyak_gemm_t(
isTransA, isTransB, beta, hasBias);
kern.reset(new xbyak_gemm_t(
isTransA, isTransB, beta, hasBias));
if (kern->create_kernel() != dnnl_success) {
st = dnnl_runtime_error;
return;
Expand All @@ -2216,7 +2217,7 @@ xbyak_gemm_t *get_xbyak_gemm(
});

return (st == dnnl_success)
? kernel_table[isTransA][isTransB][hasBias][beta_idx(beta)]
? kernel_table[isTransA][isTransB][hasBias][beta_idx(beta)].get()
: nullptr;
}

Expand Down
Loading

0 comments on commit fd6d14c

Please sign in to comment.