41 #ifndef KLU2_ANALYZE_HPP
42 #define KLU2_ANALYZE_HPP
44 #include "klu2_internal.h"
45 #include "klu2_analyze_given.hpp"
46 #include "klu2_memory.hpp"
52 template <
typename Entry,
typename Int>
53 static Int analyze_worker
78 KLU_symbolic<Entry, Int> *Symbolic,
79 KLU_common<Entry, Int> *Common
82 double amd_Info [TRILINOS_AMD_INFO], lnz, lnz1, flops, flops1 ;
83 Int k1, k2, nk, k, block, oldcol, pend, newcol, result, pc, p, newrow,
84 maxnz, nzoff, cstats [TRILINOS_COLAMD_STATS], ok, err = KLU_INVALID ;
89 for (k = 0 ; k < TRILINOS_AMD_INFO ; k++)
96 for (k = 0 ; k < n ; k++)
103 for (k = 0 ; k < n ; k++)
105 ASSERT (Pbtf [k] >= 0 && Pbtf [k] < n) ;
106 Pinv [Pbtf [k]] = k ;
109 for (k = 0 ; k < n ; k++) ASSERT (Pinv [k] != EMPTY) ;
115 Symbolic->symmetry = EMPTY ;
121 for (block = 0 ; block < nblocks ; block++)
131 PRINTF ((
"BLOCK %d, k1 %d k2-1 %d nk %d\n", block, k1, k2-1, nk)) ;
137 Lnz [block] = EMPTY ;
139 for (k = k1 ; k < k2 ; k++)
144 pend = Ap [oldcol+1] ;
145 for (p = Ap [oldcol] ; p < pend ; p++)
147 newrow = Pinv [Ai [p]] ;
155 ASSERT (newrow < k2) ;
162 maxnz = MAX (maxnz, pc) ;
163 ASSERT (KLU_valid (nk, Cp, Ci, NULL)) ;
176 for (k = 0 ; k < nk ; k++)
180 lnz1 = nk * (nk + 1) / 2 ;
181 flops1 = nk * (nk - 1) / 2 + (nk-1)*nk*(2*nk-1) / 6 ;
185 else if (ordering == 0)
193 result = KLU_OrdinalTraits<Int>::amd_order (nk, Cp, Ci, Pblk,
195 ok = (result >= TRILINOS_AMD_OK) ;
196 if (result == TRILINOS_AMD_OUT_OF_MEMORY)
198 err = KLU_OUT_OF_MEMORY ;
202 Common->mempeak = ( size_t) (MAX (Common->mempeak,
203 Common->memusage + amd_Info [TRILINOS_AMD_MEMORY])) ;
206 lnz1 = (Int) (amd_Info [TRILINOS_AMD_LNZ]) + nk ;
207 flops1 = 2 * amd_Info [TRILINOS_AMD_NMULTSUBS_LU] + amd_Info [TRILINOS_AMD_NDIV] ;
211 Symbolic->symmetry = amd_Info [TRILINOS_AMD_SYMMETRY] ;
215 else if (ordering == 1)
227 ok = KLU_OrdinalTraits<Int>::colamd (nk, nk, Cilen, Ci, Cp,
233 for (k = 0 ; k < nk ; k++)
246 lnz1 = (Common->user_order) (nk, Cp, Ci, Pblk, Common) ;
261 lnz = (lnz == EMPTY || lnz1 == EMPTY) ? EMPTY : (lnz + lnz1) ;
262 flops = (flops == EMPTY || flops1 == EMPTY) ? EMPTY : (flops + flops1) ;
268 PRINTF ((
"Pblk, 1-based:\n")) ;
269 for (k = 0 ; k < nk ; k++)
271 ASSERT (k + k1 < n) ;
272 ASSERT (Pblk [k] + k1 < n) ;
273 Q [k + k1] = Qbtf [Pblk [k] + k1] ;
275 for (k = 0 ; k < nk ; k++)
277 ASSERT (k + k1 < n) ;
278 ASSERT (Pblk [k] + k1 < n) ;
279 P [k + k1] = Pbtf [Pblk [k] + k1] ;
283 PRINTF ((
"nzoff %d Ap[n] %d\n", nzoff, Ap [n])) ;
284 ASSERT (nzoff >= 0 && nzoff <= Ap [n]) ;
287 Symbolic->lnz = lnz ;
288 Symbolic->unz = lnz ;
289 Symbolic->nzoff = nzoff ;
290 Symbolic->est_flops = flops ;
303 template <
typename Entry,
typename Int>
304 static KLU_symbolic<Entry, Int> *order_and_analyze
312 KLU_common<Entry, Int> *Common
316 KLU_symbolic<Entry, Int> *Symbolic ;
318 Int *Qbtf, *Cp, *Ci, *Pinv, *Pblk, *Pbtf, *P, *Q, *R ;
319 Int nblocks, nz, block, maxblock, k1, k2, nk, do_btf, ordering, k, Cilen,
326 Symbolic = KLU_alloc_symbolic (n, Ap, Ai, Common) ;
327 if (Symbolic == NULL)
334 Lnz = Symbolic->Lnz ;
337 ordering = Common->ordering ;
343 Cilen = KLU_OrdinalTraits<Int>::colamd_recommended (nz, n, n) ;
345 else if (ordering == 0 || (ordering == 3 && Common->user_order != NULL))
353 Common->status = KLU_INVALID ;
354 KLU_free_symbolic (&Symbolic, Common) ;
359 trilinos_amd_malloc = Common->malloc_memory ;
360 trilinos_amd_free = Common->free_memory ;
361 trilinos_amd_calloc = Common->calloc_memory ;
362 trilinos_amd_realloc = Common->realloc_memory ;
368 Pbtf = (Int *) KLU_malloc (n,
sizeof (Int), Common) ;
369 Qbtf = (Int *) KLU_malloc (n,
sizeof (Int), Common) ;
370 if (Common->status < KLU_OK)
372 KLU_free (Pbtf, n,
sizeof (Int), Common) ;
373 KLU_free (Qbtf, n,
sizeof (Int), Common) ;
374 KLU_free_symbolic (&Symbolic, Common) ;
382 do_btf = Common->btf ;
383 do_btf = (do_btf) ? TRUE : FALSE ;
384 Symbolic->ordering = ordering ;
385 Symbolic->do_btf = do_btf ;
386 Symbolic->structural_rank = EMPTY ;
397 Work = (Int *) KLU_malloc (5*n,
sizeof (Int), Common) ;
398 if (Common->status < KLU_OK)
401 KLU_free (Pbtf, n,
sizeof (Int), Common) ;
402 KLU_free (Qbtf, n,
sizeof (Int), Common) ;
403 KLU_free_symbolic (&Symbolic, Common) ;
410 nblocks = KLU_OrdinalTraits<Int>::btf_order (n, Ap, Ai,
411 Common->maxwork, &work, Pbtf, Qbtf, R,
412 &(Symbolic->structural_rank), Work) ;
413 Common->structural_rank = Symbolic->structural_rank ;
414 Common->work += work ;
416 KLU_free (Work, 5*n,
sizeof (Int), Common) ;
419 if (Symbolic->structural_rank < n)
421 for (k = 0 ; k < n ; k++)
423 Qbtf [k] = TRILINOS_BTF_UNFLIP (Qbtf [k]) ;
429 for (block = 0 ; block < nblocks ; block++)
434 PRINTF ((
"block %d size %d\n", block, nk)) ;
435 maxblock = MAX (maxblock, nk) ;
445 for (k = 0 ; k < n ; k++)
452 Symbolic->nblocks = nblocks ;
454 PRINTF ((
"maxblock size %d\n", maxblock)) ;
455 Symbolic->maxblock = maxblock ;
461 Pblk = (Int *) KLU_malloc (maxblock,
sizeof (Int), Common) ;
462 Cp = (Int *) KLU_malloc (maxblock + 1,
sizeof (Int), Common) ;
463 Ci = (Int *) KLU_malloc (MAX (Cilen, nz+1),
sizeof (Int), Common) ;
464 Pinv = (Int *) KLU_malloc (n,
sizeof (Int), Common) ;
470 if (Common->status == KLU_OK)
472 PRINTF ((
"calling analyze_worker\n")) ;
473 Common->status = analyze_worker (n, Ap, Ai, nblocks, Pbtf, Qbtf, R,
474 ordering, P, Q, Lnz, Pblk, Cp, Ci, Cilen, Pinv, Symbolic, Common) ;
475 PRINTF ((
"analyze_worker done\n")) ;
482 KLU_free (Pblk, maxblock,
sizeof (Int), Common) ;
483 KLU_free (Cp, maxblock+1,
sizeof (Int), Common) ;
484 KLU_free (Ci, MAX (Cilen, nz+1),
sizeof (Int), Common) ;
485 KLU_free (Pinv, n,
sizeof (Int), Common) ;
486 KLU_free (Pbtf, n,
sizeof (Int), Common) ;
487 KLU_free (Qbtf, n,
sizeof (Int), Common) ;
493 if (Common->status < KLU_OK)
495 KLU_free_symbolic (&Symbolic, Common) ;
505 template <
typename Entry,
typename Int>
506 KLU_symbolic<Entry, Int> *KLU_analyze
514 KLU_common<Entry, Int> *Common
526 Common->status = KLU_OK ;
527 Common->structural_rank = EMPTY ;
533 if (Common->ordering == 2)
539 return (KLU_analyze_given (n, Ap, Ai, dummy, dummy, Common)) ;
544 return (order_and_analyze (n, Ap, Ai, Common)) ;