Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#include "slu_ddefs.h"
Toshihiro Shimizu 890ddd
#include "slu_util.h"
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <algorithm></algorithm>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tlin/tlin_cblas_wrap.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tlin/tlin_superlu_wrap.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************
Toshihiro Shimizu 890ddd
//    Preliminaries
Toshihiro Shimizu 890ddd
//*************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
struct tlin::SuperMatrix final : public ::SuperMatrix {};
Shinya Kitaoka d1f6c4
struct tlin::superlu_options_t final : public ::superlu_options_t {};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
static const tlin::spmat::HashMap::size_t neg = -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool initialized = false;
Toshihiro Shimizu 890ddd
static tlin::superlu_options_t defaultOpt;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct DefaultOptsInitializer {
Shinya Kitaoka 120a6e
  DefaultOptsInitializer() {
Shinya Kitaoka 120a6e
    set_default_options(&defaultOpt);
Shinya Kitaoka 120a6e
    defaultOpt.PrintStat = NO;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
} _instance;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline bool rowLess(const tlin::spmat::HashMap::BucketNode *a,
Shinya Kitaoka 120a6e
                    const tlin::spmat::HashMap::BucketNode *b) {
Shinya Kitaoka 120a6e
  return a->m_key.first < b->m_key.first;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************
Toshihiro Shimizu 890ddd
//    SuperLU-specific Functions
Toshihiro Shimizu 890ddd
//*************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::allocS(SuperMatrix *&A, int rows, int cols, int nnz) {
Shinya Kitaoka 120a6e
  A              = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix));
Shinya Kitaoka 120a6e
  double *values = doubleMalloc(nnz);
Shinya Kitaoka 120a6e
  int *rowind    = intMalloc(nnz);
