The Computer Language
Benchmarks Game

spectral-norm Pascal Free Pascal #2 program

source code

{ The Computer Language Benchmarks Game
  http://benchmarksgame.alioth.debian.org

  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.0.4 [2017/10/03] for x86_64


Sun, 10 Dec 2017 05:44:10 GMT

MAKE:
mv spectralnorm.fpascal-2.fpascal spectralnorm.fpascal-2.pas
/opt/src/fpc-3.0.4/bin/fpc -FuInclude/fpascal -XXs -O4 -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.0.4 [2017/10/03] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling spectralnorm.fpascal-2.pas
Linking FPASCAL_RUN
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
96 lines compiled, 0.3 sec
1 warning(s) issued
mv FPASCAL_RUN spectralnorm.fpascal-2.fpascal_run
rm spectralnorm.fpascal-2.pas

0.37s to complete and log all make actions

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

PROGRAM OUTPUT:
1.274224153