From: Vadim Pisarevsky Date: Thu, 14 May 2015 20:29:09 +0000 (+0300) Subject: significantly reduced sparse matrix footprint: X-Git-Tag: accepted/tizen/6.0/unified/20201030.111113~2477^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=66eb270cf72d555e7078b1d279f2c4c1d7f0111c;p=platform%2Fupstream%2Fopencv.git significantly reduced sparse matrix footprint: http://code.opencv.org/issues/2206, http://code.opencv.org/issues/2924 --- diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 9b77a4c..42b271f 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -4714,8 +4714,8 @@ SparseMat::Hdr::Hdr( int _dims, const int* _sizes, int _type ) refcount = 1; dims = _dims; - valueOffset = (int)alignSize(sizeof(SparseMat::Node) + - sizeof(int)*std::max(dims - CV_MAX_DIM, 0), CV_ELEM_SIZE1(_type)); + valueOffset = (int)alignSize(sizeof(SparseMat::Node) - MAX_DIM*sizeof(int) + + dims*sizeof(int), CV_ELEM_SIZE1(_type)); nodeSize = alignSize(valueOffset + CV_ELEM_SIZE(_type), (int)sizeof(size_t)); @@ -5114,7 +5114,8 @@ uchar* SparseMat::newNode(const int* idx, size_t hashval) if( !hdr->freeList ) { size_t i, nsz = hdr->nodeSize, psize = hdr->pool.size(), - newpsize = std::max(psize*2, 8*nsz); + newpsize = std::max(psize*3/2, 8*nsz); + newpsize = (newpsize/nsz)*nsz; hdr->pool.resize(newpsize); uchar* pool = &hdr->pool[0]; hdr->freeList = std::max(psize, nsz); diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 102bd99..897ac40 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -1248,3 +1248,27 @@ TEST(Core_SVD, orthogonality) ASSERT_LT(norm(mat_U, Mat::eye(2, 2, type), NORM_INF), 1e-5); } } + + +TEST(Core_SparseMat, footprint) +{ + int n = 1000000; + int sz[] = { n, n }; + SparseMat m(2, sz, CV_64F); + + int nodeSize0 = (int)m.hdr->nodeSize; + double dataSize0 = ((double)m.hdr->pool.size() + (double)m.hdr->hashtab.size()*sizeof(size_t))*1e-6; + printf("before: node size=%d bytes, data size=%.0f Mbytes\n", nodeSize0, dataSize0); + + for (int i = 0; i < n; i++) + { + m.ref(i, i) = 1; + } + + double dataSize1 = ((double)m.hdr->pool.size() + (double)m.hdr->hashtab.size()*sizeof(size_t))*1e-6; + double threshold = (n*nodeSize0*1.6 + n*2.*sizeof(size_t))*1e-6; + printf("after: data size=%.0f Mbytes, threshold=%.0f MBytes\n", dataSize1, threshold); + + ASSERT_LE((int)m.hdr->nodeSize, 32); + ASSERT_LE(dataSize1, threshold); +}