深圳企业网站建设服务中心,吧台 东莞网站建设,如何用手机做音乐网站,微信订阅号关键网站本章将介绍并行编程模式#xff0c;重点是理解并行代码问题场景并使用并行编程/异步技术解决他们。本章会介绍几种最重要的编程模式。 本教程学习工程#xff1a;魔术师Dix / HandsOnParallelProgramming GitCode 1、MapReduce 模式 引入 MapReduce 是为了解决处理大数据的问… 本章将介绍并行编程模式重点是理解并行代码问题场景并使用并行编程/异步技术解决他们。本章会介绍几种最重要的编程模式。 本教程学习工程魔术师Dix / HandsOnParallelProgramming · GitCode 1、MapReduce 模式 引入 MapReduce 是为了解决处理大数据的问题例如跨服务器的大规模计算需求。该模式可以在单核计算机上使用。
1.1、映射和归约 MapReduce 程序顾名思义即 Map映射 Reduce归约。MapReduce 程序的输入作为键值对被传递输出也是同样形式。 书上讲的听起来很抽象画张图来辅助理解 输入一个列表然后通过某种方式先进行筛选返回列表然后进行分组返回键值最后返回各个分组的键值对作为结果
1.2、使用 LINQ 实现 MapReduce 其示例中扩展方法如下
public static ParallelQueryTResult MapReduceTSource, TMapped, TKey, TResult(this ParallelQueryTSource source,FuncTSource, IEnumerableTMapped map,FuncTMapped, TKey keySelector,FuncIGroupingTKey, TMapped, IEnumerableTResult reduce){return source.SelectMany(map).GroupBy(keySelector).SelectMany(reduce);} 我们用一个需求来理解这段函数 源数据为 1000 个 -100~100 的随机数 筛选出其中的正数 将其按照10位进行分组0~9一组、10~19一组以此类推 统计每个分组的个数。 那么使用上述 MapReduce 模板进行处理示例代码如下 private void RunMapReduce(){//初始化原始数据int length 1000;Listint L new Listint(length);for (int i 0; i length; i){L.Add(Random.Range(-100, 100));}var ret L.AsParallel().MapReduce(mapPositiveNumbers,//筛选正数groupNumbers,//映射分组reduceNumbers);//归约合并结果foreach (var item in ret){Debug.Log(${item.Key * 10} ~ {(item.Key 1) * 10} 出现了{item.Value} 次 );}}public static IEnumerableint mapPositiveNumbers(int number){IListint PositiveNumbers new Listint();if (number 0)PositiveNumbers.Add(number);return PositiveNumbers;}public static int groupNumbers(int number){return number / 10;}public static IEnumerableKeyValuePairint, int reduceNumbers(IGroupingint, int grouping){return new[]{new KeyValuePairint, int(grouping.Key,grouping.Count())};} 运行结果如下所示 通过上述示例这个映射与归约就容易理解多了实际上就是某一种特定的业务模板写法筛选 → 分组 → 合并。在并行编程中类似这样的写法都可以通过同样的模板代码实现。 2、聚合 聚合Aggregation是并行应用程序中使用的另一种常见的设计模式。在并行程序中数据被划分为多个单元以便可以通过多个线程在内核之间进行处理。在某个时候需要将所有相关来源数据组合起来然后才能呈现给用户。 书上的例子只讨论了使用 PLINQ 代码的示例我们也照着写一个 private void RunAggregation(){var L Utils.GetOrderList(10);var L2 L.AsParallel().Select(TestFunction.IntToString)//并行处理.ToList();//合并foreach (var item in L2)Debug.Log(item);}public static string IntToString(int x){return $ToString_{x};} 上述代码运行结果如下 可以看到这个运行模式是保证顺序的源数据是List。 一般来讲我们为了避免锁、同步等额外处理要么使用 PLINQ 这样的语法要么使用并发集合。这样可以减少我们需要手动处理锁、同步等工作。 3、分叉/合并模式 在分叉/合并Fork/Join模式中工作被分叉拆分为一组可以异步执行的任务然后根据并行化的要求和范围以相同或不同的顺序合并分叉的任务。 分叉/合并模式常见的一些实现如下 Parallel.For Parallel.ForEach Paralle.Invoke System.Threading.CountdownEvent 利用这些同步框架开发人员能快速实现开发而不必担心同步开销系统已经内部处理同步了实际上如如果额外开销不可接受用这些 API 也没办法优化。 我们将之前的代码通过 分叉/合并模式 再改一版 private void RunForkJoin(){var L Utils.GetOrderList(10);ConcurrentQueuestring queue new ConcurrentQueuestring();Parallel.For(0, L.Count, x {var ret IntToString(x);queue.Enqueue(ret);});while (queue.Count 0){string str;if (queue.TryDequeue(out str))Debug.Log(str);}} 这次我们看运行结果 很显然已经乱序了这种模式就没有按照原来数据顺序进行数据处理。这也是这个模式的特点之一我们可以选择是否要按照顺序进行合并。 4、推测处理模式 推测处理模式Speculative Processing Pattern是依赖高吞吐量以减少等待时间的另一种并行编程模式。 推测处理模式Speculative Processing Pattern 如果同时存在多种处理任务但并不知道哪一种方式速度最快。因此第一个执行的完成的任务将被输出其他任务处理结果将会忽略。 以下是一种推测处理模式的常见写法 //选择一个最快执行方法的结果并返回public static TResut SpeculativeForEachTSource, TResut(TSource source, IEnumerableFuncTSource, TResut funcs){TResut result default;Parallel.ForEach(funcs, (func, loopState) {result func(source);loopState.Stop();});return result;}//返回特定方法的最快执行结果并返回public static TResut SpeculativeForEachTSource, TResut(IEnumerableTSource source, FuncTSource, TResut func){TResut result default;Parallel.ForEach(source, (item, loopState) {result func(item);loopState.Stop();});return result;} 这种写法只会返回一个结果首先完成的任务将被返回。但是其他任务仍然有可能执行完成只是结果将不会被返回。 这里我们选择方法一进行示例调用代码如下 private void RunSpeculativeMethod_1(){Debug.Log($ RunSpeculativeMethod_1 开始 );var L1 new ListFuncint, string{IntToString,IntToString2};string result SpeculativeForEach(4, L1);Debug.Log($运行结果{result});} 连续运行2次其结果如下 第一次是使用了 IntToString2 的结果而第二次使用的 IntToString 的结果。 5、延迟模式 也就是在使用时才创建也就是懒加载。这个在之前的章节中已经有详细介绍了这里就不重复了。 详见使用延迟初始化提高性能
【C#】并行编程实战使用延迟初始化提高性能_魔术师Dix的博客-CSDN博客在前面的章节中讨论了 C# 中线程安全并发集合有助于提高代码性能、降低同步开销。本章将讨论更多有助于提高性能的概念包括使用自定义实现的内置构造。本章主要内容为通过延迟初始化提高性能相对比较简单。https://blog.csdn.net/cyf649669121/article/details/131780600
6、共享状态模式 这个主要在 【C#】并行编程实战同步原语1_魔术师Dix的博客-CSDN博客 中已经介绍过共享状态Shared State Pattern的实现其实就是各种加锁搞的好像很高级。 不过上锁不能上太多不然性能很差而且我们也应该尽可能实现无锁代码。 7、本章小结 本章介绍了各种并行编程模式其实就是各种模板的示例。当然这里讲的不可能包罗所有只是给大家提供一些参考。至此多线程编程的学习告一段落书上的内容已经讲完了。后续如果有补充会加到这个系列里。 多线程的实践还是需要在项目中多多练习。 本教程学习工程魔术师Dix / HandsOnParallelProgramming · GitCode 文章转载自: http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.sqtsl.cn.gov.cn.sqtsl.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.tgyzk.cn.gov.cn.tgyzk.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.zqdzg.cn.gov.cn.zqdzg.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.svrud.cn.gov.cn.svrud.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.tsycr.cn.gov.cn.tsycr.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.kwksj.cn.gov.cn.kwksj.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn http://www.morning.zhengdaotang.cn.gov.cn.zhengdaotang.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.bmmhs.cn.gov.cn.bmmhs.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.xjkfb.cn.gov.cn.xjkfb.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn