Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
21 Views

Insight as to why Valgrind shows memory leak for LAPACKE

  1. This is the first time I've ever used Intel's MKL LAPACKE and Valgrind. Unfortunately, I get an error with something I have little to no experience with. I could use some advice on how to cure a potential memory leak. I'm using Intel's MKL library, so I highly assume this issue is my fault, but I'm not exactly sure what to look for or how to debug the issue.

Here is the example file I'm testing from the MKL installation

  1. /*******************************************************************************
  2. * Copyright 2010-2016 Intel Corporation All Rights Reserved.
  3. *
  4. * The source code,  information  and material  ("Material") contained  herein is
  5. * owned by Intel Corporation or its  suppliers or licensors,  and  title to such
  6. * Material remains with Intel  Corporation or its  suppliers or  licensors.  The
  7. * Material  contains  proprietary  information  of  Intel or  its suppliers  and
  8. * licensors.  The Material is protected by  worldwide copyright  laws and treaty
  9. * provisions.  No part  of  the  Material   may  be  used,  copied,  reproduced,
  10. * modified, published,  uploaded, posted, transmitted,  distributed or disclosed
  11. * in any way without Intel's prior express written permission.  No license under
  12. * any patent,  copyright or other  intellectual property rights  in the Material
  13. * is granted to  or  conferred  upon  you,  either   expressly,  by implication,
  14. * inducement,  estoppel  or  otherwise.  Any  license   under such  intellectual
  15. * property rights must be express and approved by Intel in writing.
  16. *
  17. * Unless otherwise agreed by Intel in writing,  you may not remove or alter this
  18. * notice or  any  other  notice   embedded  in  Materials  by  Intel  or Intel's
  19. * suppliers or licensors in any way.
  20. *******************************************************************************/
  21. /*
  22.    LAPACKE_chesv Example.
  23.    ======================
  24.    The program computes the solution to the system of linear equations
  25.    with a Hermitian matrix A and multiple right-hand sides B,
  26.    where A is the coefficient matrix:
  27.    ( -2.90,  0.00) (  0.31,  4.46) (  9.66, -5.66) ( -2.28,  2.14)
  28.    (  0.31, -4.46) ( -7.93,  0.00) (  9.55, -4.62) ( -3.51,  3.11)
  29.    (  9.66,  5.66) (  9.55,  4.62) (  0.30,  0.00) (  9.33, -9.66)
  30.    ( -2.28, -2.14) ( -3.51, -3.11) (  9.33,  9.66) (  2.40,  0.00)
  31.    and B is the right-hand side matrix:
  32.    ( -5.69, -8.21) ( -2.83,  6.46)
  33.    ( -3.57,  1.99) ( -7.64,  1.10)
  34.    (  8.42, -9.83) ( -2.33, -4.23)
  35.    ( -5.00,  3.85) (  6.48, -3.81)
  36.    Description.
  37.    ============
  38.    The routine solves for X the complex system of linear equations A*X = B,
  39.    where A is an n-by-n Hermitian matrix, the columns of matrix B are
  40.    individual right-hand sides, and the columns of X are the corresponding
  41.    solutions.
  42.    The diagonal pivoting method is used to factor A as A = U*D*UH or
  43.    A = L*D*LH, where U (or L) is a product of permutation and unit upper
  44.    (lower) triangular matrices, and D is Hermitian and block diagonal with
  45.    1-by-1 and 2-by-2 diagonal blocks.
  46.    The factored form of A is then used to solve the system of equations A*X = B.
  47.    Example Program Results.
  48.    ========================
  49.  LAPACKE_chesv (row-major, high-level) Example Program Results
  50.  Solution
  51.  (  0.22, -0.95) ( -1.13,  0.18)
  52.  ( -1.42, -1.30) (  0.70,  1.13)
  53.  ( -0.65, -0.40) (  0.04,  0.07)
  54.  ( -0.48,  1.35) (  1.15, -0.27)
  55.  Details of factorization
  56.  (  3.17,  0.00) (  7.32,  3.28) ( -0.36,  0.06) (  0.20, -0.82)
  57.  (  0.00,  0.00) (  0.03,  0.00) ( -0.48,  0.03) (  0.25, -0.76)
  58.  (  0.00,  0.00) (  0.00,  0.00) (  0.30,  0.00) (  9.33, -9.66)
  59.  (  0.00,  0.00) (  0.00,  0.00) (  0.00,  0.00) (  2.40,  0.00)
  60.  Pivot indices
  61.      -1     -1     -3     -3
  62. */
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65. #include "mkl_lapacke.h"
  66. /* Auxiliary routines prototypes */
  67. extern void print_matrix( char* desc, MKL_INT m, MKL_INT n, MKL_Complex8* a, MKL_INT lda );
  68. extern void print_int_vector( char* desc, MKL_INT n, MKL_INT* a );
  69. /* Parameters */
  70. #define N 4
  71. #define NRHS 2
  72. #define LDA N
  73. #define LDB NRHS
  74. /* Main program */
  75. int main() {
  76.     /* Locals */
  77.     MKL_INT n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
  78.     /* Local arrays */
  79.     MKL_INT ipiv;
  80.     MKL_Complex8 a[LDA*N] = {
  81.        {-2.90f,  0.00f}, { 0.31f,  4.46f}, { 9.66f, -5.66f}, {-2.28f,  2.14f},
  82.        { 0.00f,  0.00f}, {-7.93f,  0.00f}, { 9.55f, -4.62f}, {-3.51f,  3.11f},
  83.        { 0.00f,  0.00f}, { 0.00f,  0.00f}, { 0.30f,  0.00f}, { 9.33f, -9.66f},
  84.        { 0.00f,  0.00f}, { 0.00f,  0.00f}, { 0.00f,  0.00f}, { 2.40f,  0.00f}
  85.     };
  86.     MKL_Complex8 b[LDB*N] = {
  87.        {-5.69f, -8.21f}, {-2.83f,  6.46f},
  88.        {-3.57f,  1.99f}, {-7.64f,  1.10f},
  89.        { 8.42f, -9.83f}, {-2.33f, -4.23f},
  90.        {-5.00f,  3.85f}, { 6.48f, -3.81f}
  91.     };
  92.     /* Executable statements */
  93.     printf( "LAPACKE_chesv (row-major, high-level) Example Program Results\n" );
  94.     /* Solve the equations A*X = B */
  95.     info = LAPACKE_chesv( LAPACK_ROW_MAJOR, 'U', n, nrhs, a, lda, ipiv,
  96.             b, ldb );
  97.     /* Check for the exact singularity */
  98.     if( info > 0 ) {
  99.         printf( "The element of the diagonal factor " );
  100.         printf( "D(%i,%i) is zero, so that D is singular;\n", info, info );
  101.         printf( "the solution could not be computed.\n" );
  102.         exit( 1 );
  103.     }
  104.     /* Print solution */
  105.     print_matrix( "Solution", n, nrhs, b, ldb );
  106.     /* Print details of factorization */
  107.     print_matrix( "Details of factorization", n, n, a, lda );
  108.     /* Print pivot indices */
  109.     print_int_vector( "Pivot indices", n, ipiv );
  110.     exit( 0 );
  111. } /* End of LAPACKE_chesv Example */
  112. /* Auxiliary routine: printing a matrix */
  113. void print_matrix( char* desc, MKL_INT m, MKL_INT n, MKL_Complex8* a, MKL_INT lda ) {
  114.     MKL_INT i, j;
  115.     printf( "\n %s\n", desc );
  116.     for( i = 0; i < m; i++ ) {
  117.         for( j = 0; j < n; j++ )
  118.             printf( " (%6.2f,%6.2f)", a[i*lda+j].real, a[i*lda+j].imag );
  119.         printf( "\n" );
  120.     }
  121. }
  122. /* Auxiliary routine: printing a vector of integers */
  123. void print_int_vector( char* desc, MKL_INT n, MKL_INT* a ) {
  124.     MKL_INT j;
  125.     printf( "\n %s\n", desc );
  126.     for( j = 0; j < n; j++ ) printf( " %6i", a );
  127.     printf( "\n" );
  128. }

 

  1. Valgrind output at the following line
  2.  info = LAPACKE_chesv( LAPACK_ROW_MAJOR, 'U', n, nrhs, a, lda, ipiv, b, ldb );
  3.  
  4. Multiple markers at this line
        - 69,664 bytes in 1 blocks are possibly lost in loss record 11 of 11 [PID:
         5535]
        - 288 bytes in 1 blocks are possibly lost in loss record 9 of 11 [PID: 5535]
        - 192 bytes in 1 blocks are possibly lost in loss record 6 of 11 [PID: 5535]
        - 192 bytes in 1 blocks are possibly lost in loss record 5 of 11 [PID: 5535]
        - 256 bytes in 1 blocks are possibly lost in loss record 8 of 11 [PID: 5535]
        - 224 bytes in 1 blocks are possibly lost in loss record 7 of 11 [PID: 5535]
  5.  
  6. I did not alter the example file from Intel in anyway. I simply built it and ran it. Here are the details from the Valgrind log file:
  7.  
  8. ==5535== 192 bytes in 1 blocks are possibly lost in loss record 5 of 11
  9. ==5535==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  10. ==5535==    by 0x5898A3D: mkl_serv_allocate (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  11. ==5535==    by 0x51C3192: LAPACKE_chesv (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  12. ==5535==    by 0x400A59: main (/home/troy/Eclipse_INTEL/mkl_lapacke_gcc/Debug/../chesv_row.c:112)
  13. ==5535==
  14. ==5535== 192 bytes in 1 blocks are possibly lost in loss record 6 of 11
  15. ==5535==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  16. ==5535==    by 0x5898A3D: mkl_serv_allocate (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  17. ==5535==    by 0x5074A1E: CHESV (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  18. ==5535==    by 0x51C3491: LAPACKE_chesv_work (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  19. ==5535==    by 0x51C31F2: LAPACKE_chesv (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  20. ==5535==    by 0x400A59: main (/home/troy/Eclipse_INTEL/mkl_lapacke_gcc/Debug/../chesv_row.c:112)
  21. ==5535==
  22. ==5535== 224 bytes in 1 blocks are possibly lost in loss record 7 of 11
  23. ==5535==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  24. ==5535==    by 0x5898A3D: mkl_serv_allocate (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  25. ==5535==    by 0x51C33D9: LAPACKE_chesv_work (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  26. ==5535==    by 0x51C31F2: LAPACKE_chesv (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  27. ==5535==    by 0x400A59: main (/home/troy/Eclipse_INTEL/mkl_lapacke_gcc/Debug/../chesv_row.c:112)
  28. ==5535==
  29. ==5535== 256 bytes in 1 blocks are possibly lost in loss record 8 of 11
  30. ==5535==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  31. ==5535==    by 0x5899F49: mm_account_ptr_by_tid..0 (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  32. ==5535==    by 0x5898399: mkl_serv_allocate (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  33. ==5535==    by 0x51C3192: LAPACKE_chesv (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  34. ==5535==    by 0x400A59: main (/home/troy/Eclipse_INTEL/mkl_lapacke_gcc/Debug/../chesv_row.c:112)
  35. ==5535==
  36. ==5535== 288 bytes in 1 blocks are possibly lost in loss record 9 of 11
  37. ==5535==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  38. ==5535==    by 0x5898A3D: mkl_serv_allocate (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  39. ==5535==    by 0x51C338A: LAPACKE_chesv_work (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  40. ==5535==    by 0x51C31F2: LAPACKE_chesv (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  41. ==5535==    by 0x400A59: main (/home/troy/Eclipse_INTEL/mkl_lapacke_gcc/Debug/../chesv_row.c:112)
  42. ==5535==
  43. ==5535== 69,664 bytes in 1 blocks are possibly lost in loss record 11 of 11
  44. ==5535==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  45. ==5535==    by 0x589A184: mm_account_ptr_by_tid..0 (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  46. ==5535==    by 0x5898399: mkl_serv_allocate (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_core.so)
  47. ==5535==    by 0x51C3192: LAPACKE_chesv (in /usr/local/INTEL/compilers_and_libraries_2016.2.181/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so)
  48. ==5535==    by 0x400A59: main (/home/troy/Eclipse_INTEL/mkl_lapacke_gcc/Debug/../chesv_row.c:112)
  49. ==5535==
  50.  
  51. I can post more if need be. Here is my original post on stack overflow: http://stackoverflow.com/questions/36197527/insight-as-to-why-valgrind-shows-memory-leak-for-lapacke...
  52. From what I gathered there, "I'm suspecting the MKL libraries are never released, which causes the MKL-dependant variables to be "possibly lost" but i could be wrong." That being said, I thought I'd reach out to Intel and see if they had any input. I just recently (today) update my student parallel studio package to the latest version (as you can see above in the directory paths). I should note that I'm using Ubuntu 14.04 LTS. I added this to my bashrc file
  53.  
  54.      source /usr/local/INTEL/parallel_studio_xe_2016.2.062/psxevars.sh
  55.  
  56. and in my .profile, I have
  57.  
  58.      INTEL_LICENSE_FILE=/opt/intel/licenses
  59.      export INTEL_LICENSE_FILE
  60.  
  61. Since I work on Ubuntu, I create a file under
  62.  
  63.      /etc/ld.so.conf.d/intel.conf
  64.  
  65. and add the following in the file
  66.  
  67.      /usr/local/INTEL/compilers_and_libraries/linux/lib/intel64
  68.      /usr/local/INTEL/compilers_and_libraries/linux/mkl/lib/intel64
  69.  
  70. sourcing the following completes the setup
  71.  
  72.      source ldconfig
  73.  
  74. Also, I should add that I'm using eclipse to run my software. Is there a special way to setup the MKL libraries with eclipse? I followed these instructions, so I'm not sure where my potential error is. My program gives me an answer, and everything "seems" to be correct, but I ran Valgrind for the first time and I got the errors listed above. I should note that I've already tried to work with the MKL tools to provide the specific library options. I've also tried the command line interface tool that came in the MKL directory. I encourage someone from Intel to run Valgrind against LAPACKE (any of their examples) and see if they get similar results. If not, can they provide me with their build commands (compile/link) so that I can try to resolve this issue. Thanks.
0 Kudos
4 Replies
Highlighted
Beginner
21 Views

Does anyone have any feedback?

0 Kudos
Highlighted
Moderator
21 Views

actually this is not real memory leaks, pls have a look at the MKL UsersGuide - chapter "Avoiding Memory Leaks in MKL"

0 Kudos
Highlighted
Beginner
21 Views

Can you provide a link? I went to this site, downloaded the users guide, and searched for "Avoiding Memory Leaks in MKL" but got no results....NEVERMIND, I found it. I had to search memory leaks, pg 61. I'll read it over and follow up if need be.

0 Kudos
Highlighted
Beginner
21 Views

Thank you Gennady. I read the section of the user guide you pointed to (quoted below), and checked this link on further usage of "mkl_free_buffers". In the end, adding "mkl_free_buffers();" to the end of program fixed the issue. Valgrind "found no problems to report".

Avoiding Memory Leaks in Intel MKL
When running, Intel MKL allocates and deallocates internal buffers to facilitate better performance. However,
in some cases this behavior may result in memory leaks.
To avoid memory leaks, you can do either of the following:
Set the
MKL_DISABLE_FAST_MM
environment variable to 1 or call the
mkl_disable_fast_mm()
function.
Be aware that this change may negatively impact performance of some Intel MKL functions, especially for
small problem sizes.
Call the
mkl_free_buffers()
function or the
mkl_thread_free_buffers()
function in the current
thread.
For the descriptions of the memory functions, see the Intel MKL Reference Manual, available in the Intel
Software Documentation Library.
0 Kudos