From b0613cfd234abc133f08cb834ae7a2960a2498ca Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:23 +0800 Subject: [PATCH] ADD file via upload --- Yang/4.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Yang/4.c diff --git a/Yang/4.c b/Yang/4.c new file mode 100644 index 0000000..0045c76 --- /dev/null +++ b/Yang/4.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include + +#define SIZE 1024 + +void matmul(float** A, float** B, float** C, int n) { + int i, j, k; + for (i = 0; i < n; ++i) { + for (j = 0; j < n; ++j) { + C[i][j] = 0; + for (k = 0; k < n; ++k) { + C[i][j] += A[i][k] * B[k][j]; + } + } + } +} + +void matmulNEON(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 = vmovq_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*)); + float** B = (float**)malloc(SIZE * sizeof(float*)); + float** C = (float**)malloc(SIZE * sizeof(float*)); + int i, j; + for (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)); + } + + srand(time(0)); + for (i = 0; i < SIZE; ++i) { + for (j = 0; j < SIZE; ++j) { + A[i][j] = rand() % 100; + B[i][j] = rand() % 100; + } + } + clock_t start1 = clock(); + matmul(A, B, C, SIZE); + clock_t end1 = clock(); + double time1 = (double)(end1 - start1) / CLOCKS_PER_SEC; + printf("tradition %f\n", time1); + + clock_t start2 = clock(); + matmulNEON(A, B, C, SIZE); + clock_t end2 = clock(); + double time2 = (double)(end2 - start2) / CLOCKS_PER_SEC; + printf("NEON %f\n", time2); + + for (i = 0; i < SIZE; i++) { + free(A[i]); + free(B[i]); + free(C[i]); + } + free(A); + free(B); + free(C); + + return 0; +} \ No newline at end of file