From 33becb5afe85595d134d337496cdfb17a1e689a3 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:05:26 +0800 Subject: [PATCH 01/19] ADD file via upload --- 1.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 1.c diff --git a/1.c b/1.c new file mode 100644 index 0000000..cb92f06 --- /dev/null +++ b/1.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#define SIZE 10000000 + +void vectorAdd(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +int main() { + float* A = (float*)malloc(SIZE * sizeof(float)); + float* B = (float*)malloc(SIZE * sizeof(float)); + float* C = (float*)malloc(SIZE * sizeof(float)); + if (A == NULL || B == NULL || C == NULL) { + return 1; + } + + // float A[SIZE], B[SIZE], C[SIZE]; + srand((unsigned)time(NULL)); + int i; + for (i = 0; i < SIZE; ++i) { + A[i] = rand() % 100; + B[i] = rand() % 100; + } + + + clock_t start_time = clock(); + + vectorAdd(A, B, C, SIZE); + + clock_t end_time = clock(); + double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; + + printf("%f\n", time); + + // for (i = 0; i < SIZE; ++i) { + // printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]); + // } + + free(A); + free(B); + free(C); + return 0; +} From fb09e470e40f1de8034258769bb8b4b522467e2b Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:05:48 +0800 Subject: [PATCH 02/19] Delete '1.c' --- 1.c | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 1.c diff --git a/1.c b/1.c deleted file mode 100644 index cb92f06..0000000 --- a/1.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include - -#define SIZE 10000000 - -void vectorAdd(float* A, float* B, float* C, int size) { - int i; - for (i = 0; i < size; ++i) { - C[i] = A[i] + B[i]; - } -} - -int main() { - float* A = (float*)malloc(SIZE * sizeof(float)); - float* B = (float*)malloc(SIZE * sizeof(float)); - float* C = (float*)malloc(SIZE * sizeof(float)); - if (A == NULL || B == NULL || C == NULL) { - return 1; - } - - // float A[SIZE], B[SIZE], C[SIZE]; - srand((unsigned)time(NULL)); - int i; - for (i = 0; i < SIZE; ++i) { - A[i] = rand() % 100; - B[i] = rand() % 100; - } - - - clock_t start_time = clock(); - - vectorAdd(A, B, C, SIZE); - - clock_t end_time = clock(); - double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; - - printf("%f\n", time); - - // for (i = 0; i < SIZE; ++i) { - // printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]); - // } - - free(A); - free(B); - free(C); - return 0; -} From 640608f21bf537f545c416c9acfc8112a6439c44 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:07:13 +0800 Subject: [PATCH 03/19] ADD file via upload --- Yang | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Yang diff --git a/Yang b/Yang new file mode 100644 index 0000000..e69de29 From c7edb43f30e439990a3cccfda20f9970d1cd9745 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:07:44 +0800 Subject: [PATCH 04/19] Delete 'Yang' --- Yang | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Yang diff --git a/Yang b/Yang deleted file mode 100644 index e69de29..0000000 From ee583398c5dfd35f7e8c855632bf454662e1cbed Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:09:08 +0800 Subject: [PATCH 05/19] ADD file via upload --- Yang/1/txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Yang/1/txt diff --git a/Yang/1/txt b/Yang/1/txt new file mode 100644 index 0000000..e69de29 From 01c0a29004a48cd6b35cfefda7411f87b43459aa Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:09:18 +0800 Subject: [PATCH 06/19] Delete 'Yang/1/txt' --- Yang/1/txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Yang/1/txt diff --git a/Yang/1/txt b/Yang/1/txt deleted file mode 100644 index e69de29..0000000 From a654797eb3eb8339d204fb0f7970c30648c2cd2c Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:09:46 +0800 Subject: [PATCH 07/19] ADD file via upload --- Yang/1.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Yang/1.txt diff --git a/Yang/1.txt b/Yang/1.txt new file mode 100644 index 0000000..e69de29 From c651364527a4b114d94e3a5b32fb0230f757ac28 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:03 +0800 Subject: [PATCH 08/19] ADD file via upload --- Yang/1.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Yang/1.c diff --git a/Yang/1.c b/Yang/1.c new file mode 100644 index 0000000..cb92f06 --- /dev/null +++ b/Yang/1.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#define SIZE 10000000 + +void vectorAdd(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +int main() { + float* A = (float*)malloc(SIZE * sizeof(float)); + float* B = (float*)malloc(SIZE * sizeof(float)); + float* C = (float*)malloc(SIZE * sizeof(float)); + if (A == NULL || B == NULL || C == NULL) { + return 1; + } + + // float A[SIZE], B[SIZE], C[SIZE]; + srand((unsigned)time(NULL)); + int i; + for (i = 0; i < SIZE; ++i) { + A[i] = rand() % 100; + B[i] = rand() % 100; + } + + + clock_t start_time = clock(); + + vectorAdd(A, B, C, SIZE); + + clock_t end_time = clock(); + double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; + + printf("%f\n", time); + + // for (i = 0; i < SIZE; ++i) { + // printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]); + // } + + free(A); + free(B); + free(C); + return 0; +} From 543d934b579f51df9a7866456a1eddab14bb569e Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:09 +0800 Subject: [PATCH 09/19] Delete 'Yang/1.c' --- Yang/1.c | 48 ------------------------------------------------ 1 file changed, 48 deletions(-) delete mode 100644 Yang/1.c diff --git a/Yang/1.c b/Yang/1.c deleted file mode 100644 index cb92f06..0000000 --- a/Yang/1.c +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include - -#define SIZE 10000000 - -void vectorAdd(float* A, float* B, float* C, int size) { - int i; - for (i = 0; i < size; ++i) { - C[i] = A[i] + B[i]; - } -} - -int main() { - float* A = (float*)malloc(SIZE * sizeof(float)); - float* B = (float*)malloc(SIZE * sizeof(float)); - float* C = (float*)malloc(SIZE * sizeof(float)); - if (A == NULL || B == NULL || C == NULL) { - return 1; - } - - // float A[SIZE], B[SIZE], C[SIZE]; - srand((unsigned)time(NULL)); - int i; - for (i = 0; i < SIZE; ++i) { - A[i] = rand() % 100; - B[i] = rand() % 100; - } - - - clock_t start_time = clock(); - - vectorAdd(A, B, C, SIZE); - - clock_t end_time = clock(); - double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; - - printf("%f\n", time); - - // for (i = 0; i < SIZE; ++i) { - // printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]); - // } - - free(A); - free(B); - free(C); - return 0; -} From 77b46ac33b8774223829b637dc0adaa61ec6ef7a Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:18 +0800 Subject: [PATCH 10/19] ADD file via upload --- Yang/1.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Yang/1.c diff --git a/Yang/1.c b/Yang/1.c new file mode 100644 index 0000000..cb92f06 --- /dev/null +++ b/Yang/1.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#define SIZE 10000000 + +void vectorAdd(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +int main() { + float* A = (float*)malloc(SIZE * sizeof(float)); + float* B = (float*)malloc(SIZE * sizeof(float)); + float* C = (float*)malloc(SIZE * sizeof(float)); + if (A == NULL || B == NULL || C == NULL) { + return 1; + } + + // float A[SIZE], B[SIZE], C[SIZE]; + srand((unsigned)time(NULL)); + int i; + for (i = 0; i < SIZE; ++i) { + A[i] = rand() % 100; + B[i] = rand() % 100; + } + + + clock_t start_time = clock(); + + vectorAdd(A, B, C, SIZE); + + clock_t end_time = clock(); + double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; + + printf("%f\n", time); + + // for (i = 0; i < SIZE; ++i) { + // printf("A[%d] + B[%d] = C[%d] -> %f + %f = %f\n", i, i, i, A[i], B[i], C[i]); + // } + + free(A); + free(B); + free(C); + return 0; +} From 2a4356c164a65ee32782dd0ecc6b42453b7710c2 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:31 +0800 Subject: [PATCH 11/19] Delete 'Yang/1.txt' --- Yang/1.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Yang/1.txt diff --git a/Yang/1.txt b/Yang/1.txt deleted file mode 100644 index e69de29..0000000 From 15aad110a880b3404d947a13616566123b95fb86 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:47 +0800 Subject: [PATCH 12/19] ADD file via upload --- yang/2.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 yang/2.c diff --git a/yang/2.c b/yang/2.c new file mode 100644 index 0000000..900a1d6 --- /dev/null +++ b/yang/2.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + + +#define SIZE 10000000 + +void vectorAdd(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +void vectorAddNEON(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i <= size - 4; i += 4) { + float32x4_t vecA = vld1q_f32(&A[i]); + float32x4_t vecB = vld1q_f32(&B[i]); + + float32x4_t vecC = vaddq_f32(vecA, vecB); + vst1q_f32(&C[i], vecC); + } + for (; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +int main() { + float* A = (float*)malloc(SIZE * sizeof(float)); + float* B = (float*)malloc(SIZE * sizeof(float)); + float* C = (float*)malloc(SIZE * sizeof(float)); + if (A == NULL || B == NULL || C == NULL) { + return 1; + } + + srand((unsigned)time(NULL)); + for (int i = 0; i < SIZE; ++i) { + A[i] = (float)(rand() % 100); + B[i] = (float)(rand() % 100); + } + + clock_t start_time = clock(); + vectorAdd(A, B, C, SIZE); + clock_t end_time = clock(); + double time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("tradition: %f", time1); + + start_time = clock(); + vectorAddNEON(A, B, C, SIZE); + end_time = clock(); + time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("NEON %f",time1); + + free(A); + free(B); + free(C); +} From 24f3df94831d22e5b6a20222d4744ddc0fc8b5ae Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:10:57 +0800 Subject: [PATCH 13/19] Delete 'yang/2.c' --- yang/2.c | 59 -------------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 yang/2.c diff --git a/yang/2.c b/yang/2.c deleted file mode 100644 index 900a1d6..0000000 --- a/yang/2.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include - - -#define SIZE 10000000 - -void vectorAdd(float* A, float* B, float* C, int size) { - int i; - for (i = 0; i < size; ++i) { - C[i] = A[i] + B[i]; - } -} - -void vectorAddNEON(float* A, float* B, float* C, int size) { - int i; - for (i = 0; i <= size - 4; i += 4) { - float32x4_t vecA = vld1q_f32(&A[i]); - float32x4_t vecB = vld1q_f32(&B[i]); - - float32x4_t vecC = vaddq_f32(vecA, vecB); - vst1q_f32(&C[i], vecC); - } - for (; i < size; ++i) { - C[i] = A[i] + B[i]; - } -} - -int main() { - float* A = (float*)malloc(SIZE * sizeof(float)); - float* B = (float*)malloc(SIZE * sizeof(float)); - float* C = (float*)malloc(SIZE * sizeof(float)); - if (A == NULL || B == NULL || C == NULL) { - return 1; - } - - srand((unsigned)time(NULL)); - for (int i = 0; i < SIZE; ++i) { - A[i] = (float)(rand() % 100); - B[i] = (float)(rand() % 100); - } - - clock_t start_time = clock(); - vectorAdd(A, B, C, SIZE); - clock_t end_time = clock(); - double time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; - printf("tradition: %f", time1); - - start_time = clock(); - vectorAddNEON(A, B, C, SIZE); - end_time = clock(); - time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; - printf("NEON %f",time1); - - free(A); - free(B); - free(C); -} From ce916d2fe7c5b6a473cac985c60444d145cca338 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:06 +0800 Subject: [PATCH 14/19] ADD file via upload --- Yang/2.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Yang/2.c diff --git a/Yang/2.c b/Yang/2.c new file mode 100644 index 0000000..900a1d6 --- /dev/null +++ b/Yang/2.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + + +#define SIZE 10000000 + +void vectorAdd(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +void vectorAddNEON(float* A, float* B, float* C, int size) { + int i; + for (i = 0; i <= size - 4; i += 4) { + float32x4_t vecA = vld1q_f32(&A[i]); + float32x4_t vecB = vld1q_f32(&B[i]); + + float32x4_t vecC = vaddq_f32(vecA, vecB); + vst1q_f32(&C[i], vecC); + } + for (; i < size; ++i) { + C[i] = A[i] + B[i]; + } +} + +int main() { + float* A = (float*)malloc(SIZE * sizeof(float)); + float* B = (float*)malloc(SIZE * sizeof(float)); + float* C = (float*)malloc(SIZE * sizeof(float)); + if (A == NULL || B == NULL || C == NULL) { + return 1; + } + + srand((unsigned)time(NULL)); + for (int i = 0; i < SIZE; ++i) { + A[i] = (float)(rand() % 100); + B[i] = (float)(rand() % 100); + } + + clock_t start_time = clock(); + vectorAdd(A, B, C, SIZE); + clock_t end_time = clock(); + double time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("tradition: %f", time1); + + start_time = clock(); + vectorAddNEON(A, B, C, SIZE); + end_time = clock(); + time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("NEON %f",time1); + + free(A); + free(B); + free(C); +} From 8a75d23339d6a363b22e42e3fa10804fec02ff0d Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:15 +0800 Subject: [PATCH 15/19] ADD file via upload --- Yang/3.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Yang/3.c diff --git a/Yang/3.c b/Yang/3.c new file mode 100644 index 0000000..8ca0f13 --- /dev/null +++ b/Yang/3.c @@ -0,0 +1,52 @@ +#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]; + } + } + } +} + +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 start = clock(); + matmul(A, B, C, SIZE); + clock_t end = clock(); + double time = (double)(end - start) / CLOCKS_PER_SEC; + printf("%f\n", time); + + 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 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 16/19] 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 From 377d1355ad33a6fea3bdac3fee895b569304f269 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:33 +0800 Subject: [PATCH 17/19] ADD file via upload --- Yang/5(generate).c | 116 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Yang/5(generate).c diff --git a/Yang/5(generate).c b/Yang/5(generate).c new file mode 100644 index 0000000..f02c642 --- /dev/null +++ b/Yang/5(generate).c @@ -0,0 +1,116 @@ +#include +#include +#include + +typedef struct { + int* values; + int* rowIndex; + int* colIndex; + int nonZeroCount; +} SparseMatrix; + +void sparseMatmul(SparseMatrix* A, SparseMatrix* B, SparseMatrix* C) { + int currentIndex = 0; + int i, j; + for (i = 0; i < A->nonZeroCount; i++) + { + int rowA = A->rowIndex[i]; + int colA = A->colIndex[i]; + float valueA = A->values[i]; + for (j = 0; j < A->nonZeroCount; j++) + { + int rowB = B->rowIndex[j]; + int colB = B->colIndex[j]; + float valueB = B->values[j]; + if (colA == rowB) + { + float product = valueA * valueB; + int found = 0; + int k; + for (k = 0; k < currentIndex; k++) + { + if (C->rowIndex[k] == rowA && C->colIndex[k] == colB){ + C->values[k] += product; + found = 1; + break; + } + } + if (!found) + { + C->values[currentIndex] = product; + C->rowIndex[currentIndex] = rowA; + C->colIndex[currentIndex] = colB; + currentIndex++; + } + } + } + } + C->nonZeroCount = currentIndex; +} + +void generate(SparseMatrix* matrix, int rows, int cols, int nonZeroCount){ + matrix->values = (int*)malloc(sizeof(int) * nonZeroCount); + matrix->rowIndex = (int*)malloc(sizeof(int) * nonZeroCount); + matrix->colIndex = (int*)malloc(sizeof(int) * nonZeroCount); + matrix->nonZeroCount = nonZeroCount; + int i; + for (i = 0; i < nonZeroCount; i++) + { + matrix->rowIndex[i] = rand() % rows; + matrix->colIndex[i] = rand() % cols; + matrix->values[i] = rand() %100; + } +} + +void free_matrix(SparseMatrix* matrix) { + free(matrix->values); + free(matrix->rowIndex); + free(matrix->colIndex); +} + +int main() { + srand(time(NULL)); + + // int i; + int rowsA = 1000; + int rowsB = 2000; + int colsB = 1000; + int nonZeroCountA = 10000; + int nonZeroCountB = 10000; + + SparseMatrix A, B; + generate(&A, rowsA, rowsB, nonZeroCountA); + generate(&B, rowsB, colsB, nonZeroCountB); + + // for (i = 0; i < A.nonZeroCount; ++i) { + // printf("A[%d][%d] = %d\n", A.rowIndex[i], A.colIndex[i], A.values[i]); + // } + + // for (i = 0; i < B.nonZeroCount; ++i) { + // printf("B[%d][%d] = %d\n", B.rowIndex[i], B.colIndex[i], B.values[i]); + // } + + + SparseMatrix C; + C.values = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.rowIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.colIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.nonZeroCount = 0; + + clock_t start_time = clock(); + + sparseMatmul(&A, &B, &C); + + clock_t end_time = clock(); + double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("%f\n", time); + + // for (i = 0; i < C.nonZeroCount; ++i) { + // printf("C[%d][%d] = %d\n", C.rowIndex[i], C.colIndex[i], C.values[i]); + // } + + free_matrix(&A); + free_matrix(&B); + free_matrix(&C); + return 0; +} From f7824cb11a15c89fc392ed1b6bb1b3cac0c2acf1 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:40 +0800 Subject: [PATCH 18/19] ADD file via upload --- Yang/5.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Yang/5.c diff --git a/Yang/5.c b/Yang/5.c new file mode 100644 index 0000000..2e8bb33 --- /dev/null +++ b/Yang/5.c @@ -0,0 +1,96 @@ +#include +#include +#include + +typedef struct { + int* values; + int* rowIndex; + int* colIndex; + int nonZeroCount; +} SparseMatrix; + +void sparseMatmul(SparseMatrix* A, SparseMatrix* B, SparseMatrix* C) { + int currentIndex = 0; + int i, j; + for (i = 0; i < A->nonZeroCount; i++) + { + int rowA = A->rowIndex[i]; + int colA = A->colIndex[i]; + float valueA = A->values[i]; + for (j = 0; j < A->nonZeroCount; j++) + { + int rowB = B->rowIndex[j]; + int colB = B->colIndex[j]; + float valueB = B->values[j]; + if (colA == rowB) + { + float product = valueA * valueB; + int found = 0; + int k; + for (k = 0; k < currentIndex; k++) + { + if (C->rowIndex[k] == rowA && C->colIndex[k] == colB){ + C->values[k] += product; + found = 1; + break; + } + } + if (!found) + { + C->values[currentIndex] = product; + C->rowIndex[currentIndex] = rowA; + C->colIndex[currentIndex] = colB; + currentIndex++; + } + } + } + } + C->nonZeroCount = currentIndex; +} + +void free_matrix(SparseMatrix* matrix) { + free(matrix->values); + free(matrix->rowIndex); + free(matrix->colIndex); +} + +int main() { + SparseMatrix A = { + .values = (int[]){1, 2, 3, 4, 5}, + .rowIndex = (int[]){0, 0, 1, 2, 2}, + .colIndex = (int[]){0, 2, 1, 0, 2}, + .nonZeroCount = 5 + }; + + SparseMatrix B = { + .values = (int[]){6, 8, 7, 9}, + .rowIndex = (int[]){0, 2, 1, 2}, + .colIndex = (int[]){0, 0, 1, 2}, + .nonZeroCount = 4 + }; + + SparseMatrix C; + C.values = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.rowIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.colIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.nonZeroCount = 0; + + clock_t start_time = clock(); + + sparseMatmul(&A, &B, &C); + + clock_t end_time = clock(); + double time = (double)(end_time - start_time) / CLOCKS_PER_SEC; + printf("%f\n", time); + + int i; + for (i = 0; i < C.nonZeroCount; ++i) { + printf("C[%d][%d] = %d\n", C.rowIndex[i], C.colIndex[i], C.values[i]); + } + + free_matrix(&A); + free_matrix(&B); + free_matrix(&C); + + return 0; +} From 8934bf2ca67eb62c3ed8ae3c116a8799a3077538 Mon Sep 17 00:00:00 2001 From: pmfsq5yrv <1159971203@qq.com> Date: Sat, 23 Nov 2024 16:11:47 +0800 Subject: [PATCH 19/19] ADD file via upload --- Yang/6.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 Yang/6.c diff --git a/Yang/6.c b/Yang/6.c new file mode 100644 index 0000000..d35512f --- /dev/null +++ b/Yang/6.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include + +typedef struct { + int* values; + int* rowIndex; + int* colIndex; + int nonZeroCount; +} SparseMatrix; + +void sparseMatmul(SparseMatrix* A, SparseMatrix* B, SparseMatrix* C) { + int currentIndex = 0; + int i, j; + for (i = 0; i < A->nonZeroCount; i++) + { + int rowA = A->rowIndex[i]; + int colA = A->colIndex[i]; + float valueA = A->values[i]; + for (j = 0; j < A->nonZeroCount; j++) + { + int rowB = B->rowIndex[j]; + int colB = B->colIndex[j]; + float valueB = B->values[j]; + if (colA == rowB) + { + float product = valueA * valueB; + int found = 0; + int k; + for (k = 0; k < currentIndex; k++) + { + if (C->rowIndex[k] == rowA && C->colIndex[k] == colB){ + C->values[k] += product; + found = 1; + break; + } + } + if (!found) + { + C->values[currentIndex] = product; + C->rowIndex[currentIndex] = rowA; + C->colIndex[currentIndex] = colB; + currentIndex++; + } + } + } + } + C->nonZeroCount = currentIndex; +} + +void generate(SparseMatrix* matrix, int rows, int cols, int nonZeroCount){ + matrix->values = (int*)malloc(sizeof(int) * nonZeroCount); + matrix->rowIndex = (int*)malloc(sizeof(int) * nonZeroCount); + matrix->colIndex = (int*)malloc(sizeof(int) * nonZeroCount); + matrix->nonZeroCount = nonZeroCount; + int i; + for (i = 0; i < nonZeroCount; i++) + { + matrix->rowIndex[i] = rand() % rows; + matrix->colIndex[i] = rand() % cols; + matrix->values[i] = rand() %100; + } +} + +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); + } + } +} + +void free_matrix(SparseMatrix* matrix) { + free(matrix->values); + free(matrix->rowIndex); + free(matrix->colIndex); +} + +int main() { + srand(time(NULL)); + + // int i; + int rowsA = 1000; + int rowsB = 2000; + int colsB = 1000; + int nonZeroCountA = 10000; + int nonZeroCountB = 10000; + + SparseMatrix A, B; + generate(&A, rowsA, rowsB, nonZeroCountA); + generate(&B, rowsB, colsB, nonZeroCountB); + + // for (i = 0; i < A.nonZeroCount; ++i) { + // printf("A[%d][%d] = %d\n", A.rowIndex[i], A.colIndex[i], A.values[i]); + // } + + // for (i = 0; i < B.nonZeroCount; ++i) { + // printf("B[%d][%d] = %d\n", B.rowIndex[i], B.colIndex[i], B.values[i]); + // } + + int i; + float** matrixA = (float**)malloc(rowsA * rowsB * sizeof(float*)); + float** matrixB = (float**)malloc(rowsB * colsB * sizeof(float*)); + float** matrixC = (float**)malloc(rowsA * colsB * sizeof(float*)); + for (i = 0; i < nonZeroCountA; i++) + { + int row = A.rowIndex[i]; + int col = A.colIndex[i]; + matrixA[row][col] = A.values[i]; + } + for (i = 0; i < nonZeroCountB; i++) + { + int row = B.rowIndex[i]; + int col = B.colIndex[i]; + matrixA[row][col] = B.values[i]; + } + + + + SparseMatrix C; + C.values = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.rowIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.colIndex = (int*)malloc(A.nonZeroCount * B.nonZeroCount * sizeof(int)); + C.nonZeroCount = 0; + + clock_t start_time1 = clock(); + sparseMatmul(&A, &B, &C); + clock_t end_time1 = clock(); + double time1 = (double)(end_time1 - start_time1) / CLOCKS_PER_SEC; + printf("sparseMatrix %f\n", time1); + + clock_t start_time2 = clock(); + matmulNEON(matrixA, matrixB, matrixC, rowsA * colsB) + clock_t end_time2 = clock(); + double time2 = (double)(end_time2 - start_time2) / CLOCKS_PER_SEC; + printf("sparseMatrix %f\n", time2); + + + // for (i = 0; i < C.nonZeroCount; ++i) { + // printf("C[%d][%d] = %d\n", C.rowIndex[i], C.colIndex[i], C.values[i]); + // } + + free_matrix(&A); + free_matrix(&B); + free_matrix(&C); + return 0; +}