-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_logit.c
358 lines (306 loc) · 11.4 KB
/
mpi_logit.c
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
/* COMP633 Parallel Computing @ UNC Chapel Hill, 2019
*
* PA2:
* Solve a distributed regularized logistic regression problem, i.e.,
*
* minimize sum( log(1 + exp(-b_i*(a_i'x + x_0)) ) ) + 0.5 * lambda * l(x).
*
* where l(x) = ||x||_1, l1 regularization.
* l(x) = ||x||_2^2, l2 regularization.
*
* The implementation uses MPI (Message Passing Interface) for distributed communication
* and the GNU Scientific Library (GSL) for math and solver.
*
* @author: Haidong Yi, [email protected]
* Minzhi Jiang, [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <mpi.h>
#include "header/mmio.h"
#include "header/solver.h"
void soft_threshold(gsl_vector *v, double k);
double objective(gsl_vector *x, void *param, gsl_vector *z, double lambda, char *type);
int main(int argc, char **argv)
{
int opt;
char type[3];
/* set default configs */
static int MAX_ITER = 1000;
static double RELTOL = 1e-3;
static double ABSTOL = 1e-5;
int isPrint = 0;
char dirA[80], dirb[80], dir_soln[80];
/* command line parser */
while ((opt = getopt(argc, argv, "A:b:e:E:r:t:o:p")) != -1) {
switch (opt)
{
case 'A':
if (strlen(optarg) > 80) {
fprintf(stderr, "file path -%s is too long\n", optarg);
}
strcpy(dirA, optarg);
break;
case 'b':
if (strlen(optarg) > 80) {
fprintf(stderr, "file path %s is too long, please shorten the path name, or use relative path, existing...\n", optarg);
}
strcpy(dirb, optarg);
break;
case 'e':
if ((RELTOL = atof(optarg)) == 0) {
fprintf(stderr, "Option -%c's argument %s is invalid\n", optopt, optarg);
return EXIT_FAILURE;
}
break;
case 'E':
if ((ABSTOL = atof(optarg)) == 0) {
fprintf(stderr, "Option -%c's argument %s is invalid\n", optopt, optarg);
return EXIT_FAILURE;
}
break;
case 't':
if ((MAX_ITER = atoi(optarg)) == 0) {
fprintf(stderr, "Option -%c's argument %s is invalid\n", optopt, optarg);
return EXIT_FAILURE;
}
break;
case 'r':
if (strcmp(optarg, "l1") == 0 || strcmp(optarg, "l2") == 0)
strcpy(type, optarg);
else {
fprintf(stderr, "Option -%c only supports argument %s or %s\n", optopt, "l1", "l2");
return EXIT_FAILURE;
}
break;
case 'p':
isPrint = 1;
break;
case 'o':
if (strlen(optarg) > 80)
fprintf(stderr, "file path -%s is too long\n", optarg);
strcpy(dir_soln, optarg);
break;
case '?':
if (optopt == 'A'|| optopt == 'b' || optopt == 'r' || optopt == 'e' || optopt == 't' || optopt == 'E')
fprintf(stderr, "Option -%c needs an argument\n", optopt);
else
fprintf(stderr, "Unknown option %c. \n", optopt);
return EXIT_FAILURE;
break;
default:
abort();
}
}
int rank;
int size=4;
MPI_Init(&argc, &argv); // Initialize the MPI execution environment
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Determine current running process
MPI_Comm_size(MPI_COMM_WORLD, &size); // Total number of processes
double N = (double) size; // Number of subsystems/slaves for ADMM
/* Read the data in very subsystem slaver */
FILE *f;
int m, n;
int row, col;
double entry;
sprintf(dirA, "%s%d.dat", dirA, rank + 1);
f = fopen(dirA, "r");
if (f == NULL) {
printf("ERROR: %s does not exist, exiting.\n", dirA);
exit(EXIT_FAILURE);
}
printf("process [%d] is reading %s\n", rank, dirA);
mm_read_mtx_array_size(f, &m, &n);
gsl_matrix *A = gsl_matrix_calloc(m, n);
for (int i = 0; i < m*n; i++) {
row = i % m;
col = floor(i/m);
fscanf(f, "%lf", &entry);
gsl_matrix_set(A, row, col, entry);
}
fclose(f);
/* Reading response vector b */
sprintf(dirb, "%s%d.dat", dirb, rank + 1);
f = fopen(dirb, "r");
if (f == NULL) {
printf("ERROR: %s does not exist, exiting.\n", dirb);
exit(EXIT_FAILURE);
}
printf("process [%d] is reading %s\n", rank, dirb);
mm_read_mtx_array_size(f, &m, &n);
gsl_vector *b = gsl_vector_calloc(m);
for (int i = 0; i < m; i++) {
fscanf(f, "%lf", &entry);
gsl_vector_set(b, i, entry);
}
fclose(f);
m = A->size1;
n = A->size2;
/* These are all variable definitions using by ADMM algorithm */
double rho = 1.0; // augmented Lagrangian parameter
double lambda = 1.0; // regularized parameter
gsl_vector *x = gsl_vector_calloc(n+1); // primal variable
gsl_vector *z = gsl_vector_calloc(n+1); // primal variable (splited from x)
gsl_vector *u = gsl_vector_calloc(n+1); // scaled dual variable i.e. u = (1/rho)y
gsl_vector *r = gsl_vector_calloc(n+1); // residual variable i.e. r = x - z
gsl_vector *zprev = gsl_vector_calloc(n+1); // used fro save previous iter z
gsl_vector *zdiff = gsl_vector_calloc(n+1); // difference between consecutive z, i.e. zdiff = z^{(i+1)} - z^{(i)}
gsl_vector *w = gsl_vector_calloc(n+1); // used for aggregate x + z in different process
double send[3]; // aggregate three scalars, i.e. [||r||^2, ||x||^2, ||u||^2]
double recv[3]; // reveive aggregated three scalars.
double nxstack = 0;
double nustack = 0;
double prires = 0;
double dualres = 0;
double eps_pri = 0;
double eps_dual = 0;
// /* Main ADMM solver loop */
int iter = 0;
if (isPrint) {
printf("Config: #proc: %d\tReg: %s\tMax_Iter: %6d\tRELTOL: %.6f\tABSTOL: %.6f\n", rank+1, type, MAX_ITER, RELTOL, ABSTOL);
printf("%3s %10s %10s %10s %10s %10s\n", "#", "r norm", "eps_pri", "s norm", "eps_dual", "objective");
}
/* init model parameters */
struct Param param = {A, b, u, z, rho};
gsl_vector_set_all(x, 0.);
MPI_Barrier(MPI_COMM_WORLD);
double start = MPI_Wtime();
double time_x = 0;
while(iter < MAX_ITER) {
/* x-update: via gsl multi-dimensional optimiation*/
double x_start = MPI_Wtime();
x = update_x(¶m, x); // warm start with previous x
double x_end = MPI_Wtime();
time_x += (x_end - x_start);
/* prepare for the Message passing: compute the global
sum of x and z, then update z.
*/
gsl_vector_memcpy(w, x);
gsl_vector_add(w, u); // w = x + u
/* z-update: Aggregate x + u uing MPI ALLreduce*/
gsl_vector_memcpy(zprev, z);
MPI_Allreduce(w->data, z->data, n, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
gsl_vector_scale(z, 1/N);
gsl_vector_view z1_n = gsl_vector_subvector(z, 1, n);
if (strcmp(type, "l1") == 0) {
/* l1 reg */
soft_threshold(&z1_n.vector, 0.5*lambda/(N*rho)); // l1: z1 = S_{lambda/(2 * rho)} (x1 + u1)
} else if (strcmp(type, "l2") == 0) {
/* l2 reg */
gsl_vector_scale(&z1_n.vector, N*rho/(N*rho + lambda));
}
/* u-update: u = u + x - z */
gsl_vector_add(u, x);
gsl_vector_sub(u, z);
/* Compute residual: r = x - z */
gsl_vector_memcpy(r, x);
gsl_vector_sub(r, z);
gsl_blas_ddot(r, r, &send[0]);
gsl_blas_ddot(x, x, &send[1]);
gsl_blas_ddot(u, u, &send[2]);
MPI_Allreduce(send, recv, 3, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
prires = sqrt(recv[0]); // sqrt(sum ||r_i||_2^2)
nxstack = sqrt(recv[1]); // sqrt(sum ||x_i||_2^2)
nustack = sqrt(recv[2]); // sqrt(sum ||u_i||_2^2)
/* Termination checks */
/* dual residual */
gsl_vector_memcpy(zdiff, z);
gsl_vector_sub(zdiff, zprev);
dualres = sqrt(N) * rho * gsl_blas_dnrm2(zdiff); /* ||s^k||_2^2 = N rho^2 ||z - zprev||_2^2 */
/* compute primal and dual feasibility tolerances */
eps_pri = sqrt(n*N)*ABSTOL + RELTOL * fmax(nxstack, sqrt(N) * gsl_blas_dnrm2(z));
eps_dual = sqrt(n*N)*ABSTOL + RELTOL * rho * nustack;
/* check the object values */
if (rank == 0 && isPrint) {
printf("%3d %10.4f %10.4f %10.4f %10.4f %10.4f\n", iter,
prires, eps_pri, dualres, eps_dual, objective(z, ¶m, z, lambda, type));
}
if (prires <= eps_pri && dualres <= eps_dual) {
break;
}
iter++;
}
MPI_Barrier(MPI_COMM_WORLD);
double end = MPI_Wtime();
MPI_Finalize(); /* Close the MPI execuation */
/* write the solutions using the master process */
if (rank == 0) {
printf("Total execuation time is %.8f/iter, time_x: %.8f.\n", (end - start)/iter, time_x/iter);
f = fopen(dir_soln, "w");
printf("Writing solutions to %s\n", dir_soln);
gsl_vector_fprintf(f, z, "%lf");
fclose(f);
printf("Done\n");
}
/* Clear memory */
gsl_matrix_free(A);
gsl_vector_free(b);
gsl_vector_free(x);
gsl_vector_free(u);
gsl_vector_free(z);
gsl_vector_free(r);
gsl_vector_free(zprev);
gsl_vector_free(zdiff);
gsl_vector_free(w);
return 0;
}
/*
* Implement a soft threshold element-wise function, i.e.
*
* { v - k if v > k
* S_k(v) = { 0 if |v|<= k
* { v + k if v < -k
*
* @param v: input vector
* @param k: threshold paramter
*/
void soft_threshold(gsl_vector *v, double k) {
double vi;
for (int i = 0; i < v->size; i++) {
vi = gsl_vector_get(v, i);
if (vi > k) { gsl_vector_set(v, i, vi - k); }
else if (vi < -k) { gsl_vector_set(v, i, vi + k); }
else { gsl_vector_set(v, i, 0); }
}
}
/*
* Implement the computation the objective function, i.e.
*
* sum( log(1 + exp(-b_i*(a_i'x + x_0)) ) ) + 0.5 * lambda * l(x).
*
* where l(x) = ||x||_1, l1 regularization.
* l(x) = ||x||_2^2, l2 regularization.
*
*/
double objective(gsl_vector *x, void *param, gsl_vector *z, double lambda, char *type) {
gsl_vector *y = ((struct Param *)param)->y;
gsl_matrix *X = ((struct Param *)param)->X;
int n = X->size1;
int p = x->size;
gsl_vector *X_beta = gsl_vector_calloc(n);
double beta_0 = gsl_vector_get(x, 0);
gsl_vector_const_view beta = gsl_vector_const_subvector(x, 1, p-1);
gsl_blas_dgemv(CblasNoTrans, 1, X, &beta.vector, 0, X_beta);
gsl_vector_add_constant(X_beta, beta_0);
gsl_vector_mul(X_beta, y); //
gsl_vector_scale(X_beta, -1); //
double obj = 0.;
gsl_vector_view z_1 = gsl_vector_subvector(z, 1, p-1);
if (strcmp(type, "l1") == 0) {
double reg_nrm = gsl_blas_dasum(&z_1.vector);
obj += 0.5 * lambda * reg_nrm;
} else if (strcmp(type, "l2") == 0) {
double reg_nrm = gsl_blas_dnrm2(&z_1.vector);
obj += 0.5 * lambda * pow(reg_nrm, 2);
}
for (int i = 0; i < X_beta->size; i++) {
double vi;
vi = gsl_vector_get(X_beta, i);
vi = log(1 + exp(vi));
obj += vi;
} /* for i */
gsl_vector_free(X_beta);
return obj;
}