forked from pi7mcrg2k/operator_optimization
Merge pull request 'Problem 1-6' (#1) from p8sljnpht/operator_optimization:main into main
commit
6c1d167b8b
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#define SIZE 1024
|
||||
|
||||
void vector_add_optimized(float* A, float* B, float* C, int size);
|
||||
int main() {
|
||||
float A[SIZE], B[SIZE], C[SIZE];
|
||||
int i;
|
||||
srand(time(0));
|
||||
for(i=0; i<SIZE; i++) {
|
||||
A[i] = rand() % 100;
|
||||
B[i] = rand() % 100;
|
||||
}
|
||||
|
||||
clock_t start = clock();
|
||||
vector_add_optimized(A, B, C, SIZE);
|
||||
clock_t end = clock();
|
||||
printf("初始向量加法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
}
|
||||
|
||||
void vector_add_optimized(float* A, float* B, float* C, int size) {
|
||||
int i;
|
||||
for(i=0; i<size; i++) {
|
||||
C[i] = A[i] + B[i];
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#include <arm_neon.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#define SIZE 1024
|
||||
|
||||
void vector_add_optimized(float* A, float* B, float* C, int size);
|
||||
int main() {
|
||||
float A[SIZE], B[SIZE], C[SIZE];
|
||||
int i;
|
||||
srand(time(0));
|
||||
for(i=0; i<SIZE; i++) {
|
||||
A[i] = rand() % 100;
|
||||
B[i] = rand() % 100;
|
||||
}
|
||||
|
||||
clock_t start = clock();
|
||||
vector_add_optimized(A, B, C, SIZE);
|
||||
clock_t end = clock();
|
||||
printf("使用NEON 优化向量加法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
}
|
||||
|
||||
void vector_add_optimized(float* A, float* B, float* C, int size) {
|
||||
int i;
|
||||
for(i=0; i<size; 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#define SIZE 1024
|
||||
|
||||
void matmul_optimized(float** A, float** B, float** C, int n);
|
||||
int main() {
|
||||
int n = SIZE;
|
||||
|
||||
// 分配内存空间
|
||||
float** A = (float**)malloc(sizeof(float*) * n);
|
||||
float** B = (float**)malloc(sizeof(float*) * n);
|
||||
float** C = (float**)malloc(sizeof(float*) * n);
|
||||
int i, j;
|
||||
for(i=0; i<n; i++) {
|
||||
A[i] = (float*)malloc(sizeof(float) * n);
|
||||
B[i] = (float*)malloc(sizeof(float) * n);
|
||||
C[i] = (float*)malloc(sizeof(float) * n);
|
||||
}
|
||||
|
||||
// 随机化
|
||||
srand(time(0));
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<n; 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();
|
||||
printf("初始向量乘法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
|
||||
// 释放内存空间
|
||||
for(i=0; i<n; i++) {
|
||||
free(A[i]);
|
||||
free(B[i]);
|
||||
free(C[i]);
|
||||
}
|
||||
free(A); free(B); free(C);
|
||||
}
|
||||
|
||||
void matmul_optimized(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];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
#include <arm_neon.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#define SIZE 1024
|
||||
|
||||
void matmul_optimized(float** A, float** B, float** C, int n);
|
||||
int main() {
|
||||
int n = SIZE;
|
||||
|
||||
// 分配内存空间
|
||||
float** A = (float**)malloc(sizeof(float*) * n);
|
||||
float** B = (float**)malloc(sizeof(float*) * n);
|
||||
float** C = (float**)malloc(sizeof(float*) * n);
|
||||
int i, j;
|
||||
for(i=0; i<n; i++) {
|
||||
A[i] = (float*)malloc(sizeof(float) * n);
|
||||
B[i] = (float*)malloc(sizeof(float) * n);
|
||||
C[i] = (float*)malloc(sizeof(float) * n);
|
||||
}
|
||||
|
||||
// 随机化
|
||||
srand(time(0));
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<n; 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();
|
||||
printf("使用 NEON 优化稠密矩阵乘法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
|
||||
// 释放内存空间
|
||||
for(i=0; i<n; i++) {
|
||||
free(A[i]);
|
||||
free(B[i]);
|
||||
free(C[i]);
|
||||
}
|
||||
free(A); free(B); free(C);
|
||||
}
|
||||
|
||||
void matmul_optimized(float** A, float** B, float** C, int n) {
|
||||
int i, j, k;
|
||||
float32x4_t vecA, vecB, vecC;
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<n; j++) {
|
||||
vecC = vdupq_n_f32(0.0);
|
||||
for(k=0; k<n; k+=4) {
|
||||
vecA = vld1q_f32(&A[i][k]);
|
||||
vecB = vld1q_f32(&A[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);
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#define SIZE 1024
|
||||
|
||||
void sparce_matmul_coo(float*, int*, int*, int,
|
||||
float*, int*, int*, int, float*, int*, int*, int*);
|
||||
int main() {
|
||||
// 矩阵 A 的 COO 格式
|
||||
float A_values[] = {1, 2, 3, 4, 5};
|
||||
int A_rowIndex[] = {0, 0, 1, 2, 2};
|
||||
int A_colIndex[] = {0, 2, 1, 0, 2};
|
||||
int A_nonZeroCount = 5;
|
||||
// 矩阵 B 的 Coo 格式
|
||||
float B_values[] = {6, 8, 7, 9};
|
||||
int B_rowIndex[] = {0, 2, 1, 2};
|
||||
int B_colIndex[] = {0, 0, 1, 2};
|
||||
int B_nonZeroCount = 4;// 结果矩阵 C 的 Coo 格式
|
||||
float C_values[SIZE];
|
||||
int C_rowIndex[SIZE];
|
||||
int C_colIndex[SIZE];
|
||||
int C_nonZeroCount = 0;
|
||||
|
||||
clock_t start = clock();
|
||||
sparce_matmul_coo(A_values, A_rowIndex, A_colIndex, A_nonZeroCount,
|
||||
B_values, B_rowIndex, B_colIndex, B_nonZeroCount,
|
||||
C_values, C_rowIndex, C_colIndex, &C_nonZeroCount);
|
||||
clock_t end = clock();
|
||||
printf("基础的稀疏矩阵乘法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
}
|
||||
|
||||
void sparce_matmul_coo(float* A_values, int* A_rowIndex, int* A_colIndex, int A_nonZeroCount,
|
||||
float* B_values, int* B_rowIndex, int* B_colIndex, int B_nonZeroCount,
|
||||
float* C_values, int* C_rowIndex, int* C_colIndex, int* C_nonZeroCount) {
|
||||
int currentIndex = 0;
|
||||
int i, j, k;
|
||||
int rowA, colA, rowB, colB;
|
||||
float valueA, valueB, product;
|
||||
// 遍历 A 的非零元素
|
||||
for(i=0; i<A_nonZeroCount; i++) {
|
||||
rowA = A_rowIndex[i];
|
||||
colA = A_colIndex[i];
|
||||
valueA = A_values[i];
|
||||
// 遍历 B 的非零元素
|
||||
for(j=0; j<B_nonZeroCount; j++) {
|
||||
rowB = B_rowIndex[j];
|
||||
colB = B_colIndex[j];
|
||||
valueB = B_values[j];
|
||||
// 如果 A 的列和 B 的行匹配,则计算乘积并存储结果
|
||||
if (colA == rowB) {
|
||||
product = valueA * valueB;
|
||||
// 检查是否已有此(rowA, colB) 项
|
||||
int found = 0;
|
||||
for(k=0; k<currentIndex; k++) {
|
||||
if(C_rowIndex[k] == rowA && C_colIndex[k] == colB) {
|
||||
C_values[k] += product;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
C_values[currentIndex] = product;
|
||||
C_rowIndex[currentIndex] = rowA;
|
||||
C_colIndex[currentIndex] = colB;
|
||||
currentIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*C_nonZeroCount = currentIndex;
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
#include <arm_neon.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define SIZE 1024
|
||||
#define DENSE_MATRIX_SIZE 5
|
||||
|
||||
float** sparce_matmul_coo(float* A_values, int* A_rowIndex, int* A_colIndex, int A_nonZeroCount,
|
||||
float* B_values, int* B_rowIndex, int* B_colIndex, int B_nonZeroCount);
|
||||
void matmul_optimized(float** A, float** B, float** C, int n);
|
||||
void print_matrix(float** m, int rows, int cols);
|
||||
int main() {
|
||||
// 矩阵 A 的 COO 格式
|
||||
float A_values[] = {1, 2, 3, 4, 5};
|
||||
int A_rowIndex[] = {0, 0, 1, 2, 2};
|
||||
int A_colIndex[] = {0, 2, 1, 0, 2};
|
||||
int A_nonZeroCount = 5;
|
||||
// 矩阵 B 的 Coo 格式
|
||||
float B_values[] = {6, 8, 7, 9};
|
||||
int B_rowIndex[] = {0, 2, 1, 2};
|
||||
int B_colIndex[] = {0, 0, 1, 2};
|
||||
int B_nonZeroCount = 4;// 结果矩阵 C 的 Coo 格式
|
||||
float C_values[SIZE];
|
||||
int C_rowIndex[SIZE];
|
||||
int C_colIndex[SIZE];
|
||||
int C_nonZeroCount = 0;
|
||||
|
||||
clock_t start = clock();
|
||||
float** ans = sparce_matmul_coo(A_values, A_rowIndex, A_colIndex, A_nonZeroCount,
|
||||
B_values, B_rowIndex, B_colIndex, B_nonZeroCount);
|
||||
clock_t end = clock();
|
||||
printf("优化的稀疏矩阵乘法时间:%lf\n", (double)(end-start) / CLOCKS_PER_SEC);
|
||||
print_matrix(ans, DENSE_MATRIX_SIZE, DENSE_MATRIX_SIZE);
|
||||
}
|
||||
|
||||
float** sparce_matmul_coo(float* A_values, int* A_rowIndex, int* A_colIndex, int A_nonZeroCount,
|
||||
float* B_values, int* B_rowIndex, int* B_colIndex, int B_nonZeroCount) {
|
||||
// 分配内存空间
|
||||
float** A = (float**)malloc(sizeof(float*) * DENSE_MATRIX_SIZE);
|
||||
float** B = (float**)malloc(sizeof(float*) * DENSE_MATRIX_SIZE);
|
||||
float** C = (float**)malloc(sizeof(float*) * DENSE_MATRIX_SIZE);
|
||||
int i;
|
||||
for(i=0; i<DENSE_MATRIX_SIZE; i++) {
|
||||
A[i] = (float*)malloc(sizeof(float) * DENSE_MATRIX_SIZE);
|
||||
B[i] = (float*)malloc(sizeof(float) * DENSE_MATRIX_SIZE);
|
||||
C[i] = (float*)malloc(sizeof(float) * DENSE_MATRIX_SIZE);
|
||||
memset(A[i], 0, sizeof(float) * DENSE_MATRIX_SIZE);
|
||||
memset(B[i], 0, sizeof(float) * DENSE_MATRIX_SIZE);
|
||||
memset(C[i], 0, sizeof(float) * DENSE_MATRIX_SIZE);
|
||||
}
|
||||
for (i=0; i<A_nonZeroCount; i++) A[A_rowIndex[i]][A_colIndex[i]] = A_values[i];
|
||||
for (i=0; i<B_nonZeroCount; i++) B[B_rowIndex[i]][B_colIndex[i]] = B_values[i];
|
||||
matmul_optimized(A, B, C, DENSE_MATRIX_SIZE);
|
||||
|
||||
// 释放内存空间
|
||||
for(i=0; i<DENSE_MATRIX_SIZE; i++) {
|
||||
free(A[i]);
|
||||
free(B[i]);
|
||||
}
|
||||
free(A); free(B);
|
||||
return C;
|
||||
}
|
||||
|
||||
void matmul_optimized(float** A, float** B, float** C, int n) {
|
||||
int i, j, k;
|
||||
float32x4_t vecA, vecB, vecC;
|
||||
for(i=0; i<n; i++)
|
||||
for(j=0; j<n; j++) {
|
||||
vecC = vdupq_n_f32(0.0);
|
||||
for(k=0; k<n; k+=4) {
|
||||
vecA = vld1q_f32(&A[i][k]);
|
||||
vecB = vld1q_f32(&A[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 print_matrix(float** m, int rows, int cols) {
|
||||
int i, j;
|
||||
for(i=0; i<rows; i++) {
|
||||
for(j=0; j<cols; j++) {
|
||||
printf("%5.0f ", m[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue