Browse Source

non-inlined addref and release slows down overall speed, move them to header

tags/20210507
nihuini 5 years ago
parent
commit
3631c1933d
2 changed files with 167 additions and 167 deletions
  1. +0
    -167
      src/mat.cpp
  2. +167
    -0
      src/mat.h

+ 0
- 167
src/mat.cpp View File

@@ -33,32 +33,6 @@

namespace ncnn {

Mat& Mat::operator=(const Mat& m)
{
if (this == &m)
return *this;

if (m.refcount)
NCNN_XADD(m.refcount, 1);

release();

data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;

dims = m.dims;
w = m.w;
h = m.h;
c = m.c;

cstep = m.cstep;

return *this;
}

Mat Mat::clone(Allocator* _allocator) const
{
if (empty())
@@ -408,64 +382,7 @@ void Mat::create_like(const VkImageMat& im, Allocator* _allocator)
}
#endif // NCNN_VULKAN

void Mat::addref()
{
if (refcount)
NCNN_XADD(refcount, 1);
}

void Mat::release()
{
if (refcount && NCNN_XADD(refcount, -1) == 1)
{
if (allocator)
allocator->fastFree(data);
else
fastFree(data);
}

data = 0;

elemsize = 0;
elempack = 0;

dims = 0;
w = 0;
h = 0;
c = 0;

cstep = 0;

refcount = 0;
}

#if NCNN_VULKAN
VkMat& VkMat::operator=(const VkMat& m)
{
if (this == &m)
return *this;

if (m.refcount)
NCNN_XADD(m.refcount, 1);

release();

data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;

dims = m.dims;
w = m.w;
h = m.h;
c = m.c;

cstep = m.cstep;

return *this;
}

void VkMat::create(int _w, size_t _elemsize, VkAllocator* _allocator)
{
if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
@@ -673,61 +590,6 @@ void VkMat::create_like(const VkImageMat& im, VkAllocator* _allocator)
create(im.w, im.h, im.c, im.elemsize, im.elempack, _allocator);
}

void VkMat::addref()
{
if (refcount)
NCNN_XADD(refcount, 1);
}

void VkMat::release()
{
if (refcount && NCNN_XADD(refcount, -1) == 1)
{
if (allocator && data)
{
allocator->fastFree(data);
}
}

data = 0;

elemsize = 0;
elempack = 0;

dims = 0;
w = 0;
h = 0;
c = 0;

cstep = 0;

refcount = 0;
}

VkImageMat& VkImageMat::operator=(const VkImageMat& m)
{
if (this == &m)
return *this;

if (m.refcount)
NCNN_XADD(m.refcount, 1);

release();

data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;

dims = m.dims;
w = m.w;
h = m.h;
c = m.c;

return *this;
}

void VkImageMat::create(int _w, size_t _elemsize, VkAllocator* _allocator)
{
if (dims == 1 && w == _w && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
@@ -922,35 +784,6 @@ void VkImageMat::create_like(const VkImageMat& im, VkAllocator* _allocator)
if (_dims == 3)
create(im.w, im.h, im.c, im.elemsize, im.elempack, _allocator);
}

void VkImageMat::addref()
{
if (refcount)
NCNN_XADD(refcount, 1);
}

void VkImageMat::release()
{
if (refcount && NCNN_XADD(refcount, -1) == 1)
{
if (allocator && data)
{
allocator->fastFree(data);
}
}

data = 0;

elemsize = 0;
elempack = 0;

dims = 0;
w = 0;
h = 0;
c = 0;

refcount = 0;
}
#endif // NCNN_VULKAN

void Mat::substract_mean_normalize(const float* mean_vals, const float* norm_vals)


+ 167
- 0
src/mat.h View File

@@ -960,6 +960,63 @@ inline void Mat::fill(T _v)
}
}

inline Mat& Mat::operator=(const Mat& m)
{
if (this == &m)
return *this;

if (m.refcount)
NCNN_XADD(m.refcount, 1);

release();

data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;

dims = m.dims;
w = m.w;
h = m.h;
c = m.c;

cstep = m.cstep;

return *this;
}

inline void Mat::addref()
{
if (refcount)
NCNN_XADD(refcount, 1);
}

inline void Mat::release()
{
if (refcount && NCNN_XADD(refcount, -1) == 1)
{
if (allocator)
allocator->fastFree(data);
else
fastFree(data);
}

data = 0;

elemsize = 0;
elempack = 0;

dims = 0;
w = 0;
h = 0;
c = 0;

cstep = 0;

refcount = 0;
}

inline bool Mat::empty() const
{
return data == 0 || total() == 0;
@@ -1163,6 +1220,32 @@ inline VkMat::~VkMat()
release();
}

inline VkMat& VkMat::operator=(const VkMat& m)
{
if (this == &m)
return *this;

if (m.refcount)
NCNN_XADD(m.refcount, 1);

release();

data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;

dims = m.dims;
w = m.w;
h = m.h;
c = m.c;

cstep = m.cstep;

return *this;
}

inline Mat VkMat::mapped() const
{
if (!allocator->mappable)
@@ -1188,6 +1271,37 @@ inline void* VkMat::mapped_ptr() const
return (unsigned char*)data->mapped_ptr + data->offset;
}

inline void VkMat::addref()
{
if (refcount)
NCNN_XADD(refcount, 1);
}

inline void VkMat::release()
{
if (refcount && NCNN_XADD(refcount, -1) == 1)
{
if (allocator && data)
{
allocator->fastFree(data);
}
}

data = 0;

elemsize = 0;
elempack = 0;

dims = 0;
w = 0;
h = 0;
c = 0;

cstep = 0;

refcount = 0;
}

inline bool VkMat::empty() const
{
return data == 0 || total() == 0;
@@ -1312,6 +1426,30 @@ inline VkImageMat::~VkImageMat()
release();
}

inline VkImageMat& VkImageMat::operator=(const VkImageMat& m)
{
if (this == &m)
return *this;

if (m.refcount)
NCNN_XADD(m.refcount, 1);

release();

data = m.data;
refcount = m.refcount;
elemsize = m.elemsize;
elempack = m.elempack;
allocator = m.allocator;

dims = m.dims;
w = m.w;
h = m.h;
c = m.c;

return *this;
}

inline Mat VkImageMat::mapped() const
{
if (!allocator->mappable || !data->mapped_ptr)
@@ -1337,6 +1475,35 @@ inline void* VkImageMat::mapped_ptr() const
return (unsigned char*)data->mapped_ptr + data->bind_offset;
}

inline void VkImageMat::addref()
{
if (refcount)
NCNN_XADD(refcount, 1);
}

inline void VkImageMat::release()
{
if (refcount && NCNN_XADD(refcount, -1) == 1)
{
if (allocator && data)
{
allocator->fastFree(data);
}
}

data = 0;

elemsize = 0;
elempack = 0;

dims = 0;
w = 0;
h = 0;
c = 0;

refcount = 0;
}

inline bool VkImageMat::empty() const
{
return data == 0 || total() == 0;


Loading…
Cancel
Save