ADD file via upload

main
ptslgaequ 4 days ago
parent 0b9b8d664d
commit 9125d5c2f9

@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#define SIZE 1024
void matmul(float** A, float** B, float** C, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
float sum = 0;
for (int k = 0; k < n; k++) {
sum += A[i][k] * B[k][j];}
C[i][j] = sum;}
}
}
int main() {
float** A = (float**)malloc(SIZE * sizeof(float*));
for (int i = 0; i < SIZE; i++) {
A[i] = (float*)malloc(SIZE * sizeof(float));
}
float** B = (float**)malloc(SIZE * sizeof(float*));
for (int i = 0; i < SIZE; i++) {
B[i] = (float*)malloc(SIZE * sizeof(float));
}
float** C = (float**)malloc(SIZE * sizeof(float*));
for (int i = 0; i < SIZE; i++) {
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);
B[i][j] = (float)(rand() % 100);
}
}
clock_t start_time = clock();
matmul(A, B, C, SIZE);
clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("time: %lf s\n", elapsed_time);
for (int i = 0; i < SIZE; i++) {
free(A[i]);}
free(A);
for (int i = 0; i < SIZE; i++) {
free(B[i]);}
free(B);
for (int i = 0; i < SIZE; i++) {
free(C[i]);}
free(C);
return 0;
}
Loading…
Cancel
Save