# Iterative Sparse Solver Code Examples

This section contains example code in FORTRAN 77 and C. For description of the iterative sparse solver routines based on the reverse communication interface (RCI ISS) used in this code, refer to “Iterative Sparse Solvers based on Reverse Communication Interface (RCI ISS)” in “Sparse Solver Routines”.

## Example of Use RCI (Preconditioned) Conjugate Gradient Solver for Systems with Single Right Hand Size

Example results for symmetric positive definite systems. Upon successful execution of the solver, the result of the solution array is as follows:

`The system is successfully solved`
`The following solution obtained`
`1.000 0.000 1.000 0.000`
`1.000 0.000 1.000 0.000`
`Expected solution`
`1.000 0.000 1.000 0.000`
`1.000 0.000 1.000 0.000`
`Number of iterations: 8`

## Example. FORTRAN 77 Example to Solve Symmetric Positive Definite System

****************************************************************************

* INTEL CONFIDENTIAL

* Copyright(C) 2005-2008 Intel Corporation. All Rights Reserved.

* The source code contained or described herein and all documents related to

* the source code ("Material") are owned by Intel Corporation or its suppliers

* or licensors. Title to the Material remains with Intel Corporation or its

* suppliers and licensors. The Material contains trade secrets and proprietary

* and confidential information of Intel or its suppliers and licensors. The

* Material is protected by worldwide copyright and trade secret laws and

* treaty provisions. No part of the Material may be used, copied, reproduced,

* modified, published, uploaded, posted, transmitted, distributed or disclosed

* in any way without Intel's prior express written permission.

* No license under any patent, copyright, trade secret or other intellectual

* property right is granted to or conferred upon you by disclosure or delivery

* of the Materials, either expressly, by implication, inducement, estoppel or

* otherwise. Any license under such intellectual property rights must be

* express and approved by Intel in writing.

*

****************************************************************************

*

* Content : Intel MKL RCI (P)CG Fortran 77 example

*

****************************************************************************

*

C---------------------------------------------------------------------------

C Example program for solving symmetric positive definite system of

C equations.

C---------------------------------------------------------------------------

PROGRAM rci_pcg_f77_test

IMPLICIT NONE

C---------------------------------------------------------------------------

C Define arrays for the upper triangle of the coefficient matrix and rhs vector

C Compressed sparse row storage is used for sparse representation

C---------------------------------------------------------------------------

INTEGER N, RCI_request, itercount, i

PARAMETER (N=8)

DOUBLE PRECISION rhs(N), solution(N)

INTEGER ia(9)

INTEGER ja(18)

DOUBLE PRECISION a(18)

C Fill all arrays containing matrix data.

DATA ia /1,5,8,10,12,15,17,18,19/

DATA ja

1 /1, 3, 6,7,

2 2, 3, 5,

3 3, 8,

4 4, 7,

5 5,6,7,

6 6, 8,

7 7,

8 8/

DATA a

1 /7.D0, 1.D0, 2.D0, 7.D0,

2 -4.D0, 8.D0, 2.D0,

3 1.D0, 5.D0,

4 7.D0, 9.D0,

5 5.D0, 1.D0, 5.D0,

6 -1.D0, 5.D0,

7 11.D0,

8 5.D0/

C---------------------------------------------------------------------------

C Allocate storage for the solver ?par and the initial solution vector

C---------------------------------------------------------------------------

INTEGER length

PARAMETER (length=128)

DOUBLE PRECISION expected(N)

DATA expected/1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0/

INTEGER ipar(length)

DOUBLE PRECISION dpar(length),tmp(N,4)

C---------------------------------------------------------------------------

C Initialize the right hand side through matrix-vector product

C---------------------------------------------------------------------------

CALL DCSRMV_SY('U', N, A, IA, JA, expected, rhs)

C---------------------------------------------------------------------------

C Initialize the initial guess

C---------------------------------------------------------------------------

DO I=1, N

solution(I)=1.D0

ENDDO

C---------------------------------------------------------------------------

C Initialize the solver

C---------------------------------------------------------------------------

CALL dcg_init(N,solution,rhs,RCI_request,ipar,dpar,tmp)

IF (RCI_request .NE. 0 ) GOTO 999

C---------------------------------------------------------------------------

C Set the desired parameters:

C LOGICAL parameters:

C do residual stopping test

C do not request for the user defined stopping test

C do Preconditioned Conjugate Gradient iterations

C DOUBLE PRECISION parameters

C set the relative tolerance to 1.0D-5 instead of default value 1.0D-6

C---------------------------------------------------------------------------

ipar(9)=1

ipar(10)=0

ipar(11)=1

dpar(1)=1.D-5

C---------------------------------------------------------------------------

C Check the correctness and consistency of the newly set parameters

C---------------------------------------------------------------------------

CALL dcg_check(N,solution,rhs,RCI_request,ipar,dpar,tmp)

IF (RCI_request .NE. 0 ) GOTO 999

C---------------------------------------------------------------------------

C Compute the solution by RCI PCG solver

C Reverse Communications starts here

C---------------------------------------------------------------------------

1 CALL dcg(N,solution,rhs,RCI_request,ipar,dpar,tmp)

C---------------------------------------------------------------------------

C If RCI_request=0, then the solution was found with the required precision

C---------------------------------------------------------------------------

IF (RCI_request .EQ. 0) THEN

GOTO 700

C---------------------------------------------------------------------------

C If RCI_request=1, then compute the vector A*tmp(:,1)

C and put the result in vector tmp(:,2)

C---------------------------------------------------------------------------

ELSE IF (RCI_request .EQ. 1) THEN

CALL DCSRMV_SY('U', N, A, IA, JA, TMP, TMP(1, 2))

GOTO 1

C---------------------------------------------------------------------------

C If RCI_request=3, then compute vector preconditioner matrix on tmp(:,3)

C and put the result in vector tmp(:,4)

C---------------------------------------------------------------------------

ELSE IF (RCI_request .EQ. 3) THEN

CALL DCOPY(N, TMP(1,3),1, TMP(1, 4), 1)

GOTO 1

ELSE

C---------------------------------------------------------------------------

C If RCI_request=anything else, then dcg subroutine failed

C to compute the solution vector: solution(N)

C---------------------------------------------------------------------------

GOTO 999

ENDIF

C---------------------------------------------------------------------------

C Reverse Communication ends here

C Get the current iteration number

C---------------------------------------------------------------------------

700 CALL dcg_get(N,solution,rhs,RCI_request,ipar,dpar,tmp,

& itercount)

C---------------------------------------------------------------------------

C Print solution vector: solution(N) and number of iterations: itercount

C---------------------------------------------------------------------------

WRITE(*, *) ' The system is successfully solved '

