|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <arm_neon.h> //启用 NEON 指令
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#define M 1000
|
|
|
|
|
#define N 1021
|
|
|
|
|
#define Q 1000
|
|
|
|
|
//A(M*N),B(N*Q),C(M*Q),transposed(Q*N)对于矩阵维度的说明
|
|
|
|
|
|
|
|
|
|
void transposeMatrix(float** matrix, float** transposed) {
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
for (int j = 0; j < Q; j++) {
|
|
|
|
|
transposed[j][i] = matrix[i][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void matmul(float** A, float** B, float** C) {
|
|
|
|
|
for (int i = 0; i < M; i++) {
|
|
|
|
|
for (int j = 0; j < Q; j++) {
|
|
|
|
|
float sum = 0.0f;
|
|
|
|
|
for (int k = 0; k < N; k += 4)
|
|
|
|
|
{
|
|
|
|
|
float32x4_t vecA, vecB, vecC;
|
|
|
|
|
if (k + 4 <= N)
|
|
|
|
|
{
|
|
|
|
|
// 加载A和B的4个元素,进行向量化计算
|
|
|
|
|
vecA = vld1q_f32(&A[i][k]);
|
|
|
|
|
vecB = vld1q_f32(&B[j][k]);
|
|
|
|
|
// 向量化乘法并累加结果
|
|
|
|
|
vecC = vmulq_f32(vecA, vecB);
|
|
|
|
|
sum += vgetq_lane_f32(vecC, 0) + vgetq_lane_f32(vecC, 1) +
|
|
|
|
|
vgetq_lane_f32(vecC, 2) + vgetq_lane_f32(vecC, 3);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// 处理剩余的元素
|
|
|
|
|
for (int m = k; m < N; m++)
|
|
|
|
|
{
|
|
|
|
|
sum += A[i][m] * B[j][m];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
C[i][j] = sum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
|
|
float** A = (float**)malloc(M * sizeof(float*));
|
|
|
|
|
float** B = (float**)malloc(N * sizeof(float*));
|
|
|
|
|
float** C = (float**)malloc(M * sizeof(float*));
|
|
|
|
|
float** transposed = (float**)malloc(Q * sizeof(float*));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < M; i++) {
|
|
|
|
|
A[i] = (float*)malloc(N * sizeof(float));
|
|
|
|
|
C[i] = (float*)malloc(Q * sizeof(float));
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < N; ++i)
|
|
|
|
|
{
|
|
|
|
|
B[i] = (float*)malloc(Q * sizeof(float));
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < Q; ++i)
|
|
|
|
|
{
|
|
|
|
|
transposed[i] = (float*)malloc(N * sizeof(float));
|
|
|
|
|
}
|
|
|
|
|
// 初始化矩阵数据
|
|
|
|
|
for (int i = 0; i < M; i++) {
|
|
|
|
|
for (int j = 0; j < N; j++) {
|
|
|
|
|
A[i][j] = (float)(rand() % 100) / 100.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < N; i++) {
|
|
|
|
|
for (int j = 0; j < Q; j++) {
|
|
|
|
|
B[i][j] = (float)(rand() % 100) / 100.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transposeMatrix(B, transposed);
|
|
|
|
|
|
|
|
|
|
clock_t start = clock();
|
|
|
|
|
matmul(A, transposed, C);
|
|
|
|
|
clock_t end = clock();
|
|
|
|
|
|
|
|
|
|
// 计算并输出矩阵乘法的时间
|
|
|
|
|
double multiply_time_spent = double(end - start) / CLOCKS_PER_SEC;
|
|
|
|
|
printf("使用优化的向量乘法:\n当矩阵A的维度为%d*%d,矩阵B的维度为%d*%d时,优化的向量乘法时间:%lf秒\n", M,N,N,Q,multiply_time_spent);
|
|
|
|
|
|
|
|
|
|
// 释放动态分配的内存
|
|
|
|
|
for (int i = 0; i < M; ++i) {
|
|
|
|
|
free(A[i]);free(C[i]);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < N ;++i) {
|
|
|
|
|
free(B[i]);
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < Q ;++i) {
|
|
|
|
|
free(transposed[i]);
|
|
|
|
|
}
|
|
|
|
|
free(A); free(B); free(C);free(transposed);
|
|
|
|
|
|
|
|
|
|
}
|