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

std::getline() is not working in debug mode

ulassezgn
Beginner
2,686 Views

Hi all,

I was coding a sparse matrix multiplication (spmm) program by reading a square from .mtx file and creating random square matrix (same size with first matrix). The dimension of a matrix was 36417x36417 but there were a problem with spmm. sometimes it result in segfault and results were like partial product. To understand problem i create a 5x5 matrix in .mtx file and program result in segfault everytime. For understanding clearly i installed vscode oneapi debug extension (by the way, i am coding in intel devcloud). I was using command "icpx -g -O0 -DMKL_ILP64 -I"${MKLROOT}/include" -L${MKLROOT}/lib/intel64 -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl file.cpp -o file". Firstly,  strings were shown as "<incomplete type>" in debug mode. Then, i added " -D_GLIBCXX_DEBUG" to my compiling command. I am currently using "icpx -g -O0 -DMKL_ILP64 -D_GLIBCXX_DEBUG -I"${MKLROOT}/include" -L${MKLROOT}/lib/intel64 -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl file.cpp -o file"

 

Now, the problem what i want to ask you is std::getline() function in readFile() function is reading nothing. i am completely desperate and have no idea what is problem about. My 5x5 square matrix's .mtx file and whole related code is below.

 

basic.mtx

5 5 5
1 1 4.0092939526
1 4 2.9294569526
2 1 4.2572957297
3 2 4.9290009526
4 4 1.1987939526

the code

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <mkl.h>
using namespace std;

pair<MKL_INT,MKL_INT> readFile(string txtPath, MKL_INT** rowIndex, MKL_INT** columns, double** values, char isWeight);
void makeMatrix(MKL_INT** rowIndex, MKL_INT** columns, double** values, int nnz, int colSize);
void spmm(const MKL_INT& M, const MKL_INT& N, const MKL_INT& K, 
		MKL_INT* rowIndex_A, MKL_INT* columns_A, double* values_A,
		MKL_INT* rowIndex_B, MKL_INT* columns_B, double* values_B,
		MKL_INT* pointerB_C, MKL_INT* pointerE_C, MKL_INT* columns_C, double* values_C, MKL_INT& nnz, 
		sparse_matrix_t& handle_C_p);
void freeHandle(sparse_matrix_t& handle);
void writeFileC(MKL_INT* pointerB_C, MKL_INT* pointerE_C, MKL_INT* columns, double* values, MKL_INT nnz, MKL_INT row);
void writeFileB(MKL_INT* rowIndexB , MKL_INT* columnsB, double* valuesB, MKL_INT nnz);
int main()
{
    MKL_INT** rowIndexA = new MKL_INT*;
    MKL_INT** columnsA = new MKL_INT*;
    double** valuesA = new double*;
    pair<MKL_INT,MKL_INT> nnz_colSize = readFile("basic.mtx", rowIndexA, columnsA, valuesA, 'w');
    MKL_INT** rowIndexB = new MKL_INT*;
    MKL_INT** columnsB = new MKL_INT*;
    double** valuesB = new double*;
    makeMatrix(rowIndexB, columnsB, valuesB, nnz_colSize.first, nnz_colSize.second);

    MKL_INT** pointerB_C = new MKL_INT*;
    MKL_INT** pointerE_C = new MKL_INT*;
    MKL_INT** columnsC = new MKL_INT*;
    MKL_INT* nnz = new MKL_INT;
    double** valuesC = new double*;
    sparse_matrix_t handle_C;
    cout << "Hello World! This is log!" << endl;
    
    
    spmm(nnz_colSize.second, nnz_colSize.second, nnz_colSize.second, (*rowIndexA), (*columnsA), (*valuesA), (*rowIndexB), (*columnsB), (*valuesB), *pointerB_C, *pointerE_C, *columnsC, *valuesC, nnz_colSize.first, handle_C);
    cout << "spmm-wf" << endl;
    writeFileB(*rowIndexB, *columnsB, *valuesB, nnz_colSize.first);
    writeFileC(*pointerB_C, *pointerE_C, *columnsC, *valuesC, *nnz, nnz_colSize.second);
    freeHandle(handle_C);
    return EXIT_SUCCESS;
}


