如何在同一个数组中的随机位置上执行多线程操作?(How to perform multithreading operations on random positions in the same arr

编程入门 行业动态 更新时间:2024-10-09 18:20:19
如何在同一个数组中的随机位置上执行多线程操作?(How to perform multithreading operations on random positions in the same array?)

我有一个进程在Array随机位置上运行大量任务,并希望通过使用多线程来加快速度。

它本质上做的是随机化“阵列”中的位置,检查其近似环境的值,并在满足一些特定条件时改变随机位置值。

是否有可能运行类似的东西

Parallel.For(0, n, s => { });

循环而不是下面显示的while代码块来优化这个函数,以及一个代码块如何做到这一点?

我一直在考虑为所选元素使用一些“忙”属性,但这实际上使得问题可能需要更加复杂。

public void doStuffTothisArray(ref int[,,] Array, ref IGenerator randomGenerator, int loops) { int cc = 0; int sw = 0; do { if (doStuffOnRandomPositions(ref Array, ref randomGenerator)) sw++; //if stuff was made counter if ((cc % (loops / 10)) == 0) Console.Write("{0} % \t", (cc / (loops / 10)) * 10); //some loading info cc++; //count iterations } while (cc < loops); Console.WriteLine("Stuff altered in {0} iterations: {1}", loops, sw); }

发布编辑:

划分数组并分配工作会破坏阵列的动态,因为它需要是一个完整的系统。

这是dostuff的原型..()

public static bool doStuffOnRandomPositions(ref lattice A, ref IGenerator rr) { position firstPos = new position(rr.Next(0, A.n_size),rr.Next(0, A.n_size),rr.Next(0, A.n_size)); position secondPos = randomNeighbour(ref A, firstPos, ref rr); //checks the closest 3d neighbours indexer the lattice //Console.WriteLine("first:[{0},{1},{2}]\nsecond:[{3},{4},{5}]\n", firstPos.x, firstPos.y, firstPos.z, secondPos.x, secondPos.y, secondPos.z); // get values at coordinates bool first = A.latticeArray[firstPos.x, firstPos.y, firstPos.z]; bool second = A.latticeArray[secondPos.x,secondPos.y,secondPos.z]; if (first == second) //don't bother if they are equal states return false; // checks the energies in surroundings for an eventual spin switch int surrBefore = surroundCheck(ref A, firstPos, first) ; // - surroundCheck(ref A, secondPos, second)); int surrAfter = surroundCheck(ref A, firstPos, !first) ; // - surroundCheck(ref A, secondPos, !second)); if (surrAfter < surrBefore) //switch spin states if lower total energy { A.latticeArray[firstPos.x, firstPos.y, firstPos.z] = !first; A.latticeArray[secondPos.x, secondPos.y, secondPos.z] = !second; return true; } else if ((surrAfter == surrBefore) & latticeDistribution(ref rr)) //TEMPORARY { A.latticeArray[firstPos.x, firstPos.y, firstPos.z] = !first; //TEMPORARY A.latticeArray[secondPos.x, secondPos.y, secondPos.z] = !second; //TEMPORARY return true; } else return false; } //FIX SWITCH PROBABILITIES

在这里,晶格类应该表示包含其属性的“数组”。 由于我对c#方法的经验不足,示例解决方案代码非常感谢。

I have a process running lots of tasks on random positions in Array and would like to speed this up by the use of multithreading.

What it essentially does is to randomize a position in "Array", check its close surroundings for its values and alter the random position values if a few specific conditions are met.

Would it be possible to run something like a

Parallel.For(0, n, s => { });

loop instead of the while chunk of code shown below to optimize this function, and how would a block of code to do this look like?

I've been thinking about using some "busy" property for the chosen elements but that essentially makes the problem more complicated that it might need to be.

public void doStuffTothisArray(ref int[,,] Array, ref IGenerator randomGenerator, int loops) { int cc = 0; int sw = 0; do { if (doStuffOnRandomPositions(ref Array, ref randomGenerator)) sw++; //if stuff was made counter if ((cc % (loops / 10)) == 0) Console.Write("{0} % \t", (cc / (loops / 10)) * 10); //some loading info cc++; //count iterations } while (cc < loops); Console.WriteLine("Stuff altered in {0} iterations: {1}", loops, sw); }

Post edit:

Dividing the array and distributing the work destroys the dynamics of the array, since it needs to be a complete system.

Here's a prototype of the dostuff..()

public static bool doStuffOnRandomPositions(ref lattice A, ref IGenerator rr) { position firstPos = new position(rr.Next(0, A.n_size),rr.Next(0, A.n_size),rr.Next(0, A.n_size)); position secondPos = randomNeighbour(ref A, firstPos, ref rr); //checks the closest 3d neighbours indexer the lattice //Console.WriteLine("first:[{0},{1},{2}]\nsecond:[{3},{4},{5}]\n", firstPos.x, firstPos.y, firstPos.z, secondPos.x, secondPos.y, secondPos.z); // get values at coordinates bool first = A.latticeArray[firstPos.x, firstPos.y, firstPos.z]; bool second = A.latticeArray[secondPos.x,secondPos.y,secondPos.z]; if (first == second) //don't bother if they are equal states return false; // checks the energies in surroundings for an eventual spin switch int surrBefore = surroundCheck(ref A, firstPos, first) ; // - surroundCheck(ref A, secondPos, second)); int surrAfter = surroundCheck(ref A, firstPos, !first) ; // - surroundCheck(ref A, secondPos, !second)); if (surrAfter < surrBefore) //switch spin states if lower total energy { A.latticeArray[firstPos.x, firstPos.y, firstPos.z] = !first; A.latticeArray[secondPos.x, secondPos.y, secondPos.z] = !second; return true; } else if ((surrAfter == surrBefore) & latticeDistribution(ref rr)) //TEMPORARY { A.latticeArray[firstPos.x, firstPos.y, firstPos.z] = !first; //TEMPORARY A.latticeArray[secondPos.x, secondPos.y, secondPos.z] = !second; //TEMPORARY return true; } else return false; } //FIX SWITCH PROBABILITIES

In this, the lattice class is supposed to represent the "array" with its properties incorporated. Example solution code would be very thanksful due to my inxperience with the c# methods.

最满意答案

如果您的操作范围限定为不相交的元素范围(如1-10,25-40,100-123),则可以不对各个元素并行运行,而是在不同的范围上运行操作。 如果在操作正在进行时不重新分配阵列,则不需要任何其他同步。

如果操作更改随机元素,则必须处理正确的同步,并且可能无法获得在多个线程上运行代码的任何好处。

If your operations are scoped to non-intersecting ranges of elements (like 1-10, 25-40, 100-123) that you can instead of running in parallel for individual elements run operations on separate ranges. If you don't re-allocate array while operations are in progress you will not need any other synchronization.

If you operations change random elements you'd have to work on proper synchronization and may not be able to get any benefits of running code on multiple threads.

更多推荐

本文发布于:2023-08-07 12:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多线程   组中   位置   操作   在同一个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!