-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmm_v1.cpp
More file actions
83 lines (76 loc) · 2.22 KB
/
bmm_v1.cpp
File metadata and controls
83 lines (76 loc) · 2.22 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
81
82
83
#include <iostream>
#include <omp.h>
using namespace std;
void bmm_double(
void* A, // Pointer to A with dims (b, n, m)
void* B, // Pointer to B with dims (b, p, m)
void* C, // Pointer to C with dims (b, n, p)
int b,
int n,
int m,
int p
) {
auto castA = (double*) A;
auto castB = (double*) B;
auto castC = (double*) C;
int bsA = n * m;
int bsB = m * p;
int bsC = n * p;
#pragma omp parallel for collapse(2) // distributes work to 6 cores on my machine
for (int batch = 0; batch < b; ++batch) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < p; ++j) {
#pragma omp simp
for (int k = 0; k < m; ++k) {
castC[batch * bsC + i * p + j] += castA[batch * bsA + i * m + k] * castB[batch * bsB + j * m + k];
}
}
}
}
}
void bmm_float(
void* A, // Pointer to A with dims (b, n, m)
void* B, // Pointer to B with dims (b, p, m)
void* C, // Pointer to C with dims (b, n, p)
int b,
int n,
int m,
int p
) {
auto castA = (float*) A;
auto castB = (float*) B;
auto castC = (float*) C;
int bsA = n * m;
int bsB = m * p;
int bsC = n * p;
#pragma omp parallel for collapse(2) // distributes work to 6 cores on my machine
for (int batch = 0; batch < b; ++batch) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < p; ++j) {
for (int k = 0; k < m; ++k) {
castC[batch * bsC + i * p + j] += castA[batch * bsA + i * m + k] * castB[batch * bsB + j * m + k];
}
}
}
}
}
// Expose the C++ function through an extern "C" interface
extern "C" {
void my_bmm( // IMPORTANT: expects contiguous tensors
void* A, // Pointer to A with dims (b, n, m)
void* B, // Pointer to B with dims (b, p, m)
void* C, // Pointer to C with dims (b, n, p)
int b,
int n,
int m,
int p,
const char* d_type
) {
if(*d_type == *"d"){
bmm_double(A, B, C, b, n, m, p);
}
if(*d_type == *"f") {
bmm_float(A, B, C, b, n, m, p);
}
}
}