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