WRITE(*, *) ' The following solution obtained '

WRITE(*,800)(solution(i),i =1,N)

WRITE(*, *) ' Expected solution '

WRITE(*,800)(expected(i),i =1,N)

800 FORMAT(4(F10.3))

WRITE(*,900)(itercount)

900 FORMAT(' Number of iterations: ',1(I2))

GOTO 1000

999 WRITE(*,*) 'Solver returned error code ', RCI_request

STOP

1000 CONTINUE

read *

END

## Examples of Use RCI (Preconditioned) Conjugate Gradient Solver for Systems with Multiple Right Hand Sizes (MRHS)

Example results for symmetric positive definite systems. Upon successful execution of the solver, the result of the solution array is as follows:

`The system is successfully solved`
`The following solution obtained`
`1.000 0.000 1.000 0.000`
`1.000 0.000 1.000 0.000`
`0.000 2.000 0.000 2.000`
`0.000 2.000 0.000 2.000`
`Expected solution`
`1.000 0.000 1.000 0.000`
`1.000 0.000 1.000 0.000`
`0.000 2.000 0.000 2.000`
`0.000 2.000 0.000 2.000`
``

## Example. Fortran Example to Solve Symmetric Positive Definite System with Multiple Right Hand Sizes (MRHS). No Preconditioning, No User-Defined Stopping Tests.

!*******************************************************************************

! INTEL CONFIDENTIAL

! Copyright(C) 2005-2008 Intel Corporation. All Rights Reserved.

! The source code contained or described herein and all documents related to

! the source code ("Material") are owned by Intel Corporation or its suppliers

! or licensors. Title to the Material remains with Intel Corporation or its

! suppliers and licensors. The Material contains trade secrets and proprietary

! and confidential information of Intel or its suppliers and licensors. The

! Material is protected by worldwide copyright and trade secret laws and

! treaty provisions. No part of the Material may be used, copied, reproduced,

! modified, published, uploaded, posted, transmitted, distributed or disclosed

! in any way without Intel's prior express written permission.

! No license under any patent, copyright, trade secret or other intellectual

! property right is granted to or conferred upon you by disclosure or delivery

! of the Materials, either expressly, by implication, inducement, estoppel or

! otherwise. Any license under such intellectual property rights must be

! express and approved by Intel in writing.

!

!*******************************************************************************

! Content: Intel MKL RCI (P)CG with MRHS Fortran-77 example

!

!*******************************************************************************

C---------------------------------------------------------------------------

C Example program for solving symmetric positive definite system of equations.

C Simplest case: no preconditioning and no the user-defined stopping tests.

C---------------------------------------------------------------------------

PROGRAM rci_pcgmrhs_f77_test_1

IMPLICIT NONE

INCLUDE 'mkl_solver.f77'

C---------------------------------------------------------------------------

C Define arrays for the upper triangle of the coefficient matrix and rhs vector

C Compressed sparse row storage is used for sparse representation

C---------------------------------------------------------------------------

INTEGER N, nRhs

PARAMETER (N=8,nRhs=2)

INTEGER RCI_request, itercount(nRhs), i

DOUBLE PRECISION rhs(N,nRhs)

C Matrix

INTEGER IA(9)

INTEGER JA(18)

DOUBLE PRECISION A(18)

C Fill all arrays containing matrix data.

DATA IA /1,5,8,10,12,15,17,18,19/

DATA JA

1 /1, 3, 6,7,

2 2,3, 5,

3 3, 8,

4 4, 7,

5 5,6,7,

6 6, 8,

7 7,

8 8/

DATA A

1 /7.D0, 1.D0, 2.D0, 7.D0,

2 -4.D0,8.D0, 2.D0,

3 1.D0, 5.D0,

4 7.D0, 9.D0,

5 5.D0, 1.D0, 5.D0,

6 -1.D0, 5.D0,

7 11.D0,

8 5.D0/

C---------------------------------------------------------------------------

C Allocate storage for the solver ?par and the initial solution vector

C---------------------------------------------------------------------------

INTEGER length, method

PARAMETER (length=128, method=1)

DOUBLE PRECISION expected_sol(N,nRhs)

DOUBLE PRECISION solution(N,nRhs)

INTEGER ipar(length + 2*nRhs)

DOUBLE PRECISION dpar(length + 2*nRhs),TMP(N,3 + nRhs)

C---------------------------------------------------------------------------

C Some additional variables to use with the RCI (P)CG MRHS solver

C---------------------------------------------------------------------------

DATA expected_sol/1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0,

+ 0.D0, 2.D0, 0.D0, 2.D0, 0.D0, 2.D0, 0.D0, 2.D0/

C---------------------------------------------------------------------------

C Initialize the right hand side through matrix-vector product

C---------------------------------------------------------------------------

CALL MKL_DCSRSYMV('U', N, A, IA, JA, expected_sol, rhs)

CALL MKL_DCSRSYMV('U', N, A, IA, JA, expected_sol(1,2), rhs(1,2))

C---------------------------------------------------------------------------

C Initialize the initial guess

C---------------------------------------------------------------------------

DO I=1, N

solution(I,1)=1.D0

solution(I,2)=2.D0

ENDDO

C---------------------------------------------------------------------------

C Initialize the solver

C---------------------------------------------------------------------------

DO I=1, length + 2*nRhs

ipar(i) = 0;

dpar(i) = 0;

ENDDO

CALL dcgmrhs_init(N,solution,nRhs,rhs,method,

+ RCI_request,ipar,dpar,TMP)

IF (RCI_request .NE. 0 ) GOTO 999

C---------------------------------------------------------------------------

C Set the desired parameters:

C LOGICAL parameters:

C do residual stopping test

C do not request for the user defined stopping test

C DOUBLE PRECISION parameters

C set the relative tolerance to 1.0D-5 instead of default value 1.0D-6

C---------------------------------------------------------------------------

ipar(9) =1

ipar(10)=0

dpar(1) =1.D-5

C---------------------------------------------------------------------------

C Compute the solution by RCI (P)CG MRHS solver without preconditioning

C Reverse Communications starts here

C---------------------------------------------------------------------------

1 CALL dcgmrhs(N,solution,nRhs,rhs,RCI_request,ipar,dpar,TMP)

C---------------------------------------------------------------------------

C If RCI_request=0, then the solution was found with the required precision

C---------------------------------------------------------------------------

IF (RCI_request .EQ. 0) THEN

GOTO 700

C---------------------------------------------------------------------------

C If RCI_request=1, then compute the vector A*TMP(:,1)

C and put the result in vector TMP(:,2)

C---------------------------------------------------------------------------

ELSEIF (RCI_request .EQ. 1) THEN

