Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

intel MKL library working fine in windows but cause "segmentation fault" in unix after the method "mkl_dcsrcoo"

cibin_W_
Beginner
467 Views

I have attached a sample code below:

main:

=====
int main(int argc, char **argv)
{
    
    Mat test = makeTestMat(3, 3);
wlsFilter(test, test);
return 0;
}


wlsFilter:
=======
void wlsFilter(Mat& src, Mat& dst, float lambda, float alpha)
{

    float eps = 2.2204e-11; 
    float smallNum = 0.0001;

    Mat L;
    cv::log(src + eps, L);

    int r = src.rows;
    int c = src.cols;
    int k = r*c;

    Mat dy;
    diffy(L, dy);
    dy = F(dy, lambda, alpha, smallNum);
    copyMakeBorder(dy, dy, 0, 1, 0, 0, BORDER_CONSTANT, Scalar::all(0));
    dy = dy.t();
    dy = dy.reshape(1, k);

    Mat dx;
    diffx(L, dx);
    dx = F(dx, lambda, alpha, smallNum);
    copyMakeBorder(dx, dx, 0, 0, 0, 1, BORDER_CONSTANT, Scalar::all(0));
    dx = dx.t();
    dx = dx.reshape(1, k);

    Mat B = Mat(k, 2, CV_32FC1);
    dx.copyTo(B.col(0));
    dy.copyTo(B.col(1));

    Mat e = dx.clone();
    Mat w;
    copyMakeBorder(dx, w, r, 0, 0, 0, BORDER_CONSTANT, Scalar::all(0));
    w = w.rowRange(0, w.rows - r);

    Mat s = dy.clone();
    Mat n;
    copyMakeBorder(dy, n, 1, 0, 0, 0, BORDER_CONSTANT, Scalar::all(0));
    n = n.rowRange(0, n.rows - 1);

    Mat D = 1 - (e + w + s + n);

    solveSparse_MKL(src, B, D, r, dst);

}

solveSparse_MKL:
================
void solveSparse_MKL(Mat& img, Mat& B, Mat& D, int r, Mat &dst)
{
    MKL_INT* i_csr = 0;
    MKL_INT* j_csr = 0;
    double* a_csr = 0;

    _DOUBLE_PRECISION_t* rhs = 0;

    MKL_INT nNonZeros = spDiag2_MKL(B, -r, -1, D, i_csr, j_csr, a_csr);
.
.
.
.
}

spDiag2_MKL:
============
int spDiag2_MKL(Mat& B, int d1, int d2, Mat& D, MKL_INT*& i_csr, MKL_INT*& j_csr, double*& a_csr)
{
    MKL_INT* rowind = 0;
    MKL_INT* colind = 0;
    double* acoo = 0;

    MKL_INT nnz = 0;


    Vec2i off1, off2;
    if (d1 > 0) { off1 = Vec2i(0, d1); }
    else { off1 = Vec2i(-d1, 0); }

    if (d2 > 0) { off2 = Vec2i(0, d2); }
    else { off2 = Vec2i(-d2, 0); }

    for (int i = 0; i < B.rows; ++i)
    {
        int i1 = i + off1[0];
        int j1 = i + off1[1];

        int i2 = i + off2[0];
        int j2 = i + off2[1];

        if (i1 < B.rows && j1 < B.rows)
        {
            nnz++;
        }
        if (i2 < B.rows && j2 < B.rows)
        {
            nnz++;
        }
        ++nnz;
    }
    rowind = new MKL_INT[nnz];
    colind = new MKL_INT[nnz];
    acoo = new double[nnz];
    MKL_INT ind = 0;
    for (int i = 0; i < B.rows; ++i)
    {
        int i1 = i + off1[0];
        int j1 = i + off1[1];

        int i2 = i + off2[0];
        int j2 = i + off2[1];

        if (i1 < B.rows && j1 < B.rows)
        {
            if (j1 > i1)
            {
                rowind[ind] = i1 + 1;
                colind[ind] = j1 + 1;
                acoo[ind] = B.at<float>(i, 0);
                ++ind;
            }
            else
            {
                rowind[ind] = j1 + 1;
                colind[ind] = i1 + 1;
                acoo[ind] = B.at<float>(i, 0);
                ++ind;
            }
        }
        if (i2 < B.rows && j2 < B.rows)
        {
            if (j2 > i2)
            {
                rowind[ind] = i2 + 1;
                colind[ind] = j2 + 1;
                acoo[ind] = B.at<float>(i, 1);
                ++ind;
            }
            else
            {
                rowind[ind] = j2 + 1;
                colind[ind] = i2 + 1;
                acoo[ind] = B.at<float>(i, 1);
                ++ind;
            }
        }
        rowind[ind] = i + 1;
        colind[ind] = i + 1;
        acoo[ind] = D.at<float>(i);
        ++ind;
    }
    MKL_INT m = B.rows;
    MKL_INT n = B.rows;

    a_csr = new double[nnz];
    i_csr = new MKL_INT[m + 1]; // m+1
    j_csr = new MKL_INT[nnz];

    MKL_INT info;
    MKL_INT job[8] = { 2, // COO to CSR
        1, // 1 based indexing in CSR rows
        1, // 1 based indexing in CSR cols
        0, //
        nnz, // number of the non-zero elements
        0, // job indicator
        0,
        0
    };

    
    mkl_dcsrcoo(job, &m, a_csr, j_csr, i_csr, &nnz, acoo, rowind, colind, &info);
.
.
.
.
}

 

 

