Recent

Author Topic: [SOLVED] Need help to use slegsy in NumLib correctly  (Read 475 times)

ikel

  • New Member
  • *
  • Posts: 36
[SOLVED] Need help to use slegsy in NumLib correctly
« on: September 08, 2024, 11:50:51 pm »
Hi,

I'm running the following example with slegsy, but throw an access violation error.
The original example is located here; https://wiki.freepascal.org/NumLib_Documentation#Solving_systems_of_linear_equations_(unit_sle)
Here is the code.

Code: Pascal  [Select][+][-]
  1. program solve_sys_lin_eq;
  2.  
  3. uses
  4.   typ, sle, omv;
  5.  
  6. const
  7.   n = 4;
  8.   A: array[1..n, 1..n] of ArbFloat = (
  9.     ( 5,  7,  6,  5),
  10.     ( 7, 10,  8,  7),
  11.     ( 6,  8, 10,  9),
  12.     ( 5,  7,  9, 10)
  13.   );
  14.   b: array[1..n] of ArbFloat = (
  15.     57, 79, 88, 86
  16.   );
  17.  
  18. var
  19.   x: array[1..n] of ArbFloat;
  20.   b_test: array[1..n] of ArbFloat;
  21.   ca: ArbFloat;
  22.   i, j: Integer;
  23.   term: Integer;
  24.  
  25. begin
  26.   WriteLn('Solve matrix system A x = b');
  27.   WriteLn;
  28.  
  29.   // Print A
  30.   WriteLn('Matrix A = ');
  31.   for i:= 1 to n do begin
  32.     for j := 1 to n do
  33.       Write(A[i, j]:10:0);
  34.     WriteLn;
  35.   end;
  36.   WriteLn;
  37.  
  38.   // Print b
  39.   WriteLn('Vector b = ');
  40.   for i:= 1 to n do
  41.     Write(b[i]:10:0);
  42.   WriteLn;
  43.   WriteLn;
  44.  
  45.   // Solve - select one of these methods
  46.   //slegen(n, n, A[1, 1], b[1], x[1], ca, term);
  47.   slegsy(n, n, A[1, 1], b[1], x[1], ca, term);
  48.   //slegpd(n, n, A[1, 1], b[1], x[1], ca, term);
  49.  
  50.   if term = 1 then begin
  51.     WriteLn('Solution vector x = ');
  52.     for i:= 1 to n do
  53.       Write(x[i]:10:0);
  54.     WriteLn;
  55.     WriteLn;
  56.  
  57.     omvmmv(A[1,1], n, n, n, x[1], b_test[1]);
  58.  
  59.     WriteLn('Check result: A x = (must be equal to b)');
  60.     for i:= 1 to n do
  61.       Write(b_test[i]:10:0);
  62.     WriteLn;
  63.   end
  64.   else
  65.     WriteLn('Error');
  66.  
  67.   ReadLn;
  68.  
  69. end.
  70.  

The error I get is ...

Code: Bash  [Select][+][-]
  1. $ ./SolveLinearEq.exe
  2. Solve matrix system A x = b
  3.  
  4. Matrix A =
  5.          5         7         6         5
  6.          7        10         8         7
  7.          6         8        10         9
  8.          5         7         9        10
  9.  
  10. Vector b =
  11.         57        79        88        86
  12.  
  13. An unhandled exception occurred at $0000000100015539:
  14. EAccessViolation: Access violation
  15.   $0000000100015539
  16.   $000000010000F35A
  17.   $00000001000018D3
  18.   $0000000100001AD6
  19.   $000000010000DA20
  20.   $00000001000016C0
  21.   $00007FFC4F09257D
  22.   $00007FFC5052AF28
  23.  

Can someone help me to use slegsy correctly?

Thanks.
« Last Edit: September 09, 2024, 03:10:34 am by ikel »

wp

  • Hero Member
  • *****
  • Posts: 12364
Re: Need help to use slegsy in NumLib correctly
« Reply #1 on: September 09, 2024, 12:13:15 am »
I tested your code with Laz 3.4/FPC 3.2.2 (32 bit and 64 bit), Laz main/FPC 3.2.2 (32 bit and 64 bit) and with Laz main/FPC main, all on Windows 11, and with Laz 3.4/FPC 3.2.2 on Kubuntu 24.04, and did not get an error. Which Laz/FPC versions are you using, and on with operating system?

