当前位置: 首页 > news >正文

C++11使用多线程(线程池)计算相似度实现性能优化

需求:图像识别中,注册的样本多了会影响计算速度,成为性能瓶颈,其中一个优化方法就是使用多线程。例如,注册了了3000个特征,每个特征4096个float。可以把3000个特征比对放到4个线程中进行计算,然后再把结果进行合并。实现思路:

1. 根据系统性能和需求配置线程池的大小,创建线程池,将比较任务平均分配到各个线程

2. 工作线程启动后在一个condition_variable上wait,注意:锁的范围不能太大了,否则多个线程会变成串行

3. 调用者调用识别接口,接口更新目标特征,通知各个工作线程,在另外一个condition_variable上wait,并且满足完成计数器的值等于线程数

4. 工作线程完成后将计数器加一,并且通知调用线程

5. 调用线程收集到所有线程的结果后再对结果进行合并返回

后续:

1. 代码进行优化,更优雅的实现

测试结果:

线程数

时间

1

71362ms

2

36292ms

4

19420ms

8

18465ms

16

18433ms

32

18842ms

64

19324ms

128

19388ms

256

21853ms

512

26150ms

1024

35593ms

代码如下:

#include <iostream>

#include <string>

#include <cstring>

#include <mutex>

#include <unordered_map>

#include <list>

#include <utility>

#include <algorithm>

#include <string>

#include <vector>

#include <thread>

#include <chrono>

using namespace std;

using namespace chrono;


 

double get_mold(const vector<double> &vec)

{

int n = vec.size();

double sum = 0.0;

for(int i = 0; i < n; ++i)

{

sum += vec[i] * vec[i];

}

return sqrt(sum);

}

double cosine_distance(const vector<double> &base, const vector<double> &target)

{

int n = base.size();

double tmp = 0.0;

for(int i = 0; i < n; ++i)

{

tmp += base[i] * target[i];

}

double simility = tmp / (get_mold(base) * get_mold(target));

return simility;

}


 

class Recognizer

{

public:

Recognizer(int num_threads) :

num_threads_(num_threads),

is_run_calculate_thread_(true),

is_doing_recognize(false),

result_count(0)

{

recognize_result = std::vector<RecognizeResult>(num_threads);

this->load_feature();

this->init_threads();

}

~Recognizer()

{

is_run_calculate_thread_ = false;

cv_.notify_all();

for(std::thread &th : threads_)

th.join();

}

int do_recognize(const vector<double> &feature);

private:

class CigaretteItem

{

public:

int cigarette_id_;

std::string cigarette_name_;

std::vector<double> feature_;

CigaretteItem(int cigarette_id, std::string cigarette_name, const std::vector<double> &cigarette_feature)

{

cigarette_id_ = cigarette_id;

cigarette_name_ = cigarette_name;

feature_ = std::vector<double>(cigarette_feature.size());

for(int i = 0; i < cigarette_feature.size(); i++)

{

feature_[i] = cigarette_feature[i];

}

}

};

class RecognizeResult

{

public:

int cigarette_id_;

std::string cigarette_name_;

double score_;

};

private:

int num_threads_;

bool is_run_calculate_thread_;

bool is_doing_recognize;

std::vector<CigaretteItem> ciagarette_list_;

std::vector<double> target_feature_;

std::mutex cv_mtx_;

std::condition_variable cv_;

std::vector<RecognizeResult> recognize_result;

std::vector<std::thread> threads_;

int result_count;

std::mutex result_count_mtx_;

std::mutex result_cv_mtx_;

std::condition_variable result_cv_;


 

private:

Recognizer(const Recognizer&) = delete;

Recognizer& operator=(const Recognizer&) = delete;

void load_feature();

void init_threads();

void calculate_most_similarity(const int thread_id, const int start_index, const int end_index);

};


 

void Recognizer::load_feature()

{

for(int i = 0; i < 3000; i++)

{

vector<double> fea = vector<double>(4096);

for(int i = 0; i < 4096; ++i)

fea[i] = (double)(rand() % 998 + 1) / 1000.00;

ciagarette_list_.emplace_back(i+1, "cigarette", fea);

}

}

void Recognizer::init_threads()

