【GiantPandaCV导语】计算Armv7a架构理论gflops以及自己写的某个算法的gflops的方法，另外提供了一个脚本可以显示native版矩阵乘法各个尺寸对应的gflops。

# 1. 前言¶

gflops代表计算量除以耗时获得的值。

# 2. Cortex-A53 硬件浮点峰值计算¶ #include <time.h>
#include <stdio.h>

#define LOOP (1e9)
#define OP_FLOATS (80)

void TEST(int);

static double get_time(struct timespec *start,
struct timespec *end) {
return end->tv_sec - start->tv_sec + (end->tv_nsec - start->tv_nsec) * 1e-9;
}

int main() {
struct timespec start, end;
double time_used = 0.0;

clock_gettime(CLOCK_MONOTONIC_RAW, &start);

TEST(LOOP);

clock_gettime(CLOCK_MONOTONIC_RAW, &end);

time_used = get_time(&start, &end);
printf("perf: %.6lf \r\n", LOOP * OP_FLOATS * 1.0 * 1e-9 / time_used);
}


.text
.align 5

.global TEST

TEST:

.loop2:
vmla.f32 q0, q0, q0
vmla.f32 q1, q1, q1
vmla.f32 q2, q2, q2
vmla.f32 q3, q3, q3
vmla.f32 q4, q4, q4
vmla.f32 q5, q5, q5
vmla.f32 q6, q6, q6
vmla.f32 q7, q7, q7
vmla.f32 q8, q8, q8
vmla.f32 q9, q9, q9

subs r0,r0,    #1
bne .loop2 # 3. 实现Native矩阵乘法，记录浮点峰值¶

#define A( i, j ) a[ (i)*lda + (j) ]
#define B( i, j ) b[ (i)*ldb + (j) ]
#define C( i, j ) c[ (i)*ldb + (j) ]
// gemm C = A * B + C
void MatrixMultiply(int m, int n, int k, float *a, int lda, float *b, int ldb, float *c, int ldc)
{
for(int i = 0; i < m; i++){
for (int j=0; j<n; j++ ){
for (int p=0; p<k; p++ ){
C(i, j) = C(i, j) + A(i, p) * B(p, j);
}
}
}
}


// i代表当前矩阵的长宽，长宽都等于i
for(int i = 40; i <= 800; i += 40){
m = i;
n = i;
k = i;
gflops = 2.0 * m * n * k * 1.0e-09;
lda = m;
ldb = n;
ldc = k;
a = (float *)malloc(lda * k * sizeof(float));
b = (float *)malloc(ldb * n * sizeof(float));
c = (float *)malloc(ldc * n * sizeof(float));
prec = (float *)malloc(ldc * n * sizeof(float));
nowc = (float *)malloc(ldc * n * sizeof(float));
// 随机填充矩阵
random_matrix(m, k, a, lda);
random_matrix(k, n, b, ldb);
random_matrix(m, n, prec, ldc);

memset(prec, 0, ldc * n * sizeof(float));

copy_matrix(m, n, prec, ldc, nowc, ldc);

// 以nowc为基准，判断矩阵运行算结果是否正确
MatrixMultiply(m, n, k, a, lda, b, ldb, nowc, ldc);

// 循环20次，以最快的运行时间为结果
for(int j=0; j < 20; j++){

copy_matrix(m, n, prec, ldc, c, ldc);

clock_gettime(CLOCK_MONOTONIC_RAW, &start);

MatrixMultiply(m, n, k, a, lda, b, ldb, c, ldc);

clock_gettime(CLOCK_MONOTONIC_RAW, &end);

time_tmp = get_time(&start, &end);

if(j == 0)
time_best = time_tmp;
else
time_best = min(time_best, time_tmp);
}

diff = compare_matrices(m, n, c, ldc, nowc, ldc);

if(diff > 0.5f || diff < -0.5f){
exit(0);
}

printf("%d %le %le \n", i, gflops / time_best, diff);
fflush(stdout);

free(a);
free(b);
free(c);
free(prec);
free(nowc);
}
printf("\n");
fflush(stdout);


import matplotlib.pyplot as plt
import numpy as np

def solve(filename):
f = open(filename)
sizes = 
times = [0.0]
title = 'origin'
while True:
if line:
slices = line.split(" ")
if len(slices) <= 2:
break;
size = int(slices)
time = float(slices)
sizes.append(size)
times.append(time)
return title, sizes, times

if __name__ == '__main__':
plt.xlabel('size')
plt.ylabel('gflops')
t, x, y = solve('now.txt')
plt.plot(x, y, label=t)
plt.legend()
plt.savefig('origin.png')
plt.show()
{"ft_lang_label":"__label__zh","ft_lang_prob":0.53842527,"math_prob":0.9994748,"size":4592,"snap":"2022-05-2022-21","text_gpt3_token_len":2591,"char_repetition_ratio":0.095466435,"word_repetition_ratio":0.028735632,"special_character_ratio":0.32600173,"punctuation_ratio":0.26237053,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994437,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-23T10:31:24Z\",\"WARC-Record-ID\":\"<urn:uuid:3d77582c-7ebf-4fa0-aca0-e19f0c28b662>\",\"Content-Length\":\"279809\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e0d1b794-2d4a-431f-bcfc-ac157b342b5d>\",\"WARC-Concurrent-To\":\"<urn:uuid:1b46e687-66a3-427a-9cea-a542fca578d5>\",\"WARC-IP-Address\":\"123.57.73.246\",\"WARC-Target-URI\":\"http://giantpandacv.com/project/%E9%83%A8%E7%BD%B2%E4%BC%98%E5%8C%96/AI%20%E7%A7%BB%E5%8A%A8%E7%AB%AF%E7%AE%97%E6%B3%95%E4%BC%98%E5%8C%96/%E5%A6%82%E4%BD%95%E5%88%A4%E6%96%AD%E7%AE%97%E6%B3%95%E6%98%AF%E5%90%A6%E6%9C%89%E5%8F%AF%E4%BC%98%E5%8C%96%E7%A9%BA%E9%97%B4%EF%BC%9F/\",\"WARC-Payload-Digest\":\"sha1:I75NQ3TREBS527X6VTPLDBUY4PK63NUZ\",\"WARC-Block-Digest\":\"sha1:P3A5XBUTRJAEKWNE32BNZCWF2H5VSFCB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662558015.52_warc_CC-MAIN-20220523101705-20220523131705-00426.warc.gz\"}"}