1 
2 #include <petsc/private/matimpl.h>
3 
4 /* Get new PetscMatStashSpace into the existing space */
PetscMatStashSpaceGet(PetscInt bs2,PetscInt n,PetscMatStashSpace * space)5 PetscErrorCode PetscMatStashSpaceGet(PetscInt bs2,PetscInt n,PetscMatStashSpace *space)
6 {
7   PetscMatStashSpace a;
8   PetscErrorCode     ierr;
9 
10   PetscFunctionBegin;
11   if (!n) PetscFunctionReturn(0);
12 
13   ierr = PetscMalloc(sizeof(struct _MatStashSpace),&a);CHKERRQ(ierr);
14   ierr = PetscMalloc3(n*bs2,&(a->space_head),n,&a->idx,n,&a->idy);CHKERRQ(ierr);
15 
16   a->val              = a->space_head;
17   a->local_remaining  = n;
18   a->local_used       = 0;
19   a->total_space_size = 0;
20   a->next             = NULL;
21 
22   if (*space) {
23     (*space)->next      = a;
24     a->total_space_size = (*space)->total_space_size;
25   }
26   a->total_space_size += n;
27   *space               = a;
28   PetscFunctionReturn(0);
29 }
30 
31 /* Copy the values in space into arrays val, idx and idy. Then destroy space */
PetscMatStashSpaceContiguous(PetscInt bs2,PetscMatStashSpace * space,PetscScalar * val,PetscInt * idx,PetscInt * idy)32 PetscErrorCode PetscMatStashSpaceContiguous(PetscInt bs2,PetscMatStashSpace *space,PetscScalar *val,PetscInt *idx,PetscInt *idy)
33 {
34   PetscMatStashSpace a;
35   PetscErrorCode     ierr;
36 
37   PetscFunctionBegin;
38   while ((*space)) {
39     a    = (*space)->next;
40     ierr = PetscArraycpy(val,(*space)->val,(*space)->local_used*bs2);CHKERRQ(ierr);
41     val += bs2*(*space)->local_used;
42     ierr = PetscArraycpy(idx,(*space)->idx,(*space)->local_used);CHKERRQ(ierr);
43     idx += (*space)->local_used;
44     ierr = PetscArraycpy(idy,(*space)->idy,(*space)->local_used);CHKERRQ(ierr);
45     idy += (*space)->local_used;
46 
47     ierr   =  PetscFree3((*space)->space_head,(*space)->idx,(*space)->idy);CHKERRQ(ierr);
48     ierr   =  PetscFree(*space);CHKERRQ(ierr);
49     *space = a;
50   }
51   PetscFunctionReturn(0);
52 }
53 
PetscMatStashSpaceDestroy(PetscMatStashSpace * space)54 PetscErrorCode PetscMatStashSpaceDestroy(PetscMatStashSpace *space)
55 {
56   PetscMatStashSpace a;
57   PetscErrorCode     ierr;
58 
59   PetscFunctionBegin;
60   while (*space) {
61     a      = (*space)->next;
62     ierr   = PetscFree3((*space)->space_head,(*space)->idx,(*space)->idy);CHKERRQ(ierr);
63     ierr   = PetscFree((*space));CHKERRQ(ierr);
64     *space = a;
65   }
66   *space = NULL;
67   PetscFunctionReturn(0);
68 }
69