-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmm_master.cpp
220 lines (194 loc) · 8.31 KB
/
admm_master.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
#include "data.h"
#include "cmd_master.h"
#include <fstream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include "sagecal.h"
#include "utils.h"
#include "utils_dn.h"
using namespace std;
using namespace Data;
int admm_master(dlg_app_info *app) {
Data::MPIData iodata;
iodata.tilesz = Data::TileSize;
vector <string> msnames;
vector <string> inputs;
vector <struct MsIndex> msIndexs;
int ntasks = 0;
/**********************************************************/
ntasks = app->n_inputs;
iodata.Nms = app->n_inputs;
/**** get info from slaves ***************************************/
iodata.freqs = new double[iodata.Nms];
iodata.freq0 = 0.0;
iodata.N = iodata.M = iodata.totalt = 0;
int Mo = 0;
/* use iodata to store the results, also check for consistency of results */
for (unsigned int cm = 0; cm < app->n_inputs; cm++) {
Data::IOData data;
load_iodata_dn(&(app->inputs[cm]), &data);
cout << "Slave [" << data.msname << "] N=" << data.N << " M/Mt=" << data.M << "/" << data.Mt << " tilesz="
<< data.tilesz << " totaltime=" << data.totalt << " Freq=" << data.freq0 / 1e6<< "Mhz"<< endl;
msnames.push_back(string(data.msname));
struct MsIndex msIndex;
msIndex.cm = cm;
char ms_full_name[256];
sprintf(ms_full_name,"%s", data.msname);
ms_short_name(ms_full_name, msIndex.ms);
msIndexs.push_back(msIndex);
if (cm == 0) { /* update data */
iodata.N = data.N;
Mo = data.M;
iodata.M = data.Mt;
iodata.tilesz = data.tilesz;
iodata.totalt = data.totalt;
} else { /* compare against others */
if ((iodata.N != data.N) || (iodata.M != data.Mt) || (iodata.tilesz != data.tilesz)) {
cout << "Slave " << data.msname << " parameters do not match N=" << data.N << " M=" << data.M
<< " tilesz=" << data.tilesz << endl;
}
if (iodata.totalt < data.totalt) {
/* use max value as total time */
iodata.totalt = data.totalt;
}
}
iodata.freqs[cm] = data.freq0;
iodata.freq0 += data.freq0;
Data::freeData(data);
}
for (std::vector <struct MsIndex>::iterator iter = msIndexs.begin (); iter != msIndexs.end (); iter++) {
cout << (*iter).cm << "," << (*iter).ms << endl;
}
iodata.freq0 /= (double) iodata.Nms;
cout << "Reference frequency (MHz)=" << iodata.freq0 * 1.0e-6 << endl;
/* regularization factor array, size Mx1 one per each hybrid cluster */
double *arho, *arhoslave;
if ((arho = (double *) calloc((size_t) iodata.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
if ((arhoslave = (double *) calloc((size_t) Mo, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* if text file is given, read it and update rho array */
if (Data::admm_rho_file) {
read_arho_fromfile(Data::admm_rho_file, iodata.M, arho, Mo, arhoslave);
} else {
/* copy common value */
/* setup regularization factor array */
for (int p = 0; p < iodata.M; p++) {
arho[p] = admm_rho;
}
for (int p = 0; p < Mo; p++) {
arhoslave[p] = admm_rho;
}
}
/* interpolation polynomial */
double *B, *Bi;
/* Npoly terms, for each frequency, so Npoly x Nms */
if ((B = (double *) calloc((size_t) Npoly * iodata.Nms, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(B, 0, sizeof(double) * Npoly * iodata.Nms);
/* pseudoinverse */
if ((Bi = (double *) calloc((size_t) Npoly * Npoly, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(Bi, 0, sizeof(double) * Npoly * Npoly);
setup_polynomials(B, Npoly, iodata.Nms, iodata.freqs, iodata.freq0, (Npoly == 1 ? 1 : PolyType));
/* determine how many iterations are needed */
int Ntime = (iodata.totalt + iodata.tilesz - 1) / iodata.tilesz;
/* override if input limit is given */
if (Nmaxtime > 0 && Ntime > Nmaxtime) {
Ntime = Nmaxtime;
}
cout << "Master total timeslots=" << Ntime << endl;
if (!Data::admm_rho_file) {
cout << "ADMM iterations=" << Nadmm << " polynomial order=" << Npoly << " regularization=" << admm_rho << endl;
} else {
cout << "ADMM iterations=" << Nadmm << " polynomial order=" << Npoly << " regularization given by text file "
<< Data::admm_rho_file << endl;
}
/* send array to slaves */
/* update rho on each slave */
iodata.Mo = Mo;
/*------------------------------------share data file-------------------------------------------------------------*/
char shareFile[256];
sprintf(shareFile,"%s/v.mastershare",Data::shareDir);
FILE *sp = 0;
if ((sp = fopen(shareFile, "wb")) == 0) {
fprintf(stderr, "%s: %d: no output file\n", __FILE__, __LINE__);
return 1;
}
dump_mpidata(sp, &iodata);
fwrite(&ntasks, sizeof(int), 1, sp);
for (unsigned int cm = 0; cm < msIndexs.size(); cm++) {
fwrite(&(msIndexs[cm]), sizeof(struct MsIndex), 1, sp);
}
if (sp) {
fclose(sp);
}
/* dump single mpidata*/
dump_share_mpidata(Data::shareDir, &iodata);
write_share_XYZ(Data::shareDir, "master", arho, iodata.M, "arho");
write_share_XYZ(Data::shareDir, "master", B, Npoly * iodata.Nms, "B");
write_share_XYZ(Data::shareDir, "master", Bi, Npoly * Npoly, "Bi");
/* ADMM memory */
double *Z, *Y, *z;
/* Z: 2Nx2 x Npoly x M */
/* keep ordered by M (one direction together) */
if ((Z = (double *) calloc((size_t) iodata.N * 8 * Npoly * iodata.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(Z, 0, sizeof(double) * iodata.N * 8 * Npoly * iodata.M);
/* z : 2Nx2 x M x Npoly vector, so each block is 8NM */
if ((z = (double *) calloc((size_t) iodata.N * 8 * Npoly * iodata.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(z, 0, sizeof(double) * iodata.N * 8 * Npoly * iodata.M);
/* copy of Y+rho J, M times, for each slave */
/* keep ordered by M (one direction together) */
if ((Y = (double *) calloc((size_t) iodata.N * 8 * iodata.M * iodata.Nms, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(Y, 0, sizeof(double) * iodata.N * 8 * iodata.M * iodata.Nms);
/* need a copy to calculate dual residual */
double *Zold;
if ((Zold = (double *) calloc((size_t) iodata.N * 8 * Npoly * iodata.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(Zold, 0, sizeof(double) * iodata.N * 8 * Npoly * iodata.M);
double *fratio;
if ((fratio = (double *) calloc((size_t) iodata.Nms, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
memset(fratio, 0, sizeof(double) * iodata.Nms);
write_share_XYZ(Data::shareDir, "master", Z, iodata.N * 8 * Npoly * iodata.M, "Z");
write_share_XYZ(Data::shareDir, "master", z, iodata.N * 8 * Npoly * iodata.M, "z");
write_share_XYZ(Data::shareDir, "master", Y, iodata.N * 8 * iodata.M * iodata.Nms, "Y");
write_share_XYZ(Data::shareDir, "master", Zold, iodata.N * 8 * Npoly * iodata.M, "Zold");
write_share_XYZ(Data::shareDir, "master", fratio, iodata.Nms, "fratio");
/*------------------------------------output data file------------------------------------------------------------*/
dump_mpidata_dn(&(app->outputs[0]), &iodata);
app->outputs[0].write((char *) arhoslave, sizeof(double) * iodata.Mo);
/*-----------------------------------------------free ------------------------------------------------------------*/
delete[] iodata.freqs;
free(B);
free(arhoslave);
free(arho);
free(Z);
free(Zold);
free(z);
free(fratio);
cout << "[admm_master]=========, Done." << endl;
return 0;
}