xref: /petsc/src/mat/utils/matstash.c (revision 70f55243aafb320636e2a54ff30cab5d1e8d3d7b)
1 #ifndef lint
2 static char vcid[] = "$Id: stash.c,v 1.10 1996/04/26 00:52:43 bsmith Exp bsmith $";
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. Do a linear search to determine if
15    in stash, if not add to end.
16 */
17 
18 int StashInitialize_Private(Stash *stash)
19 {
20   stash->nmax  = 0;
21   stash->n     = 0;
22   stash->array = 0;
23   stash->idx   = 0;
24   stash->idy   = 0;
25   return 0;
26 }
27 
28 int StashBuild_Private(Stash *stash)
29 {
30   stash->nmax  = CHUNCKSIZE; /* completely arbitrary number */
31   stash->n     = 0;
32   stash->array = (Scalar *) PetscMalloc( stash->nmax*(2*sizeof(int) +
33                             sizeof(Scalar))); CHKPTRQ(stash->array);
34   stash->idx   = (int *) (stash->array + stash->nmax); CHKPTRQ(stash->idx);
35   stash->idy   = (int *) (stash->idx + stash->nmax); CHKPTRQ(stash->idy);
36   return 0;
37 }
38 
39 int StashDestroy_Private(Stash *stash)
40 {
41   stash->nmax = stash->n = 0;
42   if (stash->array) {PetscFree(stash->array); stash->array = 0;}
43   return 0;
44 }
45 
46 int StashInfo_Private(Stash *stash)
47 {
48   PLogInfo(0,"Stash size %d\n",stash->n);
49   return 0;
50 }
51 
52 /*
53     Should do this properly. With a sorted array.
54 */
55 int StashValues_Private(Stash *stash,int row,int n, int *idxn,
56                         Scalar *values,InsertMode addv)
57 {
58   int    i, found, *n_idx, *n_idy;  /* int j, N = stash->n, */
59   Scalar val, *n_array;
60 
61   for ( i=0; i<n; i++ ) {
62     found = 0;
63     val = *values++;
64 /*
65    Removed search!  Now much faster assembly.
66 
67     for ( j=0; j<N; j++ ) {
68       if ( stash->idx[j] == row && stash->idy[j] == idxn[i]) {
69 
70         if (addv == ADD_VALUES) stash->array[j] += val;
71         else stash->array[j] = val;
72         found = 1;
73         break;
74       }
75     }
76 */
77     if (!found) { /* not found so add to end */
78       if ( stash->n == stash->nmax ) {
79         /* allocate a larger stash */
80         n_array = (Scalar *) PetscMalloc( (stash->nmax + CHUNCKSIZE)*(
81                                      2*sizeof(int)+sizeof(Scalar)));CHKPTRQ(n_array);
82         n_idx = (int *) (n_array + stash->nmax + CHUNCKSIZE);
83         n_idy = (int *) (n_idx + stash->nmax + CHUNCKSIZE);
84         PetscMemcpy(n_array,stash->array,stash->nmax*sizeof(Scalar));
85         PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(int));
86         PetscMemcpy(n_idy,stash->idy,stash->nmax*sizeof(int));
87         if (stash->array) PetscFree(stash->array);
88         stash->array = n_array; stash->idx = n_idx; stash->idy = n_idy;
89         stash->nmax += CHUNCKSIZE;
90       }
91       stash->array[stash->n] = val;
92       stash->idx[stash->n]   = row;
93       stash->idy[stash->n++] = idxn[i];
94     }
95   }
96   return 0;
97 }
98 
99 
100 
101