设计报告资源冲突的数据结构
我有一个有1024个地址的内存地址池。程序内部运行着 16 个线程,它们访问这些内存位置,执行读或写操作。该程序的输出采用一系列四元组的形式,其定义如下
四元组 q1 : (线程号、内存地址、读/写、时间)
例如 q1 = (12,578,r,2t), q2= (16,578 ,w,6t)
我想设计一个程序,它将四元组流作为输入,并报告如果两个以上线程尝试在 5t 秒的时间间隔内至少进行一次写入操作访问同一内存资源时发生的所有冲突。
我心里有几个解决方案,但我不确定它们是否是解决这个问题的最佳方案。我正在从设计和数据结构的角度寻找解决方案。
I have a memory address pool with 1024 addresses. There are 16 threads running inside a program which access these memory locations doing either read or write operations. The output of this program is in the form of a series of quadruples whose defn is like this
Quadruple q1 : (Thread no, Memory address, read/write , time)
e.g q1 = (12,578,r,2t), q2= (16,578,w,6t)
I want to design a program which takes the stream of quadruples as input and reports all the conflicts which occur if more than 2 threads try to access the same memory resource inside an interval of 5t secs with at least one write operation.
I have several solutions in mind but I am not sure if they are the best ones to address this problem. I am looking for a solution from a design and data structure perspective.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这里的基本问题是碰撞检测。我通常会寻找一种将元素添加到某种关联集合中的解决方案。当即将添加新元素时,您需要能够判断集合中是否已经包含相似的元素,这表明发生了冲突。在这里,您似乎需要一个允许重复元素的集合类型,例如 STL 多重映射。四元组(四元组?)显然是关联集合中的值类型,而键类型将包含确定两个元素是否表示冲突所需的数据,即内存地址和时间。为了使用像STL multimap这样的标准关联集合,您需要通过定义operator<来定义键上的一些排序。对于密钥类型(我假设这里是 C++,你没有指定)。您将碰撞定义为内存位置相同且时间值相差小于某个阈值量的两个元素。键类型的排序必须使得表示冲突的两个键在该排序下是等效的。 < 下的等价性运算符表示为 < b 为假且 b < a 也是 false,因此顺序可能由该运算符定义:
此设计存在问题,因为在 << 下两个键可能是等效的。没有平等。这意味着两个不同但相似的四元组(即两个相互冲突的值)将存储在集合中的相同键下。您可以使用更简单的排序定义
在此排序定义下,碰撞元素最终在有序关联容器中相邻(但在不同的键下),因此您可以在它们具有后处理步骤中轻松找到它们全部已添加到集合中。
So the basic problem here is collision detection. I would generally look for a solution where elements are added to some kind of associative collection. As a new element is about to be added, you need to be able to tell whether the collection already contains a similar element, indicating a collision. Here you would seem to need a collection type that allows for duplicate elements, such as the STL multimap. The Quadraple (quadruple?) would obviously be the value type in the associative collection, and the key type would contain the data necessary to determine whether two elements represent a collision, i.e. memory address and time. In order to use a standard associative collection like STL multimap, you need to define some ordering on the keys by defining operator< for the key type (I'm assuming C++ here, you didn't specify). You define a collision as two elements where the memory location is identical and the time values differ by less than some threshold amount. The ordering of the key type has to be such that two keys that represent a collision come out as equivalent under the ordering. Equivalence under the < operator is expressed as a < b is false and b < a is false as well, so the ordering might be defined by this operator:
There is a problem with this design, due to the fact that two keys may be equivalent under < without being equal. This means that two different but similar Quadraples, i.e. two values that collide with one another, would be stored under the same key in the collection. You could use a simpler definition of the ordering
Under this ordering definition, colliding elements end up adjacent in an ordered associative container (but under different keys), so you'd be able to find them easily in a post-processing step after they have all been added to the collection.