<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Intel MKL dgesvd does not seem to return correct singular vectors in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Intel-MKL-dgesvd-does-not-seem-to-return-correct-singular/m-p/1094692#M23467</link>
    <description>&lt;P&gt;&lt;SPAN style="color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; line-height: 19.5px;"&gt;I'm using Intel MKL&amp;nbsp;&lt;/SPAN&gt;&lt;CODE style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; color: rgb(34, 36, 38); background-color: rgb(238, 238, 238);"&gt;dgesvd&lt;/CODE&gt;&lt;SPAN style="color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; line-height: 19.5px;"&gt;&amp;nbsp;in the code below. When comparing the results with Matlab, the singular values are ok, while the singular vectors are not.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The C++ code I'm using&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;sstream&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;fstream&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;mkl.h&amp;gt;
#include "mkl_lapacke.h"

#include "TimingCPU.h"
#include "InputOutput.h"

using namespace std;

/**********************************/
/* RANDOM MATRIX ENTRY GENERATION */
/**********************************/
double generateRandomMatrixEntry(int N) { return 2000. * ((double)rand() / (double)(RAND_MAX - 0.2) * (1. / (double)N)) + 100.*((double)rand() / (double)(RAND_MAX - 0.2) * (1. / (double)N)) + 24.; }

/********/
/* MAIN */
/********/
int main(int argc, char **argv) {

        int Nrows, Ncols, K, numExecutions;

    char *jobu = "S", *jobvt = "S";     // --- "N" means not to calculate the corresponding singular vectors

    TimingCPU timer;

    if (argc != 5) { printf("usage: svdMKLDouble Nrows Ncols K numExecutions\n"); exit(0); }
        { std::stringstream ss(argv[1]); ss &amp;gt;&amp;gt; Nrows; }
        { std::stringstream ss(argv[2]); ss &amp;gt;&amp;gt; Ncols; }
        { std::stringstream ss(argv[3]); ss &amp;gt;&amp;gt; K; }    
        { std::stringstream ss(argv[4]); ss &amp;gt;&amp;gt; numExecutions; }

        int lda = Nrows, ldu = Nrows, ldvt = Ncols;

        double TotalTime = 0., AverageTime = 0.;

    double *A       = (double*)mkl_malloc(Nrows * Ncols * K * sizeof(double), 64);  // --- Input matrices
    double *A_QUERY = (double*)mkl_malloc(Nrows * Ncols *     sizeof(double), 64);  // --- Input matrix for MKL query
    double *U       = (double*)mkl_malloc(Nrows * Nrows * K * sizeof(double), 64);  // --- Matrices of LEFT singular vectors
    double *S       = (double*)mkl_malloc(        Ncols * K * sizeof(double), 64);  // --- Singular values
    double *V       = (double*)mkl_malloc(Ncols * Ncols * K * sizeof(double), 64);  // --- Matrices of RIGHT singular vectors

    srand(time(NULL));
    //srand(0);
    int N = 5;

    /*************************************************/
    /* QUERY AND ALLOCATION OF THE OPTIMAL WORKSPACE */
    /*************************************************/
        for (int h = 0; h &amp;lt; Nrows * Ncols; h++) A_QUERY&lt;H&gt; = generateRandomMatrixEntry(N);

    int info, lwork = -1;          
    double wkopt;
        dgesvd(jobu, jobvt, &amp;amp;Nrows, &amp;amp;Ncols, A_QUERY, &amp;amp;lda, S, U, &amp;amp;ldu, V, &amp;amp;ldvt, &amp;amp;wkopt, &amp;amp;lwork, &amp;amp;info);
        lwork = (int)wkopt;
        double *work = (double *)malloc(lwork * K * sizeof(double));                

    /**************/
    /* DUMMY CALL */
    /**************/
    for (int i = 0; i &amp;lt; K; i++)
            dgesvd(jobu, jobvt, &amp;amp;Nrows, &amp;amp;Ncols, A + (i * Nrows * Ncols), &amp;amp;lda, S + (i * Ncols), U + (i * Nrows * Nrows), &amp;amp;ldu, V + (i * Ncols * Ncols), &amp;amp;ldvt, work + i * lwork, &amp;amp;lwork, &amp;amp;info);

    /*********************/
    /* TIME MEASUREMENTS */
    /*********************/
    for (int k = 0; k &amp;lt; numExecutions; k++) {

        //srand(k);

            for (int h = 0; h &amp;lt; Nrows * Ncols * K; h++) { A&lt;H&gt; = generateRandomMatrixEntry(N); }

            timer.StartCounter();
        #pragma omp parallel for
        for (int i = 0; i &amp;lt; K; i++) {
            dgesvd(jobu, jobvt, &amp;amp;Nrows, &amp;amp;Ncols, A + (i * Nrows * Ncols), &amp;amp;lda, S + (i * Ncols), U + (i * Nrows * Nrows), &amp;amp;ldu, V + (i * Ncols * Ncols), &amp;amp;ldvt, work + i * lwork, &amp;amp;lwork, &amp;amp;info);
            // --- Convergence check            
            if (info &amp;gt; 0) { printf("Convergence failure.\n"); exit(1); }
        }
        #pragma omp barrier   
            TotalTime += timer.GetCounter(); // --- TotalTime in ms

    }

    AverageTime = TotalTime / numExecutions;
    std::cout &amp;lt;&amp;lt; std::scientific &amp;lt;&amp;lt; Nrows &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; Ncols &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; K &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; numExecutions &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; AverageTime &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; std::endl;

    /*****************************/
    /* SAVINGS FOR RESULT CHECKS */
    /*****************************/
    // --- Saving the last matrix
    saveCPUrealtxt&amp;lt;double&amp;gt;(A + ((K - 1) * Nrows * Ncols), "Source_Matrix.txt", Nrows * Ncols);  
    // --- Saving the SVD
    saveCPUrealtxt&amp;lt;double&amp;gt;(U + ((K - 1) * Nrows * Nrows), "U.txt", Nrows * Nrows);  
    saveCPUrealtxt&amp;lt;double&amp;gt;(V + ((K - 1) * Ncols * Ncols), "V.txt", Ncols * Ncols);  
    saveCPUrealtxt&amp;lt;double&amp;gt;(S + ((K - 1) * Ncols),         "S.txt", Ncols        );  

    return 0;
}&lt;/H&gt;&lt;/H&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The compilation command&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;icpc -lpthread -lmkl_sequential -lmkl_core -lmkl_intel_lp64 -lrt -openmp svdMKLDouble.cpp TimingCPU.cpp InputOutput.cpp -O3 -o svdMKLDouble&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;Code usage as...&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;./svdMKLDouble 8 8 1 1&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The Matlab code I'm using as reference&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;clear all
close all
clc