CALL MKL_DCSRSYMV('U', N, A, IA, JA, TMP, TMP(1,2))

GOTO 1

ELSE

C---------------------------------------------------------------------------

C If RCI_request=anything else, then dcg subroutine failed

C to compute the solution vector: solution

C---------------------------------------------------------------------------

GOTO 999

ENDIF

C---------------------------------------------------------------------------

C Reverse Communication ends here

C Get the current iteration number

C---------------------------------------------------------------------------

700 CALL dcgmrhs_get(N,solution,nRhs,rhs,RCI_request,ipar,dpar,TMP,

& itercount)

C---------------------------------------------------------------------------

C Print solution vector: solution(N) and number of iterations: itercount

C---------------------------------------------------------------------------

WRITE(*, *) ' The system is successfully solved '

WRITE(*, *) ' The following solution obtained '

WRITE(*,800) (solution(i,1),i =1,N)

WRITE(*,800) (solution(i,2),i =1,N)

WRITE(*, *) ' expected_sol solution '

WRITE(*,800)(expected_sol(i,1),i =1,N)

WRITE(*,800)(expected_sol(i,2),i =1,N)

800 FORMAT(4(F10.3))

GOTO 1000

999 WRITE(*,*) 'Solver returned error code ', RCI_request

STOP

1000 CONTINUE

STOP

END

## Example. Fortran Example to Solve Symmetric Positive Definite System with Multiple Right Hand Sizes (MRHS). Simple Preconditioning, No User-Defined Stopping Tests.

!*******************************************************************************

! INTEL CONFIDENTIAL

! Copyright(C) 2005-2008 Intel Corporation. All Rights Reserved.

! The source code contained or described herein and all documents related to

! the source code ("Material") are owned by Intel Corporation or its suppliers

! or licensors. Title to the Material remains with Intel Corporation or its

! suppliers and licensors. The Material contains trade secrets and proprietary

! and confidential information of Intel or its suppliers and licensors. The

! Material is protected by worldwide copyright and trade secret laws and

! treaty provisions. No part of the Material may be used, copied, reproduced,

! modified, published, uploaded, posted, transmitted, distributed or disclosed

! in any way without Intel's prior express written permission.

! No license under any patent, copyright, trade secret or other intellectual

! property right is granted to or conferred upon you by disclosure or delivery

! of the Materials, either expressly, by implication, inducement, estoppel or

! otherwise. Any license under such intellectual property rights must be

! express and approved by Intel in writing.

!

!*******************************************************************************

! Content: Intel MKL RCI (P)CG MRHS Fortran-77 example

!

!*******************************************************************************

C---------------------------------------------------------------------------

C Example program for solving symmetric positive definite system of equations.

C Simplest case: simple preconditioning and no the user-defined stopping tests.

C---------------------------------------------------------------------------

PROGRAM rci_pcgmrhs_f77_precond

IMPLICIT NONE

INCLUDE 'mkl_solver.f77'

C---------------------------------------------------------------------------

C Define arrays for the upper triangle of the coefficient matrix and rhs vector

C Compressed sparse row storage is used for sparse representation

C---------------------------------------------------------------------------

INTEGER N, nRhs

PARAMETER (N=8,nRhs=2)

INTEGER RCI_request, itercount(nRhs), i

DOUBLE PRECISION rhs(N,nRhs)

C Matrix

INTEGER IA(9)

INTEGER JA(18)

DOUBLE PRECISION A(18)

C Fill all arrays containing matrix data.

DATA IA /1,5,8,10,12,15,17,18,19/

DATA JA

1 /1, 3, 6,7,

2 2,3, 5,

3 3, 8,

4 4, 7,

5 5,6,7,

6 6, 8,

7 7,

8 8/

DATA A

1 /7.D0, 1.D0, 2.D0, 7.D0,

2 -4.D0,8.D0, 2.D0,

3 1.D0, 5.D0,

4 7.D0, 9.D0,

5 5.D0, 1.D0, 5.D0,

6 -1.D0, 5.D0,

7 11.D0,

8 5.D0/

C---------------------------------------------------------------------------

C Allocate storage for the solver ?par and the initial solution vector

C---------------------------------------------------------------------------

INTEGER length, method

PARAMETER (length=128, method=1)

DOUBLE PRECISION expected_sol(N,nRhs)

DOUBLE PRECISION solution(N,nRhs)

INTEGER ipar(length + 2*nRhs)

DOUBLE PRECISION dpar(length + 2*nRhs),TMP(N,3 + nRhs)

C---------------------------------------------------------------------------

C Some additional variables to use with the RCI (P)CG MRHS solver

C---------------------------------------------------------------------------

DATA expected_sol/1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0,

+ 0.D0, 2.D0, 0.D0, 2.D0, 0.D0, 2.D0, 0.D0, 2.D0/

C---------------------------------------------------------------------------

C Initialize the right hand side through matrix-vector product

C---------------------------------------------------------------------------

CALL MKL_DCSRSYMV('U', N, A, IA, JA, expected_sol, rhs)

CALL MKL_DCSRSYMV('U', N, A, IA, JA, expected_sol(1,2), rhs(1,2))

C---------------------------------------------------------------------------

C Initialize the initial guess

C---------------------------------------------------------------------------

DO I=1, N

solution(I,1)=1.D0

solution(I,2)=2.D0

ENDDO

C---------------------------------------------------------------------------

C Initialize the solver

C---------------------------------------------------------------------------

DO I=1, length + 2*nRhs

ipar(i) = 0;

dpar(i) = 0;

ENDDO

CALL dcgmrhs_init(N,solution,nRhs,rhs,method,

+ RCI_request,ipar,dpar,TMP)

IF (RCI_request .NE. 0 ) GOTO 999

C---------------------------------------------------------------------------

C Set the desired parameters:

C INTEGER parameters:

C set the maximal number of iterations to 100

C LOGICAL parameters:

C run the Preconditioned version of RCI (P)CG MRHS with preconditioner

C DOUBLE PRECISION parameters

C -

C---------------------------------------------------------------------------

ipar(5) = 100

ipar(11) = 1

ipar(9) = 1

ipar(10) = 0

dpar(1) =1.D-5

C---------------------------------------------------------------------------

C Compute the solution by RCI (P)CG MRHS solver

C Reverse Communications starts here

C---------------------------------------------------------------------------

1 CALL dcgmrhs(N,solution,nRhs,rhs,RCI_request,ipar,dpar,TMP)

C---------------------------------------------------------------------------

C If RCI_request=0, then the solution was found with the required precision

C---------------------------------------------------------------------------

IF (RCI_request .EQ. 0) THEN

GOTO 700

C---------------------------------------------------------------------------

C If RCI_request=1, then compute the vector A*TMP(:,1)

