-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug in the complex least-squares solver #823
Comments
I just re-ran the code, and here is what I observe
Although the error is kind of random (sometimes 139, sometimes 134), it is always the same thing:
The same applies if I declare all arrays as |
I'm closing this issue for the moment. While in the train, I played around with the code and found that it is very subtle to reproduce. I'm not quite sure what is happening. I'll re-open some time later once I've investigated a bit more (I don't want to make you lose time on this for the moment). |
Sorry for the back and forth but I've been able to isolate the smallest MWE and its quite reproducible. Here is the code program main
use stdlib_kinds, only: dp
use stdlib_linalg, only: solve_lstsq
implicit none
! Dimension of the problem.
integer, parameter :: n = 42
! Data for the least-squares problem.
complex(dp) :: A(n+1, n), b(n+1), x_true(n), x_lstsq(n)
! Internal variables.
real(dp), allocatable :: tmp(:, :, :), tmp_vec(:, :)
! Zero-out data.
A = 0.0_dp ; b = 0.0_dp ; x_true = 0.0_dp ; x_lstsq = 0.0_dp
allocate(tmp(n+1, n, 2)) ; tmp = 0.0_dp
allocate(tmp_vec(n, 2)) ; tmp_vec = 0.0_dp
! Generate a random complex least-squares problem of size (n+1, n).
call random_number(tmp) ; call random_number(tmp_vec)
A = cmplx(tmp(:, :, 1), tmp(:, :, 2), kind=dp)
x_true = cmplx(tmp_vec(:, 1), tmp_vec(:, 2), kind=dp)
b = matmul(A, x_true)
! Solve the lstsq problem.
call solve_lstsq(A, b, x_lstsq)
! Dellocate arrays.
deallocate(tmp, tmp_vec)
end program main This code crashes with the following stacktrace
The odd thing is that the following code runs smoothly program main
use stdlib_kinds, only: dp
use stdlib_linalg, only: solve_lstsq
implicit none
! Dimension of the problem.
integer, parameter :: n = 42
! Data for the least-squares problem.
complex(dp) :: A(n+1, n), b(n+1), x_true(n), x_lstsq(n)
! Internal variables.
real(dp), allocatable :: tmp(:, :, :), tmp_vec(:, :)
! Zero-out data.
A = 0.0_dp ; b = 0.0_dp ; x_true = 0.0_dp ; x_lstsq = 0.0_dp
allocate(tmp(n+1, n, 2)) ; tmp = 0.0_dp
allocate(tmp_vec(n, 2)) ; tmp_vec = 0.0_dp
! Generate a random complex least-squares problem of size (n+1, n).
call random_number(tmp) ; call random_number(tmp_vec)
A = cmplx(tmp(:, :, 1), tmp(:, :, 2), kind=dp)
x_true = cmplx(tmp_vec(:, 1), tmp_vec(:, 2), kind=dp)
b = matmul(A, x_true)
! Dellocate arrays.
deallocate(tmp, tmp_vec)
! Solve the lstsq problem.
call solve_lstsq(A, b, x_lstsq)
end program main The only difference is that I deallocate the |
I tested your MWE ... weirdly, if you use |
@loiseaujc @jalvesz, sorry - I just noticed that you've filed this bug. If you'd like my intervention, please cc me as I don't receive notifications from stdlib issues otherwise. I've just tested it on macOS, gcc 13.2.0 (ARM) and the Godbolt example runs just fine in all configurations ( |
Maybe the version of stdlib in Compiler Explorer is not up-to-date ? |
Description
Consider the following code
Although it looks quite contrived, it is adapted from a naïve implementation of a
gmres
solver whereA
actually is upper Hessenberg. It thus corresponds to the increasingly larger least-squares problem that needs to be solved at each iteration of thegmres
solver. In this MWE,A
is generated as a random matrix just to make sure the error I'll report does not depends on the structure of the matrix.Initially, everything runs just fine. However, the code crashes for
k
equal 39 with the following error:Not that the error reported actually randomly switches between
double free or corruption (out)
(withSTOP 134
),SIGSEGV
(withSTOP 139
),munmap_chunk(): invalid pointer
(withSTOP 134
), orfree(): invalide size
(withSTOP 134
). If single precision is used instead of double precision, the code crashes at iterationk = 39
instead.Expected Behaviour
The code should runs smoothly. Note that if
real
variables instead ofcomplex
ones are being used, no problem is encountered.Version of stdlib
0.6.1
Platform and Architecture
Linux with Ubuntu 22.04
Additional Information
The code is generated with
fpm
and the lateststdlib
version is fetched directly byfpm
. The code is compiled withgfortran 13.2.0
installed withconda
.The text was updated successfully, but these errors were encountered: