xref: /petsc/src/mat/utils/matstash.c (revision d07ff4555fceecf3da02886fa26b1d3c851458fc)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*d07ff455SSatish Balay static char vcid[] = "$Id: stash.c,v 1.19 1998/01/06 20:11:15 bsmith Exp balay $";
32d5177cdSBarry Smith #endif
42d5177cdSBarry Smith 
52d5177cdSBarry Smith #include "src/vec/vecimpl.h"
670f55243SBarry Smith #include "src/mat/matimpl.h"
79417f4adSLois Curfman McInnes 
897530c3fSBarry Smith #define CHUNCKSIZE   5000
99417f4adSLois Curfman McInnes /*
109417f4adSLois Curfman McInnes    This stash is currently used for all the parallel matrix implementations.
11d5d45c9bSBarry Smith    The stash is where elements of a matrix destined to be stored on other
12d5d45c9bSBarry Smith    processors are kept until matrix assembly is done.
139417f4adSLois Curfman McInnes 
1490f02eecSBarry Smith    This is a simple minded stash. Simply add entry to end of stash.
159417f4adSLois Curfman McInnes */
169417f4adSLois Curfman McInnes 
175615d1e5SSatish Balay #undef __FUNC__
18d4bb536fSBarry Smith #define __FUNC__ "StashInitialize_Private"
199417f4adSLois Curfman McInnes int StashInitialize_Private(Stash *stash)
209417f4adSLois Curfman McInnes {
213a40ed3dSBarry Smith   PetscFunctionBegin;
229417f4adSLois Curfman McInnes   stash->nmax    = 0;
23*d07ff455SSatish Balay   stash->oldnmax = 0;
249417f4adSLois Curfman McInnes   stash->n       = 0;
259417f4adSLois Curfman McInnes   stash->array   = 0;
269417f4adSLois Curfman McInnes   stash->idx     = 0;
279417f4adSLois Curfman McInnes   stash->idy     = 0;
283a40ed3dSBarry Smith   PetscFunctionReturn(0);
299417f4adSLois Curfman McInnes }
309417f4adSLois Curfman McInnes 
315615d1e5SSatish Balay #undef __FUNC__
32d4bb536fSBarry Smith #define __FUNC__ "StashBuild_Private"
339417f4adSLois Curfman McInnes int StashBuild_Private(Stash *stash)
349417f4adSLois Curfman McInnes {
35*d07ff455SSatish Balay   int ierr,flg,size,max;
36*d07ff455SSatish Balay 
373a40ed3dSBarry Smith   PetscFunctionBegin;
38*d07ff455SSatish Balay   ierr = OptionsGetInt(PETSC_NULL,"-stash_initial_size",&max,&flg);CHKERRQ(ierr);
39*d07ff455SSatish Balay   if (flg) {
40*d07ff455SSatish Balay     stash->nmax    = max;
41*d07ff455SSatish Balay     stash->oldnmax = max;
42*d07ff455SSatish Balay   } else {
439417f4adSLois Curfman McInnes     stash->nmax    = CHUNCKSIZE; /* completely arbitrary number */
44*d07ff455SSatish Balay     stash->oldnmax = CHUNCKSIZE;
45*d07ff455SSatish Balay   }
469417f4adSLois Curfman McInnes   stash->n       = 0;
470452661fSBarry Smith   stash->array   = (Scalar *) PetscMalloc( stash->nmax*(2*sizeof(int) +
4878b31e54SBarry Smith                             sizeof(Scalar))); CHKPTRQ(stash->array);
4978b31e54SBarry Smith   stash->idx     = (int *) (stash->array + stash->nmax); CHKPTRQ(stash->idx);
5078b31e54SBarry Smith   stash->idy     = (int *) (stash->idx + stash->nmax); CHKPTRQ(stash->idy);
513a40ed3dSBarry Smith   PetscFunctionReturn(0);
529417f4adSLois Curfman McInnes }
539417f4adSLois Curfman McInnes 
545615d1e5SSatish Balay #undef __FUNC__
55d4bb536fSBarry Smith #define __FUNC__ "StashDestroy_Private"
569417f4adSLois Curfman McInnes int StashDestroy_Private(Stash *stash)
579417f4adSLois Curfman McInnes {
583a40ed3dSBarry Smith   PetscFunctionBegin;
59*d07ff455SSatish Balay   /* Now update nmaxold to be app 10% more than nmax, this way the
60*d07ff455SSatish Balay      wastage of space is reduced the next time this stash is used */
61*d07ff455SSatish Balay   stash->oldnmax = (int)stash->nmax * 1.1;
62*d07ff455SSatish Balay   stash->nmax    = 0;
63*d07ff455SSatish Balay   stash->n       = 0;
640452661fSBarry Smith   if (stash->array) {PetscFree(stash->array); stash->array = 0;}
653a40ed3dSBarry Smith   PetscFunctionReturn(0);
669417f4adSLois Curfman McInnes }
679417f4adSLois Curfman McInnes 
685615d1e5SSatish Balay #undef __FUNC__
69d4bb536fSBarry Smith #define __FUNC__ "StashInfo_Private"
7097530c3fSBarry Smith int StashInfo_Private(Stash *stash)
7197530c3fSBarry Smith {
723a40ed3dSBarry Smith   PetscFunctionBegin;
73981c4779SBarry Smith   PLogInfo(0,"StashInfo_Private:Stash size %d\n",stash->n);
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
7597530c3fSBarry Smith }
7697530c3fSBarry Smith 
7797530c3fSBarry Smith /*
7897530c3fSBarry Smith     Should do this properly. With a sorted array.
7997530c3fSBarry Smith */
805615d1e5SSatish Balay #undef __FUNC__
815615d1e5SSatish Balay #define __FUNC__ "StashValues_Private"
8290f02eecSBarry Smith int StashValues_Private(Stash *stash,int row,int n, int *idxn,Scalar *values,InsertMode addv)
839417f4adSLois Curfman McInnes {
84*d07ff455SSatish Balay   int    i, found, *n_idx, *n_idy,newnmax;
859417f4adSLois Curfman McInnes   Scalar val, *n_array;
869417f4adSLois Curfman McInnes 
873a40ed3dSBarry Smith   PetscFunctionBegin;
889417f4adSLois Curfman McInnes   for ( i=0; i<n; i++ ) {
899417f4adSLois Curfman McInnes     found = 0;
909417f4adSLois Curfman McInnes     val = *values++;
919417f4adSLois Curfman McInnes     if (!found) { /* not found so add to end */
929417f4adSLois Curfman McInnes       if ( stash->n == stash->nmax ) {
939417f4adSLois Curfman McInnes         /* allocate a larger stash */
94*d07ff455SSatish Balay         if (stash->nmax == 0) newnmax = stash->oldnmax;
95*d07ff455SSatish Balay         else                  newnmax = stash->nmax *2;
96*d07ff455SSatish Balay 
97*d07ff455SSatish Balay         n_array = (Scalar *)PetscMalloc((newnmax)*(2*sizeof(int)+sizeof(Scalar)));CHKPTRQ(n_array);
98*d07ff455SSatish Balay         n_idx = (int *) (n_array + newnmax);
99*d07ff455SSatish Balay         n_idy = (int *) (n_idx + newnmax);
100416022c9SBarry Smith         PetscMemcpy(n_array,stash->array,stash->nmax*sizeof(Scalar));
101416022c9SBarry Smith         PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(int));
102416022c9SBarry Smith         PetscMemcpy(n_idy,stash->idy,stash->nmax*sizeof(int));
1030452661fSBarry Smith         if (stash->array) PetscFree(stash->array);
104*d07ff455SSatish Balay         stash->array   = n_array;
105*d07ff455SSatish Balay         stash->idx     = n_idx;
106*d07ff455SSatish Balay         stash->idy     = n_idy;
107*d07ff455SSatish Balay         stash->nmax    = newnmax;
108*d07ff455SSatish Balay         stash->oldnmax = newnmax;
1099417f4adSLois Curfman McInnes       }
1109417f4adSLois Curfman McInnes       stash->array[stash->n] = val;
1119417f4adSLois Curfman McInnes       stash->idx[stash->n]   = row;
1129417f4adSLois Curfman McInnes       stash->idy[stash->n++] = idxn[i];
1139417f4adSLois Curfman McInnes     }
1149417f4adSLois Curfman McInnes   }
1153a40ed3dSBarry Smith   PetscFunctionReturn(0);
1169417f4adSLois Curfman McInnes }
117