Last active
August 29, 2015 14:02
-
-
Save thomasdarimont/3854d280345906a29f4e to your computer and use it in GitHub Desktop.
C# FalseSharingEffects
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConcurrencyExamples | |
{ | |
class FalseSharingDemo | |
{ | |
public readonly static int NUM_THREADS = 8; // change | |
public readonly static int ITERATIONS = 500 * 1000 * 1000; | |
public static void Main(string[] args) | |
{ | |
var sw = Stopwatch.StartNew(); | |
runTest(); | |
sw.Stop(); | |
Console.WriteLine("duration = {0} ticks, {1}s", sw.ElapsedTicks, sw.Elapsed.Seconds); | |
Console.ReadKey(); | |
} | |
private static void runTest() | |
{ | |
Thread[] threads = new Thread[NUM_THREADS]; | |
for (int i = 0; i < threads.Length; i++) | |
{ | |
threads[i] = new Thread(new FalseSharing(i).run); | |
} | |
foreach (Thread t in threads) | |
{ | |
t.Start(); | |
} | |
foreach (Thread t in threads) | |
{ | |
t.Join(); | |
} | |
} | |
} | |
class FalseSharing | |
{ | |
private readonly int arrayIndex; | |
private static VolatileValue[] longs = new VolatileValue[FalseSharingDemo.NUM_THREADS]; | |
static FalseSharing() | |
{ | |
for (int i = 0; i < longs.Length; i++) | |
{ | |
longs[i] = new VolatileValue(); | |
} | |
} | |
public FalseSharing(int arrayIndex) | |
{ | |
this.arrayIndex = arrayIndex; | |
} | |
public void run() | |
{ | |
int i = FalseSharingDemo.ITERATIONS + 1; | |
while (0 != --i) | |
{ | |
longs[arrayIndex].value = i; | |
} | |
} | |
} | |
class VolatileValue | |
{ | |
public volatile int value; //4 byte | |
//int[] padding = new int[15]; //padding with (4 * 15) = 60 bytes to align the object to the 64 byte cacheline size | |
//long p0, p1, p2, p3, p4, p5, p6; int p8; // manual padding with 7 * 8 bytes =56 byte + 4 byte = 60 byte // could this be optimzed away -> unused?! | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConcurrencyExamples | |
{ | |
class FalseSharingDemo | |
{ | |
public readonly static int NUM_THREADS = 8; // change | |
public readonly static int ITERATIONS = 500 * 1000 * 1000; | |
public static void Main(string[] args) | |
{ | |
var sw = Stopwatch.StartNew(); | |
runTest(); | |
sw.Stop(); | |
Console.WriteLine("duration = {0} ticks, {1}s", sw.ElapsedTicks, sw.Elapsed.Seconds); | |
Console.ReadKey(); | |
} | |
private static void runTest() | |
{ | |
Thread[] threads = new Thread[NUM_THREADS]; | |
Enumerable.Range(0, threads.Length).Select((i) => threads[i] = new Thread(new FalseSharing(i).run)).ToList(); | |
Array.ForEach(threads, t => t.Start()); | |
Array.ForEach(threads, t => t.Join()); | |
} | |
} | |
class FalseSharing | |
{ | |
private readonly int arrayIndex; | |
private static VolatileValue[] longs = new VolatileValue[FalseSharingDemo.NUM_THREADS]; | |
static FalseSharing() | |
{ | |
for (int i = 0; i < longs.Length; i++) | |
{ | |
longs[i] = new VolatileValue(); | |
} | |
} | |
public FalseSharing(int arrayIndex) | |
{ | |
this.arrayIndex = arrayIndex; | |
} | |
public void run() | |
{ | |
int i = FalseSharingDemo.ITERATIONS + 1; | |
while (0 != --i) | |
{ | |
longs[arrayIndex].value = i; | |
} | |
} | |
} | |
class VolatileValue | |
{ | |
public volatile int value; //4 byte | |
//long p0, p1, p2, p3, p4, p5, p6; int p8; // manual padding with 7 * 8 bytes =56 byte + 4 byte = 60 byte // could this be optimzed away -> unused?! | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConcurrencyExamples | |
{ | |
class FalseSharingDemo | |
{ | |
public readonly static int NUM_THREADS = 8; // change | |
public readonly static int ITERATIONS = 500 * 1000 * 1000; | |
private static void runTest() | |
{ | |
VolatileValue[] values = new VolatileValue[FalseSharingDemo.NUM_THREADS]; | |
Parallel.For(0, NUM_THREADS, threadIndex => | |
{ | |
values[threadIndex] = new VolatileValue(); | |
int counter = FalseSharingDemo.ITERATIONS + 1; | |
while (counter-- >= 0) | |
{ | |
values[threadIndex].value = counter; | |
} | |
}); | |
} | |
public static void Main(string[] args) | |
{ | |
var sw = Stopwatch.StartNew(); | |
runTest(); | |
sw.Stop(); | |
Console.WriteLine("duration = {0} ticks, {1}s", sw.ElapsedTicks, sw.Elapsed.Seconds); | |
Console.ReadKey(); | |
} | |
} | |
class VolatileValue | |
{ | |
public volatile int value; //4 byte | |
long p0, p1, p2, p3, p4, p5, p6; int p8; // manual padding with 7 * 8 bytes =56 byte + 4 byte = 60 byte // could this be optimzed away -> unused?! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment