SparseLU_Memory.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 /*
11 
12  * NOTE: This file is the modified version of [s,d,c,z]memory.c files in SuperLU
13 
14  * -- SuperLU routine (version 3.1) --
15  * Univ. of California Berkeley, Xerox Palo Alto Research Center,
16  * and Lawrence Berkeley National Lab.
17  * August 1, 2008
18  *
19  * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
20  *
21  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
22  * EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
23  *
24  * Permission is hereby granted to use or copy this program for any
25  * purpose, provided the above notices are retained on all copies.
26  * Permission to modify the code and to distribute modified code is
27  * granted, provided the above notices are retained, and a notice that
28  * the code was modified is included with the above copyright notice.
29  */
30 
31 #ifndef EIGEN_SPARSELU_MEMORY
32 #define EIGEN_SPARSELU_MEMORY
33 
34 #include "./InternalHeaderCheck.h"
35 
36 namespace Eigen {
37 namespace internal {
38 
39 enum { LUNoMarker = 3 };
40 enum {emptyIdxLU = -1};
42 {
43  return (std::max)(m, (t+b)*w);
44 }
45 
46 template< typename Scalar>
48 {
49  return (2*w + 4 + LUNoMarker) * m * sizeof(Index) + (w + 1) * m * sizeof(Scalar);
50 }
51 
52 
53 
54 
63 template <typename Scalar, typename StorageIndex>
64 template <typename VectorType>
65 Index SparseLUImpl<Scalar,StorageIndex>::expand(VectorType& vec, Index& length, Index nbElts, Index keep_prev, Index& num_expansions)
66 {
67 
68  float alpha = 1.5; // Ratio of the memory increase
69  Index new_len; // New size of the allocated memory
70 
71  if(num_expansions == 0 || keep_prev)
72  new_len = length ; // First time allocate requested
73  else
74  new_len = (std::max)(length+1,Index(alpha * length));
75 
76  VectorType old_vec; // Temporary vector to hold the previous values
77  if (nbElts > 0 )
78  old_vec = vec.segment(0,nbElts);
79 
80  //Allocate or expand the current vector
81 #ifdef EIGEN_EXCEPTIONS
82  try
83 #endif
84  {
85  vec.resize(new_len);
86  }
87 #ifdef EIGEN_EXCEPTIONS
88  catch(std::bad_alloc& )
89 #else
90  if(!vec.size())
91 #endif
92  {
93  if (!num_expansions)
94  {
95  // First time to allocate from LUMemInit()
96  // Let LUMemInit() deals with it.
97  return -1;
98  }
99  if (keep_prev)
100  {
101  // In this case, the memory length should not not be reduced
102  return new_len;
103  }
104  else
105  {
106  // Reduce the size and increase again
107  Index tries = 0; // Number of attempts
108  do
109  {
110  alpha = (alpha + 1)/2;
111  new_len = (std::max)(length+1,Index(alpha * length));
112 #ifdef EIGEN_EXCEPTIONS
113  try
114 #endif
115  {
116  vec.resize(new_len);
117  }
118 #ifdef EIGEN_EXCEPTIONS
119  catch(std::bad_alloc& )
120 #else
121  if (!vec.size())
122 #endif
123  {
124  tries += 1;
125  if ( tries > 10) return new_len;
126  }
127  } while (!vec.size());
128  }
129  }
130  //Copy the previous values to the newly allocated space
131  if (nbElts > 0)
132  vec.segment(0, nbElts) = old_vec;
133 
134 
135  length = new_len;
136  if(num_expansions) ++num_expansions;
137  return 0;
138 }
139 
152 template <typename Scalar, typename StorageIndex>
153 Index SparseLUImpl<Scalar,StorageIndex>::memInit(Index m, Index n, Index annz, Index lwork, Index fillratio, Index panel_size, GlobalLU_t& glu)
154 {
155  Index& num_expansions = glu.num_expansions; //No memory expansions so far
156  num_expansions = 0;
157  glu.nzumax = glu.nzlumax = (std::min)(fillratio * (annz+1) / n, m) * n; // estimated number of nonzeros in U
158  glu.nzlmax = (std::max)(Index(4), fillratio) * (annz+1) / 4; // estimated nnz in L factor
159  // Return the estimated size to the user if necessary
160  Index tempSpace;
161  tempSpace = (2*panel_size + 4 + LUNoMarker) * m * sizeof(Index) + (panel_size + 1) * m * sizeof(Scalar);
162  if (lwork == emptyIdxLU)
163  {
164  Index estimated_size;
165  estimated_size = (5 * n + 5) * sizeof(Index) + tempSpace
166  + (glu.nzlmax + glu.nzumax) * sizeof(Index) + (glu.nzlumax+glu.nzumax) * sizeof(Scalar) + n;
167  return estimated_size;
168  }
169 
170  // Setup the required space
171 
172  // First allocate Integer pointers for L\U factors
173  glu.xsup.resize(n+1);
174  glu.supno.resize(n+1);
175  glu.xlsub.resize(n+1);
176  glu.xlusup.resize(n+1);
177  glu.xusub.resize(n+1);
178 
179  // Reserve memory for L/U factors
180  do
181  {
182  if( (expand<ScalarVector>(glu.lusup, glu.nzlumax, 0, 0, num_expansions)<0)
183  || (expand<ScalarVector>(glu.ucol, glu.nzumax, 0, 0, num_expansions)<0)
184  || (expand<IndexVector> (glu.lsub, glu.nzlmax, 0, 0, num_expansions)<0)
185  || (expand<IndexVector> (glu.usub, glu.nzumax, 0, 1, num_expansions)<0) )
186  {
187  //Reduce the estimated size and retry
188  glu.nzlumax /= 2;
189  glu.nzumax /= 2;
190  glu.nzlmax /= 2;
191  if (glu.nzlumax < annz ) return glu.nzlumax;
192  }
193  } while (!glu.lusup.size() || !glu.ucol.size() || !glu.lsub.size() || !glu.usub.size());
194 
195  ++num_expansions;
196  return 0;
197 
198 } // end LuMemInit
199 
209 template <typename Scalar, typename StorageIndex>
210 template <typename VectorType>
211 Index SparseLUImpl<Scalar,StorageIndex>::memXpand(VectorType& vec, Index& maxlen, Index nbElts, MemType memtype, Index& num_expansions)
212 {
213  Index failed_size;
214  if (memtype == USUB)
215  failed_size = this->expand<VectorType>(vec, maxlen, nbElts, 1, num_expansions);
216  else
217  failed_size = this->expand<VectorType>(vec, maxlen, nbElts, 0, num_expansions);
218 
219  if (failed_size)
220  return failed_size;
221 
222  return 0 ;
223 }
224 
225 } // end namespace internal
226 
227 } // end namespace Eigen
228 #endif // EIGEN_SPARSELU_MEMORY
Matrix3f m
Array< int, 3, 1 > b
int n
RowVector3d w
bfloat16() max(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:690
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:684
Index LUnumTempV(Index &m, Index &w, Index &t, Index &b)
Index LUTempSpace(Index &m, Index &w)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82