首页 \ 问答 \ 在std :: deque上并行化std :: replace(Parallelizing std::replace on std::deque)

在std :: deque上并行化std :: replace(Parallelizing std::replace on std::deque)

首先,我知道双端队列中的多个写手不是很容易处理。 但是使用以下算法,我可以保证元素上没有并发访问。 该算法在块中划分了一个deque(它非常大,这就是我将它并行化的原因)并且std :: replaces替换了deque中的值。 问题是,在某些情况下,在替换任意值之后,该值似乎仍然存在(顺便说一下:新值与旧值不同)。 可能是这个值没有从cpu寄存器中同步到内存吗? 这里的代码:

std::deque<int*> _deque;
...
int threadsCount = 25;          
int chunkSize = ceil((float) _deque.size() / (float) threadsCount);                                                                                                                          
std::vector<std::thread> threads;
for (int threadNo = 0; threadNo < threadsCount; threadNo++) {
   std::uint64_t beginIndex = threadNo * chunkSize;
   std::uint64_t endIndex = (threadNo + 1) * chunkSize;
   if (endIndex > _deque.size()) {    
      endIndex = _deque.size();      
   }
   std::deque<int*>::iterator beginIterator = _deque.begin() + beginIndex;
   std::deque<int*>::iterator endIterator = _deque.begin() + endIndex;
   threads.push_back(std::thread([beginIterator, endIterator, elementToReplace, elementNew] () {
      std::replace(beginIterator, endIterator, elementToReplace, elementNew);                                      
   }));
}
for (int threadNo = 0; threadNo < threadsCount; threadNo++) {                                                                                                                               
   threads[threadNo].join();     
}

在该算法之后,有时(不确定性)替换(elementToReplace)值仍然在deque中的情况。


First of all I know that multiple writters on a deque are not very easy to handle. But with the following algorithm I can guarantee that there is no concurrent access on elements. The algorithm divides a deque (it is very large, thats the reason why I parallelize it) in chunks and the std::replaces replaces a value in the deque. The problem is, that in some cases after replacing an arbitrary value, the value seems to still exist (btw: it is NOT the case that the new value is the same as the old one). Is it maybe the case that the value is not synced out of the cpu register to the memory? Here the code:

std::deque<int*> _deque;
...
int threadsCount = 25;          
int chunkSize = ceil((float) _deque.size() / (float) threadsCount);                                                                                                                          
std::vector<std::thread> threads;
for (int threadNo = 0; threadNo < threadsCount; threadNo++) {
   std::uint64_t beginIndex = threadNo * chunkSize;
   std::uint64_t endIndex = (threadNo + 1) * chunkSize;
   if (endIndex > _deque.size()) {    
      endIndex = _deque.size();      
   }
   std::deque<int*>::iterator beginIterator = _deque.begin() + beginIndex;
   std::deque<int*>::iterator endIterator = _deque.begin() + endIndex;
   threads.push_back(std::thread([beginIterator, endIterator, elementToReplace, elementNew] () {
      std::replace(beginIterator, endIterator, elementToReplace, elementNew);                                      
   }));
}
for (int threadNo = 0; threadNo < threadsCount; threadNo++) {                                                                                                                               
   threads[threadNo].join();     
}

After that algorithm it is sometimes (not deterministic) the case that a replaced (elementToReplace) value is still in the deque.


原文:https://stackoverflow.com/questions/45096976
更新时间:2024-05-03 20:05

最满意答案

PHP中没有关键字var 。 不是在PHP5中 - 它只是由于向后兼容性而被接受,并且用于定义类变量。


There is no keyword var in PHP. Not in PHP5 anyway - it's only accepted due to backward compatibility, and is used to define class variables.

相关问答