pair<MKL_INT,MKL_INT> readFile(string txtPath, MKL_INT** rowIndex, MKL_INT** columns, double** values, char isWeight)
{
    srand(time(NULL));
    string theElement = "Halo";
    MKL_INT rowSize, colSize, nonzeroTotal;
    ifstream theMatrix(txtPath);
    getline(theMatrix, theElement);
    istringstream in(theElement);
    in >> rowSize >> colSize >> nonzeroTotal;
    *rowIndex = new MKL_INT[nonzeroTotal];
    *columns = new MKL_INT[nonzeroTotal];
    *values = new double[nonzeroTotal];
    MKL_INT counter = 0;
    while(getline(theMatrix, theElement))
    {
        istringstream inr(theElement);
        if(isWeight == 'u')
        {
            inr >> (*rowIndex)[counter] >> (*columns)[counter];
            (*values)[counter] = 1;
        }
        else if(isWeight == 'w')
        {
            inr >> (*rowIndex)[counter] >> (*columns)[counter] >> (*values)[counter];
            cout << (*rowIndex)[counter] << " " << (*columns)[counter] << " " << (*values)[counter] << endl;
        }
        counter++;
    }
    pair<MKL_INT,MKL_INT> ret(nonzeroTotal, colSize);
    theMatrix.close();
    return ret;
}


 

Labels (2)
0 Kudos
9 Replies
VarshaS_Intel
Moderator
2,641 Views

Hi,


Thanks for posting in Intel Communities.


Could you please provide us with the complete sample reproducer code you are trying and the complete error you are getting while running the code to reproducer it at our end?


Additionally, you can find the sample examples that are present in the following path /opt/intel/oneapi/mkl/latest/examples/examples_core_c.tgz, where you can find how to read the data file in the sample codes.


Thanks & Regards,

Varsha 


0 Kudos
ulassezgn
Beginner
2,624 Views

Hi Varsha,

 

Thanks for replying. With debugging by using  GDB with GPU Debug Support for Intel® oneAPI Toolkits VS Code extension, Segmentation Fault is occured in the 138th line which is below.

 

    sparse_status_t stA = mkl_sparse_d_create_csr(&csrA, SPARSE_INDEX_BASE_ONE, M, K, rowIndex_A, rowIndex_A+1, columns_A, values_A);

 

But, I think it is because getline(theMatrix, theElement) function is returning 0 in 63rd line. For better understanding, the whole code is below. 

 

 

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <set> 
#include <mkl.h>
using namespace std;

pair<MKL_INT,MKL_INT> readFile(string txtPath, MKL_INT** rowIndex, MKL_INT** columns, double** values, char isWeight);
void makeMatrix(MKL_INT** rowIndex, MKL_INT** columns, double** values, int nnz, int colSize);
void spmm(const MKL_INT& M, const MKL_INT& N, const MKL_INT& K, 
		MKL_INT* rowIndex_A, MKL_INT* columns_A, double* values_A,
		MKL_INT* rowIndex_B, MKL_INT* columns_B, double* values_B,
		MKL_INT* pointerB_C, MKL_INT* pointerE_C, MKL_INT* columns_C, double* values_C, MKL_INT& nnz, 
		sparse_matrix_t& handle_C_p);
