| 1 |
// MPIGMRSmacro.idp file.. version 0.1 |
| 2 |
// include in MPIGMRES.edp file .. |
| 3 |
// Frederic Hecht 2010 |
| 4 |
// LGPL licence |
| 5 |
// Author [email protected] |
| 6 |
// ------------------------------- |
| 7 |
|
| 8 |
// macro to def // send-recv message |
| 9 |
// ----------------------------------- |
| 10 |
|
| 11 |
/******************************************************************/ |
| 12 |
func real maxM(matrix & A) |
| 13 |
{ |
| 14 |
int[int] I(1),J(1); |
| 15 |
real[int] C(1); |
| 16 |
[I,J,C] =A; |
| 17 |
return C.max ; |
| 18 |
} |
| 19 |
|
| 20 |
/******************************************************************/ |
| 21 |
macro InitU(n,Vh,Th,aTh,U) |
| 22 |
Vh[int] U(n); |
| 23 |
for(int j=0;j<n;++j) |
| 24 |
{ |
| 25 |
Th=aTh[j]; |
| 26 |
U[j]=0; |
| 27 |
} |
| 28 |
//EOM |
| 29 |
|
| 30 |
/******************************************************************/ |
| 31 |
macro ISendRecvAny(comm,jpart,Si,Ri) |
| 32 |
{ /* sorry no Irecv of complex objet ...*/ |
| 33 |
int n= jpart.n,nn=n+n; |
| 34 |
if(vdebug) cout << mpirank << " --ISendRecvAny " << n << endl; |
| 35 |
mpiRequest[int] rq(nn); |
| 36 |
|
| 37 |
for (int j=0;j<n;++j) |
| 38 |
Irecv(processor(jpart[j],comm,rq[j]),Ri[j]); |
| 39 |
if(vdebug) cout << mpirank << " ++ISendRecvAny " << n << endl; |
| 40 |
|
| 41 |
for (int j=0;j<n;++j) |
| 42 |
Isend(processor(jpart[j],comm,rq[n+j]),Si[j]); |
| 43 |
for (int j=0;j<nn;++j) |
| 44 |
{ |
| 45 |
int k= mpiWaitAny(rq); |
| 46 |
if(vdebug) |
| 47 |
cout << " ++ISendRecvAny wait: get " << k << endl; |
| 48 |
} |
| 49 |
} |
| 50 |
//EOM |
| 51 |
|
| 52 |
/******************************************************************/ |
| 53 |
macro SendRecvAny(comm,jpart,Si,Ri) |
| 54 |
{ |
| 55 |
int n= jpart.n; |
| 56 |
for (int j=0;j<n;++j) |
| 57 |
processor(comm,jpart[j]) << Si[j]; |
| 58 |
cout << " ** wait revc " << endl; |
| 59 |
for (int j=0;j<n;++j) |
| 60 |
processor(comm,jpart[j]) >> Ri[j]; |
| 61 |
} |
| 62 |
// EOM |
| 63 |
|
| 64 |
/******************************************************************/ |
| 65 |
macro CheckUV(comm,jpart,Si,Ri) |
| 66 |
{ |
| 67 |
int n= jpart.n; |
| 68 |
int[int] rn(n),sn(n),in(n); |
| 69 |
for (int j=0;j<n;++j) |
| 70 |
{ sn[j]= Si[j][].n; |
| 71 |
processor(jpart[j],comm) << sn[j]; |
| 72 |
processor(jpart[j],comm) << ipart; |
| 73 |
} |
| 74 |
for (int j=0;j<n;++j) |
| 75 |
processor(jpart[j],comm) >> rn[j]; |
| 76 |
for (int j=0;j<n;++j) |
| 77 |
processor(jpart[j],comm) >> in[j]; |
| 78 |
|
| 79 |
int err=0; |
| 80 |
for (int j=0;j<n;++j) |
| 81 |
{ |
| 82 |
int rj=Ri[j][].n; |
| 83 |
err += rj != rn[j]; |
| 84 |
cout << rj << " s ========= r " << rn[j] << " " << ipart << " <-> " << in[j] << " " << err << endl; |
| 85 |
|
| 86 |
} |
| 87 |
assert(err==0); |
| 88 |
}//EOM |
| 89 |
|
| 90 |
/******************************************************************/ |
| 91 |
macro SendRecvUV(comm,jpart,Si,Ri) |
| 92 |
{ |
| 93 |
int n= jpart.n; |
| 94 |
mpiRequest[int] rq(n); |
| 95 |
for (int j=0;j<n;++j) |
| 96 |
Irecv(processor(jpart[j],comm,rq[j]), Ri[j][]); |
| 97 |
|
| 98 |
for (int j=0;j<n;++j) |
| 99 |
processor(jpart[j],comm) << Si[j][]; |
| 100 |
/* for (int j=0;j<n;++j) |
| 101 |
processor(jpart[j],comm) >> Ri[j][];*/ |
| 102 |
|
| 103 |
for (int j=0;j<n;++j) |
| 104 |
int k= mpiWaitAny(rq); |
| 105 |
} |
| 106 |
// EOF |
| 107 |
|
| 108 |
/* Trick */ |
| 109 |
|
| 110 |
/**********************************************************/ |
| 111 |
macro BuildPartitioning(sizeoverlaps,mesh,Thg,Thi,aThij,RAS,pii,jpart,comm,vdebug) |
| 112 |
/**********************************************************/ |
| 113 |
|
| 114 |
int[int] jpart(mpiSize(comm)); /* list of jpart */ |
| 115 |
mesh[int] aThij(1); |
| 116 |
mesh Thi,Thii; |
| 117 |
Thi=Thg; |
| 118 |
Thii=Thg; |
| 119 |
fespace Vhi(Thi,P1); /**/ |
| 120 |
Vhi pii; |
| 121 |
|
| 122 |
{ |
| 123 |
int npart = mpiSize(comm); |
| 124 |
fespace Phg(Thii,P0); |
| 125 |
fespace Vhg(Thii,P1); |
| 126 |
|
| 127 |
int njpart=0; |
| 128 |
int ipart= mpiRank(comm); |
| 129 |
/* a good trick : int#mesh = */ |
| 130 |
func intmesh= int2d; |
| 131 |
func intmesh3=int3d; |
| 132 |
/* end trick */ |
| 133 |
int dplot1 =0; |
| 134 |
int vdebug1 = vdebug; |
| 135 |
Phg part; |
| 136 |
mesh Thin; |
| 137 |
/* build the partitioning ... */ |
| 138 |
{ |
| 139 |
part=0; |
| 140 |
if(npart>1 && ipart==0) |
| 141 |
metisdual(part[],Thg,npart); |
| 142 |
|
| 143 |
broadcast(processor(0,comm),part[]); |
| 144 |
|
| 145 |
} /* build ... */ |
| 146 |
|
| 147 |
|
| 148 |
if(vdebug>10&& mpirank==0 && !NoGraphicWindow) |
| 149 |
plot(part,fill=1,cmm="dual",wait=1); |
| 150 |
|
| 151 |
/* overlapping partition */ |
| 152 |
Vhi unssd; |
| 153 |
int nnn = sizeoverlaps*2;/* to be sure exact in thii */ |
| 154 |
|
| 155 |
{ |
| 156 |
int[int] n2o(1); |
| 157 |
Phg suppi= abs(part-ipart)<0.1; |
| 158 |
/* boolean function 1 in the subdomain 0 elswhere */ |
| 159 |
Thin=trunc(Thg,suppi>0,label=10,renum=false); /* non-overlapping mesh, interfaces have label 10 */ |
| 160 |
AddLayers(Thg,suppi[],nnn,unssd[]); /* see above ! suppi and unssd are modified */ |
| 161 |
unssd[] *= nnn; /* to put value nnn a 0 */ |
| 162 |
/* build a local mesh of thii such that all compuation of the unit partition are */ |
| 163 |
/* exact in thii */ |
| 164 |
Thii=trunc(Thg,unssd> 0.001 ,label=10,new2old=n2o,renum=false); /* overlapping mesh, interfaces have label 10 */ |
| 165 |
unssd= unssd;/* put on Thii*/ |
| 166 |
real[int] p=part[]; |
| 167 |
part=0.; part[]=p(n2o); |
| 168 |
|
| 169 |
real nnn0 = nnn - sizeoverlaps + 0.001 ; |
| 170 |
Thi=trunc(Thii,unssd>nnn0 ,label=10,renum=false); /* overlapping mesh, interfaces have label 10 */ |
| 171 |
|
| 172 |
} |
| 173 |
settt |
| 174 |
|
| 175 |
|
| 176 |
int npij=npart; |
| 177 |
Vhi[int] pij(npij);/* local partition of unit + pii */ |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
{ /* |
| 182 |
construction of the partition of the unit, |
| 183 |
let phi_i P1 FE function 1 on Thin and zero ouside of Thi and positive |
| 184 |
the partition is build with |
| 185 |
$$ p_i = phi_i/ \sum phi_i |
| 186 |
|
| 187 |
to build the partition of one domain i |
| 188 |
we nned to find all j such that supp(phi_j) \cap supp(phi_j) is not empty |
| 189 |
<=> int phi_j |
| 190 |
*/ |
| 191 |
|
| 192 |
|
| 193 |
{ |
| 194 |
/* find all j mes (supp(p_j) \cap supp(p_i)) >0 */ |
| 195 |
/* compute all phi_j on Thii */ |
| 196 |
/* remark supp p_i include in Thi */ |
| 197 |
/* */ |
| 198 |
fespace Phii(Thii,P0); |
| 199 |
fespace Vhii(Thii,P1); |
| 200 |
Vhi sumphi=0; |
| 201 |
jpart=0; |
| 202 |
njpart=0; |
| 203 |
if(ipart==0) |
| 204 |
cout <<" sizeoverlaps=" << sizeoverlaps << " " << " nnn = " << nnn<< endl; |
| 205 |
if(dplot1) plot(unssd,wait=1,cmm=" bbbb"); |
| 206 |
|
| 207 |
pii= max(unssd-sizeoverlaps,0.)/sizeoverlaps; |
| 208 |
if(RAS) pii[]=pii[] ? 1. : 0.; |
| 209 |
if(dplot1) plot(pii,wait=1,cmm=" 0000 max="+pii[].max); |
| 210 |
sumphi[] += pii[]; |
| 211 |
if(dplot1) plot(sumphi,wait=1,cmm=" summ 0000"); |
| 212 |
Vhii phii=0; |
| 213 |
real epsmes=1e-10*Thii.mesure; |
| 214 |
/*cout << " epsmes = " << epsmes <<endl;*/ |
| 215 |
for (int i=0;i<npart;++i) |
| 216 |
if(i != ipart ) |
| 217 |
{ |
| 218 |
Phii suppii=abs(i-part)<0.2; |
| 219 |
if(suppii[].max > 0.5) |
| 220 |
{ |
| 221 |
AddLayers(Thii,suppii[],sizeoverlaps,phii[]); |
| 222 |
if(RAS) |
| 223 |
{ phii[]=phii[] ? 1. : 0.; |
| 224 |
if(dplot1) plot(phii,wait=1,cmm=" RAS ij"); |
| 225 |
|
| 226 |
} |
| 227 |
assert(phii[].min >= -1e-10); |
| 228 |
real interij = int#mesh(Thi)( phii); |
| 229 |
if(interij>epsmes) |
| 230 |
{ |
| 231 |
pij[njpart]=abs(phii); |
| 232 |
if(vdebug1>2) cout << " ***** " << int#mesh(Thi)(real(pij[njpart])<0) << " " <<pij[njpart][].min << " " << phii[].min << endl; |
| 233 |
assert(int#mesh(Thi)(real(pij[njpart])<0) ==0); |
| 234 |
if(dplot1) plot(pij[njpart],wait=1,cmm=" j = "+ i + " " + njpart); |
| 235 |
sumphi[] += pij[njpart][]; |
| 236 |
if(dplot1) plot(sumphi,wait=1,cmm=" sum j = "+ i + " " + njpart); |
| 237 |
jpart[njpart++]=i; |
| 238 |
}}} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
if(dplot1) plot(sumphi,wait=1,dim=3,cmm="sum ",fill=1 ); |
| 243 |
pii[]=pii[] ./ sumphi[]; |
| 244 |
for (int j=0;j<njpart;++j) |
| 245 |
pij[j][] = pij[j][] ./ sumphi[]; |
| 246 |
jpart.resize(njpart); |
| 247 |
for (int j=0;j<njpart;++j) |
| 248 |
assert(pij[j][].max<=1); |
| 249 |
{ |
| 250 |
cout << ipart << " number of jpart " << njpart << " : "; |
| 251 |
for (int j=0;j<njpart;++j) |
| 252 |
cout << jpart[j] << " "; |
| 253 |
cout << endl; |
| 254 |
} |
| 255 |
sumphi[]=pii[]; |
| 256 |
for (int j=0;j<njpart;++j) |
| 257 |
sumphi[]+= pij[j][]; |
| 258 |
if(vdebug1>2) |
| 259 |
cout << " sum min " <<sumphi[].min << " " << sumphi[].max << endl; |
| 260 |
assert(sumphi[].min> 1.-1e-6 && sumphi[].max< 1.+1e-6); |
| 261 |
/* verification */ |
| 262 |
}}/* (Thii is remove here) */ |
| 263 |
/* end of the construction of the local partition of the unity ... */ |
| 264 |
/* on Thi ... */ |
| 265 |
/* ----------------------------------------------------------------- */ |
| 266 |
if(mpiRank(comm)==0) cout << " *** end build partition " << endl; |
| 267 |
|
| 268 |
/* computation of number of intersection .. */ |
| 269 |
/* ------------------------------------------ */ |
| 270 |
|
| 271 |
/* here pii and the pij is the locate partition of the unite on */ |
| 272 |
/* Thi ( mesh with overlap ).... */ |
| 273 |
/*Thi=splitmesh(Thi,2); */ |
| 274 |
if(dplot1 ) |
| 275 |
{ plot(Thi,pii,wait=1); |
| 276 |
for(int j=0;j<njpart;++j) |
| 277 |
plot(pij[j],cmm=" j="+j ,wait=1); } |
| 278 |
|
| 279 |
/* Partition of the unity on Thi .. */ |
| 280 |
/* computation of message. */ |
| 281 |
/* all j> we have to recive */ |
| 282 |
/* data on intersection of the support of pij[0] and pij[j] */ |
| 283 |
settt |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
aThij.resize(njpart); |
| 288 |
/* construction of the mesh intersect i,j part */ |
| 289 |
for(int jp=0;jp<njpart;++jp) |
| 290 |
aThij[jp] = trunc(Thi,pij[jp]>1e-6,label=10,renum=false); /* mesh of the supp of pij */ |
| 291 |
} |
| 292 |
if(mpiRank(comm)==0) cout << " *** end build mesh intersection " << endl; |
| 293 |
// EOM |
| 294 |
macro defPk1(i,j) i j // |
| 295 |
macro defPk2(i,j) [i j,i#1 j]// |
| 296 |
macro defPk3(i,j) [i j,i#1 j,i#2 j]// |
| 297 |
macro defPk4(i,j) [i j,i#1 j,i#2 j,i#3 j]// |
| 298 |
macro defPk5(i,j) [i j,i#1 j,i#2 j,i#3 j, i#4 j]// |
| 299 |
macro Times1(i) i // |
| 300 |
macro Times2(i) [i,i]// |
| 301 |
macro Times3(i) [i,i,i]// |
| 302 |
macro Times4(i) [i,i,i,i]// |
| 303 |
macro Times5(i) [i,i,i,i,i]// |
| 304 |
macro InitUdef(n,Vh,Th,aTh,U,N) |
| 305 |
Vh[int] defPk#N(U,)(n); |
| 306 |
for(int j=0;j<n;++j) |
| 307 |
{ |
| 308 |
Th=aTh[j]; |
| 309 |
defPk#N(U,[j])=Times#N(0.); |
| 310 |
} |
| 311 |
//EOM |
| 312 |
|
| 313 |
|
| 314 |
/******************************************************************/ |
| 315 |
macro BuildTransferMat2(ipart,mesh,Pk,N,U2V,Thi,Whi,Whij,Thij,aThij,Usend,Vrecv,jpart,Pii,vdebug) |
| 316 |
/******************************************************************/ |
| 317 |
/* construction of transfert matrix |
| 318 |
mesh: mesh or mesh3 |
| 319 |
Pk : the Pk def |
| 320 |
N : the number of componant |
| 321 |
Whi: the FE space on Thi (def here) |
| 322 |
Whij: the array of FE on each intersection |
| 323 |
Thij: the mesh variable to change Whij mesh |
| 324 |
aThij: the array of intersect mesh with Thi |
| 325 |
|
| 326 |
jpart[j]: the j th Sub Domain intersecting Thi |
| 327 |
*/ |
| 328 |
|
| 329 |
|
| 330 |
matrix[int] sMj(jpart.n), rMj(jpart.n); /* M of send to j */ |
| 331 |
fespace Whi(Thi,Pk); |
| 332 |
mesh Thij=Thi; |
| 333 |
fespace Whij(Thij,Pk);/* */ |
| 334 |
matrix Pii; |
| 335 |
{ |
| 336 |
|
| 337 |
int njpart= jpart.n; /* njpart: number of intersection which */ |
| 338 |
Whi defPk#N(wpii,)=Times#N(pii); |
| 339 |
Pii = wpii[]; |
| 340 |
for(int jp=0;jp<njpart;++jp) |
| 341 |
{ |
| 342 |
int j=jpart[jp]; |
| 343 |
Thij = aThij[jp]; |
| 344 |
if(vdebug>99)savemesh(Thij,"Th-"+j+"--"+ipart+".mesh"); |
| 345 |
int [int] U2Vdata=[0]; |
| 346 |
matrix I = interpolate(Whij,Whi,U2Vc=U2Vdata); /* Whji <- Whi */ |
| 347 |
sMj[jp] = I*Pii; /* Whi -> s Whij */ |
| 348 |
rMj[jp] = interpolate(Whij,Whi,t=1,U2Vc=U2Vdata); /* Whji -> Whi */ |
| 349 |
if(vdebug>10) { |
| 350 |
{Whi defPk#N(uuu,)=Times#N(1); Whij defPk#N(vvv,)=Times#N(-1); |
| 351 |
cout << vvv[].n <<"+=" << I.n << "x" << I.m << "*" << uuu[].n << endl; |
| 352 |
vvv[]+=I*uuu[]; cout << jp << " %%% " << vvv[].linfty << endl; assert(vvv[].linfty < 1e-6);} |
| 353 |
{Whi defPk#N(uuu,)=Times#N(1); Whij defPk#N(vvv,)=Times#N(-1) ; |
| 354 |
vvv[]+=rMj[jp]'*uuu[]; cout << jp << " ### " << vvv[].linfty << endl; assert(vvv[].linfty < 1e-6);}} |
| 355 |
}} |
| 356 |
if(ipart==0) cout << " *** end build transfert matrix " << endl; |
| 357 |
settt |
| 358 |
|
| 359 |
/* alloc array of send and recv data .. */ |
| 360 |
|
| 361 |
InitUdef(jpart.n,Whij,Thij,aThij,Usend,N) /* initU(n,Vh,Th,aTh,U) */ |
| 362 |
InitUdef(jpart.n,Whij,Thij,aThij,Vrecv,N) /* ... */ |
| 363 |
if(ipart==0) cout << " *** end init data for send/revc " << endl; |
| 364 |
// |
| 365 |
/******************************************************************/ |
| 366 |
macro BuildTransferMat(ipart,mesh,Pk,N,U2V,Thi,Whi,Whij,Thij,aThij,Usend,Vrecv,jpart,vdebug) |
| 367 |
BuildTransferMat2(ipart,mesh,Pk,N,U2V,Thi,Whi,Whij,Thij,aThij,Usend,Vrecv,jpart,Pii,vdebug) |
| 368 |
// |
| 369 |
/******************************************************************/ |
| 370 |
|
| 371 |
|
| 372 |
/******************************************************************/ |
| 373 |
// usage uplot is allu or [allu,allu1] : |
| 374 |
macro PLOTMPIALLU(mesh,Pk,defPk, Th, u, uplot, plotparm) |
| 375 |
{ int ver=verbosity; |
| 376 |
verbosity=2; |
| 377 |
if(NoGraphicWindow==0) |
| 378 |
{ |
| 379 |
if(mpirank==0) |
| 380 |
{ |
| 381 |
mesh Thi=Th; |
| 382 |
mesh[int] ath(mpisize); |
| 383 |
fespace Xh(Thi,Pk); |
| 384 |
Xh[int] defPk(allu,)(mpisize); |
| 385 |
allu[0][]=u; |
| 386 |
ath[0]=Th; |
| 387 |
mpiRequest[int] rq(mpisize); |
| 388 |
for(int i= 1; i <mpisize;++i) |
| 389 |
Irecv(processor(i,rq[i]),ath[i]); |
| 390 |
for (int i=1;i<mpisize;++i) |
| 391 |
int k= mpiWaitAny(rq); |
| 392 |
for (int i=1;i<mpisize;++i) |
| 393 |
{ Thi=ath[i]; |
| 394 |
Irecv(processor(i,rq[i]),allu[i][]);} |
| 395 |
for (int i=1;i<mpisize;++i) |
| 396 |
int k= mpiWaitAny(rq); |
| 397 |
plot(uplot,plotparm); |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
processor(0)<<Th; |
| 402 |
processor(0)<<u; |
| 403 |
} |
| 404 |
mpiBarrier(mpiCommWorld); |
| 405 |
} |
| 406 |
verbosity=ver; |
| 407 |
return true; |
| 408 |
} |
| 409 |
//EOM ... |
| 410 |
/******************************************************************/ |
| 411 |
|