C and put the result in vector TMP(:,2)

C---------------------------------------------------------------------------

ELSEIF (RCI_request .EQ. 1) THEN

CALL MKL_DCSRSYMV('U', N, A, IA, JA, TMP, TMP(1,2))

GOTO 1

C---------------------------------------------------------------------------

C If RCI_request=3, then apply the simplest preconditioning

C on vector TMP(:,3+par(3)) and put the result in vector TMP(:,3)

C---------------------------------------------------------------------------

ELSEIF (RCI_request .EQ. 3) THEN

do i = 1, n

tmp(i, 3) = tmp (i, 3+ipar(3));

end do

GOTO 1

ELSE

C---------------------------------------------------------------------------

C If RCI_request=anything else, then dcg subroutine failed

C to compute the solution vector: solution(N)

C---------------------------------------------------------------------------

GOTO 999

ENDIF

C---------------------------------------------------------------------------

C Reverse Communication ends here

C Get the current iteration number

C---------------------------------------------------------------------------

700 CALL dcgmrhs_get(N,solution,nRhs,rhs,RCI_request,ipar,dpar,TMP,

& itercount)

C---------------------------------------------------------------------------

C Print solution vector: solution(N) and number of iterations: itercount

C---------------------------------------------------------------------------

WRITE(*, *) ' The system is successfully solved '

WRITE(*, *) ' The following solution obtained '

WRITE(*,800) (solution(i,1),i =1,N)

WRITE(*,800) (solution(i,2),i =1,N)

WRITE(*, *) ' expected_sol solution '

WRITE(*,800)(expected_sol(i,1),i =1,N)

WRITE(*,800)(expected_sol(i,2),i =1,N)

800 FORMAT(4(F10.3))

GOTO 1000

999 WRITE(*,*) 'Solver returned error code ', RCI_request

STOP

1000 CONTINUE

STOP

END

## Example. Fortran Example to Solve Symmetric Positive Definite System with Multiple Right Hand Sizes (MRHS). User-Defined Stopping Tests, No Preconditioning.

!*******************************************************************************

! INTEL CONFIDENTIAL

! Copyright(C) 2005-2008 Intel Corporation. All Rights Reserved.

! The source code contained or described herein and all documents related to

! the source code ("Material") are owned by Intel Corporation or its suppliers

! or licensors. Title to the Material remains with Intel Corporation or its

! suppliers and licensors. The Material contains trade secrets and proprietary

! and confidential information of Intel or its suppliers and licensors. The

! Material is protected by worldwide copyright and trade secret laws and

! treaty provisions. No part of the Material may be used, copied, reproduced,

! modified, published, uploaded, posted, transmitted, distributed or disclosed

! in any way without Intel's prior express written permission.

! No license under any patent, copyright, trade secret or other intellectual

! property right is granted to or conferred upon you by disclosure or delivery

! of the Materials, either expressly, by implication, inducement, estoppel or

! otherwise. Any license under such intellectual property rights must be

! express and approved by Intel in writing.

!

!*******************************************************************************

! Content: Intel MKL RCI (P)CG MRHS Fortran-77 example

!

!*******************************************************************************

C---------------------------------------------------------------------------

C Example program for solving symmetric positive definite system of equations.

C Simplest case: no preconditioning and the user-defined stopping tests.

C---------------------------------------------------------------------------

PROGRAM rci_pcgmrhs_f77_stop_crt

IMPLICIT NONE

INCLUDE 'mkl_solver.f77'

C---------------------------------------------------------------------------

C Define arrays for the upper triangle of the coefficient matrix and rhs vector

C Compressed sparse row storage is used for sparse representation

C---------------------------------------------------------------------------

INTEGER N, nRhs

PARAMETER (N=8,nRhs=2)

INTEGER RCI_request, itercount(nRhs), i

DOUBLE PRECISION rhs(N,nRhs)

C Matrix

INTEGER IA(9)

INTEGER JA(18)

DOUBLE PRECISION A(18)

C Fill all arrays containing matrix data.

DATA IA /1,5,8,10,12,15,17,18,19/

DATA JA

1 /1, 3, 6,7,

2 2,3, 5,

3 3, 8,

4 4, 7,

5 5,6,7,

6 6, 8,

7 7,

8 8/

DATA A

1 /7.D0, 1.D0, 2.D0, 7.D0,

2 -4.D0,8.D0, 2.D0,

3 1.D0, 5.D0,

4 7.D0, 9.D0,

5 5.D0, 1.D0, 5.D0,

6 -1.D0, 5.D0,

7 11.D0,

8 5.D0/

C---------------------------------------------------------------------------

C Allocate storage for the solver ?par and the initial solution vector

C---------------------------------------------------------------------------

INTEGER length, method

PARAMETER (length=128, method=1)

DOUBLE PRECISION expected_sol(N,nRhs)

DOUBLE PRECISION solution(N,nRhs)

INTEGER ipar(length + 2*nRhs)

DOUBLE PRECISION dpar(length + 2*nRhs),TMP(N,3 + nRhs)

C---------------------------------------------------------------------------

C Some additional variables to use with the RCI (P)CG MRHS solver

C---------------------------------------------------------------------------

DATA expected_sol/1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0, 1.D0, 0.D0,

+ 0.D0, 2.D0, 0.D0, 2.D0, 0.D0, 2.D0, 0.D0, 2.D0/

DOUBLE PRECISION DNRM2, Euclidean_norm, temp(N)

EXTERNAL DNRM2

C---------------------------------------------------------------------------

C Initialize the right hand side through matrix-vector product

C---------------------------------------------------------------------------

CALL MKL_DCSRSYMV('U', N, A, IA, JA, expected_sol, rhs)

CALL MKL_DCSRSYMV('U', N, A, IA, JA, expected_sol(1,2), rhs(1,2))

C---------------------------------------------------------------------------

C Initialize the initial guess

C---------------------------------------------------------------------------

DO I=1, N

solution(I,1)=1.D0

solution(I,2)=2.D0

ENDDO

C---------------------------------------------------------------------------

C Initialize the solver

C---------------------------------------------------------------------------

DO I=1, length + 2*nRhs

ipar(i) = 0;

dpar(i) = 0;

ENDDO

CALL dcgmrhs_init(N,solution,nRhs,rhs,method,

+ RCI_request,ipar,dpar,TMP)

IF (RCI_request .NE. 0 ) GOTO 999

C---------------------------------------------------------------------------

C Set the desired parameters:

C LOGICAL parameters:

C set the maximal number of iterations to 100

C request for the user defined stopping test

C---------------------------------------------------------------------------

ipar(5) = 100

ipar(10) = 1

dpar(1) = 1.d-5

C---------------------------------------------------------------------------