void freeHandle(sparse_matrix_t& handle);
void writeFileC(MKL_INT* pointerB_C, MKL_INT* pointerE_C, MKL_INT* columns, double* values, MKL_INT nnz, MKL_INT row);
void writeFileB(MKL_INT* rowIndexB , MKL_INT* columnsB, double* valuesB, MKL_INT nnz);
int main()
{
    MKL_INT** rowIndexA = new MKL_INT*;
    MKL_INT** columnsA = new MKL_INT*;
    double** valuesA = new double*;
    pair<MKL_INT,MKL_INT> nnz_colSize = readFile("basic.mtx", rowIndexA, columnsA, valuesA, 'w');
    MKL_INT** rowIndexB = new MKL_INT*;
    MKL_INT** columnsB = new MKL_INT*;
    double** valuesB = new double*;
    makeMatrix(rowIndexB, columnsB, valuesB, nnz_colSize.first, nnz_colSize.second);

    MKL_INT** pointerB_C = new MKL_INT*;
    MKL_INT** pointerE_C = new MKL_INT*;
    MKL_INT** columnsC = new MKL_INT*;
    MKL_INT* nnz = new MKL_INT;
    double** valuesC = new double*;
    sparse_matrix_t handle_C;
    cout << "Hello World! This is log!" << endl;
    
    
    spmm(nnz_colSize.second, nnz_colSize.second, nnz_colSize.second, (*rowIndexA), (*columnsA), (*valuesA), (*rowIndexB), (*columnsB), (*valuesB), *pointerB_C, *pointerE_C, *columnsC, *valuesC, nnz_colSize.first, handle_C);
    cout << "spmm-wf" << endl;
    writeFileB(*rowIndexB, *columnsB, *valuesB, nnz_colSize.first);
    writeFileC(*pointerB_C, *pointerE_C, *columnsC, *valuesC, *nnz, nnz_colSize.second);
    freeHandle(handle_C);
    return EXIT_SUCCESS;
}


pair<MKL_INT,MKL_INT> readFile(string txtPath, MKL_INT** rowIndex, MKL_INT** columns, double** values, char isWeight)
{
    srand(time(NULL));
    string theElement = "Halo";
    MKL_INT rowSize, colSize, nonzeroTotal;
    ifstream theMatrix(txtPath);
    getline(theMatrix, theElement);
    istringstream in(theElement);
    in >> rowSize >> colSize >> nonzeroTotal;
    *rowIndex = new MKL_INT[nonzeroTotal];
    *columns = new MKL_INT[nonzeroTotal];
    *values = new double[nonzeroTotal];
    MKL_INT counter = 0;
    while(getline(theMatrix, theElement))
    {
        istringstream inr(theElement);
        if(isWeight == 'u')
        {
            inr >> (*rowIndex)[counter] >> (*columns)[counter];
            (*values)[counter] = 1;
        }
        else if(isWeight == 'w')
        {
            inr >> (*rowIndex)[counter] >> (*columns)[counter] >> (*values)[counter];
            cout << (*rowIndex)[counter] << " " << (*columns)[counter] << " " << (*values)[counter] << endl;
        }
        counter++;
    }
    pair<MKL_INT,MKL_INT> ret(nonzeroTotal, colSize);
    theMatrix.close();
    return ret;
}

void makeMatrix(MKL_INT** rowIndex, MKL_INT** columns, double** values, int nnz, int colSize)
{
    *rowIndex = new MKL_INT[nnz];
    *columns = new MKL_INT[nnz];
    *values = new double[nnz];

    map<int, set<int>> indices;
    
    for(int k = 0; k < nnz; k++)
    {
        int i = rand() % colSize;
        int j = rand() % colSize;

        if(indices.find(i) == indices.end())
            indices[i] = {j};
        else if(indices[i].find(j) == indices[i].end())
            indices[i].insert(j);
        else
            k--;
    }

    int offset = 0;

    for(int i = 0; i < colSize; i++)
    {
        if(indices.find(i) != indices.end())
        {
            set<int> &rows = indices[i];
            for(auto it = rows.cbegin(); it != rows.cend(); ++it, ++offset)
            {
                (*rowIndex)[offset] = *it+1;
                (*columns)[offset] = i+1;
                (*values)[offset] = rand() % 4 + (float((rand() % 10)) / 10);
            }
        }
    }
}

