当前位置:网站首页>NEON优化1:软件性能优化、降功耗怎么搞?
NEON优化1:软件性能优化、降功耗怎么搞?
2022-06-27 05:24:00 【来知晓】
NEON优化1:软件性能优化、硬件降功耗怎么搞?
背景
为了移动端或嵌入式设备等场景也能用上前沿技术,产品往往会上一些复杂的算法模型,但由于算法开销过大,导致实时性差、功耗高
等问题,需要进行端侧的性能优化。
如何在不改变算法效果的前提下,降低算法代码的时间复杂度,成了许多工程师不得不面对的问题。
性能优化基础之MCPS和MIPS
首先,在进行性能优化前,应找到一个具体的性能衡量指标,即MIPS/MCPS。
- MIPS:million instructions per second,程序运行时每秒所耗费的指令数
- MCPS:million instructions per second,程序运行时每秒所耗费的周期数
MIPS和MCPS的区别
- MIPS是指令数,不同平台软仿和硬仿的差距不大
- MCPS是周期数,由于有硬件优化,可能不同平台会出现MCPS不同,甚至比MIPS还小。
一般软仿结果,MIPS都比MCPS小,因为软仿工具RVDS的CPI最小才能为1,硬仿结果能直接获得MCPS数。硬仿时,好的CPU能做到CPI小于1,即1个周期多指令,具体见:link。
With a single-execution-unit processor, the best CPI attainable is 1. However, with a multiple-execution-unit processor, one may achieve even better CPI values (CPI < 1).
MIPS计算DEMO
目录结构:
- src
- main.c
- void test(int* arr, int len);
- vpu.h
- vpu.s
- main.c
计算代码:
#include <stdio.h>
#define MIPS_COUNT_ARM_CORTEX
#ifdef MIPS_COUNT_ARM_CORTEX
#include "v7_pmu.h"
#endif
#ifdef MIPS_COUNT_ARM_CORTEX
#define MILLION_UNIT (1000000.f)
#define KILO_UNIT (1000.f)
#define FRAME_LEN_MS (10.f) // 10ms
#define COUNT_NUM 1000
unsigned int counter0;
unsigned int cycle_count1;
unsigned int cycle_count2;
unsigned int cur_time = 0;
long double cur_time_tmp = 0.0;
double avg_time = 0;
unsigned long avg_time_tmp = 0;
unsigned int peak_time = 0;
float cycle2mips_coef = (1 / MILLION_UNIT) / (FRAME_LEN_MS / KILO_UNIT); // unit: mips
#endif
void main(void) {
// set mannual
cnt = COUNT_NUM;
while(cnt--) {
#ifdef MIPS_COUNT_ARM_CORTEX
enable_pmu(); // Enable the PMU
reset_ccnt(); // Reset the CCNT (cycle counter)
reset_pmn(); // Reset the configurable counters
pmn_config(0, 0x03); // Configure counter 0 to count event code 0x03
enable_ccnt(); // Enable CCNT
enable_pmn(0); // Enable counter
counter0 = read_pmn(0); // Read counter 0
cycle_count1 = read_ccnt(); // Read Core cycle
#endif
// test();
#ifdef MIPS_COUNT_ARM_CORTEX
cycle_count2 = read_ccnt();
cur_time = cycle_count2 - cycle_count1;
// 10^6 => million cycle, *1000/frmeLms => second
cur_time_tmp = (float)cur_time * cycle2mips_coef; // mips
avg_time_tmp += (unsigned int)cur_time_tmp;
if (cur_time > peak_time) {
peak_time = cur_time;
}
printf("%.2f mips \n", cur_time_tmp);
#endif
}
#ifdef MIPS_COUNT_ARM_CORTEX
avg_time = (double)avg_time_tmp / COUNT_NUM;
printf("max %.2f mips \n", (float)peak_time * cycle2mips_coef);
printf("avg %.2f mips \n", avg_time);
#endif
}
计算开销的模块函数通常放到要测试开销的相关函数如test()
前后,即可得到该函数的单独MIPS开销,当然,也可以根据总体程序运行的开销乘相关函数所占开销比例得到,但计算不便,这里不推荐。
测试工具及流程
所需工具
软仿测试工具通常采用ARM公司的RVDS
(RealView Development Suite)开发套件,模拟各种内核处理进行仿真,得到开销数据。
硬仿测试工具通常是用Andriod平台自带的simpleperf
工具,将可执行文件直接推到手机上运行,实时抓取CPU数据得到实际开销数据,并绘制出图,俗称火焰图。
软硬仿优化流程
- 软仿流程
- 安装RVDS软件
- 配置代码工程环境
- 跑通代码
- 编写开销计算代码
- 仿真Profile
- 得到热点函数和开销基线
- 进行代码优化
- 测试热点函数开销
- 硬仿流程
- 与软仿流程类似
- 建议先软仿,再硬仿
- 涉及到IO读写等开销问题,软仿无法模拟实际运行情况,以硬仿结果为准
有了热点开销函数,就可以进行相关指令集及代码优化了。
小结
本文分享了性能优化的背景及基础概念,时间复杂度计算之MCPS和MIPS,以及测试工具和软硬仿流程。下篇分享NEON优化的案例与经验。
边栏推荐
- ES6 0622 III
- 014 C language foundation: C string
- 双位置继电器HJWS-9440
- 018 basics of C language: C file reading and writing
- leetcode-20. Valid parentheses -js version
- When STM32 turns off PWM output, it is a method to fix IO output at high or low level.
- Py2neo basic syntax
- Zener diode zener diode sod123 package positive and negative distinction
- Pycharm 中 Terminal 无法进入 venv 环境的问题
- neo4j community与neo4j desktop冲突
猜你喜欢
Redis high availability cluster (sentry, cluster)
neo4j图数据库基本概念
双位置继电器XJLS-8G/220
Terminal in pychar cannot enter the venv environment
快速排序(非遞歸)和歸並排序
QT using Valgrind to analyze memory leaks
LeetCode-515. 在每个树行中找最大值
【622. 设计循环队列】
Microservice system design -- unified authentication service design
竣达技术丨多品牌精密空调集中监控方案
随机推荐
Microservice system design -- unified authentication service design
AD22 gerber files 点开 gerber steup 界面 有问题 官方解决方法
Microservice system design -- Distributed timing service design
EPICS记录参考5 -- 数组模拟输入记录Array Analog Input (aai)
Microservice system design - service fusing and degradation design
C language implementation timer
006 C language foundation: C storage class
019 basics of C language: C preprocessing
Logu p4683 [ioi2008] type printer problem solving
微服务系统设计——服务熔断和降级设计
什么是BFC?有什么用?
three.js第一人称 相机前枪的跟随
认知篇----2022高考志愿该如何填报
Flink生产问题(1.10)
Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
[622. design cycle queue]
018 basics of C language: C file reading and writing
Machunmei, the first edition of principles and applications of database... Compiled final review notes
Chapter 1 Introduction
Microservice system design -- API gateway service design