首页 > 其他分享 >test cpu performance with matrix multiplication

test cpu performance with matrix multiplication

时间:2022-12-01 17:06:08浏览次数:55  
标签:matrix thread int multiplication test performance cpu


一 需求:

测试cpu计算性能

二 方法:

1.使用一定规模方阵执行乘法运算,不需要保存结果。

2.根据CPU核数开启线程执行乘法运算

3.事先将线程执行任务放入线程对应的任务容器,然后开启线程,统计时间

4.采用cpu绑定,程序没有加锁,几乎没有系统开销。

三 代码

1.cpu_binding.hpp

#ifndef CPU_BINDING_HPP_
#define CPU_BINDING_HPP_
#include <pthread.h>
#include <thread>
using namespace std;
class cpu_binding {
public:
static cpu_binding &get_instance() {
static cpu_binding cpu_binding_;
return cpu_binding_;
}
inline int get_cpu_num() const {
return cpu_num_;
}
bool binding(int cpu) {
if (cpu < 0) {
return false;
}
cpu = (cpu + 1) % cpu_num_;
if (0 == cpu) {
return true;
}
cpu_set_t cpu_set = {0};
CPU_SET(cpu, &cpu_set);
return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set) >= 0;
}
private:
cpu_binding() {
cpu_num_ = thread::hardware_concurrency();
}
virtual ~cpu_binding() = default;
private:
int cpu_num_;
};

#endif /* CPU_BINDING_HPP_ */

2.cpu_performance_test.hpp

#ifndef CPU_PERFORMANCE_TEST_HPP_
#define CPU_PERFORMANCE_TEST_HPP_
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <functional>
#include "cpu_binding.hpp"
using namespace std;
struct coordinate {
coordinate(int a = 0, int b = 0) : row(a), col(b) {
}
int row;
int col;
};
class cpu_performance_test {
public:
static inline cpu_performance_test &get_instance() {
static cpu_performance_test test;
return test;
}
inline void set_matrix_dimension(int d) {
matrix_dimension_ = d;
}
void init() {
init_square_matrix(square_matrix0_);
init_square_matrix(square_matrix1_);
init_coordinate_array();
init_threads();
}
private:
cpu_performance_test() {
thread_num_ = thread::hardware_concurrency();
matrix_dimension_ = 100;
}
cpu_performance_test(const cpu_performance_test &) = delete;
cpu_performance_test & operator = (const cpu_performance_test &) = delete;
virtual ~cpu_performance_test() {
join_threads();
}
private:
void init_square_matrix(vector<vector<int> >&square_matrix) {
int number = 0;
vector<int>matrix_line;
random_device rd;
matrix_line.reserve(matrix_dimension_);
cout << "square matrix as following:" << endl;
cout << "===============================================" << endl;
for (int i = 0;i < matrix_dimension_;i++) {
for (int j = 0;j < matrix_dimension_;j++) {
number = rd() % 1888;
matrix_line.emplace_back(number);
cout << number << " ";
}
square_matrix.emplace_back(matrix_line);
matrix_line.clear();
cout << endl;
}
cout << "===============================================" << endl;
}
void init_coordinate_array() {
corrdinate_array_.resize(thread_num_);
int loop = 0;
for (int i = 0;i < matrix_dimension_;i++) {
for (int j = 0;j < matrix_dimension_;j++) {
corrdinate_array_[loop].emplace_back(coordinate(i, j));
cout << "thread id = " << loop << endl;
cout << "thread task data is " << "(" << i << "," << j << ")" << endl;
loop = (loop + 1) % thread_num_;

}
}
}
void init_threads() {
cout << "thread number = " << thread_num_ << endl;
threads_.resize(thread_num_);
int thread_id = 0;
now_ = std::chrono::steady_clock::now();
for (auto &thread_obj : threads_) {
thread_obj = thread(bind(&cpu_performance_test::thread_compute, this, thread_id++));
}
}
void join_threads() {
for (auto &thread_obj : threads_) {
if (thread_obj.joinable()) {
thread_obj.join();
}
}
auto end_time = chrono::steady_clock::now();
chrono::duration<double>time_span = chrono::duration_cast<chrono::duration<double>>(end_time - now_);
cout << "elapse time = " << time_span.count() << " seconds." << endl;
}
void thread_compute(int thread_id) {
cpu_binding::get_instance().binding(thread_id);
const auto vec = corrdinate_array_[thread_id];
int sum = 0;
for (auto &coordinate_obj : vec) {
for (int i = 0;i < matrix_dimension_;i++) {
sum += square_matrix0_[coordinate_obj.row][i] * square_matrix1_[i][coordinate_obj.col];
}
sum = 0;
}
}
private:
int thread_num_;
int matrix_dimension_;
private:
vector<vector<int> >square_matrix0_;
vector<vector<int> >square_matrix1_;
vector<vector<coordinate>>corrdinate_array_;
vector<thread>threads_;
private:
chrono::steady_clock::time_point now_;
};

