为了移动端或嵌入式设备等场景也能用上前沿技术,产品往往会上一些复杂的算法模型,但由于算法开销过大,导致实时性差、功耗高等问题,需要进行端侧的性能优化。
如何在不改变算法效果的前提下,降低算法代码的时间复杂度,成了许多工程师不得不面对的问题。
首先,在进行性能优化前,应找到一个具体的性能衡量指标,即MIPS/MCPS。
MIPS和MCPS的区别
一般软仿结果,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
目录结构:
计算代码:
#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数据得到实际开销数据,并绘制出图,俗称火焰图。
软硬仿优化流程
有了热点开销函数,就可以进行相关指令集及代码优化了。
本文分享了性能优化的背景及基础概念,时间复杂度计算之MCPS和MIPS,以及测试工具和软硬仿流程。下篇分享NEON优化的案例与经验。