void spmm(const MKL_INT& M, const MKL_INT& N, const MKL_INT& K, 
		MKL_INT* rowIndex_A, MKL_INT* columns_A, double* values_A,
		MKL_INT* rowIndex_B, MKL_INT* columns_B, double* values_B,
		MKL_INT* pointerB_C, MKL_INT* pointerE_C, MKL_INT* columns_C, double* values_C, MKL_INT& nnz, 
		sparse_matrix_t& handle_C_p)
{
    MKL_INT rows, cols;
    ofstream statusFile("status.txt");
    sparse_index_base_t indexing = SPARSE_INDEX_BASE_ONE;
    sparse_matrix_t csrA = NULL, csrB = NULL, csrc=NULL;
    statusFile << "SPARSE_STATUS_SUCCESS: " << SPARSE_STATUS_SUCCESS << endl;
    statusFile << "SPARSE_STATUS_ALLOC_FAILED: " << SPARSE_STATUS_ALLOC_FAILED << endl;
    statusFile << "SPARSE_STATUS_EXECUTION_FAILED: " << SPARSE_STATUS_EXECUTION_FAILED << endl;
    statusFile << "SPARSE_STATUS_INTERNAL_ERROR: " << SPARSE_STATUS_INTERNAL_ERROR << endl;
    statusFile << "SPARSE_STATUS_INVALID_VALUE: " << SPARSE_STATUS_INVALID_VALUE << endl;
    statusFile << "SPARSE_STATUS_NOT_INITIALIZED: " << SPARSE_STATUS_NOT_INITIALIZED << endl;
    statusFile << "SPARSE_STATUS_NOT_SUPPORTED: " << SPARSE_STATUS_NOT_SUPPORTED << endl;

    sparse_status_t stA = mkl_sparse_d_create_csr(&csrA, SPARSE_INDEX_BASE_ONE, M, K, rowIndex_A, rowIndex_A+1, columns_A, values_A);
    statusFile << "status A creation: " << stA << endl;
    sparse_status_t stB = mkl_sparse_d_create_csr(&csrB, SPARSE_INDEX_BASE_ONE, K, N, rowIndex_B, rowIndex_B+1, columns_B, values_B);
    statusFile << "status B creation: " << stB << endl;


    sparse_status_t opA = mkl_sparse_optimize(csrA);
    statusFile << "status opA: " << opA << endl;
    sparse_status_t opB = mkl_sparse_optimize(csrB);
    statusFile << "status opB: " << opB << endl;

    sparse_status_t spmmst = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE, csrA, csrA, &csrC);
    statusFile << "status SpMM: " << spmmst << endl;
    //sparse_status_t sporder = mkl_sparse_order(csrC);
    //statusFile << "status Ordered: " << sporder << endl;
    sparse_status_t ex = mkl_sparse_d_export_csr(csrC, &indexing, &rows, &cols, &pointerB_C, &pointerE_C, &columns_C, &values_C);
    statusFile << "status export: " << ex << endl;
    statusFile << "row size: " << rows << endl;
    //cout << rows << " " << cols;
    nnz = pointerE_C[M-1];

    sparse_status_t destA = mkl_sparse_destroy(csrA);
    statusFile << "status destroy A: " << destA << endl;
    sparse_status_t destB = mkl_sparse_destroy(csrB);
    statusFile << "status destroy B: " << destB << endl;
    handle_C_p = csrC;
    statusFile.close();
}

void freeHandle(sparse_matrix_t& handle)
{
    mkl_sparse_destroy(handle);
}

void writeFileC(MKL_INT* pointerB_C, MKL_INT* pointerE_C, MKL_INT* columns, double* values, MKL_INT nnz , MKL_INT row)
{
    ofstream MatC("matrixc.txt");
    ofstream rowFile("row.txt");
    MKL_INT iter = *pointerB_C;
    int counter = 0;

    for(MKL_INT i = 0; i < row; i++)
    {
        MKL_INT itb = pointerB_C[i];
        MKL_INT ite = pointerE_C[i];
        while(itb != ite)
        {
            rowFile << itb << " " << columns[itb] <<  endl;
            //MatC << "Row Index: " << i << "   Column Index: " <<  columns[itb] << "   Value: " << values[itb] << endl;            
            MatC << i << " " << columns[itb] << " " << values[itb] << " " << endl;
            itb++;
        }
    }
    MatC.close();
}

