source code
/* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Jesper Meyer
*/
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Threading.Tasks;
namespace SpectralNorm
{
unsafe class Program
{
public static void Main(string[] args)
{
int n = 100;
if (args.Length > 0) n = int.Parse(args[0]);
fixed (double* u = new double[n])
fixed (double* v = new double[n])
{
new Span<double>(u, n).Fill(1);
for (var i = 0; i < 10; i++)
{
mult_AtAv(u, v, n);
mult_AtAv(v, u, n);
}
var result = Math.Sqrt(dot(u, v, n) / dot(v, v, n));
Console.WriteLine("{0:f9}", result);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double A(int i, int j)
{
return (i + j) * (i + j + 1) / 2 + i + 1;
}
private static double dot(double* v, double* u, int n)
{
double sum = 0;
for (var i = 0; i < n; i++)
sum += v[i] * u[i];
return sum;
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void mult_Av(double* v, double* outv, int n)
{
Parallel.For(0, n, i =>
{
var sum = Vector128<double>.Zero;
for (var j = 0; j < n; j += 2)
{
var b = Sse2.LoadVector128(v + j);
var a = Vector128.Create(A(i, j), A(i, j + 1));
sum = Sse2.Add(sum, Sse2.Divide(b, a));
}
var add = Sse3.HorizontalAdd(sum, sum);
var value = Unsafe.As<Vector128<double>, double>(ref add);
Unsafe.WriteUnaligned(outv + i, value);
});
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void mult_Atv(double* v, double* outv, int n)
{
Parallel.For(0, n, i =>
{
var sum = Vector128<double>.Zero;
for (var j = 0; j < n; j += 2)
{
var b = Sse2.LoadVector128(v + j);
var a = Vector128.Create(A(j, i), A(j + 1, i));
sum = Sse2.Add(sum, Sse2.Divide(b, a));
}
var add = Sse3.HorizontalAdd(sum, sum);
var value = Unsafe.As<Vector128<double>, double>(ref add);
Unsafe.WriteUnaligned(outv + i, value);
});
}
private static void mult_AtAv(double* v, double* outv, int n)
{
fixed (double* tmp = new double[n])
{
mult_Av(v, tmp, n);
mult_Atv(tmp, outv, n);
}
}
}
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
.NET SDK 9.0.100
Host Version: 9.0.0
Commit: 9d5a6a9aa4
<OutputType>Exe
<TargetFramework>net9.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false
Wed, 13 Nov 2024 23:14:51 GMT
MAKE:
cp spectralnorm.csharpcore-5.csharpcore Program.cs
cp Include/csharpcore/program.csproj .
mkdir obj
cp Include/csharpcore/project.assets.json ./obj
/opt/src/dotnet-sdk-9.0.100/dotnet build -c Release --use-current-runtime
Determining projects to restore...
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.csproj (in 6.05 sec).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/bin/Release/net9.0/linux-x64/program.dll
Build succeeded.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
2 Warning(s)
0 Error(s)
Time Elapsed 00:00:11.08
13.01s to complete and log all make actions
COMMAND LINE:
./bin/Release/net9.0/linux-x64/program 5500
PROGRAM OUTPUT:
1.274224153