Esempio n. 1
0
        public static void MultipleTasksFuzzyMatch()
        {
            var tasks   = new List <Task <List <string> > >();
            var matches = new List <string>();

            BenchPerformance.Time("Multi Tasks Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                foreach (var word in WordsToSearch)
                {
                    tasks.Add(Task.Factory.StartNew <List <string> >((w) =>
                    {
                        List <string> localMatches = new List <string>();
                        var localMathes            = FuzzyMatch.JaroWinklerModule.bestMatch(Words, (string)w);
                        localMatches.AddRange(localMathes.Select(m => m.Word));
                        return(localMatches);
                    }, word));
                }

                Task.Factory.ContinueWhenAll(tasks.ToArray(), (ts) =>
                {
                    matches = new List <string>(tasks.SelectMany(t => t.Result).Distinct());
                }).Wait();
            });
            foreach (var match in matches)
            {
                Log(match);
            }
            Console.WriteLine();
        }
Esempio n. 2
0
        // TODO
        // (1) implement a fast fuzzy match
        // you can use either PLINQ and/or Parallel loop. The latter requires attention to avoid race condition


        #region Solution

        public static void ParallelLoopFuzzyMatch()
        {
            List <string> matches = new List <string>();

            BenchPerformance.Time("Parallel Loop Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                object sync = new object();

                Parallel.ForEach(WordsToSearch,
                                 // thread local initializer
                                 () => { return(new List <string>()); },
                                 (word, loopState, localMatches) =>
                {
                    var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word);
                    localMatches.AddRange(localMathes.Select(m => m.Word));                        // same code
                    return(localMatches);
                },
                                 (finalResult) =>
                {
                    // thread local aggregator
                    lock (sync) matches.AddRange(finalResult);
                }
                                 );
            });

            foreach (var match in matches.Distinct())
            {
                Log(match);
            }
        }
Esempio n. 3
0
        public static void RunDemo(List <string> urls)
        {
            BenchPerformance.Time("Web crawler execution", () =>
            {
                var webPageTitles = from url in urls
                                    from pageContent in WebCrawler(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });

            BenchPerformance.Time("Web crawler execution using memoization", () =>
            {
                var webPageTitles = from url in urls
                                    from pageContent in WebCrawlerMemoized(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });


            BenchPerformance.Time("Thread-safe memoization function", () =>
            {
                var webPageTitles = from url in urls.AsParallel()
                                    from pageContent in WebCrawlerMemoizedThreadSafe(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });
        }
Esempio n. 4
0
        public static void Run(List <string> urls)
        {
            BenchPerformance.Time("Web crawler execution", () =>
            {
                var webPageTitles = from url in urls
                                    from pageContent in WebCrawler(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });

            BenchPerformance.Time("Web crawler execution using memoization", () =>
            {
                var webPageTitles = from url in urls
                                    from pageContent in WebCrawlerMemoized(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });


            BenchPerformance.Time("Thread-safe memoization function", () =>
            {
                // TODO : 1.2
                // (1) implement parallel web crawler with thread safe memoization
                // go to the Memoziation file
                var webPageTitles = new int[] { };

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });
        }
Esempio n. 5
0
 public static void ParallelLinqFuzzyMatch()
 {
     BenchPerformance.Time("Parallel Linq Fuzzy Match",
                           iterations: Data.Iterations, operation: () =>
     {
         ParallelQuery <string> matches = (from word in WordsToSearch.AsParallel()
                                           from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word)
                                           select match.Word);
         matches.ForAll(match =>
         {
             Log(match);
         });
     });
 }
Esempio n. 6
0
        public static void LinqFuzzyMatch()
        {
            BenchPerformance.Time("Linq Fuzzy Match", () =>
            {
                var matches = (from word in WordsToSearch
                               from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word)
                               select match.Word);

                foreach (var match in matches)
                {
                    Log(match);
                }
            });
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            BenchPerformance.Time("Test", () =>
            {
                int sum = 0;
                for (int i = 0; i < 100; i++)
                {
                    sum += i;
                }
            }, 10);


            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
Esempio n. 8
0
        public void ParallelArrayFuzzyMatch()
        {
            BenchPerformance.Time("Parallel Array F# Fuzzy Match", () =>
            {
                var matches = (from word in WordsToSearch
                               from match in FuzzyMatch.JaroWinklerModule.Parallel.bestMatch(Words, word)
                               select match.Word);

                foreach (var match in matches)
                {
                    Console.Write("{0}\t", match);
                }

                Console.WriteLine();
            });
        }
Esempio n. 9
0
        public static void ParallelLinqPartitionerFuzzyMatch()
        {
            BenchPerformance.Time("Parallel PLinq  partitioner Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                var partitioner = Partitioner.Create(WordsToSearch, EnumerablePartitionerOptions.NoBuffering);

                ParallelQuery <string> matches = (from word in WordsToSearch.AsParallel()
                                                  from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word)
                                                  select match.Word);
                matches.ForAll(match =>
                {
                    Log(match);
                });
            });
        }
Esempio n. 10
0
        public void ParallelLinqFuzzyMatch()
        {
            BenchPerformance.Time("Parallel Linq Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                ParallelQuery <string> matches = (from word in WordsToSearch.AsParallel()
                                                  from match in FuzzyMatch.JaroWinklerModule.bestMatch(Words, word)
                                                  select match.Word);

                foreach (var match in matches)
                {
                    Console.Write("{0}\t", match);
                }

                Console.WriteLine();
            });
        }
