-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmul.c
More file actions
80 lines (64 loc) · 1.7 KB
/
Copy pathmatmul.c
File metadata and controls
80 lines (64 loc) · 1.7 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <mpi.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <omp_samples.h>
#define MAT_IX(row, col, cols) col + row * cols
typedef struct {
int rows;
int cols;
int* data;
} matrix_t;
matrix_t amatmul(matrix_t mat_a, matrix_t mat_b) {
matrix_t mat_out;
assert(mat_a.cols == mat_b.rows);
mat_out.rows = mat_a.rows;
mat_out.cols = mat_b.cols;
mat_out.data = calloc(mat_out.rows * mat_out.cols, sizeof(int));
#pragma omp parallel for schedule(dynamic)
for(int i = 0; i < mat_out.rows * mat_out.cols; i++){
int row = i / mat_out.cols;
int col = i % mat_out.cols;
int sum = 0;
#pragma omp parallel for reduction(+:sum) schedule(dynamic)
for (int j = 0; j < mat_a.cols; j++){
sum +=
mat_a.data[MAT_IX(row, j, mat_a.cols)] *
mat_b.data[MAT_IX(j, col, mat_b.cols)];
}
mat_out.data[i] = sum;
}
return mat_out;
}
void matrix_fmt(matrix_t mat) {
for (int i = 0; i < mat.rows; i++) {
for (int j = 0; j < mat.cols; j++){
printf("%d\t", mat.data[MAT_IX(i, j, mat.cols)]);
}
printf("\n");
}
printf("\n");
}
static void wrapper(int _) {
// columnwise sum
int A[] = {
3, 2, 1, 5, 6,
1, 1, 1, 1, 1,
2, 2, 2, 2, 2
};
int B[] = {
2, 3,
8, 8,
1, 2,
3, 11,
7, 8,
};
matrix_t mat_a = { .rows = 3, .cols = 5, .data = A };
matrix_t mat_b = { .rows = 5, .cols = 2, .data = B };
matrix_t mat_out;
omp_set_num_threads(8);
mat_out = amatmul(mat_a, mat_b);
matrix_fmt(mat_out);
}
SAMPLE(wrapper, 0)