The Computer Language
23.03 Benchmarks Game

spectral-norm Free Pascal #2 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
}

program spectralnorm;

uses cthreads, cmem, mtprocs;

type
    aod = array of double;
    td =
    record
        w1 : ^aod;
        w2 : ^aod;
    end;

var
    n,i : ptrint;
    u,v,tmp : aod;
    vBv,vv : double;
    w  : td;
    wp : ^td;


function A(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) * td(Data^).w1^[j];

    td(Data^).w2^[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) * td(Data^).w1^[j];

    td(Data^).w2^[i] := q;
end;


procedure mulAtAv (AtA1, AtA2 : aod);
begin
    w.w1 := @AtA1;
    w.w2 := @tmp;
    ProcThreadPool.DoParallel(@mulAv,  0, n-1, wp);

    w.w1 := @tmp;
    w.w2 := @AtA2;
    ProcThreadPool.DoParallel(@mulAtv, 0, n-1, wp);
end;


begin
    Val(paramstr(1), n, i);
    SetLength(u, n);
    SetLength(v, n);
    SetLength(tmp, n);

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

    for i := 0 to n-1 do
        u[i] := 1.0;

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

    for i := 0 to n-1 do
    begin
        vBv := vBv + u[i]*v[i];
        vv  := vv  + v[i]*v[i];
    end;

    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.2 [2021/05/16]


Tue, 24 Jan 2023 06:38:06 GMT

MAKE:
mv spectralnorm.fpascal-2.fpascal spectralnorm.fpascal-2.pas
/opt/src/fpc-3.2.2/bin/fpc -FuInclude/fpascal -XXs -O3 -Ci- -Cr- -g- -CpCOREAVX -CfAVX -Tlinux -Fi Include/fpascal -oFPASCAL_RUN spectralnorm.fpascal-2.pas
Warning: Only one source file supported, changing source file to compile from "Include/fpascal" into "spectralnorm.fpascal-2.pas"
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling spectralnorm.fpascal-2.pas
Linking FPASCAL_RUN
96 lines compiled, 1.0 sec
1 warning(s) issued
mv FPASCAL_RUN spectralnorm.fpascal-2.fpascal_run
rm spectralnorm.fpascal-2.pas

1.24s to complete and log all make actions

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

PROGRAM OUTPUT:
1.274224153