-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproxasaga_atomic.cpp
223 lines (198 loc) · 8.15 KB
/
proxasaga_atomic.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <atomic>
#include <math.h> /* exp */
#include <stdint.h>
#include <time.h>
double inline partial_gradient(double p, double b) {
// partial gradient of logistic loss
double phi, exp_t;
p = p * b;
if (p > 0)
phi = 1. / (1. + exp(-p));
else {
exp_t = exp(p);
phi = exp_t / (1. + exp_t);
}
return (phi - 1) * b;
}
/* L1 proximal operator */
double inline prox(double x, double step_size) {
return fmax(x - step_size, 0) - fmax(- x - step_size, 0);
}
/* set foo = foo + bar atomically */
void inline add_atomic(std::atomic<double>* foo, double bar) {
auto current = foo[0].load();
while (!foo[0].compare_exchange_weak(current, current + bar))
;
}
void saga_single_thread(
std::atomic<double>* x, std::atomic<double>* memory_gradient, std::atomic<double>* gradient_average,
double* A_data, int64_t* A_indices, int64_t* A_indptr, double* b,
double* d, int64_t n_samples, int64_t n_features, double alpha,
double beta, double step_size, int64_t max_iter, double* trace_x,
double* trace_time, int thread_id, int64_t iter_freq) {
int64_t i, j, j_idx, local_counter=0, global_counter;
double p, grad_i, incr, old_grad, delta;
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int64_t> uni(0, n_samples-1);
struct timespec start, finish;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &start);
while (true) {
/* take a snapshot of the current vector of iterates in trace_x
and time in trace_time, in order to plot convergence later on */
if (local_counter % iter_freq == 0 && thread_id == 0) {
int64_t c = local_counter / iter_freq;
for (j=0; j<n_features; j++) {
trace_x[n_features * c + j] = x[j];
}
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
trace_time[c] = (double) elapsed;
printf(".. iteration %lld, time elapsed %f min ..\n", c, elapsed / 60.);
}
i = uni(rng);
p = 0.;
for (j=A_indptr[i]; j < A_indptr[i+1]; j++) {
j_idx = A_indices[j];
p += x[j_idx] * A_data[j];
}
grad_i = partial_gradient(p, b[i]);
old_grad = memory_gradient[i].load();
while (!memory_gradient[i].compare_exchange_weak(old_grad, grad_i))
;
incr = grad_i - old_grad;
// .. update coefficients ..
for (j=A_indptr[i]; j < A_indptr[i+1]; j++){
j_idx = A_indices[j];
delta = incr * A_data[j] + d[j_idx] * (gradient_average[j_idx] + alpha * x[j_idx]);
x[j_idx] = prox(x[j_idx] - step_size * delta, beta * step_size * d[j_idx]);
add_atomic(&gradient_average[j_idx], incr * A_data[j] / n_samples);
}
local_counter ++;
if (local_counter >= iter_freq * max_iter){
if (thread_id == 0) {
printf(".. done %lld iterations ..\n", local_counter / iter_freq);
}
return;
}
}
}
void saga_single_thread_nonatomic(
double* x, double* memory_gradient, double* gradient_average,
double* A_data, int64_t* A_indices, int64_t* A_indptr, double* b, double* d, int64_t n_samples,
int64_t n_features, double alpha, double beta, double step_size, int64_t max_iter,
double* trace_x, double* trace_time, int64_t iter_freq) {
int64_t i, j, j_idx, local_counter=0, global_counter;
double p, grad_i, incr, old_grad, delta;
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<int64_t> uni(0, n_samples-1);
struct timespec start, finish;
double elapsed;
clock_gettime(CLOCK_MONOTONIC, &start);
while (true) {
/* take a snapshot of the current vector of iterates in trace_x
and time in trace_time, in order to plot convergence later on */
if (local_counter % iter_freq == 0) {
int64_t c = local_counter / iter_freq;
for (j=0; j<n_features; j++) {
trace_x[n_features * c + j] = x[j];
}
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
trace_time[c] = (double) elapsed;
printf(".. iteration %lld, time elapsed %f min ..\n", c, elapsed / 60.);
}
i = uni(rng);
p = 0.;
for (j=A_indptr[i]; j < A_indptr[i+1]; j++) {
j_idx = A_indices[j];
p += x[j_idx] * A_data[j];
}
grad_i = partial_gradient(p, b[i]);
old_grad = memory_gradient[i];
memory_gradient[i] = grad_i;
incr = grad_i - old_grad;
// .. update coefficients ..
for (j=A_indptr[i]; j < A_indptr[i+1]; j++){
j_idx = A_indices[j];
delta = incr * A_data[j] + d[j_idx] * (gradient_average[j_idx] + alpha * x[j_idx]);
x[j_idx] = prox(x[j_idx] - step_size * delta, beta * step_size * d[j_idx]);
gradient_average[j_idx] += incr * A_data[j] / n_samples;
}
local_counter ++;
if (local_counter >= iter_freq * max_iter){
printf(".. done %lld iterations ..\n", local_counter / iter_freq);
return;
}
}
}
int prox_asaga(
double* x, double* A_data, int64_t* A_indices, int64_t* A_indptr,
double* b, double* d, int64_t n_samples, int64_t n_features,
int64_t n_threads, double alpha, double beta, double step_size,
int64_t max_iter, double* trace_x, double* trace_time, int64_t iter_freq) {
std::vector<std::thread> threads;
std::atomic<double>* memory_gradient;
std::atomic<double>* gradient_average;
double* memory_gradient_nonatomic;
double* gradient_average_nonatomic;
if (n_threads == 1) {
memory_gradient_nonatomic = new double[n_samples];
gradient_average_nonatomic = new double[n_features];
for(int i=0; i<n_samples; i++){
memory_gradient_nonatomic[i] = 0;
}
for(int j=0; j<n_features; j++){
gradient_average_nonatomic[j] = 0;
}
saga_single_thread_nonatomic(x, memory_gradient_nonatomic,
gradient_average_nonatomic, A_data, A_indices, A_indptr,
b, d, n_samples, n_features, alpha, beta, step_size, max_iter,
trace_x, trace_time, iter_freq);
} else {
memory_gradient = new std::atomic<double>[n_samples];
for(int i=0; i<n_samples; i++){
memory_gradient[i] = 0;
}
gradient_average = new std::atomic<double>[n_features];
for(int j=0; j<n_features; j++){
gradient_average[j] = 0;
}
std::atomic<double>* x_atomic = new std::atomic<double>[n_features];
for(int j=0; j<n_features; j++){
x_atomic[j] = x[j];
}
for (int i = 0; i < n_threads; ++i) {
threads.push_back(std::thread(saga_single_thread, x_atomic, memory_gradient, gradient_average, A_data,
A_indices, A_indptr, b, d, n_samples, n_features, alpha, beta, step_size, max_iter,
trace_x, trace_time, i, iter_freq));
}
for(auto &t : threads){
t.join();
}
for(int j=0; j<n_features; j++){
x[j] = x_atomic[j];
}
delete[] memory_gradient;
delete[] gradient_average;
}
return 0;
}
/* expose the above prox_asaga function so that it can be called from */
extern "C"
{
extern int cffi_prox_asaga(double* x, double* A_data, int64_t* A_indices, int64_t* A_indptr, double* b,
double* d, int64_t n_samples, int64_t n_features, int64_t n_threads, double alpha, double beta,
double step_size, int64_t max_iter, double* trace_x, double* trace_time, int64_t iter_freq) {
return prox_asaga(x, A_data, A_indices, A_indptr, b, d, n_samples, n_features,
n_threads, alpha, beta, step_size, max_iter, trace_x, trace_time, iter_freq);
}
}