C Compute the solution by RCI (P)CG MRHS solver without preconditioning

C Reverse Communications starts here

C---------------------------------------------------------------------------

1 CALL dcgmrhs(N,solution,nRhs,rhs,RCI_request,ipar,dpar,TMP)

C---------------------------------------------------------------------------

C If RCI_request=0, then the solution was found with the required precision

C---------------------------------------------------------------------------

IF (RCI_request .EQ. 0) THEN

GOTO 700

C---------------------------------------------------------------------------

C If RCI_request=1, then compute the vector A*TMP(:,1)

C and put the result in vector TMP(:,2)

C---------------------------------------------------------------------------

ELSEIF (RCI_request .EQ. 1) THEN

CALL MKL_DCSRSYMV('U', N, A, IA, JA, TMP, TMP(1,2))

GOTO 1

C---------------------------------------------------------------------------

C If RCI_request=2, then do the user-defined stopping test: compute the

C Euclidean norm of the actual residual using MKL routines and check if

C it is less than 1.D-8

C---------------------------------------------------------------------------

ELSEIF (RCI_request .EQ. 2) THEN

CALL MKL_DCSRSYMV('U', N, A, IA, JA, solution(1,ipar(3)), temp)

CALL DAXPY(N,-1.D0,rhs(1,ipar(3)),1,temp,1)

Euclidean_norm = DNRM2(N,temp,1)

IF (Euclidean_norm .GT. 1.D-4) THEN

C---------------------------------------------------------------------------

C The solution has not been found yet according to the user-defined stopping

C test. Continue RCI (P)CG MRHS iterations.

C---------------------------------------------------------------------------

GOTO 1

ELSE

GOTO 700

END IF

ELSE

GOTO 999

ENDIF

C---------------------------------------------------------------------------

C Reverse Communication ends here

C Get the current iteration number

C---------------------------------------------------------------------------

700 CALL dcgmrhs_get(N,solution,nRhs,rhs,RCI_request,ipar,dpar,TMP,

& itercount)

C---------------------------------------------------------------------------

C Print solution vector: solution(N) and number of iterations: itercount

C---------------------------------------------------------------------------

WRITE(*, *) ' The system is successfully solved '

WRITE(*, *) ' The following solution obtained '

WRITE(*,800) (solution(i,1),i =1,N)

WRITE(*,800) (solution(i,2),i =1,N)

WRITE(*, *) ' expected_sol solution '

WRITE(*,800)(expected_sol(i,1),i =1,N)

WRITE(*,800)(expected_sol(i,2),i =1,N)

800 FORMAT(4(F10.3))

GOTO 1000

999 WRITE(*,*) 'Solver returned error code ', RCI_request

STOP

1000 CONTINUE

STOP

END

## Fortran Example of Using RCI (Preconditioned) Flexible Generalized Minimal Residual Solver.

Fortran example results for a non-symmetric indefinite system. Upon successful execution of the solver, the following result is printed (up to rounding errors that depend on the computer system used):

`--------------------------------------------------`
`The SIMPLEST example of usage of RCI FGMRES solver`
`to solve a non-symmetric indefinite non-degenerate`
`algebraic system of linear equations`
`--------------------------------------------------`
`Some info about the current run of RCI FGMRES method:`
`As IPAR(8)=1, the automatic test for the maximal number of`
`iterations will be performed`
`As IPAR(9)=1, the automatic residual test will be performed`
`+++`
`As IPAR(10)=0, the user-defined stopping test will not be`
`requested, thus, RCI_REQUEST will not take the value 2`
`+++`
`As IPAR(11)=0, the Preconditioned FGMRES iterations will not`
`be performed, thus, RCI_REQUEST will not take the value 3`
`+++`
`As IPAR(12)=1, the automatic test for the norm of the next`
`generated vector is not equal to zero up to rounding and computational`
`errors will be performed, thus, RCI_REQUEST will not take the value 4`
`+++`
`The system has been SUCCESSFULLY solved`
`The following solution has been obtained:`
`COMPUTED_SOLUTION(1)=-0.100E+01`
`COMPUTED_SOLUTION(2)= 0.100E+01`
`COMPUTED_SOLUTION(3)= 0.305E-15`
`COMPUTED_SOLUTION(4)= 0.100E+01`
`COMPUTED_SOLUTION(5)=-0.100E+01`
``
`The expected solution is:`
`EXPECTED_SOLUTION(1)=-0.100E+01`
`EXPECTED_SOLUTION(2)= 0.100E+01`
`EXPECTED_SOLUTION(3)= 0.000E+00`
`EXPECTED_SOLUTION(4)= 0.100E+01`
`EXPECTED_SOLUTION(5)=-0.100E+01`
`+++`
`Number of iterations: 5`
`+++`
``

## Example. Fortran Example to Solve Non-Symmetric Indefinite System

C***************************************************************************

C INTEL CONFIDENTIAL

C Copyright(C) 2005-2008 Intel Corporation. All Rights Reserved.

C The source code contained or described herein and all documents related to

C the source code ("Material") are owned by Intel Corporation or its suppliers

C or licensors. Title to the Material remains with Intel Corporation or its

C suppliers and licensors. The Material contains trade secrets and proprietary

C and confidential information of Intel or its suppliers and licensors. The

C Material is protected by worldwide copyright and trade secret laws and

C treaty provisions. No part of the Material may be used, copied, reproduced,

C modified, published, uploaded, posted, transmitted, distributed or disclosed

C in any way without Intel's prior express written permission.

C No license under any patent, copyright, trade secret or other intellectual

C property right is granted to or conferred upon you by disclosure or delivery

C of the Materials, either expressly, by implication, inducement, estoppel or

C otherwise. Any license under such intellectual property rights must be

C express and approved by Intel in writing.

C

C***************************************************************************

C Content:

C Intel MKL RCI (P)FGMRES ((Preconditioned) Flexible Generalized Minimal

C RESidual method) example

C****************************************************************************

C

C---------------------------------------------------------------------------

C Example program for solving non-symmetric indefinite system of equations

C Simplest case: no preconditioning and no user-defined stopping tests

C---------------------------------------------------------------------------

PROGRAM FGMRES_NO_PRECON_F

INCLUDE "mkl_rci.fi"

INTEGER N

PARAMETER(N=5)

INTEGER SIZE

PARAMETER (SIZE=128)

C---------------------------------------------------------------------------

C Define arrays for the upper triangle of the coefficient matrix

C Compressed sparse row storage is used for sparse representation

----------------------------------------------------------------------------

INTEGER IA(6)

DATA IA /1,3,6,9,12,14/

INTEGER JA(13)

DATA JA / 1, 3,

1 1, 2, 4,

2 2, 3, 5,

3 3, 4, 5,