Shinya Kitaoka 120a6e
  int *colptr    = intMalloc(cols + 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  dCreate_CompCol_Matrix(A, rows, cols, nnz, values, rowind, colptr, SLU_NC,
Shinya Kitaoka 120a6e
                         SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::allocD(SuperMatrix *&A, int rows, int cols) {
Shinya Kitaoka 120a6e
  A = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double *values = doubleMalloc(rows * cols * sizeof(double));
Shinya Kitaoka 120a6e
  dCreate_Dense_Matrix(A, rows, cols, values, rows, SLU_DN, SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::allocS(SuperMatrix *&A, int rows, int cols, int nnz, int *colptr,
Shinya Kitaoka 120a6e
                  int *rowind, double *values) {
Shinya Kitaoka 120a6e
  A = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix));
Shinya Kitaoka 120a6e
  dCreate_CompCol_Matrix(A, rows, cols, nnz, values, rowind, colptr, SLU_NC,
Shinya Kitaoka 120a6e
                         SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::allocD(SuperMatrix *&A, int rows, int cols, int lda,
Shinya Kitaoka 120a6e
                  double *values) {
Shinya Kitaoka 120a6e
  A = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix));
Shinya Kitaoka 120a6e
  dCreate_Dense_Matrix(A, rows, cols, values, lda, SLU_DN, SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::freeS(SuperMatrix *m) {
Shinya Kitaoka 120a6e
  if (!m) return;
Shinya Kitaoka 120a6e
  Destroy_CompCol_Matrix(m);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(m);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::freeD(SuperMatrix *m) {
Shinya Kitaoka 120a6e
  if (!m) return;
Shinya Kitaoka 120a6e
  Destroy_Dense_Matrix(m);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(m);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::createS(SuperMatrix &A, int rows, int cols, int nnz) {
Shinya Kitaoka 120a6e
  double *values = doubleMalloc(nnz);
Shinya Kitaoka 120a6e
  int *rowind    = intMalloc(nnz);
Shinya Kitaoka 120a6e
  int *colptr    = intMalloc(cols + 1);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  dCreate_CompCol_Matrix(&A, rows, cols, nnz, values, rowind, colptr, SLU_NC,
Shinya Kitaoka 120a6e
                         SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::createD(SuperMatrix &A, int rows, int cols) {
Shinya Kitaoka 120a6e
  double *values = doubleMalloc(rows * cols * sizeof(double));
Shinya Kitaoka 120a6e
  dCreate_Dense_Matrix(&A, rows, cols, values, rows, SLU_DN, SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::createS(SuperMatrix &A, int rows, int cols, int nnz, int *colptr,
Shinya Kitaoka 120a6e
                   int *rowind, double *values) {
Shinya Kitaoka 120a6e
  dCreate_CompCol_Matrix(&A, rows, cols, nnz, values, rowind, colptr, SLU_NC,
Shinya Kitaoka 120a6e
                         SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::createD(SuperMatrix &A, int rows, int cols, int lda,
Shinya Kitaoka 120a6e
                   double *values) {
Shinya Kitaoka 120a6e
  dCreate_Dense_Matrix(&A, rows, cols, values, lda, SLU_DN, SLU_D, SLU_GE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::destroyS(SuperMatrix &A, bool destroyData) {
Shinya Kitaoka 120a6e
  if (destroyData)
Shinya Kitaoka 120a6e
    Destroy_CompCol_Matrix(&A);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    SUPERLU_FREE(A.Store);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::destroyD(SuperMatrix &A, bool destroyData) {
Shinya Kitaoka 120a6e
  if (destroyData)
Shinya Kitaoka 120a6e
    Destroy_Dense_Matrix(&A);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    SUPERLU_FREE(A.Store);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::freeF(SuperFactors *F) {
Shinya Kitaoka 120a6e
  if (!F) return;
Shinya Kitaoka 120a6e
  Destroy_SuperNode_Matrix(F->L);
Shinya Kitaoka 120a6e
  Destroy_CompCol_Matrix(F->U);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(F->L);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(F->U);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(F->perm_r);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(F->perm_c);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(F);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::readDN(SuperMatrix *A, int &lda, double *&values) {
Shinya Kitaoka 120a6e
  assert(A->Stype == SLU_DN);
Shinya Kitaoka 120a6e
  DNformat *storage = (DNformat *)A->Store;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  lda    = storage->lda;
Shinya Kitaoka 120a6e
  values = (double *)storage->nzval;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::readNC(SuperMatrix *A, int &nnz, int *&colptr, int *&rowind,
Shinya Kitaoka 120a6e
                  double *&values) {
Shinya Kitaoka 120a6e
  assert(A->Stype == SLU_NC);  // Only SLU_NC (CCS) format is supported here
Shinya Kitaoka 120a6e
  NCformat *storage = (NCformat *)A->Store;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  nnz    = storage->nnz;
Shinya Kitaoka 120a6e
  values = (double *)storage->nzval;
Shinya Kitaoka 120a6e
  rowind = (int *)storage->rowind;
Shinya Kitaoka 120a6e
  colptr = (int *)storage->colptr;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::traduceD(const mat &m, SuperMatrix *&A) {
Shinya Kitaoka 120a6e
  int rows = m.rows(), cols = m.cols();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!A) allocD(A, rows, cols);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int lda;
Shinya Kitaoka 120a6e
  double *Avalues = 0;
Shinya Kitaoka 120a6e
  readDN(A, lda, Avalues);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(A->nrow == rows && A->ncol == cols && lda == rows);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  memcpy(Avalues, m.values(), rows * cols * sizeof(double));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::traduceS(spmat &m, SuperMatrix *&A) {
Shinya Kitaoka 120a6e
  int rows = m.rows(), cols = m.cols(), nnz = (int)m.entries().size();
Shinya Kitaoka 120a6e
  spmat::HashMap &entries = m.entries();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Build or extract pointers to out's data
Shinya Kitaoka 120a6e
  double *values;
Shinya Kitaoka 120a6e
  int *rowind, *colptr;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!A) allocS(A, rows, cols, nnz);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Retrieve NC arrays from A
Shinya Kitaoka 120a6e
  int Annz;
Shinya Kitaoka 120a6e
  readNC(A, Annz, colptr, rowind, values);
Shinya Kitaoka 120a6e
  assert(A->nrow == rows && A->ncol == cols && Annz == nnz);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Rehash to cols buckets
Shinya Kitaoka 120a6e
  if (entries.hashFunctor().m_cols != cols) entries.hashFunctor().m_cols = cols;
Shinya Kitaoka 120a6e
  entries.rehash(cols);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Copy each bucket to the corresponding col
Shinya Kitaoka 120a6e
  const std::vector<spmat::hashmap::size_t> &buckets = m.entries().buckets();</spmat::hashmap::size_t>
Shinya Kitaoka 120a6e
  const tcg::list<spmat::hashmap::bucketnode> &nodes = m.entries().items();</spmat::hashmap::bucketnode>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::vector<const *="" spmat::hashmap::bucketnode=""> colEntries;</const>
Shinya Kitaoka 120a6e
  std::vector<const *="" spmat::hashmap::bucketnode="">::size_type j, size;</const>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double *currVal = values;
Shinya Kitaoka 120a6e
  int *currRowind = rowind;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (int i = 0; i < cols; ++i) {
Shinya Kitaoka 120a6e
    colptr[i]                      = (int)(currVal - values);
Shinya Kitaoka 120a6e
    spmat::HashMap::size_t nodeIdx = buckets[i];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Retrieve all column entry pointers.
Shinya Kitaoka 120a6e
    colEntries.clear();
Shinya Kitaoka 120a6e
    while (nodeIdx != neg) {
Shinya Kitaoka 120a6e
      const spmat::HashMap::BucketNode &node = nodes[nodeIdx];
Shinya Kitaoka 120a6e
      colEntries.push_back(&node);
Shinya Kitaoka 120a6e
      nodeIdx = nodes[nodeIdx].m_next;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Sort them by row
Shinya Kitaoka 120a6e
    std::sort(colEntries.begin(), colEntries.end(), rowLess);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Finally, write them in the SuperMatrix
Shinya Kitaoka 120a6e
    size = colEntries.size();
Shinya Kitaoka 120a6e
    for (j = 0; j < size; ++j) {
Shinya Kitaoka 120a6e
      const spmat::HashMap::BucketNode &node = *colEntries[j];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      *currRowind = node.m_key.first;
Shinya Kitaoka 120a6e
      *currVal    = node.m_val;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      ++currVal, ++currRowind;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  colptr[cols] = nnz;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::traduceD(const tlin::sparse_matrix<double> &m, SuperMatrix *&A) {</double>
Shinya Kitaoka 120a6e
  int rows = m.rows(), cols = m.cols();
Shinya Kitaoka 120a6e
  const spmat::HashMap &entries = m.entries();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Build or extract pointers to out's data
Shinya Kitaoka 120a6e
  double *values;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!A) allocD(A, rows, cols);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Retrieve DN arrays from A
Shinya Kitaoka 120a6e
  int lda;
Shinya Kitaoka 120a6e
  readDN(A, lda, values);
Shinya Kitaoka 120a6e
  assert(A->nrow == rows && A->ncol == cols && lda == rows);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Copy each value in entries to A
Shinya Kitaoka 120a6e
  spmat::HashMap::const_iterator it;
Shinya Kitaoka 120a6e
  for (it = entries.begin(); it != entries.end(); ++it)
Shinya Kitaoka 120a6e
    values[it->m_key.second * rows + it->m_key.first] = it->m_val;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::factorize(SuperMatrix *A, SuperFactors *&F, superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  assert(A->nrow == A->ncol);
Shinya Kitaoka 120a6e
  int n = A->nrow;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!F) F = (SuperFactors *)SUPERLU_MALLOC(sizeof(SuperFactors));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!opt) opt = &defaultOpt;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  F->perm_c = intMalloc(n);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  get_perm_c(3, A, F->perm_c);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperMatrix AC;
Shinya Kitaoka 120a6e
  int *etree = intMalloc(n);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  sp_preorder(opt, A, F->perm_c, etree, &AC);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  F->L      = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix));
Shinya Kitaoka 120a6e
  F->U      = (SuperMatrix *)SUPERLU_MALLOC(sizeof(SuperMatrix));
Shinya Kitaoka 120a6e
  F->perm_r = intMalloc(n);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperLUStat_t stat;
Shinya Kitaoka 120a6e
  StatInit(&stat);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int result;
Shinya Kitaoka 120a6e
  dgstrf(opt, &AC, sp_ienv(1), sp_ienv(2), etree, NULL, 0, F->perm_c, F->perm_r,
Shinya Kitaoka 120a6e
         F->L, F->U, &stat, &result);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  StatFree(&stat);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Destroy_CompCol_Permuted(&AC);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(etree);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (result != 0) freeF(F), F = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperFactors *F, SuperMatrix *BX, superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  assert(F);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!opt) opt = &defaultOpt;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperLUStat_t stat;
Shinya Kitaoka 120a6e
  StatInit(&stat);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int result;
Shinya Kitaoka 120a6e
  dgstrs(NOTRANS, F->L, F->U, F->perm_c, F->perm_r, BX, &stat, &result);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  StatFree(&stat);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperFactors *F, SuperMatrix *B, SuperMatrix *&X,
Shinya Kitaoka 120a6e
                 superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  if (!X) allocD(X, B->nrow, B->ncol);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double *Bvalues = 0, *Xvalues = 0;
Shinya Kitaoka 120a6e
  int lda;
Shinya Kitaoka 120a6e
  readDN(B, lda, Bvalues);
Shinya Kitaoka 120a6e
  readDN(X, lda, Xvalues);
Shinya Kitaoka 120a6e
  memcpy(Xvalues, Bvalues, B->nrow * B->ncol * sizeof(double));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  solve(F, X, opt);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperMatrix *A, SuperMatrix *BX, superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  assert(A->nrow == A->ncol);
Shinya Kitaoka 120a6e
  int n = A->nrow;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!opt) opt = &defaultOpt;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperMatrix L, U;
Shinya Kitaoka 120a6e
  int *perm_c, *perm_r;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  perm_c = intMalloc(n);
Shinya Kitaoka 120a6e
  perm_r = intMalloc(n);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperLUStat_t stat;
Shinya Kitaoka 120a6e
  StatInit(&stat);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int result;
Shinya Kitaoka 120a6e
  dgssv(opt, A, perm_c, perm_r, &L, &U, BX, &stat, &result);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Destroy_SuperNode_Matrix(&L);
Shinya Kitaoka 120a6e
  Destroy_CompCol_Matrix(&U);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(perm_r);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(perm_c);
Shinya Kitaoka 120a6e
  StatFree(&stat);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperMatrix *A, SuperMatrix *B, SuperMatrix *&X,
Shinya Kitaoka 120a6e
                 superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  if (!X) allocD(X, B->nrow, B->ncol);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double *Bvalues = 0, *Xvalues = 0;
Shinya Kitaoka 120a6e
  int lda;
Shinya Kitaoka 120a6e
  readDN(B, lda, Bvalues);
Shinya Kitaoka 120a6e
  readDN(X, lda, Xvalues);
Shinya Kitaoka 120a6e
  memcpy(Xvalues, Bvalues, B->nrow * B->ncol * sizeof(double));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  solve(A, X, opt);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperFactors *F, double *bx, superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  SuperMatrix BX;
Shinya Kitaoka 120a6e
  int rows = F->L->nrow;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  createD(BX, rows, 1, rows, bx);
Shinya Kitaoka 120a6e
  tlin::solve(F, &BX, opt);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(BX.Store);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperFactors *F, double *b, double *&x,
Shinya Kitaoka 120a6e
                 superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  SuperMatrix B, X;
Shinya Kitaoka 120a6e
  int rows = F->L->nrow;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!x) x = (double *)malloc(rows * sizeof(double));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  createD(B, rows, 1, rows, b);
Shinya Kitaoka 120a6e
  createD(X, rows, 1, rows, x);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperMatrix *Xptr = &X;  //&X is const
Shinya Kitaoka 120a6e
  tlin::solve(F, &B, Xptr, opt);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SUPERLU_FREE(B.Store);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(X.Store);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperMatrix *A, double *bx, superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  SuperMatrix BX;
Shinya Kitaoka 120a6e
  int rows = A->nrow;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  createD(BX, rows, 1, rows, bx);
Shinya Kitaoka 120a6e
  tlin::solve(A, &BX, opt);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(BX.Store);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::solve(SuperMatrix *A, double *b, double *&x,
Shinya Kitaoka 120a6e
                 superlu_options_t *opt) {
Shinya Kitaoka 120a6e
  SuperMatrix B, X;
Shinya Kitaoka 120a6e
  int rows = A->nrow;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!x) x = (double *)malloc(rows * sizeof(double));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  createD(B, rows, 1, rows, b);
Shinya Kitaoka 120a6e
  createD(X, rows, 1, rows, x);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperMatrix *Xptr = &X;  //&X is const
Shinya Kitaoka 120a6e
  tlin::solve(A, &B, Xptr, opt);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(B.Store);
Shinya Kitaoka 120a6e
  SUPERLU_FREE(X.Store);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************
Toshihiro Shimizu 890ddd
//    BLAS-related Functions
Toshihiro Shimizu 890ddd
//*************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::multiplyS(const SuperMatrix *A, const double *x, double *&y) {
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
int sp_dgemv (char *, double, SuperMatrix *, double *,
Shinya Kitaoka 120a6e
                  int, double, double *, int);
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!y) {
Shinya Kitaoka 120a6e
    y = (double *)malloc(A->nrow * sizeof(double));
Shinya Kitaoka 120a6e
    memset(y, 0, A->nrow * sizeof(double));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  SuperMatrix *_A = const_cast<supermatrix *="">(A);</supermatrix>
Shinya Kitaoka 120a6e
  double *_x      = const_cast<double *="">(x);</double>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  sp_dgemv("N", 1.0, _A, _x, 1, 1.0, y, 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void tlin::multiplyD(const SuperMatrix *A, const double *x, double *&y) {
Shinya Kitaoka 120a6e
  int lda;
Shinya Kitaoka 120a6e
  double *values;
Shinya Kitaoka 120a6e
  tlin::readDN(const_cast<supermatrix *="">(A), lda, values);</supermatrix>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tlin::multiply(A->nrow, A->ncol, values, x, y);
Toshihiro Shimizu 890ddd
}