Esempio n. 11
0
        }                                // Console.Write("{0}\t", match);

        public static void SequentialFuzzyMatch()
        {
            List <string> matches = new List <string>();

            BenchPerformance.Time("Sequential Fuzzy Match", iterations: Data.Iterations, operation: () =>
            {
                foreach (var word in WordsToSearch)
                {
                    var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word);
                    matches.AddRange(localMathes.Select(m => m.Word));
                }
            });
            foreach (var match in matches.Distinct())
            {
                Log(match);
            }
            Console.WriteLine();
        }
Esempio n. 12
0
        public void TwoThreadsFuzzyMatch()
        {
            List <string> matches = new List <string>();

            BenchPerformance.Time("Two Thread Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                var t1 = new Thread(() =>
                {
                    var take  = WordsToSearch.Count / 2;
                    var start = 0;

                    foreach (var word in WordsToSearch.Take(take))
                    {
                        var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word);
                        matches.AddRange(localMathes.Select(m => m.Word));
                    }
                });
                var t2 = new Thread(() =>
                {
                    var start = WordsToSearch.Count / 2;
                    var take  = WordsToSearch.Count - start;

                    foreach (var word in WordsToSearch.Skip(start).Take(take))
                    {
                        var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word);
                        matches.AddRange(localMathes.Select(m => m.Word));
                    }
                });
                t1.Start();
                t2.Start();
                t1.Join();
                t2.Join();
            });
            foreach (var match in matches.Distinct())
            {
                Console.Write("{0}\t", match);
            }
            Console.WriteLine();
        }
Esempio n. 13
0
        public static void SumPrimeNumber_Reducer()
        {
            int len = 10000000;
            Func <int, bool> isPrime = n =>
            {
                if (n == 1)
                {
                    return(false);
                }
                if (n == 2)
                {
                    return(true);
                }
                var boundary = (int)Math.Floor(Math.Sqrt(n));
                for (int i = 2; i <= boundary; ++i)
                {
                    if (n % i == 0)
                    {
                        return(false);
                    }
                }
                return(true);
            };

            // Parallel sum of a collection using parallel Reducer
            BenchPerformance.Time("Parallel sum of a collection using parallel Reducer", () =>
            {
                // TODO
                // calculate the total with parallel Reducer
                //
                // Note : if the "len" value increases over int.maxvalue, the LINQ/PLINQ "Sum" operator does not work.
                // for example, the following code does not work. The reducer function should fix the issue
                // var total = Enumerable.Range(0, len).Where(isPrime).AsParallel().Sum();

                // implement the ReducePartitioner function
                var total = ParallelReducer.ReducePartitioner(Enumerable.Range(0, len), i => i, (a, b) => a + b);

                Console.WriteLine($"The total is {total}");
            }, 5);
        }
Esempio n. 14
0
        public void MultipleThreadsFuzzyMatch()
        {
            List <string> matches = new List <string>();

            BenchPerformance.Time("Multi Thread Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                var threads = new Thread[Environment.ProcessorCount];

                for (int i = 0; i < threads.Length; i++)
                {
                    var index      = i;
                    threads[index] = new Thread(() =>
                    {
                        var take  = WordsToSearch.Count / (Math.Min(WordsToSearch.Count, threads.Length));
                        var start = index == threads.Length - 1 ? WordsToSearch.Count - take : index * take;
                        foreach (var word in WordsToSearch.Skip(start).Take(take))
                        {
                            var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word);
                            matches.AddRange(localMathes.Select(m => m.Word));
                        }
                    });
                }

                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i].Start();
                }
                for (int i = 0; i < threads.Length; i++)
                {
                    threads[i].Join();
                }
            });
            foreach (var match in matches.Distinct())
            {
                Console.Write("{0}\t", match);
            }
            Console.WriteLine();
        }