4 4, 5 /

DOUBLE PRECISION A(13)

DATA A / 1.0, -1.0,

1 -1.0, 1.0, -1.0,

2 1.0,-2.0, 1.0,

3 -1.0, 2.0, -1.0,

4 -1.0, -3.0 /

C---------------------------------------------------------------------------

C Allocate storage for the ?par parameters and the solution/rhs vectors

C---------------------------------------------------------------------------

INTEGER IPAR(SIZE)

DOUBLE PRECISION DPAR(SIZE), TMP(N*(2*N+1)+(N*(N+9))/2+1)

DOUBLE PRECISION EXPECTED_SOLUTION(N)

DATA EXPECTED_SOLUTION /-1.0,1.0,0.0,1.0,-1.0/

DOUBLE PRECISION RHS(N)

DOUBLE PRECISION COMPUTED_SOLUTION(N)

C---------------------------------------------------------------------------

C Some additional variables to use with the RCI (P)FGMRES solver

C---------------------------------------------------------------------------

INTEGER ITERCOUNT

INTEGER RCI_REQUEST, I

PRINT *,'--------------------------------------------------'

PRINT *,'The SIMPLEST example of usage of RCI FGMRES solver'

PRINT *,'to solve a non-symmetric indefinite non-degenerate'

PRINT *,' algebraic system of linear equations'

PRINT *,'--------------------------------------------------'

C---------------------------------------------------------------------------

C Initialize variables and the right hand side through matrix-vector product

C---------------------------------------------------------------------------

CALL MKL_DCSRGEMV('N', N, A, IA, JA, EXPECTED_SOLUTION, RHS)

C---------------------------------------------------------------------------

C Initialize the initial guess

C---------------------------------------------------------------------------

DO I=1,N

COMPUTED_SOLUTION(I)=1.0

ENDDO

C---------------------------------------------------------------------------

C Initialize the solver

C---------------------------------------------------------------------------

CALL DFGMRES_INIT(N, COMPUTED_SOLUTION, RHS, RCI_REQUEST, IPAR,

1 DPAR, TMP)

IF (RCI_REQUEST.NE.0) GOTO 999

C---------------------------------------------------------------------------

C Set the desired parameters:

C LOGICAL parameters:

C do residual stopping test

C do not request for the user defined stopping test

C do the check of the norm of the next generated vector automatically

C DOUBLE PRECISION parameters

C set the relative tolerance to 1.0D-3 instead of default value 1.0D-6

C---------------------------------------------------------------------------

IPAR(9)=1

IPAR(10)=0

IPAR(12)=1

DPAR(1)=1.0D-3

C---------------------------------------------------------------------------

C Check the correctness and consistency of the newly set parameters

C---------------------------------------------------------------------------

CALL DFGMRES_CHECK(N, COMPUTED_SOLUTION, RHS, RCI_REQUEST,

1 IPAR, DPAR, TMP)

IF (RCI_REQUEST.NE.0) GOTO 999

C---------------------------------------------------------------------------

C Print the info about the RCI FGMRES method

C---------------------------------------------------------------------------

PRINT *, ''

PRINT *,'Some info about the current run of RCI FGMRES method:'

PRINT *, ''

IF (IPAR(8).NE.0) THEN

WRITE(*,'(A,I1,A)') 'As IPAR(8)=',IPAR(8),', the automatic',

1 ' test for the maximal number of iterations will be'

PRINT *,'performed'

ELSE

WRITE(*,'(A,I1,A)') 'As IPAR(8)=',IPAR(8),', the automatic ',

1 ' test for the maximal number of iterations will be'

PRINT *,'skipped'

ENDIF

PRINT *,'+++'

IF (IPAR(9).NE.0) THEN

WRITE(*,'(A,I1,A)') 'As IPAR(9)=',IPAR(9),', the automatic',

1 ' residual test will be performed'

ELSE

WRITE(*,'(A,I1,A)') 'As IPAR(9)=',IPAR(9),', the automatic',

1 ' residual test will be skipped'

ENDIF

PRINT *,'+++'

IF (IPAR(10).NE.0) THEN

WRITE(*,'(A,I1,A)') 'As IPAR(10)=',IPAR(10),', the user-defined',

1 ' stopping test will be requested via'

PRINT *,'RCI_REQUEST=2'

ELSE

WRITE(*,'(A,I1,A)') 'As IPAR(10)=',IPAR(10),', the user-defined',

1 ' stopping test will not be requested, thus,'

PRINT *,'RCI_REQUEST will not take the value 2'

ENDIF

PRINT *,'+++'

IF (IPAR(11).NE.0) THEN

WRITE(*,'(A,I1,A)') 'As IPAR(11)=',IPAR(11),', the',

1 ' Preconditioned FGMRES iterations will be performed, thus,'

WRITE(*,'(A,A)')'the preconditioner action will be requested via',

1 ' RCI_REQUEST=3'

ELSE

WRITE(*,'(A,I1,A)') 'As IPAR(11)=',IPAR(11),', the',

1 ' Preconditioned FGMRES iterations will not be performed,'

WRITE(*,'(A)')'thus, RCI_REQUEST will not take the value 3'

ENDIF

PRINT *,'+++'

IF (IPAR(12).NE.0) THEN

WRITE(*,'(A,I1,A)')'As IPAR(12)=',IPAR(12),', the automatic',

1 ' test for the norm of the next generated vector is'

WRITE(*,'(A,A)')'not equal to zero up to rounding and computational',

1 ' errors will be performed,'

PRINT *,'thus, RCI_REQUEST will not take the value 4'

ELSE

WRITE(*,'(A,I1,A)')'As IPAR(12)=',IPAR(12),', the automatic',

1 ' test for the norm of the next generated vector is'

WRITE(*,'(A,A)')'not equal to zero up to rounding and computational',

1 ' errors will be skipped,'

WRITE(*,'(A,A)')'thus, the user-defined test will be requested via',

1 ' RCI_REQUEST=4'

ENDIF

PRINT *,'+++'

C---------------------------------------------------------------------------

C Compute the solution by RCI (P)FGMRES solver without preconditioning

C Reverse Communication starts here

C---------------------------------------------------------------------------

1 CALL DFGMRES(N, COMPUTED_SOLUTION, RHS, RCI_REQUEST, IPAR,

1 DPAR, TMP)

C---------------------------------------------------------------------------

C If RCI_REQUEST=0, then the solution was found with the required precision

C---------------------------------------------------------------------------

IF (RCI_REQUEST.EQ.0) GOTO 3

C---------------------------------------------------------------------------

C If RCI_REQUEST=1, then compute the vector A*TMP(IPAR(22))

C and put the result in vector TMP(IPAR(23))

