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.

70 lines
1.8 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <arm_neon.h>
#define SIZE 1024
// NEON<4F>Ż<EFBFBD><C5BB>ij<EFBFBD><C4B3>ܾ<EFBFBD><DCBE><EFBFBD><EFBFBD>˷<EFBFBD><CBB7><EFBFBD><EFBFBD><EFBFBD>
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.0);
for (int k = 0; k < n; k += 4) {
float32x4_t a = vld1q_f32(A[i] + k);
float32x4_t b = vld1q_f32(B[k] + j);
sum = vmlaq_f32(sum, a, b);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۼӵ<DBBC>C[i][j]
float* sumPtr = (float*)&sum;
for (int l = 0; l < 4; l++) {
C[i][j] += sumPtr[l];
}
}
}
}
int main() {
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ沢<DAB4><E6B2A2>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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));
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>A<EFBFBD><41>B
srand((unsigned int)time(NULL));
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
}
// <20><>ʱ<EFBFBD><CAB1>ʼ
clock_t start = clock();
// ִ<><D6B4>NEON<4F>Ż<EFBFBD><C5BB>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD>˷<EFBFBD>
matmul_optimized(A, B, C, SIZE);
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
clock_t end = clock();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
printf("NEON<EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><EFBFBD><EFBFBD>ʱ: %lf <20><><EFBFBD><EFBFBD>\n", 1000.0 * (end - start) / CLOCKS_PER_SEC);
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
for (int i = 0; i < SIZE; i++) {
free(A[i]);
free(B[i]);
free(C[i]);
}
free(A);
free(B);
free(C);
return 0;
}