forked from pi7mcrg2k/operator_optimization
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.
53 lines
1.1 KiB
53 lines
1.1 KiB
17 hours ago
|
#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];
|
||
|
}
|
||
|
}
|
||
|
}
|