forked from microsoft/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscSamples.cs
67 lines (60 loc) · 2.93 KB
/
MiscSamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace Microsoft.Spark.CSharp.Samples
{
class MiscSamples
{
/// <summary>
/// Sample to compute th evalue of Pi
/// </summary>
[Sample]
internal static void PiSample()
{
const int slices = 3;
var n = (int) Math.Min(100000L*slices, int.MaxValue);
var values = new List<int>(n);
for (int i = 0; i <= n; i++)
{
values.Add(i);
}
var count = SparkCLRSamples.SparkContext.Parallelize(values, slices)
.Map(i =>
{
var random = new Random(); //if this definition is moved out of the anonymous method,
//the delegate will form a closure and the compiler
//will generate a type for it without Serializable attribute
//and hence serialization will fail
//the alternative is to use PiHelper below which is marked Serializable
var x = random.NextDouble() * 2 - 1;
var y = random.NextDouble() * 2 - 1;
return (x * x + y * y) < 1 ? 1 : 0;
}
)
.Reduce((x, y) => x + y);
Console.WriteLine("Pi is roughly " + 4.0 * (int)count / n);
/********* Alternative to the count method provided above. This produces more accurate results because of the way Random is used ***********/
var countComputedUsingAnotherApproach = SparkCLRSamples.SparkContext.Parallelize(values, slices).Map(new PiHelper().Execute).Reduce((x, y) => x + y);
var approximatePiValue = 4.0* countComputedUsingAnotherApproach/n;
Console.WriteLine("Pi is roughly " + approximatePiValue);
/********************************************************************/
if (SparkCLRSamples.Configuration.IsValidationEnabled)
{
Assert.AreEqual(3.14, approximatePiValue, 0.019);
}
}
[Serializable]
private class PiHelper
{
private readonly Random random = new Random();
public int Execute(int input)
{
var x = random.NextDouble() * 2 - 1;
var y = random.NextDouble() * 2 - 1;
return (x * x + y * y) < 1 ? 1 : 0;
}
}
}
}