在线程之间分割循环迭代(Dividing loop iterations among threads)
我最近编写了一个小数量的计算程序,基本上在N维网格上循环,并在每个点执行一些计算。
for (int i1 = 0; i1 < N; i1++) for (int i2 = 0; i2 < N; i2++) for (int i3 = 0; i3 < N; i3++) for (int i4 = 0; i4 < N; i4++) histogram[bin_index(i1, i2, i3, i4)] += 1; // see bottom of question
它工作得很好,yadda yadda yadda,可爱的图表结果;-)但是,后来我想,我的电脑上有2个内核,为什么不让这个程序多线程,所以我可以运行两倍的速度?
现在,我的循环总共运行了大约十亿次计算,我需要一些方法将它们在线程之间分开。 我想我应该将计算分组为“任务” - 比方说最外层循环的每次迭代都是一项任务 - 并将任务交给线程。 我考虑过了
- 只是给线程#n最外层循环的所有迭代,其中
i1 % nthreads == n
- 从本质上预先确定哪些任务转到哪个线程- 试图设置一些互斥保护的变量,它保存需要执行的下一个任务的参数(本例中为
i1
) - 动态分配任务给线程有什么理由选择另一种方法? 或者我还没有想过的另一种方法? 它甚至重要吗?
顺便说一下,我用C编写了这个特定的程序,但我想我会在其他语言中再次做同样的事情,所以答案不一定是C语言特定的。 (如果有人知道用于Linux的C库可以做这种事情,但我很想知道它)
编辑 :在这种情况下,
bin_index
是一个确定性函数,除了它自己的局部变量外,它不会改变任何东西。 像这样的东西:int bin_index(int i1, int i2, int i3, int i4) { // w, d, h are constant floats float x1 = i1 * w / N, x2 = i2 * w / N, y1 = i3 * d / N, y2 = i4 * d / N; float l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + h * h); float th = acos(h / l); // th_max is a constant float (previously computed as a function of w, d, h) return (int)(th / th_max); }
(尽管我赞赏所有的评论,甚至那些不适用于确定性bin_index的评论)
I recently wrote a small number-crunching program that basically loops over an N-dimensional grid and performs some calculation at each point.
for (int i1 = 0; i1 < N; i1++) for (int i2 = 0; i2 < N; i2++) for (int i3 = 0; i3 < N; i3++) for (int i4 = 0; i4 < N; i4++) histogram[bin_index(i1, i2, i3, i4)] += 1; // see bottom of question
It worked fine, yadda yadda yadda, lovely graphs resulted ;-) But then I thought, I have 2 cores on my computer, why not make this program multithreaded so I could run it twice as fast?
Now, my loops run a total of, let's say, around a billion calculations, and I need some way to split them up among threads. I figure I should group the calculations into "tasks" - say each iteration of the outermost loop is a task - and hand out the tasks to threads. I've considered
- just giving thread #n all iterations of the outermost loop where
i1 % nthreads == n
- essentially predetermining which tasks go to which threads- trying to set up some mutex-protected variable which holds the parameter(s) (
i1
in this case) of the next task that needs executing - assigning tasks to threads dynamicallyWhat reasons are there to choose one approach over the other? Or another approach I haven't thought about? Does it even matter?
By the way, I wrote this particular program in C, but I imagine I'll be doing the same kind of thing again in other languages as well so answers need not be C-specific. (If anyone knows a C library for Linux that does this sort of thing, though, I'd love to know about it)
EDIT: in this case
bin_index
is a deterministic function which doesn't change anything except its own local variables. Something like this:int bin_index(int i1, int i2, int i3, int i4) { // w, d, h are constant floats float x1 = i1 * w / N, x2 = i2 * w / N, y1 = i3 * d / N, y2 = i4 * d / N; float l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + h * h); float th = acos(h / l); // th_max is a constant float (previously computed as a function of w, d, h) return (int)(th / th_max); }
(although I appreciate all the comments, even those which don't apply to a deterministic bin_index)
原文:https://stackoverflow.com/questions/564577
最满意答案
这是 Freemarker网页展示技术的语法,类似于jsp(也是网页展示技术),freemarker现在国外用得比较多,好处我就不说了,<#include>这种是freemarker自带的标签命令,类似于jsp的 , 而<@p.>这是通过freemarker的宏自定义出来的命令,相当于jsp 的自定义标签. ps:当然网页展示技术的背后,都必定会有解析这些命令的,并最终转化成网页能识别的(html)代码(架包)
其他回答
看上去你这个像电脑语言,就是c语言
c开头是include c#开头是using 他们是调用 引用的意思 你这个引用了一个image_upload.html页面 换句话说 引用好比一个工具箱 假如你要修理电脑 要先把工具包拿出来 然后 在包里拿你所需要的工具 但是你没有这个工具包的时候 你就无法用工具修电脑
相关问答
更多-
freemarker问题,ftl文件制作问题,java问题。[2022-10-20]
ftl只是一个后缀而已,不一定需要叫xxx.ftl,java封装好data,调用freemarker的process替换其中的${xxx} -
eclipse中修改了ftl文件中的一个链接,但是运行结果不变[2022-02-18]
尝试清空浏览器缓存 -
在html页面中包含<#include (ftl文件) />这是什么语法,提供点相关资料![2022-07-24]
这是 Freemarker网页展示技术的语法,类似于jsp(也是网页展示技术),freemarker现在国外用得比较多,好处我就不说了,<#include>这种是freemarker自带的标签命令,类似于jsp的 , 而<@p.>这是通过freemarker的宏自定义出来的命令,相当于jsp 的自定义标签. ps:当然网页展示技术的背后,都必定会有解析这些命令的,并最终转化成网页能识别的(html)代码(架包) -
主题资源的文件路径存储在主题数据中,例如:
如何从ofbiz的ftl文件中正确调用java函数?(how to properly call a java function from a ftl file in ofbiz?)[2021-12-15]
使用ofbiz控制器,您可以通过两种方式发回响应: 优先更常见的方式:在你的java事件中调用一些服务,或者做一些处理,然后你处理一个request属性的结果,然后在视图中渲染这些属性。 如您所见,您的控制器指向一个视图。 这种情况不太常见,当您需要向用户发送一些无法在视图中呈现的数据时,在这种情况下,您手动在响应对象中写入数据,然后关闭响应流。 我假设你正是这样做的。 在这种情况下,您不应该在控制器中返回视图,因为您已经编写了数据并希望关闭响应流。 所以只需将控制器更改为:每次需要加载模板时(对于#include或Configuration.getTemplate等),FreeMarker都会调用Configuration的TemplateLoader来执行此操作(除非模板已经在缓存中)。 由TemplateLoader来解释地址并实际加载模板。 没有包含从http:地址加载的TemplateLoader ,因此您需要创建一个自定义的TemplateLoader实现。 然后将templateLoader配置设置为它。 Every time a template need to ...FMPP使用旧的FTL语法(FMPP use old FTL syntax)[2023-08-04]
看来你不能......它不会通过FMPP的设置API暴露。 如果有任何安慰,新的语法更实用,因为它捕获更多的拼写错误,并且Eclipse插件支持它。 所以如果你有一些旧的模板,那么最好花15分钟时间,用#版本搜索并替换FTL标签。 I was able to solve this by adding an "oldSyntax" Setting myself using the "setOldTemplateSyntax" method that is offered by the Engine. http ...就像skuro所说的那样,你并没有正确地做到这一点。 很简单你的配置是正确的但你的ftl缺乏一些逻辑。 为什么你首先检查<#if field.control.params.caseID??>然后不分配它? 它应该是: <#if field.control.params.caseID??> <#assign caseID = field.control.params.caseID> <#else> <#assign caseID = 1> #if> 不需要上下文或args或其他东西。 看一下 ...您可以尝试使用带有html的文本的th:utext标记。 这是官方文档 - 未转义的文本 Things didn't work out using th:text nor th:utext however what I did was to read the document.html as a text file on java and then placed the text on the textarea. I used some most of the code posted on: http:// ...您可以轻松访问modelMap数据: <#list dto as dtoList>${dtoList.var1} ${dtoList.var1} 相关文章
更多- Storm-源码分析-Topology Submit-Executor-mk-threads
- Lua循环遍历
- Linux内核Bridge代码的STP的实现(转)
- 关于Thread类中的start()方法和run()方法
- 线程报错 thread
- twitter storm源码走读之2 -- tuple消息发送场景分析
- 关于多线程问题,signalAll,await问题
- 关于线程的问题
- 关于线程
- java WHILE 循环问题
最新问答
更多- 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
- linux的常用命令干什么用的
- 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
- 如何交换返回集中的行?(How to swap rows in a return set?)
- 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
- 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
- Java中的不可变类(Immutable class in Java)
- WordPress发布查询(WordPress post query)
- 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
- 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
- GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
- 绑定属性设置器未被调用(Bound Property Setter not getting Called)
- linux ubuntu14.04版没有那个文件或目录
- 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
- 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
- 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
- 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
- ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
- linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
- Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
- 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
- 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
- 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
- Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
- 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
- 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
- 如何按部门显示报告(How to display a report by Department wise)
- Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
- Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
- WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)