sparse_solve.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <sfdpgen/sparse_solve.h>
  13. #include <sfdpgen/sfdp.h>
  14. #include <math.h>
  15. #include <common/arith.h>
  16. #include <common/types.h>
  17. #include <common/globals.h>
  18. #include <util/alloc.h>
  19. /* #define DEBUG_PRINT */
  20. static double *diag_precon(const double *diag, double *x, double *y) {
  21. int i, m;
  22. m = (int) diag[0];
  23. diag++;
  24. for (i = 0; i < m; i++) y[i] = x[i]*diag[i];
  25. return y;
  26. }
  27. static double *diag_precon_new(SparseMatrix A) {
  28. int i, j, m = A->m, *ia = A->ia, *ja = A->ja;
  29. double *a = A->a;
  30. assert(A->type == MATRIX_TYPE_REAL);
  31. assert(a);
  32. double *data = gv_calloc(A->m + 1, sizeof(double));
  33. double *diag = data;
  34. diag[0] = m;
  35. diag++;
  36. for (i = 0; i < m; i++){
  37. diag[i] = 1.;
  38. for (j = ia[i]; j < ia[i+1]; j++){
  39. if (i == ja[j] && fabs(a[j]) > 0) diag[i] = 1./a[j];
  40. }
  41. }
  42. return data;
  43. }
  44. static double conjugate_gradient(SparseMatrix A, const double *precon, int n,
  45. double *x, double *rhs, double tol,
  46. double maxit) {
  47. double res, alpha;
  48. double rho, rho_old = 1, res0, beta;
  49. int iter = 0;
  50. double *z = gv_calloc(n, sizeof(double));
  51. double *r = gv_calloc(n, sizeof(double));
  52. double *p = gv_calloc(n, sizeof(double));
  53. double *q = gv_calloc(n, sizeof(double));
  54. SparseMatrix_multiply_vector(A, x, &r);
  55. r = vector_subtract_to(n, rhs, r);
  56. res0 = res = sqrt(vector_product(n, r, r))/n;
  57. #ifdef DEBUG_PRINT
  58. if (Verbose){
  59. fprintf(stderr,
  60. "on entry, cg iter = %d of %.0f, residual = %g, tol = %g\n",
  61. iter, maxit, res, tol);
  62. }
  63. #endif
  64. while ((iter++) < maxit && res > tol*res0){
  65. z = diag_precon(precon, r, z);
  66. rho = vector_product(n, r, z);
  67. if (iter > 1){
  68. beta = rho/rho_old;
  69. p = vector_saxpy(n, z, p, beta);
  70. } else {
  71. memcpy(p, z, sizeof(double)*n);
  72. }
  73. SparseMatrix_multiply_vector(A, p, &q);
  74. alpha = rho/vector_product(n, p, q);
  75. x = vector_saxpy2(n, x, p, alpha);
  76. r = vector_saxpy2(n, r, q, -alpha);
  77. res = sqrt(vector_product(n, r, r))/n;
  78. rho_old = rho;
  79. }
  80. free(z); free(r); free(p); free(q);
  81. #ifdef DEBUG
  82. _statistics[0] += iter - 1;
  83. #endif
  84. #ifdef DEBUG_PRINT
  85. if (Verbose){
  86. fprintf(stderr, " cg iter = %d, residual = %g, relative res = %g\n", iter, res, res/res0);
  87. }
  88. #endif
  89. return res;
  90. }
  91. static double cg(SparseMatrix A, const double *precond, int n, int dim,
  92. double *x0, double *rhs, double tol, double maxit) {
  93. double res = 0;
  94. int k, i;
  95. double *x = gv_calloc(n, sizeof(double));
  96. double *b = gv_calloc(n, sizeof(double));
  97. for (k = 0; k < dim; k++){
  98. for (i = 0; i < n; i++) {
  99. x[i] = x0[i*dim+k];
  100. b[i] = rhs[i*dim+k];
  101. }
  102. res += conjugate_gradient(A, precond, n, x, b, tol, maxit);
  103. for (i = 0; i < n; i++) {
  104. rhs[i*dim+k] = x[i];
  105. }
  106. }
  107. free(x);
  108. free(b);
  109. return res;
  110. }
  111. double SparseMatrix_solve(SparseMatrix A, int dim, double *x0, double *rhs,
  112. double tol, double maxit) {
  113. int n = A->m;
  114. double *precond = diag_precon_new(A);
  115. double res = cg(A, precond, n, dim, x0, rhs, tol, maxit);
  116. free(precond);
  117. return res;
  118. }