|
|
|
@ -0,0 +1,60 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#define SIZE 102
|
|
|
|
|
void matmul(float** A, float** B, float** C, int n)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < n; ++i)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < n; ++j)
|
|
|
|
|
{
|
|
|
|
|
C[i][j] = 0;
|
|
|
|
|
for (int k = 0; k < n; ++k)
|
|
|
|
|
{
|
|
|
|
|
C[i][j] += A[i][k] * B[k][j];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
|
|
// 分配矩阵内存
|
|
|
|
|
float** A = (float**)malloc(SIZE * sizeof(float*));
|
|
|
|
|
float** B = (float**)malloc(SIZE * sizeof(float*));
|
|
|
|
|
float** C = (float**)malloc(SIZE * sizeof(float*));
|
|
|
|
|
for (int 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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//初始化矩阵数据
|
|
|
|
|
for (int i = 0; i < SIZE; i++)
|
|
|
|
|
{
|
|
|
|
|
for(int j=0;j<SIZE;j++)
|
|
|
|
|
{
|
|
|
|
|
A[i][j] = (float)(rand() % 100) / 100.0f;
|
|
|
|
|
B[i][j] = (float)(rand() % 100) / 100.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clock_t start = clock();
|
|
|
|
|
matmul(A, B, C, SIZE);
|
|
|
|
|
clock_t end = clock();
|
|
|
|
|
|
|
|
|
|
// 计算并输出向量乘法的时间
|
|
|
|
|
double multiply_time_spent = double(end - start) / CLOCKS_PER_SEC;
|
|
|
|
|
printf("使用基础的向量乘法:\n当SIZE取%d时,初始向量乘法时间:%lf秒\n", SIZE,multiply_time_spent);
|
|
|
|
|
|
|
|
|
|
// 释放动态分配的内存
|
|
|
|
|
for (int i = 0; i < SIZE; ++i) {
|
|
|
|
|
free(A[i]);free(B[i]);free(C[i]);
|
|
|
|
|
}
|
|
|
|
|
free(A);free(B);free(C);
|
|
|
|
|
|
|
|
|
|
}
|