如何比较使用parallel的两个数组? [英] How can I compare two array using parallel for?

查看:70
本文介绍了如何比较使用parallel的两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想比较两个数组并找出它们之间的区别。

但比较中有一个错误,因为当我使用parallel作为索引时数组不会按顺序增加1,2,3,...



请帮助我。



我尝试过:



Parallel.For(1,28,Sub(jjj)

如果ImgRecord.chromosome(jjj)= True那么

mstr = CStr(Form1.inp_img_measures(jjj))

d1 = CDbl(mstr)

d2 = CDbl(Form1.MeasuresArray(i,jjj))



AMCGH = AMCGH + Abs((d1 - d2))



结束如果

结束子)

解决方案

引用:

但是比较中有一个错误,因为当我使用并行索引o时f数组不会按顺序增加1,2,3,...



并行顺序是相互排斥的条件。

你可以拥有其中一个,但不能同时拥有两个。



您必须重新考虑您的算法,以便在 parallel 部分中不需要顺序条件。


你真的需要阅读关于Parallel.For的文档。



它通过将序列分成块并分配每个块来工作。问题空间到另一个线程。



例如,你有一个Parallel.For(1,100,...)。序列可分为1-25,26-50,51-75,76-100。每个线程都可以独立于其他线程工作。没有线程知道其他人正在做什么,并且他们之间没有同步。



问题正如你所描述的那样,不适合并行化。由于您需要按顺序运行整个序列,因此您必须在正在运行的Sub中内置同步代码。这将使您的代码基本上是单线程的,完全绕过Parallel的整个概念。


Hi,
I want to compare two arrays and find the difference between them.
But there is an error in comparision because when I use parallel for the index of the array does not increase in a sequence 1,2,3,...

Please, help me.

What I have tried:

Parallel.For(1, 28, Sub(jjj)
If ImgRecord.chromosome(jjj) = True Then
mstr = CStr(Form1.inp_img_measures(jjj))
d1 = CDbl(mstr)
d2 = CDbl(Form1.MeasuresArray(i, jjj))

AMCGH = AMCGH + Abs((d1 - d2))

End If
End Sub)

解决方案

Quote:

But there is an error in comparision because when I use parallel for the index of the array does not increase in a sequence 1,2,3,...


Parallel and in sequence are mutually exclusive conditions.
You can have one of them, but not both.

You have to rethink your algorithm so that you don't need in sequence condition in the parallel part.


You really need to read the documentation on Parallel.For.

It works by partitioning the sequence into chunks and assigning each chunk of the problem space to a different thread.

For example, you have a Parallel.For(1, 100, ...). The sequence may get divided into 1-25, 26-50, 51-75, 76-100. Each thread gets to work on its own chunk, separately from the others. No thread knows about what the others are doing and there is no synchronization between them.

The problem, as you describe it, does not lend itself to being parallelized. Since you need the entire sequence to run, well, sequentially, you would have to have synchronization code built into the Sub you're running. This would make your code essentially single-threaded, completely bypassing the entire concept of Parallel.


这篇关于如何比较使用parallel的两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