mirror of
https://github.com/opencv/opencv.git
synced 2025-06-13 21:23:31 +08:00
Merge pull request #19029 from diablodale:fix19004-memthreadstart
add thread-safe startup of fastMalloc and fastFree * add perf test core memory allocation * fix threading in isAlignedAllocationEnabled() * tweaks requested by maintainer
This commit is contained in:
parent
40ca8f4695
commit
ad94d8cc4f
48
modules/core/perf/perf_allocation.cpp
Executable file
48
modules/core/perf/perf_allocation.cpp
Executable file
@ -0,0 +1,48 @@
|
|||||||
|
// This file is part of OpenCV project.
|
||||||
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||||
|
// of this distribution and at http://opencv.org/license.html.
|
||||||
|
|
||||||
|
#include "perf_precomp.hpp"
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
using namespace perf;
|
||||||
|
|
||||||
|
#define ALLOC_MAT_SIZES ::perf::szSmall24, ::perf::szSmall32, ::perf::szSmall64, \
|
||||||
|
::perf::sz5MP, ::perf::sz2K, ::perf::szSmall128, ::perf::szODD, ::perf::szQVGA, \
|
||||||
|
::perf::szVGA, ::perf::szSVGA, ::perf::sz720p, ::perf::sz1080p, ::perf::sz2160p, \
|
||||||
|
::perf::sz4320p, ::perf::sz3MP, ::perf::szXGA, ::perf::szSXGA, ::perf::szWQHD, \
|
||||||
|
::perf::sznHD, ::perf::szqHD
|
||||||
|
|
||||||
|
namespace opencv_test
|
||||||
|
{
|
||||||
|
|
||||||
|
typedef perf::TestBaseWithParam<MatType> MatDepth_tb;
|
||||||
|
|
||||||
|
PERF_TEST_P(MatDepth_tb, DISABLED_Allocation_Aligned,
|
||||||
|
testing::Values(CV_8UC1, CV_16SC1, CV_8UC3, CV_8UC4))
|
||||||
|
{
|
||||||
|
const int matType = GetParam();
|
||||||
|
const cv::Mat utility(1, 1, matType);
|
||||||
|
const size_t elementBytes = utility.elemSize();
|
||||||
|
|
||||||
|
const std::array<cv::Size, 20> sizes{ALLOC_MAT_SIZES};
|
||||||
|
std::array<size_t, 20> bytes;
|
||||||
|
for (size_t i = 0; i < sizes.size(); ++i)
|
||||||
|
{
|
||||||
|
bytes[i] = sizes[i].width * sizes[i].height * elementBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare.time(60)
|
||||||
|
.iterations(100);
|
||||||
|
|
||||||
|
TEST_CYCLE()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 100000; ++i)
|
||||||
|
{
|
||||||
|
fastFree(fastMalloc(bytes[i % sizes.size()]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SANITY_CHECK_NOTHING();
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
@ -100,25 +100,27 @@ static bool readMemoryAlignmentParameter()
|
|||||||
// TODO add checks for valgrind, ASAN if value == false
|
// TODO add checks for valgrind, ASAN if value == false
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined _MSC_VER
|
||||||
|
#pragma warning(suppress:4714) // preventive: const marked as __forceinline not inlined
|
||||||
|
static __forceinline
|
||||||
|
#else
|
||||||
static inline
|
static inline
|
||||||
|
#endif
|
||||||
bool isAlignedAllocationEnabled()
|
bool isAlignedAllocationEnabled()
|
||||||
{
|
{
|
||||||
static bool initialized = false;
|
// use construct on first use idiom https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
|
||||||
static bool useMemalign = true;
|
// details: https://github.com/opencv/opencv/issues/15691
|
||||||
if (!initialized)
|
static bool useMemalign = readMemoryAlignmentParameter();
|
||||||
{
|
|
||||||
initialized = true; // trick to avoid stuck in acquire (works only if allocations are scope based)
|
|
||||||
useMemalign = readMemoryAlignmentParameter();
|
|
||||||
}
|
|
||||||
return useMemalign;
|
return useMemalign;
|
||||||
}
|
}
|
||||||
// do not use variable directly, details: https://github.com/opencv/opencv/issues/15691
|
|
||||||
|
// need for this static const is disputed; retaining as it doesn't cause harm
|
||||||
static const bool g_force_initialization_memalign_flag
|
static const bool g_force_initialization_memalign_flag
|
||||||
#if defined __GNUC__
|
#if defined __GNUC__
|
||||||
__attribute__((unused))
|
__attribute__((unused))
|
||||||
#endif
|
#endif
|
||||||
= isAlignedAllocationEnabled();
|
= isAlignedAllocationEnabled();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef OPENCV_ALLOC_ENABLE_STATISTICS
|
#ifdef OPENCV_ALLOC_ENABLE_STATISTICS
|
||||||
|
Loading…
Reference in New Issue
Block a user