Skip to content

Commit

Permalink
[C#] EPVS (#702)
Browse files Browse the repository at this point in the history
Add EPVS data structure
  • Loading branch information
tli2 authored Jun 10, 2022
1 parent 54cf1b7 commit 417381c
Show file tree
Hide file tree
Showing 10 changed files with 1,152 additions and 104 deletions.
11 changes: 11 additions & 0 deletions cs/FASTER.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{C60F148B-2
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TstRunner", "playground\TstRunner\TstRunner.csproj", "{A265D9D2-3FEA-48BB-B1CC-273ECFEA0611}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EpvsSample", "samples\EpvsSample\EpvsSample.csproj", "{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -289,6 +291,14 @@ Global
{A265D9D2-3FEA-48BB-B1CC-273ECFEA0611}.Release|Any CPU.ActiveCfg = Release|x64
{A265D9D2-3FEA-48BB-B1CC-273ECFEA0611}.Release|x64.ActiveCfg = Release|x64
{A265D9D2-3FEA-48BB-B1CC-273ECFEA0611}.Release|x64.Build.0 = Release|x64
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Debug|x64.ActiveCfg = Debug|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Debug|x64.Build.0 = Debug|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Release|Any CPU.Build.0 = Release|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Release|x64.ActiveCfg = Release|Any CPU
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -320,6 +330,7 @@ Global
{9EFCF8C5-320B-473C-83DE-3815981D465B} = {E6026D6A-01C5-4582-B2C1-64751490DABE}
{E8C7FB0F-38B8-468A-B1CA-8793DF8F2693} = {E6026D6A-01C5-4582-B2C1-64751490DABE}
{A265D9D2-3FEA-48BB-B1CC-273ECFEA0611} = {E6026D6A-01C5-4582-B2C1-64751490DABE}
{580E4D8A-8FDD-4B3A-8C6E-8F51E270BDB2} = {62BC1134-B6E1-476A-B894-7CA278A8B6DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A0750637-2CCB-4139-B25E-F2CE740DCFAC}
Expand Down
9 changes: 9 additions & 0 deletions cs/remote/FASTER.remote.sln
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ Global
{2238A430-8D61-40A3-A23B-B1163A4CCBC6}.Release|Any CPU.Build.0 = Release|Any CPU
{2238A430-8D61-40A3-A23B-B1163A4CCBC6}.Release|x64.ActiveCfg = Release|x64
{2238A430-8D61-40A3-A23B-B1163A4CCBC6}.Release|x64.Build.0 = Release|x64
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Debug|x64.ActiveCfg = Debug|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Debug|x64.Build.0 = Debug|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Release|Any CPU.Build.0 = Release|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Release|x64.ActiveCfg = Release|Any CPU
{09E373B7-2C08-4EDD-AE68-8087A03AD490}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -169,6 +177,7 @@ Global
{6A49ADD2-DC25-47E1-9D29-5DC6380E880A} = {6B8D1038-C9D5-4111-B5CE-BF64E7D12AE1}
{2238A430-8D61-40A3-A23B-B1163A4CCBC6} = {8CF11B91-A6B6-4B81-AD43-2B07CF60F8FF}
{8D1F793C-DA1E-4C75-B824-E510BB54534E} = {1065AE7E-DEA5-4E21-AE39-95B93C074B17}
{09E373B7-2C08-4EDD-AE68-8087A03AD490} = {6B8D1038-C9D5-4111-B5CE-BF64E7D12AE1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FB603D60-F72D-4DAD-9349-442A45E20276}
Expand Down
172 changes: 172 additions & 0 deletions cs/samples/EpvsSample/EpvsBench.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using FASTER.core;

namespace EpvsSample
{
internal class EpvsBench
{
internal SemaphoreSlim testedLatch = null!;
internal EpochProtectedVersionScheme tested = null!;
internal byte[] hashBytes = null!;

internal class Worker
{
private byte[] scratchPad;

private HashAlgorithm hasher;

// private long scratchPad;
private EpvsBench parent;
private List<int> versionChangeIndexes;
private int numOps, versionChangeDelay, numaStyle, threadId;

private byte syncMode;

internal Worker(EpvsBench parent, Options options, Random random, int threadId)
{
hasher = new SHA256Managed();
scratchPad = new byte[hasher.HashSize / 8];
this.parent = parent;
versionChangeIndexes = new List<int>();
numOps = options.NumOps;
versionChangeDelay = options.VersionChangeDelay;
numaStyle = options.NumaStyle;
this.threadId = threadId;

for (var i = 0; i < numOps; i++)
{
if (random.NextDouble() < options.VersionChangeProbability)
versionChangeIndexes.Add(i);
}

switch (options.SynchronizationMode)
{
case "epvs":
syncMode = 1;
break;
case "epvs-refresh":
syncMode = 2;
break;
case "latch":
syncMode = 3;
break;
}

}

private void DoWork(int numUnits)
{
for (var i = 0; i < numUnits; i++)
hasher.TryComputeHash(parent.hashBytes, scratchPad, out _);
// scratchPad++;
}

internal void RunOneThread()
{
if (numaStyle == 0)
Native32.AffinitizeThreadRoundRobin((uint) threadId);
else if (numaStyle == 1)
Native32.AffinitizeThreadShardedNuma((uint) threadId, 2); // assuming two NUMA sockets

if (syncMode == 2)
parent.tested.Enter();

var nextChangeIndex = 0;
for (var i = 0; i < numOps; i++)
{
switch (syncMode)
{
case 1:
if (nextChangeIndex < versionChangeIndexes.Count &&
i == versionChangeIndexes[nextChangeIndex])
{
parent.tested.TryAdvanceVersionWithCriticalSection((_, _) => DoWork(versionChangeDelay));
nextChangeIndex++;
}
else
{
parent.tested.Enter();
DoWork(1);
parent.tested.Leave();
}
break;
case 2:
if (nextChangeIndex < versionChangeIndexes.Count &&
i == versionChangeIndexes[nextChangeIndex])
{
while (parent.tested.TryAdvanceVersionWithCriticalSection((_, _) =>
{
DoWork(versionChangeDelay);
}) == StateMachineExecutionStatus.RETRY)
parent.tested.Refresh();
nextChangeIndex++;
}
else
{
parent.tested.Refresh();
DoWork(1);
}

break;
case 3:
parent.testedLatch.Wait();
if (nextChangeIndex < versionChangeIndexes.Count &&
i == versionChangeIndexes[nextChangeIndex])
{
DoWork(versionChangeDelay);
nextChangeIndex++;
}
else
{
DoWork(1);
}
parent.testedLatch.Release();
break;
default:
throw new NotImplementedException();
}
}

if (syncMode == 2)
parent.tested.Leave();
}
}


internal void RunExperiment(Options options)
{
hashBytes = new byte[8];
new Random().NextBytes(hashBytes);
tested = new EpochProtectedVersionScheme(new LightEpoch());
testedLatch = new SemaphoreSlim(1, 1);

var threads = new List<Thread>();
var random = new Random();
for (var i = 0; i < options.NumThreads; i++)
{
var worker = new Worker(this, options, random, i);
var t = new Thread(() => worker.RunOneThread());
threads.Add(t);
}

var sw = Stopwatch.StartNew();
foreach (var t in threads)
t.Start();
foreach (var t in threads)
t.Join();
var timeMilli = sw.ElapsedMilliseconds;
var throughput = options.NumOps * options.NumThreads * 1000.0 / timeMilli;
Console.WriteLine(throughput);
if (!options.OutputFile.Equals(""))
{
using var outputFile = new StreamWriter(options.OutputFile, true);
outputFile.WriteLine(throughput);
}
}
}
}
17 changes: 17 additions & 0 deletions cs/samples/EpvsSample/EpvsSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\core\FASTER.core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit 417381c

Please sign in to comment.