ergo
template_lapack_larrb.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
28  /* This file belongs to the template_lapack part of the Ergo source
29  * code. The source files in the template_lapack directory are modified
30  * versions of files originally distributed as CLAPACK, see the
31  * Copyright/license notice in the file template_lapack/COPYING.
32  */
33 
34 
35 #ifndef TEMPLATE_LAPACK_LARRB_HEADER
36 #define TEMPLATE_LAPACK_LARRB_HEADER
37 
38 template<class Treal>
39 int template_lapack_larrb(integer *n, Treal *d__, Treal *lld,
40  integer *ifirst, integer *ilast, Treal *rtol1, Treal *rtol2,
41  integer *offset, Treal *w, Treal *wgap, Treal *werr,
42  Treal *work, integer *iwork, Treal *pivmin, Treal *
43  spdiam, integer *twist, integer *info)
44 {
45  /* System generated locals */
46  integer i__1;
47  Treal d__1, d__2;
48 
49  /* Local variables */
50  integer i__, k, r__, i1, ii, ip;
51  Treal gap, mid, tmp, back, lgap, rgap, left;
52  integer iter, nint, prev, next;
53  Treal cvrgd, right, width;
54  integer negcnt;
55  Treal mnwdth;
56  integer olnint, maxitr;
57 
58 
59 /* -- LAPACK auxiliary routine (version 3.2) -- */
60 /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
61 /* November 2006 */
62 
63 /* .. Scalar Arguments .. */
64 /* .. */
65 /* .. Array Arguments .. */
66 /* .. */
67 
68 /* Purpose */
69 /* ======= */
70 
71 /* Given the relatively robust representation(RRR) L D L^T, DLARRB */
72 /* does "limited" bisection to refine the eigenvalues of L D L^T, */
73 /* W( IFIRST-OFFSET ) through W( ILAST-OFFSET ), to more accuracy. Initial */
74 /* guesses for these eigenvalues are input in W, the corresponding estimate */
75 /* of the error in these guesses and their gaps are input in WERR */
76 /* and WGAP, respectively. During bisection, intervals */
77 /* [left, right] are maintained by storing their mid-points and */
78 /* semi-widths in the arrays W and WERR respectively. */
79 
80 /* Arguments */
81 /* ========= */
82 
83 /* N (input) INTEGER */
84 /* The order of the matrix. */
85 
86 /* D (input) DOUBLE PRECISION array, dimension (N) */
87 /* The N diagonal elements of the diagonal matrix D. */
88 
89 /* LLD (input) DOUBLE PRECISION array, dimension (N-1) */
90 /* The (N-1) elements L(i)*L(i)*D(i). */
91 
92 /* IFIRST (input) INTEGER */
93 /* The index of the first eigenvalue to be computed. */
94 
95 /* ILAST (input) INTEGER */
96 /* The index of the last eigenvalue to be computed. */
97 
98 /* RTOL1 (input) DOUBLE PRECISION */
99 /* RTOL2 (input) DOUBLE PRECISION */
100 /* Tolerance for the convergence of the bisection intervals. */
101 /* An interval [LEFT,RIGHT] has converged if */
102 /* RIGHT-LEFT.LT.MAX( RTOL1*GAP, RTOL2*MAX(|LEFT|,|RIGHT|) ) */
103 /* where GAP is the (estimated) distance to the nearest */
104 /* eigenvalue. */
105 
106 /* OFFSET (input) INTEGER */
107 /* Offset for the arrays W, WGAP and WERR, i.e., the IFIRST-OFFSET */
108 /* through ILAST-OFFSET elements of these arrays are to be used. */
109 
110 /* W (input/output) DOUBLE PRECISION array, dimension (N) */
111 /* On input, W( IFIRST-OFFSET ) through W( ILAST-OFFSET ) are */
112 /* estimates of the eigenvalues of L D L^T indexed IFIRST throug */
113 /* ILAST. */
114 /* On output, these estimates are refined. */
115 
116 /* WGAP (input/output) DOUBLE PRECISION array, dimension (N-1) */
117 /* On input, the (estimated) gaps between consecutive */
118 /* eigenvalues of L D L^T, i.e., WGAP(I-OFFSET) is the gap between */
119 /* eigenvalues I and I+1. Note that if IFIRST.EQ.ILAST */
120 /* then WGAP(IFIRST-OFFSET) must be set to ZERO. */
121 /* On output, these gaps are refined. */
122 
123 /* WERR (input/output) DOUBLE PRECISION array, dimension (N) */
124 /* On input, WERR( IFIRST-OFFSET ) through WERR( ILAST-OFFSET ) are */
125 /* the errors in the estimates of the corresponding elements in W. */
126 /* On output, these errors are refined. */
127 
128 /* WORK (workspace) DOUBLE PRECISION array, dimension (2*N) */
129 /* Workspace. */
130 
131 /* IWORK (workspace) INTEGER array, dimension (2*N) */
132 /* Workspace. */
133 
134 /* PIVMIN (input) DOUBLE PRECISION */
135 /* The minimum pivot in the Sturm sequence. */
136 
137 /* SPDIAM (input) DOUBLE PRECISION */
138 /* The spectral diameter of the matrix. */
139 
140 /* TWIST (input) INTEGER */
141 /* The twist index for the twisted factorization that is used */
142 /* for the negcount. */
143 /* TWIST = N: Compute negcount from L D L^T - LAMBDA I = L+ D+ L+^T */
144 /* TWIST = 1: Compute negcount from L D L^T - LAMBDA I = U- D- U-^T */
145 /* TWIST = R: Compute negcount from L D L^T - LAMBDA I = N(r) D(r) N(r) */
146 
147 /* INFO (output) INTEGER */
148 /* Error flag. */
149 
150 /* Further Details */
151 /* =============== */
152 
153 /* Based on contributions by */
154 /* Beresford Parlett, University of California, Berkeley, USA */
155 /* Jim Demmel, University of California, Berkeley, USA */
156 /* Inderjit Dhillon, University of Texas, Austin, USA */
157 /* Osni Marques, LBNL/NERSC, USA */
158 /* Christof Voemel, University of California, Berkeley, USA */
159 
160 /* ===================================================================== */
161 
162 /* .. Parameters .. */
163 /* .. */
164 /* .. Local Scalars .. */
165 /* .. */
166 /* .. External Functions .. */
167 
168 /* .. */
169 /* .. Intrinsic Functions .. */
170 /* .. */
171 /* .. Executable Statements .. */
172 
173  /* Parameter adjustments */
174  --iwork;
175  --work;
176  --werr;
177  --wgap;
178  --w;
179  --lld;
180  --d__;
181 
182  /* Function Body */
183  *info = 0;
184 
185  maxitr = (integer) ((template_blas_log(*spdiam + *pivmin) - template_blas_log(*pivmin)) / template_blas_log(2.)) +
186  2;
187  mnwdth = *pivmin * 2.;
188 
189  r__ = *twist;
190  if (r__ < 1 || r__ > *n) {
191  r__ = *n;
192  }
193 
194 /* Initialize unconverged intervals in [ WORK(2*I-1), WORK(2*I) ]. */
195 /* The Sturm Count, Count( WORK(2*I-1) ) is arranged to be I-1, while */
196 /* Count( WORK(2*I) ) is stored in IWORK( 2*I ). The integer IWORK( 2*I-1 ) */
197 /* for an unconverged interval is set to the index of the next unconverged */
198 /* interval, and is -1 or 0 for a converged interval. Thus a linked */
199 /* list of unconverged intervals is set up. */
200 
201  i1 = *ifirst;
202 /* The number of unconverged intervals */
203  nint = 0;
204 /* The last unconverged interval found */
205  prev = 0;
206  rgap = wgap[i1 - *offset];
207  i__1 = *ilast;
208  for (i__ = i1; i__ <= i__1; ++i__) {
209  k = i__ << 1;
210  ii = i__ - *offset;
211  left = w[ii] - werr[ii];
212  right = w[ii] + werr[ii];
213  lgap = rgap;
214  rgap = wgap[ii];
215  gap = minMACRO(lgap,rgap);
216 /* Make sure that [LEFT,RIGHT] contains the desired eigenvalue */
217 /* Compute negcount from dstqds facto L+D+L+^T = L D L^T - LEFT */
218 
219 /* Do while( NEGCNT(LEFT).GT.I-1 ) */
220 
221  back = werr[ii];
222 L20:
223  negcnt = template_lapack_laneg(n, &d__[1], &lld[1], &left, pivmin, &r__);
224  if (negcnt > i__ - 1) {
225  left -= back;
226  back *= 2.;
227  goto L20;
228  }
229 
230 /* Do while( NEGCNT(RIGHT).LT.I ) */
231 /* Compute negcount from dstqds facto L+D+L+^T = L D L^T - RIGHT */
232 
233  back = werr[ii];
234 L50:
235  negcnt = template_lapack_laneg(n, &d__[1], &lld[1], &right, pivmin, &r__);
236  if (negcnt < i__) {
237  right += back;
238  back *= 2.;
239  goto L50;
240  }
241  width = (d__1 = left - right, absMACRO(d__1)) * .5;
242 /* Computing MAX */
243  d__1 = absMACRO(left), d__2 = absMACRO(right);
244  tmp = maxMACRO(d__1,d__2);
245 /* Computing MAX */
246  d__1 = *rtol1 * gap, d__2 = *rtol2 * tmp;
247  cvrgd = maxMACRO(d__1,d__2);
248  if (width <= cvrgd || width <= mnwdth) {
249 /* This interval has already converged and does not need refinement. */
250 /* (Note that the gaps might change through refining the */
251 /* eigenvalues, however, they can only get bigger.) */
252 /* Remove it from the list. */
253  iwork[k - 1] = -1;
254 /* Make sure that I1 always points to the first unconverged interval */
255  if (i__ == i1 && i__ < *ilast) {
256  i1 = i__ + 1;
257  }
258  if (prev >= i1 && i__ <= *ilast) {
259  iwork[(prev << 1) - 1] = i__ + 1;
260  }
261  } else {
262 /* unconverged interval found */
263  prev = i__;
264  ++nint;
265  iwork[k - 1] = i__ + 1;
266  iwork[k] = negcnt;
267  }
268  work[k - 1] = left;
269  work[k] = right;
270 /* L75: */
271  }
272 
273 /* Do while( NINT.GT.0 ), i.e. there are still unconverged intervals */
274 /* and while (ITER.LT.MAXITR) */
275 
276  iter = 0;
277 L80:
278  prev = i1 - 1;
279  i__ = i1;
280  olnint = nint;
281  i__1 = olnint;
282  for (ip = 1; ip <= i__1; ++ip) {
283  k = i__ << 1;
284  ii = i__ - *offset;
285  rgap = wgap[ii];
286  lgap = rgap;
287  if (ii > 1) {
288  lgap = wgap[ii - 1];
289  }
290  gap = minMACRO(lgap,rgap);
291  next = iwork[k - 1];
292  left = work[k - 1];
293  right = work[k];
294  mid = (left + right) * .5;
295 /* semiwidth of interval */
296  width = right - mid;
297 /* Computing MAX */
298  d__1 = absMACRO(left), d__2 = absMACRO(right);
299  tmp = maxMACRO(d__1,d__2);
300 /* Computing MAX */
301  d__1 = *rtol1 * gap, d__2 = *rtol2 * tmp;
302  cvrgd = maxMACRO(d__1,d__2);
303  if (width <= cvrgd || width <= mnwdth || iter == maxitr) {
304 /* reduce number of unconverged intervals */
305  --nint;
306 /* Mark interval as converged. */
307  iwork[k - 1] = 0;
308  if (i1 == i__) {
309  i1 = next;
310  } else {
311 /* Prev holds the last unconverged interval previously examined */
312  if (prev >= i1) {
313  iwork[(prev << 1) - 1] = next;
314  }
315  }
316  i__ = next;
317  goto L100;
318  }
319  prev = i__;
320 
321 /* Perform one bisection step */
322 
323  negcnt = template_lapack_laneg(n, &d__[1], &lld[1], &mid, pivmin, &r__);
324  if (negcnt <= i__ - 1) {
325  work[k - 1] = mid;
326  } else {
327  work[k] = mid;
328  }
329  i__ = next;
330 L100:
331  ;
332  }
333  ++iter;
334 /* do another loop if there are still unconverged intervals */
335 /* However, in the last iteration, all intervals are accepted */
336 /* since this is the best we can do. */
337  if (nint > 0 && iter <= maxitr) {
338  goto L80;
339  }
340 
341 
342 /* At this point, all the intervals have converged */
343  i__1 = *ilast;
344  for (i__ = *ifirst; i__ <= i__1; ++i__) {
345  k = i__ << 1;
346  ii = i__ - *offset;
347 /* All intervals marked by '0' have been refined. */
348  if (iwork[k - 1] == 0) {
349  w[ii] = (work[k - 1] + work[k]) * .5;
350  werr[ii] = work[k] - w[ii];
351  }
352 /* L110: */
353  }
354 
355  i__1 = *ilast;
356  for (i__ = *ifirst + 1; i__ <= i__1; ++i__) {
357  k = i__ << 1;
358  ii = i__ - *offset;
359 /* Computing MAX */
360  d__1 = 0., d__2 = w[ii] - werr[ii] - w[ii - 1] - werr[ii - 1];
361  wgap[ii - 1] = maxMACRO(d__1,d__2);
362 /* L111: */
363  }
364  return 0;
365 
366 /* End of DLARRB */
367 
368 } /* dlarrb_ */
369 
370 #endif