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