Amesos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
amesos_klu_sort.c
Go to the documentation of this file.
1 /* ========================================================================== */
2 /* === KLU_sort ============================================================= */
3 /* ========================================================================== */
4 
5 /* sorts the columns of L and U so that the row indices appear in strictly
6  * increasing order.
7  */
8 
9 #include "amesos_klu_internal.h"
10 
11 /* ========================================================================== */
12 /* === sort ================================================================= */
13 /* ========================================================================== */
14 
15 /* Sort L or U using a double-transpose */
16 
17 static void sort (Int n, Int *Xip, Int *Xlen, Unit *LU, Int *Tp, Int *Tj,
18  Entry *Tx, Int *W)
19 {
20  Int *Xi ;
21  Entry *Xx ;
22  Int p, i, j, len, nz, tp, xlen, pend ;
23 
24  ASSERT (KLU_valid_LU (n, FALSE, Xip, Xlen, LU)) ;
25 
26  /* count the number of entries in each row of L or U */
27  for (i = 0 ; i < n ; i++)
28  {
29  W [i] = 0 ;
30  }
31  for (j = 0 ; j < n ; j++)
32  {
33  GET_POINTER (LU, Xip, Xlen, Xi, Xx, j, len) ;
34  for (p = 0 ; p < len ; p++)
35  {
36  W [Xi [p]]++ ;
37  }
38  }
39 
40  /* construct the row pointers for T */
41  nz = 0 ;
42  for (i = 0 ; i < n ; i++)
43  {
44  Tp [i] = nz ;
45  nz += W [i] ;
46  }
47  Tp [n] = nz ;
48  for (i = 0 ; i < n ; i++)
49  {
50  W [i] = Tp [i] ;
51  }
52 
53  /* transpose the matrix into Tp, Ti, Tx */
54  for (j = 0 ; j < n ; j++)
55  {
56  GET_POINTER (LU, Xip, Xlen, Xi, Xx, j, len) ;
57  for (p = 0 ; p < len ; p++)
58  {
59  tp = W [Xi [p]]++ ;
60  Tj [tp] = j ;
61  Tx [tp] = Xx [p] ;
62  }
63  }
64 
65  /* transpose the matrix back into Xip, Xlen, Xi, Xx */
66  for (j = 0 ; j < n ; j++)
67  {
68  W [j] = 0 ;
69  }
70  for (i = 0 ; i < n ; i++)
71  {
72  pend = Tp [i+1] ;
73  for (p = Tp [i] ; p < pend ; p++)
74  {
75  j = Tj [p] ;
76  GET_POINTER (LU, Xip, Xlen, Xi, Xx, j, len) ;
77  xlen = W [j]++ ;
78  Xi [xlen] = i ;
79  Xx [xlen] = Tx [p] ;
80  }
81  }
82 
83  ASSERT (KLU_valid_LU (n, FALSE, Xip, Xlen, LU)) ;
84 }
85 
86 
87 /* ========================================================================== */
88 /* === KLU_sort ============================================================= */
89 /* ========================================================================== */
90 
92 (
93  KLU_symbolic *Symbolic,
94  KLU_numeric *Numeric,
95  KLU_common *Common
96 )
97 {
98  Int *R, *W, *Tp, *Ti, *Lip, *Uip, *Llen, *Ulen ;
99  Entry *Tx ;
100  Unit **LUbx ;
101  Int n, nk, nz, block, nblocks, maxblock, k1 ;
102  size_t m1 ;
103 
104  if (Common == NULL)
105  {
106  return (FALSE) ;
107  }
108  Common->status = KLU_OK ;
109 
110  n = Symbolic->n ;
111  R = Symbolic->R ;
112  nblocks = Symbolic->nblocks ;
113  maxblock = Symbolic->maxblock ;
114 
115  Lip = Numeric->Lip ;
116  Llen = Numeric->Llen ;
117  Uip = Numeric->Uip ;
118  Ulen = Numeric->Ulen ;
119  LUbx = (Unit **) Numeric->LUbx ;
120 
121  m1 = ((size_t) maxblock) + 1 ;
122 
123  /* allocate workspace */
124  nz = MAX (Numeric->max_lnz_block, Numeric->max_unz_block) ;
125  W = KLU_malloc (maxblock, sizeof (Int), Common) ;
126  Tp = KLU_malloc (m1, sizeof (Int), Common) ;
127  Ti = KLU_malloc (nz, sizeof (Int), Common) ;
128  Tx = KLU_malloc (nz, sizeof (Entry), Common) ;
129 
130  PRINTF (("\n======================= Start sort:\n")) ;
131 
132  if (Common->status == KLU_OK)
133  {
134  /* sort each block of L and U */
135  for (block = 0 ; block < nblocks ; block++)
136  {
137  k1 = R [block] ;
138  nk = R [block+1] - k1 ;
139  if (nk > 1)
140  {
141  PRINTF (("\n-------------------block: %d nk %d\n", block, nk)) ;
142  sort (nk, Lip + k1, Llen + k1, LUbx [block], Tp, Ti, Tx, W) ;
143  sort (nk, Uip + k1, Ulen + k1, LUbx [block], Tp, Ti, Tx, W) ;
144  }
145  }
146  }
147 
148  PRINTF (("\n======================= sort done.\n")) ;
149 
150  /* free workspace */
151  KLU_free (W, maxblock, sizeof (Int), Common) ;
152  KLU_free (Tp, m1, sizeof (Int), Common) ;
153  KLU_free (Ti, nz, sizeof (Int), Common) ;
154  KLU_free (Tx, nz, sizeof (Entry), Common) ;
155  return (Common->status == KLU_OK) ;
156 }
#define GET_POINTER(LU, Xip, Xlen, Xi, Xx, k, xlen)
Int KLU_sort(KLU_symbolic *Symbolic, KLU_numeric *Numeric, KLU_common *Common)
static void sort(Int n, Int *Xip, Int *Xlen, Unit *LU, Int *Tp, Int *Tj, Entry *Tx, Int *W)
#define KLU_symbolic
#define Int
#define FALSE
double Unit
#define MAX(a, b)
#define NULL
#define ASSERT(expression)
#define KLU_numeric
#define KLU_valid_LU
#define KLU_malloc
#define KLU_common
#define Entry
#define PRINTF(params)
int n
#define KLU_free
#define KLU_OK