xref: /petsc/src/mat/utils/matstash.c (revision 4be1940c4a046bd11131d7583d63c94b63fffd45)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: stash.c,v 1.19 1998/01/06 20:11:15 bsmith Exp balay $";
3 #endif
4 
5 #include "src/vec/vecimpl.h"
6 #include "src/mat/matimpl.h"
7 
8 #define CHUNCKSIZE   5000
9 /*
10    This stash is currently used for all the parallel matrix implementations.
11    The stash is where elements of a matrix destined to be stored on other
12    processors are kept until matrix assembly is done.
13 
14    This is a simple minded stash. Simply add entry to end of stash.
15 */
16 
17 #undef __FUNC__
18 #define __FUNC__ "StashInitialize_Private"
19 int StashInitialize_Private(Stash *stash)
20 {
21   PetscFunctionBegin;
22   stash->nmax    = 0;
23   stash->oldnmax = 0;
24   stash->n       = 0;
25   stash->array   = 0;
26   stash->idx     = 0;
27   stash->idy     = 0;
28   PetscFunctionReturn(0);
29 }
30 
31 #undef __FUNC__
32 #define __FUNC__ "StashBuild_Private"
33 int StashBuild_Private(Stash *stash)
34 {
35   int ierr,flg,size,max;
36 
37   PetscFunctionBegin;
38   ierr = OptionsGetInt(PETSC_NULL,"-stash_initial_size",&max,&flg);CHKERRQ(ierr);
39   if (flg) {
40     stash->nmax    = max;
41     stash->oldnmax = max;
42   } else {
43     stash->nmax    = CHUNCKSIZE; /* completely arbitrary number */
44     stash->oldnmax = CHUNCKSIZE;
45   }
46   stash->n       = 0;
47   stash->array   = (Scalar *) PetscMalloc( stash->nmax*(2*sizeof(int) +
48                             sizeof(Scalar))); CHKPTRQ(stash->array);
49   stash->idx     = (int *) (stash->array + stash->nmax); CHKPTRQ(stash->idx);
50   stash->idy     = (int *) (stash->idx + stash->nmax); CHKPTRQ(stash->idy);
51   PetscFunctionReturn(0);
52 }
53 
54 #undef __FUNC__
55 #define __FUNC__ "StashDestroy_Private"
56 int StashDestroy_Private(Stash *stash)
57 {
58   PetscFunctionBegin;
59   /* Now update nmaxold to be app 10% more than nmax, this way the
60      wastage of space is reduced the next time this stash is used */
61   stash->oldnmax = (int)stash->nmax * 1.1;
62   stash->nmax    = 0;
63   stash->n       = 0;
64   if (stash->array) {PetscFree(stash->array); stash->array = 0;}
65   PetscFunctionReturn(0);
66 }
67 
68 #undef __FUNC__
69 #define __FUNC__ "StashInfo_Private"
70 int StashInfo_Private(Stash *stash)
71 {
72   PetscFunctionBegin;
73   PLogInfo(0,"StashInfo_Private:Stash size %d\n",stash->n);
74   PetscFunctionReturn(0);
75 }
76 
77 /*
78     Should do this properly. With a sorted array.
79 */
80 #undef __FUNC__
81 #define __FUNC__ "StashValues_Private"
82 int StashValues_Private(Stash *stash,int row,int n, int *idxn,Scalar *values,InsertMode addv)
83 {
84   int    i, found, *n_idx, *n_idy,newnmax;
85   Scalar val, *n_array;
86 
87   PetscFunctionBegin;
88   for ( i=0; i<n; i++ ) {
89     found = 0;
90     val = *values++;
91     if (!found) { /* not found so add to end */
92       if ( stash->n == stash->nmax ) {
93         /* allocate a larger stash */
94         if (stash->nmax == 0) newnmax = stash->oldnmax;
95         else                  newnmax = stash->nmax *2;
96 
97         n_array = (Scalar *)PetscMalloc((newnmax)*(2*sizeof(int)+sizeof(Scalar)));CHKPTRQ(n_array);
98         n_idx = (int *) (n_array + newnmax);
99         n_idy = (int *) (n_idx + newnmax);
100         PetscMemcpy(n_array,stash->array,stash->nmax*sizeof(Scalar));
101         PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(int));
102         PetscMemcpy(n_idy,stash->idy,stash->nmax*sizeof(int));
103         if (stash->array) PetscFree(stash->array);
104         stash->array   = n_array;
105         stash->idx     = n_idx;
106         stash->idy     = n_idy;
107         stash->nmax    = newnmax;
108         stash->oldnmax = newnmax;
109       }
110       stash->array[stash->n] = val;
111       stash->idx[stash->n]   = row;
112       stash->idy[stash->n++] = idxn[i];
113     }
114   }
115   PetscFunctionReturn(0);
116 }
117