* support omp

This commit is contained in:
lxowalle
2025-04-10 18:16:49 +08:00
parent a3deeeb3e8
commit 7a01716082
6 changed files with 91 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
menu "Basic config"
config OMP_ENABLE
bool "Enable omp"
default n if PLATFORM = "maixcam"
default y if PLATFORM = "maixcam2"
help
Enable OMP to accelerate performance on multi-core platforms.
config OMP_ENABLE_DYNAMIC
bool "Enable omp dynamic"
default n if PLATFORM = "maixcam"
default n if PLATFORM = "maixcam2"
depends on OMP_ENABLE
help
Enable or disable dynamic adjustment of the number of threads in a parallel region
config OMP_THREAD_NUMBER
int "Config omp thread number"
default 1 if PLATFORM = "maixcam"
default 2 if PLATFORM = "maixcam2"
depends on OMP_ENABLE
help
Set the number of threads that will be used in parallel regions
endmenu

View File

@@ -19,6 +19,7 @@ namespace maix::util
*/
#define CATCH_EXCEPTION_RUN_RETURN(func, err_ret_value, ...) \
while(1){ \
maix::util::init_before_main();\
try \
{ \
int ret = (int)func(__VA_ARGS__); \
@@ -67,6 +68,13 @@ namespace maix::util
*/
void enable_kernel_debug();
/**
* @brief Initialize before main
* The function is used to add preparatory operations that need to be executed before the main program runs.
* @maixpy maix.util.init_before_main
*/
void init_before_main();
/**
* @brief register exit function
* @maixcdk maix.util.register_exit_function

View File

@@ -14,6 +14,10 @@ namespace maix::util
(void)process;
}
void init_before_main() {
}
void do_exit_function() {
}

View File

@@ -44,6 +44,10 @@ namespace maix::util
static std::vector<void(*)()> *exit_function_list;
void init_before_main() {
}
void register_exit_function(void (*process)(void)) {
if (exit_function_list == nullptr) {
exit_function_list = new std::vector<void(*)()>;

View File

@@ -6,6 +6,10 @@
#include <unistd.h>
#include <vector>
#if CONFIG_OMP_ENABLE
#include "omp.h"
#endif
namespace maix::util
{
void disable_kernel_debug() {
@@ -44,6 +48,17 @@ namespace maix::util
static std::vector<void(*)()> *exit_function_list;
void init_before_main() {
#if CONFIG_OMP_ENABLE
#if OMP_ENABLE_DYNAMIC
omp_set_dynamic(1);
#else
omp_set_dynamic(0);
#endif
omp_set_num_threads(CONFIG_OMP_THREAD_NUMBER);
#endif
}
void register_exit_function(void (*process)(void)) {
if (exit_function_list == nullptr) {
exit_function_list = new std::vector<void(*)()>;

View File

@@ -13,6 +13,9 @@
#include <vector>
#include <string>
#include <array>
#include <sched.h>
#include <thread>
#include "omp.h"
#ifdef PLATFORM_MAIXCAM
#include "sophgo_middleware.hpp"
#elif defined(PLATFORM_MAIXCAM2)
@@ -1551,8 +1554,21 @@ __EXIT:
image::Image *Image::copy()
{
image::Image *ret = new image::Image(_width, _height, _format);
;
#if defined(PLATFORM_MAIXCAM2)
auto src = (uint8_t *)_data;
auto dst = (uint8_t *)ret->data();
auto copy_size = ret->data_size() / 2;
#pragma omp parallel sections
{
#pragma omp section
memcpy(dst, src, copy_size);
#pragma omp section
memcpy(dst + copy_size, src + copy_size, copy_size);
}
#else
memcpy(ret->data(), _data, _width * _height * image::fmt_size[_format]);
#endif
return ret;
}
@@ -1692,6 +1708,26 @@ __EXIT:
if (!ok)
return err::ERR_RUNTIME;
break;
case FMT_YVU420SP:
{
cv::Mat bgr;
img.rows = _height * 3 / 2;
cv::cvtColor(img, bgr, cv::COLOR_YUV2BGR_NV21);
ok = cv::imwrite(path, bgr, params);
if (!ok)
return err::ERR_RUNTIME;
break;
}
case FMT_YUV420SP:
{
cv::Mat bgr;
img.rows = _height * 3 / 2;
cv::cvtColor(img, bgr, cv::COLOR_YUV2BGR_NV12);
ok = cv::imwrite(path, bgr, params);
if (!ok)
return err::ERR_RUNTIME;
break;
}
case FMT_JPEG:
{
std::string lower_path(path);