SparseLU_heap_relax_snode.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /* This file is a modified version of heap_relax_snode.c file in SuperLU
  10. * -- SuperLU routine (version 3.0) --
  11. * Univ. of California Berkeley, Xerox Palo Alto Research Center,
  12. * and Lawrence Berkeley National Lab.
  13. * October 15, 2003
  14. *
  15. * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
  16. *
  17. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
  18. * EXPRESSED OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  19. *
  20. * Permission is hereby granted to use or copy this program for any
  21. * purpose, provided the above notices are retained on all copies.
  22. * Permission to modify the code and to distribute modified code is
  23. * granted, provided the above notices are retained, and a notice that
  24. * the code was modified is included with the above copyright notice.
  25. */
  26. #ifndef SPARSELU_HEAP_RELAX_SNODE_H
  27. #define SPARSELU_HEAP_RELAX_SNODE_H
  28. namespace Eigen {
  29. namespace internal {
  30. /**
  31. * \brief Identify the initial relaxed supernodes
  32. *
  33. * This routine applied to a symmetric elimination tree.
  34. * It assumes that the matrix has been reordered according to the postorder of the etree
  35. * \param n The number of columns
  36. * \param et elimination tree
  37. * \param relax_columns Maximum number of columns allowed in a relaxed snode
  38. * \param descendants Number of descendants of each node in the etree
  39. * \param relax_end last column in a supernode
  40. */
  41. template <typename Scalar, typename StorageIndex>
  42. void SparseLUImpl<Scalar,StorageIndex>::heap_relax_snode (const Index n, IndexVector& et, const Index relax_columns, IndexVector& descendants, IndexVector& relax_end)
  43. {
  44. // The etree may not be postordered, but its heap ordered
  45. IndexVector post;
  46. internal::treePostorder(StorageIndex(n), et, post); // Post order etree
  47. IndexVector inv_post(n+1);
  48. for (StorageIndex i = 0; i < n+1; ++i) inv_post(post(i)) = i; // inv_post = post.inverse()???
  49. // Renumber etree in postorder
  50. IndexVector iwork(n);
  51. IndexVector et_save(n+1);
  52. for (Index i = 0; i < n; ++i)
  53. {
  54. iwork(post(i)) = post(et(i));
  55. }
  56. et_save = et; // Save the original etree
  57. et = iwork;
  58. // compute the number of descendants of each node in the etree
  59. relax_end.setConstant(emptyIdxLU);
  60. Index j, parent;
  61. descendants.setZero();
  62. for (j = 0; j < n; j++)
  63. {
  64. parent = et(j);
  65. if (parent != n) // not the dummy root
  66. descendants(parent) += descendants(j) + 1;
  67. }
  68. // Identify the relaxed supernodes by postorder traversal of the etree
  69. Index snode_start; // beginning of a snode
  70. StorageIndex k;
  71. Index nsuper_et_post = 0; // Number of relaxed snodes in postordered etree
  72. Index nsuper_et = 0; // Number of relaxed snodes in the original etree
  73. StorageIndex l;
  74. for (j = 0; j < n; )
  75. {
  76. parent = et(j);
  77. snode_start = j;
  78. while ( parent != n && descendants(parent) < relax_columns )
  79. {
  80. j = parent;
  81. parent = et(j);
  82. }
  83. // Found a supernode in postordered etree, j is the last column
  84. ++nsuper_et_post;
  85. k = StorageIndex(n);
  86. for (Index i = snode_start; i <= j; ++i)
  87. k = (std::min)(k, inv_post(i));
  88. l = inv_post(j);
  89. if ( (l - k) == (j - snode_start) ) // Same number of columns in the snode
  90. {
  91. // This is also a supernode in the original etree
  92. relax_end(k) = l; // Record last column
  93. ++nsuper_et;
  94. }
  95. else
  96. {
  97. for (Index i = snode_start; i <= j; ++i)
  98. {
  99. l = inv_post(i);
  100. if (descendants(i) == 0)
  101. {
  102. relax_end(l) = l;
  103. ++nsuper_et;
  104. }
  105. }
  106. }
  107. j++;
  108. // Search for a new leaf
  109. while (descendants(j) != 0 && j < n) j++;
  110. } // End postorder traversal of the etree
  111. // Recover the original etree
  112. et = et_save;
  113. }
  114. } // end namespace internal
  115. } // end namespace Eigen
  116. #endif // SPARSELU_HEAP_RELAX_SNODE_H