C---------------------------------------------------------------------------

IF (RCI_REQUEST.EQ.1) THEN

CALL MKL_DCSRGEMV('N',N, A, IA, JA, TMP(IPAR(22)), TMP(IPAR(23)))

GOTO 1

C---------------------------------------------------------------------------

C If RCI_REQUEST=anything else, then DFGMRES subroutine failed

C to compute the solution vector: COMPUTED_SOLUTION(N)

C---------------------------------------------------------------------------

ELSE

GOTO 999

ENDIF

C---------------------------------------------------------------------------

C Reverse Communication ends here

C Get the current iteration number and the FGMRES solution (DO NOT FORGET to

C call DFGMRES_GET routine as COMPUTED_SOLUTION is still containing

C the initial guess!)

C---------------------------------------------------------------------------

3 CALL DFGMRES_GET(N, COMPUTED_SOLUTION, RHS, RCI_REQUEST, IPAR,

1 DPAR, TMP, ITERCOUNT)

C---------------------------------------------------------------------------

C Print solution vector: COMPUTED_SOLUTION(N) and

C the number of iterations: ITERCOUNT

C---------------------------------------------------------------------------

PRINT *, ''

PRINT *,' The system has been SUCCESSFULLY solved'

PRINT *, ''

PRINT *,' The following solution has been obtained:'

DO I=1,N

WRITE(*,'(A18,I1,A2,E10.3)') 'COMPUTED_SOLUTION(',I,')=',

1 COMPUTED_SOLUTION(I)

ENDDO

PRINT *, ''

PRINT *,' The expected solution is:'

DO I=1,N

WRITE(*,'(A18,I1,A2,E10.3)') 'EXPECTED_SOLUTION(',I,')=',

1 EXPECTED_SOLUTION(I)

ENDDO

PRINT *, ''

PRINT *,' Number of iterations: ',ITERCOUNT

GOTO 1000

999 PRINT *,'The solver has returned the ERROR code ', RCI_REQUEST

1000 CONTINUE

END

## C Example of Using RCI (Preconditioned) Flexible Generalized Minimal Residual Solver.

C example results for the same non-symmetric indefinite system as in the previous example. The results are the same up to the notational convention between C and Fortran. Please pay special attention to how it is recommended to handle the differences between the Fortran and C arrays. Specifically, in this example the addresses for input/result are adjusted for user-defined operations from IPAR(22) to ipar[21]-1, and from IPAR(23) to ipar[22]-1, respectively. Upon successful execution of the solver, the following result is printed (up to rounding errors that depend on the computer system used):

`--------------------------------------------------`
`The SIMPLEST example of usage of RCI FGMRES solver`
`to solve a non-symmetric indefinite non-degenerate`
`algebraic system of linear equations`
`--------------------------------------------------`
`Some info about the current run of RCI FGMRES method:`
``
`As ipar[7]=1, the automatic test for the maximal number of iterations will be performed`
`+++`
`As ipar[8]=1, the automatic residual test will be performed`
`+++`
`As ipar[9]=0, the user-defined stopping test will not be requested, thus,`
`RCI_request will not take the value 2`
`+++`
`As ipar[10]=0, the Preconditioned FGMRES iterations will not`
`be performed, thus, RCI_request will not take the value 3`
`+++`
`As ipar[11]=1, the automatic test for the norm of the next`
`generated vector is not equal to zero up to rounding and computational`
`errors will be performed, thus, RCI_request will not take the value 4`
`+++`
``
`The system has been SUCCESSFULLY solved`
``
`The following solution has been obtained:`
`computed_solution[0]=-1.000000e+000`
`computed_solution[1]=1.000000e+000`
`computed_solution[2]=3.053113e-016`
`computed_solution[3]=1.000000e+000`
`computed_solution[4]=-1.000000e+000`
``
`The expected solution is:`
`expected_solution[0]=-1.000000e+000`
`expected_solution[1]=1.000000e+000`
`expected_solution[2]=0.000000e+000`
`expected_solution[3]=1.000000e+000`
`expected_solution[4]=-1.000000e+000`
``
`Number of iterations: 5`
``

## Example. C Example to Solve Non-Symmetric Indefinite System

/******************************************************************************

/* INTEL CONFIDENTIAL

/* Copyright(C) 2005-2008 Intel Corporation. All Rights Reserved.

/* The source code contained or described herein and all documents related to

/* the source code ("Material")are owned by Intel Corporation or its suppliers

/* or licensors. Title to the Material remains with Intel Corporation or its

/* suppliers and licensors.The Material contains trade secrets and proprietary

/* and confidential information of Intel or its suppliers and licensors. The

/* Material is protected by worldwide copyright and trade secret laws and

/* treaty provisions. No part of the Material may be used, copied, reproduced,

/* modified,published, uploaded, posted, transmitted, distributed or disclosed

/* in any way without Intel's prior express written permission.

/* No license under any patent, copyright, trade secret or other intellectual

/* property right is granted to or conferred upon you by disclosure or delivery

/* of the Materials, either expressly, by implication, inducement, estoppel or

/* otherwise. Any license under such intellectual property rights must be

/* express and approved by Intel in writing.

/*

/******************************************************************************

/* Content:

/* Intel MKL RCI (P)FGMRES ((Preconditioned) Flexible Generalized Minimal

/* RESidual method) example

/*****************************************************************************/

/*---------------------------------------------------------------------------

/* Example program for solving non-symmetric indefinite system of equations

/* Simplest case: no preconditioning and no user-defined stopping tests

/*---------------------------------------------------------------------------*/

#include <stdio.h>

#include "mkl_blas.h"

#include "mkl_spblas.h"

#include "mkl_rci.h"

#define N 5

#define size 128

int main(void)