Description:

I the above code "mkl_dcsrcoo(job, &m, a_csr, j_csr, i_csr, &nnz, acoo, rowind, colind, &info);"

The values of m,nnz,acoo,rowind,colind are same in both windows and unix 
But the a_csr,j_csr,i_csr values differs.

Can you pls tell why its differs?

Before calling the mkl_dcsrcoo the values are same,but after calling the method the values differs,so i hope the issue occurs in the "mkl_dcsrcoo" method.
Pls suggest any solutions?

Thanks in advance.

 

Regards,

CIBIN

 

0 Kudos
8 Replies
Gennady_F_Intel
Moderator
467 Views

if input results are same then the output results have to be the same too for all OS. Could you reduce the case, extract your input date and make the standalone reproducer? we will have a look at the problem.

and what version of mkl do you use? we will check the list of known issues associated with this routine. 

0 Kudos
cibin_W_
Beginner
467 Views

@Gennady ,Thanks for the reply

The version using are

Windows->2017.2.187
Unix->2017.2.174

0 Kudos
mecej4
Honored Contributor III
467 Views

Sorry, that is not enough information to run your code. You should provide code with no ellipses (missing code lines indicated by dots), and your code should contain all the declarations and #include lines necessary to make it compilable.

0 Kudos
cibin_W_
Beginner
467 Views

The minimal source is :

code:

 

#include "iostream"
        #include "stdio.h"
#include <vector>

#include "mkl_service.h"
#include "mkl_pardiso.h"
#include "mkl_types.h"
#include "mkl_dss.h"
#include "mkl_types.h"
#include "mkl_spblas.h"

using namespace std;
using std::vector;

//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
void solveSparse_MKL()
{
    _DOUBLE_PRECISION_t rhs[9] = { 0,0.333333,0.666667,0.111111,0.444444,0.777778,0.222222,0.555556,0.888889 };

    MKL_INT nnz = 23;
    MKL_INT nRows = 9;
    MKL_INT nCols = 9;
    MKL_INT nRhs = 1;
    MKL_INT rhs_len = 9;

    double acoo[] = { -0.0537308, -0.0512116, 1.10494, -4.17055, -1.73111, 6.95287, -7.78207, 0, 10.5132, -1.73111, -0.865586, 3.65043, -5.3765, -2.14414, 13.5568, -8.98329, 0, 19.9095, -1.30956, 4.04067, -2.5529, 10.239, 12.5362 };
    MKL_INT rowind[] = { 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,8,8,9 };
    MKL_INT colind[] = { 4, 2, 1, 5, 3, 2, 6, 4, 3, 7, 5, 4, 8, 6, 5, 9, 7, 6, 8, 7, 9, 8, 9 };

    MKL_INT info;
    MKL_INT job[8] = { 2, // COO to CSR
        1, // 1 based indexing in CSR rows
        1, // 1 based indexing in CSR cols
        0, //
        nnz, // number of the non-zero elements
        0, // job indicator
        0,
        0
    };

    MKL_INT* i_csr = new MKL_INT[nCols + 1]; // m+1
    MKL_INT* j_csr = new MKL_INT[nnz];
    double* a_csr = new double[nnz];

    mkl_dcsrcoo(job, &nCols, a_csr, j_csr, i_csr, &nnz, acoo, rowind, colind, &info);

    _DOUBLE_PRECISION_t* solValues = new _DOUBLE_PRECISION_t[rhs_len];

    // Allocate storage for the solver handle and the right-hand side.
    _MKL_DSS_HANDLE_t handle = 0;
    _INTEGER_t error;
    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)
        printf("Solver returned error code %d\n", error);
    // -------------------------------------------
    // Define the non-zero structure of the matrix
    // -------------------------------------------
    error = dss_define_structure(handle, sym, i_csr, nRows, nCols, j_csr, nnz);
    if (error != MKL_DSS_SUCCESS)
        printf("Solver returned error code %d\n", error);
    // ------------------
    // Reorder the matrix
    // ------------------
    error = dss_reorder(handle, opt, 0);
    if (error != MKL_DSS_SUCCESS)
        printf("Solver returned error code %d\n", error);
    // ------------------
    // Factor the matrix
    // ------------------
    error = dss_factor_real(handle, type, a_csr);
    if (error != MKL_DSS_SUCCESS)
        printf("Solver returned error code %d\n", error);
    // ------------------------
    // Get the solution vector  
    // ------------------------
    error = dss_solve_real(handle, opt, rhs, nRhs, solValues);
    if (error != MKL_DSS_SUCCESS)
        printf("Solver returned error code %d\n", error);


    cout << "------------------------------" << endl;
    cout << "solution " << endl;
    cout << "------------------------------" << endl;
    for (int j = 0; j < rhs_len; ++j)
    {
        cout << solValues << endl;
    }
    // --------------------------
    // Deallocate solver storage  
    // --------------------------
    error = dss_delete(handle, opt);
    if (error != MKL_DSS_SUCCESS)
        printf("Solver returned error code %d\n", error);


    delete[] a_csr;
    delete[] i_csr;
    delete[] j_csr;
    delete[] solValues;
}

