D并发写入缓冲区 [英] D concurrent writing to buffer
问题描述
假设您有一个大小为N的缓冲区,必须设置为确定值(例如为零或其他值)。缓冲区中的值设置分为M个线程,每个处理缓冲区的N / M个元素。
Say you have a buffer of size N which must be set to definite values (say to zero, or something else). This value setting in the buffer is divided over M threads, each handling N / M elements of the buffer.
缓冲区不能是 immutable
,因为我们改变了值。消息传递也不会工作,因为它禁止传递ref或array(= pointer)类型。所以它必须通过
shared
发生?不,因为在我的情况下,缓冲区元素的类型 creal
,因此算术不是原子。
The buffer cannot be immutable
, since we change the values. Message passing won't work either, since it is forbidden to pass ref or array (= pointer) types. So it must happen through shared
? No, since in my case the buffer elements are of type creal
and thus arithmetics are not atomic.
结束,主程序必须等待所有线程完成。给定每个线程仅写入数组的子集,并且没有线程在数组中与另一个线程重叠或以任何方式依赖于彼此。
At the end, the main program must wait until all threads are finished. It is given that each thread only writes to a subset of the array and none of the threads have overlap in the array with another thread or in any way depend on eachother.
PS:有时我可以简单地将数组连续分成M个,但有时候逐列转换数组(数组是1D,但表示2D数据)。这使得线程使用的各个阵列实际上在母阵列中交织。 Arbour。
PS: sometimes I can simply divide the array in M consecutive pieces, but sometimes I go over the array (the array is 1D but represents 2D data) column-by-column. Which makes the individual arrays the threads use be actually interleaved in the mother-array. Argh.
编辑:我想出类型 shared(creal)[] code>将工作,因为现在的元素是共享的,而不是数组本身。你可以并行交织数组我敢打赌。有一些缺点,但是:
I figured out that the type shared(creal)[]
would work, since now the elements are shared and not the array itself. You could parallelize interleaved arrays I bet. There is some disadvantage though:
存储类是非常严格的,分配必须提供与关键词。这使得它几乎不被包封;因为调用者必须提供数组,所以它必须传递一个
shared
数组,而不能只是通常传递一个规则数组,让处理函数担心并行性。不,调用函数必须担心并行性,以便处理函数接收一个共享数组,而不需要将数组重新分配到 shared
空间。
The shared
storage class is so strict, that the allocation must be supplied with the keyword. Which makes it hardly encapsulated; since the caller must supply the array, it is obligated to pass a shared
array and can't just generically pass a regular array and let the processing function worry about parallelism. No, the calling function must worry about parallelism too, so that the processing function receives a shared array and needn't reallocate the array into shared
space.
还有一个很奇怪的错误,当我在某些地方动态分配 这是一个错误在DMD / OptLink shared(creal)[]
挂起在分配。似乎很随机,找不到罪魁祸首...
在测试示例中,这是工作,但不是在我的项目...
There is also a very strange bug, that when I dynamically allocate This turned out to be a bug in DMD / OptLink.shared(creal)[]
at certain spots, it simply hangs at allocation. Seems very random and can't find the culprit...
In the test example this works, but not in my project...
EDIT2:我从来没有提到,但它是用于实现FFT(快速傅立叶定理)。所以我没有权力选择精确的缓存对齐切片。所有我知道的是元素是类型creal和元素的数量是2的幂(每行/列)。
I never mentioned, but it's for implementing the FFT (Fast Fourier Theorem). So I have no power over selecting precise cache aligned slices. All I know is the elements are of type creal and the number of elements is a power of 2 (per row / column).
推荐答案
p>您可以使用 std.parallelism 模组
T[] buff;
foreach(ref elem;parallel(buff))elem=0;
但是如果你想重新创建这个,你可以使用共享线程访问某个元素,如果你用适当的 join()
或 Task。* force()
这么好)
but if you want to reinvent this you can just use shared (it is thread safe to only let 1 thread accesses a certain element at the time and if you enforce this with the appropriate join()
or Task.*force()
so much the better)
这篇关于D并发写入缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!