{

/*---------------------------------------------------------------------------

/* Define arrays for the upper triangle of the coefficient matrix

/* Compressed sparse row storage is used for sparse representation

/*---------------------------------------------------------------------------*/

MKL_INT ia[6]={1,3,6,9,12,14};

MKL_INT ja[13]={ 1, 3,

1, 2, 4,

2, 3, 5,

3, 4, 5,

4, 5 };

double A[13]={ 1.0, -1.0,

-1.0, 1.0, -1.0,

1.0, -2.0, 1.0,

-1.0, 2.0,-1.0,

-1.0,-3.0 };

/*---------------------------------------------------------------------------

/* Allocate storage for the ?par parameters and the solution/rhs vectors

/*---------------------------------------------------------------------------*/

MKL_INT ipar[size];

double dpar[size], tmp[N*(2*N+1)+(N*(N+9))/2+1];

double expected_solution[N]={-1.0,1.0,0.0,1.0,-1.0};

double rhs[N];

double computed_solution[N];

/*---------------------------------------------------------------------------

/* Some additional variables to use with the RCI (P)FGMRES solver

/*---------------------------------------------------------------------------*/

MKL_INT itercount;

MKL_INT RCI_request, i, ivar;

double dvar;

char cvar;

printf("--------------------------------------------------\n");

printf("The SIMPLEST example of usage of RCI FGMRES solver\n");

printf("to solve a non-symmetric indefinite non-degenerate\n");

printf(" algebraic system of linear equations\n");

printf("--------------------------------------------------\n\n");

/*---------------------------------------------------------------------------

/* Initialize variables and the right hand side through matrix-vector product

/*---------------------------------------------------------------------------*/

ivar=N;

cvar='N';

mkl_dcsrgemv(&cvar, &ivar, A, ia, ja, expected_solution, rhs);

/*---------------------------------------------------------------------------

/* Initialize the initial guess

/*---------------------------------------------------------------------------

for(i=0;i<N;i++)

{

computed_solution[i]=1.0;

}

/*---------------------------------------------------------------------------

/* Initialize the solver

/*---------------------------------------------------------------------------

dfgmres_init(&ivar, computed_solution, rhs, &RCI_request, ipar, dpar, tmp);

if (RCI_request!=0) goto FAILED;

/*---------------------------------------------------------------------------

/* Set the desired parameters:

/* LOGICAL parameters:

/* do residual stopping test

/* do not request for the user defined stopping test

/* do the check of the norm of the next generated vector automatically

/* DOUBLE PRECISION parameters

/* set the relative tolerance to 1.0D-3 instead of default value 1.0D-6

/*---------------------------------------------------------------------------*/

ipar[8]=1;

ipar[9]=0;

ipar[11]=1;

dpar[0]=1.0E-3;

/*---------------------------------------------------------------------------

/* Check the correctness and consistency of the newly set parameters

/*---------------------------------------------------------------------------*/

dfgmres_check(&ivar, computed_solution, rhs, &RCI_request, ipar, dpar, tmp);

if (RCI_request!=0) goto FAILED;

/*---------------------------------------------------------------------------

/* Print the info about the RCI FGMRES method

/*---------------------------------------------------------------------------*/

printf("Some info about the current run of RCI FGMRES method:\n\n");

if (ipar[7])

{

printf("As ipar[7]=%d, the automatic test for the maximal number of iterations will be\n", ipar[7]);

printf("performed\n");

}

else

{

printf("As ipar[7]=%d, the automatic test for the maximal number of iterations will be\n", ipar[7]);

printf("skipped\n");

}

printf("+++\n");

if (ipar[8])

{

printf("As ipar[8]=%d, the automatic residual test will be performed\n", ipar[8]);

}

else

{

printf("As ipar[8]=%d, the automatic residual test will be skipped\n", ipar[8]);

}

printf("+++\n");

if (ipar[9])

{

printf("As ipar[9]=%d, the user-defined stopping test will be requested via\n", ipar[9]);

printf("RCI_request=2\n");

}

else

{

printf("As ipar[9]=%d, the user-defined stopping test will not be requested, thus,\n", ipar[9]);

printf("RCI_request will not take the value 2\n");

}

printf("+++\n");

if (ipar[10])

{

printf("As ipar[10]=%d, the Preconditioned FGMRES iterations will be performed, thus,\n", ipar[10]);

printf("the preconditioner action will be requested via RCI_request=3\n");

}

else

{

printf("As ipar[10]=%d, the Preconditioned FGMRES iterations will not be performed,\n", ipar[10]);

printf("thus, RCI_request will not take the value 3\n");

}

printf("+++\n");

if (ipar[11])

{

printf("As ipar[11]=%d, the automatic test for the norm of the next generated vector is\n", ipar[11]);

printf("not equal to zero up to rounding and computational errors will be performed,\n");

printf("thus, RCI_request will not take the value 4\n");

}

else

{

printf("As ipar[11]=%d, the automatic test for the norm of the next generated vector is\n", ipar[11]);

printf("not equal to zero up to rounding and computational errors will be skipped,\n");

printf("thus, the user-defined test will be requested via RCI_request=4\n");

}

printf("+++\n\n");

/*---------------------------------------------------------------------------

/* Compute the solution by RCI (P)FGMRES solver without preconditioning

/* Reverse Communication starts here

/*---------------------------------------------------------------------------*/

ONE: dfgmres(&ivar, computed_solution, rhs, &RCI_request, ipar, dpar, tmp);

/*---------------------------------------------------------------------------

/* If RCI_request=0, then the solution was found with the required precision

/*---------------------------------------------------------------------------*/

if (RCI_request==0) goto COMPLETE;

/*---------------------------------------------------------------------------

/* If RCI_request=1, then compute the vector A*tmp[ipar[21]-1]

/* and put the result in vector tmp[ipar[22]-1]

/*---------------------------------------------------------------------------

/* NOTE that ipar[21] and ipar[22] contain FORTRAN style addresses,

/* therefore, in C code it is required to subtract 1 from them to get

/* C style addresses

/*---------------------------------------------------------------------------*/

if (RCI_request==1)

{

mkl_dcsrgemv(&cvar, &ivar, A, ia, ja, &tmp[ipar[21]-1], &tmp[ipar[22]-1]);

goto ONE;

}

/*---------------------------------------------------------------------------

/* If RCI_request=anything else, then dfgmres subroutine failed

/* to compute the solution vector: computed_solution[N]

/*---------------------------------------------------------------------------*/

else

{

goto FAILED;

}

/*---------------------------------------------------------------------------

/* Reverse Communication ends here

/* Get the current iteration number and the FGMRES solution (DO NOT FORGET

/* to call dfgmres_get routine as computed_solution is still containing

/* the initial guess!)

/*---------------------------------------------------------------------------*/

COMPLETE: dfgmres_get(&ivar, computed_solution, rhs, &RCI_request, ipar, dpar, tmp, &itercount);

/*

/*---------------------------------------------------------------------------

/* Print solution vector: computed_solution[N] and the number of iterations: itercount

/*---------------------------------------------------------------------------*/

printf(" The system has been SUCCESSFULLY solved \n");

printf("\n The following solution has been obtained: \n");

for (i=0;i<N;i++)

{

printf("computed_solution[%d]=",i);

printf("%e\n", computed_solution[i]);

{

printf("\n The expected solution[%d]=",i);

for (i=0;i<N;i++)

{

printf("expected_solution[%d]=",i);

printf("%e\n", expected_solution[i]);

{

printf("\n Number of iterations: %d",itercount);

goto SUCCEDED;

FAILED: printf("The solver has returned the ERROR code %d", RCI_request);

SUCCEDED: return 0;

}

Parent topic: Code Examples