|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <ctime>
|
|
|
|
|
|
#include <arm_neon.h>
|
|
|
|
|
|
|
|
|
#define SIZE 1024
|
|
|
|
|
|
|
|
|
void matmul_optimized(float** A, float** B, float** C, int n) {
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
for (int j = 0; j < n; j++) {
|
|
|
float32x4_t vecC = vdupq_n_f32(0);
|
|
|
for (int k = 0; k < n; k += 4) {
|
|
|
|
|
|
float32x4_t vecA = vld1q_f32(&A[i][k]);
|
|
|
|
|
|
float32x4_t vecB = vld1q_f32(&B[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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
float** A = (float**)malloc(SIZE * sizeof(float*));
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
A[i] = (float*)malloc(SIZE * sizeof(float));
|
|
|
}
|
|
|
|
|
|
float** B = (float**)malloc(SIZE * sizeof(float*));
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
B[i] = (float*)malloc(SIZE * sizeof(float));
|
|
|
}
|
|
|
|
|
|
float** C = (float**)malloc(SIZE * sizeof(float*));
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
C[i] = (float*)malloc(SIZE * sizeof(float));
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
for (int j = 0; j < SIZE; j++) {
|
|
|
A[i][j] = (float)(rand() % 100);
|
|
|
B[i][j] = (float)(rand() % 100);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
clock_t start_time, end_time;
|
|
|
|
|
|
start_time = clock();
|
|
|
|
|
|
|
|
|
matmul_optimized(A, B, C, SIZE);
|
|
|
|
|
|
|
|
|
end_time = clock();
|
|
|
|
|
|
|
|
|
double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
|
|
|
printf("NEON优化矩阵乘法的运行时间:%f 秒\n", elapsed_time);
|
|
|
|
|
|
|
|
|
for (int i = 0; i < SIZE; i++) {
|
|
|
free(A[i]);
|
|
|
free(B[i]);
|
|
|
free(C[i]);
|
|
|
}
|
|
|
free(A);
|
|
|
free(B);
|
|
|
free(C);
|
|
|
|
|
|
return 0;
|
|
|
} |