DSDP
sdpalloc.c
1 #include "numchol.h"
2 
3 int iSum(int,int*);
4 void ShutDown();
5 int ExitProc(int,char *);
6 
7 int iAlloc(int len,
8  char *info,
9  int **r)
10 {
11  *r=NULL;
12 
13  if (len)
14  {
15  *r=(int*)calloc(len,sizeof(int));
16  if (!(*r)){ ExitProc(OutOfSpc,info); return 1;}
17  }
18  return 0;
19 } /* iAlloc */
20 
21 void iFree(int **x)
22 {
23  int *r=*x;
24 
25  if (r)
26  {
27  free(r);
28  *x=NULL;
29  }
30 } /* iFree */
31 
32 void cFree(char **x)
33 {
34  char *r = *x;
35  /*
36  printf("\n r %d &r %d",r,&r);
37  getchar();
38  */
39  if(r)
40  { free(r);
41  *x = NULL;
42  }
43 
44 }
45 
46 int dAlloc(int len,
47  char *info,
48  double **rr)
49 {
50  double *r=NULL;
51 
52  if (len) {
53  r=(double*)calloc(len,sizeof(double));
54  if (!r){ ExitProc(OutOfSpc,info); return 1;}
55  }
56  *rr=r;
57  return 0;
58 } /* dAlloc */
59 
60 void dFree(double **x)
61 {
62  double *r=*x;
63 
64  if (r)
65  {
66  free(r);
67  *x=NULL;
68  }
69 } /* dFree */
70 
71 
72 
73 int LvalAlloc(chfac *sf,
74  char *info)
75 {
76  int ierr=0,nnz;
77 
78  nnz=iSum(sf->nrow,sf->ujsze);
79  if ( nnz<=sf->unnz )
80  return 1;
81 
82  sf->unnz=0;
83  if (sf->uval) dFree(&sf->uval);
84  ierr=dAlloc(nnz,info,&sf->uval);
85 
86  sf->unnz=nnz;
87  if (ierr) return 1;
88  return 0;
89 } /* LvalAlloc */
90 
91 int CfcAlloc(int maxrow,
92  char *info,chfac**rr)
93 {
94  chfac *r=NULL;
95  int ierr=0;
96 
97  if (maxrow) {
98  r=(chfac*)calloc(1,sizeof(chfac));
99  if (!r) ExitProc(OutOfSpc,info);
100 
101  r->mrow =maxrow;
102  r->nrow =maxrow;
103 
104  r->snnz =0;
105  ierr=iAlloc(maxrow,info,&r->shead); if(ierr) return 1;
106  ierr=iAlloc(maxrow,info,&r->ssize); if(ierr) return 1;
107  r->ssub =NULL;
108  ierr=dAlloc(maxrow,info,&r->diag); if(ierr) return 1;
109  ierr=dAlloc(maxrow,info,&r->sqrtdiag); if(ierr) return 1;
110  r->unnz =0;
111  r->ujnz =0;
112  ierr=iAlloc(maxrow,info,&r->ujbeg); if(ierr) return 1;
113  ierr=iAlloc(maxrow,info,&r->uhead); if(ierr) return 1;
114  ierr=iAlloc(maxrow,info,&r->ujsze); if(ierr) return 1;
115  r->usub =NULL;
116  r->uval =NULL;
117  ierr=iAlloc(maxrow,info,&r->perm); if(ierr) return 1;
118  ierr=iAlloc(maxrow,info,&r->invp); if(ierr) return 1;
119  r->nsnds=0;
120  ierr=iAlloc(maxrow+1,info,&r->subg); if(ierr) return 1;
121  r->n=maxrow;
122  r->alldense=0;
123  r->tolpiv=1.0e-13; /* Standard */
124  r->tolpiv=1.0e-35;
125  r->cachesize =256;
126 #ifdef DSDPCACHESIZE
127  if (DSDPCACHESIZE>0){
128  r->cachesize = (int)DSDPCACHESIZE;
129  }
130 #endif
131  r->cacheunit =1000;
132 
133  }
134  *rr=r;
135  return 0;
136 } /* SchlAlloc */
137 
138 void CfcFree(chfac **sf)
139 {
140  chfac *r=*sf;
141 
142  if (*sf) {
143  iFree(&r->shead);
144  iFree(&r->ssize);
145  iFree(&r->ssub);
146  dFree(&r->diag);
147  dFree(&r->sqrtdiag);
148  iFree(&r->uhead);
149  iFree(&r->ujsze);
150  dFree(&r->uval);
151  iFree(&r->perm);
152  iFree(&r->subg);
153  iFree(&r->dhead);
154  iFree(&r->dbeg);
155  iFree(&r->dsub);
156  iFree(&r->iw);
157  dFree(&r->rw);
158  if (r->alldense){
159  r->invp=0;
160  r->ujbeg=0;
161  r->usub=0;
162  }else{
163  iFree(&r->invp);
164  iFree(&r->ujbeg);
165  iFree(&r->usub);
166  }
167  free(r);
168  }
169  *sf=NULL;
170 } /* CfcFree */
171 
172 int dPtAlloc(int n,
173  char *info,double ***rr)
174 {
175  int ierr,i;
176  double **r;
177 
178  r=NULL;
179  *rr=NULL;
180  if (!n) return 0;
181 
182  r=(double **)calloc(n,sizeof(double*));
183  if (!r){
184  ExitProc(OutOfSpc,info);
185  return 1;
186  }
187  ierr=dAlloc(n*(n-1)/2,info,&r[0]);
188  if(ierr) return 1;
189  for (i=1; i<n; i++)
190  r[i]=r[i-1]+n-i;
191 
192  *rr=r;
193  return 0;
194 } /* dPtAlloc */
195 
196 void dPtFree(double ***x)
197 {
198  double **r=*x;
199 
200  if (r) {
201  if (r[0])
202  dFree(&r[0]);
203  free(r);
204  *x=NULL;
205  }
206 } /* dPtFree */
207