forked from bioinf-jku/librfn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_operations.cpp
More file actions
126 lines (102 loc) · 3.25 KB
/
Copy pathcpu_operations.cpp
File metadata and controls
126 lines (102 loc) · 3.25 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Copyright © 2015 Thomas Unterthiner
Licensed under GPL, version 2 or a later (see LICENSE.txt)
*/
#include "cpu_operations.h"
#include <cstdio>
#include <algorithm>
using std::printf;
using std::max;
//float* CPU_Operations::ones = 0;
CPU_Operations::CPU_Operations(const int m, const int n, const int k,
unsigned long seed, int gpu_id) {
srand(seed);
int maxsize = max(max(n, m), k);
ones = malloc(maxsize*sizeof(float));
for (int i = 0; i < maxsize; ++i)
ones[i] = 1.0f;
var_tmp = malloc(maxsize*sizeof(float));
}
CPU_Operations::~CPU_Operations() {
free(ones);
free(var_tmp);
}
static void colmeans(const float* X, float* means, const int nrows, const int ncols) {
memset(means, 0, ncols*sizeof(float));
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
means[j] += X[i*ncols+j];
}
}
for (int j = 0; j < ncols; ++j)
means[j] /= nrows;
}
void CPU_Operations::dgmm(const char* mode, const int m, const int n, const float* A,
int lda, const float* x, int incx, float* C, int ldc) const {
if (mode[0] == 'l' || mode[0] == 'L') {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
C[i*ldc+j] = A[i*lda+j] * x[j];
}
} else {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j)
C[i*ldc+j] = A[i*lda+j] * x[i];
}
}
}
void CPU_Operations::calculate_column_variance(const float* X, const unsigned nrows,
const unsigned ncols, float* variances) {
colmeans(X, var_tmp, nrows, ncols);
memset(variances, 0, ncols*sizeof(float));
for (unsigned i = 0; i < nrows; ++i) {
for (unsigned j = 0; j < ncols; ++j) {
const float x = X[i*ncols+j] - var_tmp[j];
variances[j] += x*x;
}
}
for (unsigned j = 0; j < ncols; ++j) {
variances[j] /= nrows;
}
}
void CPU_Operations::invsqrt(float* s, const unsigned n) const {
for (unsigned j = 0; j < n; ++j) {
if (s[j] == 0)
s[j] = 1.0f;
else
s[j] = 1.0 / sqrtf(s[j]);
}
}
void CPU_Operations::scale_columns(float* X, const unsigned nrows, const unsigned ncols, float* s) const {
for (unsigned i = 0; i < nrows; ++i) {
for (unsigned j = 0; j < ncols; ++j) {
X[i*ncols+j] *= s[j];
}
}
}
void CPU_Operations::scale_rows(float* X, const unsigned nrows, const unsigned ncols, float* s) const {
for (unsigned i = 0; i < nrows; ++i) {
for (unsigned j = 0; j < ncols; ++j) {
X[i*ncols+j] *= s[i];
}
}
}
/// Prints a column major matrix.
void CPU_Operations::printMatrixCM(const float* a, int n, int m, const char* fmt) {
const char* format = fmt == 0 ? "%1.3f " : fmt;
for (int i = 0; i < n; ++i) {
for (int j =0 ; j < m; ++j)
printf(format, a[i + j*n]);
printf("\n");
}
printf("\n");
}
/// Prints a row-major matrix
void CPU_Operations::printMatrixRM(const float* a, int n, int m, const char* fmt) {
const char* format = fmt == 0 ? "%1.3f " : fmt;
for (int i = 0; i < n; ++i) {
for (int j =0 ; j < m; ++j)
printf(format, a[i*m + j]);
printf("\n");
}
}