-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_z_master.cpp
199 lines (183 loc) · 8.52 KB
/
update_z_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
//
// Created by wsl on 11/07/17.
//
#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 update_z_master(dlg_app_info *app) {
vector <struct MsIndex> msIndexs;
Data::MPIData mpiData;
int ntasks = 0;
int admm = get_last_iter(app->uid), ct = get_second_last_iter(app->uid);
cout << "[update_z_master]=======, admm=" << admm << ",ct=" << ct << endl;
/*------------------------------------read share------------------------------------------------------------------*/
char shareFile[256];
sprintf(shareFile,"%s/v.mastershare",Data::shareDir);
FILE *sp = 0;
if ((sp = fopen(shareFile, "rb")) == 0) {
fprintf(stderr, "%s: %d: no output file\n", __FILE__, __LINE__);
return 1;
}
load_mpidata(sp, &mpiData);
fread(&ntasks, sizeof(int), 1, sp);
for (unsigned int cm = 0; cm <ntasks; cm++) {
struct MsIndex msIndex = {};
fread(&msIndex, sizeof(struct MsIndex), 1, sp);
msIndexs.push_back(msIndex);
}
if (sp) {
fclose(sp);
}
/*------------------------------------var init--------------------------------------------------------------------*/
double *arho;
if ((arho = (double *) calloc((size_t) mpiData.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
double *B, *Bi;
/* Npoly terms, for each frequency, so Npoly x Nms */
if ((B = (double *) calloc((size_t) Npoly * mpiData.Nms, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* pseudoinverse */
if ((Bi = (double *) calloc((size_t) Npoly * Npoly, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
double *Z, *Y, *z;
/* Z: 2Nx2 x Npoly x M */
/* keep ordered by M (one direction together) */
if ((Z = (double *) calloc((size_t) mpiData.N * 8 * Npoly * mpiData.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* z : 2Nx2 x M x Npoly vector, so each block is 8NM */
if ((z = (double *) calloc((size_t) mpiData.N * 8 * Npoly * mpiData.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* Y, 2Nx2 , M times */
if ((Y = (double *) calloc((size_t) mpiData.N * 8 * mpiData.M * mpiData.Nms, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
/* need a copy to calculate dual residual */
double *Zold;
if ((Zold = (double *) calloc((size_t) mpiData.N * 8 * Npoly * mpiData.M, sizeof(double))) == 0) {
fprintf(stderr, "%s: %d: no free memory\n", __FILE__, __LINE__);
exit(1);
}
read_share_XYZ(Data::shareDir, "master", arho, mpiData.M, "arho");
read_share_XYZ(Data::shareDir, "master", B, Npoly * mpiData.Nms, "B");
read_share_XYZ(Data::shareDir, "master", Bi, Npoly * Npoly, "Bi");
read_share_XYZ(Data::shareDir, "master", Z , mpiData.N * 8 * Npoly * mpiData.M, "Z");
read_share_XYZ(Data::shareDir, "master", z, mpiData.N * 8 * Npoly * mpiData.M, "z");
read_share_XYZ(Data::shareDir, "master", Y, mpiData.N * 8 * mpiData.M * mpiData.Nms, "Y");
read_share_XYZ(Data::shareDir, "master", Zold, mpiData.N * 8 * Npoly * mpiData.M, "Zold");
/*------------------------------------input slave update Y--------------------------------------------------------*/
int flag = 0;
for (unsigned int cm = 0; cm < app->n_inputs; cm++) {
app->inputs[cm].read((char *)&flag, sizeof(int));
if (flag == 0) {
Data::IOData iodata;
load_iodata_dn_noprefix(&(app->inputs[cm]), &iodata);
// cout << "[update_z_master]======,"<< iodata.msname <<",iodata.N/M/Mt/Nms:" << iodata.N << "/"
// << iodata.M << "/" << iodata.Mt << "/" << iodata.Nms
// << ", iodata.freq0:" << iodata.freq0/ 1e6<< "Mhz" << endl;
char short_name[128], ms_full_name[256];
sprintf(ms_full_name,"%s", iodata.msname);
ms_short_name(ms_full_name, short_name);
for (unsigned int sm = 0; sm < msIndexs.size(); sm++) {
if(strcmp(short_name, msIndexs[sm].ms)==0) {
cout << "[update_z_master]======, find:" << msIndexs[sm].ms << ", idx:" << msIndexs[sm].cm << endl;
app->inputs[cm].read((char *)&Y[msIndexs[sm].cm * mpiData.N * 8 * mpiData.M], sizeof(double) * mpiData.N * 8 * mpiData.M);
break;
}
}
Data::freeData(iodata);
}
}
/*-----------------------------------process and share output-----------------------------------------------------*/
if(admm==0) {
calculate_manifold_average(mpiData.N, mpiData.M, mpiData.Nms, Y, 20, Data::Nt);
/* send updated Y back to each slave */
for (int im = 0; im < mpiData.Nms; im++) {
write_share_XYZ(Data::shareDir, msIndexs[im].ms, &Y[msIndexs[im].cm * mpiData.N * 8 * mpiData.M], mpiData.N * 8 * mpiData.M, "Y");
}
}
/* update Z */
/* add to 8NM vector, multiplied by Npoly different scalars, Nms times */
for (int ci = 0; ci < Npoly; ci++) {
my_dcopy(8 * mpiData.N * mpiData.M, Y, 1, &z[ci * 8 * mpiData.N * mpiData.M], 1);
my_dscal(8 * mpiData.N * mpiData.M, B[ci], &z[ci * 8 * mpiData.N * mpiData.M]);
}
for (int im = 1; im < mpiData.Nms; im++) {
for (int ci = 0; ci < Npoly; ci++) {
my_daxpy(8 * mpiData.N * mpiData.M, &Y[im * 8 * mpiData.N * mpiData.M], B[im * Npoly + ci],
&z[ci * 8 * mpiData.N * mpiData.M]);
}
}
/* also scale by 1/rho, only if rho>0, otherwise set it to 0.0*/
for (int im = 0; im < mpiData.M; im++) {
double invscale = 0.0;
if (arho[im] > 0.0) {
invscale = 1.0 / arho[im];
}
for (int ci = 0; ci < Npoly; ci++) {
my_dscal(8 * mpiData.N, invscale, &z[8 * mpiData.N * mpiData.M * ci + 8 * mpiData.N * im]);
}
}
/* find product z_tilde x Bi^T, z_tilde with proper reshaping */
my_dcopy(mpiData.N * 8 * Npoly * mpiData.M, Z, 1, Zold, 1);
update_global_z(Z, mpiData.N, mpiData.M, Npoly, z, Bi);
/* find dual error ||Zold-Znew|| */
my_daxpy(mpiData.N * 8 * Npoly * mpiData.M, Z, -1.0, Zold);
/* dual residual per one real parameter */
if (Data::verbose) {
cout << "ADMM : " << admm << " dual residual="
<< my_dnrm2(mpiData.N * 8 * Npoly * mpiData.M, Zold) / sqrt((double) 8 * mpiData.N * Npoly * mpiData.M)
<< endl;
} else {
cout << "Timeslot:" << ct << " ADMM:" << admm << endl;
}
/* send B_i Z to each slave */
for (int im = 0; im < mpiData.Nms; im++) {
for (int p = 0; p < mpiData.M; p++) {
memset(&z[8 * mpiData.N * p], 0, sizeof(double) * (size_t) mpiData.N * 8);
for (int ci = 0; ci < Npoly; ci++) {
my_daxpy(8 * mpiData.N, &Z[p * 8 * mpiData.N * Npoly + ci * 8 * mpiData.N], B[im * Npoly + ci],
&z[8 * mpiData.N * p]);
}
}
write_share_XYZ(Data::shareDir, msIndexs[im].ms, z, mpiData.N * 8 * mpiData.M, "Z");
}
/*-----------------------------------------share output-----------------------------------------------------------*/
write_share_XYZ(Data::shareDir, "master", B, Npoly * mpiData.Nms, "B");
write_share_XYZ(Data::shareDir, "master", Bi, Npoly * Npoly, "Bi");
write_share_XYZ(Data::shareDir, "master", Z , mpiData.N * 8 * Npoly * mpiData.M, "Z");
write_share_XYZ(Data::shareDir, "master", z, mpiData.N * 8 * Npoly * mpiData.M, "z");
write_share_XYZ(Data::shareDir, "master", Y, mpiData.N * 8 * mpiData.M * mpiData.Nms, "Y");
write_share_XYZ(Data::shareDir, "master", Zold, mpiData.N * 8 * Npoly * mpiData.M, "Zold");
/*-----------------------------------------------output-----------------------------------------------------------*/
dump_mpidata_dn(&(app->outputs[0]), &mpiData);
/*-----------------------------------------------free-------------------------------------------------------------*/
delete[] mpiData.freqs;
free(Z);
free(Zold);
free(z);
free(Y);
free(B);
free(Bi);
free(arho);
cout << "[update_z_master]=======, Done." << endl;
return 0;
}