1 2 /*************************************xxt.c************************************ 3 Module Name: xxt 4 Module Info: 5 6 author: Henry M. Tufo III 7 e-mail: hmt@asci.uchicago.edu 8 contact: 9 +--------------------------------+--------------------------------+ 10 |MCS Division - Building 221 |Department of Computer Science | 11 |Argonne National Laboratory |Ryerson 152 | 12 |9700 S. Cass Avenue |The University of Chicago | 13 |Argonne, IL 60439 |Chicago, IL 60637 | 14 |(630) 252-5354/5986 ph/fx |(773) 702-6019/8487 ph/fx | 15 +--------------------------------+--------------------------------+ 16 17 Last Modification: 3.20.01 18 **************************************xxt.c***********************************/ 19 20 21 /*************************************xxt.c************************************ 22 NOTES ON USAGE: 23 24 **************************************xxt.c***********************************/ 25 #include "src/ksp/pc/impls/tfs/tfs.h" 26 27 #define LEFT -1 28 #define RIGHT 1 29 #define BOTH 0 30 #define MAX_FORTRAN_HANDLES 10 31 32 typedef struct xxt_solver_info { 33 int n, m, n_global, m_global; 34 int nnz, max_nnz, msg_buf_sz; 35 int *nsep, *lnsep, *fo, nfo, *stages; 36 int *col_sz, *col_indices; 37 PetscScalar **col_vals, *x, *solve_uu, *solve_w; 38 int nsolves; 39 PetscScalar tot_solve_time; 40 } xxt_info; 41 42 typedef struct matvec_info { 43 int n, m, n_global, m_global; 44 int *local2global; 45 gs_ADT gs_handle; 46 PetscErrorCode (*matvec)(struct matvec_info*,PetscScalar*,PetscScalar*); 47 void *grid_data; 48 } mv_info; 49 50 struct xxt_CDT{ 51 int id; 52 int ns; 53 int level; 54 xxt_info *info; 55 mv_info *mvi; 56 }; 57 58 static int n_xxt=0; 59 static int n_xxt_handles=0; 60 61 /* prototypes */ 62 static void do_xxt_solve(xxt_ADT xxt_handle, PetscScalar *rhs); 63 static void check_init(void); 64 static void check_handle(xxt_ADT xxt_handle); 65 static void det_separators(xxt_ADT xxt_handle); 66 static void do_matvec(mv_info *A, PetscScalar *v, PetscScalar *u); 67 static int xxt_generate(xxt_ADT xxt_handle); 68 static int do_xxt_factor(xxt_ADT xxt_handle); 69 static mv_info *set_mvi(int *local2global, int n, int m, void *matvec, void *grid_data); 70 71 72 73 /*************************************xxt.c************************************ 74 Function: XXT_new() 75 76 Input : 77 Output: 78 Return: 79 Description: 80 **************************************xxt.c***********************************/ 81 xxt_ADT 82 XXT_new(void) 83 { 84 xxt_ADT xxt_handle; 85 86 87 88 /* rolling count on n_xxt ... pot. problem here */ 89 n_xxt_handles++; 90 xxt_handle = (xxt_ADT)malloc(sizeof(struct xxt_CDT)); 91 xxt_handle->id = ++n_xxt; 92 xxt_handle->info = NULL; xxt_handle->mvi = NULL; 93 94 return(xxt_handle); 95 } 96 97 98 /*************************************xxt.c************************************ 99 Function: XXT_factor() 100 101 Input : 102 Output: 103 Return: 104 Description: 105 **************************************xxt.c***********************************/ 106 int 107 XXT_factor(xxt_ADT xxt_handle, /* prev. allocated xxt handle */ 108 int *local2global, /* global column mapping */ 109 int n, /* local num rows */ 110 int m, /* local num cols */ 111 void *matvec, /* b_loc=A_local.x_loc */ 112 void *grid_data /* grid data for matvec */ 113 ) 114 { 115 check_init(); 116 check_handle(xxt_handle); 117 118 /* only 2^k for now and all nodes participating */ 119 if ((1<<(xxt_handle->level=i_log2_num_nodes))!=num_nodes) 120 {error_msg_fatal("only 2^k for now and MPI_COMM_WORLD!!! %d != %d\n",1<<i_log2_num_nodes,num_nodes);} 121 122 /* space for X info */ 123 xxt_handle->info = (xxt_info*)malloc(sizeof(xxt_info)); 124 125 /* set up matvec handles */ 126 xxt_handle->mvi = set_mvi(local2global, n, m, matvec, grid_data); 127 128 /* matrix is assumed to be of full rank */ 129 /* LATER we can reset to indicate rank def. */ 130 xxt_handle->ns=0; 131 132 /* determine separators and generate firing order - NB xxt info set here */ 133 det_separators(xxt_handle); 134 135 return(do_xxt_factor(xxt_handle)); 136 } 137 138 139 /*************************************xxt.c************************************ 140 Function: XXT_solve 141 142 Input : 143 Output: 144 Return: 145 Description: 146 **************************************xxt.c***********************************/ 147 int 148 XXT_solve(xxt_ADT xxt_handle, double *x, double *b) 149 { 150 151 check_init(); 152 check_handle(xxt_handle); 153 154 /* need to copy b into x? */ 155 if (b) 156 {rvec_copy(x,b,xxt_handle->mvi->n);} 157 do_xxt_solve(xxt_handle,x); 158 159 return(0); 160 } 161 162 163 /*************************************xxt.c************************************ 164 Function: XXT_free() 165 166 Input : 167 Output: 168 Return: 169 Description: 170 **************************************xxt.c***********************************/ 171 int 172 XXT_free(xxt_ADT xxt_handle) 173 { 174 175 check_init(); 176 check_handle(xxt_handle); 177 n_xxt_handles--; 178 179 free(xxt_handle->info->nsep); 180 free(xxt_handle->info->lnsep); 181 free(xxt_handle->info->fo); 182 free(xxt_handle->info->stages); 183 free(xxt_handle->info->solve_uu); 184 free(xxt_handle->info->solve_w); 185 free(xxt_handle->info->x); 186 free(xxt_handle->info->col_vals); 187 free(xxt_handle->info->col_sz); 188 free(xxt_handle->info->col_indices); 189 free(xxt_handle->info); 190 free(xxt_handle->mvi->local2global); 191 gs_free(xxt_handle->mvi->gs_handle); 192 free(xxt_handle->mvi); 193 free(xxt_handle); 194 195 196 197 /* if the check fails we nuke */ 198 /* if NULL pointer passed to free we nuke */ 199 /* if the calls to free fail that's not my problem */ 200 return(0); 201 } 202 203 204 205 /*************************************xxt.c************************************ 206 Function: 207 208 Input : 209 Output: 210 Return: 211 Description: 212 **************************************xxt.c***********************************/ 213 int 214 XXT_stats(xxt_ADT xxt_handle) 215 { 216 int op[] = {NON_UNIFORM,GL_MIN,GL_MAX,GL_ADD,GL_MIN,GL_MAX,GL_ADD,GL_MIN,GL_MAX,GL_ADD}; 217 int fop[] = {NON_UNIFORM,GL_MIN,GL_MAX,GL_ADD}; 218 int vals[9], work[9]; 219 PetscScalar fvals[3], fwork[3]; 220 221 222 223 check_init(); 224 check_handle(xxt_handle); 225 226 /* if factorization not done there are no stats */ 227 if (!xxt_handle->info||!xxt_handle->mvi) 228 { 229 if (!my_id) 230 {printf("XXT_stats() :: no stats available!\n");} 231 return 1; 232 } 233 234 vals[0]=vals[1]=vals[2]=xxt_handle->info->nnz; 235 vals[3]=vals[4]=vals[5]=xxt_handle->mvi->n; 236 vals[6]=vals[7]=vals[8]=xxt_handle->info->msg_buf_sz; 237 giop(vals,work,sizeof(op)/sizeof(op[0])-1,op); 238 239 fvals[0]=fvals[1]=fvals[2] 240 =xxt_handle->info->tot_solve_time/xxt_handle->info->nsolves++; 241 grop(fvals,fwork,sizeof(fop)/sizeof(fop[0])-1,fop); 242 243 if (!my_id) 244 { 245 printf("%d :: min xxt_nnz=%d\n",my_id,vals[0]); 246 printf("%d :: max xxt_nnz=%d\n",my_id,vals[1]); 247 printf("%d :: avg xxt_nnz=%g\n",my_id,1.0*vals[2]/num_nodes); 248 printf("%d :: tot xxt_nnz=%d\n",my_id,vals[2]); 249 printf("%d :: xxt C(2d) =%g\n",my_id,vals[2]/(pow(1.0*vals[5],1.5))); 250 printf("%d :: xxt C(3d) =%g\n",my_id,vals[2]/(pow(1.0*vals[5],1.6667))); 251 printf("%d :: min xxt_n =%d\n",my_id,vals[3]); 252 printf("%d :: max xxt_n =%d\n",my_id,vals[4]); 253 printf("%d :: avg xxt_n =%g\n",my_id,1.0*vals[5]/num_nodes); 254 printf("%d :: tot xxt_n =%d\n",my_id,vals[5]); 255 printf("%d :: min xxt_buf=%d\n",my_id,vals[6]); 256 printf("%d :: max xxt_buf=%d\n",my_id,vals[7]); 257 printf("%d :: avg xxt_buf=%g\n",my_id,1.0*vals[8]/num_nodes); 258 printf("%d :: min xxt_slv=%g\n",my_id,fvals[0]); 259 printf("%d :: max xxt_slv=%g\n",my_id,fvals[1]); 260 printf("%d :: avg xxt_slv=%g\n",my_id,fvals[2]/num_nodes); 261 } 262 263 return(0); 264 } 265 266 267 /*************************************xxt.c************************************ 268 Function: do_xxt_factor 269 270 Input : 271 Output: 272 Return: 273 Description: get A_local, local portion of global coarse matrix which 274 is a row dist. nxm matrix w/ n<m. 275 o my_ml holds address of ML struct associated w/A_local and coarse grid 276 o local2global holds global number of column i (i=0,...,m-1) 277 o local2global holds global number of row i (i=0,...,n-1) 278 o mylocmatvec performs A_local . vec_local (note that gs is performed using 279 gs_init/gop). 280 281 mylocmatvec = my_ml->Amat[grid_tag].matvec->external; 282 mylocmatvec (void :: void *data, double *in, double *out) 283 **************************************xxt.c***********************************/ 284 static 285 int 286 do_xxt_factor(xxt_ADT xxt_handle) 287 { 288 int flag; 289 290 291 flag=xxt_generate(xxt_handle); 292 293 return(flag); 294 } 295 296 297 /*************************************xxt.c************************************ 298 Function: 299 300 Input : 301 Output: 302 Return: 303 Description: 304 **************************************xxt.c***********************************/ 305 static 306 int 307 xxt_generate(xxt_ADT xxt_handle) 308 { 309 int i,j,k,idex; 310 int dim, col; 311 PetscScalar *u, *uu, *v, *z, *w, alpha, alpha_w; 312 int *segs; 313 int op[] = {GL_ADD,0}; 314 int off, len; 315 PetscScalar *x_ptr; 316 int *iptr, flag; 317 int start=0, end, work; 318 int op2[] = {GL_MIN,0}; 319 gs_ADT gs_handle; 320 int *nsep, *lnsep, *fo; 321 int a_n=xxt_handle->mvi->n; 322 int a_m=xxt_handle->mvi->m; 323 int *a_local2global=xxt_handle->mvi->local2global; 324 int level; 325 int xxt_nnz=0, xxt_max_nnz=0; 326 int n, m; 327 int *col_sz, *col_indices, *stages; 328 PetscScalar **col_vals, *x; 329 int n_global; 330 int xxt_zero_nnz=0; 331 int xxt_zero_nnz_0=0; 332 PetscBLASInt i1 = 1; 333 PetscScalar dm1 = -1.0; 334 335 n=xxt_handle->mvi->n; 336 nsep=xxt_handle->info->nsep; 337 lnsep=xxt_handle->info->lnsep; 338 fo=xxt_handle->info->fo; 339 end=lnsep[0]; 340 level=xxt_handle->level; 341 gs_handle=xxt_handle->mvi->gs_handle; 342 343 /* is there a null space? */ 344 /* LATER add in ability to detect null space by checking alpha */ 345 for (i=0, j=0; i<=level; i++) 346 {j+=nsep[i];} 347 348 m = j-xxt_handle->ns; 349 if (m!=j) 350 {printf("xxt_generate() :: null space exists %d %d %d\n",m,j,xxt_handle->ns);} 351 352 /* get and initialize storage for x local */ 353 /* note that x local is nxm and stored by columns */ 354 col_sz = (int*) malloc(m*sizeof(PetscInt)); 355 col_indices = (int*) malloc((2*m+1)*sizeof(int)); 356 col_vals = (PetscScalar **) malloc(m*sizeof(PetscScalar *)); 357 for (i=j=0; i<m; i++, j+=2) 358 { 359 col_indices[j]=col_indices[j+1]=col_sz[i]=-1; 360 col_vals[i] = NULL; 361 } 362 col_indices[j]=-1; 363 364 /* size of separators for each sub-hc working from bottom of tree to top */ 365 /* this looks like nsep[]=segments */ 366 stages = (int*) malloc((level+1)*sizeof(PetscInt)); 367 segs = (int*) malloc((level+1)*sizeof(PetscInt)); 368 ivec_zero(stages,level+1); 369 ivec_copy(segs,nsep,level+1); 370 for (i=0; i<level; i++) 371 {segs[i+1] += segs[i];} 372 stages[0] = segs[0]; 373 374 /* temporary vectors */ 375 u = (PetscScalar *) malloc(n*sizeof(PetscScalar)); 376 z = (PetscScalar *) malloc(n*sizeof(PetscScalar)); 377 v = (PetscScalar *) malloc(a_m*sizeof(PetscScalar)); 378 uu = (PetscScalar *) malloc(m*sizeof(PetscScalar)); 379 w = (PetscScalar *) malloc(m*sizeof(PetscScalar)); 380 381 /* extra nnz due to replication of vertices across separators */ 382 for (i=1, j=0; i<=level; i++) 383 {j+=nsep[i];} 384 385 /* storage for sparse x values */ 386 n_global = xxt_handle->info->n_global; 387 xxt_max_nnz = (int)(2.5*pow(1.0*n_global,1.6667) + j*n/2)/num_nodes; 388 x = (PetscScalar *) malloc(xxt_max_nnz*sizeof(PetscScalar)); 389 xxt_nnz = 0; 390 391 /* LATER - can embed next sep to fire in gs */ 392 /* time to make the donuts - generate X factor */ 393 for (dim=i=j=0;i<m;i++) 394 { 395 /* time to move to the next level? */ 396 while (i==segs[dim]) 397 { 398 #ifdef SAFE 399 if (dim==level) 400 {error_msg_fatal("dim about to exceed level\n"); break;} 401 #endif 402 403 stages[dim++]=i; 404 end+=lnsep[dim]; 405 } 406 stages[dim]=i; 407 408 /* which column are we firing? */ 409 /* i.e. set v_l */ 410 /* use new seps and do global min across hc to determine which one to fire */ 411 (start<end) ? (col=fo[start]) : (col=INT_MAX); 412 giop_hc(&col,&work,1,op2,dim); 413 414 /* shouldn't need this */ 415 if (col==INT_MAX) 416 { 417 error_msg_warning("hey ... col==INT_MAX??\n"); 418 continue; 419 } 420 421 /* do I own it? I should */ 422 rvec_zero(v ,a_m); 423 if (col==fo[start]) 424 { 425 start++; 426 idex=ivec_linear_search(col, a_local2global, a_n); 427 if (idex!=-1) 428 {v[idex] = 1.0; j++;} 429 else 430 {error_msg_fatal("NOT FOUND!\n");} 431 } 432 else 433 { 434 idex=ivec_linear_search(col, a_local2global, a_m); 435 if (idex!=-1) 436 {v[idex] = 1.0;} 437 } 438 439 /* perform u = A.v_l */ 440 rvec_zero(u,n); 441 do_matvec(xxt_handle->mvi,v,u); 442 443 /* uu = X^T.u_l (local portion) */ 444 /* technically only need to zero out first i entries */ 445 /* later turn this into an XXT_solve call ? */ 446 rvec_zero(uu,m); 447 x_ptr=x; 448 iptr = col_indices; 449 for (k=0; k<i; k++) 450 { 451 off = *iptr++; 452 len = *iptr++; 453 454 uu[k] = BLASdot_(&len,u+off,&i1,x_ptr,&i1); 455 x_ptr+=len; 456 } 457 458 459 /* uu = X^T.u_l (comm portion) */ 460 ssgl_radd (uu, w, dim, stages); 461 462 /* z = X.uu */ 463 rvec_zero(z,n); 464 x_ptr=x; 465 iptr = col_indices; 466 for (k=0; k<i; k++) 467 { 468 off = *iptr++; 469 len = *iptr++; 470 471 BLASaxpy_(&len,&uu[k],x_ptr,&i1,z+off,&i1); 472 x_ptr+=len; 473 } 474 475 /* compute v_l = v_l - z */ 476 rvec_zero(v+a_n,a_m-a_n); 477 BLASaxpy_(&n,&dm1,z,&i1,v,&i1); 478 479 /* compute u_l = A.v_l */ 480 if (a_n!=a_m) 481 {gs_gop_hc(gs_handle,v,"+\0",dim);} 482 rvec_zero(u,n); 483 do_matvec(xxt_handle->mvi,v,u); 484 485 /* compute sqrt(alpha) = sqrt(v_l^T.u_l) - local portion */ 486 alpha = BLASdot_(&n,u,&i1,v,&i1); 487 /* compute sqrt(alpha) = sqrt(v_l^T.u_l) - comm portion */ 488 grop_hc(&alpha, &alpha_w, 1, op, dim); 489 490 alpha = (PetscScalar) sqrt((double)alpha); 491 492 /* check for small alpha */ 493 /* LATER use this to detect and determine null space */ 494 if (fabs(alpha)<1.0e-14) 495 {error_msg_fatal("bad alpha! %g\n",alpha);} 496 497 /* compute v_l = v_l/sqrt(alpha) */ 498 rvec_scale(v,1.0/alpha,n); 499 500 /* add newly generated column, v_l, to X */ 501 flag = 1; 502 off=len=0; 503 for (k=0; k<n; k++) 504 { 505 if (v[k]!=0.0) 506 { 507 len=k; 508 if (flag) 509 {off=k; flag=0;} 510 } 511 } 512 513 len -= (off-1); 514 515 if (len>0) 516 { 517 if ((xxt_nnz+len)>xxt_max_nnz) 518 { 519 error_msg_warning("increasing space for X by 2x!\n"); 520 xxt_max_nnz *= 2; 521 x_ptr = (PetscScalar *) malloc(xxt_max_nnz*sizeof(PetscScalar)); 522 rvec_copy(x_ptr,x,xxt_nnz); 523 free(x); 524 x = x_ptr; 525 x_ptr+=xxt_nnz; 526 } 527 xxt_nnz += len; 528 rvec_copy(x_ptr,v+off,len); 529 530 /* keep track of number of zeros */ 531 if (dim) 532 { 533 for (k=0; k<len; k++) 534 { 535 if (x_ptr[k]==0.0) 536 {xxt_zero_nnz++;} 537 } 538 } 539 else 540 { 541 for (k=0; k<len; k++) 542 { 543 if (x_ptr[k]==0.0) 544 {xxt_zero_nnz_0++;} 545 } 546 } 547 col_indices[2*i] = off; 548 col_sz[i] = col_indices[2*i+1] = len; 549 col_vals[i] = x_ptr; 550 } 551 else 552 { 553 col_indices[2*i] = 0; 554 col_sz[i] = col_indices[2*i+1] = 0; 555 col_vals[i] = x_ptr; 556 } 557 } 558 559 /* close off stages for execution phase */ 560 while (dim!=level) 561 { 562 stages[dim++]=i; 563 error_msg_warning("disconnected!!! dim(%d)!=level(%d)\n",dim,level); 564 } 565 stages[dim]=i; 566 567 xxt_handle->info->n=xxt_handle->mvi->n; 568 xxt_handle->info->m=m; 569 xxt_handle->info->nnz=xxt_nnz; 570 xxt_handle->info->max_nnz=xxt_max_nnz; 571 xxt_handle->info->msg_buf_sz=stages[level]-stages[0]; 572 xxt_handle->info->solve_uu = (PetscScalar *) malloc(m*sizeof(PetscScalar)); 573 xxt_handle->info->solve_w = (PetscScalar *) malloc(m*sizeof(PetscScalar)); 574 xxt_handle->info->x=x; 575 xxt_handle->info->col_vals=col_vals; 576 xxt_handle->info->col_sz=col_sz; 577 xxt_handle->info->col_indices=col_indices; 578 xxt_handle->info->stages=stages; 579 xxt_handle->info->nsolves=0; 580 xxt_handle->info->tot_solve_time=0.0; 581 582 free(segs); 583 free(u); 584 free(v); 585 free(uu); 586 free(z); 587 free(w); 588 589 return(0); 590 } 591 592 593 /*************************************xxt.c************************************ 594 Function: 595 596 Input : 597 Output: 598 Return: 599 Description: 600 **************************************xxt.c***********************************/ 601 static 602 void 603 do_xxt_solve(xxt_ADT xxt_handle, PetscScalar *uc) 604 { 605 int off, len, *iptr; 606 int level =xxt_handle->level; 607 int n =xxt_handle->info->n; 608 int m =xxt_handle->info->m; 609 int *stages =xxt_handle->info->stages; 610 int *col_indices=xxt_handle->info->col_indices; 611 PetscScalar *x_ptr, *uu_ptr; 612 PetscScalar *solve_uu=xxt_handle->info->solve_uu; 613 PetscScalar *solve_w =xxt_handle->info->solve_w; 614 PetscScalar *x =xxt_handle->info->x; 615 PetscBLASInt i1 = 1; 616 617 uu_ptr=solve_uu; 618 rvec_zero(uu_ptr,m); 619 620 /* x = X.Y^T.b */ 621 /* uu = Y^T.b */ 622 for (x_ptr=x,iptr=col_indices; *iptr!=-1; x_ptr+=len) 623 { 624 off=*iptr++; len=*iptr++; 625 *uu_ptr++ = BLASdot_(&len,uc+off,&i1,x_ptr,&i1); 626 } 627 628 /* comunication of beta */ 629 uu_ptr=solve_uu; 630 if (level) {ssgl_radd(uu_ptr, solve_w, level, stages);} 631 632 rvec_zero(uc,n); 633 634 /* x = X.uu */ 635 for (x_ptr=x,iptr=col_indices; *iptr!=-1; x_ptr+=len) 636 { 637 off=*iptr++; len=*iptr++; 638 BLASaxpy_(&len,uu_ptr++,x_ptr,&i1,uc+off,&i1); 639 } 640 641 } 642 643 644 /*************************************Xxt.c************************************ 645 Function: check_init 646 647 Input : 648 Output: 649 Return: 650 Description: 651 **************************************xxt.c***********************************/ 652 static 653 void 654 check_init(void) 655 { 656 comm_init(); 657 658 } 659 660 661 /*************************************xxt.c************************************ 662 Function: check_handle() 663 664 Input : 665 Output: 666 Return: 667 Description: 668 **************************************xxt.c***********************************/ 669 static 670 void 671 check_handle(xxt_ADT xxt_handle) 672 { 673 #ifdef SAFE 674 int vals[2], work[2], op[] = {NON_UNIFORM,GL_MIN,GL_MAX}; 675 #endif 676 677 678 if (xxt_handle==NULL) 679 {error_msg_fatal("check_handle() :: bad handle :: NULL %d\n",xxt_handle);} 680 681 #ifdef SAFE 682 vals[0]=vals[1]=xxt_handle->id; 683 giop(vals,work,sizeof(op)/sizeof(op[0])-1,op); 684 if ((vals[0]!=vals[1])||(xxt_handle->id<=0)) 685 {error_msg_fatal("check_handle() :: bad handle :: id mismatch min/max %d/%d %d\n", 686 vals[0],vals[1], xxt_handle->id);} 687 #endif 688 689 } 690 691 692 /*************************************xxt.c************************************ 693 Function: det_separators 694 695 Input : 696 Output: 697 Return: 698 Description: 699 det_separators(xxt_handle, local2global, n, m, mylocmatvec, grid_data); 700 **************************************xxt.c***********************************/ 701 static 702 void 703 det_separators(xxt_ADT xxt_handle) 704 { 705 int i, ct, id; 706 int mask, edge, *iptr; 707 int *dir, *used; 708 int sum[4], w[4]; 709 PetscScalar rsum[4], rw[4]; 710 int op[] = {GL_ADD,0}; 711 PetscScalar *lhs, *rhs; 712 int *nsep, *lnsep, *fo, nfo=0; 713 gs_ADT gs_handle=xxt_handle->mvi->gs_handle; 714 int *local2global=xxt_handle->mvi->local2global; 715 int n=xxt_handle->mvi->n; 716 int m=xxt_handle->mvi->m; 717 int level=xxt_handle->level; 718 int shared=FALSE; 719 720 dir = (int*)malloc(sizeof(PetscInt)*(level+1)); 721 nsep = (int*)malloc(sizeof(PetscInt)*(level+1)); 722 lnsep= (int*)malloc(sizeof(PetscInt)*(level+1)); 723 fo = (int*)malloc(sizeof(PetscInt)*(n+1)); 724 used = (int*)malloc(sizeof(PetscInt)*n); 725 726 ivec_zero(dir ,level+1); 727 ivec_zero(nsep ,level+1); 728 ivec_zero(lnsep,level+1); 729 ivec_set (fo ,-1,n+1); 730 ivec_zero(used,n); 731 732 lhs = (double*)malloc(sizeof(PetscScalar)*m); 733 rhs = (double*)malloc(sizeof(PetscScalar)*m); 734 735 /* determine the # of unique dof */ 736 rvec_zero(lhs,m); 737 rvec_set(lhs,1.0,n); 738 gs_gop_hc(gs_handle,lhs,"+\0",level); 739 rvec_zero(rsum,2); 740 for (ct=i=0;i<n;i++) 741 { 742 if (lhs[i]!=0.0) 743 {rsum[0]+=1.0/lhs[i]; rsum[1]+=lhs[i];} 744 } 745 grop_hc(rsum,rw,2,op,level); 746 rsum[0]+=0.1; 747 rsum[1]+=0.1; 748 /* if (!my_id) 749 { 750 printf("xxt n unique = %d (%g)\n",(int) rsum[0], rsum[0]); 751 printf("xxt n shared = %d (%g)\n",(int) rsum[1], rsum[1]); 752 }*/ 753 754 if (fabs(rsum[0]-rsum[1])>EPS) 755 {shared=TRUE;} 756 757 xxt_handle->info->n_global=xxt_handle->info->m_global=(int) rsum[0]; 758 xxt_handle->mvi->n_global =xxt_handle->mvi->m_global =(int) rsum[0]; 759 760 /* determine separator sets top down */ 761 if (shared) 762 { 763 for (iptr=fo+n,id=my_id,mask=num_nodes>>1,edge=level;edge>0;edge--,mask>>=1) 764 { 765 /* set rsh of hc, fire, and collect lhs responses */ 766 (id<mask) ? rvec_zero(lhs,m) : rvec_set(lhs,1.0,m); 767 gs_gop_hc(gs_handle,lhs,"+\0",edge); 768 769 /* set lsh of hc, fire, and collect rhs responses */ 770 (id<mask) ? rvec_set(rhs,1.0,m) : rvec_zero(rhs,m); 771 gs_gop_hc(gs_handle,rhs,"+\0",edge); 772 773 for (i=0;i<n;i++) 774 { 775 if (id< mask) 776 { 777 if (lhs[i]!=0.0) 778 {lhs[i]=1.0;} 779 } 780 if (id>=mask) 781 { 782 if (rhs[i]!=0.0) 783 {rhs[i]=1.0;} 784 } 785 } 786 787 if (id< mask) 788 {gs_gop_hc(gs_handle,lhs,"+\0",edge-1);} 789 else 790 {gs_gop_hc(gs_handle,rhs,"+\0",edge-1);} 791 792 /* count number of dofs I own that have signal and not in sep set */ 793 rvec_zero(rsum,4); 794 for (ivec_zero(sum,4),ct=i=0;i<n;i++) 795 { 796 if (!used[i]) 797 { 798 /* number of unmarked dofs on node */ 799 ct++; 800 /* number of dofs to be marked on lhs hc */ 801 if (id< mask) 802 { 803 if (lhs[i]!=0.0) 804 {sum[0]++; rsum[0]+=1.0/lhs[i];} 805 } 806 /* number of dofs to be marked on rhs hc */ 807 if (id>=mask) 808 { 809 if (rhs[i]!=0.0) 810 {sum[1]++; rsum[1]+=1.0/rhs[i];} 811 } 812 } 813 } 814 815 /* go for load balance - choose half with most unmarked dofs, bias LHS */ 816 (id<mask) ? (sum[2]=ct) : (sum[3]=ct); 817 (id<mask) ? (rsum[2]=ct) : (rsum[3]=ct); 818 giop_hc(sum,w,4,op,edge); 819 grop_hc(rsum,rw,4,op,edge); 820 rsum[0]+=0.1; rsum[1]+=0.1; rsum[2]+=0.1; rsum[3]+=0.1; 821 822 if (id<mask) 823 { 824 /* mark dofs I own that have signal and not in sep set */ 825 for (ct=i=0;i<n;i++) 826 { 827 if ((!used[i])&&(lhs[i]!=0.0)) 828 { 829 ct++; nfo++; 830 831 if (nfo>n) 832 {error_msg_fatal("nfo about to exceed n\n");} 833 834 *--iptr = local2global[i]; 835 used[i]=edge; 836 } 837 } 838 if (ct>1) {ivec_sort(iptr,ct);} 839 840 lnsep[edge]=ct; 841 nsep[edge]=(int) rsum[0]; 842 dir [edge]=LEFT; 843 } 844 845 if (id>=mask) 846 { 847 /* mark dofs I own that have signal and not in sep set */ 848 for (ct=i=0;i<n;i++) 849 { 850 if ((!used[i])&&(rhs[i]!=0.0)) 851 { 852 ct++; nfo++; 853 854 if (nfo>n) 855 {error_msg_fatal("nfo about to exceed n\n");} 856 857 *--iptr = local2global[i]; 858 used[i]=edge; 859 } 860 } 861 if (ct>1) {ivec_sort(iptr,ct);} 862 863 lnsep[edge]=ct; 864 nsep[edge]= (int) rsum[1]; 865 dir [edge]=RIGHT; 866 } 867 868 /* LATER or we can recur on these to order seps at this level */ 869 /* do we need full set of separators for this? */ 870 871 /* fold rhs hc into lower */ 872 if (id>=mask) 873 {id-=mask;} 874 } 875 } 876 else 877 { 878 for (iptr=fo+n,id=my_id,mask=num_nodes>>1,edge=level;edge>0;edge--,mask>>=1) 879 { 880 /* set rsh of hc, fire, and collect lhs responses */ 881 (id<mask) ? rvec_zero(lhs,m) : rvec_set(lhs,1.0,m); 882 gs_gop_hc(gs_handle,lhs,"+\0",edge); 883 884 /* set lsh of hc, fire, and collect rhs responses */ 885 (id<mask) ? rvec_set(rhs,1.0,m) : rvec_zero(rhs,m); 886 gs_gop_hc(gs_handle,rhs,"+\0",edge); 887 888 /* count number of dofs I own that have signal and not in sep set */ 889 for (ivec_zero(sum,4),ct=i=0;i<n;i++) 890 { 891 if (!used[i]) 892 { 893 /* number of unmarked dofs on node */ 894 ct++; 895 /* number of dofs to be marked on lhs hc */ 896 if ((id< mask)&&(lhs[i]!=0.0)) {sum[0]++;} 897 /* number of dofs to be marked on rhs hc */ 898 if ((id>=mask)&&(rhs[i]!=0.0)) {sum[1]++;} 899 } 900 } 901 902 /* go for load balance - choose half with most unmarked dofs, bias LHS */ 903 (id<mask) ? (sum[2]=ct) : (sum[3]=ct); 904 giop_hc(sum,w,4,op,edge); 905 906 /* lhs hc wins */ 907 if (sum[2]>=sum[3]) 908 { 909 if (id<mask) 910 { 911 /* mark dofs I own that have signal and not in sep set */ 912 for (ct=i=0;i<n;i++) 913 { 914 if ((!used[i])&&(lhs[i]!=0.0)) 915 { 916 ct++; nfo++; 917 *--iptr = local2global[i]; 918 used[i]=edge; 919 } 920 } 921 if (ct>1) {ivec_sort(iptr,ct);} 922 lnsep[edge]=ct; 923 } 924 nsep[edge]=sum[0]; 925 dir [edge]=LEFT; 926 } 927 /* rhs hc wins */ 928 else 929 { 930 if (id>=mask) 931 { 932 /* mark dofs I own that have signal and not in sep set */ 933 for (ct=i=0;i<n;i++) 934 { 935 if ((!used[i])&&(rhs[i]!=0.0)) 936 { 937 ct++; nfo++; 938 *--iptr = local2global[i]; 939 used[i]=edge; 940 } 941 } 942 if (ct>1) {ivec_sort(iptr,ct);} 943 lnsep[edge]=ct; 944 } 945 nsep[edge]=sum[1]; 946 dir [edge]=RIGHT; 947 } 948 /* LATER or we can recur on these to order seps at this level */ 949 /* do we need full set of separators for this? */ 950 951 /* fold rhs hc into lower */ 952 if (id>=mask) 953 {id-=mask;} 954 } 955 } 956 957 958 /* level 0 is on processor case - so mark the remainder */ 959 for (ct=i=0;i<n;i++) 960 { 961 if (!used[i]) 962 { 963 ct++; nfo++; 964 *--iptr = local2global[i]; 965 used[i]=edge; 966 } 967 } 968 if (ct>1) {ivec_sort(iptr,ct);} 969 lnsep[edge]=ct; 970 nsep [edge]=ct; 971 dir [edge]=LEFT; 972 973 xxt_handle->info->nsep=nsep; 974 xxt_handle->info->lnsep=lnsep; 975 xxt_handle->info->fo=fo; 976 xxt_handle->info->nfo=nfo; 977 978 free(dir); 979 free(lhs); 980 free(rhs); 981 free(used); 982 983 } 984 985 986 /*************************************xxt.c************************************ 987 Function: set_mvi 988 989 Input : 990 Output: 991 Return: 992 Description: 993 **************************************xxt.c***********************************/ 994 static 995 mv_info *set_mvi(int *local2global, int n, int m, void *matvec, void *grid_data) 996 { 997 mv_info *mvi; 998 999 1000 mvi = (mv_info*)malloc(sizeof(mv_info)); 1001 mvi->n=n; 1002 mvi->m=m; 1003 mvi->n_global=-1; 1004 mvi->m_global=-1; 1005 mvi->local2global=(int*)malloc((m+1)*sizeof(PetscInt)); 1006 ivec_copy(mvi->local2global,local2global,m); 1007 mvi->local2global[m] = INT_MAX; 1008 mvi->matvec=(PetscErrorCode (*)(mv_info*,PetscScalar*,PetscScalar*))matvec; 1009 mvi->grid_data=grid_data; 1010 1011 /* set xxt communication handle to perform restricted matvec */ 1012 mvi->gs_handle = gs_init(local2global, m, num_nodes); 1013 1014 return(mvi); 1015 } 1016 1017 1018 /*************************************xxt.c************************************ 1019 Function: set_mvi 1020 1021 Input : 1022 Output: 1023 Return: 1024 Description: 1025 1026 computes u = A.v 1027 do_matvec(xxt_handle->mvi,v,u); 1028 **************************************xxt.c***********************************/ 1029 static 1030 void do_matvec(mv_info *A, PetscScalar *v, PetscScalar *u) 1031 { 1032 A->matvec((mv_info*)A->grid_data,v,u); 1033 } 1034 1035 1036 1037