跨多个核心将2个大整数(System.Numerics)相乘 [英] Multiply 2 big integers (System.Numerics) across several cores

查看:87
本文介绍了跨多个核心将2个大整数(System.Numerics)相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我需要一个占用BigInt1和BigInt2然后返回乘法BigInt的例程。



我试过把最大的BigInt除以较小的BigInt创建一个循环运行在多个线程上的例程添加到BigInt总计虽然它起作用,但它比在一个线程上执行BigInt.Multiply要慢得多! xD。



我的原始循环在每个线程内部看起来像下面一样,尽管100%我的CPU它太慢了。

Basically i need a routine that takes BigInt1 and BigInt2 then returns the multiplied BigInt.

I have tried just dividing the biggest BigInt by the smaller one creating a looping routine that runs across several threads adding to a BigInt total although it worked it was (much) slower than just executing BigInt.Multiply on one thread! xD.

my original loop looked something like below inside each thread and despite 100% my CPU it was far too slow.

      private void MultiplicationThread()
       {
           int MyThreadID = GetNextThreadID();
           if (PartsOfBig[MyThreadID] > 0)
           {
               BigInteger NextBlock = new BigInteger();
               NextBlock = BigInteger.Multiply(PartsOfBig[MyThreadID],TheSmall[MyThreadID]);
               lock (TheBigLock)
               {
                   TheBig = BigInteger.Add(TheBig, NextBlock);
               }
           }
       }
}

推荐答案

引用:

它的工作速度比在一个线程上执行BigInt.Multiply要快得多

it worked it was (much) slower than just executing BigInt.Multiply on one thread

是的,这就是我所期望的一件事。当您在不同的核心中执行操作时,需要检查并更新各个核心之间的操作的每个步骤的多个值,这会严重降低该过程的速度。通过以错误的方式进行多线程处理,应用程序可能会非常慢。

CP上有一些文章非常详细地解释了背景。

Yes, that's what I do expect for such a case. When you do the operation in different cores, several values need to be checked and updated for each step of the operation between the individual cores, and that slows the process down severely. By doing multi-threading the wrong way, an application can be slowed down extremely.
There are some articles here on CP explaining the background in great detail.


我认为你不会找到解决方案。乘法并不是一个真正的并行问题。它通常是原子的。



此外,它看起来好像你正在追踪结转或将它们添加回适当的地方所以你回答的是会出错。
I don't think you're going to find a solution to this. Multiplication is not really a parallel problem. It's normally atomic.

Also, it doesn't appear as though you're tracking carry-overs or adding them back into the appropriate places so you're answer is going to come out wrong.


这篇关于跨多个核心将2个大整数(System.Numerics)相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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