#endif /* CPU_PERFORMANCE_TEST_HPP_ */

3.main.cpp

#include "cpu_performance_test.hpp"
int main() {
cpu_performance_test::get_instance().init();

return 0;
}

4.make.sh

g++ -std=c++17 -g -o Test main.cpp cpu_performance_test.hpp cpu_binding.hpp -pthread

 

标签:matrix,thread,int,multiplication,test,performance,cpu
From: https://blog.51cto.com/u_15899033/5902959

相关文章

  • Learn by testing: Unix socket 如何通信?
    本文参考communicatewithunixsockets做了一些简单的测试,了解unixsocket是如何通信的。创建一个tcpsocket创建一个unixsocket,命令不会返回,会一直等待:#nc-U/tmp......
  • test
    图片上传......
  • .NET6之MiniAPI(二十九):UnitTest
    MiniAPI的单元测试与asp.netweb api的单元测试大体是相同的(毕竟都是asp.netcore),只是在小细节上有一些差异,文章中会说到这点。本文测试框架是XUnit,Mock框架是Moq,......
  • .NET6之MiniAPI(二十九):UnitTest
    MiniAPI的单元测试与asp.netweb api的单元测试大体是相同的(毕竟都是asp.netcore),只是在小细节上有一些差异,文章中会说到这点。本文测试框架是XUnit,Mock框架是Mo......
  • .NET6之MiniAPI(二十九):UnitTest
    MiniAPI的单元测试与asp.netweb api的单元测试大体是相同的(毕竟都是asp.netcore),只是在小细节上有一些差异,文章中会说到这点。本文测试框架是XUnit,Mock框架是Mo......
  • Pytest - 使用pytest-xdsit 插件运行后 logging 模块日志不会输出的问题
    背景:自己写的日志打印模块,用pytest-n=auto后日志就不会输出#tools.set_loggging.pyimportlogging.handlersimportsysfromconcurrent_log_handlerimportConc......
  • pytest + loguru + allure 生成的报告没有log信息
    背景前段时间一直认为loguru这个库很不错,并且应用到了项目中,生成的日志文件也非常好看但是最后的allure报告中和html报告中都没有log信息。然就是各种查查:loguru作者说l......
  • mybatis 中 if-test 判断
    之前用都是判断参数是否为空之类的,今天要判断等于一个字符,直接写等于号反而会没有执行直接跳过,后来上网查阅了资料才知道原因是:mybatis是用OGNL表达式来解析的,在OGNL的表......
  • pytest + yaml 框架 -6.hooks 钩子功能实现
    前言在发送请求的时候,我们希望在发送请求参数前,带上签名的值,或者返回的内容需要二次处理,解密后返回。此功能我们可以用hooks钩子来实现pip安装插件pipinstallpyte......
  • 火山引擎 DataTester 智能运营,帮企业实现“千人千面”精准营销
    今天,越来越多的企业与商家把营销的目光聚焦于线上,并不断在精细化运营和精细化流量上投入,耕耘精准营销。然而,想要做好精准营销难度不小,首要维度是客户精准,要能对客户属性有......