{

for(int i = 0; i < num_threads_; i++)

{

int step = this->ciagarette_list_.size() / this->num_threads_;

int start_index = i * step;

int end_index = (i+1) * step;

if(i == num_threads_ - 1){

end_index = ciagarette_list_.size();

}

std::cout << "thread" << i << " starts at " << start_index << "; ends at " << end_index << std::endl;

threads_.emplace_back(&Recognizer::calculate_most_similarity, this, i, start_index, end_index);

}

}

void Recognizer::calculate_most_similarity(const int thread_id, const int start_index, const int end_index)

{

while(is_run_calculate_thread_)

{

{

std::unique_lock<std::mutex> lock(cv_mtx_);

cv_.wait(lock);

}

//cout << "thread" << thread_id << " is running" << endl;

double max_score = -1.00;

int max_score_index = -1;

for(int i = start_index; i < end_index; ++i){

double score = cosine_distance(ciagarette_list_[i].feature_, target_feature_);

if(score > max_score)

{

max_score = score;

max_score_index = i;

}

}

recognize_result[thread_id].cigarette_id_ = ciagarette_list_[max_score_index].cigarette_id_;

recognize_result[thread_id].cigarette_name_ = ciagarette_list_[max_score_index].cigarette_name_;

recognize_result[thread_id].score_ = max_score;

{

std::unique_lock<std::mutex> lock(result_count_mtx_);

result_count += 1;

}

result_cv_.notify_one();

//std::cout << "thread" << thread_id << " finish one task" << endl;

}

//std::cout << "thread" << thread_id << " finished." << std::endl;

}

int Recognizer::do_recognize(const vector<double> &feature)

{

if(is_doing_recognize)

return -1;

is_doing_recognize = true;

this->target_feature_ = feature;

//cout << "cv_.notify_all()" << endl;

cv_.notify_all();

std::unique_lock<std::mutex> lock(result_cv_mtx_);

result_cv_.wait(lock, [this](){return this->num_threads_ == this->result_count;});

//std::cout << "all threads finish computing similarity" << endl;

int max_score_cigarette_id = -1;

std::string max_score_cigarette_name = "";

double max_score = -1.0;

for(int i = 0; i < num_threads_; ++i)

{

if(recognize_result[i].score_ > max_score)

{

max_score_cigarette_id = recognize_result[i].cigarette_id_;

max_score_cigarette_name = recognize_result[i].cigarette_name_;

max_score = recognize_result[i].score_;

}

}

//cout << "cigarette_id=" << max_score_cigarette_id << ", cigarette_name=" << max_score_cigarette_name << ", score=" << max_score << endl;

this->result_count = 0;

is_doing_recognize = false;

return 0;

}


 

int main(void)

{

Recognizer recognizer{1024};

std::this_thread::sleep_for(std::chrono::seconds(1));

const int loops = 400;

auto start_time = system_clock::now();

for(int i = 0; i < loops; i++)

{

//cout << endl;

std::vector<double> target_feature = std::vector<double>(4096);

for(int i = 0; i < 4096; ++i)

{

//target_feature[i] = (double)(rand() % 998 + 1) / 1000.000;

target_feature[i] = (double)(i % 1000 + 1) / 1000.00;

}

recognizer.do_recognize(target_feature);

//if((i+1) % 20 == 0)

// cout << "i=" << i << endl;

}

auto end_time = system_clock::now();

auto duration = duration_cast<milliseconds>(end_time - start_time);

cout << "eplased_time:" << duration.count() << "ms" << endl;

std::this_thread::sleep_for(std::chrono::seconds(2));

return 0;

}

相关文章:

C++11使用多线程(线程池)计算相似度实现性能优化

需求&#xff1a;图像识别中&#xff0c;注册的样本多了会影响计算速度&#xff0c;成为性能瓶颈&#xff0c;其中一个优化方法就是使用多线程。例如&#xff0c;注册了了3000个特征&#xff0c;每个特征4096个float。可以把3000个特征比对放到4个线程中进行计算&#xff0c;然…...

【测绘程序设计】——平面坐标转换

