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