void writeFileB(MKL_INT* rowIndexB , MKL_INT* columnsB, double* valuesB, MKL_INT nnz)
{
    ofstream MatB("matrixb.txt");
    for(MKL_INT i = 0; i < nnz; i++)
    {
        MatB << rowIndexB[i] << " " << columnsB[i] << " " << valuesB[i] << endl;
        //MatB << "Row Index: " << rowIndexB[i] << "   Column Index: " << columnsB[i] << "   Value: " << valuesB[i] << endl; 
    }
    MatB.close();
}

 

I couldn't unzip examples_core_c.tgz  by using Intel® Developer Cloud because it is read-file system. ( I used "tar zxvf examples_core_c.tgz" command in correct path.) Is there any other way?

 

Thanks for helping, 

 

Ulas

 

0 Kudos
VarshaS_Intel
Moderator
2,549 Views

Hi,

 

Thanks for your reply.

 

>>I couldn't unzip examples_core_c.tgz by using Intel® Developer Cloud because it is read-file system. ( I used "tar zxvf examples_core_c.tgz" command in correct path.) Is there any other way?

No, as it is a read-only file path, we can't unzip it in the Intel MKL folder. We need to copy and untar it in the folder where we copied it.

 

We would see similar behavior while running on debug mode, we will get back to you on this issue.

 

Meanwhile, could you please try using the Intel oneAPI Command Prompt where we are able to get the expected results? Please find the below command and screenshot for more details:

icx /DMKL_ILP64 -I"%MKLROOT%\include" Source.cpp mkl_intel_ilp64.lib mkl_intel_thread.lib mkl_core.lib libiomp5md.lib

VarshaS_Intel_0-1691416884563.png

 

 

Thanks & Regards,

Varsha

 

0 Kudos
ulassezgn
Beginner
2,535 Views

Hi Varsha,

 

Thanks for replying,

 

i tried your suggested command and the command i used before. Results are in screenshot below. There is some problem with directories but i didn't understand well maybe because i am using devcloud idk. 

 

Screenshot:

ulassezgn_1-1691422512145.png

 

Thanks,

 

Ulas

0 Kudos
VarshaS_Intel
Moderator
2,526 Views

Hi,


Apologies for any difference in understanding. We are working on your issue for Intel DevCloud.


Could you please share us with the expected results of your code to understand your issue better?


Thanks & Regards,

Varsha


0 Kudos
ulassezgn
Beginner
2,503 Views

Hi,

Thanks for replying. The expected result is same with your screenshot but, in my local, i couldn't run your command as mentioned above. with the command i run, i got segFault.

 

"basic.mtx" file should be read as matrix A, 

"makeMatrix" function creates matrix B and "writeFileB" writes matrix B to "matrixb.txt",

"status.txt" file is wroten by status of the mkl functions.

spmm results are wroten to "matrixc.txt" file through "writeFileC" function.

 

Thanks, 

Ulas

  


@VarshaS_Intel wrote:

Hi,

 

Apologies for any difference in understanding. We are working on your issue for Intel DevCloud.

 

Could you please share us with the expected results of your code to understand your issue better?

 

Thanks & Regards,

Varsha



 

0 Kudos
ulassezgn
Beginner
2,474 Views

Have you had any chance to look at the error?

0 Kudos
VarshaS_Intel
Moderator
2,457 Views

Hi,

 

The size of the row pointer array's size should be (rows+1) instead of (nnz). Each rowPtr[i] should hold the initial index of row i. However, you are currently allocating an array of size nnz, storing the row number for each specific element.

 

Could you please retry with the updated size and get back to us if the issue persists?

 

Please check the below documentation for more details:

https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-c/2023-2/sparse-blas-csr-matrix-storage-format.html

 

Thanks & Regards,

Varsha


0 Kudos
VarshaS_Intel
Moderator
2,405 Views

Hi,


We have not heard back from you. Could you please provide us with an update on your issue?


Thanks & Regards,

Varsha


0 Kudos
Reply