在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
最满意答案
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
< ... -
您的示例不显示用于初始化类属性的Heredoc 。 从Heredoc手册,使用Heredoc初始化一个类属性自5.3开始工作: class foo { public $bar = <<因为; 你的EOB;之前有空格EOB; (结束标识符)是其中4个; 删除它们。 根据heredoc上的手册: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 警告 请注意,除了分号(;)之外,具有结束标识符的行必须不包含其他字符,这一点非常重要。 这尤其意味着标识符可能不会缩进,并且在分号之前或之后可能没有任何空格或制表符。 同样重要的是要意识到结束标识符之前的第一个字符必须是本地 ...
尝试使用heredoc在回显中打印变量时出现PHP错误(PHP error when attempting to print variables within an echo using heredoc)[2023-08-15]
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.前两个例子中的终止符后面可能有空格,例如 EOD;[space] 有了这个:heredoc语句有问题(Problem with heredoc statement)[2022-10-30]
从PHP手册中了解Heredoc语法 : 结束标识符必须从行的第一列开始。 稍后在漂亮的红色警告框中: 请注意,关闭标识符的行必须不包含其他字符,除了可能使用分号(;)外,非常重要。 这意味着特别是标识符可能不会缩进 ,并且在分号之前或之后可能没有任何空格或制表符。 所以你需要像这样编写代码以符合语法规范: $table = <<