Borderbasix

dpivmacrev_z.hpp
Go to the documentation of this file.
1 #ifndef ALREADY_dpivmacrev
2 #define ALREADY_dpivmacrev
3 
4 template<>
5 int
7  const int jcol, /* in */
8  const macrev_Z u, /* in - diagonal pivoting threshold */
9  int *usepr, /* re-use the pivot sequence given by perm_r/iperm_r */
10  int *perm_r, /* may be modified */
11  int *iperm_r, /* in - inverse of perm_r */
12  int *iperm_c, /* in - used to find diagonal of Pc*A*Pc' */
13  int *pivrow, /* out */
14  GlobalLU_t<macrev_Z> *Glu /* modified - global LU data structures */
15  )
16 {
17 /*
18  * Purpose
19  * =======
20  * Performs the numerical pivoting on the current column of L,
21  * and the CDIV operation.
22  *
23  * Pivot policy:
24  * (1) Compute thresh = u * max_(i>=j) abs(A_ij);
25  * (2) IF user specifies pivot row k and abs(A_kj) >= thresh THEN
26  * pivot row = k;
27  * ELSE IF abs(A_jj) >= thresh THEN
28  * pivot row = j;
29  * ELSE
30  * pivot row = m;
31  *
32  * Note: If you absolutely want to use a given pivot order, then set u=0.0.
33  *
34  * Return value: 0 success;
35  * i > 0 U(i,i) is exactly zero.
36  *
37  */
38  int fsupc; /* first column in the supernode */
39  int nsupc; /* no of columns in the supernode */
40  int nsupr; /* no of rows in the supernode */
41  int lptr; /* points to the starting subscript of the supernode */
42  int pivptr, old_pivptr, diag, diagind;
43  macrev_Z pivmax, rtemp, thresh;
44  macrev_Z temp;
45  macrev_Z *lu_sup_ptr;
46  macrev_Z *lu_col_ptr;
47  int *lsub_ptr;
48  int isub, icol, k, itemp;
49  int *lsub, *xlsub;
50  macrev_Z *lusup;
51  int *xlusup;
52  extern SuperLUStat_t SuperLUStat;
53  flops_t *ops = SuperLUStat.ops;
54 
55  /* Initialize pointers */
56  lsub = Glu->lsub;
57  xlsub = Glu->xlsub;
58  lusup = Glu->lusup;
59  xlusup = Glu->xlusup;
60  fsupc = (Glu->xsup)[(Glu->supno)[jcol]];
61  nsupc = jcol - fsupc; /* excluding jcol; nsupc >= 0 */
62  lptr = xlsub[fsupc];
63  nsupr = xlsub[fsupc+1] - lptr;
64  lu_sup_ptr = &lusup[xlusup[fsupc]]; /* start of the current supernode */
65  lu_col_ptr = &lusup[xlusup[jcol]]; /* start of jcol in the supernode */
66  lsub_ptr = &lsub[lptr]; /* start of row indices of the supernode */
67 
68 #ifdef DEBUG
69 if ( jcol == MIN_COL ) {
70  printf("Before cdiv: col %d\n", jcol);
71  for (k = nsupc; k < nsupr; k++)
72  printf(" lu[%d] %f\n", lsub_ptr[k], lu_col_ptr[k]);
73 }
74 #endif
75 
76  /* Determine the largest abs numerical value for partial pivoting;
77  Also search for user-specified pivot, and diagonal element. */
78  if ( *usepr ) *pivrow = iperm_r[jcol];
79  diagind = iperm_c[jcol];
80  pivmax =(macrev_Z)0;
81  pivptr = nsupc;
82  diag = EMPTY;
83  old_pivptr = nsupc;
84  for (isub = nsupc; isub < nsupr; ++isub) {
85  rtemp = lu_col_ptr[isub];
86 
87  if ( rtemp != 0 ) {
88  pivmax = rtemp;
89  pivptr = isub;
90  break;
91  }
92  if ( *usepr && lsub_ptr[isub] == *pivrow ) old_pivptr = isub;
93  if ( lsub_ptr[isub] == diagind ) diag = isub;
94  }
95  //cout<<"comparaison dans dpivvotL "<<pivmax<<endl;
96  /* Test for singularity */
97  if ( pivmax ==0) {
98  *pivrow = lsub_ptr[nsupc];
99  //cout<<"degenere et pivrow "<<*pivrow<<endl;
100  perm_r[*pivrow] = jcol;
101  *usepr = 0;
102  return (jcol+1);
103  }
104 
105  thresh = u * pivmax;
106 
107  /* Choose appropriate pivotal element by our policy. */
108  if ( *usepr ) {
109  //fabs!!!!
110  rtemp = lu_col_ptr[old_pivptr];
111  //fabs!!!!!!!!!!!!!!!!! faire gaffe en generic
112  if ( rtemp != (macrev_Z)0 )
113  pivptr = old_pivptr;
114  else
115  *usepr = 0;
116  }
117  if ( *usepr == 0 ) {
118  /* Use diagonal pivot? */
119  if ( diag >= 0 ) { /* diagonal exists */
120  rtemp = lu_col_ptr[diag];
121  if ( rtemp != (macrev_Z)0 ) pivptr = diag;
122  }
123  *pivrow = lsub_ptr[pivptr];
124  }
125 
126  /* Record pivot row */
127  // cout<<"piv row "<<*pivrow<<" jcol "<<jcol<<endl;
128  perm_r[*pivrow] = jcol;
129 
130  /* Interchange row subscripts */
131  if ( pivptr != nsupc ) {
132  itemp = lsub_ptr[pivptr];
133  lsub_ptr[pivptr] = lsub_ptr[nsupc];
134  lsub_ptr[nsupc] = itemp;
135 
136  /* Interchange numerical values as well, for the whole snode, such
137  * that L is indexed the same way as A.
138  */
139  for (icol = 0; icol <= nsupc; icol++) {
140  itemp = pivptr + icol * nsupr;
141  temp = lu_sup_ptr[itemp];
142  lu_sup_ptr[itemp] = lu_sup_ptr[nsupc + icol*nsupr];
143  lu_sup_ptr[nsupc + icol*nsupr] = temp;
144  }
145  } /* if */
146 
147  /* cdiv operation */
148  ops[FACT] += nsupr - nsupc;
149  temp = (macrev_Z)1 / lu_col_ptr[nsupc];
150  for (k = nsupc+1; k < nsupr; k++)
151  lu_col_ptr[k] *= temp;
152 
153  return 0;
154 }
155 
156 void
157 dSetRWork(int m, int panel_size, macrev_Z *dworkptr,
158  macrev_Z **dense, macrev_Z **tempv)
159 {
160  macrev_Z zero(0);
161  int i;
162  int maxsuper = sp_ienv(3),
163  rowblk = sp_ienv(4);
164  *dense = dworkptr;
165  *tempv = *dense + panel_size*m;
166  for(i=0;i<m*panel_size;i++)
167  mpz_init((*dense)[i].rep);
168  for(i=0;i<NUM_TEMPV(m,panel_size,maxsuper,rowblk);i++)
169  mpz_init((*tempv)[i].rep);
170  //dfill (*dense, m * panel_size, zero);
171  //dfill (*tempv, NUM_TEMPV(m,panel_size,maxsuper,rowblk), zero);
172 }
173 
174 void dLUWorkFree(int m,int panel_size, int *iwork, macrev_Z *dwork
175  , GlobalLU_t<macrev_Z > *Glu)
176 {
177  int i;
178  int maxsuper = sp_ienv(3),
179  rowblk = sp_ienv(4);
180  if ( Glu->MemModel == SYSTEM ) {
181  SUPERLU_FREE (iwork);
182  for(i=0;i<m*panel_size+NUM_TEMPV(m,panel_size,maxsuper,rowblk);i++)
183  mpz_clear((dwork[i].rep));
184  SUPERLU_FREE (dwork);
185  } else {
186  stack.used -= (stack.size - stack.top2);
187  stack.top2 = stack.size;
188 /* dStackCompress(Glu); */
189  }
190 
191  SUPERLU_FREE (expanders);
192 }
193 
194 void* dexpand (
195  int *prev_len, /* length used from previous call */
196  MemType type, /* which part of the memory to expand */
197  int len_to_copy, /* size of the memory to be copied to new store */
198  int keep_prev, /* = 1: use prev_len;
199  = 0: compute new_len to expand */
200  GlobalLU_t<macrev_Z > *Glu /* modified - global LU data structures */
201  )
202 {
203  double EXPAND = 1.5;
204  double alpha;
205  void *new_mem, *old_mem;
206  int new_len, tries, lword, extra, bytes_to_copy;
207 
208  alpha = EXPAND;
209 
210  if ( no_expand == 0 || keep_prev ) /* First time allocate requested */
211  new_len = *prev_len;
212  else {
213  new_len = (int)(alpha * *prev_len);
214  }
215 
216  if ( type == LSUB || type == USUB ) lword = sizeof(int);
217  else lword = sizeof(macrev_Z);
218 
219  if ( Glu->MemModel == SYSTEM ) {
220  new_mem = (void *) SUPERLU_MALLOC(new_len * lword);
221  if(new_mem && lword==sizeof(macrev_Z))
222  for(extra=0;extra<new_len;extra++)
223  mpz_init(((macrev_Z*)new_mem)[extra].rep);
224 /* new_mem = (void *) calloc(new_len, lword); */
225  if ( no_expand != 0 ) {
226  tries = 0;
227  if ( keep_prev ) {
228  if ( !new_mem ) return (NULL);
229  } else {
230  while ( !new_mem )
231  {
232  if ( ++tries > 10 ) return (NULL);
233  alpha = Reduce(alpha);
234  new_len = (int)(alpha * *prev_len);
235  new_mem = (void *) SUPERLU_MALLOC(new_len * lword);
236 /* new_mem = (void *) calloc(new_len, lword); */
237  }
238  if(tries && lword==sizeof(macrev_Z))
239  for(int extra=0;extra<new_len;extra++)
240  mpz_init(((macrev_Z*)new_mem)[extra].rep);
241  }
242  if ( type == LSUB || type == USUB ) {
243  copy_mem_int(len_to_copy, expanders[type].mem, new_mem);
244  } else {
245  copy_mem_double<macrev_Z >(len_to_copy,
246  expanders[type].mem, new_mem);
247  //cout<<"'j'en detruit ici"<<endl;
248  for(unsigned int i=0;i<len_to_copy;i++)
249  mpz_clear(((macrev_Z*)expanders[type].mem)[i].rep);
250  }
251 
252  SUPERLU_FREE (expanders[type].mem);
253  }
254  expanders[type].mem = (void *) new_mem;
255 
256  } else { /* MemModel == USER */
257  if ( no_expand == 0 ) {
258  new_mem = duser_malloc(new_len * lword, HEAD);
259  if ( NotDoubleAlign(new_mem) &&
260  (type == LUSUP || type == UCOL) ) {
261  old_mem = new_mem;
262  new_mem = (void *)DoubleAlign(new_mem);
263  extra = (char*)new_mem - (char*)old_mem;
264 #ifdef DEBUG
265  printf("expand(): not aligned, extra %d\n", extra);
266 #endif
267  stack.top1 += extra;
268  stack.used += extra;
269  }
270  expanders[type].mem = (void *) new_mem;
271  }
272  else {
273  tries = 0;
274  extra = (new_len - *prev_len) * lword;
275  if ( keep_prev ) {
276  if ( StackFull(extra) ) return (NULL);
277  } else {
278  while ( StackFull(extra) ) {
279  if ( ++tries > 10 ) return (NULL);
280  alpha = Reduce(alpha);
281  new_len = (int)(alpha * *prev_len);
282  extra = (new_len - *prev_len) * lword;
283  }
284  }
285 
286  if ( type != USUB ) {
287  new_mem = (void*)((char*)expanders[type + 1].mem + extra);
288  bytes_to_copy = (char*)stack.array + stack.top1
289  - (char*)expanders[type + 1].mem;
290  user_bcopy((char*)expanders[type+1].mem,
291  (char*)new_mem, bytes_to_copy);
292 
293  if ( type < USUB ) {
294  Glu->usub = (int*)
295  (expanders[USUB].mem =
296  (void*)((char*)expanders[USUB].mem + extra));
297  }
298  if ( type < LSUB ) {
299  Glu->lsub =(int*)(expanders[LSUB].mem =
300  (void*)((char*)expanders[LSUB].mem + extra));
301  }
302  if ( type < UCOL ) {
303  Glu->ucol =(macrev_Z*)( expanders[UCOL].mem =
304  (void*)((char*)expanders[UCOL].mem + extra));
305  }
306  stack.top1 += extra;
307  stack.used += extra;
308  if ( type == UCOL ) {
309  stack.top1 += extra; /* Add same amount for USUB */
310  stack.used += extra;
311  }
312 
313  } /* if ... */
314 
315  } /* else ... */
316  }
317 
318  expanders[type].size = new_len;
319  *prev_len = new_len;
320  if ( no_expand ) ++no_expand;
321 
322  return (void *) expanders[type].mem;
323 
324 } /* dexpand */
325 
326 template<>
327 harewell<macrev_Z >::harewell(const int nrow,const int ncol)
328 {
329  //Pour eviter le undefined behavior on met tous lespointeurs non initialises a NULL
330  this->nrow=nrow;
331  this->ncol=ncol;
332  this->rowind=(int*)malloc(sizeof(int)*(this->ncol));
333  this->colptr=(int*)malloc(sizeof(int)*(1+this->ncol));
334  this->nzval=MAC_REV_MALLOC<macrev_Z >(sizeof(macrev_Z)*(this->ncol));
335  this->nnz=this->ncol;
336  for(int i=0;i<this->ncol;i++)
337  {
338  this->rowind[i]=i;
339  this->colptr[i]=i;
340  // ((T*)this->nzval)[i]=0;
341  }
342  this->colptr[this->ncol]=this->nnz;
343 }
344 
345 template<>
346 inline void harewell<macrev_Z >::destroystore()
347 {
348  free(rowind);
349  rowind=NULL;
350  free(colptr);
351  colptr=NULL;
352  // cout<<"nnz "<<nnz<<endl;
353  MAC_REV_FREE<macrev_Z>(nzval,nnz*sizeof(macrev_Z));
354  nzval=NULL;
355  nnz=0;
356 }
357 /*
358 void destroystore(SuperMatrix<macrev_Z >& m)
359 {
360  //cout<<"appel de destroystrore"<<endl;
361  switch (m.Stype)
362  {
363  case NC:
364  {
365  for(int i=0;i<((NCformat*)m.Store)->nnz;i++)
366  mpz_clear(((((macrev_Z*)((NCformat*)m.Store)->nzval))[i].rep));
367  Destroy_CompCol_Matrix(&m);
368  break;
369  }
370  case SC:
371  {
372  for(int i=0;i<((SCformat*)m.Store)->nnz;i++)
373  mpz_clear(((((macrev_Z*)((SCformat*)m.Store)->nzval))[i]).rep);
374  Destroy_SuperNode_Matrix(&m);
375  break;
376  }
377  case NCP:
378  {
379  //cout<<"compcol permuted"<<endl;
380  Destroy_CompCol_Permuted(&m);
381  break;
382  }
383  default:
384  {
385  ABORT("Used Type Storage not yet supported \n");
386  }
387  };
388 
389  }*/
390 
391 void remiseformatNC(SuperMatrix<macrev_Z > &M,const vector<macrev_Z > &nzval
392  ,const vector<int> &rowind,const vector<int> &colptr
393  ,int coeff)
394 {
395  ((NCformat *)M.Store)->nnz=coeff;
396  destroystore(M);
397  M.Stype=NC;
398  M.Store=(void *)malloc(sizeof(NCformat));
399  NCformat *Mstore=(NCformat *)M.Store;
400  Mstore->nnz=nzval.size();
401  Mstore->nzval=(void *)malloc(sizeof(macrev_Z)*(nzval.size()));
402  Mstore->rowind=(int*)malloc(sizeof(int)*(nzval.size()));
403  Mstore->colptr=(int*)malloc(sizeof(int)*(colptr.size()));
404  for(unsigned int i=0;i<nzval.size();++i)
405  {
406  Mstore->rowind[i]=rowind[i];
407  mpz_init(((macrev_Z*)(Mstore->nzval))[i].rep);
408  ((macrev_Z*)(Mstore->nzval))[i]=nzval[i];
409  }
410  for(unsigned int i=0;i<colptr.size();++i)
411  Mstore->colptr[i]=colptr[i];
412  return;
413 }
414 
415 
416 #endif //ALREADY_dpivmacrev
void dLUWorkFree(int m, int panel_size, int *iwork, macrev_Z *dwork, GlobalLU_t< macrev_Z > *Glu)
Definition: dpivmacrev_z.hpp:174
void * dexpand(int *prev_len, MemType type, int len_to_copy, int keep_prev, GlobalLU_t< macrev_Z > *Glu)
Definition: dpivmacrev_z.hpp:194
void free(void *)
void * malloc(YYSIZE_T)
void remiseformatNC(SuperMatrix< macrev_Z > &M, const vector< macrev_Z > &nzval, const vector< int > &rowind, const vector< int > &colptr, int coeff)
Definition: dpivmacrev_z.hpp:391
MSKint32t k
Definition: mosek.h:2713
MSKint64t MSKint32t MSKint64t MSKsymmattypee * type
Definition: mosek.h:4257
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
void dSetRWork(int m, int panel_size, macrev_Z *dworkptr, macrev_Z **dense, macrev_Z **tempv)
Definition: dpivmacrev_z.hpp:157
int dpivotL(const int jcol, const macrev_Z u, int *usepr, int *perm_r, int *iperm_r, int *iperm_c, int *pivrow, GlobalLU_t< macrev_Z > *Glu)
Definition: dpivmacrev_z.hpp:6
Home  |  Download & InstallContributions