I would like to use the C function LAPACKE_dgels.
I manage to use the example provided on http://www.netlib.org/lapack/lapacke.html
I also manage to use the function with a dynamic allocation of input/output B. It works as expected.
However, when I run valgrind on a static or dynamic memory allocation of input/output B, valgrind reports errors, such as 'Invalid free() / delete / delete[]'
Since the size of output B is different (lower) than the input, I guess the function performs memory allocation/deallocation on variable B.
I wonder how memory is handled for this function, and would like some advise to use properly the function LAPACKE_dgels with a dynamic allocated input B.
Should one use LAPACK_malloc?
Below is the example provided and the result from valgrind
- Code: Select all
[jacquenot@visu2 demo]$ cat demo_lapacke_dgels.c
#include <stdio.h>
#include <lapacke.h>
int main (int argc, const char * argv[])
{
double a[5][3] = {1,1,1,2,3,4,3,5,2,4,2,5,5,4,3};
double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
lapack_int info,m,n,lda,ldb,nrhs;
int i,j;
m = 5;
n = 3;
nrhs = 2;
lda = 3;
ldb = 2;
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);
for(i=0;i<n;i++)
{
for(j=0;j<nrhs;j++)
{
printf("%lf ",b[i][j]);
}
printf("\n");
}
return(info);
}
[jacquenot@visu2 Release]$ ./demo_lapacke_dgels
2.000000 1.000000
1.000000 1.000000
1.000000 2.000000
[jacquenot@visu2 Release]$ valgrind ./demo_lapacke_dgels
==5860== Memcheck, a memory error detector
==5860== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==5860== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==5860== Command: ./demo_lapacke_dgels
==5860==
2.000000 1.000000
1.000000 1.000000
1.000000 2.000000
==5860== Invalid free() / delete / delete[]
==5860== at 0x4A05D21: free (vg_replace_malloc.c:325)
==5860== by 0x378750ADAA: ??? (in /lib64/libc-2.5.so)
==5860== by 0x378750A9A1: ??? (in /lib64/libc-2.5.so)
==5860== by 0x48024E8: _vgnU_freeres (vg_preloaded.c:62)
==5860== by 0x37874334E4: exit (in /lib64/libc-2.5.so)
==5860== by 0x378741D99A: (below main) (in /lib64/libc-2.5.so)
==5860== Address 0x5abe0b8 is not stack'd, malloc'd or (recently) free'd
==5860==
==5860==
==5860== HEAP SUMMARY:
==5860== in use at exit: 0 bytes in 0 blocks
==5860== total heap usage: 18 allocs, 20 frees, 3,561 bytes allocated
==5860==
==5860== All heap blocks were freed -- no leaks are possible
==5860==
==5860== For counts of detected and suppressed errors, rerun with: -v
==5860== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4)
[jacquenot@visu2 Release]$ ./demo_lapacke_dgels
2.000000 1.000000
1.000000 1.000000
1.000000 2.000000
[jacquenot@visu2 Release]$ ldd demo_lapacke_dgels
linux-vdso.so.1 => (0x00007fffd9ffd000)
liblapacke.so => ./liblapacke.so (0x00002b8a5ebb8000)
liblapack.so => ./liblapack.so (0x00002b8a5ef6e000)
libblas.so => ./libblas.so (0x00002b8a5f7fe000)
libc.so.6 => /lib64/libc.so.6 (0x0000003787400000)
libtmglib.so => ./libtmglib.so (0x00002b8a5fa6c000)
libm.so.6 => /lib64/libm.so.6 (0x0000003f0c800000)
libgfortran.so.3 => /usr/lib64/libgfortran.so.3 (0x00002b8a5fcd3000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003387600000)
/lib64/ld-linux-x86-64.so.2 (0x0000003787000000)
Here is an example of what valgrind reports on dynamic allocated input B
- Code: Select all
[jacquenot@visu2 DEBUG]$ ./run_all_unit_test_tools_math_lapack --gtest_filter=ToolsMathLapackTest.dgels005x003
Running main() from gtest_main.cc
Note: Google Test filter = ToolsMathLapackTest.dgels005x003
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ToolsMathLapackTest
[ RUN ] ToolsMathLapackTest.dgels005x003
[ OK ] ToolsMathLapackTest.dgels005x003 (0 ms)
[----------] 1 test from ToolsMathLapackTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
[jacquenot@visu2 DEBUG]$ valgrind ./run_all_unit_test_tools_math_lapack --gtest_filter=ToolsMathLapackTest.dgels005x003
==26590== Memcheck, a memory error detector
==26590== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==26590== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==26590== Command: ./run_all_unit_test_tools_math_lapack --gtest_filter=ToolsMathLapackTest.dgels005x003
==26590==
Running main() from gtest_main.cc
Note: Google Test filter = ToolsMathLapackTest.dgels005x003
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ToolsMathLapackTest
[ RUN ] ToolsMathLapackTest.dgels005x003
[ OK ] ToolsMathLapackTest.dgels005x003 (109 ms)
[----------] 1 test from ToolsMathLapackTest (132 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (205 ms total)
[ PASSED ] 1 test.
==26590== Invalid free() / delete / delete[]
==26590== at 0x4A05D21: free (vg_replace_malloc.c:325)
==26590== by 0x378750ADAA: ??? (in /lib64/libc-2.5.so)
==26590== by 0x378750A9A1: ??? (in /lib64/libc-2.5.so)
==26590== by 0x48024E8: _vgnU_freeres (vg_preloaded.c:62)
==26590== by 0x37874334E4: exit (in /lib64/libc-2.5.so)
==26590== by 0x378741D99A: (below main) (in /lib64/libc-2.5.so)
==26590== Address 0x6449b58 is not stack'd, malloc'd or (recently) free'd
==26590==
==26590==
==26590== HEAP SUMMARY:
==26590== in use at exit: 0 bytes in 0 blocks
==26590== total heap usage: 358 allocs, 360 frees, 64,566 bytes allocated
==26590==
==26590== All heap blocks were freed -- no leaks are possible
==26590==
==26590== For counts of detected and suppressed errors, rerun with: -v
==26590== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 4 from 4)
[jacquenot@visu2 DEBUG]$
Best regards
Guillaume Jacquenot

