#include #include #include #include #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 sum = 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]); sum = vmlaq_f32(sum, vecA, vecB); } C[i][j] = vgetq_lane_f32(sum, 0) + vgetq_lane_f32(sum, 1) + vgetq_lane_f32(sum, 2) + vgetq_lane_f32(sum, 3); // 提取结果 } } } int main() { float** A = (float**)malloc(SIZE * sizeof(float*)); float** B = (float**)malloc(SIZE * sizeof(float*)); float** C = (float**)malloc(SIZE * sizeof(float*)); for (int i = 0; i < SIZE; i++) { A[i] = (float*)malloc(SIZE * sizeof(float)); B[i] = (float*)malloc(SIZE * sizeof(float)); C[i] = (float*)malloc(SIZE * sizeof(float)); } for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; 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(); double time_taken = (double)(end - start) / CLOCKS_PER_SEC; printf("优化后的稠密矩阵乘法: %f seconds\n", time_taken); for (int i = 0; i < SIZE; i++) { free(A[i]); free(B[i]); free(C[i]); } free(A); free(B); free(C); return 0; }