xref: /petsc/src/mat/utils/matstash.c (revision d5d45c9bad5d66633d6ef030bee5689b038db93c)
1 #include "vec/vecimpl.h"
2 #include "matimpl.h"
3 
4 #define CHUNCKSIZE   100
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 StashValues_Private(Stash *stash,int row,int n, int *idxn,
43                         Scalar *values,InsertMode addv)
44 {
45   int    i,j,N = stash->n,found,*n_idx, *n_idy;
46   Scalar val,*n_array;
47 
48   for ( i=0; i<n; i++ ) {
49     found = 0;
50     val = *values++;
51     for ( j=0; j<N; j++ ) {
52       if ( stash->idx[j] == row && stash->idy[j] == idxn[i]) {
53         /* found a match */
54         if (addv == ADD_VALUES) stash->array[j] += val;
55         else stash->array[j] = val;
56         found = 1;
57         break;
58       }
59     }
60     if (!found) { /* not found so add to end */
61       if ( stash->n == stash->nmax ) {
62         /* allocate a larger stash */
63         n_array = (Scalar *) PetscMalloc( (stash->nmax + CHUNCKSIZE)*(
64                                      2*sizeof(int)+sizeof(Scalar)));CHKPTRQ(n_array);
65         n_idx = (int *) (n_array + stash->nmax + CHUNCKSIZE);
66         n_idy = (int *) (n_idx + stash->nmax + CHUNCKSIZE);
67         PetscMemcpy(n_array,stash->array,stash->nmax*sizeof(Scalar));
68         PetscMemcpy(n_idx,stash->idx,stash->nmax*sizeof(int));
69         PetscMemcpy(n_idy,stash->idy,stash->nmax*sizeof(int));
70         if (stash->array) PetscFree(stash->array);
71         stash->array = n_array; stash->idx = n_idx; stash->idy = n_idy;
72         stash->nmax += CHUNCKSIZE;
73       }
74       stash->array[stash->n]   = val;
75       stash->idx[stash->n]     = row;
76       stash->idy[stash->n++]   = idxn[i];
77     }
78   }
79   return 0;
80 }
81 
82 
83