-
Notifications
You must be signed in to change notification settings - Fork 15
/
rwrapper.cpp
180 lines (130 loc) · 5.44 KB
/
rwrapper.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <Rcpp.h>
#include <vector>
#include <cstdio>
#include "librfn.h"
#include "use_R_impl.h"
#include <ctime>
using namespace Rcpp;
RcppExport SEXP R_train_rfn(SEXP Xs, SEXP Ws, SEXP Ps, SEXP ns, SEXP ms, SEXP ks, SEXP n_iters,
SEXP batch_sizes, SEXP etaWs, SEXP etaPs, SEXP minPs, SEXP h_thresholds, SEXP dropout_rates,
SEXP input_noise_rates, SEXP l2_weightdecays, SEXP l1_weightdecays, SEXP momentums,
SEXP noise_types, SEXP apply_relus, SEXP apply_scalings, SEXP apply_newton_updates, SEXP seeds, SEXP gpu_id, SEXP verbose)
{
BEGIN_RCPP
int n = as<int>(ns);
int m = as<int>(ms);
int k = as<int>(ks);
std::vector<float> X = as<std::vector<float> >(Xs);
std::vector<float> W = as<std::vector<float> >(Ws);
std::vector<float> P = as<std::vector<float> >(Ps);
GetRNGstate();
clock_t t = clock();
train_rfn(&X[0], &W[0], &P[0], n, m, k, as<int>(n_iters), as<int>(batch_sizes), as<float>(etaWs),
as<float>(etaPs), as<float>(minPs), as<float>(h_thresholds), as<float>(dropout_rates),
as<float>(input_noise_rates), as<float>(l2_weightdecays), as<float>(l1_weightdecays),
as<float>(momentums), as<int>(noise_types), as<int>(apply_relus), as<int>(apply_scalings),
as<int>(apply_newton_updates), as<int>(seeds), as<int>(gpu_id), as<bool>(verbose));
t = clock() - t;
PutRNGstate();
NumericVector W_ret = wrap(W);
W_ret.attr("dim") = Dimension(m, k); /* conversion from rowmajor 2 colmajor */
NumericVector P_ret = wrap(P);
List ret;
ret["W"] = W_ret;
ret["P"] = P_ret;
ret["T"] = wrap<double>(((double) t) / CLOCKS_PER_SEC);
return ret;
END_RCPP
}
RcppExport SEXP R_train_rfn_sparse(SEXP Xs, SEXP rowvs, SEXP colvs, SEXP Ws, SEXP Ps, SEXP ns, SEXP ms, SEXP ks, SEXP n_iters,
SEXP batch_sizes, SEXP etaWs, SEXP etaPs, SEXP minPs, SEXP h_thresholds, SEXP dropout_rates,
SEXP input_noise_rates, SEXP l2_weightdecays, SEXP l1_weightdecays, SEXP momentums,
SEXP noise_types, SEXP apply_relus, SEXP apply_scalings, SEXP apply_newton_updates, SEXP seeds, SEXP gpu_id, SEXP verbose)
{
BEGIN_RCPP
int n = as<int>(ns);
int m = as<int>(ms);
int k = as<int>(ks);
std::vector<int> rowv = as<std::vector<int> >(rowvs);
std::vector<int> colv = as<std::vector<int> >(colvs);
std::vector<float> X = as<std::vector<float> >(Xs);
std::vector<float> W = as<std::vector<float> >(Ws);
std::vector<float> P = as<std::vector<float> >(Ps);
GetRNGstate();
clock_t t = clock();
train_rfn_sparse(&X[0], &colv[0], &rowv[0], &W[0], &P[0], n, m, k, as<int>(n_iters), as<int>(batch_sizes), as<float>(etaWs),
as<float>(etaPs), as<float>(minPs), as<float>(h_thresholds), as<float>(dropout_rates),
as<float>(input_noise_rates), as<float>(l2_weightdecays), as<float>(l1_weightdecays),
as<float>(momentums), as<int>(noise_types), as<int>(apply_relus), as<int>(apply_scalings),
as<int>(apply_newton_updates), as<int>(seeds), as<int>(gpu_id), as<bool>(verbose));
t = clock() - t;
PutRNGstate();
NumericVector W_ret = wrap(W);
W_ret.attr("dim") = Dimension(m, k);
NumericVector P_ret = wrap(P);
List ret;
ret["W"] = W_ret;
ret["P"] = P_ret;
ret["T"] = wrap<double>(((double) t) / CLOCKS_PER_SEC);
return ret;
END_RCPP
}
RcppExport SEXP R_calculate_W(SEXP Xs, SEXP Ws, SEXP Ps, SEXP ns, SEXP ms, SEXP ks, SEXP activations,
SEXP apply_scalings, SEXP h_thresholds, SEXP gpu_id)
{
BEGIN_RCPP
int n = as<int>(ns);
int m = as<int>(ms);
int k = as<int>(ks);
std::vector<float> X = as<std::vector<float> >(Xs);
std::vector<float> W = as<std::vector<float> >(Ws);
std::vector<float> P = as<std::vector<float> >(Ps);
std::vector<float> Wout(k*m);
calculate_W(&X[0], &W[0], &P[0], &Wout[0], n, m, k, as<int>(activations),
as<int>(apply_scalings), as<float>(h_thresholds), as<int>(gpu_id));
NumericVector Wout_ret = wrap(Wout);
Wout_ret.attr("dim") = Dimension(m, k);
return Wout_ret;
END_RCPP
}
RcppExport SEXP R_calculate_W_sparse(SEXP Xs, SEXP rowvs, SEXP colvs, SEXP Ws, SEXP Ps, SEXP ns, SEXP ms, SEXP ks, SEXP activations,
SEXP apply_scalings, SEXP h_thresholds, SEXP gpu_id)
{
BEGIN_RCPP
int n = as<int>(ns);
int m = as<int>(ms);
int k = as<int>(ks);
std::vector<int> rowv = as<std::vector<int> >(rowvs);
std::vector<int> colv = as<std::vector<int> >(colvs);
std::vector<float> X = as<std::vector<float> >(Xs);
std::vector<float> W = as<std::vector<float> >(Ws);
std::vector<float> P = as<std::vector<float> >(Ps);
std::vector<float> Wout(k*m);
calculate_W_sparse(&X[0], &colv[0], &rowv[0], &W[0], &P[0], &Wout[0], n, m, k, as<int>(activations),
as<int>(apply_scalings), as<float>(h_thresholds), as<int>(gpu_id));
NumericVector Wout_ret = wrap(Wout);
Wout_ret.attr("dim") = Dimension(m, k);
return Wout_ret;
END_RCPP
}
//#include <mkl_spblas.h>
//#include <mkl_trans.h>
using std::cerr;
using std::endl;
RcppExport SEXP somatcopy(SEXP s_arows, SEXP s_acols, SEXP s_A)
{
BEGIN_RCPP
char order = 'c';//as<char>(s_order);
char trans = 't';//as<char>(s_trans);
size_t arows = as<size_t>(s_arows);
size_t acols = as<size_t>(s_acols);
std::vector<float> A = as<std::vector<float> >(s_A);
size_t lda = arows;
std::vector<float> B(A.size());
size_t ldb = acols;
//mkl_somatcopy(order, trans, arows, acols, 1.f, &A[0], lda, &B[0], ldb);
NumericVector B_ret = wrap(B);
B_ret.attr("dim") = Dimension(acols, arows);
return B_ret;
END_RCPP
}