The Computer Language
Benchmarks Game

spectral-norm Free Pascal #3 program

source code

{ The Computer Language Benchmarks Game
  https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

  - contributed by Ian Osgood
  - modified by Vincent Snijders
  - modified by Peter Blackman
  - modified by Akira1364
}

program spectralnorm;

uses cmem, {$ifdef UNIX}cthreads,{$endif} mtprocs;

type
  aod = array of double;
  paod = ^aod;
  aodpair = array[0..1] of paod;

var
  i, n, len: ptrint;
  u, v, tmp: aod;
  vBv, vv: double;
  up, uplow, vp: pdouble;
  w: aodpair;
  wp: ^aodpair;

function A(const i,j : ptrint): double; inline;
begin
    A := 1 / ((i + j) * (i + j + 1) div 2 + i + 1);
end;

procedure mulAv(i: ptrint; Data: Pointer; Item: TMultiThreadProcItem);
var
  j: ptrint;
  q: double;
begin
  q := 0;
  for j := 0 to n - 1 do
    q := q + A(i,j) * aodpair(Data^)[0]^[j];
  aodpair(Data^)[1]^[i] := q;
end;

procedure mulAtv(i: ptrint; Data: Pointer; Item: TMultiThreadProcItem);
var
  j: ptrint;
  q: double;
begin
  q := 0;
  for j := 0 to n - 1 do
    q := q + A(j,i) * aodpair(Data^)[0]^[j];
  aodpair(Data^)[1]^[i] := q;
end;

procedure mulAtAv(const PAtA1, PAtA2: paod); inline;
begin
  w[0] := PAtA1;
  w[1] := @tmp;
  ProcThreadPool.DoParallel(@mulAv, 0, len, wp);
  w[0] := @tmp;
  w[1] := PAtA2;
  ProcThreadPool.DoParallel(@mulAtv, 0, len, wp);
end;

begin
  val(paramstr(1), n, i);
  setlength(u, n);
  setlength(v, n);
  setlength(tmp, n);

  len := n - 1;

  for i := len downto 0 do
    u[i] := 1.0;

  vBv := 0;
  vv := 0;
  wp := @w;

  for i := 1 to 10 do begin
    mulAtAv(@u, @v);
    mulAtAv(@v, @u);
  end;

  up := @u[len];
  uplow := @u[0];
  vp := @v[len];
  repeat
    vBv := vBv + up^ * vp^;
    vv := vv + vp^ * vp^;
    Dec(up);
    Dec(vp);
  until up = uplow;

  writeln(sqrt(vBv / vv) : 0 : 9);
end.
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Free Pascal Compiler version 3.2.0 [2020/06/14] for x86_64



Mon, 29 Jun 2020 20:09:50 GMT

MAKE:
mv spectralnorm.fpascal-3.fpascal spectralnorm.fpascal-3.pas
/opt/src/fpc-3.2.0/bin/fpc -FuInclude/fpascal -XXs -O3 -Ci- -Cr- -g- -CpCOREI -Tlinux -Fi Include/fpascal -oFPASCAL_RUN spectralnorm.fpascal-3.pas
Warning: Only one source file supported, changing source file to compile from "Include/fpascal" into "spectralnorm.fpascal-3.pas"
Free Pascal Compiler version 3.2.0 [2020/06/14] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling spectralnorm.fpascal-3.pas
Compiling ./Include/fpascal/mtprocs.pas
Compiling ./Include/fpascal/mtpcpu.pas
mtprocs.pas(235,3) Note: Call to subroutine "procedure TProcThreadPool.EnterPoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(252,5) Note: Call to subroutine "procedure TProcThreadPool.LeavePoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(302,3) Note: Call to subroutine "procedure TProcThreadPool.EnterPoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(308,7) Note: Call to subroutine "procedure TProcThreadGroup.RemoveThread(AThread:TProcThread);" marked as inline is not inlined
mtprocs.pas(319,5) Note: Call to subroutine "procedure TProcThreadPool.LeavePoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(351,7) Note: Call to subroutine "procedure TProcThreadGroup.Run(Index:Int64;Data:Pointer;Item:TMultiThreadProcItem);" marked as inline is not inlined
mtprocs.pas(353,7) Note: Call to subroutine "procedure TProcThreadPool.EnterPoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(380,9) Note: Call to subroutine "procedure TProcThreadPool.LeavePoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(624,3) Note: Call to subroutine "procedure TProcThreadPool.EnterPoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(637,5) Note: Call to subroutine "procedure TProcThreadPool.LeavePoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(645,3) Note: Call to subroutine "procedure TProcThreadPool.EnterPoolCriticalSection;" marked as inline is not inlined
mtprocs.pas(653,5) Note: Call to subroutine "procedure TProcThreadPool.LeavePoolCriticalSection;" marked as inline is not inlined
Linking FPASCAL_RUN
1078 lines compiled, 1.0 sec
1 warning(s) issued
12 note(s) issued
mv FPASCAL_RUN spectralnorm.fpascal-3.fpascal_run
rm spectralnorm.fpascal-3.pas

1.27s to complete and log all make actions

COMMAND LINE:
./spectralnorm.fpascal-3.fpascal_run 5500

PROGRAM OUTPUT:
1.274224153