Dimorga
Dimorga

Reputation: 11

"Invalid read of size 8" warning from Valgrind when calling zhemv blas function in C++

I'm computing a hermitian (self-adjoint) matrix times a complex vector multiplication by means of ZHEMV in BLAS by calling the function from a C++ interface. The problem I see is getting an "Invalid read of size 8" warning from Valgrind when performing the matrix vector multiplication for ODD DIMENSIONS only.

This is the code that gives me such a warning:

#include <iostream>
#include <vector>
#include <complex>
#include <cmath>
#include <cstdlib>
#include <cassert>

typedef std::complex<double> Complex;
typedef std::vector<Complex> cvector;

extern "C"
{
    int zhemv_(const char &uplo, int *n, Complex *alpha, Complex *v, int *lda, Complex *x, int *incx, Complex *beta, Complex *w, int *incy);
}

int main()
{
    int Dim;
    int ld = 1;
    Complex unit(1,0);
    Complex zero(0,0);

    std::cout<<"Read dimension:"<<std::endl;
    std::cin>>Dim ;

    std::vector<Complex> vec1(Dim,1);
    std::vector<Complex> vec2(Dim,2);
    std::vector<Complex> Ham(Dim*Dim,1.5);

    zhemv_('U',&Dim,&unit,&Ham[0],&Dim,&vec1[0],&ld,&zero,&vec2[0],&ld);
}

The warning I get is the following:

==363614== Invalid read of size 8

==363614== at 0x65316CF: zhemv_U_HASWELL (in /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so)

Up to know I'm stuck, and I guess this could be related to the way I insert the n, lda, incx and incy integers in zhemv. I've tried different ways, but I don't get rid of the warning from Valgrind. Also, the BLAS documentation doesn't seem too helpful at this point.

This code is a MWE that I extracted from a larger project, and is the probable cause of a segmentation fault I obtain in the larger program.

Upvotes: 1

Views: 60

Answers (1)

Paul Floyd
Paul Floyd

Reputation: 6936

"get rid of the warning from Valgrind"

I hope that you aim is to fix the error in your code. If you are reading from invalid memory it means that your code has Undefined Behaviour. Anything could happen as a result, the most likely being incorrect results or a crash.

Read the error report

You only posted the first two lines of the error report (where the error in the code comes from). You didn't post the information that memcheck provides about the error address. If you can work out which variable is at fault then fixing the error will be a lot easier. If you can't work out which variable it is from the log, use vgdb.

Check zhemv_ alignment requirements

I think that this is the most likely cause of the problem. For instance, if zhemv_is expecting 32byte alignment for the complex arrays and you are allocating them with 16byte alignment then your even sized arrays will be OK but your odd sized ones won't.

Upvotes: 1

Related Questions