-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EPVS data structure
- Loading branch information
Showing
10 changed files
with
1,152 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.