-
Notifications
You must be signed in to change notification settings - Fork 1
/
linear.cpp
3578 lines (3217 loc) · 87.4 KB
/
linear.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <stdarg.h>
#include <locale.h>
#include "linear.h"
#include <mpi.h>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#ifndef TIMER
#define TIMER
#define NS_PER_SEC 1000000000
double wall_time_diff(int64_t ed, int64_t st)
{
return (double)(ed-st)/(double)NS_PER_SEC;
}
int64_t wall_clock_ns()
{
#if __unix__
struct timespec tspec;
clock_gettime(CLOCK_MONOTONIC, &tspec);
return tspec.tv_sec*NS_PER_SEC + tspec.tv_nsec;
#else
#if __MACH__
return 0;
#else
struct timeval tv;
gettimeofday( &tv, NULL );
return tv.tv_sec*NS_PER_SEC + tv.tv_usec*1000;
#endif
#endif
}
#endif
double communication;
double global_n;
typedef signed char schar;
#ifndef min
template <class T> static inline T min(T x,T y) { return (x<y)?x:y; }
#endif
#ifndef max
template <class T> static inline T max(T x,T y) { return (x>y)?x:y; }
#endif
template <class S, class T> static inline void clone(T*& dst, S* src, int n)
{
dst = new T[n];
memcpy((void *)dst,(void *)src,sizeof(T)*n);
}
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
#define INF HUGE_VAL
static void print_string_stdout(const char *s)
{
fputs(s,stdout);
fflush(stdout);
}
static void (*liblinear_print_string) (const char *) = &print_string_stdout;
#if 1
static void info(const char *fmt,...)
{
if(mpi_get_rank()!=0)
return;
char buf[BUFSIZ];
va_list ap;
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
(*liblinear_print_string)(buf);
}
#else
static void info(const char *fmt,...) {}
#endif
class sparse_operator
{
public:
static double dot(const double *s, const feature_node *x)
{
double ret = 0;
while(x->index != -1)
{
ret += s[x->index-1]*x->value;
x++;
}
return (ret);
}
static void axpy(const double a, const feature_node *x, double *y)
{
while(x->index != -1)
{
y[x->index-1] += a*x->value;
x++;
}
}
};
#ifdef __cplusplus
extern "C" {
#endif
extern double dnrm2_(int *, double *, int *);
extern double ddot_(int *, double *, int *, double *, int *);
extern int daxpy_(int *, double *, double *, int *, double *, int *);
extern int dscal_(int *, double *, double *, int *);
extern int dgemv_(char *, int *, int *, double *, double *, int *, double *, int *, double *, double *, int *);
extern void dgetrf_(int *, int *, double *, int *, int *, int *);
extern void dgetri_(int *, double *, int *, int *, double *, int *, int *);
#ifdef __cplusplus
}
#endif
void inverse(double* A, int N) //compute the inverse of a symmetric PD matrix
{
int *IPIV = new int[N+1];
int LWORK = N*N;
double *WORK = new double[LWORK];
int INFO;
dgetrf_(&N,&N,A,&N,IPIV,&INFO);
dgetri_(&N,A,&N,IPIV,WORK,&LWORK,&INFO);
delete[] IPIV;
delete[] WORK;
}
class regularized_fun
{
public:
virtual double fun(double *w) = 0;
virtual double f_update(double *w, double *step, double Q, double eta, int *index = NULL, int index_length = 0, int localstart = 0, int locallength = 0) = 0;
virtual void loss_grad(double *w, double *g, const std::vector<int> &fullindex = std::vector<int>{-1}) = 0;
virtual void Hv(double *s, double *Hs, const std::vector<int> &fullindex = std::vector<int>{-1}, double *w = NULL) = 0;
virtual double vHv(double *v, const std::vector<int> &fullindex = std::vector<int>{-1}) = 0; //For estimating the starting step size in sparsa
virtual void get_diag_preconditioner(double *M, const std::vector<int> &fullindex = std::vector<int>{-1}, double *w = NULL) = 0;
virtual void full_grad(double *w, double *g, std::vector<int> &index, double *full_g, std::vector<int> &local_nonzero_set) = 0;
virtual void setselection(double *w, double *loss_g, std::vector<int> &prev_index, std::vector<int> &curr_index, double shrinkage = 1.0) = 0;
virtual int get_nr_variable(void) = 0;
virtual double armijo_line_search(double *step, double *w, double *loss_g, double *step_size, double eta, int *num_line_search_steps, double *delta_ret, int *index = NULL, int indexlength = 0, int localstart = 0, int locallength = 0) = 0;
virtual double smooth_line_search(double *w, double *smooth_step, double delta, double eta, std::vector<int> &index, double *fnew) = 0;
virtual void prox_grad(double *w, double *g, double *step, double *oldd, double alpha = 0, int index_length = -1) = 0;
virtual double regularizer(double *w, int length) = 0;
virtual void get_local_index_start_and_length(int global_length, int *local_start, int *local_length) = 0;
int start; // Every node handles only a part of the w vector, from start to start + length - 1;
int length;
int *recv_count;
int *displace;
protected:
double C;
double *z;
double *Xw;
double *D;
const problem *prob;
double reg;
double current_f;
};
class l1r_fun: public regularized_fun
{
public:
l1r_fun(const problem *prob, double C);
virtual ~l1r_fun(){}
double fun(double *w);
double f_update(double *w, double *step, double Q, double eta, int *index = NULL, int index_length = 0, int localstart = 0, int locallength = 0);
void loss_grad(double *w, double *g, const std::vector<int> &fullindex = std::vector<int>{-1});
void Hv(double *s, double *Hs, const std::vector<int> &fullindex = std::vector<int>{-1}, double *w = NULL);
double vHv(double *v, const std::vector<int> &fullindex = std::vector<int>{-1}); //For estimating the starting step size in sparsa
void get_diag_preconditioner(double *M, const std::vector<int> &fullindex = std::vector<int>{-1}, double *w = NULL);
void full_grad(double *w, double *g, std::vector<int> &index, double *full_g, std::vector<int> &local_nonzero_set);
void setselection(double *w, double *loss_g, std::vector<int> &prev_index, std::vector<int> &curr_index, double shrinkage = 1.0);
int get_nr_variable(void);
double armijo_line_search(double *step, double *w, double *loss_g, double *step_size, double eta, int *num_line_search_steps, double *delta_ret, int *index = NULL, int indexlength = 0, int localstart = 0, int locallength = 0);
double smooth_line_search(double *w, double *smooth_step, double delta, double eta, std::vector<int> &index, double *fnew);
void Xv(double *v, double *Xv, const int *index = NULL, int index_length = 0);
void prox_grad(double *w, double *g, double *step, double *oldd, double alpha = 0, int index_length = -1);
double regularizer(double *w, int length);
void get_local_index_start_and_length(int global_length, int *local_start, int *local_length);
protected:
virtual double loss(int i, double wx_i) = 0;
virtual void update_zD() = 0;
void XTv(double *v, double *XTv, const std::vector<int> &fullindex = std::vector<int>{-1});
};
class l1r_lr_fun: public l1r_fun
{
public:
l1r_lr_fun(const problem *prob, double C);
~l1r_lr_fun();
protected:
double loss(int i, double xw_i);
void update_zD();
};
class lasso: public l1r_lr_fun
{
public:
lasso(const problem *prob, double C);
~lasso();
private:
double loss(int i, double xw_i); // Loss is (w^T x - y)^2 / 2
void update_zD();
};
class l1r_l2_svc_fun: public l1r_lr_fun
{
public:
l1r_l2_svc_fun(const problem *prob, double C);
~l1r_l2_svc_fun();
private:
double loss(int i, double xw_i);
void update_zD();
};
class grouplasso_mlr_fun: public regularized_fun
{
public:
grouplasso_mlr_fun(const problem *prob, double C, int nr_class);
~grouplasso_mlr_fun();
double fun(double *w);
double f_update(double *w, double *step, double Q, double eta, int *index = NULL, int index_length = 0, int localstart = 0, int locallength = 0);
void loss_grad(double *w, double *g, const std::vector<int> &fullindex = std::vector<int>{-1});
void Hv(double *s, double *Hs, const std::vector<int> &fullindex = std::vector<int>{-1}, double *w = NULL);
double vHv(double *v, const std::vector<int> &fullindex = std::vector<int>{-1}); //For estimating the starting step size in sparsa
void get_diag_preconditioner(double *M, const std::vector<int> &fullindex = std::vector<int>{-1}, double *w = NULL);
void full_grad(double *w, double *g, std::vector<int> &index, double *full_g, std::vector<int> &local_nonzero_set);
void setselection(double *w, double *loss_g, std::vector<int> &prev_index, std::vector<int> &curr_index, double shrinkage = 1.0);
int get_nr_variable(void);
double armijo_line_search(double *step, double *w, double *loss_g, double *step_size, double eta, int *num_line_search_steps, double *delta_ret, int *index = NULL, int indexlength = 0, int localstart = 0, int locallength = 0);
double smooth_line_search(double *w, double *smooth_step, double delta, double eta, std::vector<int> &index, double *fnew);
void prox_grad(double *w, double *g, double *step, double *oldd, double alpha = 0, int index_length = -1);
double regularizer(double *w, int length);
void get_local_index_start_and_length(int global_length, int *local_start, int *local_length);
protected:
int nr_class;
int full_len;
int Xw_length;
void Xv(double *v, double *Xv, const int *index = NULL, int index_length = 0);
void XTv(double *v, double *XTv, const std::vector<int> &fullindex = std::vector<int>{-1});
double loss(int i, double* wx_i);
void update_zD();
int localfeatures;
int localfeature_start;
};
l1r_fun::l1r_fun(const problem *prob, double C)
{
int l=prob->l;
this->prob = prob;
z = new double[l];
Xw = new double[l];
D = new double[l];
this->C = C;
int w_size = get_nr_variable();
int nr_node = mpi_get_size();
int rank = mpi_get_rank();
int shift = int(ceil(double(w_size) / double(nr_node)));
recv_count = new int[nr_node];
displace = new int[nr_node];
this->start = shift * rank;
this->length = min(max(w_size - start, 0), shift);
if (length == 0)
start = 0;
int counter = 0;
for (int i=0;i<nr_node;i++)
{
recv_count[i] = shift;
displace[i] = counter;
counter += shift;
if (counter >= w_size)
{
counter = 0;
shift = 0;
}
else if (counter + shift > w_size)
shift = w_size - counter;
}
}
void l1r_fun::get_local_index_start_and_length(int global_length, int *local_start, int *local_length)
{
int nr_node = mpi_get_size();
int rank = mpi_get_rank();
int shift = int(ceil(double(global_length) / double(nr_node)));
int idx_start = shift * rank;
int idx_length = min(max(global_length - idx_start, 0), shift);
if (idx_length == 0)
idx_start = 0;
*local_start = idx_start;
*local_length = idx_length;
}
double l1r_fun::regularizer(double *w, int reg_length)
{
double ret = 0;
for (int i=0;i<reg_length;i++)
ret += fabs(w[i]);
return ret;
}
double l1r_fun::fun(double *w)
{
int i;
double f=0;
int l=prob->l;
double w_size = get_nr_variable();
int nnz = 0;
for (i=0;i<w_size;i++)
nnz += (w[i] != 0);
if (nnz == 0)
memset(Xw, 0, sizeof(double) * l);
else if ((double)nnz / w_size < 0.5)
{
int *index = new int[nnz];
double *subw = new double[nnz];
int counter = 0;
for (i=0;i<w_size;i++)
if (w[i] != 0)
{
subw[counter] = w[i];
index[counter++] = i;
}
Xv(subw, Xw, index, nnz);
delete[] index;
delete[] subw;
}
else
Xv(w, Xw);
reg = regularizer(w + start, length);
for(i=0;i<l;i++)
f += loss(i, Xw[i]);
double buffer[2] = {f, reg};
mpi_allreduce(buffer, 2, MPI_DOUBLE, MPI_SUM);
communication += 2 / global_n;
reg = buffer[1];
f = C * buffer[0] + reg;
current_f = f;
return(f);
}
double l1r_fun::f_update(double *w, double *step, double Q, double eta, int *index, int index_length, int localstart, int locallength)
{
if (Q > 0)
return current_f;
int *localindex = NULL;
double *localstep;
int i;
int inc = 1;
int l = prob->l;
double one = 1.0;
double *substep;
int *subindex;
int nnz = 0;
int len = index_length;
int w_size = get_nr_variable();
if (len == 0)
len = w_size;
double reg_diff = 0;
if (index != NULL) // If a subset is selected
{
localindex = index + localstart;
localstep = step + localstart;
for (i=0;i<locallength;i++)
reg_diff += fabs(w[localindex[i]] + localstep[i]) - fabs(w[localindex[i]]);
}
else
{
double *localw = w + start;
localstep = step + start;
for (i=0;i<length;i++)
reg_diff += fabs(localw[i] + localstep[i]) - fabs(localw[i]);
}
for (i=0;i<len;i++)
nnz += (step[i] != 0);
if (nnz == 0)
memset(z, 0, sizeof(double) * l);
else if (nnz < len * 0.5)
{
substep = new double[nnz];
subindex = new int[nnz];
int counter = 0;
if (index == NULL)
{
for (i=0;i<w_size;i++)
if (step[i] != 0)
{
substep[counter] = step[i];
subindex[counter++] = i;
}
}
else
{
for (i=0;i<len;i++)
if (step[i] != 0)
{
substep[counter] = step[i];
subindex[counter++] = index[i];
}
}
Xv(substep, z, subindex, nnz);
delete[] substep;
delete[] subindex;
}
else
Xv(step, z, index, index_length);
daxpy_(&l, &one, z, &inc, Xw, &inc);
double f_new = 0;
for(i=0; i<l; i++)
f_new += loss(i, Xw[i]);
f_new *= C;
f_new += reg_diff;
//If profiling gets sparsity, should consider and only updating individual losses
//This should be the case when data has sparsity and the update is extremely sparse
mpi_allreduce(&f_new, 1, MPI_DOUBLE, MPI_SUM);
communication += 1 / global_n;
f_new += reg;
if (f_new - current_f <= eta * Q)
{
mpi_allreduce(®_diff, 1, MPI_DOUBLE, MPI_SUM);
communication += 1 / global_n;
current_f = f_new;
reg += reg_diff;
}
else
{
double factor = -1;
daxpy_(&l, &factor, z, &inc, Xw, &inc);
}
return current_f;
}
void l1r_fun::loss_grad(double *w, double *g, const std::vector<int> &fullindex)
{
int i;
int n = prob->n;
int global_index_length = (int)fullindex.size();
update_zD();
XTv(z, g, fullindex);
if (fullindex[0] == -1)
{
mpi_allreduce(g, n, MPI_DOUBLE, MPI_SUM);
communication += 1.0;
}
else
{
mpi_allreduce(g, global_index_length, MPI_DOUBLE, MPI_SUM);
for (i = global_index_length - 1; i >= 0; i--)
{
if (fullindex[i] == i)
break;
g[fullindex[i]] = g[i];
g[i] = 0;
}
communication += (double) global_index_length / global_n;
}
}
void l1r_fun::Hv(double *s, double *Hs, const std::vector<int> &fullindex, double *w)
{
int i;
int l=prob->l;
int w_size=(int)fullindex.size();
Xv(s, z, &fullindex[0], w_size);
for(i=0;i<l;i++)
z[i] = C*D[i]*z[i];
XTv(z, Hs, fullindex);
mpi_allreduce(Hs, w_size, MPI_DOUBLE, MPI_SUM);
communication += w_size / global_n;
}
double l1r_fun::vHv(double *s, const std::vector<int> &index)
{
int i;
int inc = 1;
int l=prob->l;
int index_size = (int) index.size();
bool dense = (index[0] == -1 || index_size == get_nr_variable());
double *subs;
if (dense)
Xv(s, z);
else
{
subs = new double[index_size];
for (i=0;i<index_size;i++)
subs[i] = s[index[i]];
Xv(subs, z, index.data(), index_size);
}
double alpha = 0;
for(i=0;i<l;i++)
alpha += z[i] * z[i] * D[i];// v^THv = C (Xv)^T D (Xv)
double buffer[2];
double norm2;
if (dense)
norm2 = ddot_(&length, s + start, &inc, s + start, &inc);
else
{
int nr_node = mpi_get_size();
int rank = mpi_get_rank();
int shift = int(ceil(double(index_size) / double(nr_node)));
int indexstart = shift * rank;
int indexlength = min(max(index_size - indexstart, 0), shift);
if (indexlength == 0)
indexstart = 0;
norm2 = ddot_(&indexlength, subs + indexstart, &inc, subs + indexstart, &inc);
}
buffer[0] = alpha;
buffer[1] = norm2;
mpi_allreduce(buffer, 2, MPI_DOUBLE, MPI_SUM);
norm2 = buffer[1];
alpha = buffer[0] * C / norm2;
communication += 2 / global_n;
if (!dense)
delete[] subs;
return alpha;
}
void l1r_fun::get_diag_preconditioner(double *M, const std::vector<int> &fullindex, double *w)
{
int i;
int w_size=(int)fullindex.size();
feature_node **x = prob->x;
for (i=0; i<w_size; i++)
M[i] = 0.0;
for (i=0; i<w_size; i++)
{
feature_node *s;
s=x[fullindex[i]];
while (s->index!=-1)
{
M[i] += s->value*s->value*C*D[s->index - 1];
s++;
}
}
mpi_allreduce(M, w_size, MPI_DOUBLE, MPI_SUM);
communication += w_size / global_n;
}
void l1r_fun::full_grad(double *w, double *g, std::vector<int> &index, double *full_g, std::vector<int> &nonzero_set)
{
for (int i=0, full_i=0;i<(int) index.size();i++)
{
int j = index[i];
if (w[j] != 0)
{
nonzero_set.push_back(j);
full_g[full_i++] = g[j] + (2 * (w[j] > 0) - 1);
}
}
}
void l1r_fun::Xv(double *v, double *Xv, const int *index, int index_length)
{
int i;
int l=prob->l;
int w_size=get_nr_variable();
if (index_length > 0)
w_size = index_length;
feature_node **x=prob->x;
for(i=0;i<l;i++)
Xv[i]=0;
for(i=0;i<w_size;i++)
if (v[i] != 0)
{
feature_node *s;
if (index_length > 0)
s = x[index[i]];
else
s = x[i];
sparse_operator::axpy(v[i], s, Xv);
}
}
void l1r_fun::XTv(double *v, double *XTv, const std::vector<int> &fullindex)
{
int i;
int w_size=(int)fullindex.size();
if (fullindex[0] == -1)
w_size = get_nr_variable();
feature_node **x=prob->x;
for(i=0;i<w_size;i++)
{
feature_node * s;
if (fullindex[0] == -1)
s=x[i];
else
s=x[fullindex[i]];
XTv[i] = sparse_operator::dot(v, s);
}
}
void l1r_fun::setselection(double *w, double *loss_g, std::vector<int> &prev_index, std::vector<int> &curr_index, double shrinkage) //Some preliminary set selection strategy
{
// (input) prev_index: absolute global index
// (output) curr_index: absolute global index
curr_index.clear();
double M = 0;
M = shrinkage / get_nr_variable();
for (std::vector<int>::iterator it = prev_index.begin(); it != prev_index.end(); it++)
if (w[*it] != 0 || fabs(loss_g[*it]) > 1 - M)
curr_index.push_back(*it);
}
int l1r_fun::get_nr_variable(void)
{
return prob->n;
}
double l1r_fun::smooth_line_search(double *w, double *smooth_step, double delta, double eta, std::vector<int> &index, double *fnew)
{
double discard_threshold = 1e-4; //If the step size is too small then do not take this step
double discard_threshold2 = 1e-6; //If the step size is too small then do not take this step
int i;
int l = prob->l;
int inc = 1;
double step_size = 1;
int max_num_linesearch = 10;
for (i=0;i<(int)index.size();i++)
if (smooth_step[i] != 0)
{
double tmp = -w[index[i]] / smooth_step[i];
if (tmp > 0)
step_size = min(step_size, tmp);
}
if (step_size < discard_threshold)
{
info("INITIAL STEP SIZE TOO SMALL: %g\n",step_size);
*fnew = current_f;
return -1;
}
double reg_diff = 0;
for (i=0;i<(int) index.size();i++)
if (smooth_step[i] != 0)
reg_diff += fabs(w[index[i]] + step_size * smooth_step[i]) - fabs(w[index[i]]);
reg_diff /= step_size;
Xv(smooth_step, z, &index[0], (int) index.size());
int num_linesearch;
delta *= eta;
daxpy_(&l, &step_size, z, &inc, Xw, &inc);
for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
{
if (step_size < discard_threshold2)
{
info("LINE SEARCH FAILED: Step size = %g\n",step_size);
double factor = -step_size;
daxpy_(&l, &factor, z, &inc, Xw, &inc);
step_size = 0;
*fnew = current_f;
break;
}
double f_new = 0;
for(i=0; i<l; i++)
f_new += loss(i, Xw[i]);
//If profiling gets sparsity, should consider tracking loss as a sum and only update individual losses
mpi_allreduce(&f_new, 1, MPI_DOUBLE, MPI_SUM);
f_new = f_new * C + reg + reg_diff * step_size;
communication += 1 / global_n;
if (f_new - current_f <= delta * step_size)
{
current_f = f_new;
*fnew = f_new;
reg += reg_diff * step_size;
break;
}
else
{
step_size *= 0.5;
double factor = -step_size;
daxpy_(&l, &factor, z, &inc, Xw, &inc);
}
}
if (num_linesearch == max_num_linesearch)
{
info("LINE SEARCH FAILED: Step size = %g\n",step_size);
double factor = -step_size;
daxpy_(&l, &factor, z, &inc, Xw, &inc);
step_size = 0;
*fnew = current_f;
}
return step_size;
}
double l1r_fun::armijo_line_search(double *step, double *w, double *loss_g, double *step_size, double eta, int *num_line_search_steps, double *delta_ret, int *index, int indexlength, int localstart, int locallength)
{
int i;
int inc = 1;
int l = prob->l;
int max_num_linesearch = 100;
double localstepsize = (*step_size);
double reg_diff = 0;
int num_linesearch;
double delta = 0;
if (indexlength > 0)
{
for (i=0;i<locallength;i++)
reg_diff += fabs(w[index[localstart + i]] + step[localstart + i]) - fabs(w[index[localstart + i]]);
for (i=0;i<locallength;i++)
delta += loss_g[index[localstart + i]] * step[localstart + i];
}
else
{
for (i=0;i<length;i++)
reg_diff += fabs(w[start + i] + step[start + i]) - fabs(w[start + i]);
for (i=0;i<length;i++)
delta += loss_g[start + i] * step[start + i];
}
delta += reg_diff;
mpi_allreduce(&delta, 1, MPI_DOUBLE, MPI_SUM);
*delta_ret = delta;
communication += 1 / global_n;
delta *= eta;
if (indexlength > 0)
Xv(step, z, index, indexlength);
else
Xv(step, z);
daxpy_(&l, &localstepsize, z, &inc, Xw, &inc);
for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++)
{
double cond = 0;
for(i=0; i<l; i++)
cond += loss(i,Xw[i]);
cond *= C;
cond += reg_diff;
//If profiling gets sparsity, should consider tracking loss as a sum and only update individual losses
mpi_allreduce(&cond, 1, MPI_DOUBLE, MPI_SUM);
communication += 1 / global_n;
if (cond + reg - current_f <= delta * localstepsize)
{
mpi_allreduce(®_diff, 1, MPI_DOUBLE, MPI_SUM);
communication += 1 / global_n;
current_f = cond + reg;
reg += reg_diff;
break;
}
else
{
localstepsize *= 0.5;
double factor = -localstepsize;
daxpy_(&l, &factor, z, &inc, Xw, &inc);
reg_diff = 0;
if (indexlength > 0)
for (i=0;i<locallength;i++)
reg_diff += fabs(w[index[localstart + i]] + localstepsize * step[localstart + i]) - fabs(w[index[localstart + i]]);
else
for (i=0;i<length;i++)
reg_diff += fabs(w[start + i] + localstepsize * step[start + i]) - fabs(w[start + i]);
}
}
*num_line_search_steps = num_linesearch;
*step_size = localstepsize;
if (num_linesearch >= max_num_linesearch)
{
info("LINE SEARCH FAILED\n");
*step_size = 0;
}
return current_f;
}
void l1r_fun::prox_grad(double *w, double *g, double *local_step, double *oldd, double alpha, int index_length)
{
if (alpha <= 0)
{
int inc = 1;
alpha = ddot_(&length, g, &inc, g, &inc);
mpi_allreduce(&alpha, 1, MPI_DOUBLE, MPI_SUM);
communication += 1 / global_n;
alpha = sqrt(alpha);
}
if (index_length < 0)
index_length = length;
for (int i=0;i<index_length;i++)
{
double u = w[i] + oldd[i] - g[i] / alpha;
local_step[i] = max(fabs(u) - 1.0 / alpha, 0.0);
local_step[i] *= ((u > 0) - (u<0));
}
}
l1r_lr_fun::l1r_lr_fun(const problem *prob, double C): l1r_fun(prob, C)
{
}
l1r_lr_fun::~l1r_lr_fun()
{
delete[] z;
delete[] Xw;
delete[] D;
delete[] recv_count;
delete[] displace;
}
double l1r_lr_fun::loss(int i, double xw_i)
{
double yXw = prob->y[i]*xw_i;
if (yXw >= 0)
return log(1 + exp(-yXw));
else
return (-yXw+log(1 + exp(yXw)));
}
void l1r_lr_fun::update_zD()
{
for(int i=0;i<prob->l;i++)
{
z[i] = 1/(1 + exp(-prob->y[i]*Xw[i]));
D[i] = z[i]*(1-z[i]);
z[i] = C*(z[i]-1)*prob->y[i];
}
}
lasso::lasso(const problem *prob, double C): l1r_lr_fun(prob, C)
{
for (int i=0;i<prob->l;i++)
D[i] = 1.0;
}
lasso::~lasso()
{
}
double lasso::loss(int i, double xw_i)
{
double tmp = (prob->y[i] - xw_i);
return tmp * tmp / 2;
}
void lasso::update_zD()
{
for(int i=0;i<prob->l;i++)
z[i] = C * (Xw[i] - prob->y[i]);
}
l1r_l2_svc_fun::l1r_l2_svc_fun(const problem *prob, double C):
l1r_lr_fun(prob, C)
{
}
l1r_l2_svc_fun::~l1r_l2_svc_fun()
{
}
double l1r_l2_svc_fun::loss(int i, double wx_i)
{
double d = 1 - prob->y[i] * wx_i;
if (d > 0)
return C * d * d;
else
return 0;
}
void l1r_l2_svc_fun::update_zD()
{
for(int i=0;i<prob->l;i++)
{
z[i] = Xw[i] * prob->y[i];
if (z[i] < 1)
{
z[i] = 2 * C*prob->y[i]*(z[i]-1);
D[i] = 2;
}
else
{
z[i] = 0;
D[i] = 0;
}
}
}
grouplasso_mlr_fun::grouplasso_mlr_fun(const problem *prob, double C, int nr_class)
{
this->C = C;
this->nr_class = nr_class;
this->prob = prob;
int l=prob->l;
Xw_length = l * nr_class;
full_len = prob->n * nr_class;
z = new double[Xw_length];
Xw = new double[Xw_length];
D = new double[Xw_length];
this->C = C;
int w_size = prob->n;
int nr_node = mpi_get_size();
int rank = mpi_get_rank();
int shift = int(ceil(double(w_size) / double(nr_node)));
recv_count = new int[nr_node];
displace = new int[nr_node];
localfeature_start = shift * rank;
localfeatures = min(max(w_size - localfeature_start , 0), shift);
if (localfeatures == 0)
{
localfeature_start = 0;
}
this->length = localfeatures * nr_class;
this->start = localfeature_start * nr_class;
shift *= nr_class;
int counter = 0;
for (int i=0;i<nr_node;i++)
{
recv_count[i] = shift;
displace[i] = counter;
counter += shift;
if (counter >= full_len)
{
counter = 0;
shift = 0;
}
else if (counter + shift > full_len)
shift = full_len - counter;
}
}
grouplasso_mlr_fun::~grouplasso_mlr_fun()
{
delete[] z;
delete[] Xw;
delete[] D;
delete[] recv_count;
delete[] displace;
}
double grouplasso_mlr_fun::fun(double *w)
{
int i,j;
double f=0;
int nnz = 0;
int l=prob->l;
int w_size = get_nr_variable();
for (i=0;i<w_size;i+=nr_class)
{
int hit = 0;
for (j=i;j<i+nr_class;j++)
if (w[j] != 0)
{
hit = 1;
break;
}
nnz += hit;
}
nnz *= nr_class;
if (nnz == 0)
memset(Xw, 0, sizeof(double) * l * nr_class);
else if ((double)nnz / w_size < 0.5)
{
int *index = new int[nnz];