execute( KMeansOptions options, IEnumerable <Vector> vectors, ref Vector[] centers, out int[] clusterIds, int nParallelDegree = 1, bool bAvoidLazyEval = true ) { ParallelQuery <Vector> result = null; m_pStopwatch.Start(); m_nStartParallelPhase = m_pStopwatch.ElapsedMilliseconds; result = PLINQKMeans.Steps(vectors.AsParallel().WithDegreeOfParallelism(nParallelDegree), centers.AsParallel().WithDegreeOfParallelism(nParallelDegree), options.m_nMaxIterations, nParallelDegree, bAvoidLazyEval); result.Count(); m_pStopwatch.Stop(); m_nEndParallelPhase = m_pStopwatch.ElapsedMilliseconds; clusterIds = null; centers = result.ToArray(); return(options.m_nMaxIterations); }
public static void ToArray(Labeled <ParallelQuery <int> > labeled, int count) { ParallelQuery <int> query = labeled.Item; int seen = 0; Assert.All(query.ToArray(), x => Assert.Equal(seen++, x)); Assert.Equal(count, seen); }
public static void ToArray_Unordered(Labeled <ParallelQuery <int> > labeled, int count) { ParallelQuery <int> query = labeled.Item; IntegerRangeSet seen = new IntegerRangeSet(0, count); Assert.All(query.ToArray(), x => seen.Add(x)); seen.AssertComplete(); }
public static void Zip_AsOrdered_ThreadedDeadlock(Labeled <ParallelQuery <int> > left, int leftCount, Labeled <ParallelQuery <int> > right, int rightCount, int degree) { ParallelQuery <int> query = left.Item.WithDegreeOfParallelism(degree).Zip <int, int, int>(right.Item, (a, b) => { throw new DeliberateTestException(); }); AggregateException ae = Assert.Throws <AggregateException>(() => query.ToArray()); Assert.All(ae.InnerExceptions, e => Assert.IsType <DeliberateTestException>(e)); }
public static void Zip_AsOrdered_ThreadedDeadlock(Labeled <ParallelQuery <int> > left, int leftCount, Labeled <ParallelQuery <int> > right, int rightCount, int degree) { _ = leftCount; _ = rightCount; ParallelQuery <int> query = left.Item.WithDegreeOfParallelism(degree).Zip <int, int, int>(right.Item, (a, b) => { throw new DeliberateTestException(); }); AssertThrows.Wrapped <DeliberateTestException>(() => query.ToArray()); }
public void TestMethod6() { int count = 10000; ParallelQuery <Task> taskList = ParallelEnumerable.Range(0, count) .WithDegreeOfParallelism(Environment.ProcessorCount) .Select(async index => { string result = await Helper.RequestPage("http://127.0.0.1:8080/Account").ConfigureAwait(false); }); Task.WhenAll(taskList.ToArray()).Wait(); }
public static void PlinqChunkPartitioner_DontEnumerateAfterException(Labeled <ParallelQuery <int> > left, int leftCount, Labeled <ParallelQuery <int> > right, int rightCount) { ParallelQuery <int> query = left.Item.WithExecutionMode(ParallelExecutionMode.ForceParallelism) .Select(x => { if (x == 4) { throw new DeliberateTestException(); } return(x); }) .Zip(right.Item, (a, b) => a + b) .AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism); AggregateException ae = Assert.Throws <AggregateException>(() => query.ToArray()); Assert.Single(ae.InnerExceptions); Assert.All(ae.InnerExceptions, e => Assert.IsType <DeliberateTestException>(e)); }
Steps( ParallelQuery <Vector> data, ParallelQuery <Vector> centers, int nSteps, int nParallelDegree, bool bAvoidLazyEval ) { for (int i = 0; i < nSteps; i++) { centers = Step(data, centers); if (bAvoidLazyEval) { centers = centers.ToArray().AsParallel().WithDegreeOfParallelism(nParallelDegree); } } return(centers); }
private static void RunZipTest1(int dataSize) { int[] ints = new int[dataSize]; int[] ints2 = new int[dataSize]; for (int i = 0; i < ints.Length; i++) { ints[i] = i; ints2[i] = i; } ParallelQuery <Pair <int, int> > q = ints.AsParallel().Zip <int, int, Pair <int, int> >(ints2.AsParallel(), (i, j) => new Pair <int, int>(i, j)); Pair <int, int>[] p = q.ToArray <Pair <int, int> >(); foreach (Pair <int, int> x in p) { if (x.First != x.Second) { Assert.True(false, string.Format("RunZipTest1({2}): > Failed... {0} != {1}", x.First, x.Second, dataSize)); } } }
static void Main(string[] args) { string result1 = Helper.PostJsonData("http://127.0.0.1:8080", "{" + "'FirstName':'Kenan','LastName':'Hancer'" + "}\r\n\r\n").Result; string result2 = Helper.RequestPage("http://127.0.0.1:8080").Result; string result3 = Helper.RequestPage("http://127.0.0.1:8080/Hello").Result; string result4 = Helper.RequestPage("http://127.0.0.1:8080/Account/Hasan/40").Result; Console.WriteLine(result1); Console.WriteLine(result2); Console.WriteLine(result3); Console.WriteLine(result4); int count = 10000; ParallelQuery <Task> taskList = ParallelEnumerable.Range(0, count) .WithDegreeOfParallelism(Environment.ProcessorCount) .Select(async index => { string result = await Helper.RequestPage("http://127.0.0.1:8080/Account/Enes/4").ConfigureAwait(false); }); Stopwatch sw = Stopwatch.StartNew(); Task.WhenAll(taskList.ToArray()).Wait(); sw.Stop(); Console.WriteLine($"{count} requests are responded in {sw.Elapsed}"); Console.ReadKey(); }
private static void RunAllTests( TestTracker result, ParallelQuery <int> q, bool orderPreserved, string leftOpName, bool leftOrderDefined) { LogTestRun(leftOpName, "All1", orderPreserved); result.MustEqual( q.All(i => i > 100), q.ToArray().Any(i => i > 100)); LogTestRun(leftOpName, "All2", orderPreserved); result.MustEqual( q.All(i => i == 75), q.ToArray().All(i => i == 75)); LogTestRun(leftOpName, "Any1", orderPreserved); result.MustEqual( q.Any(i => i > 100), q.ToArray().Any(i => i > 100)); LogTestRun(leftOpName, "Any2", orderPreserved); result.MustEqual( q.Any(i => i == 75), q.ToArray().Any(i => i == 75)); LogTestRun(leftOpName, "Concat", orderPreserved); result.MustSequenceEqual( q.Concat(q).Concat(new int[] { 1, 2, 3 }.AsParallel()), q.Reverse().Reverse().ToArray().Concat(q.Reverse().Reverse()).Concat(new int[] { 1, 2, 3 }), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "DefaultIfEmpty", orderPreserved); result.MustSequenceEqual( q.DefaultIfEmpty(), q.ToArray().DefaultIfEmpty(), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "ElementAt", orderPreserved); IEnumerable <int> q2 = q.ToArray(); int count1 = q.Count(), count2 = q2.Count(); List <int> list1 = new List <int>(); List <int> list2 = new List <int>(); for (int i = 0; i < count1; i++) { list1.Add(q.ElementAt(i)); } for (int i = 0; i < count2; i++) { list2.Add(q2.ElementAt(i)); } result.MustSequenceEqual(list1, list2, leftOrderDefined); LogTestRun(leftOpName, "Except", orderPreserved); result.MustSequenceEqual( q.Except(Enumerable.Range(90, 50).AsParallel()), q.ToArray().Except(Enumerable.Range(90, 50)), false); LogTestRun(leftOpName, "First", orderPreserved); CheckFirstOrLast( result, q.First(), q.ToArray().First(), leftOrderDefined); LogTestRun(leftOpName, "GroupBy", orderPreserved); result.MustGroupByEqual( q.GroupBy(i => i % 5, (i, e) => new Pair <int, IEnumerable <int> >(i, e)), q.ToArray().GroupBy(i => i % 5, (i, e) => new Pair <int, IEnumerable <int> >(i, e))); LogTestRun(leftOpName, "GroupJoin", orderPreserved); result.MustSequenceEqual( q.GroupJoin(q, i => i, i => i, (i, e) => e.FirstOrDefault()), q.ToArray().GroupJoin(q.ToArray(), i => i, i => i, (i, e) => e.FirstOrDefault()), false); LogTestRun(leftOpName, "Intersect", orderPreserved); result.MustSequenceEqual( q.Intersect(Enumerable.Range(90, 50).AsParallel()), q.ToArray().Intersect(Enumerable.Range(90, 50)), false); LogTestRun(leftOpName, "Join1", orderPreserved); result.MustSequenceEqual( q.Join((new int[] { 1, 1, 2, 3, 3 }).AsParallel(), i => i, i => i, (i, j) => i + j), q.ToArray().Join(new int[] { 1, 1, 2, 3, 3 }, i => i, i => i, (i, j) => i + j), false); LogTestRun(leftOpName, "Join2", orderPreserved); result.MustSequenceEqual( q.Join((new int[] { 1, 1, 100, 3, 3 }).AsParallel(), i => new String('a', i), i => new String('a', i), (i, j) => i + j), q.ToArray().Join(new int[] { 1, 1, 100, 3, 3 }, i => new String('a', i), i => new String('a', i), (i, j) => i + j), false); LogTestRun(leftOpName, "Last", orderPreserved); CheckFirstOrLast( result, q.Last(), q.ToArray().Last(), leftOrderDefined); LogTestRun(leftOpName, "Min", orderPreserved); CheckFirstOrLast( result, q.Min(), q.ToArray().Min(), leftOrderDefined); LogTestRun(leftOpName, "Max", orderPreserved); CheckFirstOrLast( result, q.Min(), q.ToArray().Min(), leftOrderDefined); LogTestRun(leftOpName, "OrderBy-ThenBy", orderPreserved); result.MustSequenceEqual( q.Concat(q).OrderBy(i => i % 5).ThenBy(i => - i), q.ToArray().Concat(q).OrderBy(i => i % 5).ThenBy(i => - i), true); LogTestRun(leftOpName, "OrderByDescending-ThenByDescending", orderPreserved); result.MustSequenceEqual( q.Concat(q).OrderByDescending(i => i % 5).ThenByDescending(i => - i), q.ToArray().Concat(q).OrderByDescending(i => i % 5).ThenByDescending(i => - i), true); LogTestRun(leftOpName, "Reverse", orderPreserved); result.MustSequenceEqual( q.Concat(q).Reverse(), q.ToArray().Concat(q).Reverse(), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "Select", orderPreserved); result.MustSequenceEqual( q.Select(i => 5 * i - 17), q.ToArray().Select(i => 5 * i - 17), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "SelectMany", orderPreserved); result.MustSequenceEqual( q.SelectMany(i => new int[] { 1, 2, 3 }, (i, j) => i + 100 * j), q.ToArray().SelectMany(i => new int[] { 1, 2, 3 }, (i, j) => i + 100 * j), false); LogTestRun(leftOpName, "SequenceEqual", orderPreserved); if (orderPreserved && leftOrderDefined) { result.MustEqual(q.SequenceEqual(q), true); } else { // We don't check the return value as it can be either true or false q.SequenceEqual(q); } LogTestRun(leftOpName, "Skip", orderPreserved); CheckTakeSkip( result, q.Skip(10), q.ToArray().Skip(10), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "SkipWhile", orderPreserved); CheckTakeSkip( result, q.SkipWhile(i => i < 30), q.ToArray().SkipWhile(i => i < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "SkipWhileIndexed", orderPreserved); CheckTakeSkip( result, q.SkipWhile((i, j) => j < 30), q.ToArray().SkipWhile((i, j) => j < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "Take", orderPreserved); CheckTakeSkip( result, q.Take(10), q.ToArray().Take(10), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "TakeWhile", orderPreserved); CheckTakeSkip( result, q.TakeWhile(i => i < 30), q.ToArray().TakeWhile(i => i < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "TakeWhileIndexed", orderPreserved); CheckTakeSkip( result, q.TakeWhile((i, j) => j < 30), q.ToArray().TakeWhile((i, j) => j < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "Union", orderPreserved); result.MustSequenceEqual( q.Union(Enumerable.Range(90, 50).AsParallel()), q.ToArray().Union(Enumerable.Range(90, 50)), false); LogTestRun(leftOpName, "Where", orderPreserved); result.MustSequenceEqual( q.Where(i => i < 20 || i > 80), q.ToArray().Where(i => i < 20 || i > 80), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "Zip", orderPreserved); IEnumerable <KeyValuePair <int, int> > zipQ = q.Zip(q, (i, j) => new KeyValuePair <int, int>(i, j)); result.MustSequenceEqual( zipQ.Select(p => p.Key), q.Reverse().Reverse().ToArray(), orderPreserved && leftOrderDefined); result.MustSequenceEqual( zipQ.Select(p => p.Value), q.Reverse().Reverse().ToArray(), orderPreserved && leftOrderDefined); }
/// <summary> /// Starte die parallelisierte Filterung asynchron. Sortiert anschliessend sequentiell. /// Kehrt sofort nach dem asynchronen Start der parallelisierten Filterung zurück /// </summary> /// <returns></returns> public async Task <IEnumerable <IAsteroid> > GetFilteredSortedSetAsync() { var filtered = await Task.Run(() => query.ToArray()); return(await Task.Run(() => filtered.OrderBy(r => r.DistanceSunInAU).AsParallel())); }
public static void Main1() { Dictionary <char, long> counts = new Dictionary <char, long>(); Dictionary <char, long> countsSingle = new Dictionary <char, long>(); string alphabets = "abcdefghijklmnopqrstuvwxyz"; foreach (char c in alphabets) { counts.Add(c, 0); } foreach (char c in alphabets) { countsSingle.Add(c, 0); } Stopwatch sw = Stopwatch.StartNew(); sw.Start(); string[] words = File.ReadAllText(@"C:\Users\ja13\Documents\Visual Studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\CountWordsCaseDocument.txt").Split(' '); ParallelQuery <string> query = words.AsParallel().Where(w => (int)w.ToUpper()[0] >= 65 && (int)w.ToUpper()[0] <= 90 == true).Select(m => m.ToLower()); string[] result = query.ToArray(); object locker = new object(); // counts each alphabets words Parallel.ForEach(result, () => { // initialize thread's local dictionary var dict = new Dictionary <char, long>(); string _alphabets = "abcdefghijklmnopqrstuvwxyz"; foreach (char c in _alphabets) { dict.Add(c, 0); } return(dict); } , (w, state, localDict) => { // update thread's local dictionary char startChar = w[0]; localDict[startChar] = localDict[startChar] + 1; return(localDict); } , (localDict) => { lock (locker) { // update global dictionary with thread's local dictionary foreach (var j in localDict) { counts[j.Key] = counts[j.Key] + localDict[j.Key]; } } } ); sw.Stop(); Console.Write("Time to get words with parallel query : "); Console.WriteLine(sw.ElapsedMilliseconds); sw.Reset(); sw.Start(); string[] words1 = File.ReadAllText(@"C:\Users\ja13\Documents\Visual Studio 2015\Projects\ConsoleApplication2\ConsoleApplication2\CountWordsCaseDocument.txt").Split(' '); IEnumerable <string> query1 = words1.Where(w => (int)w.ToUpper()[0] >= 65 && (int)w.ToUpper()[0] <= 90 == true).Select(m => m.ToLower()); string[] result1 = query1.ToArray(); sw.Stop(); foreach (var s in result1) { char c = s[0]; countsSingle[c] = countsSingle[c] + 1; } sw.Stop(); Console.Write("Time with single thread query : "); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadLine(); }
static async Task <int> MainAsync(Dictionary <string, string> argsDict, CancellationToken token) { if (argsDict == null || argsDict.Count < 4) { throw new ArgumentOutOfRangeException("argsDict"); } //FileStream filestream = new FileStream("Log.txt", FileMode.Truncate); //var streamwriter = new StreamWriter(filestream); //streamwriter.AutoFlush = true; //Console.SetOut(streamwriter); //Console.SetError(streamwriter); string ip; string portNumber; int port; string clientNumber; int clientCount = 0; string testCaseNumber; string showEvents; if (!argsDict.TryGetValue("ipnumber", out ip)) { throw new ArgumentOutOfRangeException("ipnumber"); } if (!argsDict.TryGetValue("port", out portNumber) || !int.TryParse(portNumber, out port)) { throw new ArgumentOutOfRangeException("port"); } if (!argsDict.TryGetValue("clientnumber", out clientNumber) || !int.TryParse(clientNumber, out clientCount)) { throw new ArgumentOutOfRangeException("clientnumber"); } if (!argsDict.TryGetValue("testnumber", out testCaseNumber)) { throw new ArgumentOutOfRangeException("testnumber"); } if (!argsDict.TryGetValue("showevents", out showEvents)) { throw new ArgumentOutOfRangeException("showevents"); } showEventsToConsole = yesArray.Contains(showEvents); requestCount = 0; responseCount = 0; string url = $"http://{ip}:{portNumber}"; Stopwatch sw = Stopwatch.StartNew(); if (testCaseNumber == "1") { await SingleThreadClientTest(url, clientCount); } else if (testCaseNumber == "2") { ParallelQuery <Task> taskList = MultithreadedClientTest(url, clientCount, token); await Task.WhenAll(taskList.ToArray()).ContinueWith(t => t, token); } else { return(await Task.FromResult <int>(0)); } sw.Stop(); await Console.Out.WriteLineAsync($"\nProcessor Count: {Environment.ProcessorCount}, Maximum number of concurrently executing tasks that will be used to process the query."); await Console.Out.WriteLineAsync($"Request Count: {requestCount}"); await Console.Out.WriteLineAsync($"Response Count: {responseCount}"); await Console.Out.WriteLineAsync($"Elapsed: {sw.Elapsed}"); await Console.Out.WriteLineAsync($"Response Count in 1sn: {responseCount / (sw.Elapsed.Seconds <= 0 ? 1 : sw.Elapsed.Seconds)}"); return(await Task.FromResult <int>(0)); }
/// <summary> /// Filters data /// </summary> /// <param name="filters">filters to apply</param> public void FilterData(List <Filter> filters) { if (filters.Count == 0) { return; } foreach (var filter in filters) { filter.Field.Type = _columnNameTypes[filter.Field.UniqueName]; if (filter.Value == null) { ParallelQuery <int> indexes = DataColumnIndexes.AsParallel(); if (filter.Include != null) { if (filter.Field.Type == ColumnType.stringType) { CheckIncludeFilter(Data.GetColumn <string>(filter.Field.UniqueName), filter.Include, ref indexes); } else { CheckIncludeFilter(Data.GetColumn <double?>(filter.Field.UniqueName), filter.Include, ref indexes); } } else if (filter.Exclude != null) { if (filter.Field.Type == ColumnType.stringType) { CheckExcludeFilter(Data.GetColumn <string>(filter.Field.UniqueName), filter.Exclude, ref indexes); } else { CheckExcludeFilter(Data.GetColumn <double?>(filter.Field.UniqueName), filter.Exclude, ref indexes); } } else if (filter.Query != null) { if (filter.Field.Type == ColumnType.doubleType) { var column = Data.GetColumn <double?>(filter.Field.UniqueName); indexes = indexes.Where(index => CheckNumberFilterQuery(column[index], filter.Query)); } else if (filter.Field.Type == ColumnType.stringType) { var column = Data.GetColumn <string>(filter.Field.UniqueName); indexes = indexes.Where(index => CheckStringFilterQuery(column[index], filter.Query)); } else if (filter.Field.Type == ColumnType.dateType) { var column = Data.GetColumn <double?>(filter.Field.UniqueName); indexes = indexes.Where(index => CheckDateFilterQuery(column[index], filter.Query)); } } DataColumnIndexes = indexes.ToArray(); } if (filter.Value != null) { filter.Value.Field.Type = _columnNameTypes[filter.Value.Field.UniqueName]; var calculatedTotalsAggregation = new List <Aggregation>(); CalcByFields(new List <FieldModel> { filter.Field }, null, new List <FieldFuncValue> { filter.Value }, ref calculatedTotalsAggregation); var calculatedTotals = new Dictionary <object, double>(); foreach (var agg in calculatedTotalsAggregation) { calculatedTotals.Add(agg.Keys[filter.Field.UniqueName], agg.Values[filter.Value.Field.UniqueName][filter.Value.Func]); } CheckValueFilterQuery(ref calculatedTotals, filter); if (filter.Field.Type == ColumnType.stringType) { var column = Data.GetColumn <string>(filter.Field.UniqueName); DataColumnIndexes = DataColumnIndexes.Where(index => calculatedTotals.ContainsKey(column[index])).ToArray(); } else { var column = Data.GetColumn <double?>(filter.Field.UniqueName); DataColumnIndexes = DataColumnIndexes.Where(index => calculatedTotals.ContainsKey(column[index])).ToArray(); } } } }