# Direct Sparse Solver Code Examples

This section contains example code in FORTRAN 77, Fortran 90 and C. For description of the sparse solver routines used in this code, refer to “Direct Sparse Solver (DSS) Interface Routines” in “Sparse Solver Routines”. The example code solves the equations presented in Direct Method section of Appendix A - a symmetric positive definite system of equations `Ax= b` with a sparse matrix, where

## Example results for symmetric systems

Upon successful execution of the solver, the determinant and the result of the solution array are as follows

pow of determinant is 0.000

base of determinant is 2.250

Determinant is 2.250

Solution Array: -326.333 983.000 163.417 398.000 61.500

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

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

* INTEL CONFIDENTIAL

* Copyright(C) 2004-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 DSS Fortran 77 example

*

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

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

C Example program for solving symmetric positive definite system of

C equations.

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

PROGRAM solver_f77_test

IMPLICIT NONE

INCLUDE 'mkl_dss.f77'

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

C Define the array and rhs vectors

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

INTEGER nRows, nCols, nNonZeros, i, nRhs

PARAMETER (nRows = 5,

1 nCols = 5,

2 nNonZeros = 9,

3 nRhs = 1)

INTEGER rowIndex(nRows + 1), columns(nNonZeros)

DOUBLE PRECISION values(nNonZeros), rhs(nRows)

DATA rowIndex / 1, 6, 7, 8, 9, 10 /

DATA columns / 1, 2, 3, 4, 5, 2, 3, 4, 5 /

DATA values / 9, 1.5, 6, .75, 3, 0.5, 12, .625, 16 /

DATA rhs / 1, 2, 3, 4, 5 /

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

C Allocate storage for the solver handle and the solution vector

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

DOUBLE PRECISION solution(nRows)

INTEGER*8 handle

INTEGER error

CHARACTER*15 statIn

DOUBLE PRECISION statOut(5)

INTEGER bufLen

PARAMETER(bufLen = 20)

INTEGER buff(bufLen)

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

C Initialize the solver

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

error = dss_create(handle, MKL_DSS_DEFAULTS)

IF (error .NE. MKL_DSS_SUCCESS ) GOTO 999

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

C Define the non-zero structure of the matrix

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

error = dss_define_structure( handle, MKL_DSS_SYMMETRIC,

& rowIndex, nRows, nCols, columns, nNonZeros )

IF (error .NE. MKL_DSS_SUCCESS ) GOTO 999

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

C Reorder the matrix

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

error = dss_reorder( handle, MKL_DSS_DEFAULTS, 0)

IF (error .NE. MKL_DSS_SUCCESS ) GOTO 999

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

C Factor the matrix

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

error = dss_factor_real( handle,

& MKL_DSS_DEFAULTS, VALUES)

IF (error .NE. MKL_DSS_SUCCESS ) GOTO 999

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

C Get the solution vector

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

error = dss_solve_real( handle, MKL_DSS_DEFAULTS,

& rhs, nRhs, solution)

IF (error .NE. MKL_DSS_SUCCESS ) GOTO 999

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

C Print Determinant of the matrix

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

statIn = 'determinant'

call mkl_cvt_to_null_terminated_str(buff,bufLen,statIn)

error = dss_statistics(handle, MKL_DSS_DEFAULTS,

& buff,statOut)

WRITE(*,"(' pow of determinant is ', 5(F10.3))") statOut(1)

WRITE(*,"(' base of determinant is ', 5(F10.3))") statOut(2)

WRITE(*,"(' Determinant is ', 5(F10.3))")(10**statOut(1))*

& statOut(2)

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

C Deallocate solver storage

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

error = dss_delete( handle, MKL_DSS_DEFAULTS )

IF (error .NE. MKL_DSS_SUCCESS ) GOTO 999

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

C Print solution vector

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

WRITE(*,900) (solution(i), i = 1, nCols)

900 FORMAT(' Solution Array: ',5(F10.3))

GOTO 1000

999 WRITE(*,*) "Solver returned error code ", error

1000 END

## Example. C Example to Solve Symmetric Positive Definite System

