-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat_cpu_simple_matrix_multiplication.c
More file actions
56 lines (41 loc) · 1.4 KB
/
float_cpu_simple_matrix_multiplication.c
File metadata and controls
56 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <omp.h>
#include <time.h>
#include "utils.h"
float *matrix_multiply(float *A, float *B, int rowsA, int colsA, int colsB) {
float *C = (float *)malloc(rowsA * colsB * sizeof(float));
#pragma omp parallel for
for (int i = 0; i < rowsA * colsB; i++) {
C[i] = 0.0;
}
#pragma omp parallel for
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
C[i * colsB + j] += A[i * colsA + k] * B[k * colsB + j];
}
}
}
return C;
}
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <matrixA.txt> <matrixB.txt> <result.txt>\n", argv[0]);
return -1;
}
int rowsA, colsA, rowsB, colsB;
float *A, *B, *C;
A = read_float_matrix(argv[1], &rowsA, &colsA);
B = read_float_matrix(argv[2], &rowsB, &colsB);
validate_dimensions(rowsA, colsA, rowsB, colsB);
printf("Done loading data, starting computations with %d threads\n", omp_get_max_threads());
omp_set_num_threads(omp_get_max_threads());
float start_time = omp_get_wtime();
C = matrix_multiply(A, B, rowsA, colsA, colsB);
float end_time = omp_get_wtime();
printf("Matrix multiplication completed in %f seconds\n", end_time - start_time);
write_float_matrix(argv[3], C, rowsA, colsB);
free(A);
free(B);
free(C);
return 0;
}