You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}