/*

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

* INTEL CONFIDENTIAL

* Copyright(C) 2004-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 DSS C example

*

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

/*

** Example program to solve symmetric positive definite system of equations.

*/

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

#include "mkl_dss.h"

/*

** Define the array and rhs vectors

*/

#define NROWS 5

#define NCOLS 5

#define NNONZEROS 9

#define NRHS 1

#if defined(MKL_ILP64)

#define MKL_INT long long

#else

#define MKL_INT int

#endif

static const MKL_INT nRows = NROWS ;

static const MKL_INT nCols = NCOLS ;

static const MKL_INT nNonZeros = NNONZEROS ;

static const MKL_INT nRhs = NRHS ;

static _INTEGER_t rowIndex[NROWS+1] = { 1, 6, 7, 8, 9, 10 };

static _INTEGER_t columns[NNONZEROS] = { 1, 2, 3, 4, 5, 2, 3, 4, 5 };

static _DOUBLE_PRECISION_t values[NNONZEROS] = { 9, 1.5, 6, .75, 3, 0.5, 12, .625, 16 };

static _DOUBLE_PRECISION_t rhs[NCOLS] = { 1, 2, 3, 4, 5 };

MKL_INT main() {

MKL_INT i;

/* Allocate storage for the solver handle and the right-hand side. */

_DOUBLE_PRECISION_t solValues[NROWS];

_MKL_DSS_HANDLE_t handle;

_INTEGER_t error;

_CHARACTER_STR_t statIn[] = "determinant";

_DOUBLE_PRECISION_t statOut[5];

MKL_INT opt = MKL_DSS_DEFAULTS;

MKL_INT sym = MKL_DSS_SYMMETRIC;

MKL_INT type = MKL_DSS_POSITIVE_DEFINITE;

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

/* Initialize the solver */

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

error = dss_create(handle, opt );

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* Define the non-zero structure of the matrix */

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

error = dss_define_structure(

handle, sym, rowIndex, nRows, nCols,

columns, nNonZeros );

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* Reorder the matrix */

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

error = dss_reorder(handle, opt, 0);

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* Factor the matrix */

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

error = dss_factor_real( handle, type, values );

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* Get the solution vector */

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

error = dss_solve_real( handle, opt, rhs, nRhs, solValues );

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* Get the determinant (not for a diagonal matrix) */

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

if ( nRows < nNonZeros ) {

error = dss_statistics(handle, opt, statIn, statOut);

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* print determinant*/

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

printf(" determinant power is %g \n", statOut[0]);

printf(" determinant base is %g \n", statOut[1]);

printf(" Determinant is %g \n", (pow(10.0,statOut[0]))*statOut[1]);

}

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

/* Deallocate solver storage */

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

error = dss_delete( handle, opt );

if ( error != MKL_DSS_SUCCESS ) goto printError;

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

/* Print solution vector */

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

printf(" Solution array: ");

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

printf(" %g", solValues[i] );

printf("\n");

exit(0);

printError:

printf("Solver returned error code %d\n", error);

exit(1);

}

## Example. Fortran 90 Example to Solve Symmetric Positive Definite System

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

! INTEL CONFIDENTIAL

! Copyright(C) 2004-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 DSS Fortran 90 example

!

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

!--------------------------------------------------------------------------

!

! Example program for solving a symmetric positive definite system of

! equations.

!

!--------------------------------------------------------------------------

INCLUDE 'mkl_dss.f90' ! Include the standard DSS "header file."

PROGRAM solver_f90_test

use mkl_dss

IMPLICIT NONE

INTEGER, PARAMETER :: dp = KIND(1.0D0)

INTEGER :: error

INTEGER :: i

INTEGER, PARAMETER :: bufLen = 20

! Define the data arrays and the solution and rhs vectors.

INTEGER, ALLOCATABLE :: columns( : )

INTEGER :: nCols

INTEGER :: nNonZeros

INTEGER :: nRhs

INTEGER :: nRows

REAL(KIND=DP), ALLOCATABLE :: rhs( : )

INTEGER, ALLOCATABLE :: rowIndex( : )