//-----------------------------------------------------------------------------------------------------
// https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor
//-----------------------------------------------------------------------------------------------------
int main(void)
{
    solveSparse_MKL();
    getchar();
    return 0;
}
// c:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl\examples\examples_core_c.zip\

Expected result:

------------------------------
solution
------------------------------
0.0384921
0.494602
0.582386
0.320155
0.502434
0.591085
0.358512
0.513289
0.598997

 

Get the expected result in Windows,but in ubuntu it gives "segmentation fault"

 

 

 

 

 

 

0 Kudos
Gennady_F_Intel
Moderator
467 Views

I see no problem with RH7.

./u731522]$ ./a.out
Major version:           2017
Minor version:           0
Update version:          0
Product status:          Product
Build:                   20160801
Platform:                Intel(R) 64 architecture
Processor optimization:  Intel(R) Advanced Vector Extensions (Intel(R) AVX) enabled processors
================================================================

------------------------------
solution
------------------------------
0.0384921
0.494602
0.582386
0.320155
0.502434
0.591085
0.358512
0.513289
0.598997

How did you link the case?

Could you get more details about your OS? 

Could you add    mkl_get_version(&Version);  routine and show the output?

--Gennady

 

 

0 Kudos
cibin_W_
Beginner
466 Views

mkl_get_version(&Version):

====================

Major version: 2017
Minor version: 0
Update version: 2
Product status: Product
Build: 20170126
Platform: Intel(R) 64 architecture
Processor optimization: Intel(R) Streaming SIMD Extensions 2 (Intel(R) SSE2) enabled processors
================================================================

Os details:

Screenshot from 2017-05-03 15:32:08.png

0 Kudos
cibin_W_
Beginner
466 Views

I linked and generated exe using:

========================

1-->g++ -std=c++11 -DMKL_ILP64 -m64 -I/include -c -O2 -Iinclude -Iinclude/opencv -Iinclude/opencv2 -Iboost -Iboost/boost -Idlib/include -Itbb/include -I/opt/intel/compilers_and_libraries/linux/mkl/include -I. -MMD -MP -MF "build/Release/GNU-Linux/main.o.d" -c main.cpp
2-->g++ -o test main.o -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_ilp64 -lmkl_tbb_thread -lmkl_core -ltbb -lstdc++ -lpthread -lm -ldl

0 Kudos
Gennady_F_Intel
Moderator
466 Views

I see no problems with the same conditions: MKL 2017 u2, ilp64 mode, tbb layer, SS2 ISA.  But I used RH7.

pls see output below.

g++ -DMKL_ILP64 -m64 -I/opt/intel/compilers_and_libraries_2017/mkl/include mkl_dcsrcoo_test.cpp  \
-L/opt/intel/compilers_and_libraries_2017/mkl/lib/intel64/lib/intel64 \
-Wl,--no-as-needed -lmkl_intel_ilp64 -lmkl_tbb_thread -lmkl_core -ltbb -lstdc++ -lpthread -lm -ldl
.....]$ ./a.out
Major version:           2017
Minor version:           0
Update version:          2
Product status:          Product
Build:                   20170126
Platform:                Intel(R) 64 architecture
Processor optimization:  Intel(R) Streaming SIMD Extensions 2 (Intel(R) SSE2) enabled processors
================================================================

------------------------------
solution
------------------------------
0.0384921
0.494602
0.582386
0.320155
0.502434
0.591085
0.358512
0.513289
0.598997

g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)

 

0 Kudos
Reply