Esempio n. 15
0
        public static void ThreadFuzzyMatch()
        {
            List <string> matches = new List <string>();

            BenchPerformance.Time("Thread Fuzzy Match",
                                  iterations: Data.Iterations, operation: () =>
            {
                var t = new Thread(() =>
                {
                    foreach (var word in WordsToSearch)
                    {
                        var localMathes = FuzzyMatch.JaroWinklerModule.bestMatch(Words, word);
                        matches.AddRange(localMathes.Select(m => m.Word));
                    }
                });
                t.Start();
                t.Join();
            });
            foreach (var match in matches.Distinct())
            {
                Log(match);
            }
        }
Esempio n. 16
0
        public static void FromZeroToPLINQ()
        {
            var numbers   = Enumerable.Range(1, 1000000).ToArray();
            var allocNums = Enumerable.Range(1, 10000000).ToArray();

            BenchPerformance.Time("LINQ", () =>
            {
                var results = (from n in numbers
                               where SimpleAlghos.isPrime(n)
                               select n).ToArray();
            }, 2);


            BenchPerformance.Time("PLINQ", () =>
            {
                var results = (from n in numbers.AsParallel()
                               where SimpleAlghos.isPrime(n)
                               select n).ToArray();
            }, 2);


            BenchPerformance.Time("PLINQ REF TYPE", () =>
            {
                var results = (from n in allocNums.AsParallel()
                               select new { Value = n }).ToArray();
            }, 2);

            BenchPerformance.Time("PLINQ VALUE TYPE", () =>
            {
                var results = (from n in allocNums.AsParallel()
                               select new Wrapper {
                    Value = n
                }).ToArray();
            }, 2);

            BenchPerformance.Time("SHARED SEQ", () =>
            {
                int[] count = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 100000000; j++)
                    {
                        count[i] = count[i] + j;
                    }
                }
            }, 2);

            BenchPerformance.Time("SHARED PLINQ", () =>
            {
                int[] count = new int[4];
                ParallelEnumerable.Range(0, 4).ForAll(i =>
                {
                    for (int j = 0; j < 100000000; j++)
                    {
                        count[i] = count[i] + j;
                    }
                });
            }, 2);

            BenchPerformance.Time("BETTER SHARED PLINQ", () =>
            {
                const int PADDING = 16;
                int[] count       = new int[5 * PADDING];
                ParallelEnumerable.Range(0, 4).ForAll(i =>
                {
                    int offset = (i + 1) * PADDING;
                    for (int j = 0; j < 100000000; j++)
                    {
                        count[offset] = count[offset] + j;
                    }
                });
            }, 2);

            BenchPerformance.Time("ALTERNATIVE SHARED PLINQ", () =>
            {
                int[][] count = new int[4][];
                ParallelEnumerable.Range(0, 4).ForAll(i =>
                {
                    count[i] = new int[1];
                    for (int j = 0; j < 100000000; j++)
                    {
                        count[i][0] = count[i][0] + j;
                    }
                });
            }, 2);
        }
Esempio n. 17
0
        static void ParallelFilterMap()
        {
            bool IsPrime(int n)
            {
                if (n == 1)
                {
                    return(false);
                }
                if (n == 2)
                {
                    return(true);
                }
                var boundary = (int)Math.Floor(Math.Sqrt(n));

                for (int i = 2; i <= boundary; ++i)
                {
                    if (n % i == 0)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            BigInteger ToPow(int n) => (BigInteger)Math.BigMul(n, n);

            var numbers = Enumerable.Range(0, 100000000).ToList();

            BigInteger SeqOperation() => numbers.Where(IsPrime).Select(ToPow).Aggregate(BigInteger.Add);
            BigInteger ParallelLinqOperation() => numbers.AsParallel().Where(IsPrime).Select(ToPow).Aggregate(BigInteger.Add);

            // TODO : 6.6
            // Update the FilterMap function to reduce the result into a single (or different) type.
            // you should remove the ".Aggregate(BigInteger.Add)" part and add a reducer function.
            // Keep parallelism and thread safety
            BigInteger ParallelFilterMapInline() => numbers.FilterMap(IsPrime, ToPow).Aggregate(BigInteger.Add);


            Console.WriteLine("Square Prime Sum [0..10000000]");
            Func <Func <BigInteger>, Action> runSum = (func) =>
                                                      new Action(
                () =>
            {
                var result = func();
                Console.WriteLine($"Sum = {result}");
            });

            var sumImplementations =
                new[]
            {
                new Tuple <String, Action>(
                    "C# Sequential", runSum(SeqOperation)),
                new Tuple <String, Action>(
                    "C# Parallel LINQ", runSum(ParallelLinqOperation)),
                new Tuple <String, Action>(
                    "C# Parallel FilterMap inline", runSum(ParallelFilterMapInline))
            };

            foreach (var item in sumImplementations)
            {
                BenchPerformance.Time(item.Item1, item.Item2);
            }
        }
 public static void Run()
 {
     BenchPerformance.Time("Parallel Map Reduce word count", RunBookMapReduce, 5);
 }