REAL(KIND=DP), ALLOCATABLE :: solution( : )

REAL(KIND=DP), ALLOCATABLE :: values( : )

TYPE(MKL_DSS_HANDLE) :: handle ! Allocate storage for the solver handle.

REAL(KIND=DP),ALLOCATABLE::statOUt( : )

CHARACTER*15 statIn

INTEGER perm(1)

INTEGER buff(bufLen)

EXTERNAL MKL_CVT_TO_NULL_TERMINATED_STR

! Set the problem to be solved.

nRows = 5

nCols = 5

nNonZeros = 9

nRhs = 1

perm(1) = 0

ALLOCATE( rowIndex(nRows + 1 ) )

rowIndex = (/ 1, 6, 7, 8, 9, 10 /)

ALLOCATE( columns(nNonZeros ) )

columns = (/ 1, 2, 3, 4, 5, 2, 3, 4, 5 /)

ALLOCATE( values( nNonZeros ) )

values = (/ 9.0_DP, 1.5_DP, 6.0_DP, 0.75_DP, 3.0_DP, 0.5_DP, 12.0_DP, &

& 0.625_DP, 16.0_DP /)

ALLOCATE( rhs( nRows ) )

rhs = (/ 1.0_DP, 2.0_DP, 3.0_DP, 4.0_DP, 5.0_DP /)

! Initialize the solver.

error = dss_create( handle, MKL_DSS_DEFAULTS )

IF (error /= MKL_DSS_SUCCESS) GOTO 999

! Define the non-zero structure of the matrix.

error = dss_define_structure( handle, MKL_DSS_SYMMETRIC, rowIndex, nRows, &

& nCols, columns, nNonZeros )

IF (error /= MKL_DSS_SUCCESS) GOTO 999

! Reorder the matrix.

error = dss_reorder( handle, MKL_DSS_DEFAULTS, perm )

IF (error /= MKL_DSS_SUCCESS) GOTO 999

! Factor the matrix.

error = dss_factor_real( handle, MKL_DSS_DEFAULTS, values )

IF (error /= MKL_DSS_SUCCESS) GOTO 999

! Allocate the solution vector and solve the problem.

ALLOCATE( solution( nRows ) )

error = dss_solve_real(handle, MKL_DSS_DEFAULTS, rhs, nRhs, solution )

IF (error /= MKL_DSS_SUCCESS) GOTO 999

! Print Out the determinant of the matrix

ALLOCATE(statOut( 5 ) )

statIn = 'determinant'

call mkl_cvt_to_null_terminated_str(buff,bufLen,statIn)

error = dss_statistics(handle, MKL_DSS_DEFAULTS, buff, statOut )

IF (error /= MKL_DSS_SUCCESS) GOTO 999

WRITE(*,"('pow of determinant is '(5F10.3))") ( statOut(1) )

WRITE(*,"('base of determinant is '(5F10.3))") ( statOut(2) )

WRITE(*,"('Determinant is '(5F10.3))") ( (10**statOut(1))*statOut(2) )

END IF

! Deallocate solver storage and various local arrays.

error = DSS_DELETE( handle, MKL_DSS_DEFAULTS )

IF (error /= MKL_DSS_SUCCESS ) GOTO 999

IF ( ALLOCATED( rowIndex) ) DEALLOCATE( rowIndex )

IF ( ALLOCATED( columns ) ) DEALLOCATE( columns )

IF ( ALLOCATED( values ) ) DEALLOCATE( values )

IF ( ALLOCATED( rhs ) ) DEALLOCATE( rhs )

IF ( ALLOCATED( statOut ) ) DEALLOCATE( statOut )

! Print the solution vector, deallocate it and exit

WRITE(*,"('Solution Array: '(5F10.3))") ( solution(i), i = 1, nCols )

IF ( ALLOCATED( solution ) ) DEALLOCATE( solution )

GOTO 1000

! Print an error message and exit

999 WRITE(*,*) "Solver returned error code ", error

1000 CONTINUE

END PROGRAM solver_f90_test

Parent topic: Code Examples