2

I do a blas matrix/vector product with this simple code:

#include "mkl.h"
#include <stdio.h>
int main(){
const int M = 2;
const int N = 3;
double *x = new double[N];
double *A = new double[M*N];
double *b = new double[M];
for (int i = 0; i < M; i++){
    b[i] = 0.0; //Not necessary but anyway...
    for (int j = 0; j < N; j++){
        A[j * M + i] = i + j * 2;
    }
}
for (int j = 0; j < N; j++)
    x[j] = j*j;
const int incr = 1;
const double alpha = 1.0;
const double beta = 0.0;
const char no = 'N';
dgemv(&no, &M, &N, &alpha, A, &M, x, &incr, &beta, b, &incr );
printf("b = [%e %e]'\n",b[0],b[1]);

delete[] x;
delete[] A;
delete[] b;

}

While the displayed result is as expected ([18, 23]), Intel Inspector finds one invalid memory access and 2 invalid partial memory access when calling dgemv. The invalid memory access and one invalid partial memory access are related to memory allocated corresponding to the vector b. The second invalid partial memory access is related with the memory allocated for A. I do not get any error if I use a static array.

It also happens with other MKL functions, such as dgesv or when I try to use cblas_dgemv. I use Intel Inspector XE 2016 and intel C++ Compiler 16.0 with MKL sequential.

Is my dgemv call wrong, or is that a false positive. Anyone experienced that?

Thanks

EDIT:

As suggested by Josh Milthorpe: the error appears only on small-size arrays, probably because MKL is trying to access memory in large chunks for efficiency.

I did several tests, and M needs to be at least 20 in order to not get an error. N can be any positive number. I suppose that this is not a bug, and MKL is just accessing memory outside of the allocated space for the matrix, but does not alter or really use it.

seb007
  • 488
  • 3
  • 19
  • If all your arrays have a size fixed at the time of compilation, why don't you use actual arrays instead of allocating dynamically? – Some programmer dude Oct 28 '16 at 11:04
  • This is just a minimal example to reproduce the problem. On this example blas is a bit overkill and I could also do the product by hand ;-). Variable size arrays are needed in my larger code. – seb007 Oct 28 '16 at 11:23
  • 1
    Your `dgemv` call looks fine to me. I suspect MKL is trying to access memory in large chunks for efficiency, which is causing some bug for small matrix/vector sizes. Does Inspector report these accesses for larger input sizes? – Josh Milthorpe Oct 28 '16 at 11:27
  • 1
    Excellent guess: I have no error detected with larger arrays (e.g. M=100, N=200). Now a follow-up question: do you think MKL actually does something wrong for small matrices (like altering memory outside of the arrays), or should I just ignore the reported errors? This sizes of the matrices are usually large in my code, but sometimes they are small. – seb007 Oct 28 '16 at 11:37
  • I couldn't say; if the invalid access is only a read, it may not cause any problems, whereas if it's a write, it could cause trouble later on. You might want to report it to Intel. – Josh Milthorpe Oct 28 '16 at 16:24
  • I reported this on the intel forum. Not sure that I understand correctly, but it looks like it is safe to use dgemv for any matrix: https://software.intel.com/en-us/forums/intel-math-kernel-library/topic/701545 – seb007 Nov 03 '16 at 10:17

0 Answers0