0

I will use cblas in a C++ project, so I am trying to learn it. I wrote a simple code, but it is giving segmentation fault. My code is

#include <cmath>
#include <vector>
#include <algorithm>


#include <cstring>


#include <mkl.h>

#include <mkl_types.h>
#include <mkl_cblas.h>
#include <mkl_lapacke.h>

template <typename T>
inline T** allocate_matrix ( const int row, const int col )
{
    T **a = NULL;
    T *base = NULL;

    int ret = posix_memalign ( ( void** ) &a, 16, row * sizeof ( T* ) );
    if ( ret != 0 )
    {
    throw std::bad_alloc();
    }

    ret = posix_memalign ( ( void** ) &base, 16, row * col * sizeof ( T ) );
    if ( ret != 0 )
    {
    throw std::bad_alloc();
    }

    for ( int r = 0; r < row; ++r )
    {
    a[r] = &base[r * col];
    }

    return a;
}


template <typename T>
T* allocate_vector ( size_t size )
{
    T* mem = NULL;

    int ret = posix_memalign ( ( void** ) &mem, 16, size * sizeof ( T ) );
    if ( ret != 0 )
    {
    throw std::bad_alloc();
    }

    return mem;
}



typedef float          real_type;


void main()
{


    float *m_working_vector_a;
    float *m_working_vector_b;
    float **weightt;

    m_working_vector_a = allocate_vector<real_type> ( 2 );
    m_working_vector_b = allocate_vector<real_type> ( 2 );
    weightt = allocate_matrix<real_type> ( 2,2 );


    for ( int i = 0; i < 2; ++i ) m_working_vector_a[i] = -2;
    for ( int i = 0; i < 2; ++i ) m_working_vector_b[i] = -2;
    for ( int i = 0; i < 2; ++i )
    for ( int j = 0; j < 2; ++j )
        weightt[i][j] = -2;

    cblas_sger ( CblasRowMajor,
        2, 2,
        1.0f,
        m_working_vector_a, 1,
        m_working_vector_b, 1,
        weightt[0], 2 );


}

I compiled it on Kubuntu 14.10 with Intel® Parallel Studio XE 2015. I used the commands

icpc  -g -O0   -I/opt/intel/composerxe/mkl/include    -c  dene.cpp
icpc  -g -O0   -I/opt/intel/composerxe/mkl/include     dene.o   -Wl,--start-group /opt/intel/composerxe/mkl/lib/intel64/libmkl_intel_ilp64.a /opt/intel/composerxe/mkl/lib/intel64/libmkl_core.a /opt/intel/composerxe/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group    -liomp5 -o dene

Valgrind gives the following result

valgrind --tool=memcheck --leak-check=yes --track-origins=yes dene

==8148== Memcheck, a memory error detector
==8148== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==8148== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==8148== Command: dene
==8148== 
==8148== Conditional jump or move depends on uninitialised value(s)
==8148==    at 0x401B19: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Conditional jump or move depends on uninitialised value(s)
==8148==    at 0x401B26: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Conditional jump or move depends on uninitialised value(s)
==8148==    at 0x402031: mkl_blas_errchk_sger (in /home/si/sim/dene)
==8148==    by 0x401C4B: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Conditional jump or move depends on uninitialised value(s)
==8148==    at 0x40205E: mkl_blas_errchk_sger (in /home/si/sim/dene)
==8148==    by 0x401C4B: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Conditional jump or move depends on uninitialised value(s)
==8148==    at 0x408D9D: mkl_blas_mc3_xsaxpy (in /home/si/sim/dene)
==8148==    by 0x402BFD: mkl_blas_mc3_sger (in /home/si/sim/dene)
==8148==    by 0x401D34: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Conditional jump or move depends on uninitialised value(s)
==8148==    at 0x408D9D: mkl_blas_mc3_xsaxpy (in /home/si/sim/dene)
==8148==    by 0x402C56: mkl_blas_mc3_sger (in /home/si/sim/dene)
==8148==    by 0x401D34: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Use of uninitialised value of size 8
==8148==    at 0x409CF5: mkl_blas_mc3_xsaxpy (in /home/si/sim/dene)
==8148==    by 0x402C56: mkl_blas_mc3_sger (in /home/si/sim/dene)
==8148==    by 0x401D34: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Uninitialised value was created by a stack allocation
==8148==    at 0x401679: main (dene.cpp:66)
==8148== 
==8148== Invalid read of size 4
==8148==    at 0x409CF5: mkl_blas_mc3_xsaxpy (in /home/si/sim/dene)
==8148==    by 0x402C56: mkl_blas_mc3_sger (in /home/si/sim/dene)
==8148==    by 0x401D34: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  Address 0x3c06175138 is not stack'd, malloc'd or (recently) free'd
==8148== 
==8148== 
==8148== Process terminating with default action of signal 11 (SIGSEGV)
==8148==  Access not within mapped region at address 0x3C06175138
==8148==    at 0x409CF5: mkl_blas_mc3_xsaxpy (in /home/si/sim/dene)
==8148==    by 0x402C56: mkl_blas_mc3_sger (in /home/si/sim/dene)
==8148==    by 0x401D34: SGER (in /home/si/sim/dene)
==8148==    by 0x401B75: cblas_sger (in /home/si/sim/dene)
==8148==    by 0x4017B6: main (dene.cpp:75)
==8148==  If you believe this happened as a result of a stack
==8148==  overflow in your program's main thread (unlikely but
==8148==  possible), you can try to increase the size of the
==8148==  main thread stack using the --main-stacksize= flag.
==8148==  The main thread stack size used in this run was 8388608.
==8148== 
==8148== HEAP SUMMARY:
==8148==     in use at exit: 48 bytes in 4 blocks
==8148==   total heap usage: 4 allocs, 0 frees, 48 bytes allocated
==8148== 
==8148== LEAK SUMMARY:
==8148==    definitely lost: 0 bytes in 0 blocks
==8148==    indirectly lost: 0 bytes in 0 blocks
==8148==      possibly lost: 0 bytes in 0 blocks
==8148==    still reachable: 48 bytes in 4 blocks
==8148==         suppressed: 0 bytes in 0 blocks
==8148== Reachable blocks (those to which a pointer was found) are not shown.
==8148== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==8148== 
==8148== For counts of detected and suppressed errors, rerun with: -v
==8148== ERROR SUMMARY: 8 errors from 8 contexts (suppressed: 1 from 1)
Segmentation fault (core dumped)

Could you please advice how to fix this?

Dundun
  • 1
  • BLAS requires matrices to be stored in pitched linear memory. There is no support for matrices stored as arrays of pointers. – talonmies Oct 25 '14 at 14:58
  • ret = posix_memalign ( ( void** ) &base, 16, row * col * sizeof ( T ) ); is creating a big linear memory with a size of column times rows. weightt[0] is giving its address to cblas. Shouldn't it work? – Dundun Oct 25 '14 at 15:47

0 Answers0