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