ADD file via upload

main
p8sljnpht 4 days ago
parent d3edcf589a
commit 5d750743a6

@ -0,0 +1,59 @@
#include <arm_neon.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 1024
void matmul_optimized(float** A, float** B, float** C, int n);
int main() {
int n = SIZE;
// 分配内存空间
float** A = (float**)malloc(sizeof(float*) * n);
float** B = (float**)malloc(sizeof(float*) * n);
float** C = (float**)malloc(sizeof(float*) * n);
int i, j;
for(i=0; i<n; i++) {
A[i] = (float*)malloc(sizeof(float) * n);
B[i] = (float*)malloc(sizeof(float) * n);
C[i] = (float*)malloc(sizeof(float) * n);
}
// 随机化
srand(time(0));
for(i=0; i<n; i++)
for(j=0; j<n; j++) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
// 计算并计时
clock_t start = clock();
matmul_optimized(A, B, C, SIZE);
clock_t end = clock();
printf("使用 NEON 优化稠密矩阵乘法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
// 释放内存空间
for(i=0; i<n; i++) {
free(A[i]);
free(B[i]);
free(C[i]);
}
free(A); free(B); free(C);
}
void matmul_optimized(float** A, float** B, float** C, int n) {
int i, j, k;
float32x4_t vecA, vecB, vecC;
for(i=0; i<n; i++)
for(j=0; j<n; j++) {
vecC = vdupq_n_f32(0.0);
for(k=0; k<n; k+=4) {
vecA = vld1q_f32(&A[i][k]);
vecB = vld1q_f32(&A[k][j]);
vecC = vmlaq_f32(vecC, vecA, vecB);
}
C[i][j] = vgetq_lane_f32(vecC, 0) + vgetq_lane_f32(vecC, 1) +
vgetq_lane_f32(vecC, 2) + vgetq_lane_f32(vecC, 3);
}
}
Loading…
Cancel
Save