更多
  • 脚本中未加载jquery库 jquery library was not loaded in the script
  • 我个人不会使用HEREDOC。 它只是不利于一个好的“模板构建”系统。 所有的HTML被锁定在一个有几个缺点的字符串中 WYSIWYG没有选择 来自IDE的HTML没有代码完成 输出(HTML)锁定到逻辑文件 你最终不得不像现在想要的那样使用黑客来实现更复杂的模板,比如循环 获取基本的模板引擎,或者只是使用包含PHP的PHP - 这就是为什么语言有分隔符。 template_file.php <?php echo $page_title; ?>< ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/phpheredocyfzsblsyfjgcwzysx_191" target="_blank">为什么PHP在Heredoc语法中使用变量时不会引发任何警告/错误/注意事项?(Why PHP is not throwing any warning/error/notice upon using variables in Heredoc syntax?)</a><i>[2022-07-28] </i></h2> </div> <div class="tw_li_cont"> 您的示例不显示用于初始化类属性的Heredoc 。 从Heredoc手册,使用Heredoc初始化一个类属性自5.3开始工作: class foo { public $bar = <<<EOT bar EOT; } 使用变量初始化类属性不会: class foo { public $bar = <<<EOT {$_SERVER['PHP_SELF']} bar EOT; } 致命错误:常量表达式包含无效操作 使用任何不计算为常量表达式的方法都是一样的: class foo { p ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/phpheredocfuzhibianliangcuowu_195" target="_blank">PHP heredoc赋值给变量错误(PHP heredoc assignment to a variable error)</a><i>[2023-12-19] </i></h2> </div> <div class="tw_li_cont"> 因为; 你的EOB;之前有空格EOB; (结束标识符)是其中4个; 删除它们。 根据heredoc上的手册: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 警告 请注意,除了分号(;)之外,具有结束标识符的行必须不包含其他字符,这一点非常重要。 这尤其意味着标识符可能不会缩进,并且在分号之前或之后可能没有任何空格或制表符。 同样重要的是要意识到结束标识符之前的第一个字符必须是本地 ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/csheredochxzdyblsphpcw_215" target="_blank">尝试使用heredoc在回显中打印变量时出现PHP错误(PHP error when attempting to print variables within an echo using heredoc)</a><i>[2023-08-15] </i></h2> </div> <div class="tw_li_cont"> PHP中没有关键字var 。 不是在PHP5中 - 它只是由于向后兼容性而被接受,并且用于定义类变量。 There is no keyword var in PHP. Not in PHP5 anyway - it's only accepted due to backward compatibility, and is used to define class variables. </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/heredocnowdocdaozhicuowu_275" target="_blank">为什么这些Heredoc和Nowdoc会导致错误?(Why do these Heredoc and Nowdoc cause errors?)</a><i>[2023-01-31] </i></h2> </div> <div class="tw_li_cont"> 前两个例子中的终止符后面可能有空格,例如 EOD;[space] 有了这个: <?php echo <<<EOL test EOL;[space] 我收到你的错误信息,但没有空间,没有错误。 不管是否收盘,这都是真的。 You probably have spaces after the terminator in your first two examples, e.g. EOD;[space] With this: <?php echo <<<EOL test EOL;[space] I get ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/heredocyuju_309" target="_blank">heredoc语句有问题(Problem with heredoc statement)</a><i>[2022-10-30] </i></h2> </div> <div class="tw_li_cont"> 从PHP手册中了解Heredoc语法 : 结束标识符必须从行的第一列开始。 稍后在漂亮的红色警告框中: 请注意,关闭标识符的行必须不包含其他字符,除了可能使用分号(;)外,非常重要。 这意味着特别是标识符可能不会缩进 ,并且在分号之前或之后可能没有任何空格或制表符。 所以你需要像这样编写代码以符合语法规范: $table = <<<ENDHTML <div style="text-align:center;"> <table border="0.5" cellpadding="1" cell ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/echonengzaiheredoczhongshipsql_342" target="_blank">\ echo不能在heredoc中使用psql(\echo is not working in heredoc pased to psql)</a><i>[2022-04-11] </i></h2> </div> <div class="tw_li_cont"> 来自Abelisto的回答 可能是因为“\ o [FILE]将所有查询结果发送到文件或管道”。 而不是\ echo尝试: “\ qecho [STRING]写入字符串来查询输出流 Answer from Abelisto Probably its because "\o [FILE] send all query results to file or |pipe". Instead of \echo try : "\qecho [STRING] write string to query output st ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/phpheredocbhzhtmlzxsblwordpress_351" target="_blank">PHP Heredoc不会在HTML中显示变量 - WordPress(PHP Heredoc won't display variables in HTML - WordPress)</a><i>[2023-03-14] </i></h2> </div> <div class="tw_li_cont"> 对不起大家,当我填充$ envUserProfile变量时,我有拼写错误。 固定。 Sorry everyone, I had typos when I's populating $envUserProfile variable. Fixed. </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/eredheredocnowdocyufa_416" target="_blank">Ered in heredoc / nowdoc语法(Echo in heredoc / nowdoc syntax)</a><i>[2022-07-15] </i></h2> </div> <div class="tw_li_cont"> 我相信这是你打算做的事情,看起来有点尴尬: $fn = escape($mt->from_name); $time = escape(date("F d, Y - h:i a", strtotime($mt->st_time))); $stc = nl2br(escape($mt->st_content)); //START EMAIL User::sendNewticket($send_to, 'Maintenance Ticket ' . Input::get('st_id'), <<<TEXT He ... </div> </div> </li> </ul> </div> <div class="main_right"> <div class="search-out"> <div class="search"> <form action="/wenda" target="_blank" method="get"> <input type="search" autocorrect="off" autocomplete="off" placeholder="请输入关键词" id="q" name="q" value=""> <button class="btn_s" type="submit">搜索</button> </form> </div> </div> <div class="commonh"> <h2>相关文章</h2> <span class="fr"><a href="/jiaocheng" target="_blank">更多</a></span> </div> <div class="right_list"> <li> <a title="javascript replace 连续用两次的问题" href="/article/javascriptreplacelxylcdwt_5" target="_blank">javascript replace 连续用两次的问题</a> </li> <li> <a title="Memcached replace 替换已存在的 key命令" href="/article/Memcachedreplacemingling1334_12" target="_blank">Memcached replace 替换已存在的 key命令</a> </li> <li> <a title="add more solr core for switch deploy old new replace hot deploy" href="/article/addmoresolrcoreforswitchdeployol_2" target="_blank">add more solr core for switch deploy old new replace hot deploy</a> </li> <li> <a title="error C2668: 'M' : ambiguous call to overloaded function" href="/article/errorC2668Mambiguouscalltooverloadedfunction_5" target="_blank">error C2668: 'M' : ambiguous call to overloaded function</a> </li> <li> <a title="引入thread后socket接受不了报文了" href="/article/yrthreadhsocketjsblbwl_4" target="_blank">引入thread后socket接受不了报文了</a> </li> <li> <a title="Hadoop lzo文件的并行map处理" href="/article/Hadooplzowjdbxmapcl_0" target="_blank">Hadoop lzo文件的并行map处理</a> </li> <li> <a title="HTML5 视频播放事件属性与API控件 " href="/article/HTML5spbfsjsxyAPIkj_0" target="_blank">HTML5 视频播放事件属性与API控件 </a> </li> <li> <a title="Hadoop 通过distcp进行并行复制" href="/article/Hadooptgdistcpjxbxfz_0" target="_blank">Hadoop 通过distcp进行并行复制</a> </li> <li> <a title="Hadoop集群lzo文件的并行map处理" href="/article/Hadoopjqlzowjdbxmapcl_0" target="_blank">Hadoop集群lzo文件的并行map处理</a> </li> <li> <a title="I18N 国际化 简介" href="/article/I18Nguojihuajianjie_8" target="_blank">I18N 国际化 简介</a> </li> </div> <div class="commonh"> <h2>最新问答</h2> <span class="fr"><a href="/wenda" target="_blank">更多</a></span> </div> <div class="right_list"> <li> <a title="python的访问器方法有哪些" href="/wenda/pythonfangwenqifangfa_93" target="_blank">python的访问器方法有哪些</a> </li> <li> <a title="使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)" href="/wenda/zendframeworkzjoinsqljssj_199" target="_blank">使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)</a> </li> <li> <a title="透明度错误IE11(Transparency bug IE11)" href="/wenda/toumingducuowuie11_313" target="_blank">透明度错误IE11(Transparency bug IE11)</a> </li> <li> <a title="linux的基本操作命令。。。" href="/wenda/linuxdejibencaozuomingling_66" target="_blank">linux的基本操作命令。。。</a> </li> <li> <a title="响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)" href="/wenda/xynavizdh1navh1ljbqzy_271" target="_blank">响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)</a> </li> <li> <a title="在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)" href="/wenda/czdqwjrabztx_237" target="_blank">在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)</a> </li> <li> <a title="NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)" href="/wenda/nfctigongyangdaikuan_274" target="_blank">NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)</a> </li> <li> <a title="<td>元素上的盒子阴影行为(box-shadow behaviour on <td> elements)" href="/wenda/tdyuansuheziyinying_387" target="_blank"><td>元素上的盒子阴影行为(box-shadow behaviour on <td> elements)</a> </li> <li> <a title="Laravel检查是否存在记录(Laravel Checking If a Record Exists)" href="/wenda/laraveljianchazaiji_164" target="_blank">Laravel检查是否存在记录(Laravel Checking If a Record Exists)</a> </li> <li> <a title="设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)" href="/wenda/szbase64txdxjavascriptangularjs_127" target="_blank">设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)</a> </li> <li> <a title="想学Linux 运维 深圳有哪个培训机构好一点" href="/wenda/xxlinuxywszpxjgyd_15" target="_blank">想学Linux 运维 深圳有哪个培训机构好一点</a> </li> <li> <a title="为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)" href="/wenda/sbxylambdazbhclbl_160" target="_blank">为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)</a> </li> <li> <a title="在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)" href="/wenda/framework35zsfwqbqszvisiblesx_157" target="_blank">在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)</a> </li> <li> <a title="AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)" href="/wenda/adonetappenderzlog4netljlxwx_518" target="_blank">AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)</a> </li> <li> <a title="错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)" href="/wenda/cuowufasongshezhibiaoti_226" target="_blank">错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)</a> </li> <li> <a title="等待EC2实例重启(Wait for an EC2 instance to reboot)" href="/wenda/dengdaiec2shilizhongqi_206" target="_blank">等待EC2实例重启(Wait for an EC2 instance to reboot)</a> </li> <li> <a title="如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)" href="/wenda/hzhbszszzbds_160" target="_blank">如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)</a> </li> <li> <a title="使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)" href="/wenda/sbopenglglutzhzdbx_480" target="_blank">使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)</a> </li> <li> <a title="江民杀毒软件的KSysnon.sys模块是什么东西?" href="/wenda/jmsdrjksysnonsysmkssmdx_68" target="_blank">江民杀毒软件的KSysnon.sys模块是什么东西?</a> </li> <li> <a title="处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)" href="/wenda/clqcdadd_xpathadd_valuesdylsxadd_160" target="_blank">处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)</a> </li> <li> <a title="sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)" href="/wenda/sp_updatestatsdzsqlserver2005zwf_476" target="_blank">sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)</a> </li> <li> <a title="如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)" href="/wenda/cjcxyxfwjhclilsymysqlshell_445" target="_blank">如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)</a> </li> <li> <a title="AESGCM解密失败的MAC(AESGCM decryption failing with MAC)" href="/wenda/aesgcmjiemishibaimac_544" target="_blank">AESGCM解密失败的MAC(AESGCM decryption failing with MAC)</a> </li> <li> <a title="SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)" href="/wenda/sqlchaxunziduanbaohanx_162" target="_blank">SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)</a> </li> <li> <a title="PerSession与PerCall(PerSession vs. PerCall)" href="/wenda/persessionpercall_90" target="_blank">PerSession与PerCall(PerSession vs. PerCall)</a> </li> <li> <a title="C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)" href="/wenda/clggzhsdxsxszzyq_374" target="_blank">C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)</a> </li> <li> <a title="平衡一个精灵(Balancing a sprite)" href="/wenda/pinghengjingling_437" target="_blank">平衡一个精灵(Balancing a sprite)</a> </li> <li> <a title="n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))" href="/wenda/n2cmsaspnetwjcdjccwwjglq_444" target="_blank">n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))</a> </li> <li> <a title="Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)" href="/wenda/zurbfoundationqiantaowanggeduiqi_221" target="_blank">Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)</a> </li> <li> <a title="湖北京山哪里有修平板计算机的" href="/wenda/hubeijingshanxiupingbanjisuanji_10" target="_blank">湖北京山哪里有修平板计算机的</a> </li> </div> </div> </div> </div> <div style="clear:both;"></div> <div class="footer"> <div class="mainbox"> <div class="info"> <p>Copyright ©2023 <a href="https://www.peixunduo.com" target="_blank">peixunduo.com</a> All Rights Reserved.<a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备14003112号</a> </p> <p>本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!</p> </div> </div> </div> <script type="text/javascript" src="/resources/js/common.js?v=324"></script> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https'){ bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else{ bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?9eebaceb5e4371a0aad59712a1a1ecff"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>