ikel

  • New Member
  • *
  • Posts: 36
Re: Need help to use slegsy in NumLib correctly
« Reply #2 on: September 09, 2024, 12:48:59 am »
Hi wp,

I'm sorry, I mised out the important bit out. I am uing the following.

Lazarus 3.5 (rev lazarus_3_4-96-gcfd24b5c32)
FPC 3.2.3 x86_64-win64-win32/win64
Win 11.

Let me try the snippet on FPC 3.2.2. Thank you.

-ikel

ikel

  • New Member
  • *
  • Posts: 36
Re: Need help to use slegsy in NumLib correctly
« Reply #3 on: September 09, 2024, 01:09:30 am »
I tested your code with Laz 3.4/FPC 3.2.2 (32 bit and 64 bit), Laz main/FPC 3.2.2 (32 bit and 64 bit) and with Laz main/FPC main, all on Windows 11, and with Laz 3.4/FPC 3.2.2 on Kubuntu 24.04, and did not get an error. Which Laz/FPC versions are you using, and on with operating system?

wp, the snippet works when I use FPC 3.2.2 x86_64-win64-win32/win64. The snippet doesn't work on FPC 3.2.3 (fixes).

Thanks for your help!

-ikel

jamie

  • Hero Member
  • *****
  • Posts: 6579
Re: Need help to use slegsy in NumLib correctly
« Reply #4 on: September 09, 2024, 02:18:08 am »
So, I have an enquiry, all those other units you have in the USES list, were they also compiled with the 3.2.3 release?

The only true wisdom is knowing you know nothing

ikel

  • New Member
  • *
  • Posts: 36
Re: Need help to use slegsy in NumLib correctly
« Reply #5 on: September 09, 2024, 03:10:15 am »
So, I have an enquiry, all those other units you have in the USES list, were they also compiled with the 3.2.3 release?



Good question. I didn't think of that.
I believe not. This is the log from fpcupdeluxe.

Code: Pascal  [Select][+][-]
  1. info: FPC Native Installer (BuildModule: FPC): To compile this FPC, we will use the (already available) compiler with version : 3.2.2.
  2.  

Thaddy

  • Hero Member
  • *****
  • Posts: 15717
  • Censorship about opinions does not belong here.
Re: [SOLVED] Need help to use slegsy in NumLib correctly
« Reply #6 on: September 09, 2024, 07:41:13 am »
I can reproduce the exact error on windows 11-64 and a recent 3.3.1 trunk. The error does not appear in a recent Ubuntu64 + a recent trunk.
Both compiled like
fpc -CX -XXs -O4 solve_sys_lin_eq.pas
So what has changed since you marked it as solved?
Same trunk version

Error is around line 47.
If I compile in fpc - which is the default - mode instead of objfpc, I get a compile time error instead of a runtime error:
solve_sys_lin_eq.pas(47,45) Error: (3069) Call by var for arg no. 7 has to match exactly: Got "SmallInt" expected "LongInt", but that is likely due to integer size in fpc mode.
I am more worried about which Arbfloat is chosen for win64, but looks ok: size 8 so double.
« Last Edit: September 09, 2024, 08:10:45 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

wp

  • Hero Member
  • *****
  • Posts: 12364
Re: [SOLVED] Need help to use slegsy in NumLib correctly
« Reply #7 on: September 09, 2024, 10:11:27 am »
Strange. I can confirm Thaddy's observation that the crash happens with FPC 3.3.1/64 bit on Windows (it's at line 254 in procedure mdtgtr() of unit mdt) . But when I copy the units det, mdt, and sle from the numlib folder into the project folder (for easier debugging) the crash is gone (attached this project)... Then I rebuilt FPC, but it does not fix the issue in the original project.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11778
  • FPC developer.
Re: [SOLVED] Need help to use slegsy in NumLib correctly
« Reply #8 on: September 09, 2024, 10:13:45 am »
win64 uses SSE for floating point, it might be a problem there. Or some FPU masking not done for win64.

 

TinyPortal © 2005-2018