#
# itpack solve 
#
@PROBLEM itpack_solve
@INCLUDE <math.h>
@FUNCTION itpack_solve

@DASHI $(MPI_INCLUDE_DIR)
@DASHI $(NETSOLVE_ROOT)/src/SampleNumericalSoftware/SparseSolvers/sparse
@DASHI $(NETSOLVE_ROOT)/src/SampleNumericalSoftware/SparseSolvers/sparse/aux
@DASHI $(NETSOLVE_ROOT)/src/SampleNumericalSoftware/SparseSolvers/sparse/itpack
@INCLUDE "mpi.h"
@INCLUDE "sparse_globals.h"
@INCLUDE "itpack.h"
@INCLUDE "itpack_auxs.h"
@INCLUDE "sparse_auxs.h"
@INCLUDE "matrix_auxs.h"
@INCLUDE "itpack/netsolve_itpack.h"

@LIB -lm
@LIB -L$(NETSOLVE_ROOT)/src/SampleNumericalSoftware/SparseSolvers/sparse/lib/$(NETSOLVE_ARCH)
@LIB -lnetsolve_itpack
@LIB -lnetsolve_aux_distr
@LIB -lnetsolve_tester
@LIB -lnetsolve_aux
@LIB $(NETSOLVE_ROOT)/lib/$(NETSOLVE_ARCH)/itpack_$(NETSOLVE_ARCH).a
@LIB -L$(MPI_DIR)/lib
@LIB -lmpich
@LIB -ldl
@LIB -lc

@LANGUAGE FORTRAN
@MAJOR ROW
@PATH /ItPack/
@COMPLEXITY 2,4
@DESCRIPTION
This subroutine calls the ITPACK JCG (Jacobi conjugate
gradient) algorithm.
http://www.netlib.org/itpack/index.html
@PARALLEL MPI

@INPUT 4
@OBJECT SPARSEMATRIX D sm
the sparse matrix
@OBJECT VECTOR D rhs_vector
the right-hand-side vector
@OBJECT SCALAR D rtol
error value
@OBJECT SCALAR I maxit
maximum number of iterations

@OUTPUT 2
@OBJECT VECTOR D sol_vector
solution vector
@OBJECT SCALAR I iterations
iterations converged

@CALLINGSEQUENCE
@ARG mI0,nI0,mI1
@ARG fI0
@ARG I0
@ARG iI0
@ARG pI0
@ARG I1
@ARG I2
@ARG I3
@ARG O0
@ARG O1

@CODE
extern int itpack_params(direct_info_block *info, double rtol, int maxit);
int i;
int ierr;
int first=1;
int conv, its;
iterative_info_block info=0;
MPI_Comm comm= MPI_COMM_WORLD;

  if(@pI0@[0] == 0)
    ierr = crs_mat_tobase1(@I0@, @iI0@, @pI0@, *@mI0@);

  ierr = itpack_params(&info,*@I2@,*@I3@);

  @O0@ = (double*)malloc(*@mI0@*sizeof(double));
  @O1@ = (int*)malloc(sizeof(int));
  *@mO0@ = *@mI1@;

  for(i=0; i<*@mI1@; i++)
    @O0@[i] = 0.0;
  /* we should probably change this in the future version of netsolve to
     give a starting vector */

  ierr = itpack_iterative_driver
    (comm, @I0@, @iI0@, @pI0@, first, *@mI0@, @I1@, @O0@, info);ERR_RETURN(ierr);

  ierr = itpack_get_return_params(info,&conv,&its);

  if (conv) {
    printf("The method converged in %d iterations\\n",its);
    *@O1@ = its;
  } else {
    printf("No convergence in the number of iterations specified\\n");
  }

@END_CODE
