9.2 Calling BLAS/LAPACK Routines from MOSEK

Sometimes users need to perform linear algebra operations that involve dense matrices and vectors. Also MOSEK extensively uses high-performance linear algebra routines from the BLAS and LAPACK packages and some of these routines are included in the package shipped to the users.

The MOSEK versions of BLAS/LAPACK routines:

  • use MOSEK data types and return value conventions,

  • preserve the BLAS/LAPACK naming convention.

Therefore the user can leverage on efficient linear algebra routines, with a simplified interface, with no need for additional packages.

List of available routines

Table 9.1 BLAS routines available.

BLAS Name

MOSEK function

Math Expression

AXPY

Env.axpy

\(y=\alpha x+y\)

DOT

Env.dot

\(x^T y\)

GEMV

Env.gemv

\(y=\alpha Ax + \beta y\)

GEMM

Env.gemm

\(C=\alpha AB+ \beta C\)

SYRK

Env.syrk

\(C=\alpha AA^T+ \beta C\)

Table 9.2 LAPACK routines available.

LAPACK Name

MOSEK function

Description

POTRF

Env.potrf

Cholesky factorization of a semidefinite symmetric matrix

SYEVD

Env.syevd

Eigenvalues and eigenvectors of a symmetric matrix

SYEIG

Env.syeig

Eigenvalues of a symmetric matrix

Source code examples

In Listing 9.2 we provide a simple working example. It has no practical meaning except showing how to organize the input and call the methods.

Listing 9.2 Calling BLAS and LAPACK routines from Optimizer API for Java. Click here to download.
package com.mosek.example;

public class blas_lapack {
  static final int n = 3, m = 2, k = 3;

  public static void main (String[] args) {

    double alpha = 2.0, beta = 0.5;
    double[] x = {1., 1., 1.};
    double[] y = {1., 2., 3.};
    double[] z = {1.0, 1.0};

    /*A has m=2 rows and k=3 cols*/
    double[] A = {1., 1., 2., 2., 3., 3.};
    /*B has k=3 rows and n=3 cols*/
    double[] B = {1., 1., 1., 1., 1., 1., 1., 1., 1.};
    double[] C = { 1., 2., 3., 4., 5., 6.};

    double[] D = {1.0, 1.0, 1.0, 1.0};
    double[] Q = {1.0, 0.0, 0.0, 2.0};
    double[] v = new double[2];

    double[] xy = {0.};

    try (mosek.Env  env = new mosek.Env()) {
      /*  routines*/

      env.dot(n, x, y, xy);

      env.axpy(n, alpha, x, y);

      env.gemv(mosek.transpose.no, m, n, alpha, A, x, beta, z);

      env.gemm(mosek.transpose.no, mosek.transpose.no, m, n, k, alpha, A, B, beta, C);

      env.syrk(mosek.uplo.lo, mosek.transpose.no, m, k, alpha, A, beta, D);

      /* LAPACK routines*/

      env.potrf(mosek.uplo.lo, m, Q);

      env.syeig(mosek.uplo.lo, m, Q, v);

      env.syevd(mosek.uplo.lo, m, Q, v);

    } catch (mosek.Exception e) {
      System.out.println ("An error/warning was encountered");
      System.out.println (e.toString());
    }
  }
}