xref: /petsc/src/mat/impls/aij/seq/inode.c (revision 3f6d85c9ddf3f47f60d3dbdd13fdb272a697af0e)
14c1414c8SBarry Smith #define PETSCMAT_DLL
24c1414c8SBarry Smith 
34c1414c8SBarry Smith /*
44c1414c8SBarry Smith   This file provides high performance routines for the Inode format (compressed sparse row)
54c1414c8SBarry Smith   by taking advantage of rows with identical nonzero structure (I-nodes).
64c1414c8SBarry Smith */
77c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"
84c1414c8SBarry Smith 
94c1414c8SBarry Smith #undef __FUNCT__
104c1414c8SBarry Smith #define __FUNCT__ "Mat_CreateColInode"
114c1414c8SBarry Smith static PetscErrorCode Mat_CreateColInode(Mat A,PetscInt* size,PetscInt ** ns)
124c1414c8SBarry Smith {
134c1414c8SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
144c1414c8SBarry Smith   PetscErrorCode ierr;
154c1414c8SBarry Smith   PetscInt       i,count,m,n,min_mn,*ns_row,*ns_col;
164c1414c8SBarry Smith 
174c1414c8SBarry Smith   PetscFunctionBegin;
18d0f46423SBarry Smith   n      = A->cmap->n;
19d0f46423SBarry Smith   m      = A->rmap->n;
204c1414c8SBarry Smith   ns_row = a->inode.size;
214c1414c8SBarry Smith 
224c1414c8SBarry Smith   min_mn = (m < n) ? m : n;
234c1414c8SBarry Smith   if (!ns) {
244c1414c8SBarry Smith     for (count=0,i=0; count<min_mn; count+=ns_row[i],i++);
254c1414c8SBarry Smith     for(; count+1 < n; count++,i++);
264c1414c8SBarry Smith     if (count < n)  {
274c1414c8SBarry Smith       i++;
284c1414c8SBarry Smith     }
294c1414c8SBarry Smith     *size = i;
304c1414c8SBarry Smith     PetscFunctionReturn(0);
314c1414c8SBarry Smith   }
324c1414c8SBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&ns_col);CHKERRQ(ierr);
334c1414c8SBarry Smith 
344c1414c8SBarry Smith   /* Use the same row structure wherever feasible. */
354c1414c8SBarry Smith   for (count=0,i=0; count<min_mn; count+=ns_row[i],i++) {
364c1414c8SBarry Smith     ns_col[i] = ns_row[i];
374c1414c8SBarry Smith   }
384c1414c8SBarry Smith 
394c1414c8SBarry Smith   /* if m < n; pad up the remainder with inode_limit */
404c1414c8SBarry Smith   for(; count+1 < n; count++,i++) {
414c1414c8SBarry Smith     ns_col[i] = 1;
424c1414c8SBarry Smith   }
434c1414c8SBarry Smith   /* The last node is the odd ball. padd it up with the remaining rows; */
444c1414c8SBarry Smith   if (count < n)  {
454c1414c8SBarry Smith     ns_col[i] = n - count;
464c1414c8SBarry Smith     i++;
474c1414c8SBarry Smith   } else if (count > n) {
484c1414c8SBarry Smith     /* Adjust for the over estimation */
494c1414c8SBarry Smith     ns_col[i-1] += n - count;
504c1414c8SBarry Smith   }
514c1414c8SBarry Smith   *size = i;
524c1414c8SBarry Smith   *ns   = ns_col;
534c1414c8SBarry Smith   PetscFunctionReturn(0);
544c1414c8SBarry Smith }
554c1414c8SBarry Smith 
564c1414c8SBarry Smith 
574c1414c8SBarry Smith /*
584c1414c8SBarry Smith       This builds symmetric version of nonzero structure,
594c1414c8SBarry Smith */
604c1414c8SBarry Smith #undef __FUNCT__
614c1414c8SBarry Smith #define __FUNCT__ "MatGetRowIJ_Inode_Symmetric"
624c1414c8SBarry Smith static PetscErrorCode MatGetRowIJ_Inode_Symmetric(Mat A,PetscInt *iia[],PetscInt *jja[],PetscInt ishift,PetscInt oshift)
634c1414c8SBarry Smith {
644c1414c8SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
654c1414c8SBarry Smith   PetscErrorCode ierr;
664c1414c8SBarry Smith   PetscInt       *work,*ia,*ja,*j,nz,nslim_row,nslim_col,m,row,col,*jmax,n;
674c1414c8SBarry Smith   PetscInt       *tns,*tvc,*ns_row = a->inode.size,*ns_col,nsz,i1,i2,*ai= a->i,*aj = a->j;
684c1414c8SBarry Smith 
694c1414c8SBarry Smith   PetscFunctionBegin;
704c1414c8SBarry Smith   nslim_row = a->inode.node_count;
71d0f46423SBarry Smith   m         = A->rmap->n;
72d0f46423SBarry Smith   n         = A->cmap->n;
734c1414c8SBarry Smith   if (m != n) SETERRQ(PETSC_ERR_SUP,"MatGetRowIJ_Inode_Symmetric: Matrix should be square");
744c1414c8SBarry Smith 
754c1414c8SBarry Smith   /* Use the row_inode as column_inode */
764c1414c8SBarry Smith   nslim_col = nslim_row;
774c1414c8SBarry Smith   ns_col    = ns_row;
784c1414c8SBarry Smith 
794c1414c8SBarry Smith   /* allocate space for reformated inode structure */
804c1414c8SBarry Smith   ierr = PetscMalloc((nslim_col+1)*sizeof(PetscInt),&tns);CHKERRQ(ierr);
814c1414c8SBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&tvc);CHKERRQ(ierr);
824c1414c8SBarry Smith   for (i1=0,tns[0]=0; i1<nslim_col; ++i1) tns[i1+1] = tns[i1]+ ns_row[i1];
834c1414c8SBarry Smith 
844c1414c8SBarry Smith   for (i1=0,col=0; i1<nslim_col; ++i1){
854c1414c8SBarry Smith     nsz = ns_col[i1];
864c1414c8SBarry Smith     for (i2=0; i2<nsz; ++i2,++col)
874c1414c8SBarry Smith       tvc[col] = i1;
884c1414c8SBarry Smith   }
894c1414c8SBarry Smith   /* allocate space for row pointers */
904c1414c8SBarry Smith   ierr = PetscMalloc((nslim_row+1)*sizeof(PetscInt),&ia);CHKERRQ(ierr);
914c1414c8SBarry Smith   *iia = ia;
924c1414c8SBarry Smith   ierr = PetscMemzero(ia,(nslim_row+1)*sizeof(PetscInt));CHKERRQ(ierr);
934c1414c8SBarry Smith   ierr = PetscMalloc((nslim_row+1)*sizeof(PetscInt),&work);CHKERRQ(ierr);
944c1414c8SBarry Smith 
954c1414c8SBarry Smith   /* determine the number of columns in each row */
964c1414c8SBarry Smith   ia[0] = oshift;
974c1414c8SBarry Smith   for (i1=0,row=0 ; i1<nslim_row; row+=ns_row[i1],i1++) {
984c1414c8SBarry Smith 
994c1414c8SBarry Smith     j    = aj + ai[row] + ishift;
1004c1414c8SBarry Smith     jmax = aj + ai[row+1] + ishift;
1014c1414c8SBarry Smith     i2   = 0;
1024c1414c8SBarry Smith     col  = *j++ + ishift;
1034c1414c8SBarry Smith     i2   = tvc[col];
1044c1414c8SBarry Smith     while (i2<i1 && j<jmax) { /* 1.[-xx-d-xx--] 2.[-xx-------],off-diagonal elemets */
1054c1414c8SBarry Smith       ia[i1+1]++;
1064c1414c8SBarry Smith       ia[i2+1]++;
1074c1414c8SBarry Smith       i2++;                     /* Start col of next node */
1084c1414c8SBarry Smith       while(((col=*j+ishift)<tns[i2]) && (j<jmax)) ++j;
1094c1414c8SBarry Smith       i2 = tvc[col];
1104c1414c8SBarry Smith     }
1114c1414c8SBarry Smith     if(i2 == i1) ia[i2+1]++;    /* now the diagonal element */
1124c1414c8SBarry Smith   }
1134c1414c8SBarry Smith 
1144c1414c8SBarry Smith   /* shift ia[i] to point to next row */
1154c1414c8SBarry Smith   for (i1=1; i1<nslim_row+1; i1++) {
1164c1414c8SBarry Smith     row        = ia[i1-1];
1174c1414c8SBarry Smith     ia[i1]    += row;
1184c1414c8SBarry Smith     work[i1-1] = row - oshift;
1194c1414c8SBarry Smith   }
1204c1414c8SBarry Smith 
1214c1414c8SBarry Smith   /* allocate space for column pointers */
1224c1414c8SBarry Smith   nz   = ia[nslim_row] + (!ishift);
1234c1414c8SBarry Smith   ierr = PetscMalloc(nz*sizeof(PetscInt),&ja);CHKERRQ(ierr);
1244c1414c8SBarry Smith   *jja = ja;
1254c1414c8SBarry Smith 
1264c1414c8SBarry Smith  /* loop over lower triangular part putting into ja */
1274c1414c8SBarry Smith   for (i1=0,row=0; i1<nslim_row; row += ns_row[i1],i1++) {
1284c1414c8SBarry Smith     j    = aj + ai[row] + ishift;
1294c1414c8SBarry Smith     jmax = aj + ai[row+1] + ishift;
1304c1414c8SBarry Smith     i2   = 0;                     /* Col inode index */
1314c1414c8SBarry Smith     col  = *j++ + ishift;
1324c1414c8SBarry Smith     i2   = tvc[col];
1334c1414c8SBarry Smith     while (i2<i1 && j<jmax) {
1344c1414c8SBarry Smith       ja[work[i2]++] = i1 + oshift;
1354c1414c8SBarry Smith       ja[work[i1]++] = i2 + oshift;
1364c1414c8SBarry Smith       ++i2;
1374c1414c8SBarry Smith       while(((col=*j+ishift)< tns[i2])&&(j<jmax)) ++j; /* Skip rest col indices in this node */
1384c1414c8SBarry Smith       i2 = tvc[col];
1394c1414c8SBarry Smith     }
1404c1414c8SBarry Smith     if (i2 == i1) ja[work[i1]++] = i2 + oshift;
1414c1414c8SBarry Smith 
1424c1414c8SBarry Smith   }
1434c1414c8SBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
1444c1414c8SBarry Smith   ierr = PetscFree(tns);CHKERRQ(ierr);
1454c1414c8SBarry Smith   ierr = PetscFree(tvc);CHKERRQ(ierr);
1464c1414c8SBarry Smith   PetscFunctionReturn(0);
1474c1414c8SBarry Smith }
1484c1414c8SBarry Smith 
1494c1414c8SBarry Smith /*
1504c1414c8SBarry Smith       This builds nonsymmetric version of nonzero structure,
1514c1414c8SBarry Smith */
1524c1414c8SBarry Smith #undef __FUNCT__
1534c1414c8SBarry Smith #define __FUNCT__ "MatGetRowIJ_Inode_Nonsymmetric"
1544c1414c8SBarry Smith static PetscErrorCode MatGetRowIJ_Inode_Nonsymmetric(Mat A,PetscInt *iia[],PetscInt *jja[],PetscInt ishift,PetscInt oshift)
1554c1414c8SBarry Smith {
1564c1414c8SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
1574c1414c8SBarry Smith   PetscErrorCode ierr;
1584c1414c8SBarry Smith   PetscInt       *work,*ia,*ja,*j,nz,nslim_row,n,row,col,*ns_col,nslim_col;
1594c1414c8SBarry Smith   PetscInt       *tns,*tvc,*ns_row = a->inode.size,nsz,i1,i2,*ai= a->i,*aj = a->j;
1604c1414c8SBarry Smith 
1614c1414c8SBarry Smith   PetscFunctionBegin;
1624c1414c8SBarry Smith   nslim_row = a->inode.node_count;
163d0f46423SBarry Smith   n         = A->cmap->n;
1644c1414c8SBarry Smith 
1654c1414c8SBarry Smith   /* Create The column_inode for this matrix */
1664c1414c8SBarry Smith   ierr = Mat_CreateColInode(A,&nslim_col,&ns_col);CHKERRQ(ierr);
1674c1414c8SBarry Smith 
1684c1414c8SBarry Smith   /* allocate space for reformated column_inode structure */
1694c1414c8SBarry Smith   ierr = PetscMalloc((nslim_col +1)*sizeof(PetscInt),&tns);CHKERRQ(ierr);
1704c1414c8SBarry Smith   ierr = PetscMalloc((n +1)*sizeof(PetscInt),&tvc);CHKERRQ(ierr);
1714c1414c8SBarry Smith   for (i1=0,tns[0]=0; i1<nslim_col; ++i1) tns[i1+1] = tns[i1] + ns_col[i1];
1724c1414c8SBarry Smith 
1734c1414c8SBarry Smith   for (i1=0,col=0; i1<nslim_col; ++i1){
1744c1414c8SBarry Smith     nsz = ns_col[i1];
1754c1414c8SBarry Smith     for (i2=0; i2<nsz; ++i2,++col)
1764c1414c8SBarry Smith       tvc[col] = i1;
1774c1414c8SBarry Smith   }
1784c1414c8SBarry Smith   /* allocate space for row pointers */
1794c1414c8SBarry Smith   ierr = PetscMalloc((nslim_row+1)*sizeof(PetscInt),&ia);CHKERRQ(ierr);
1804c1414c8SBarry Smith   *iia = ia;
1814c1414c8SBarry Smith   ierr = PetscMemzero(ia,(nslim_row+1)*sizeof(PetscInt));CHKERRQ(ierr);
1824c1414c8SBarry Smith   ierr = PetscMalloc((nslim_row+1)*sizeof(PetscInt),&work);CHKERRQ(ierr);
1834c1414c8SBarry Smith 
1844c1414c8SBarry Smith   /* determine the number of columns in each row */
1854c1414c8SBarry Smith   ia[0] = oshift;
1864c1414c8SBarry Smith   for (i1=0,row=0; i1<nslim_row; row+=ns_row[i1],i1++) {
1874c1414c8SBarry Smith     j   = aj + ai[row] + ishift;
1884c1414c8SBarry Smith     col = *j++ + ishift;
1894c1414c8SBarry Smith     i2  = tvc[col];
1904c1414c8SBarry Smith     nz  = ai[row+1] - ai[row];
1914c1414c8SBarry Smith     while (nz-- > 0) {           /* off-diagonal elemets */
1924c1414c8SBarry Smith       ia[i1+1]++;
1934c1414c8SBarry Smith       i2++;                     /* Start col of next node */
1944c1414c8SBarry Smith       while (((col = *j++ + ishift) < tns[i2]) && nz > 0) {nz--;}
1954c1414c8SBarry Smith       if (nz > 0) i2 = tvc[col];
1964c1414c8SBarry Smith     }
1974c1414c8SBarry Smith   }
1984c1414c8SBarry Smith 
1994c1414c8SBarry Smith   /* shift ia[i] to point to next row */
2004c1414c8SBarry Smith   for (i1=1; i1<nslim_row+1; i1++) {
2014c1414c8SBarry Smith     row        = ia[i1-1];
2024c1414c8SBarry Smith     ia[i1]    += row;
2034c1414c8SBarry Smith     work[i1-1] = row - oshift;
2044c1414c8SBarry Smith   }
2054c1414c8SBarry Smith 
2064c1414c8SBarry Smith   /* allocate space for column pointers */
2074c1414c8SBarry Smith   nz   = ia[nslim_row] + (!ishift);
2084c1414c8SBarry Smith   ierr = PetscMalloc(nz*sizeof(PetscInt),&ja);CHKERRQ(ierr);
2094c1414c8SBarry Smith   *jja = ja;
2104c1414c8SBarry Smith 
2114c1414c8SBarry Smith  /* loop over matrix putting into ja */
2124c1414c8SBarry Smith   for (i1=0,row=0; i1<nslim_row; row+=ns_row[i1],i1++) {
2134c1414c8SBarry Smith     j   = aj + ai[row] + ishift;
2144c1414c8SBarry Smith     i2  = 0;                     /* Col inode index */
2154c1414c8SBarry Smith     col = *j++ + ishift;
2164c1414c8SBarry Smith     i2  = tvc[col];
2174c1414c8SBarry Smith     nz  = ai[row+1] - ai[row];
2184c1414c8SBarry Smith     while (nz-- > 0) {
2194c1414c8SBarry Smith       ja[work[i1]++] = i2 + oshift;
2204c1414c8SBarry Smith       ++i2;
2214c1414c8SBarry Smith       while(((col = *j++ + ishift) < tns[i2]) && nz > 0) {nz--;}
2224c1414c8SBarry Smith       if (nz > 0) i2 = tvc[col];
2234c1414c8SBarry Smith     }
2244c1414c8SBarry Smith   }
2254c1414c8SBarry Smith   ierr = PetscFree(ns_col);CHKERRQ(ierr);
2264c1414c8SBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
2274c1414c8SBarry Smith   ierr = PetscFree(tns);CHKERRQ(ierr);
2284c1414c8SBarry Smith   ierr = PetscFree(tvc);CHKERRQ(ierr);
2294c1414c8SBarry Smith   PetscFunctionReturn(0);
2304c1414c8SBarry Smith }
2314c1414c8SBarry Smith 
2324c1414c8SBarry Smith #undef __FUNCT__
2334c1414c8SBarry Smith #define __FUNCT__ "MatGetRowIJ_Inode"
2348f7157efSSatish Balay static PetscErrorCode MatGetRowIJ_Inode(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
2354c1414c8SBarry Smith {
2364c1414c8SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2374c1414c8SBarry Smith   PetscErrorCode ierr;
2384c1414c8SBarry Smith 
2394c1414c8SBarry Smith   PetscFunctionBegin;
2404c1414c8SBarry Smith   *n     = a->inode.node_count;
2414c1414c8SBarry Smith   if (!ia) PetscFunctionReturn(0);
2428f7157efSSatish Balay   if (!blockcompressed) {
2438f7157efSSatish Balay     ierr = MatGetRowIJ_SeqAIJ(A,oshift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);;
2448f7157efSSatish Balay   } else if (symmetric) {
2454c1414c8SBarry Smith     ierr = MatGetRowIJ_Inode_Symmetric(A,ia,ja,0,oshift);CHKERRQ(ierr);
2464c1414c8SBarry Smith   } else {
2474c1414c8SBarry Smith     ierr = MatGetRowIJ_Inode_Nonsymmetric(A,ia,ja,0,oshift);CHKERRQ(ierr);
2484c1414c8SBarry Smith   }
2494c1414c8SBarry Smith   PetscFunctionReturn(0);
2504c1414c8SBarry Smith }
2514c1414c8SBarry Smith 
2524c1414c8SBarry Smith #undef __FUNCT__
2534c1414c8SBarry Smith #define __FUNCT__ "MatRestoreRowIJ_Inode"
2548f7157efSSatish Balay static PetscErrorCode MatRestoreRowIJ_Inode(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
2554c1414c8SBarry Smith {
2564c1414c8SBarry Smith   PetscErrorCode ierr;
2574c1414c8SBarry Smith 
2584c1414c8SBarry Smith   PetscFunctionBegin;
2594c1414c8SBarry Smith   if (!ia) PetscFunctionReturn(0);
2608f7157efSSatish Balay 
2618f7157efSSatish Balay   if (!blockcompressed) {
2628f7157efSSatish Balay     ierr = MatRestoreRowIJ_SeqAIJ(A,oshift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);;
2638f7157efSSatish Balay   } else {
2644c1414c8SBarry Smith     ierr = PetscFree(*ia);CHKERRQ(ierr);
2654c1414c8SBarry Smith     ierr = PetscFree(*ja);CHKERRQ(ierr);
2668f7157efSSatish Balay   }
2678f7157efSSatish Balay 
2684c1414c8SBarry Smith   PetscFunctionReturn(0);
2694c1414c8SBarry Smith }
2704c1414c8SBarry Smith 
2714c1414c8SBarry Smith /* ----------------------------------------------------------- */
2724c1414c8SBarry Smith 
2734c1414c8SBarry Smith #undef __FUNCT__
2744c1414c8SBarry Smith #define __FUNCT__ "MatGetColumnIJ_Inode_Nonsymmetric"
2754c1414c8SBarry Smith static PetscErrorCode MatGetColumnIJ_Inode_Nonsymmetric(Mat A,PetscInt *iia[],PetscInt *jja[],PetscInt ishift,PetscInt oshift)
2764c1414c8SBarry Smith {
2774c1414c8SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2784c1414c8SBarry Smith   PetscErrorCode ierr;
2794c1414c8SBarry Smith   PetscInt       *work,*ia,*ja,*j,nz,nslim_row, n,row,col,*ns_col,nslim_col;
2804c1414c8SBarry Smith   PetscInt       *tns,*tvc,*ns_row = a->inode.size,nsz,i1,i2,*ai= a->i,*aj = a->j;
2814c1414c8SBarry Smith 
2824c1414c8SBarry Smith   PetscFunctionBegin;
2834c1414c8SBarry Smith   nslim_row = a->inode.node_count;
284d0f46423SBarry Smith   n         = A->cmap->n;
2854c1414c8SBarry Smith 
2864c1414c8SBarry Smith   /* Create The column_inode for this matrix */
2874c1414c8SBarry Smith   ierr = Mat_CreateColInode(A,&nslim_col,&ns_col);CHKERRQ(ierr);
2884c1414c8SBarry Smith 
2894c1414c8SBarry Smith   /* allocate space for reformated column_inode structure */
2904c1414c8SBarry Smith   ierr = PetscMalloc((nslim_col + 1)*sizeof(PetscInt),&tns);CHKERRQ(ierr);
2914c1414c8SBarry Smith   ierr = PetscMalloc((n + 1)*sizeof(PetscInt),&tvc);CHKERRQ(ierr);
2924c1414c8SBarry Smith   for (i1=0,tns[0]=0; i1<nslim_col; ++i1) tns[i1+1] = tns[i1] + ns_col[i1];
2934c1414c8SBarry Smith 
2944c1414c8SBarry Smith   for (i1=0,col=0; i1<nslim_col; ++i1){
2954c1414c8SBarry Smith     nsz = ns_col[i1];
2964c1414c8SBarry Smith     for (i2=0; i2<nsz; ++i2,++col)
2974c1414c8SBarry Smith       tvc[col] = i1;
2984c1414c8SBarry Smith   }
2994c1414c8SBarry Smith   /* allocate space for column pointers */
3004c1414c8SBarry Smith   ierr = PetscMalloc((nslim_col+1)*sizeof(PetscInt),&ia);CHKERRQ(ierr);
3014c1414c8SBarry Smith   *iia = ia;
3024c1414c8SBarry Smith   ierr = PetscMemzero(ia,(nslim_col+1)*sizeof(PetscInt));CHKERRQ(ierr);
3034c1414c8SBarry Smith   ierr = PetscMalloc((nslim_col+1)*sizeof(PetscInt),&work);CHKERRQ(ierr);
3044c1414c8SBarry Smith 
3054c1414c8SBarry Smith   /* determine the number of columns in each row */
3064c1414c8SBarry Smith   ia[0] = oshift;
3074c1414c8SBarry Smith   for (i1=0,row=0; i1<nslim_row; row+=ns_row[i1],i1++) {
3084c1414c8SBarry Smith     j   = aj + ai[row] + ishift;
3094c1414c8SBarry Smith     col = *j++ + ishift;
3104c1414c8SBarry Smith     i2  = tvc[col];
3114c1414c8SBarry Smith     nz  = ai[row+1] - ai[row];
3124c1414c8SBarry Smith     while (nz-- > 0) {           /* off-diagonal elemets */
3134c1414c8SBarry Smith       /* ia[i1+1]++; */
3144c1414c8SBarry Smith       ia[i2+1]++;
3154c1414c8SBarry Smith       i2++;
3164c1414c8SBarry Smith       while (((col = *j++ + ishift) < tns[i2]) && nz > 0) {nz--;}
3174c1414c8SBarry Smith       if (nz > 0) i2 = tvc[col];
3184c1414c8SBarry Smith     }
3194c1414c8SBarry Smith   }
3204c1414c8SBarry Smith 
3214c1414c8SBarry Smith   /* shift ia[i] to point to next col */
3224c1414c8SBarry Smith   for (i1=1; i1<nslim_col+1; i1++) {
3234c1414c8SBarry Smith     col        = ia[i1-1];
3244c1414c8SBarry Smith     ia[i1]    += col;
3254c1414c8SBarry Smith     work[i1-1] = col - oshift;
3264c1414c8SBarry Smith   }
3274c1414c8SBarry Smith 
3284c1414c8SBarry Smith   /* allocate space for column pointers */
3294c1414c8SBarry Smith   nz   = ia[nslim_col] + (!ishift);
3304c1414c8SBarry Smith   ierr = PetscMalloc(nz*sizeof(PetscInt),&ja);CHKERRQ(ierr);
3314c1414c8SBarry Smith   *jja = ja;
3324c1414c8SBarry Smith 
3334c1414c8SBarry Smith  /* loop over matrix putting into ja */
3344c1414c8SBarry Smith   for (i1=0,row=0; i1<nslim_row; row+=ns_row[i1],i1++) {
3354c1414c8SBarry Smith     j   = aj + ai[row] + ishift;
3364c1414c8SBarry Smith     i2  = 0;                     /* Col inode index */
3374c1414c8SBarry Smith     col = *j++ + ishift;
3384c1414c8SBarry Smith     i2  = tvc[col];
3394c1414c8SBarry Smith     nz  = ai[row+1] - ai[row];
3404c1414c8SBarry Smith     while (nz-- > 0) {
3414c1414c8SBarry Smith       /* ja[work[i1]++] = i2 + oshift; */
3424c1414c8SBarry Smith       ja[work[i2]++] = i1 + oshift;
3434c1414c8SBarry Smith       i2++;
3444c1414c8SBarry Smith       while(((col = *j++ + ishift) < tns[i2]) && nz > 0) {nz--;}
3454c1414c8SBarry Smith       if (nz > 0) i2 = tvc[col];
3464c1414c8SBarry Smith     }
3474c1414c8SBarry Smith   }
3484c1414c8SBarry Smith   ierr = PetscFree(ns_col);CHKERRQ(ierr);
3494c1414c8SBarry Smith   ierr = PetscFree(work);CHKERRQ(ierr);
3504c1414c8SBarry Smith   ierr = PetscFree(tns);CHKERRQ(ierr);
3514c1414c8SBarry Smith   ierr = PetscFree(tvc);CHKERRQ(ierr);
3524c1414c8SBarry Smith   PetscFunctionReturn(0);
3534c1414c8SBarry Smith }
3544c1414c8SBarry Smith 
3554c1414c8SBarry Smith #undef __FUNCT__
3564c1414c8SBarry Smith #define __FUNCT__ "MatGetColumnIJ_Inode"
3578f7157efSSatish Balay static PetscErrorCode MatGetColumnIJ_Inode(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
3584c1414c8SBarry Smith {
3594c1414c8SBarry Smith   PetscErrorCode ierr;
3604c1414c8SBarry Smith 
3614c1414c8SBarry Smith   PetscFunctionBegin;
3624c1414c8SBarry Smith   ierr = Mat_CreateColInode(A,n,PETSC_NULL);CHKERRQ(ierr);
3634c1414c8SBarry Smith   if (!ia) PetscFunctionReturn(0);
3644c1414c8SBarry Smith 
3658f7157efSSatish Balay   if (!blockcompressed) {
3668f7157efSSatish Balay     ierr = MatGetColumnIJ_SeqAIJ(A,oshift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);;
3678f7157efSSatish Balay   } else if (symmetric) {
3684c1414c8SBarry Smith     /* Since the indices are symmetric it does'nt matter */
3694c1414c8SBarry Smith     ierr = MatGetRowIJ_Inode_Symmetric(A,ia,ja,0,oshift);CHKERRQ(ierr);
3704c1414c8SBarry Smith   } else {
3714c1414c8SBarry Smith     ierr = MatGetColumnIJ_Inode_Nonsymmetric(A,ia,ja,0,oshift);CHKERRQ(ierr);
3724c1414c8SBarry Smith   }
3734c1414c8SBarry Smith   PetscFunctionReturn(0);
3744c1414c8SBarry Smith }
3754c1414c8SBarry Smith 
3764c1414c8SBarry Smith #undef __FUNCT__
3774c1414c8SBarry Smith #define __FUNCT__ "MatRestoreColumnIJ_Inode"
3788f7157efSSatish Balay static PetscErrorCode MatRestoreColumnIJ_Inode(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
3794c1414c8SBarry Smith {
3804c1414c8SBarry Smith   PetscErrorCode ierr;
3814c1414c8SBarry Smith 
3824c1414c8SBarry Smith   PetscFunctionBegin;
3834c1414c8SBarry Smith   if (!ia) PetscFunctionReturn(0);
3848f7157efSSatish Balay   if (!blockcompressed) {
3858f7157efSSatish Balay     ierr = MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,blockcompressed,n,ia,ja,done);CHKERRQ(ierr);;
3868f7157efSSatish Balay   } else {
3874c1414c8SBarry Smith     ierr = PetscFree(*ia);CHKERRQ(ierr);
3884c1414c8SBarry Smith     ierr = PetscFree(*ja);CHKERRQ(ierr);
3898f7157efSSatish Balay   }
3904c1414c8SBarry Smith   PetscFunctionReturn(0);
3914c1414c8SBarry Smith }
3924c1414c8SBarry Smith 
3934c1414c8SBarry Smith /* ----------------------------------------------------------- */
3944c1414c8SBarry Smith 
3954c1414c8SBarry Smith #undef __FUNCT__
3964c1414c8SBarry Smith #define __FUNCT__ "MatMult_Inode"
3974c1414c8SBarry Smith static PetscErrorCode MatMult_Inode(Mat A,Vec xx,Vec yy)
3984c1414c8SBarry Smith {
3994c1414c8SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
4004c1414c8SBarry Smith   PetscScalar       sum1,sum2,sum3,sum4,sum5,tmp0,tmp1;
401d9fead3dSBarry Smith   PetscScalar       *y;
402dd6ea824SBarry Smith   const PetscScalar *x;
403dd6ea824SBarry Smith   const MatScalar   *v1,*v2,*v3,*v4,*v5;
4044c1414c8SBarry Smith   PetscErrorCode    ierr;
40598c9bda7SSatish Balay   PetscInt          *idx,i1,i2,n,i,row,node_max,*ns,*ii,nsz,sz,nonzerorow=0;
4064c1414c8SBarry Smith 
4074c1414c8SBarry Smith #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
4084c1414c8SBarry Smith #pragma disjoint(*x,*y,*v1,*v2,*v3,*v4,*v5)
4094c1414c8SBarry Smith #endif
4104c1414c8SBarry Smith 
4114c1414c8SBarry Smith   PetscFunctionBegin;
4124c1414c8SBarry Smith   if (!a->inode.size) SETERRQ(PETSC_ERR_COR,"Missing Inode Structure");
4134c1414c8SBarry Smith   node_max = a->inode.node_count;
4144c1414c8SBarry Smith   ns       = a->inode.size;     /* Node Size array */
415d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
4164c1414c8SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
4174c1414c8SBarry Smith   idx  = a->j;
4184c1414c8SBarry Smith   v1   = a->a;
4194c1414c8SBarry Smith   ii   = a->i;
4204c1414c8SBarry Smith 
4214c1414c8SBarry Smith   for (i = 0,row = 0; i< node_max; ++i){
4224c1414c8SBarry Smith     nsz  = ns[i];
4234c1414c8SBarry Smith     n    = ii[1] - ii[0];
42498c9bda7SSatish Balay     nonzerorow += (n>0)*nsz;
4254c1414c8SBarry Smith     ii  += nsz;
4264c1414c8SBarry Smith     sz   = n;                   /* No of non zeros in this row */
4274c1414c8SBarry Smith                                 /* Switch on the size of Node */
4284c1414c8SBarry Smith     switch (nsz){               /* Each loop in 'case' is unrolled */
4294c1414c8SBarry Smith     case 1 :
4304c1414c8SBarry Smith       sum1  = 0;
4314c1414c8SBarry Smith 
4324c1414c8SBarry Smith       for(n = 0; n< sz-1; n+=2) {
4334c1414c8SBarry Smith         i1   = idx[0];          /* The instructions are ordered to */
4344c1414c8SBarry Smith         i2   = idx[1];          /* make the compiler's job easy */
4354c1414c8SBarry Smith         idx += 2;
4364c1414c8SBarry Smith         tmp0 = x[i1];
4374c1414c8SBarry Smith         tmp1 = x[i2];
4384c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
4394c1414c8SBarry Smith        }
4404c1414c8SBarry Smith 
4414c1414c8SBarry Smith       if (n == sz-1){          /* Take care of the last nonzero  */
4424c1414c8SBarry Smith         tmp0  = x[*idx++];
4434c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
4444c1414c8SBarry Smith       }
4454c1414c8SBarry Smith       y[row++]=sum1;
4464c1414c8SBarry Smith       break;
4474c1414c8SBarry Smith     case 2:
4484c1414c8SBarry Smith       sum1  = 0;
4494c1414c8SBarry Smith       sum2  = 0;
4504c1414c8SBarry Smith       v2    = v1 + n;
4514c1414c8SBarry Smith 
4524c1414c8SBarry Smith       for (n = 0; n< sz-1; n+=2) {
4534c1414c8SBarry Smith         i1   = idx[0];
4544c1414c8SBarry Smith         i2   = idx[1];
4554c1414c8SBarry Smith         idx += 2;
4564c1414c8SBarry Smith         tmp0 = x[i1];
4574c1414c8SBarry Smith         tmp1 = x[i2];
4584c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
4594c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
4604c1414c8SBarry Smith       }
4614c1414c8SBarry Smith       if (n == sz-1){
4624c1414c8SBarry Smith         tmp0  = x[*idx++];
4634c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
4644c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
4654c1414c8SBarry Smith       }
4664c1414c8SBarry Smith       y[row++]=sum1;
4674c1414c8SBarry Smith       y[row++]=sum2;
4684c1414c8SBarry Smith       v1      =v2;              /* Since the next block to be processed starts there*/
4694c1414c8SBarry Smith       idx    +=sz;
4704c1414c8SBarry Smith       break;
4714c1414c8SBarry Smith     case 3:
4724c1414c8SBarry Smith       sum1  = 0;
4734c1414c8SBarry Smith       sum2  = 0;
4744c1414c8SBarry Smith       sum3  = 0;
4754c1414c8SBarry Smith       v2    = v1 + n;
4764c1414c8SBarry Smith       v3    = v2 + n;
4774c1414c8SBarry Smith 
4784c1414c8SBarry Smith       for (n = 0; n< sz-1; n+=2) {
4794c1414c8SBarry Smith         i1   = idx[0];
4804c1414c8SBarry Smith         i2   = idx[1];
4814c1414c8SBarry Smith         idx += 2;
4824c1414c8SBarry Smith         tmp0 = x[i1];
4834c1414c8SBarry Smith         tmp1 = x[i2];
4844c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
4854c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
4864c1414c8SBarry Smith         sum3 += v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
4874c1414c8SBarry Smith       }
4884c1414c8SBarry Smith       if (n == sz-1){
4894c1414c8SBarry Smith         tmp0  = x[*idx++];
4904c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
4914c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
4924c1414c8SBarry Smith         sum3 += *v3++ * tmp0;
4934c1414c8SBarry Smith       }
4944c1414c8SBarry Smith       y[row++]=sum1;
4954c1414c8SBarry Smith       y[row++]=sum2;
4964c1414c8SBarry Smith       y[row++]=sum3;
4974c1414c8SBarry Smith       v1       =v3;             /* Since the next block to be processed starts there*/
4984c1414c8SBarry Smith       idx     +=2*sz;
4994c1414c8SBarry Smith       break;
5004c1414c8SBarry Smith     case 4:
5014c1414c8SBarry Smith       sum1  = 0;
5024c1414c8SBarry Smith       sum2  = 0;
5034c1414c8SBarry Smith       sum3  = 0;
5044c1414c8SBarry Smith       sum4  = 0;
5054c1414c8SBarry Smith       v2    = v1 + n;
5064c1414c8SBarry Smith       v3    = v2 + n;
5074c1414c8SBarry Smith       v4    = v3 + n;
5084c1414c8SBarry Smith 
5094c1414c8SBarry Smith       for (n = 0; n< sz-1; n+=2) {
5104c1414c8SBarry Smith         i1   = idx[0];
5114c1414c8SBarry Smith         i2   = idx[1];
5124c1414c8SBarry Smith         idx += 2;
5134c1414c8SBarry Smith         tmp0 = x[i1];
5144c1414c8SBarry Smith         tmp1 = x[i2];
5154c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] *tmp1; v1 += 2;
5164c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] *tmp1; v2 += 2;
5174c1414c8SBarry Smith         sum3 += v3[0] * tmp0 + v3[1] *tmp1; v3 += 2;
5184c1414c8SBarry Smith         sum4 += v4[0] * tmp0 + v4[1] *tmp1; v4 += 2;
5194c1414c8SBarry Smith       }
5204c1414c8SBarry Smith       if (n == sz-1){
5214c1414c8SBarry Smith         tmp0  = x[*idx++];
5224c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
5234c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
5244c1414c8SBarry Smith         sum3 += *v3++ * tmp0;
5254c1414c8SBarry Smith         sum4 += *v4++ * tmp0;
5264c1414c8SBarry Smith       }
5274c1414c8SBarry Smith       y[row++]=sum1;
5284c1414c8SBarry Smith       y[row++]=sum2;
5294c1414c8SBarry Smith       y[row++]=sum3;
5304c1414c8SBarry Smith       y[row++]=sum4;
5314c1414c8SBarry Smith       v1      =v4;              /* Since the next block to be processed starts there*/
5324c1414c8SBarry Smith       idx    +=3*sz;
5334c1414c8SBarry Smith       break;
5344c1414c8SBarry Smith     case 5:
5354c1414c8SBarry Smith       sum1  = 0;
5364c1414c8SBarry Smith       sum2  = 0;
5374c1414c8SBarry Smith       sum3  = 0;
5384c1414c8SBarry Smith       sum4  = 0;
5394c1414c8SBarry Smith       sum5  = 0;
5404c1414c8SBarry Smith       v2    = v1 + n;
5414c1414c8SBarry Smith       v3    = v2 + n;
5424c1414c8SBarry Smith       v4    = v3 + n;
5434c1414c8SBarry Smith       v5    = v4 + n;
5444c1414c8SBarry Smith 
5454c1414c8SBarry Smith       for (n = 0; n<sz-1; n+=2) {
5464c1414c8SBarry Smith         i1   = idx[0];
5474c1414c8SBarry Smith         i2   = idx[1];
5484c1414c8SBarry Smith         idx += 2;
5494c1414c8SBarry Smith         tmp0 = x[i1];
5504c1414c8SBarry Smith         tmp1 = x[i2];
5514c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] *tmp1; v1 += 2;
5524c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] *tmp1; v2 += 2;
5534c1414c8SBarry Smith         sum3 += v3[0] * tmp0 + v3[1] *tmp1; v3 += 2;
5544c1414c8SBarry Smith         sum4 += v4[0] * tmp0 + v4[1] *tmp1; v4 += 2;
5554c1414c8SBarry Smith         sum5 += v5[0] * tmp0 + v5[1] *tmp1; v5 += 2;
5564c1414c8SBarry Smith       }
5574c1414c8SBarry Smith       if (n == sz-1){
5584c1414c8SBarry Smith         tmp0  = x[*idx++];
5594c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
5604c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
5614c1414c8SBarry Smith         sum3 += *v3++ * tmp0;
5624c1414c8SBarry Smith         sum4 += *v4++ * tmp0;
5634c1414c8SBarry Smith         sum5 += *v5++ * tmp0;
5644c1414c8SBarry Smith       }
5654c1414c8SBarry Smith       y[row++]=sum1;
5664c1414c8SBarry Smith       y[row++]=sum2;
5674c1414c8SBarry Smith       y[row++]=sum3;
5684c1414c8SBarry Smith       y[row++]=sum4;
5694c1414c8SBarry Smith       y[row++]=sum5;
5704c1414c8SBarry Smith       v1      =v5;       /* Since the next block to be processed starts there */
5714c1414c8SBarry Smith       idx    +=4*sz;
5724c1414c8SBarry Smith       break;
5734c1414c8SBarry Smith     default :
5744c1414c8SBarry Smith       SETERRQ(PETSC_ERR_COR,"Node size not yet supported");
5754c1414c8SBarry Smith     }
5764c1414c8SBarry Smith   }
577d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
5784c1414c8SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
57998c9bda7SSatish Balay   ierr = PetscLogFlops(2*a->nz - nonzerorow);CHKERRQ(ierr);
5804c1414c8SBarry Smith   PetscFunctionReturn(0);
5814c1414c8SBarry Smith }
5824c1414c8SBarry Smith /* ----------------------------------------------------------- */
5834c1414c8SBarry Smith /* Almost same code as the MatMult_Inode() */
5844c1414c8SBarry Smith #undef __FUNCT__
5854c1414c8SBarry Smith #define __FUNCT__ "MatMultAdd_Inode"
5864c1414c8SBarry Smith static PetscErrorCode MatMultAdd_Inode(Mat A,Vec xx,Vec zz,Vec yy)
5874c1414c8SBarry Smith {
5884c1414c8SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
5894c1414c8SBarry Smith   PetscScalar    sum1,sum2,sum3,sum4,sum5,tmp0,tmp1;
590dd6ea824SBarry Smith   MatScalar      *v1,*v2,*v3,*v4,*v5;
591dd6ea824SBarry Smith   PetscScalar    *x,*y,*z,*zt;
5924c1414c8SBarry Smith   PetscErrorCode ierr;
5934c1414c8SBarry Smith   PetscInt       *idx,i1,i2,n,i,row,node_max,*ns,*ii,nsz,sz;
5944c1414c8SBarry Smith 
5954c1414c8SBarry Smith   PetscFunctionBegin;
5964c1414c8SBarry Smith   if (!a->inode.size) SETERRQ(PETSC_ERR_COR,"Missing Inode Structure");
5974c1414c8SBarry Smith   node_max = a->inode.node_count;
5984c1414c8SBarry Smith   ns       = a->inode.size;     /* Node Size array */
5994c1414c8SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
6004c1414c8SBarry Smith   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
6014c1414c8SBarry Smith   if (zz != yy) {
6024c1414c8SBarry Smith     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
6034c1414c8SBarry Smith   } else {
6044c1414c8SBarry Smith     z = y;
6054c1414c8SBarry Smith   }
6064c1414c8SBarry Smith   zt = z;
6074c1414c8SBarry Smith 
6084c1414c8SBarry Smith   idx  = a->j;
6094c1414c8SBarry Smith   v1   = a->a;
6104c1414c8SBarry Smith   ii   = a->i;
6114c1414c8SBarry Smith 
6124c1414c8SBarry Smith   for (i = 0,row = 0; i< node_max; ++i){
6134c1414c8SBarry Smith     nsz  = ns[i];
6144c1414c8SBarry Smith     n    = ii[1] - ii[0];
6154c1414c8SBarry Smith     ii  += nsz;
6164c1414c8SBarry Smith     sz   = n;                   /* No of non zeros in this row */
6174c1414c8SBarry Smith                                 /* Switch on the size of Node */
6184c1414c8SBarry Smith     switch (nsz){               /* Each loop in 'case' is unrolled */
6194c1414c8SBarry Smith     case 1 :
6204c1414c8SBarry Smith       sum1  = *zt++;
6214c1414c8SBarry Smith 
6224c1414c8SBarry Smith       for(n = 0; n< sz-1; n+=2) {
6234c1414c8SBarry Smith         i1   = idx[0];          /* The instructions are ordered to */
6244c1414c8SBarry Smith         i2   = idx[1];          /* make the compiler's job easy */
6254c1414c8SBarry Smith         idx += 2;
6264c1414c8SBarry Smith         tmp0 = x[i1];
6274c1414c8SBarry Smith         tmp1 = x[i2];
6284c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
6294c1414c8SBarry Smith        }
6304c1414c8SBarry Smith 
6314c1414c8SBarry Smith       if(n   == sz-1){          /* Take care of the last nonzero  */
6324c1414c8SBarry Smith         tmp0  = x[*idx++];
6334c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
6344c1414c8SBarry Smith       }
6354c1414c8SBarry Smith       y[row++]=sum1;
6364c1414c8SBarry Smith       break;
6374c1414c8SBarry Smith     case 2:
6384c1414c8SBarry Smith       sum1  = *zt++;
6394c1414c8SBarry Smith       sum2  = *zt++;
6404c1414c8SBarry Smith       v2    = v1 + n;
6414c1414c8SBarry Smith 
6424c1414c8SBarry Smith       for(n = 0; n< sz-1; n+=2) {
6434c1414c8SBarry Smith         i1   = idx[0];
6444c1414c8SBarry Smith         i2   = idx[1];
6454c1414c8SBarry Smith         idx += 2;
6464c1414c8SBarry Smith         tmp0 = x[i1];
6474c1414c8SBarry Smith         tmp1 = x[i2];
6484c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
6494c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
6504c1414c8SBarry Smith       }
6514c1414c8SBarry Smith       if(n   == sz-1){
6524c1414c8SBarry Smith         tmp0  = x[*idx++];
6534c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
6544c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
6554c1414c8SBarry Smith       }
6564c1414c8SBarry Smith       y[row++]=sum1;
6574c1414c8SBarry Smith       y[row++]=sum2;
6584c1414c8SBarry Smith       v1      =v2;              /* Since the next block to be processed starts there*/
6594c1414c8SBarry Smith       idx    +=sz;
6604c1414c8SBarry Smith       break;
6614c1414c8SBarry Smith     case 3:
6624c1414c8SBarry Smith       sum1  = *zt++;
6634c1414c8SBarry Smith       sum2  = *zt++;
6644c1414c8SBarry Smith       sum3  = *zt++;
6654c1414c8SBarry Smith       v2    = v1 + n;
6664c1414c8SBarry Smith       v3    = v2 + n;
6674c1414c8SBarry Smith 
6684c1414c8SBarry Smith       for (n = 0; n< sz-1; n+=2) {
6694c1414c8SBarry Smith         i1   = idx[0];
6704c1414c8SBarry Smith         i2   = idx[1];
6714c1414c8SBarry Smith         idx += 2;
6724c1414c8SBarry Smith         tmp0 = x[i1];
6734c1414c8SBarry Smith         tmp1 = x[i2];
6744c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
6754c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
6764c1414c8SBarry Smith         sum3 += v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
6774c1414c8SBarry Smith       }
6784c1414c8SBarry Smith       if (n == sz-1){
6794c1414c8SBarry Smith         tmp0  = x[*idx++];
6804c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
6814c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
6824c1414c8SBarry Smith         sum3 += *v3++ * tmp0;
6834c1414c8SBarry Smith       }
6844c1414c8SBarry Smith       y[row++]=sum1;
6854c1414c8SBarry Smith       y[row++]=sum2;
6864c1414c8SBarry Smith       y[row++]=sum3;
6874c1414c8SBarry Smith       v1       =v3;             /* Since the next block to be processed starts there*/
6884c1414c8SBarry Smith       idx     +=2*sz;
6894c1414c8SBarry Smith       break;
6904c1414c8SBarry Smith     case 4:
6914c1414c8SBarry Smith       sum1  = *zt++;
6924c1414c8SBarry Smith       sum2  = *zt++;
6934c1414c8SBarry Smith       sum3  = *zt++;
6944c1414c8SBarry Smith       sum4  = *zt++;
6954c1414c8SBarry Smith       v2    = v1 + n;
6964c1414c8SBarry Smith       v3    = v2 + n;
6974c1414c8SBarry Smith       v4    = v3 + n;
6984c1414c8SBarry Smith 
6994c1414c8SBarry Smith       for (n = 0; n< sz-1; n+=2) {
7004c1414c8SBarry Smith         i1   = idx[0];
7014c1414c8SBarry Smith         i2   = idx[1];
7024c1414c8SBarry Smith         idx += 2;
7034c1414c8SBarry Smith         tmp0 = x[i1];
7044c1414c8SBarry Smith         tmp1 = x[i2];
7054c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] *tmp1; v1 += 2;
7064c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] *tmp1; v2 += 2;
7074c1414c8SBarry Smith         sum3 += v3[0] * tmp0 + v3[1] *tmp1; v3 += 2;
7084c1414c8SBarry Smith         sum4 += v4[0] * tmp0 + v4[1] *tmp1; v4 += 2;
7094c1414c8SBarry Smith       }
7104c1414c8SBarry Smith       if (n == sz-1){
7114c1414c8SBarry Smith         tmp0  = x[*idx++];
7124c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
7134c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
7144c1414c8SBarry Smith         sum3 += *v3++ * tmp0;
7154c1414c8SBarry Smith         sum4 += *v4++ * tmp0;
7164c1414c8SBarry Smith       }
7174c1414c8SBarry Smith       y[row++]=sum1;
7184c1414c8SBarry Smith       y[row++]=sum2;
7194c1414c8SBarry Smith       y[row++]=sum3;
7204c1414c8SBarry Smith       y[row++]=sum4;
7214c1414c8SBarry Smith       v1      =v4;              /* Since the next block to be processed starts there*/
7224c1414c8SBarry Smith       idx    +=3*sz;
7234c1414c8SBarry Smith       break;
7244c1414c8SBarry Smith     case 5:
7254c1414c8SBarry Smith       sum1  = *zt++;
7264c1414c8SBarry Smith       sum2  = *zt++;
7274c1414c8SBarry Smith       sum3  = *zt++;
7284c1414c8SBarry Smith       sum4  = *zt++;
7294c1414c8SBarry Smith       sum5  = *zt++;
7304c1414c8SBarry Smith       v2    = v1 + n;
7314c1414c8SBarry Smith       v3    = v2 + n;
7324c1414c8SBarry Smith       v4    = v3 + n;
7334c1414c8SBarry Smith       v5    = v4 + n;
7344c1414c8SBarry Smith 
7354c1414c8SBarry Smith       for (n = 0; n<sz-1; n+=2) {
7364c1414c8SBarry Smith         i1   = idx[0];
7374c1414c8SBarry Smith         i2   = idx[1];
7384c1414c8SBarry Smith         idx += 2;
7394c1414c8SBarry Smith         tmp0 = x[i1];
7404c1414c8SBarry Smith         tmp1 = x[i2];
7414c1414c8SBarry Smith         sum1 += v1[0] * tmp0 + v1[1] *tmp1; v1 += 2;
7424c1414c8SBarry Smith         sum2 += v2[0] * tmp0 + v2[1] *tmp1; v2 += 2;
7434c1414c8SBarry Smith         sum3 += v3[0] * tmp0 + v3[1] *tmp1; v3 += 2;
7444c1414c8SBarry Smith         sum4 += v4[0] * tmp0 + v4[1] *tmp1; v4 += 2;
7454c1414c8SBarry Smith         sum5 += v5[0] * tmp0 + v5[1] *tmp1; v5 += 2;
7464c1414c8SBarry Smith       }
7474c1414c8SBarry Smith       if(n   == sz-1){
7484c1414c8SBarry Smith         tmp0  = x[*idx++];
7494c1414c8SBarry Smith         sum1 += *v1++ * tmp0;
7504c1414c8SBarry Smith         sum2 += *v2++ * tmp0;
7514c1414c8SBarry Smith         sum3 += *v3++ * tmp0;
7524c1414c8SBarry Smith         sum4 += *v4++ * tmp0;
7534c1414c8SBarry Smith         sum5 += *v5++ * tmp0;
7544c1414c8SBarry Smith       }
7554c1414c8SBarry Smith       y[row++]=sum1;
7564c1414c8SBarry Smith       y[row++]=sum2;
7574c1414c8SBarry Smith       y[row++]=sum3;
7584c1414c8SBarry Smith       y[row++]=sum4;
7594c1414c8SBarry Smith       y[row++]=sum5;
7604c1414c8SBarry Smith       v1      =v5;       /* Since the next block to be processed starts there */
7614c1414c8SBarry Smith       idx    +=4*sz;
7624c1414c8SBarry Smith       break;
7634c1414c8SBarry Smith     default :
7644c1414c8SBarry Smith       SETERRQ(PETSC_ERR_COR,"Node size not yet supported");
7654c1414c8SBarry Smith     }
7664c1414c8SBarry Smith   }
7674c1414c8SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
7684c1414c8SBarry Smith   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
7694c1414c8SBarry Smith   if (zz != yy) {
7704c1414c8SBarry Smith     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
7714c1414c8SBarry Smith   }
7724c1414c8SBarry Smith   ierr = PetscLogFlops(2*a->nz);CHKERRQ(ierr);
7734c1414c8SBarry Smith   PetscFunctionReturn(0);
7744c1414c8SBarry Smith }
7754c1414c8SBarry Smith 
7764c1414c8SBarry Smith /* ----------------------------------------------------------- */
7774c1414c8SBarry Smith #undef __FUNCT__
7784c1414c8SBarry Smith #define __FUNCT__ "MatSolve_Inode"
7794c1414c8SBarry Smith PetscErrorCode MatSolve_Inode(Mat A,Vec bb,Vec xx)
7804c1414c8SBarry Smith {
7814c1414c8SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
7824c1414c8SBarry Smith   IS                iscol = a->col,isrow = a->row;
7834c1414c8SBarry Smith   PetscErrorCode    ierr;
7845d0c19d7SBarry Smith   const PetscInt    *r,*c,*rout,*cout;
7855d0c19d7SBarry Smith   PetscInt          i,j,n = A->rmap->n,*ai = a->i,nz,*a_j = a->j;
7865d0c19d7SBarry Smith   PetscInt          node_max,*ns,row,nsz,aii,*vi,*ad,*aj,i0,i1;
787d9fead3dSBarry Smith   PetscScalar       *x,*tmp,*tmps,tmp0,tmp1;
788d9fead3dSBarry Smith   PetscScalar       sum1,sum2,sum3,sum4,sum5;
789dd6ea824SBarry Smith   const MatScalar   *v1,*v2,*v3,*v4,*v5,*a_a = a->a,*aa;
790dd6ea824SBarry Smith   const PetscScalar *b;
7914c1414c8SBarry Smith 
7924c1414c8SBarry Smith   PetscFunctionBegin;
7934c1414c8SBarry Smith   if (!a->inode.size) SETERRQ(PETSC_ERR_COR,"Missing Inode Structure");
7944c1414c8SBarry Smith   node_max = a->inode.node_count;
7954c1414c8SBarry Smith   ns       = a->inode.size;     /* Node Size array */
7964c1414c8SBarry Smith 
797d9fead3dSBarry Smith   ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
7984c1414c8SBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
7994c1414c8SBarry Smith   tmp  = a->solve_work;
8004c1414c8SBarry Smith 
8014c1414c8SBarry Smith   ierr = ISGetIndices(isrow,&rout);CHKERRQ(ierr); r = rout;
8024c1414c8SBarry Smith   ierr = ISGetIndices(iscol,&cout);CHKERRQ(ierr); c = cout + (n-1);
8034c1414c8SBarry Smith 
8044c1414c8SBarry Smith   /* forward solve the lower triangular */
8054c1414c8SBarry Smith   tmps = tmp ;
8064c1414c8SBarry Smith   aa   = a_a ;
8074c1414c8SBarry Smith   aj   = a_j ;
8084c1414c8SBarry Smith   ad   = a->diag;
8094c1414c8SBarry Smith 
8104c1414c8SBarry Smith   for (i = 0,row = 0; i< node_max; ++i){
8114c1414c8SBarry Smith     nsz = ns[i];
8124c1414c8SBarry Smith     aii = ai[row];
8134c1414c8SBarry Smith     v1  = aa + aii;
8144c1414c8SBarry Smith     vi  = aj + aii;
8154c1414c8SBarry Smith     nz  = ad[row]- aii;
8164c1414c8SBarry Smith 
8174c1414c8SBarry Smith     switch (nsz){               /* Each loop in 'case' is unrolled */
8184c1414c8SBarry Smith     case 1 :
8194c1414c8SBarry Smith       sum1 = b[*r++];
8204c1414c8SBarry Smith       /*      while (nz--) sum1 -= *v1++ *tmps[*vi++];*/
8214c1414c8SBarry Smith       for(j=0; j<nz-1; j+=2){
8224c1414c8SBarry Smith         i0   = vi[0];
8234c1414c8SBarry Smith         i1   = vi[1];
8244c1414c8SBarry Smith         vi  +=2;
8254c1414c8SBarry Smith         tmp0 = tmps[i0];
8264c1414c8SBarry Smith         tmp1 = tmps[i1];
8274c1414c8SBarry Smith         sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
8284c1414c8SBarry Smith       }
8294c1414c8SBarry Smith       if(j == nz-1){
8304c1414c8SBarry Smith         tmp0 = tmps[*vi++];
8314c1414c8SBarry Smith         sum1 -= *v1++ *tmp0;
8324c1414c8SBarry Smith       }
8334c1414c8SBarry Smith       tmp[row ++]=sum1;
8344c1414c8SBarry Smith       break;
8354c1414c8SBarry Smith     case 2:
8364c1414c8SBarry Smith       sum1 = b[*r++];
8374c1414c8SBarry Smith       sum2 = b[*r++];
8384c1414c8SBarry Smith       v2   = aa + ai[row+1];
8394c1414c8SBarry Smith 
8404c1414c8SBarry Smith       for(j=0; j<nz-1; j+=2){
8414c1414c8SBarry Smith         i0   = vi[0];
8424c1414c8SBarry Smith         i1   = vi[1];
8434c1414c8SBarry Smith         vi  +=2;
8444c1414c8SBarry Smith         tmp0 = tmps[i0];
8454c1414c8SBarry Smith         tmp1 = tmps[i1];
8464c1414c8SBarry Smith         sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
8474c1414c8SBarry Smith         sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
8484c1414c8SBarry Smith       }
8494c1414c8SBarry Smith       if(j == nz-1){
8504c1414c8SBarry Smith         tmp0 = tmps[*vi++];
8514c1414c8SBarry Smith         sum1 -= *v1++ *tmp0;
8524c1414c8SBarry Smith         sum2 -= *v2++ *tmp0;
8534c1414c8SBarry Smith       }
8544c1414c8SBarry Smith       sum2 -= *v2++ * sum1;
8554c1414c8SBarry Smith       tmp[row ++]=sum1;
8564c1414c8SBarry Smith       tmp[row ++]=sum2;
8574c1414c8SBarry Smith       break;
8584c1414c8SBarry Smith     case 3:
8594c1414c8SBarry Smith       sum1 = b[*r++];
8604c1414c8SBarry Smith       sum2 = b[*r++];
8614c1414c8SBarry Smith       sum3 = b[*r++];
8624c1414c8SBarry Smith       v2   = aa + ai[row+1];
8634c1414c8SBarry Smith       v3   = aa + ai[row+2];
8644c1414c8SBarry Smith 
8654c1414c8SBarry Smith       for (j=0; j<nz-1; j+=2){
8664c1414c8SBarry Smith         i0   = vi[0];
8674c1414c8SBarry Smith         i1   = vi[1];
8684c1414c8SBarry Smith         vi  +=2;
8694c1414c8SBarry Smith         tmp0 = tmps[i0];
8704c1414c8SBarry Smith         tmp1 = tmps[i1];
8714c1414c8SBarry Smith         sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
8724c1414c8SBarry Smith         sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
8734c1414c8SBarry Smith         sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
8744c1414c8SBarry Smith       }
8754c1414c8SBarry Smith       if (j == nz-1){
8764c1414c8SBarry Smith         tmp0 = tmps[*vi++];
8774c1414c8SBarry Smith         sum1 -= *v1++ *tmp0;
8784c1414c8SBarry Smith         sum2 -= *v2++ *tmp0;
8794c1414c8SBarry Smith         sum3 -= *v3++ *tmp0;
8804c1414c8SBarry Smith       }
8814c1414c8SBarry Smith       sum2 -= *v2++ * sum1;
8824c1414c8SBarry Smith       sum3 -= *v3++ * sum1;
8834c1414c8SBarry Smith       sum3 -= *v3++ * sum2;
8844c1414c8SBarry Smith       tmp[row ++]=sum1;
8854c1414c8SBarry Smith       tmp[row ++]=sum2;
8864c1414c8SBarry Smith       tmp[row ++]=sum3;
8874c1414c8SBarry Smith       break;
8884c1414c8SBarry Smith 
8894c1414c8SBarry Smith     case 4:
8904c1414c8SBarry Smith       sum1 = b[*r++];
8914c1414c8SBarry Smith       sum2 = b[*r++];
8924c1414c8SBarry Smith       sum3 = b[*r++];
8934c1414c8SBarry Smith       sum4 = b[*r++];
8944c1414c8SBarry Smith       v2   = aa + ai[row+1];
8954c1414c8SBarry Smith       v3   = aa + ai[row+2];
8964c1414c8SBarry Smith       v4   = aa + ai[row+3];
8974c1414c8SBarry Smith 
8984c1414c8SBarry Smith       for (j=0; j<nz-1; j+=2){
8994c1414c8SBarry Smith         i0   = vi[0];
9004c1414c8SBarry Smith         i1   = vi[1];
9014c1414c8SBarry Smith         vi  +=2;
9024c1414c8SBarry Smith         tmp0 = tmps[i0];
9034c1414c8SBarry Smith         tmp1 = tmps[i1];
9044c1414c8SBarry Smith         sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
9054c1414c8SBarry Smith         sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
9064c1414c8SBarry Smith         sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
9074c1414c8SBarry Smith         sum4 -= v4[0] * tmp0 + v4[1] * tmp1; v4 += 2;
9084c1414c8SBarry Smith       }
9094c1414c8SBarry Smith       if (j == nz-1){
9104c1414c8SBarry Smith         tmp0 = tmps[*vi++];
9114c1414c8SBarry Smith         sum1 -= *v1++ *tmp0;
9124c1414c8SBarry Smith         sum2 -= *v2++ *tmp0;
9134c1414c8SBarry Smith         sum3 -= *v3++ *tmp0;
9144c1414c8SBarry Smith         sum4 -= *v4++ *tmp0;
9154c1414c8SBarry Smith       }
9164c1414c8SBarry Smith       sum2 -= *v2++ * sum1;
9174c1414c8SBarry Smith       sum3 -= *v3++ * sum1;
9184c1414c8SBarry Smith       sum4 -= *v4++ * sum1;
9194c1414c8SBarry Smith       sum3 -= *v3++ * sum2;
9204c1414c8SBarry Smith       sum4 -= *v4++ * sum2;
9214c1414c8SBarry Smith       sum4 -= *v4++ * sum3;
9224c1414c8SBarry Smith 
9234c1414c8SBarry Smith       tmp[row ++]=sum1;
9244c1414c8SBarry Smith       tmp[row ++]=sum2;
9254c1414c8SBarry Smith       tmp[row ++]=sum3;
9264c1414c8SBarry Smith       tmp[row ++]=sum4;
9274c1414c8SBarry Smith       break;
9284c1414c8SBarry Smith     case 5:
9294c1414c8SBarry Smith       sum1 = b[*r++];
9304c1414c8SBarry Smith       sum2 = b[*r++];
9314c1414c8SBarry Smith       sum3 = b[*r++];
9324c1414c8SBarry Smith       sum4 = b[*r++];
9334c1414c8SBarry Smith       sum5 = b[*r++];
9344c1414c8SBarry Smith       v2   = aa + ai[row+1];
9354c1414c8SBarry Smith       v3   = aa + ai[row+2];
9364c1414c8SBarry Smith       v4   = aa + ai[row+3];
9374c1414c8SBarry Smith       v5   = aa + ai[row+4];
9384c1414c8SBarry Smith 
9394c1414c8SBarry Smith       for (j=0; j<nz-1; j+=2){
9404c1414c8SBarry Smith         i0   = vi[0];
9414c1414c8SBarry Smith         i1   = vi[1];
9424c1414c8SBarry Smith         vi  +=2;
9434c1414c8SBarry Smith         tmp0 = tmps[i0];
9444c1414c8SBarry Smith         tmp1 = tmps[i1];
9454c1414c8SBarry Smith         sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
9464c1414c8SBarry Smith         sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
9474c1414c8SBarry Smith         sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
9484c1414c8SBarry Smith         sum4 -= v4[0] * tmp0 + v4[1] * tmp1; v4 += 2;
9494c1414c8SBarry Smith         sum5 -= v5[0] * tmp0 + v5[1] * tmp1; v5 += 2;
9504c1414c8SBarry Smith       }
9514c1414c8SBarry Smith       if (j == nz-1){
9524c1414c8SBarry Smith         tmp0 = tmps[*vi++];
9534c1414c8SBarry Smith         sum1 -= *v1++ *tmp0;
9544c1414c8SBarry Smith         sum2 -= *v2++ *tmp0;
9554c1414c8SBarry Smith         sum3 -= *v3++ *tmp0;
9564c1414c8SBarry Smith         sum4 -= *v4++ *tmp0;
9574c1414c8SBarry Smith         sum5 -= *v5++ *tmp0;
9584c1414c8SBarry Smith       }
9594c1414c8SBarry Smith 
9604c1414c8SBarry Smith       sum2 -= *v2++ * sum1;
9614c1414c8SBarry Smith       sum3 -= *v3++ * sum1;
9624c1414c8SBarry Smith       sum4 -= *v4++ * sum1;
9634c1414c8SBarry Smith       sum5 -= *v5++ * sum1;
9644c1414c8SBarry Smith       sum3 -= *v3++ * sum2;
9654c1414c8SBarry Smith       sum4 -= *v4++ * sum2;
9664c1414c8SBarry Smith       sum5 -= *v5++ * sum2;
9674c1414c8SBarry Smith       sum4 -= *v4++ * sum3;
9684c1414c8SBarry Smith       sum5 -= *v5++ * sum3;
9694c1414c8SBarry Smith       sum5 -= *v5++ * sum4;
9704c1414c8SBarry Smith 
9714c1414c8SBarry Smith       tmp[row ++]=sum1;
9724c1414c8SBarry Smith       tmp[row ++]=sum2;
9734c1414c8SBarry Smith       tmp[row ++]=sum3;
9744c1414c8SBarry Smith       tmp[row ++]=sum4;
9754c1414c8SBarry Smith       tmp[row ++]=sum5;
9764c1414c8SBarry Smith       break;
9774c1414c8SBarry Smith     default:
9784c1414c8SBarry Smith       SETERRQ(PETSC_ERR_COR,"Node size not yet supported \n");
9794c1414c8SBarry Smith     }
9804c1414c8SBarry Smith   }
9814c1414c8SBarry Smith   /* backward solve the upper triangular */
9824c1414c8SBarry Smith   for (i=node_max -1 ,row = n-1 ; i>=0; i--){
9834c1414c8SBarry Smith     nsz = ns[i];
9844c1414c8SBarry Smith     aii = ai[row+1] -1;
9854c1414c8SBarry Smith     v1  = aa + aii;
9864c1414c8SBarry Smith     vi  = aj + aii;
9874c1414c8SBarry Smith     nz  = aii- ad[row];
9884c1414c8SBarry Smith     switch (nsz){               /* Each loop in 'case' is unrolled */
9894c1414c8SBarry Smith     case 1 :
9904c1414c8SBarry Smith       sum1 = tmp[row];
9914c1414c8SBarry Smith 
9924c1414c8SBarry Smith       for(j=nz ; j>1; j-=2){
9934c1414c8SBarry Smith         vi  -=2;
9944c1414c8SBarry Smith         i0   = vi[2];
9954c1414c8SBarry Smith         i1   = vi[1];
9964c1414c8SBarry Smith         tmp0 = tmps[i0];
9974c1414c8SBarry Smith         tmp1 = tmps[i1];
9984c1414c8SBarry Smith         v1   -= 2;
9994c1414c8SBarry Smith         sum1 -= v1[2] * tmp0 + v1[1] * tmp1;
10004c1414c8SBarry Smith       }
10014c1414c8SBarry Smith       if (j==1){
10024c1414c8SBarry Smith         tmp0  = tmps[*vi--];
10034c1414c8SBarry Smith         sum1 -= *v1-- * tmp0;
10044c1414c8SBarry Smith       }
10054c1414c8SBarry Smith       x[*c--] = tmp[row] = sum1*a_a[ad[row]]; row--;
10064c1414c8SBarry Smith       break;
10074c1414c8SBarry Smith     case 2 :
10084c1414c8SBarry Smith       sum1 = tmp[row];
10094c1414c8SBarry Smith       sum2 = tmp[row -1];
10104c1414c8SBarry Smith       v2   = aa + ai[row]-1;
10114c1414c8SBarry Smith       for (j=nz ; j>1; j-=2){
10124c1414c8SBarry Smith         vi  -=2;
10134c1414c8SBarry Smith         i0   = vi[2];
10144c1414c8SBarry Smith         i1   = vi[1];
10154c1414c8SBarry Smith         tmp0 = tmps[i0];
10164c1414c8SBarry Smith         tmp1 = tmps[i1];
10174c1414c8SBarry Smith         v1   -= 2;
10184c1414c8SBarry Smith         v2   -= 2;
10194c1414c8SBarry Smith         sum1 -= v1[2] * tmp0 + v1[1] * tmp1;
10204c1414c8SBarry Smith         sum2 -= v2[2] * tmp0 + v2[1] * tmp1;
10214c1414c8SBarry Smith       }
10224c1414c8SBarry Smith       if (j==1){
10234c1414c8SBarry Smith         tmp0  = tmps[*vi--];
10244c1414c8SBarry Smith         sum1 -= *v1-- * tmp0;
10254c1414c8SBarry Smith         sum2 -= *v2-- * tmp0;
10264c1414c8SBarry Smith       }
10274c1414c8SBarry Smith 
10284c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum1*a_a[ad[row]]; row--;
10294c1414c8SBarry Smith       sum2   -= *v2-- * tmp0;
10304c1414c8SBarry Smith       x[*c--] = tmp[row] = sum2*a_a[ad[row]]; row--;
10314c1414c8SBarry Smith       break;
10324c1414c8SBarry Smith     case 3 :
10334c1414c8SBarry Smith       sum1 = tmp[row];
10344c1414c8SBarry Smith       sum2 = tmp[row -1];
10354c1414c8SBarry Smith       sum3 = tmp[row -2];
10364c1414c8SBarry Smith       v2   = aa + ai[row]-1;
10374c1414c8SBarry Smith       v3   = aa + ai[row -1]-1;
10384c1414c8SBarry Smith       for (j=nz ; j>1; j-=2){
10394c1414c8SBarry Smith         vi  -=2;
10404c1414c8SBarry Smith         i0   = vi[2];
10414c1414c8SBarry Smith         i1   = vi[1];
10424c1414c8SBarry Smith         tmp0 = tmps[i0];
10434c1414c8SBarry Smith         tmp1 = tmps[i1];
10444c1414c8SBarry Smith         v1   -= 2;
10454c1414c8SBarry Smith         v2   -= 2;
10464c1414c8SBarry Smith         v3   -= 2;
10474c1414c8SBarry Smith         sum1 -= v1[2] * tmp0 + v1[1] * tmp1;
10484c1414c8SBarry Smith         sum2 -= v2[2] * tmp0 + v2[1] * tmp1;
10494c1414c8SBarry Smith         sum3 -= v3[2] * tmp0 + v3[1] * tmp1;
10504c1414c8SBarry Smith       }
10514c1414c8SBarry Smith       if (j==1){
10524c1414c8SBarry Smith         tmp0  = tmps[*vi--];
10534c1414c8SBarry Smith         sum1 -= *v1-- * tmp0;
10544c1414c8SBarry Smith         sum2 -= *v2-- * tmp0;
10554c1414c8SBarry Smith         sum3 -= *v3-- * tmp0;
10564c1414c8SBarry Smith       }
10574c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum1*a_a[ad[row]]; row--;
10584c1414c8SBarry Smith       sum2   -= *v2-- * tmp0;
10594c1414c8SBarry Smith       sum3   -= *v3-- * tmp0;
10604c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum2*a_a[ad[row]]; row--;
10614c1414c8SBarry Smith       sum3   -= *v3-- * tmp0;
10624c1414c8SBarry Smith       x[*c--] = tmp[row] = sum3*a_a[ad[row]]; row--;
10634c1414c8SBarry Smith 
10644c1414c8SBarry Smith       break;
10654c1414c8SBarry Smith     case 4 :
10664c1414c8SBarry Smith       sum1 = tmp[row];
10674c1414c8SBarry Smith       sum2 = tmp[row -1];
10684c1414c8SBarry Smith       sum3 = tmp[row -2];
10694c1414c8SBarry Smith       sum4 = tmp[row -3];
10704c1414c8SBarry Smith       v2   = aa + ai[row]-1;
10714c1414c8SBarry Smith       v3   = aa + ai[row -1]-1;
10724c1414c8SBarry Smith       v4   = aa + ai[row -2]-1;
10734c1414c8SBarry Smith 
10744c1414c8SBarry Smith       for (j=nz ; j>1; j-=2){
10754c1414c8SBarry Smith         vi  -=2;
10764c1414c8SBarry Smith         i0   = vi[2];
10774c1414c8SBarry Smith         i1   = vi[1];
10784c1414c8SBarry Smith         tmp0 = tmps[i0];
10794c1414c8SBarry Smith         tmp1 = tmps[i1];
10804c1414c8SBarry Smith         v1  -= 2;
10814c1414c8SBarry Smith         v2  -= 2;
10824c1414c8SBarry Smith         v3  -= 2;
10834c1414c8SBarry Smith         v4  -= 2;
10844c1414c8SBarry Smith         sum1 -= v1[2] * tmp0 + v1[1] * tmp1;
10854c1414c8SBarry Smith         sum2 -= v2[2] * tmp0 + v2[1] * tmp1;
10864c1414c8SBarry Smith         sum3 -= v3[2] * tmp0 + v3[1] * tmp1;
10874c1414c8SBarry Smith         sum4 -= v4[2] * tmp0 + v4[1] * tmp1;
10884c1414c8SBarry Smith       }
10894c1414c8SBarry Smith       if (j==1){
10904c1414c8SBarry Smith         tmp0  = tmps[*vi--];
10914c1414c8SBarry Smith         sum1 -= *v1-- * tmp0;
10924c1414c8SBarry Smith         sum2 -= *v2-- * tmp0;
10934c1414c8SBarry Smith         sum3 -= *v3-- * tmp0;
10944c1414c8SBarry Smith         sum4 -= *v4-- * tmp0;
10954c1414c8SBarry Smith       }
10964c1414c8SBarry Smith 
10974c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum1*a_a[ad[row]]; row--;
10984c1414c8SBarry Smith       sum2   -= *v2-- * tmp0;
10994c1414c8SBarry Smith       sum3   -= *v3-- * tmp0;
11004c1414c8SBarry Smith       sum4   -= *v4-- * tmp0;
11014c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum2*a_a[ad[row]]; row--;
11024c1414c8SBarry Smith       sum3   -= *v3-- * tmp0;
11034c1414c8SBarry Smith       sum4   -= *v4-- * tmp0;
11044c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum3*a_a[ad[row]]; row--;
11054c1414c8SBarry Smith       sum4   -= *v4-- * tmp0;
11064c1414c8SBarry Smith       x[*c--] = tmp[row] = sum4*a_a[ad[row]]; row--;
11074c1414c8SBarry Smith       break;
11084c1414c8SBarry Smith     case 5 :
11094c1414c8SBarry Smith       sum1 = tmp[row];
11104c1414c8SBarry Smith       sum2 = tmp[row -1];
11114c1414c8SBarry Smith       sum3 = tmp[row -2];
11124c1414c8SBarry Smith       sum4 = tmp[row -3];
11134c1414c8SBarry Smith       sum5 = tmp[row -4];
11144c1414c8SBarry Smith       v2   = aa + ai[row]-1;
11154c1414c8SBarry Smith       v3   = aa + ai[row -1]-1;
11164c1414c8SBarry Smith       v4   = aa + ai[row -2]-1;
11174c1414c8SBarry Smith       v5   = aa + ai[row -3]-1;
11184c1414c8SBarry Smith       for (j=nz ; j>1; j-=2){
11194c1414c8SBarry Smith         vi  -= 2;
11204c1414c8SBarry Smith         i0   = vi[2];
11214c1414c8SBarry Smith         i1   = vi[1];
11224c1414c8SBarry Smith         tmp0 = tmps[i0];
11234c1414c8SBarry Smith         tmp1 = tmps[i1];
11244c1414c8SBarry Smith         v1   -= 2;
11254c1414c8SBarry Smith         v2   -= 2;
11264c1414c8SBarry Smith         v3   -= 2;
11274c1414c8SBarry Smith         v4   -= 2;
11284c1414c8SBarry Smith         v5   -= 2;
11294c1414c8SBarry Smith         sum1 -= v1[2] * tmp0 + v1[1] * tmp1;
11304c1414c8SBarry Smith         sum2 -= v2[2] * tmp0 + v2[1] * tmp1;
11314c1414c8SBarry Smith         sum3 -= v3[2] * tmp0 + v3[1] * tmp1;
11324c1414c8SBarry Smith         sum4 -= v4[2] * tmp0 + v4[1] * tmp1;
11334c1414c8SBarry Smith         sum5 -= v5[2] * tmp0 + v5[1] * tmp1;
11344c1414c8SBarry Smith       }
11354c1414c8SBarry Smith       if (j==1){
11364c1414c8SBarry Smith         tmp0  = tmps[*vi--];
11374c1414c8SBarry Smith         sum1 -= *v1-- * tmp0;
11384c1414c8SBarry Smith         sum2 -= *v2-- * tmp0;
11394c1414c8SBarry Smith         sum3 -= *v3-- * tmp0;
11404c1414c8SBarry Smith         sum4 -= *v4-- * tmp0;
11414c1414c8SBarry Smith         sum5 -= *v5-- * tmp0;
11424c1414c8SBarry Smith       }
11434c1414c8SBarry Smith 
11444c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum1*a_a[ad[row]]; row--;
11454c1414c8SBarry Smith       sum2   -= *v2-- * tmp0;
11464c1414c8SBarry Smith       sum3   -= *v3-- * tmp0;
11474c1414c8SBarry Smith       sum4   -= *v4-- * tmp0;
11484c1414c8SBarry Smith       sum5   -= *v5-- * tmp0;
11494c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum2*a_a[ad[row]]; row--;
11504c1414c8SBarry Smith       sum3   -= *v3-- * tmp0;
11514c1414c8SBarry Smith       sum4   -= *v4-- * tmp0;
11524c1414c8SBarry Smith       sum5   -= *v5-- * tmp0;
11534c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum3*a_a[ad[row]]; row--;
11544c1414c8SBarry Smith       sum4   -= *v4-- * tmp0;
11554c1414c8SBarry Smith       sum5   -= *v5-- * tmp0;
11564c1414c8SBarry Smith       tmp0    = x[*c--] = tmp[row] = sum4*a_a[ad[row]]; row--;
11574c1414c8SBarry Smith       sum5   -= *v5-- * tmp0;
11584c1414c8SBarry Smith       x[*c--] = tmp[row] = sum5*a_a[ad[row]]; row--;
11594c1414c8SBarry Smith       break;
11604c1414c8SBarry Smith     default:
11614c1414c8SBarry Smith       SETERRQ(PETSC_ERR_COR,"Node size not yet supported \n");
11624c1414c8SBarry Smith     }
11634c1414c8SBarry Smith   }
11644c1414c8SBarry Smith   ierr = ISRestoreIndices(isrow,&rout);CHKERRQ(ierr);
11654c1414c8SBarry Smith   ierr = ISRestoreIndices(iscol,&cout);CHKERRQ(ierr);
1166d9fead3dSBarry Smith   ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
11674c1414c8SBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
1168d0f46423SBarry Smith   ierr = PetscLogFlops(2*a->nz - A->cmap->n);CHKERRQ(ierr);
11694c1414c8SBarry Smith   PetscFunctionReturn(0);
11704c1414c8SBarry Smith }
11714c1414c8SBarry Smith 
11724c1414c8SBarry Smith #undef __FUNCT__
11734c1414c8SBarry Smith #define __FUNCT__ "MatLUFactorNumeric_Inode"
11740481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_Inode(Mat B,Mat A,const MatFactorInfo *info)
11754c1414c8SBarry Smith {
1176719d5645SBarry Smith   Mat               C = B;
11774c1414c8SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)C->data;
11784c1414c8SBarry Smith   IS                iscol = b->col,isrow = b->row,isicol = b->icol;
11794c1414c8SBarry Smith   PetscErrorCode    ierr;
11805d0c19d7SBarry Smith   const PetscInt    *r,*ic,*c,*ics;
11815d0c19d7SBarry Smith   PetscInt          n = A->rmap->n,*bi = b->i;
11824c1414c8SBarry Smith   PetscInt          *bj = b->j,*nbj=b->j +1,*ajtmp,*bjtmp,nz,nz_tmp,row,prow;
11835d0c19d7SBarry Smith   PetscInt          i,j,idx,*ai = a->i,*aj = a->j,*bd = b->diag,node_max,nodesz;
11844c1414c8SBarry Smith   PetscInt          *ns,*tmp_vec1,*tmp_vec2,*nsmap,*pj;
1185dd6ea824SBarry Smith   PetscScalar       mul1,mul2,mul3,tmp;
1186dd6ea824SBarry Smith   MatScalar         *pc1,*pc2,*pc3,*ba = b->a,*pv,*rtmp11,*rtmp22,*rtmp33;
1187dd6ea824SBarry Smith   const MatScalar   *v1,*v2,*v3,*aa = a->a,*rtmp1;
11884c1414c8SBarry Smith   PetscReal         rs=0.0;
11894c1414c8SBarry Smith   LUShift_Ctx       sctx;
11904c1414c8SBarry Smith   PetscInt          newshift;
11914c1414c8SBarry Smith 
11924c1414c8SBarry Smith   PetscFunctionBegin;
11934c1414c8SBarry Smith   sctx.shift_top      = 0;
11944c1414c8SBarry Smith   sctx.nshift_max     = 0;
11954c1414c8SBarry Smith   sctx.shift_lo       = 0;
11964c1414c8SBarry Smith   sctx.shift_hi       = 0;
11970481f469SBarry Smith   sctx.shift_fraction = 0;
11984c1414c8SBarry Smith 
11994c1414c8SBarry Smith   /* if both shift schemes are chosen by user, only use info->shiftpd */
12004c1414c8SBarry Smith   if (info->shiftpd) { /* set sctx.shift_top=max{rs} */
12014c1414c8SBarry Smith     sctx.shift_top = 0;
12024c1414c8SBarry Smith     for (i=0; i<n; i++) {
12034c1414c8SBarry Smith       /* calculate rs = sum(|aij|)-RealPart(aii), amt of shift needed for this row */
12044c1414c8SBarry Smith       rs    = 0.0;
12054c1414c8SBarry Smith       ajtmp = aj + ai[i];
12064c1414c8SBarry Smith       rtmp1 = aa + ai[i];
12074c1414c8SBarry Smith       nz = ai[i+1] - ai[i];
12084c1414c8SBarry Smith       for (j=0; j<nz; j++){
12094c1414c8SBarry Smith         if (*ajtmp != i){
12104c1414c8SBarry Smith           rs += PetscAbsScalar(*rtmp1++);
12114c1414c8SBarry Smith         } else {
12124c1414c8SBarry Smith           rs -= PetscRealPart(*rtmp1++);
12134c1414c8SBarry Smith         }
12144c1414c8SBarry Smith         ajtmp++;
12154c1414c8SBarry Smith       }
12164c1414c8SBarry Smith       if (rs>sctx.shift_top) sctx.shift_top = rs;
12174c1414c8SBarry Smith     }
12184c1414c8SBarry Smith     if (sctx.shift_top == 0.0) sctx.shift_top += 1.e-12;
12194c1414c8SBarry Smith     sctx.shift_top *= 1.1;
12204c1414c8SBarry Smith     sctx.nshift_max = 5;
12214c1414c8SBarry Smith     sctx.shift_lo   = 0.;
12224c1414c8SBarry Smith     sctx.shift_hi   = 1.;
12234c1414c8SBarry Smith   }
12244c1414c8SBarry Smith   sctx.shift_amount = 0;
12254c1414c8SBarry Smith   sctx.nshift       = 0;
12264c1414c8SBarry Smith 
12274c1414c8SBarry Smith   ierr  = ISGetIndices(isrow,&r);CHKERRQ(ierr);
12284c1414c8SBarry Smith   ierr  = ISGetIndices(iscol,&c);CHKERRQ(ierr);
12294c1414c8SBarry Smith   ierr  = ISGetIndices(isicol,&ic);CHKERRQ(ierr);
1230d9fead3dSBarry Smith   ierr  = PetscMalloc((3*n+1)*sizeof(PetscScalar),&rtmp11);CHKERRQ(ierr);
1231d9fead3dSBarry Smith   ierr  = PetscMemzero(rtmp11,(3*n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
12324c1414c8SBarry Smith   ics   = ic ;
1233d9fead3dSBarry Smith   rtmp22 = rtmp11 + n;
1234d9fead3dSBarry Smith   rtmp33 = rtmp22 + n;
12354c1414c8SBarry Smith 
12364c1414c8SBarry Smith   node_max = a->inode.node_count;
12374c1414c8SBarry Smith   ns       = a->inode.size;
12384c1414c8SBarry Smith   if (!ns){
12394c1414c8SBarry Smith     SETERRQ(PETSC_ERR_PLIB,"Matrix without inode information");
12404c1414c8SBarry Smith   }
12414c1414c8SBarry Smith 
12424c1414c8SBarry Smith   /* If max inode size > 3, split it into two inodes.*/
12434c1414c8SBarry Smith   /* also map the inode sizes according to the ordering */
12444c1414c8SBarry Smith   ierr = PetscMalloc((n+1)* sizeof(PetscInt),&tmp_vec1);CHKERRQ(ierr);
12454c1414c8SBarry Smith   for (i=0,j=0; i<node_max; ++i,++j){
12464c1414c8SBarry Smith     if (ns[i]>3) {
12474c1414c8SBarry Smith       tmp_vec1[j] = ns[i]/2; /* Assuming ns[i] < =5  */
12484c1414c8SBarry Smith       ++j;
12494c1414c8SBarry Smith       tmp_vec1[j] = ns[i] - tmp_vec1[j-1];
12504c1414c8SBarry Smith     } else {
12514c1414c8SBarry Smith       tmp_vec1[j] = ns[i];
12524c1414c8SBarry Smith     }
12534c1414c8SBarry Smith   }
12544c1414c8SBarry Smith   /* Use the correct node_max */
12554c1414c8SBarry Smith   node_max = j;
12564c1414c8SBarry Smith 
12574c1414c8SBarry Smith   /* Now reorder the inode info based on mat re-ordering info */
12584c1414c8SBarry Smith   /* First create a row -> inode_size_array_index map */
12594c1414c8SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt)+1,&nsmap);CHKERRQ(ierr);
12604c1414c8SBarry Smith   ierr = PetscMalloc(node_max*sizeof(PetscInt)+1,&tmp_vec2);CHKERRQ(ierr);
12614c1414c8SBarry Smith   for (i=0,row=0; i<node_max; i++) {
12624c1414c8SBarry Smith     nodesz = tmp_vec1[i];
12634c1414c8SBarry Smith     for (j=0; j<nodesz; j++,row++) {
12644c1414c8SBarry Smith       nsmap[row] = i;
12654c1414c8SBarry Smith     }
12664c1414c8SBarry Smith   }
12674c1414c8SBarry Smith   /* Using nsmap, create a reordered ns structure */
12684c1414c8SBarry Smith   for (i=0,j=0; i< node_max; i++) {
12694c1414c8SBarry Smith     nodesz       = tmp_vec1[nsmap[r[j]]];    /* here the reordered row_no is in r[] */
12704c1414c8SBarry Smith     tmp_vec2[i]  = nodesz;
12714c1414c8SBarry Smith     j           += nodesz;
12724c1414c8SBarry Smith   }
12734c1414c8SBarry Smith   ierr = PetscFree(nsmap);CHKERRQ(ierr);
12744c1414c8SBarry Smith   ierr = PetscFree(tmp_vec1);CHKERRQ(ierr);
12754c1414c8SBarry Smith   /* Now use the correct ns */
12764c1414c8SBarry Smith   ns = tmp_vec2;
12774c1414c8SBarry Smith 
12784c1414c8SBarry Smith   do {
12794c1414c8SBarry Smith     sctx.lushift = PETSC_FALSE;
12804c1414c8SBarry Smith     /* Now loop over each block-row, and do the factorization */
12814c1414c8SBarry Smith     for (i=0,row=0; i<node_max; i++) {
12824c1414c8SBarry Smith       nodesz = ns[i];
12834c1414c8SBarry Smith       nz     = bi[row+1] - bi[row];
12844c1414c8SBarry Smith       bjtmp  = bj + bi[row];
12854c1414c8SBarry Smith 
12864c1414c8SBarry Smith       switch (nodesz){
12874c1414c8SBarry Smith       case 1:
12884c1414c8SBarry Smith         for  (j=0; j<nz; j++){
12894c1414c8SBarry Smith           idx        = bjtmp[j];
1290d9fead3dSBarry Smith           rtmp11[idx] = 0.0;
12914c1414c8SBarry Smith         }
12924c1414c8SBarry Smith 
12934c1414c8SBarry Smith         /* load in initial (unfactored row) */
12944c1414c8SBarry Smith         idx    = r[row];
12954c1414c8SBarry Smith         nz_tmp = ai[idx+1] - ai[idx];
12964c1414c8SBarry Smith         ajtmp  = aj + ai[idx];
12974c1414c8SBarry Smith         v1     = aa + ai[idx];
12984c1414c8SBarry Smith 
12994c1414c8SBarry Smith         for (j=0; j<nz_tmp; j++) {
13004c1414c8SBarry Smith           idx        = ics[ajtmp[j]];
1301d9fead3dSBarry Smith           rtmp11[idx] = v1[j];
13024c1414c8SBarry Smith         }
1303d9fead3dSBarry Smith         rtmp11[ics[r[row]]] += sctx.shift_amount;
13044c1414c8SBarry Smith 
13054c1414c8SBarry Smith         prow = *bjtmp++ ;
13064c1414c8SBarry Smith         while (prow < row) {
1307d9fead3dSBarry Smith           pc1 = rtmp11 + prow;
13084c1414c8SBarry Smith           if (*pc1 != 0.0){
13094c1414c8SBarry Smith             pv   = ba + bd[prow];
13104c1414c8SBarry Smith             pj   = nbj + bd[prow];
13114c1414c8SBarry Smith             mul1 = *pc1 * *pv++;
13124c1414c8SBarry Smith             *pc1 = mul1;
13134c1414c8SBarry Smith             nz_tmp = bi[prow+1] - bd[prow] - 1;
13144c1414c8SBarry Smith             ierr = PetscLogFlops(2*nz_tmp);CHKERRQ(ierr);
13154c1414c8SBarry Smith             for (j=0; j<nz_tmp; j++) {
13164c1414c8SBarry Smith               tmp = pv[j];
13174c1414c8SBarry Smith               idx = pj[j];
1318d9fead3dSBarry Smith               rtmp11[idx] -= mul1 * tmp;
13194c1414c8SBarry Smith             }
13204c1414c8SBarry Smith           }
13214c1414c8SBarry Smith           prow = *bjtmp++ ;
13224c1414c8SBarry Smith         }
13234c1414c8SBarry Smith         pj  = bj + bi[row];
13244c1414c8SBarry Smith         pc1 = ba + bi[row];
13254c1414c8SBarry Smith 
1326d9fead3dSBarry Smith         sctx.pv    = rtmp11[row];
1327d9fead3dSBarry Smith         rtmp11[row] = 1.0/rtmp11[row]; /* invert diag */
13284c1414c8SBarry Smith         rs         = 0.0;
13294c1414c8SBarry Smith         for (j=0; j<nz; j++) {
13304c1414c8SBarry Smith           idx    = pj[j];
1331d9fead3dSBarry Smith           pc1[j] = rtmp11[idx]; /* rtmp11 -> ba */
13324c1414c8SBarry Smith           if (idx != row) rs += PetscAbsScalar(pc1[j]);
13334c1414c8SBarry Smith         }
13344c1414c8SBarry Smith         sctx.rs  = rs;
1335c6b1f410SHong Zhang         ierr = MatLUCheckShift_inline(info,sctx,row,newshift);CHKERRQ(ierr);
13364c1414c8SBarry Smith         if (newshift == 1) goto endofwhile;
13374c1414c8SBarry Smith         break;
13384c1414c8SBarry Smith 
13394c1414c8SBarry Smith       case 2:
13404c1414c8SBarry Smith         for (j=0; j<nz; j++) {
13414c1414c8SBarry Smith           idx        = bjtmp[j];
1342d9fead3dSBarry Smith           rtmp11[idx] = 0.0;
1343d9fead3dSBarry Smith           rtmp22[idx] = 0.0;
13444c1414c8SBarry Smith         }
13454c1414c8SBarry Smith 
13464c1414c8SBarry Smith         /* load in initial (unfactored row) */
13474c1414c8SBarry Smith         idx    = r[row];
13484c1414c8SBarry Smith         nz_tmp = ai[idx+1] - ai[idx];
13494c1414c8SBarry Smith         ajtmp  = aj + ai[idx];
13504c1414c8SBarry Smith         v1     = aa + ai[idx];
13514c1414c8SBarry Smith         v2     = aa + ai[idx+1];
13524c1414c8SBarry Smith         for (j=0; j<nz_tmp; j++) {
13534c1414c8SBarry Smith           idx        = ics[ajtmp[j]];
1354d9fead3dSBarry Smith           rtmp11[idx] = v1[j];
1355d9fead3dSBarry Smith           rtmp22[idx] = v2[j];
13564c1414c8SBarry Smith         }
1357d9fead3dSBarry Smith         rtmp11[ics[r[row]]]   += sctx.shift_amount;
1358d9fead3dSBarry Smith         rtmp22[ics[r[row+1]]] += sctx.shift_amount;
13594c1414c8SBarry Smith 
13604c1414c8SBarry Smith         prow = *bjtmp++ ;
13614c1414c8SBarry Smith         while (prow < row) {
1362d9fead3dSBarry Smith           pc1 = rtmp11 + prow;
1363d9fead3dSBarry Smith           pc2 = rtmp22 + prow;
13644c1414c8SBarry Smith           if (*pc1 != 0.0 || *pc2 != 0.0){
13654c1414c8SBarry Smith             pv   = ba + bd[prow];
13664c1414c8SBarry Smith             pj   = nbj + bd[prow];
13674c1414c8SBarry Smith             mul1 = *pc1 * *pv;
13684c1414c8SBarry Smith             mul2 = *pc2 * *pv;
13694c1414c8SBarry Smith             ++pv;
13704c1414c8SBarry Smith             *pc1 = mul1;
13714c1414c8SBarry Smith             *pc2 = mul2;
13724c1414c8SBarry Smith 
13734c1414c8SBarry Smith             nz_tmp = bi[prow+1] - bd[prow] - 1;
13744c1414c8SBarry Smith             for (j=0; j<nz_tmp; j++) {
13754c1414c8SBarry Smith               tmp = pv[j];
13764c1414c8SBarry Smith               idx = pj[j];
1377d9fead3dSBarry Smith               rtmp11[idx] -= mul1 * tmp;
1378d9fead3dSBarry Smith               rtmp22[idx] -= mul2 * tmp;
13794c1414c8SBarry Smith             }
13804c1414c8SBarry Smith             ierr = PetscLogFlops(4*nz_tmp);CHKERRQ(ierr);
13814c1414c8SBarry Smith           }
13824c1414c8SBarry Smith           prow = *bjtmp++ ;
13834c1414c8SBarry Smith         }
13844c1414c8SBarry Smith 
13854c1414c8SBarry Smith         /* Now take care of diagonal 2x2 block. Note: prow = row here */
1386d9fead3dSBarry Smith         pc1 = rtmp11 + prow;
1387d9fead3dSBarry Smith         pc2 = rtmp22 + prow;
13884c1414c8SBarry Smith 
13894c1414c8SBarry Smith         sctx.pv = *pc1;
13904c1414c8SBarry Smith         pj      = bj + bi[prow];
13914c1414c8SBarry Smith         rs      = 0.0;
13924c1414c8SBarry Smith         for (j=0; j<nz; j++){
13934c1414c8SBarry Smith           idx = pj[j];
1394d9fead3dSBarry Smith           if (idx != prow) rs += PetscAbsScalar(rtmp11[idx]);
13954c1414c8SBarry Smith         }
13964c1414c8SBarry Smith         sctx.rs = rs;
1397c6b1f410SHong Zhang         ierr = MatLUCheckShift_inline(info,sctx,row,newshift);CHKERRQ(ierr);
13984c1414c8SBarry Smith         if (newshift == 1) goto endofwhile;
13994c1414c8SBarry Smith 
14004c1414c8SBarry Smith         if (*pc2 != 0.0){
14014c1414c8SBarry Smith           pj     = nbj + bd[prow];
14024c1414c8SBarry Smith           mul2   = (*pc2)/(*pc1); /* since diag is not yet inverted.*/
14034c1414c8SBarry Smith           *pc2   = mul2;
14044c1414c8SBarry Smith           nz_tmp = bi[prow+1] - bd[prow] - 1;
14054c1414c8SBarry Smith           for (j=0; j<nz_tmp; j++) {
14064c1414c8SBarry Smith             idx = pj[j] ;
1407d9fead3dSBarry Smith             tmp = rtmp11[idx];
1408d9fead3dSBarry Smith             rtmp22[idx] -= mul2 * tmp;
14094c1414c8SBarry Smith           }
14104c1414c8SBarry Smith           ierr = PetscLogFlops(2*nz_tmp);CHKERRQ(ierr);
14114c1414c8SBarry Smith         }
14124c1414c8SBarry Smith 
14134c1414c8SBarry Smith         pj  = bj + bi[row];
14144c1414c8SBarry Smith         pc1 = ba + bi[row];
14154c1414c8SBarry Smith         pc2 = ba + bi[row+1];
14164c1414c8SBarry Smith 
1417d9fead3dSBarry Smith         sctx.pv = rtmp22[row+1];
14184c1414c8SBarry Smith         rs = 0.0;
1419d9fead3dSBarry Smith         rtmp11[row]   = 1.0/rtmp11[row];
1420d9fead3dSBarry Smith         rtmp22[row+1] = 1.0/rtmp22[row+1];
14214c1414c8SBarry Smith         /* copy row entries from dense representation to sparse */
14224c1414c8SBarry Smith         for (j=0; j<nz; j++) {
14234c1414c8SBarry Smith           idx    = pj[j];
1424d9fead3dSBarry Smith           pc1[j] = rtmp11[idx];
1425d9fead3dSBarry Smith           pc2[j] = rtmp22[idx];
14264c1414c8SBarry Smith           if (idx != row+1) rs += PetscAbsScalar(pc2[j]);
14274c1414c8SBarry Smith         }
14284c1414c8SBarry Smith         sctx.rs = rs;
1429c6b1f410SHong Zhang         ierr = MatLUCheckShift_inline(info,sctx,row+1,newshift);CHKERRQ(ierr);
14304c1414c8SBarry Smith         if (newshift == 1) goto endofwhile;
14314c1414c8SBarry Smith         break;
14324c1414c8SBarry Smith 
14334c1414c8SBarry Smith       case 3:
14344c1414c8SBarry Smith         for  (j=0; j<nz; j++) {
14354c1414c8SBarry Smith           idx        = bjtmp[j];
1436d9fead3dSBarry Smith           rtmp11[idx] = 0.0;
1437d9fead3dSBarry Smith           rtmp22[idx] = 0.0;
1438d9fead3dSBarry Smith           rtmp33[idx] = 0.0;
14394c1414c8SBarry Smith         }
14404c1414c8SBarry Smith         /* copy the nonzeros for the 3 rows from sparse representation to dense in rtmp*[] */
14414c1414c8SBarry Smith         idx    = r[row];
14424c1414c8SBarry Smith         nz_tmp = ai[idx+1] - ai[idx];
14434c1414c8SBarry Smith         ajtmp = aj + ai[idx];
14444c1414c8SBarry Smith         v1    = aa + ai[idx];
14454c1414c8SBarry Smith         v2    = aa + ai[idx+1];
14464c1414c8SBarry Smith         v3    = aa + ai[idx+2];
14474c1414c8SBarry Smith         for (j=0; j<nz_tmp; j++) {
14484c1414c8SBarry Smith           idx        = ics[ajtmp[j]];
1449d9fead3dSBarry Smith           rtmp11[idx] = v1[j];
1450d9fead3dSBarry Smith           rtmp22[idx] = v2[j];
1451d9fead3dSBarry Smith           rtmp33[idx] = v3[j];
14524c1414c8SBarry Smith         }
1453d9fead3dSBarry Smith         rtmp11[ics[r[row]]]   += sctx.shift_amount;
1454d9fead3dSBarry Smith         rtmp22[ics[r[row+1]]] += sctx.shift_amount;
1455d9fead3dSBarry Smith         rtmp33[ics[r[row+2]]] += sctx.shift_amount;
14564c1414c8SBarry Smith 
14574c1414c8SBarry Smith         /* loop over all pivot row blocks above this row block */
14584c1414c8SBarry Smith         prow = *bjtmp++ ;
14594c1414c8SBarry Smith         while (prow < row) {
1460d9fead3dSBarry Smith           pc1 = rtmp11 + prow;
1461d9fead3dSBarry Smith           pc2 = rtmp22 + prow;
1462d9fead3dSBarry Smith           pc3 = rtmp33 + prow;
14634c1414c8SBarry Smith           if (*pc1 != 0.0 || *pc2 != 0.0 || *pc3 !=0.0){
14644c1414c8SBarry Smith             pv   = ba  + bd[prow];
14654c1414c8SBarry Smith             pj   = nbj + bd[prow];
14664c1414c8SBarry Smith             mul1 = *pc1 * *pv;
14674c1414c8SBarry Smith             mul2 = *pc2 * *pv;
14684c1414c8SBarry Smith             mul3 = *pc3 * *pv;
14694c1414c8SBarry Smith             ++pv;
14704c1414c8SBarry Smith             *pc1 = mul1;
14714c1414c8SBarry Smith             *pc2 = mul2;
14724c1414c8SBarry Smith             *pc3 = mul3;
14734c1414c8SBarry Smith 
14744c1414c8SBarry Smith             nz_tmp = bi[prow+1] - bd[prow] - 1;
14754c1414c8SBarry Smith             /* update this row based on pivot row */
14764c1414c8SBarry Smith             for (j=0; j<nz_tmp; j++) {
14774c1414c8SBarry Smith               tmp = pv[j];
14784c1414c8SBarry Smith               idx = pj[j];
1479d9fead3dSBarry Smith               rtmp11[idx] -= mul1 * tmp;
1480d9fead3dSBarry Smith               rtmp22[idx] -= mul2 * tmp;
1481d9fead3dSBarry Smith               rtmp33[idx] -= mul3 * tmp;
14824c1414c8SBarry Smith             }
14834c1414c8SBarry Smith             ierr = PetscLogFlops(6*nz_tmp);CHKERRQ(ierr);
14844c1414c8SBarry Smith           }
14854c1414c8SBarry Smith           prow = *bjtmp++ ;
14864c1414c8SBarry Smith         }
14874c1414c8SBarry Smith 
14884c1414c8SBarry Smith         /* Now take care of diagonal 3x3 block in this set of rows */
14894c1414c8SBarry Smith         /* note: prow = row here */
1490d9fead3dSBarry Smith         pc1 = rtmp11 + prow;
1491d9fead3dSBarry Smith         pc2 = rtmp22 + prow;
1492d9fead3dSBarry Smith         pc3 = rtmp33 + prow;
14934c1414c8SBarry Smith 
14944c1414c8SBarry Smith         sctx.pv = *pc1;
14954c1414c8SBarry Smith         pj      = bj + bi[prow];
14964c1414c8SBarry Smith         rs      = 0.0;
14974c1414c8SBarry Smith         for (j=0; j<nz; j++){
14984c1414c8SBarry Smith           idx = pj[j];
1499d9fead3dSBarry Smith           if (idx != row) rs += PetscAbsScalar(rtmp11[idx]);
15004c1414c8SBarry Smith         }
15014c1414c8SBarry Smith         sctx.rs = rs;
1502c6b1f410SHong Zhang         ierr = MatLUCheckShift_inline(info,sctx,row,newshift);CHKERRQ(ierr);
15034c1414c8SBarry Smith         if (newshift == 1) goto endofwhile;
15044c1414c8SBarry Smith 
15054c1414c8SBarry Smith         if (*pc2 != 0.0 || *pc3 != 0.0){
15064c1414c8SBarry Smith           mul2 = (*pc2)/(*pc1);
15074c1414c8SBarry Smith           mul3 = (*pc3)/(*pc1);
15084c1414c8SBarry Smith           *pc2 = mul2;
15094c1414c8SBarry Smith           *pc3 = mul3;
15104c1414c8SBarry Smith           nz_tmp = bi[prow+1] - bd[prow] - 1;
15114c1414c8SBarry Smith           pj     = nbj + bd[prow];
15124c1414c8SBarry Smith           for (j=0; j<nz_tmp; j++) {
15134c1414c8SBarry Smith             idx = pj[j] ;
1514d9fead3dSBarry Smith             tmp = rtmp11[idx];
1515d9fead3dSBarry Smith             rtmp22[idx] -= mul2 * tmp;
1516d9fead3dSBarry Smith             rtmp33[idx] -= mul3 * tmp;
15174c1414c8SBarry Smith           }
15184c1414c8SBarry Smith           ierr = PetscLogFlops(4*nz_tmp);CHKERRQ(ierr);
15194c1414c8SBarry Smith         }
15204c1414c8SBarry Smith         ++prow;
15214c1414c8SBarry Smith 
1522d9fead3dSBarry Smith         pc2 = rtmp22 + prow;
1523d9fead3dSBarry Smith         pc3 = rtmp33 + prow;
15244c1414c8SBarry Smith         sctx.pv = *pc2;
15254c1414c8SBarry Smith         pj      = bj + bi[prow];
15264c1414c8SBarry Smith         rs      = 0.0;
15274c1414c8SBarry Smith         for (j=0; j<nz; j++){
15284c1414c8SBarry Smith           idx = pj[j];
1529d9fead3dSBarry Smith           if (idx != prow) rs += PetscAbsScalar(rtmp22[idx]);
15304c1414c8SBarry Smith         }
15314c1414c8SBarry Smith         sctx.rs = rs;
1532c6b1f410SHong Zhang         ierr = MatLUCheckShift_inline(info,sctx,row+1,newshift);CHKERRQ(ierr);
15334c1414c8SBarry Smith         if (newshift == 1) goto endofwhile;
15344c1414c8SBarry Smith 
15354c1414c8SBarry Smith         if (*pc3 != 0.0){
15364c1414c8SBarry Smith           mul3   = (*pc3)/(*pc2);
15374c1414c8SBarry Smith           *pc3   = mul3;
15384c1414c8SBarry Smith           pj     = nbj + bd[prow];
15394c1414c8SBarry Smith           nz_tmp = bi[prow+1] - bd[prow] - 1;
15404c1414c8SBarry Smith           for (j=0; j<nz_tmp; j++) {
15414c1414c8SBarry Smith             idx = pj[j] ;
1542d9fead3dSBarry Smith             tmp = rtmp22[idx];
1543d9fead3dSBarry Smith             rtmp33[idx] -= mul3 * tmp;
15444c1414c8SBarry Smith           }
15454c1414c8SBarry Smith           ierr = PetscLogFlops(4*nz_tmp);CHKERRQ(ierr);
15464c1414c8SBarry Smith         }
15474c1414c8SBarry Smith 
15484c1414c8SBarry Smith         pj  = bj + bi[row];
15494c1414c8SBarry Smith         pc1 = ba + bi[row];
15504c1414c8SBarry Smith         pc2 = ba + bi[row+1];
15514c1414c8SBarry Smith         pc3 = ba + bi[row+2];
15524c1414c8SBarry Smith 
1553d9fead3dSBarry Smith         sctx.pv = rtmp33[row+2];
15544c1414c8SBarry Smith         rs = 0.0;
1555d9fead3dSBarry Smith         rtmp11[row]   = 1.0/rtmp11[row];
1556d9fead3dSBarry Smith         rtmp22[row+1] = 1.0/rtmp22[row+1];
1557d9fead3dSBarry Smith         rtmp33[row+2] = 1.0/rtmp33[row+2];
15584c1414c8SBarry Smith         /* copy row entries from dense representation to sparse */
15594c1414c8SBarry Smith         for (j=0; j<nz; j++) {
15604c1414c8SBarry Smith           idx    = pj[j];
1561d9fead3dSBarry Smith           pc1[j] = rtmp11[idx];
1562d9fead3dSBarry Smith           pc2[j] = rtmp22[idx];
1563d9fead3dSBarry Smith           pc3[j] = rtmp33[idx];
15644c1414c8SBarry Smith           if (idx != row+2) rs += PetscAbsScalar(pc3[j]);
15654c1414c8SBarry Smith         }
15664c1414c8SBarry Smith 
15674c1414c8SBarry Smith         sctx.rs = rs;
1568c6b1f410SHong Zhang         ierr = MatLUCheckShift_inline(info,sctx,row+2,newshift);CHKERRQ(ierr);
15694c1414c8SBarry Smith         if (newshift == 1) goto endofwhile;
15704c1414c8SBarry Smith         break;
15714c1414c8SBarry Smith 
15724c1414c8SBarry Smith       default:
15734c1414c8SBarry Smith         SETERRQ(PETSC_ERR_COR,"Node size not yet supported \n");
15744c1414c8SBarry Smith       }
15754c1414c8SBarry Smith       row += nodesz;                 /* Update the row */
15764c1414c8SBarry Smith     }
15774c1414c8SBarry Smith     endofwhile:;
15784c1414c8SBarry Smith   } while (sctx.lushift);
1579d9fead3dSBarry Smith   ierr = PetscFree(rtmp11);CHKERRQ(ierr);
15804c1414c8SBarry Smith   ierr = PetscFree(tmp_vec2);CHKERRQ(ierr);
15814c1414c8SBarry Smith   ierr = ISRestoreIndices(isicol,&ic);CHKERRQ(ierr);
15824c1414c8SBarry Smith   ierr = ISRestoreIndices(isrow,&r);CHKERRQ(ierr);
15834c1414c8SBarry Smith   ierr = ISRestoreIndices(iscol,&c);CHKERRQ(ierr);
1584719d5645SBarry Smith   (B)->ops->solve           = MatSolve_Inode;
1585db4efbfdSBarry Smith   /* do not set solve add, since MatSolve_Inode + Add is faster */
1586db4efbfdSBarry Smith   C->ops->solvetranspose     = MatSolveTranspose_SeqAIJ;
1587db4efbfdSBarry Smith   C->ops->solvetransposeadd  = MatSolveTransposeAdd_SeqAIJ;
15884c1414c8SBarry Smith   C->assembled   = PETSC_TRUE;
15895c9eb25fSBarry Smith   C->preallocated = PETSC_TRUE;
15904c1414c8SBarry Smith   if (sctx.nshift) {
1591e738e422SBarry Smith     if (info->shiftpd) {
15920481f469SBarry Smith       ierr = PetscInfo4(A,"number of shift_pd tries %D, shift_amount %G, diagonal shifted up by %e fraction top_value %e\n",sctx.nshift,sctx.shift_amount,sctx.shift_fraction,sctx.shift_top);CHKERRQ(ierr);
1593e738e422SBarry Smith     } else if (info->shiftnz) {
1594e738e422SBarry Smith       ierr = PetscInfo2(A,"number of shift_nz tries %D, shift_amount %G\n",sctx.nshift,sctx.shift_amount);CHKERRQ(ierr);
15954c1414c8SBarry Smith     }
15964c1414c8SBarry Smith   }
1597d0f46423SBarry Smith   ierr = PetscLogFlops(C->cmap->n);CHKERRQ(ierr);
15984c1414c8SBarry Smith   PetscFunctionReturn(0);
15994c1414c8SBarry Smith }
16004c1414c8SBarry Smith 
16014c1414c8SBarry Smith /*
16024c1414c8SBarry Smith      Makes a longer coloring[] array and calls the usual code with that
16034c1414c8SBarry Smith */
16044c1414c8SBarry Smith #undef __FUNCT__
16054c1414c8SBarry Smith #define __FUNCT__ "MatColoringPatch_Inode"
16064c1414c8SBarry Smith PetscErrorCode MatColoringPatch_Inode(Mat mat,PetscInt ncolors,PetscInt nin,ISColoringValue coloring[],ISColoring *iscoloring)
16074c1414c8SBarry Smith {
16084c1414c8SBarry Smith   Mat_SeqAIJ       *a = (Mat_SeqAIJ*)mat->data;
16094c1414c8SBarry Smith   PetscErrorCode  ierr;
1610d0f46423SBarry Smith   PetscInt        n = mat->cmap->n,m = a->inode.node_count,j,*ns = a->inode.size,row;
16114c1414c8SBarry Smith   PetscInt        *colorused,i;
16124c1414c8SBarry Smith   ISColoringValue *newcolor;
16134c1414c8SBarry Smith 
16144c1414c8SBarry Smith   PetscFunctionBegin;
16154c1414c8SBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&newcolor);CHKERRQ(ierr);
16164c1414c8SBarry Smith   /* loop over inodes, marking a color for each column*/
16174c1414c8SBarry Smith   row = 0;
16184c1414c8SBarry Smith   for (i=0; i<m; i++){
16194c1414c8SBarry Smith     for (j=0; j<ns[i]; j++) {
16204c1414c8SBarry Smith       newcolor[row++] = coloring[i] + j*ncolors;
16214c1414c8SBarry Smith     }
16224c1414c8SBarry Smith   }
16234c1414c8SBarry Smith 
16244c1414c8SBarry Smith   /* eliminate unneeded colors */
16254c1414c8SBarry Smith   ierr = PetscMalloc(5*ncolors*sizeof(PetscInt),&colorused);CHKERRQ(ierr);
16264c1414c8SBarry Smith   ierr = PetscMemzero(colorused,5*ncolors*sizeof(PetscInt));CHKERRQ(ierr);
16274c1414c8SBarry Smith   for (i=0; i<n; i++) {
16284c1414c8SBarry Smith     colorused[newcolor[i]] = 1;
16294c1414c8SBarry Smith   }
16304c1414c8SBarry Smith 
16314c1414c8SBarry Smith   for (i=1; i<5*ncolors; i++) {
16324c1414c8SBarry Smith     colorused[i] += colorused[i-1];
16334c1414c8SBarry Smith   }
16344c1414c8SBarry Smith   ncolors = colorused[5*ncolors-1];
16354c1414c8SBarry Smith   for (i=0; i<n; i++) {
163657815681SSatish Balay     newcolor[i] = colorused[newcolor[i]]-1;
16374c1414c8SBarry Smith   }
16384c1414c8SBarry Smith   ierr = PetscFree(colorused);CHKERRQ(ierr);
16397adad957SLisandro Dalcin   ierr = ISColoringCreate(((PetscObject)mat)->comm,ncolors,n,newcolor,iscoloring);CHKERRQ(ierr);
16404c1414c8SBarry Smith   ierr = PetscFree(coloring);CHKERRQ(ierr);
16414c1414c8SBarry Smith   PetscFunctionReturn(0);
16424c1414c8SBarry Smith }
16434c1414c8SBarry Smith 
16447c4f633dSBarry Smith #include "../src/inline/ilu.h"
16452af78befSBarry Smith 
16462af78befSBarry Smith #undef __FUNCT__
16472af78befSBarry Smith #define __FUNCT__ "MatRelax_Inode"
16482af78befSBarry Smith PetscErrorCode MatRelax_Inode(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
16492af78befSBarry Smith {
16502af78befSBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1651a77337e4SBarry Smith   PetscScalar        *x,*xs,sum1,sum2,sum3,sum4,sum5,tmp0,tmp1,tmp2,tmp3;
1652a77337e4SBarry Smith   MatScalar          *ibdiag,*bdiag;
1653d9fead3dSBarry Smith   PetscScalar        *b,*xb,tmp4,tmp5,x1,x2,x3,x4,x5;
1654dd6ea824SBarry Smith   const MatScalar    *v = a->a,*v1,*v2,*v3,*v4,*v5;
165562bba022SBarry Smith   PetscReal          zeropivot = 1.0e-15, shift = 0.0;
16562af78befSBarry Smith   PetscErrorCode     ierr;
1657f0d39aaaSBarry Smith   PetscInt           n,m = a->inode.node_count,*sizes = a->inode.size,cnt = 0,i,j,row,i1,i2;
1658f0d39aaaSBarry Smith   PetscInt           *idx,*diag = a->diag,*ii = a->i,sz,k;
16592af78befSBarry Smith 
16602af78befSBarry Smith   PetscFunctionBegin;
166171f1c65dSBarry Smith   if (omega != 1.0) SETERRQ(PETSC_ERR_SUP,"No support for omega != 1.0; use -mat_no_inode");
166271f1c65dSBarry Smith   if (fshift != 0.0) SETERRQ(PETSC_ERR_SUP,"No support for fshift != 0.0; use -mat_no_inode");
166371f1c65dSBarry Smith   if (flag & SOR_EISENSTAT) SETERRQ(PETSC_ERR_SUP,"No support for Eisenstat trick; use -mat_no_inode");
1664*3f6d85c9SBarry Smith   if (its > 1) {
1665*3f6d85c9SBarry Smith     /* switch to non-inode version */
1666*3f6d85c9SBarry Smith     ierr = MatRelax_SeqAIJ(A,bb,omega,flag,fshift,its,lits,xx);CHKERRQ(ierr);
1667*3f6d85c9SBarry Smith     PetscFunctionReturn(0);
1668*3f6d85c9SBarry Smith   }
16692af78befSBarry Smith 
167071f1c65dSBarry Smith   if (!a->inode.ibdiagvalid) {
16712af78befSBarry Smith     if (!a->inode.ibdiag) {
16722af78befSBarry Smith       /* calculate space needed for diagonal blocks */
16732af78befSBarry Smith       for (i=0; i<m; i++) {
16742af78befSBarry Smith 	cnt += sizes[i]*sizes[i];
16752af78befSBarry Smith       }
1676f0d39aaaSBarry Smith       a->inode.bdiagsize = cnt;
1677a77337e4SBarry Smith       ierr   = PetscMalloc2(cnt,MatScalar,&a->inode.ibdiag,cnt,MatScalar,&a->inode.bdiag);CHKERRQ(ierr);
167871f1c65dSBarry Smith     }
167971f1c65dSBarry Smith 
168071f1c65dSBarry Smith     /* copy over the diagonal blocks and invert them */
16812af78befSBarry Smith     ibdiag = a->inode.ibdiag;
16822af78befSBarry Smith     bdiag  = a->inode.bdiag;
16832af78befSBarry Smith     cnt = 0;
16842af78befSBarry Smith     for (i=0, row = 0; i<m; i++) {
16852af78befSBarry Smith       for (j=0; j<sizes[i]; j++) {
1686f0d39aaaSBarry Smith         for (k=0; k<sizes[i]; k++) {
1687f0d39aaaSBarry Smith           bdiag[cnt+k*sizes[i]+j] = v[diag[row+j] - j + k];
1688f0d39aaaSBarry Smith         }
16892af78befSBarry Smith       }
1690a77337e4SBarry Smith       ierr = PetscMemcpy(ibdiag+cnt,bdiag+cnt,sizes[i]*sizes[i]*sizeof(MatScalar));CHKERRQ(ierr);
16912af78befSBarry Smith 
16922af78befSBarry Smith       switch(sizes[i]) {
16932af78befSBarry Smith         case 1:
16942af78befSBarry Smith           /* Create matrix data structure */
169564c62002SMatthew Knepley           if (PetscAbsScalar(ibdiag[cnt]) < zeropivot) SETERRQ1(PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot on row %D",row);
169664c62002SMatthew Knepley           ibdiag[cnt] = 1.0/ibdiag[cnt];
16972af78befSBarry Smith           break;
16982af78befSBarry Smith         case 2:
169962bba022SBarry Smith           ierr = Kernel_A_gets_inverse_A_2(ibdiag+cnt,shift);CHKERRQ(ierr);
17002af78befSBarry Smith           break;
17012af78befSBarry Smith         case 3:
170262bba022SBarry Smith           ierr = Kernel_A_gets_inverse_A_3(ibdiag+cnt,shift);CHKERRQ(ierr);
17032af78befSBarry Smith           break;
17042af78befSBarry Smith         case 4:
170562bba022SBarry Smith           ierr = Kernel_A_gets_inverse_A_4(ibdiag+cnt,shift);CHKERRQ(ierr);
17062af78befSBarry Smith           break;
17072af78befSBarry Smith         case 5:
170862bba022SBarry Smith           ierr = Kernel_A_gets_inverse_A_5(ibdiag+cnt,shift);CHKERRQ(ierr);
17092af78befSBarry Smith           break;
17102af78befSBarry Smith        default:
17112af78befSBarry Smith 	 SETERRQ1(PETSC_ERR_SUP,"Inode size %D not supported",sizes[i]);
17122af78befSBarry Smith       }
17132af78befSBarry Smith       cnt += sizes[i]*sizes[i];
17142af78befSBarry Smith       row += sizes[i];
17152af78befSBarry Smith     }
171671f1c65dSBarry Smith     a->inode.ibdiagvalid = PETSC_TRUE;
17172af78befSBarry Smith   }
17182af78befSBarry Smith   ibdiag = a->inode.ibdiag;
17192af78befSBarry Smith   bdiag  = a->inode.bdiag;
17202af78befSBarry Smith 
17212af78befSBarry Smith   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
17222af78befSBarry Smith   if (xx != bb) {
17232af78befSBarry Smith     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
17242af78befSBarry Smith   } else {
17252af78befSBarry Smith     b = x;
17262af78befSBarry Smith   }
17272af78befSBarry Smith 
17282af78befSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
17292af78befSBarry Smith   xs   = x;
17302af78befSBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
17312af78befSBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
17322af78befSBarry Smith 
17338862d2efSBarry Smith       for (i=0, row=0; i<m; i++) {
17348862d2efSBarry Smith         sz  = diag[row] - ii[row];
17358862d2efSBarry Smith         v1  = a->a + ii[row];
17368862d2efSBarry Smith         idx = a->j + ii[row];
17378862d2efSBarry Smith 
17388862d2efSBarry Smith         /* see comments for MatMult_Inode() for how this is coded */
17398862d2efSBarry Smith         switch (sizes[i]){
17408862d2efSBarry Smith           case 1:
17418862d2efSBarry Smith 
17428862d2efSBarry Smith             sum1  = b[row];
17438862d2efSBarry Smith             for(n = 0; n<sz-1; n+=2) {
17448862d2efSBarry Smith               i1   = idx[0];
17458862d2efSBarry Smith               i2   = idx[1];
17468862d2efSBarry Smith               idx += 2;
17478862d2efSBarry Smith               tmp0 = x[i1];
17488862d2efSBarry Smith               tmp1 = x[i2];
17498862d2efSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
17508862d2efSBarry Smith             }
17518862d2efSBarry Smith 
17528862d2efSBarry Smith             if (n == sz-1){
1753f0d39aaaSBarry Smith               tmp0  = x[*idx];
1754f0d39aaaSBarry Smith               sum1 -= *v1 * tmp0;
17558862d2efSBarry Smith             }
17568862d2efSBarry Smith             x[row++] = sum1*(*ibdiag++);
17578862d2efSBarry Smith             break;
1758f0d39aaaSBarry Smith           case 2:
1759f0d39aaaSBarry Smith             v2    = a->a + ii[row+1];
1760f0d39aaaSBarry Smith             sum1  = b[row];
1761f0d39aaaSBarry Smith             sum2  = b[row+1];
1762f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
1763f0d39aaaSBarry Smith               i1   = idx[0];
1764f0d39aaaSBarry Smith               i2   = idx[1];
1765f0d39aaaSBarry Smith               idx += 2;
1766f0d39aaaSBarry Smith               tmp0 = x[i1];
1767f0d39aaaSBarry Smith               tmp1 = x[i2];
1768f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
1769f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
1770f0d39aaaSBarry Smith             }
1771f0d39aaaSBarry Smith 
1772f0d39aaaSBarry Smith             if (n == sz-1){
1773f0d39aaaSBarry Smith               tmp0  = x[*idx];
1774f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0;
1775f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0;
1776f0d39aaaSBarry Smith             }
1777f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[0] + sum2*ibdiag[2];
1778f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[1] + sum2*ibdiag[3];
1779f0d39aaaSBarry Smith             ibdiag  += 4;
1780f0d39aaaSBarry Smith             break;
1781f0d39aaaSBarry Smith           case 3:
1782f0d39aaaSBarry Smith             v2    = a->a + ii[row+1];
1783f0d39aaaSBarry Smith             v3    = a->a + ii[row+2];
1784f0d39aaaSBarry Smith             sum1  = b[row];
1785f0d39aaaSBarry Smith             sum2  = b[row+1];
1786f0d39aaaSBarry Smith             sum3  = b[row+2];
1787f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
1788f0d39aaaSBarry Smith               i1   = idx[0];
1789f0d39aaaSBarry Smith               i2   = idx[1];
1790f0d39aaaSBarry Smith               idx += 2;
1791f0d39aaaSBarry Smith               tmp0 = x[i1];
1792f0d39aaaSBarry Smith               tmp1 = x[i2];
1793f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
1794f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
1795f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
1796f0d39aaaSBarry Smith             }
1797f0d39aaaSBarry Smith 
1798f0d39aaaSBarry Smith             if (n == sz-1){
1799f0d39aaaSBarry Smith               tmp0  = x[*idx];
1800f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0;
1801f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0;
1802f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0;
1803f0d39aaaSBarry Smith             }
1804f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[0] + sum2*ibdiag[3] + sum3*ibdiag[6];
1805f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[1] + sum2*ibdiag[4] + sum3*ibdiag[7];
1806f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[2] + sum2*ibdiag[5] + sum3*ibdiag[8];
1807f0d39aaaSBarry Smith             ibdiag  += 9;
1808f0d39aaaSBarry Smith             break;
1809f0d39aaaSBarry Smith           case 4:
1810f0d39aaaSBarry Smith             v2    = a->a + ii[row+1];
1811f0d39aaaSBarry Smith             v3    = a->a + ii[row+2];
1812f0d39aaaSBarry Smith             v4    = a->a + ii[row+3];
1813f0d39aaaSBarry Smith             sum1  = b[row];
1814f0d39aaaSBarry Smith             sum2  = b[row+1];
1815f0d39aaaSBarry Smith             sum3  = b[row+2];
1816f0d39aaaSBarry Smith             sum4  = b[row+3];
1817f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
1818f0d39aaaSBarry Smith               i1   = idx[0];
1819f0d39aaaSBarry Smith               i2   = idx[1];
1820f0d39aaaSBarry Smith               idx += 2;
1821f0d39aaaSBarry Smith               tmp0 = x[i1];
1822f0d39aaaSBarry Smith               tmp1 = x[i2];
1823f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
1824f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
1825f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
1826f0d39aaaSBarry Smith               sum4 -= v4[0] * tmp0 + v4[1] * tmp1; v4 += 2;
1827f0d39aaaSBarry Smith             }
1828f0d39aaaSBarry Smith 
1829f0d39aaaSBarry Smith             if (n == sz-1){
1830f0d39aaaSBarry Smith               tmp0  = x[*idx];
1831f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0;
1832f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0;
1833f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0;
1834f0d39aaaSBarry Smith               sum4 -= v4[0] * tmp0;
1835f0d39aaaSBarry Smith             }
1836f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[0] + sum2*ibdiag[4] + sum3*ibdiag[8] + sum4*ibdiag[12];
1837f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[1] + sum2*ibdiag[5] + sum3*ibdiag[9] + sum4*ibdiag[13];
1838f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[2] + sum2*ibdiag[6] + sum3*ibdiag[10] + sum4*ibdiag[14];
1839f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[3] + sum2*ibdiag[7] + sum3*ibdiag[11] + sum4*ibdiag[15];
1840f0d39aaaSBarry Smith             ibdiag  += 16;
1841f0d39aaaSBarry Smith             break;
1842f0d39aaaSBarry Smith           case 5:
1843f0d39aaaSBarry Smith             v2    = a->a + ii[row+1];
1844f0d39aaaSBarry Smith             v3    = a->a + ii[row+2];
1845f0d39aaaSBarry Smith             v4    = a->a + ii[row+3];
1846f0d39aaaSBarry Smith             v5    = a->a + ii[row+4];
1847f0d39aaaSBarry Smith             sum1  = b[row];
1848f0d39aaaSBarry Smith             sum2  = b[row+1];
1849f0d39aaaSBarry Smith             sum3  = b[row+2];
1850f0d39aaaSBarry Smith             sum4  = b[row+3];
1851f0d39aaaSBarry Smith             sum5  = b[row+4];
1852f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
1853f0d39aaaSBarry Smith               i1   = idx[0];
1854f0d39aaaSBarry Smith               i2   = idx[1];
1855f0d39aaaSBarry Smith               idx += 2;
1856f0d39aaaSBarry Smith               tmp0 = x[i1];
1857f0d39aaaSBarry Smith               tmp1 = x[i2];
1858f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
1859f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
1860f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
1861f0d39aaaSBarry Smith               sum4 -= v4[0] * tmp0 + v4[1] * tmp1; v4 += 2;
1862f0d39aaaSBarry Smith               sum5 -= v5[0] * tmp0 + v5[1] * tmp1; v5 += 2;
1863f0d39aaaSBarry Smith             }
1864f0d39aaaSBarry Smith 
1865f0d39aaaSBarry Smith             if (n == sz-1){
1866f0d39aaaSBarry Smith               tmp0  = x[*idx];
1867f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0;
1868f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0;
1869f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0;
1870f0d39aaaSBarry Smith               sum4 -= v4[0] * tmp0;
1871f0d39aaaSBarry Smith               sum5 -= v5[0] * tmp0;
1872f0d39aaaSBarry Smith             }
1873f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[0] + sum2*ibdiag[5] + sum3*ibdiag[10] + sum4*ibdiag[15] + sum5*ibdiag[20];
1874f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[1] + sum2*ibdiag[6] + sum3*ibdiag[11] + sum4*ibdiag[16] + sum5*ibdiag[21];
1875f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[2] + sum2*ibdiag[7] + sum3*ibdiag[12] + sum4*ibdiag[17] + sum5*ibdiag[22];
1876f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[3] + sum2*ibdiag[8] + sum3*ibdiag[13] + sum4*ibdiag[18] + sum5*ibdiag[23];
1877f0d39aaaSBarry Smith             x[row++] = sum1*ibdiag[4] + sum2*ibdiag[9] + sum3*ibdiag[14] + sum4*ibdiag[19] + sum5*ibdiag[24];
1878f0d39aaaSBarry Smith             ibdiag  += 25;
1879f0d39aaaSBarry Smith             break;
18808862d2efSBarry Smith 	  default:
18818862d2efSBarry Smith    	    SETERRQ1(PETSC_ERR_SUP,"Inode size %D not supported",sizes[i]);
18828862d2efSBarry Smith         }
18832af78befSBarry Smith       }
18842af78befSBarry Smith 
18852af78befSBarry Smith       xb = x;
18862af78befSBarry Smith       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18872af78befSBarry Smith     } else xb = b;
18882af78befSBarry Smith     if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) &&
18892af78befSBarry Smith         (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) {
18908862d2efSBarry Smith       cnt = 0;
18918862d2efSBarry Smith       for (i=0, row=0; i<m; i++) {
18922af78befSBarry Smith 
18938862d2efSBarry Smith         switch (sizes[i]){
18948862d2efSBarry Smith           case 1:
18958862d2efSBarry Smith             x[row++] *= bdiag[cnt++];
18968862d2efSBarry Smith             break;
1897f0d39aaaSBarry Smith           case 2:
1898f0d39aaaSBarry Smith             x1   = x[row]; x2 = x[row+1];
1899f0d39aaaSBarry Smith             tmp1 = x1*bdiag[cnt] + x2*bdiag[cnt+2];
1900f0d39aaaSBarry Smith             tmp2 = x1*bdiag[cnt+1] + x2*bdiag[cnt+3];
1901f0d39aaaSBarry Smith             x[row++] = tmp1;
1902f0d39aaaSBarry Smith             x[row++] = tmp2;
1903f0d39aaaSBarry Smith             cnt += 4;
1904f0d39aaaSBarry Smith             break;
1905f0d39aaaSBarry Smith           case 3:
1906f0d39aaaSBarry Smith             x1   = x[row]; x2 = x[row+1]; x3 = x[row+2];
1907f0d39aaaSBarry Smith             tmp1 = x1*bdiag[cnt] + x2*bdiag[cnt+3] + x3*bdiag[cnt+6];
1908f0d39aaaSBarry Smith             tmp2 = x1*bdiag[cnt+1] + x2*bdiag[cnt+4] + x3*bdiag[cnt+7];
1909f0d39aaaSBarry Smith             tmp3 = x1*bdiag[cnt+2] + x2*bdiag[cnt+5] + x3*bdiag[cnt+8];
1910f0d39aaaSBarry Smith             x[row++] = tmp1;
1911f0d39aaaSBarry Smith             x[row++] = tmp2;
1912f0d39aaaSBarry Smith             x[row++] = tmp3;
1913f0d39aaaSBarry Smith             cnt += 9;
1914f0d39aaaSBarry Smith             break;
1915f0d39aaaSBarry Smith           case 4:
1916f0d39aaaSBarry Smith             x1   = x[row]; x2 = x[row+1]; x3 = x[row+2]; x4 = x[row+3];
1917f0d39aaaSBarry Smith             tmp1 = x1*bdiag[cnt] + x2*bdiag[cnt+4] + x3*bdiag[cnt+8] + x4*bdiag[cnt+12];
1918f0d39aaaSBarry Smith             tmp2 = x1*bdiag[cnt+1] + x2*bdiag[cnt+5] + x3*bdiag[cnt+9] + x4*bdiag[cnt+13];
1919f0d39aaaSBarry Smith             tmp3 = x1*bdiag[cnt+2] + x2*bdiag[cnt+6] + x3*bdiag[cnt+10] + x4*bdiag[cnt+14];
1920f0d39aaaSBarry Smith             tmp4 = x1*bdiag[cnt+3] + x2*bdiag[cnt+7] + x3*bdiag[cnt+11] + x4*bdiag[cnt+15];
1921f0d39aaaSBarry Smith             x[row++] = tmp1;
1922f0d39aaaSBarry Smith             x[row++] = tmp2;
1923f0d39aaaSBarry Smith             x[row++] = tmp3;
1924f0d39aaaSBarry Smith             x[row++] = tmp4;
1925f0d39aaaSBarry Smith             cnt += 16;
1926f0d39aaaSBarry Smith             break;
1927f0d39aaaSBarry Smith           case 5:
1928f0d39aaaSBarry Smith             x1   = x[row]; x2 = x[row+1]; x3 = x[row+2]; x4 = x[row+3]; x5 = x[row+4];
1929f0d39aaaSBarry Smith             tmp1 = x1*bdiag[cnt] + x2*bdiag[cnt+5] + x3*bdiag[cnt+10] + x4*bdiag[cnt+15] + x5*bdiag[cnt+20];
1930f0d39aaaSBarry Smith             tmp2 = x1*bdiag[cnt+1] + x2*bdiag[cnt+6] + x3*bdiag[cnt+11] + x4*bdiag[cnt+16] + x5*bdiag[cnt+21];
1931f0d39aaaSBarry Smith             tmp3 = x1*bdiag[cnt+2] + x2*bdiag[cnt+7] + x3*bdiag[cnt+12] + x4*bdiag[cnt+17] + x5*bdiag[cnt+22];
1932f0d39aaaSBarry Smith             tmp4 = x1*bdiag[cnt+3] + x2*bdiag[cnt+8] + x3*bdiag[cnt+13] + x4*bdiag[cnt+18] + x5*bdiag[cnt+23];
1933f0d39aaaSBarry Smith             tmp5 = x1*bdiag[cnt+4] + x2*bdiag[cnt+9] + x3*bdiag[cnt+14] + x4*bdiag[cnt+19] + x5*bdiag[cnt+24];
1934f0d39aaaSBarry Smith             x[row++] = tmp1;
1935f0d39aaaSBarry Smith             x[row++] = tmp2;
1936f0d39aaaSBarry Smith             x[row++] = tmp3;
1937f0d39aaaSBarry Smith             x[row++] = tmp4;
1938f0d39aaaSBarry Smith             x[row++] = tmp5;
1939f0d39aaaSBarry Smith             cnt += 25;
1940f0d39aaaSBarry Smith             break;
19418862d2efSBarry Smith 	  default:
19428862d2efSBarry Smith    	    SETERRQ1(PETSC_ERR_SUP,"Inode size %D not supported",sizes[i]);
19438862d2efSBarry Smith         }
19442af78befSBarry Smith       }
19452af78befSBarry Smith       ierr = PetscLogFlops(m);CHKERRQ(ierr);
19462af78befSBarry Smith     }
19472af78befSBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
19482af78befSBarry Smith 
1949f0d39aaaSBarry Smith       ibdiag = a->inode.ibdiag+a->inode.bdiagsize;
1950d0f46423SBarry Smith       for (i=m-1, row=A->rmap->n-1; i>=0; i--) {
1951f0d39aaaSBarry Smith         ibdiag -= sizes[i]*sizes[i];
19528862d2efSBarry Smith         sz      = ii[row+1] - diag[row] - 1;
19538862d2efSBarry Smith         v1      = a->a + diag[row] + 1;
19548862d2efSBarry Smith         idx     = a->j + diag[row] + 1;
19552af78befSBarry Smith 
19568862d2efSBarry Smith         /* see comments for MatMult_Inode() for how this is coded */
19578862d2efSBarry Smith         switch (sizes[i]){
19588862d2efSBarry Smith           case 1:
19598862d2efSBarry Smith 
19608862d2efSBarry Smith             sum1  = xb[row];
19618862d2efSBarry Smith             for(n = 0; n<sz-1; n+=2) {
19628862d2efSBarry Smith               i1   = idx[0];
19638862d2efSBarry Smith               i2   = idx[1];
19648862d2efSBarry Smith               idx += 2;
19658862d2efSBarry Smith               tmp0 = x[i1];
19668862d2efSBarry Smith               tmp1 = x[i2];
19678862d2efSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
19688862d2efSBarry Smith             }
19698862d2efSBarry Smith 
19708862d2efSBarry Smith             if (n == sz-1){
1971f0d39aaaSBarry Smith               tmp0  = x[*idx];
1972f0d39aaaSBarry Smith               sum1 -= *v1*tmp0;
19738862d2efSBarry Smith             }
1974f0d39aaaSBarry Smith             x[row--] = sum1*(*ibdiag);
1975f0d39aaaSBarry Smith             break;
1976f0d39aaaSBarry Smith 
1977f0d39aaaSBarry Smith           case 2:
1978f0d39aaaSBarry Smith 
1979f0d39aaaSBarry Smith             sum1  = xb[row];
1980f0d39aaaSBarry Smith             sum2  = xb[row-1];
1981f0d39aaaSBarry Smith             /* note that sum1 is associated with the second of the two rows */
1982f0d39aaaSBarry Smith             v2    = a->a + diag[row-1] + 2;
1983f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
1984f0d39aaaSBarry Smith               i1   = idx[0];
1985f0d39aaaSBarry Smith               i2   = idx[1];
1986f0d39aaaSBarry Smith               idx += 2;
1987f0d39aaaSBarry Smith               tmp0 = x[i1];
1988f0d39aaaSBarry Smith               tmp1 = x[i2];
1989f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
1990f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
1991f0d39aaaSBarry Smith             }
1992f0d39aaaSBarry Smith 
1993f0d39aaaSBarry Smith             if (n == sz-1){
1994f0d39aaaSBarry Smith               tmp0  = x[*idx];
1995f0d39aaaSBarry Smith               sum1 -= *v1*tmp0;
1996f0d39aaaSBarry Smith               sum2 -= *v2*tmp0;
1997f0d39aaaSBarry Smith             }
1998f0d39aaaSBarry Smith             x[row--] = sum2*ibdiag[1] + sum1*ibdiag[3];
1999f0d39aaaSBarry Smith             x[row--] = sum2*ibdiag[0] + sum1*ibdiag[2];
2000f0d39aaaSBarry Smith             break;
2001f0d39aaaSBarry Smith           case 3:
2002f0d39aaaSBarry Smith 
2003f0d39aaaSBarry Smith             sum1  = xb[row];
2004f0d39aaaSBarry Smith             sum2  = xb[row-1];
2005f0d39aaaSBarry Smith             sum3  = xb[row-2];
2006f0d39aaaSBarry Smith             v2    = a->a + diag[row-1] + 2;
2007f0d39aaaSBarry Smith             v3    = a->a + diag[row-2] + 3;
2008f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
2009f0d39aaaSBarry Smith               i1   = idx[0];
2010f0d39aaaSBarry Smith               i2   = idx[1];
2011f0d39aaaSBarry Smith               idx += 2;
2012f0d39aaaSBarry Smith               tmp0 = x[i1];
2013f0d39aaaSBarry Smith               tmp1 = x[i2];
2014f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
2015f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
2016f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
2017f0d39aaaSBarry Smith             }
2018f0d39aaaSBarry Smith 
2019f0d39aaaSBarry Smith             if (n == sz-1){
2020f0d39aaaSBarry Smith               tmp0  = x[*idx];
2021f0d39aaaSBarry Smith               sum1 -= *v1*tmp0;
2022f0d39aaaSBarry Smith               sum2 -= *v2*tmp0;
2023f0d39aaaSBarry Smith               sum3 -= *v3*tmp0;
2024f0d39aaaSBarry Smith             }
2025f0d39aaaSBarry Smith             x[row--] = sum3*ibdiag[2] + sum2*ibdiag[5] + sum1*ibdiag[8];
2026f0d39aaaSBarry Smith             x[row--] = sum3*ibdiag[1] + sum2*ibdiag[4] + sum1*ibdiag[7];
2027f0d39aaaSBarry Smith             x[row--] = sum3*ibdiag[0] + sum2*ibdiag[3] + sum1*ibdiag[6];
2028f0d39aaaSBarry Smith             break;
2029f0d39aaaSBarry Smith           case 4:
2030f0d39aaaSBarry Smith 
2031f0d39aaaSBarry Smith             sum1  = xb[row];
2032f0d39aaaSBarry Smith             sum2  = xb[row-1];
2033f0d39aaaSBarry Smith             sum3  = xb[row-2];
2034f0d39aaaSBarry Smith             sum4  = xb[row-3];
2035f0d39aaaSBarry Smith             v2    = a->a + diag[row-1] + 2;
2036f0d39aaaSBarry Smith             v3    = a->a + diag[row-2] + 3;
2037f0d39aaaSBarry Smith             v4    = a->a + diag[row-3] + 4;
2038f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
2039f0d39aaaSBarry Smith               i1   = idx[0];
2040f0d39aaaSBarry Smith               i2   = idx[1];
2041f0d39aaaSBarry Smith               idx += 2;
2042f0d39aaaSBarry Smith               tmp0 = x[i1];
2043f0d39aaaSBarry Smith               tmp1 = x[i2];
2044f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
2045f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
2046f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
2047f0d39aaaSBarry Smith               sum4 -= v4[0] * tmp0 + v4[1] * tmp1; v4 += 2;
2048f0d39aaaSBarry Smith             }
2049f0d39aaaSBarry Smith 
2050f0d39aaaSBarry Smith             if (n == sz-1){
2051f0d39aaaSBarry Smith               tmp0  = x[*idx];
2052f0d39aaaSBarry Smith               sum1 -= *v1*tmp0;
2053f0d39aaaSBarry Smith               sum2 -= *v2*tmp0;
2054f0d39aaaSBarry Smith               sum3 -= *v3*tmp0;
2055f0d39aaaSBarry Smith               sum4 -= *v4*tmp0;
2056f0d39aaaSBarry Smith             }
2057f0d39aaaSBarry Smith             x[row--] = sum4*ibdiag[3] + sum3*ibdiag[7] + sum2*ibdiag[11] + sum1*ibdiag[15];
2058f0d39aaaSBarry Smith             x[row--] = sum4*ibdiag[2] + sum3*ibdiag[6] + sum2*ibdiag[10] + sum1*ibdiag[14];
2059f0d39aaaSBarry Smith             x[row--] = sum4*ibdiag[1] + sum3*ibdiag[5] + sum2*ibdiag[9] + sum1*ibdiag[13];
2060f0d39aaaSBarry Smith             x[row--] = sum4*ibdiag[0] + sum3*ibdiag[4] + sum2*ibdiag[8] + sum1*ibdiag[12];
2061f0d39aaaSBarry Smith             break;
2062f0d39aaaSBarry Smith           case 5:
2063f0d39aaaSBarry Smith 
2064f0d39aaaSBarry Smith             sum1  = xb[row];
2065f0d39aaaSBarry Smith             sum2  = xb[row-1];
2066f0d39aaaSBarry Smith             sum3  = xb[row-2];
2067f0d39aaaSBarry Smith             sum4  = xb[row-3];
2068f0d39aaaSBarry Smith             sum5  = xb[row-4];
2069f0d39aaaSBarry Smith             v2    = a->a + diag[row-1] + 2;
2070f0d39aaaSBarry Smith             v3    = a->a + diag[row-2] + 3;
2071f0d39aaaSBarry Smith             v4    = a->a + diag[row-3] + 4;
2072f0d39aaaSBarry Smith             v5    = a->a + diag[row-4] + 5;
2073f0d39aaaSBarry Smith             for(n = 0; n<sz-1; n+=2) {
2074f0d39aaaSBarry Smith               i1   = idx[0];
2075f0d39aaaSBarry Smith               i2   = idx[1];
2076f0d39aaaSBarry Smith               idx += 2;
2077f0d39aaaSBarry Smith               tmp0 = x[i1];
2078f0d39aaaSBarry Smith               tmp1 = x[i2];
2079f0d39aaaSBarry Smith               sum1 -= v1[0] * tmp0 + v1[1] * tmp1; v1 += 2;
2080f0d39aaaSBarry Smith               sum2 -= v2[0] * tmp0 + v2[1] * tmp1; v2 += 2;
2081f0d39aaaSBarry Smith               sum3 -= v3[0] * tmp0 + v3[1] * tmp1; v3 += 2;
2082f0d39aaaSBarry Smith               sum4 -= v4[0] * tmp0 + v4[1] * tmp1; v4 += 2;
2083f0d39aaaSBarry Smith               sum5 -= v5[0] * tmp0 + v5[1] * tmp1; v5 += 2;
2084f0d39aaaSBarry Smith             }
2085f0d39aaaSBarry Smith 
2086f0d39aaaSBarry Smith             if (n == sz-1){
2087f0d39aaaSBarry Smith               tmp0  = x[*idx];
2088f0d39aaaSBarry Smith               sum1 -= *v1*tmp0;
2089f0d39aaaSBarry Smith               sum2 -= *v2*tmp0;
2090f0d39aaaSBarry Smith               sum3 -= *v3*tmp0;
2091f0d39aaaSBarry Smith               sum4 -= *v4*tmp0;
2092f0d39aaaSBarry Smith               sum5 -= *v5*tmp0;
2093f0d39aaaSBarry Smith             }
2094f0d39aaaSBarry Smith             x[row--] = sum5*ibdiag[4] + sum4*ibdiag[9] + sum3*ibdiag[14] + sum2*ibdiag[19] + sum1*ibdiag[24];
2095f0d39aaaSBarry Smith             x[row--] = sum5*ibdiag[3] + sum4*ibdiag[8] + sum3*ibdiag[13] + sum2*ibdiag[18] + sum1*ibdiag[23];
2096f0d39aaaSBarry Smith             x[row--] = sum5*ibdiag[2] + sum4*ibdiag[7] + sum3*ibdiag[12] + sum2*ibdiag[17] + sum1*ibdiag[22];
2097f0d39aaaSBarry Smith             x[row--] = sum5*ibdiag[1] + sum4*ibdiag[6] + sum3*ibdiag[11] + sum2*ibdiag[16] + sum1*ibdiag[21];
2098f0d39aaaSBarry Smith             x[row--] = sum5*ibdiag[0] + sum4*ibdiag[5] + sum3*ibdiag[10] + sum2*ibdiag[15] + sum1*ibdiag[20];
20998862d2efSBarry Smith             break;
21008862d2efSBarry Smith 	  default:
21018862d2efSBarry Smith    	    SETERRQ1(PETSC_ERR_SUP,"Inode size %D not supported",sizes[i]);
21028862d2efSBarry Smith         }
21032af78befSBarry Smith       }
21042af78befSBarry Smith 
21052af78befSBarry Smith       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
21062af78befSBarry Smith     }
21072af78befSBarry Smith     its--;
21082af78befSBarry Smith   }
21092af78befSBarry Smith   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
21102af78befSBarry Smith   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
21112af78befSBarry Smith   PetscFunctionReturn(0);
21122af78befSBarry Smith }
21132af78befSBarry Smith 
21142af78befSBarry Smith 
21154c1414c8SBarry Smith /*
21164c1414c8SBarry Smith     samestructure indicates that the matrix has not changed its nonzero structure so we
21174c1414c8SBarry Smith     do not need to recompute the inodes
21184c1414c8SBarry Smith */
21194c1414c8SBarry Smith #undef __FUNCT__
21204c1414c8SBarry Smith #define __FUNCT__ "Mat_CheckInode"
21214c1414c8SBarry Smith PetscErrorCode Mat_CheckInode(Mat A,PetscTruth samestructure)
21224c1414c8SBarry Smith {
21234c1414c8SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
21244c1414c8SBarry Smith   PetscErrorCode ierr;
21254c1414c8SBarry Smith   PetscInt       i,j,m,nzx,nzy,*idx,*idy,*ns,*ii,node_count,blk_size;
2126d74fe821SBarry Smith   PetscTruth     flag;
21274c1414c8SBarry Smith 
21284c1414c8SBarry Smith   PetscFunctionBegin;
2129d74fe821SBarry Smith   if (!a->inode.use)                     PetscFunctionReturn(0);
21304c1414c8SBarry Smith   if (a->inode.checked && samestructure) PetscFunctionReturn(0);
21314c1414c8SBarry Smith 
21324c1414c8SBarry Smith 
2133d0f46423SBarry Smith   m = A->rmap->n;
21344c1414c8SBarry Smith   if (a->inode.size) {ns = a->inode.size;}
21354c1414c8SBarry Smith   else {ierr = PetscMalloc((m+1)*sizeof(PetscInt),&ns);CHKERRQ(ierr);}
21364c1414c8SBarry Smith 
21374c1414c8SBarry Smith   i          = 0;
21384c1414c8SBarry Smith   node_count = 0;
21394c1414c8SBarry Smith   idx        = a->j;
21404c1414c8SBarry Smith   ii         = a->i;
21414c1414c8SBarry Smith   while (i < m){                /* For each row */
21424c1414c8SBarry Smith     nzx = ii[i+1] - ii[i];       /* Number of nonzeros */
21434c1414c8SBarry Smith     /* Limits the number of elements in a node to 'a->inode.limit' */
21444c1414c8SBarry Smith     for (j=i+1,idy=idx,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
21454c1414c8SBarry Smith       nzy     = ii[j+1] - ii[j]; /* Same number of nonzeros */
21464c1414c8SBarry Smith       if (nzy != nzx) break;
21474c1414c8SBarry Smith       idy  += nzx;             /* Same nonzero pattern */
21484c1414c8SBarry Smith       ierr = PetscMemcmp(idx,idy,nzx*sizeof(PetscInt),&flag);CHKERRQ(ierr);
21494c1414c8SBarry Smith       if (!flag) break;
21504c1414c8SBarry Smith     }
21514c1414c8SBarry Smith     ns[node_count++] = blk_size;
21524c1414c8SBarry Smith     idx += blk_size*nzx;
21534c1414c8SBarry Smith     i    = j;
21544c1414c8SBarry Smith   }
21554c1414c8SBarry Smith   /* If not enough inodes found,, do not use inode version of the routines */
2156f0d39aaaSBarry Smith   if (!a->inode.size && m && node_count > .9*m) {
21574c1414c8SBarry Smith     ierr = PetscFree(ns);CHKERRQ(ierr);
21584c1414c8SBarry Smith     a->inode.node_count     = 0;
21594c1414c8SBarry Smith     a->inode.size           = PETSC_NULL;
21604c1414c8SBarry Smith     a->inode.use            = PETSC_FALSE;
21614c1414c8SBarry Smith     ierr = PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);CHKERRQ(ierr);
21624c1414c8SBarry Smith   } else {
21634c1414c8SBarry Smith     A->ops->mult            = MatMult_Inode;
21642af78befSBarry Smith     A->ops->relax           = MatRelax_Inode;
21654c1414c8SBarry Smith     A->ops->multadd         = MatMultAdd_Inode;
21664c1414c8SBarry Smith     A->ops->getrowij        = MatGetRowIJ_Inode;
21674c1414c8SBarry Smith     A->ops->restorerowij    = MatRestoreRowIJ_Inode;
21684c1414c8SBarry Smith     A->ops->getcolumnij     = MatGetColumnIJ_Inode;
21694c1414c8SBarry Smith     A->ops->restorecolumnij = MatRestoreColumnIJ_Inode;
21704c1414c8SBarry Smith     A->ops->coloringpatch   = MatColoringPatch_Inode;
21714c1414c8SBarry Smith     a->inode.node_count     = node_count;
21724c1414c8SBarry Smith     a->inode.size           = ns;
21734c1414c8SBarry Smith     ierr = PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);CHKERRQ(ierr);
21744c1414c8SBarry Smith   }
21754c1414c8SBarry Smith   PetscFunctionReturn(0);
21764c1414c8SBarry Smith }
21774c1414c8SBarry Smith 
21784c1414c8SBarry Smith /*
21794c1414c8SBarry Smith      This is really ugly. if inodes are used this replaces the
21804c1414c8SBarry Smith   permutations with ones that correspond to rows/cols of the matrix
21814c1414c8SBarry Smith   rather then inode blocks
21824c1414c8SBarry Smith */
21834c1414c8SBarry Smith #undef __FUNCT__
21844c1414c8SBarry Smith #define __FUNCT__ "MatInodeAdjustForInodes"
21854c1414c8SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInodeAdjustForInodes(Mat A,IS *rperm,IS *cperm)
21864c1414c8SBarry Smith {
21874c1414c8SBarry Smith   PetscErrorCode ierr,(*f)(Mat,IS*,IS*);
21884c1414c8SBarry Smith 
21894c1414c8SBarry Smith   PetscFunctionBegin;
21904c1414c8SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)A,"MatInodeAdjustForInodes_C",(void (**)(void))&f);CHKERRQ(ierr);
21914c1414c8SBarry Smith   if (f) {
21924c1414c8SBarry Smith     ierr = (*f)(A,rperm,cperm);CHKERRQ(ierr);
21934c1414c8SBarry Smith   }
21944c1414c8SBarry Smith   PetscFunctionReturn(0);
21954c1414c8SBarry Smith }
21964c1414c8SBarry Smith 
21974c1414c8SBarry Smith EXTERN_C_BEGIN
21984c1414c8SBarry Smith #undef __FUNCT__
21994c1414c8SBarry Smith #define __FUNCT__ "MatAdjustForInodes_Inode"
22004c1414c8SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInodeAdjustForInodes_Inode(Mat A,IS *rperm,IS *cperm)
22014c1414c8SBarry Smith {
22024c1414c8SBarry Smith   Mat_SeqAIJ      *a=(Mat_SeqAIJ*)A->data;
22034c1414c8SBarry Smith   PetscErrorCode ierr;
22045d0c19d7SBarry Smith   PetscInt       m = A->rmap->n,n = A->cmap->n,i,j,nslim_row = a->inode.node_count;
22055d0c19d7SBarry Smith   const PetscInt *ridx,*cidx;
22064c1414c8SBarry Smith   PetscInt       row,col,*permr,*permc,*ns_row =  a->inode.size,*tns,start_val,end_val,indx;
22074c1414c8SBarry Smith   PetscInt       nslim_col,*ns_col;
22084c1414c8SBarry Smith   IS             ris = *rperm,cis = *cperm;
22094c1414c8SBarry Smith 
22104c1414c8SBarry Smith   PetscFunctionBegin;
22114c1414c8SBarry Smith   if (!a->inode.size) PetscFunctionReturn(0); /* no inodes so return */
22124c1414c8SBarry Smith   if (a->inode.node_count == m) PetscFunctionReturn(0); /* all inodes are of size 1 */
22134c1414c8SBarry Smith 
22144c1414c8SBarry Smith   ierr  = Mat_CreateColInode(A,&nslim_col,&ns_col);CHKERRQ(ierr);
22154c1414c8SBarry Smith   ierr  = PetscMalloc((((nslim_row>nslim_col)?nslim_row:nslim_col)+1)*sizeof(PetscInt),&tns);CHKERRQ(ierr);
22164c1414c8SBarry Smith   ierr  = PetscMalloc((m+n+1)*sizeof(PetscInt),&permr);CHKERRQ(ierr);
22174c1414c8SBarry Smith   permc = permr + m;
22184c1414c8SBarry Smith 
22194c1414c8SBarry Smith   ierr  = ISGetIndices(ris,&ridx);CHKERRQ(ierr);
22204c1414c8SBarry Smith   ierr  = ISGetIndices(cis,&cidx);CHKERRQ(ierr);
22214c1414c8SBarry Smith 
22224c1414c8SBarry Smith   /* Form the inode structure for the rows of permuted matric using inv perm*/
22234c1414c8SBarry Smith   for (i=0,tns[0]=0; i<nslim_row; ++i) tns[i+1] = tns[i] + ns_row[i];
22244c1414c8SBarry Smith 
22254c1414c8SBarry Smith   /* Construct the permutations for rows*/
22264c1414c8SBarry Smith   for (i=0,row = 0; i<nslim_row; ++i){
22274c1414c8SBarry Smith     indx      = ridx[i];
22284c1414c8SBarry Smith     start_val = tns[indx];
22294c1414c8SBarry Smith     end_val   = tns[indx + 1];
22304c1414c8SBarry Smith     for (j=start_val; j<end_val; ++j,++row) permr[row]= j;
22314c1414c8SBarry Smith   }
22324c1414c8SBarry Smith 
22334c1414c8SBarry Smith   /* Form the inode structure for the columns of permuted matrix using inv perm*/
22344c1414c8SBarry Smith   for (i=0,tns[0]=0; i<nslim_col; ++i) tns[i+1] = tns[i] + ns_col[i];
22354c1414c8SBarry Smith 
22364c1414c8SBarry Smith  /* Construct permutations for columns */
22374c1414c8SBarry Smith   for (i=0,col=0; i<nslim_col; ++i){
22384c1414c8SBarry Smith     indx      = cidx[i];
22394c1414c8SBarry Smith     start_val = tns[indx];
22404c1414c8SBarry Smith     end_val   = tns[indx + 1];
22414c1414c8SBarry Smith     for (j = start_val; j<end_val; ++j,++col) permc[col]= j;
22424c1414c8SBarry Smith   }
22434c1414c8SBarry Smith 
22444c1414c8SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,permr,rperm);CHKERRQ(ierr);
22454c1414c8SBarry Smith   ISSetPermutation(*rperm);
22464c1414c8SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,permc,cperm);CHKERRQ(ierr);
22474c1414c8SBarry Smith   ISSetPermutation(*cperm);
22484c1414c8SBarry Smith 
22494c1414c8SBarry Smith   ierr  = ISRestoreIndices(ris,&ridx);CHKERRQ(ierr);
22504c1414c8SBarry Smith   ierr  = ISRestoreIndices(cis,&cidx);CHKERRQ(ierr);
22514c1414c8SBarry Smith 
22524c1414c8SBarry Smith   ierr = PetscFree(ns_col);CHKERRQ(ierr);
22534c1414c8SBarry Smith   ierr = PetscFree(permr);CHKERRQ(ierr);
22544c1414c8SBarry Smith   ierr = ISDestroy(cis);CHKERRQ(ierr);
22554c1414c8SBarry Smith   ierr = ISDestroy(ris);CHKERRQ(ierr);
22564c1414c8SBarry Smith   ierr = PetscFree(tns);CHKERRQ(ierr);
22574c1414c8SBarry Smith   PetscFunctionReturn(0);
22584c1414c8SBarry Smith }
22594c1414c8SBarry Smith EXTERN_C_END
22604c1414c8SBarry Smith 
22614c1414c8SBarry Smith #undef __FUNCT__
22624c1414c8SBarry Smith #define __FUNCT__ "MatInodeGetInodeSizes"
22634c1414c8SBarry Smith /*@C
22644c1414c8SBarry Smith    MatInodeGetInodeSizes - Returns the inode information of the Inode matrix.
22654c1414c8SBarry Smith 
22664c1414c8SBarry Smith    Collective on Mat
22674c1414c8SBarry Smith 
22684c1414c8SBarry Smith    Input Parameter:
22694c1414c8SBarry Smith .  A - the Inode matrix or matrix derived from the Inode class -- e.g., SeqAIJ
22704c1414c8SBarry Smith 
22714c1414c8SBarry Smith    Output Parameter:
22724c1414c8SBarry Smith +  node_count - no of inodes present in the matrix.
22734c1414c8SBarry Smith .  sizes      - an array of size node_count,with sizes of each inode.
22744c1414c8SBarry Smith -  limit      - the max size used to generate the inodes.
22754c1414c8SBarry Smith 
22764c1414c8SBarry Smith    Level: advanced
22774c1414c8SBarry Smith 
22784c1414c8SBarry Smith    Notes: This routine returns some internal storage information
22794c1414c8SBarry Smith    of the matrix, it is intended to be used by advanced users.
22804c1414c8SBarry Smith    It should be called after the matrix is assembled.
22814c1414c8SBarry Smith    The contents of the sizes[] array should not be changed.
22824c1414c8SBarry Smith    PETSC_NULL may be passed for information not requested.
22834c1414c8SBarry Smith 
22844c1414c8SBarry Smith .keywords: matrix, seqaij, get, inode
22854c1414c8SBarry Smith 
22864c1414c8SBarry Smith .seealso: MatGetInfo()
22874c1414c8SBarry Smith @*/
22884c1414c8SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInodeGetInodeSizes(Mat A,PetscInt *node_count,PetscInt *sizes[],PetscInt *limit)
22894c1414c8SBarry Smith {
22904c1414c8SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt*,PetscInt*[],PetscInt*);
22914c1414c8SBarry Smith 
22924c1414c8SBarry Smith   PetscFunctionBegin;
22934c1414c8SBarry Smith   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
22944c1414c8SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)A,"MatInodeGetInodeSizes_C",(void (**)(void))&f);CHKERRQ(ierr);
22954c1414c8SBarry Smith   if (f) {
22964c1414c8SBarry Smith     ierr = (*f)(A,node_count,sizes,limit);CHKERRQ(ierr);
22974c1414c8SBarry Smith   }
22984c1414c8SBarry Smith   PetscFunctionReturn(0);
22994c1414c8SBarry Smith }
23004c1414c8SBarry Smith 
23014c1414c8SBarry Smith EXTERN_C_BEGIN
23024c1414c8SBarry Smith #undef __FUNCT__
23034c1414c8SBarry Smith #define __FUNCT__ "MatInodeGetInodeSizes_Inode"
23044c1414c8SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInodeGetInodeSizes_Inode(Mat A,PetscInt *node_count,PetscInt *sizes[],PetscInt *limit)
23054c1414c8SBarry Smith {
23064c1414c8SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
23074c1414c8SBarry Smith 
23084c1414c8SBarry Smith   PetscFunctionBegin;
23094c1414c8SBarry Smith   if (node_count) *node_count = a->inode.node_count;
23104c1414c8SBarry Smith   if (sizes)      *sizes      = a->inode.size;
23114c1414c8SBarry Smith   if (limit)      *limit      = a->inode.limit;
23124c1414c8SBarry Smith   PetscFunctionReturn(0);
23134c1414c8SBarry Smith }
23144c1414c8SBarry Smith EXTERN_C_END
2315