xref: /petsc/src/mat/utils/matstash.c (revision ee0de897fec99be7c10c7637bbd068d61b2ce625)
1 #include "vec/vecimpl.h"
2 #include "matimpl.h"
3 
4 #define CHUNCKSIZE   5000
5 /*
6    This stash is currently used for all the parallel matrix implementations.
7    The stash is where elements of a matrix destined to be stored on other
8    processors are kept until matrix assembly is done.
9 
10    This is a simple minded stash. Do a linear search to determine if
11    in stash, if not add to end.
12 */
13 
14 int StashInitialize_Private(Stash *stash)
15 {
16   stash->nmax  = 0;
17   stash->n     = 0;
18   stash->array = 0;
19   stash->idx   = 0;
20   stash->idy   = 0;
21   return 0;
22 }
23 
24 int StashBuild_Private(Stash *stash)
25 {
26   stash->nmax  = CHUNCKSIZE; /* completely arbitrary number */
27   stash->n     = 0;
28   stash->array = (Scalar *) PetscMalloc( stash->nmax*(2*sizeof(int) +
29                             sizeof(Scalar))); CHKPTRQ(stash->array);
30   stash->idx   = (int *) (stash->array + stash->nmax); CHKPTRQ(stash->idx);
31   stash->idy   = (int *) (stash->idx + stash->nmax); CHKPTRQ(stash->idy);
32   return 0;
33 }
34 
35 int StashDestroy_Private(Stash *stash)
36 {
37   stash->nmax = stash->n = 0;
38   if (stash->array) {PetscFree(stash->array); stash->array = 0;}
39   return 0;
40 }
41 
42 int StashInfo_Private(Stash *stash)
43 {
44   PLogInfo(0,"Stash size %d\n",stash->n);
45   return 0;
46 }
47 
48 /*
49     Should do this properly. With a sorted array.
50 */
51 int StashValues_Private(Stash *stash,int row,int n, int *idxn,
52                         Scalar *values,InsertMode addv)
53 {
54   int    i, found, *n_idx, *n_idy;  /* int j, N = stash->n, */
55   Scalar val, *n_array;
56 
57   for ( i=0; i<n; i++ ) {
58     found = 0;
59     val = *values++;
60 /*
61    Removed search!  Now much faster assembly.
62 
63     for ( j=0; j<N; j++ ) {
64       if ( stash->idx[j] == row && stash->idy[j] == idxn[i]) {
65 
66         if (addv == ADD_VALUES) stash->array[j] += val;
67         else stash->array[j] = val;
68         found = 1;
69         break;
70       }
71     }
72 */
73     if (!found) { /* not found so add to end */
74       if ( stash->n == stash->nmax ) {
75         /* allocate a larger stash */
76         n_array = (Scalar *) PetscMalloc( (stash->nmax + CHUNCKSIZE)*(
77                                      2*sizeof(int)+sizeof(Scalar)));CHKPTRQ(n_array);
78         n_idx = (int *) (n_array + stash->nmax + CHUNCKSIZE);
79         n_idy = (int *) (n_idx + stash->nmax + CHUNCKSIZE);
80         PetscMemcpy(n_array,stash->array,stash->nmax*sizeof(Scalar));
81         PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(int));
82         PetscMemcpy(n_idy,stash->idy,stash->nmax*sizeof(int));
83         if (stash->array) PetscFree(stash->array);
84         stash->array = n_array; stash->idx = n_idx; stash->idy = n_idy;
85         stash->nmax += CHUNCKSIZE;
86       }
87       stash->array[stash->n] = val;
88       stash->idx[stash->n]   = row;
89       stash->idy[stash->n++] = idxn[i];
90     }
91   }
92   return 0;
93 }
94 
95 
96 
97