测绘工程中经常遇到平面坐标转换——比如,北京54(或西安80)平面坐标转换成CGCS2000平面坐标、工程独立坐标系平面坐标转换成CGCS2000平面坐标等,常用转换模型包括:①三参数法(2平移+1旋转);②四参数法(赫尔默特法,2平移+1旋转+1尺度);③六参数法(仿射变换法,2平移…...

五子棋的设计与实现

术&#xff1a;Java等摘要&#xff1a;五子棋是一种两人对弈的纯策略型棋类游戏&#xff0c;非常容易上手&#xff0c;老少皆宜。为了更好的推广五子棋&#xff0c;研究简单的人工智能方式&#xff0c;运用Java开发五子棋游戏。主要包含了人机对战&#xff0c;棋盘初始化&#…...

大数据项目软硬件选择

目录 一.技术选型 二.系统数据流程设计 三.框架版本选型 如何选择Apache/CDH/HDP版本...

redis数据结构的适用场景分析

1、String 类型的内存空间消耗问题&#xff0c;以及选择节省内存开销的数据类型的解决方案。 为什么 String 类型内存开销大&#xff1f; 图片 ID 和图片存储对象 ID 都是 10 位数&#xff0c;我们可以用两个 8 字节的 Long 类型表示这两个 ID。因为 8 字节的 Long 类型最大可以…...

同步、异步、全双工、半双工的区别

1、通讯 1.1 并行通讯 定义&#xff1a;一条信息的各位数据被同时传送的通讯方式称为并行通讯&#xff1b; 特点&#xff1a; 各个数据位同时发送&#xff0c;传送速度快、效率高&#xff0c;但有多少数据位就需要多少根数据线&#xff0c;因此传送成本高&#xff0c;并且只…...

ClickHouse 与 Amazon S3 结合?一起来探索其中奥秘

目录ClickHouse 简介ClickHouse 与对象存储ClickHouse 与 S3 结合的三种方法示例参考架构小结参考资料ClickHouse 简介ClickHouse 是一种快速的、开源的、用于联机分析&#xff08;OLAP&#xff09;的列式数据库管理系统&#xff08;DBMS&#xff09;&#xff0c;由俄罗斯的Yan…...

【Spark分布式内存计算框架——Structured Streaming】1. Structured Streaming 概述

前言 Apache Spark在2016年的时候启动了Structured Streaming项目&#xff0c;一个基于Spark SQL的全新流计算引擎Structured Streaming&#xff0c;让用户像编写批处理程序一样简单地编写高性能的流处理程序。 Structured Streaming并不是对Spark Streaming的简单改进&#xf…...

【Windows】【Linux】---- Java证书导入

问题&#xff1a; PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 无法找到请求目标的有效证书路径 一、Windows—java证书导入 1、下载证书到本地&#xff08;以下…...

【Linux学习】菜鸟入门——gcc与g++简要使用

一、gcc/g gcc/g是编译器&#xff0c;gcc是GCC(GUN Compiler Collection&#xff0c;GUN编译器集合)中的C编译器&#xff1b;g是GCC中的C编译器。使用g编译文件时会自动链接STL标准库&#xff0c;而gcc不会自动链接STL标准库。下面简单介绍一下Linux环境下&#xff08;Windows差…...

Cadence Allegro 导出Bill of Material Report详解

⏪《上一篇》   🏡《总目录》   ⏩《下一篇》 目录 1,概述2,Assigned Functions Report作用3,Assigned Functions Report示例4,Assigned Functions Report导出方法4.1,方法14.2,方法2B站关注“硬小二”浏览更多演示视频...

localStorage线上问题的思考

一、背景&#xff1a; localStorage作为HTML5 Web Storage的API之一&#xff0c;使用标准的键值对&#xff08;Key-Value,简称KV&#xff09;数据类型主要作用是本地存储。本地存储是指将数据按照键值对的方式保存在客户端计算机中&#xff0c;直到用户或者脚本主动清除数据&a…...

什么是DNS域名解析

什么是DNS域名解析&#xff1f;因特网上作为域名和IP地址相互映射的一个分布式数据库&#xff0c;能够使用户更方便的访问互联网&#xff0c;而不用去记住能够被机器直接读取的IP数串。通过主机名&#xff0c;得到该主机名对应的IP地址的过程叫做域名解析。正向解析&#xff1a…...

Cadence Allegro 导出Assigned Functions Report详解

⏪《上一篇》   🏡《总目录》   ⏩《下一篇》 目录 1,概述2,Assigned Functions Report作用3,Assigned Functions Report示例4,Assigned Functions Report导出方法4.1,方法14.2,方法2B站关注“硬小二”浏览更多演示视频...

Python中Opencv和PIL.Image读取图片的差异对比

近日&#xff0c;在进行深度学习进行推理的时候&#xff0c;发现不管怎么样都得不出正确的结果&#xff0c;再仔细和正确的代码进行对比了后发现原来是Python中不同的库读取的图片数组是有差异的。 image np.array(Image.open(image_file).convert(RGB)) image cv2.imread(…...

win10 WSL2 使用Ubuntu配置与安装教程

Win10 22H2ubuntu 22.04ROS2 文章目录一、什么是WSL2二、Win10 系统配置2.1 更新Windows版本2.2 Win10系统启用两个功能2.3 Win10开启BIOS/CPU开启虚拟化(VT)&#xff08;很关键&#xff09;2.4 下载并安装wsl_update_x64.msi2.5 PowerShell安装组件三、PowerShell安装Ubuntu3.…...

LeetCode每日一题(28. Find the Index of the First Occurrence in a String)

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack “sadbutsad”, needle “sad” Output: 0 Explanation: “sad” occurs at index 0 and…...

Android 圆弧形 SeekBar

效果预览package com.gcssloop.widget;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graph…...

java 字典

java 字典 数据结构总览 Map Map 描述的是一种映射关系&#xff0c;一个 key 对应一个 value&#xff0c;可以添加&#xff0c;删除&#xff0c;修改和获取 key/value&#xff0c;util 提供了多种 Map HashMap: hash 表实现的 map&#xff0c;插入删除查找性能都是 O(1)&…...

【企业服务器LNMP环境搭建】mysql安装

MySQL安装步骤&#xff1a; 1、相关说明 1.1、编译参数的说明 -DCMAKE_INSTALL_PREFIX安装到的软件目录-DMYSQL_DATADIR数据文件存储的路径-DSYSCONFDIR配置文件路径 (my.cnf)-DENABLED_LOCAL_INFILE1使用localmysql客户端的配置-DWITH_PARTITION_STORAGE_ENGINE使mysql支持…...

cf2117E

原题链接&#xff1a;https://codeforces.com/contest/2117/problem/E 题目背景&#xff1a; 给定两个数组a,b&#xff0c;可以执行多次以下操作&#xff1a;选择 i (1 < i < n - 1)&#xff0c;并设置 或&#xff0c;也可以在执行上述操作前执行一次删除任意 和 。求…...

C++ 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

uniapp中使用aixos 报错

问题&#xff1a; 在uniapp中使用aixos&#xff0c;运行后报如下错误&#xff1a; AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...

【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统

目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索&#xff08;基于物理空间 广播范围&#xff09;2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...

.Net Framework 4/C# 关键字(非常用,持续更新...)

一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...

安全突围:重塑内生安全体系:齐向东在2025年BCS大会的演讲

文章目录 前言第一部分&#xff1a;体系力量是突围之钥第一重困境是体系思想落地不畅。第二重困境是大小体系融合瓶颈。第三重困境是“小体系”运营梗阻。 第二部分&#xff1a;体系矛盾是突围之障一是数据孤岛的障碍。二是投入不足的障碍。三是新旧兼容难的障碍。 第三部分&am…...

【JavaSE】多线程基础学习笔记

多线程基础 -线程相关概念 程序&#xff08;Program&#xff09; 是为完成特定任务、用某种语言编写的一组指令的集合简单的说:就是我们写的代码 进程 进程是指运行中的程序&#xff0c;比如我们使用QQ&#xff0c;就启动了一个进程&#xff0c;操作系统就会为该进程分配内存…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

SQL Server 触发器调用存储过程实现发送 HTTP 请求

文章目录 需求分析解决第 1 步:前置条件,启用 OLE 自动化方式 1:使用 SQL 实现启用 OLE 自动化方式 2:Sql Server 2005启动OLE自动化方式 3:Sql Server 2008启动OLE自动化第 2 步:创建存储过程第 3 步:创建触发器扩展 - 如何调试?第 1 步:登录 SQL Server 2008第 2 步…...

c# 局部函数 定义、功能与示例

C# 局部函数&#xff1a;定义、功能与示例 1. 定义与功能 局部函数&#xff08;Local Function&#xff09;是嵌套在另一个方法内部的私有方法&#xff0c;仅在包含它的方法内可见。 • 作用&#xff1a;封装仅用于当前方法的逻辑&#xff0c;避免污染类作用域&#xff0c;提升…...