load Source_Matrix.txt
load U.txt
load V.txt
load S.txt

Source_Matrix = reshape(Source_Matrix, 8, 8);
U             = reshape(U, 8, 8);
V             = reshape(V, 8, 8);

[UU, SS, VV]  = svd(Source_Matrix);

100 * sqrt(sum(abs(diag(SS) - S).^2) / sum(abs(diag(SS)).^2))

100 * sqrt(sum(sum((abs(Source_Matrix - UU * SS * VV.').^2))) / sum(sum((abs(Source_Matrix).^2))))

100 * sqrt(sum(sum((abs(Source_Matrix - U  * diag(S)  * V).^2))) / sum(sum((abs(Source_Matrix).^2)))) &lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The comparison results&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;ans =

    0.0512        ---&amp;gt; Singular values correctly computed


ans =

   5.1387e-13     ---&amp;gt; Matlab SVD works correctly


ans =

  132.3127        ---&amp;gt; Intel MKL singular values do not seem to be correct&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The question:&lt;/STRONG&gt;&lt;SPAN style="color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; line-height: 19.5px;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM style="border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;What I'm doing wrong?&lt;/EM&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Mar 2016 08:21:49 GMT</pubDate>
    <dc:creator>Angelo_L_1</dc:creator>
    <dc:date>2016-03-02T08:21:49Z</dc:date>
    <item>
      <title>Intel MKL dgesvd does not seem to return correct singular vectors</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Intel-MKL-dgesvd-does-not-seem-to-return-correct-singular/m-p/1094692#M23467</link>
      <description>&lt;P&gt;&lt;SPAN style="color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; line-height: 19.5px;"&gt;I'm using Intel MKL&amp;nbsp;&lt;/SPAN&gt;&lt;CODE style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; color: rgb(34, 36, 38); background-color: rgb(238, 238, 238);"&gt;dgesvd&lt;/CODE&gt;&lt;SPAN style="color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; line-height: 19.5px;"&gt;&amp;nbsp;in the code below. When comparing the results with Matlab, the singular values are ok, while the singular vectors are not.&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The C++ code I'm using&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;sstream&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;fstream&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;mkl.h&amp;gt;
#include "mkl_lapacke.h"

#include "TimingCPU.h"
#include "InputOutput.h"

using namespace std;

/**********************************/
/* RANDOM MATRIX ENTRY GENERATION */
/**********************************/
double generateRandomMatrixEntry(int N) { return 2000. * ((double)rand() / (double)(RAND_MAX - 0.2) * (1. / (double)N)) + 100.*((double)rand() / (double)(RAND_MAX - 0.2) * (1. / (double)N)) + 24.; }

/********/
/* MAIN */
/********/
int main(int argc, char **argv) {

        int Nrows, Ncols, K, numExecutions;

    char *jobu = "S", *jobvt = "S";     // --- "N" means not to calculate the corresponding singular vectors

    TimingCPU timer;

    if (argc != 5) { printf("usage: svdMKLDouble Nrows Ncols K numExecutions\n"); exit(0); }
        { std::stringstream ss(argv[1]); ss &amp;gt;&amp;gt; Nrows; }
        { std::stringstream ss(argv[2]); ss &amp;gt;&amp;gt; Ncols; }
        { std::stringstream ss(argv[3]); ss &amp;gt;&amp;gt; K; }    
        { std::stringstream ss(argv[4]); ss &amp;gt;&amp;gt; numExecutions; }

        int lda = Nrows, ldu = Nrows, ldvt = Ncols;

        double TotalTime = 0., AverageTime = 0.;

    double *A       = (double*)mkl_malloc(Nrows * Ncols * K * sizeof(double), 64);  // --- Input matrices
    double *A_QUERY = (double*)mkl_malloc(Nrows * Ncols *     sizeof(double), 64);  // --- Input matrix for MKL query
    double *U       = (double*)mkl_malloc(Nrows * Nrows * K * sizeof(double), 64);  // --- Matrices of LEFT singular vectors
    double *S       = (double*)mkl_malloc(        Ncols * K * sizeof(double), 64);  // --- Singular values
    double *V       = (double*)mkl_malloc(Ncols * Ncols * K * sizeof(double), 64);  // --- Matrices of RIGHT singular vectors

    srand(time(NULL));
    //srand(0);
    int N = 5;

    /*************************************************/
    /* QUERY AND ALLOCATION OF THE OPTIMAL WORKSPACE */
    /*************************************************/
        for (int h = 0; h &amp;lt; Nrows * Ncols; h++) A_QUERY&lt;H&gt; = generateRandomMatrixEntry(N);

    int info, lwork = -1;          
    double wkopt;
        dgesvd(jobu, jobvt, &amp;amp;Nrows, &amp;amp;Ncols, A_QUERY, &amp;amp;lda, S, U, &amp;amp;ldu, V, &amp;amp;ldvt, &amp;amp;wkopt, &amp;amp;lwork, &amp;amp;info);
        lwork = (int)wkopt;
        double *work = (double *)malloc(lwork * K * sizeof(double));                

    /**************/
    /* DUMMY CALL */
    /**************/
    for (int i = 0; i &amp;lt; K; i++)
            dgesvd(jobu, jobvt, &amp;amp;Nrows, &amp;amp;Ncols, A + (i * Nrows * Ncols), &amp;amp;lda, S + (i * Ncols), U + (i * Nrows * Nrows), &amp;amp;ldu, V + (i * Ncols * Ncols), &amp;amp;ldvt, work + i * lwork, &amp;amp;lwork, &amp;amp;info);

    /*********************/
    /* TIME MEASUREMENTS */
    /*********************/
    for (int k = 0; k &amp;lt; numExecutions; k++) {

        //srand(k);

            for (int h = 0; h &amp;lt; Nrows * Ncols * K; h++) { A&lt;H&gt; = generateRandomMatrixEntry(N); }

            timer.StartCounter();
        #pragma omp parallel for
        for (int i = 0; i &amp;lt; K; i++) {
            dgesvd(jobu, jobvt, &amp;amp;Nrows, &amp;amp;Ncols, A + (i * Nrows * Ncols), &amp;amp;lda, S + (i * Ncols), U + (i * Nrows * Nrows), &amp;amp;ldu, V + (i * Ncols * Ncols), &amp;amp;ldvt, work + i * lwork, &amp;amp;lwork, &amp;amp;info);
            // --- Convergence check            
            if (info &amp;gt; 0) { printf("Convergence failure.\n"); exit(1); }
        }
        #pragma omp barrier   
            TotalTime += timer.GetCounter(); // --- TotalTime in ms

    }

    AverageTime = TotalTime / numExecutions;
    std::cout &amp;lt;&amp;lt; std::scientific &amp;lt;&amp;lt; Nrows &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; Ncols &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; K &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; numExecutions &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; AverageTime &amp;lt;&amp;lt; " " &amp;lt;&amp;lt; std::endl;

    /*****************************/
    /* SAVINGS FOR RESULT CHECKS */
    /*****************************/
    // --- Saving the last matrix
    saveCPUrealtxt&amp;lt;double&amp;gt;(A + ((K - 1) * Nrows * Ncols), "Source_Matrix.txt", Nrows * Ncols);  
    // --- Saving the SVD
    saveCPUrealtxt&amp;lt;double&amp;gt;(U + ((K - 1) * Nrows * Nrows), "U.txt", Nrows * Nrows);  
    saveCPUrealtxt&amp;lt;double&amp;gt;(V + ((K - 1) * Ncols * Ncols), "V.txt", Ncols * Ncols);  
    saveCPUrealtxt&amp;lt;double&amp;gt;(S + ((K - 1) * Ncols),         "S.txt", Ncols        );  

    return 0;
}&lt;/H&gt;&lt;/H&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The compilation command&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;icpc -lpthread -lmkl_sequential -lmkl_core -lmkl_intel_lp64 -lrt -openmp svdMKLDouble.cpp TimingCPU.cpp InputOutput.cpp -O3 -o svdMKLDouble&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;Code usage as...&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;./svdMKLDouble 8 8 1 1&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The Matlab code I'm using as reference&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;clear all
close all
clc

load Source_Matrix.txt
load U.txt
load V.txt
load S.txt

Source_Matrix = reshape(Source_Matrix, 8, 8);
U             = reshape(U, 8, 8);
V             = reshape(V, 8, 8);

[UU, SS, VV]  = svd(Source_Matrix);

100 * sqrt(sum(abs(diag(SS) - S).^2) / sum(abs(diag(SS)).^2))

100 * sqrt(sum(sum((abs(Source_Matrix - UU * SS * VV.').^2))) / sum(sum((abs(Source_Matrix).^2))))

100 * sqrt(sum(sum((abs(Source_Matrix - U  * diag(S)  * V).^2))) / sum(sum((abs(Source_Matrix).^2)))) &lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The comparison results&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE class="brush:python;"&gt;ans =

    0.0512        ---&amp;gt; Singular values correctly computed


ans =

   5.1387e-13     ---&amp;gt; Matlab SVD works correctly


ans =

  132.3127        ---&amp;gt; Intel MKL singular values do not seem to be correct&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG style="margin: 0px; padding: 0px; border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;The question:&lt;/STRONG&gt;&lt;SPAN style="color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; line-height: 19.5px;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM style="border: 0px; font-size: 15px; color: rgb(34, 36, 38); font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; line-height: 19.5px;"&gt;What I'm doing wrong?&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Mar 2016 08:21:49 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Intel-MKL-dgesvd-does-not-seem-to-return-correct-singular/m-p/1094692#M23467</guid>
      <dc:creator>Angelo_L_1</dc:creator>
      <dc:date>2016-03-02T08:21:49Z</dc:date>
    </item>
  </channel>
</rss>

