使用Parallel.For中的输出参数调用函数

时间:2015-03-05 07:51:00

标签: c# parameters output parallel.for

如何使用输出参数修改方法#2的功能?方法#1&方法#2具有相同的功能,但会产生不同的结果。我不知道为什么。调用函数与输出参数或Mpir.NET类型或Parallel.For。

中的HashSet可能有问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Mpir.NET;

namespace ConsoleApplication4
{
    class Program
    {
        public struct numbers
        {
            public numbers(mpz_t p, mpz_t q)
            {
                this.q = q;
                this.p = p;
            }
            mpz_t q;
            mpz_t p;
        };

        static void Main(string[] args)
        {
            Int32 arraySize = 100;
            HashSet<numbers> pairs = new HashSet<numbers>();
            HashSet<numbers> unique = new HashSet<numbers>();

            mpz_t[] numbersArray = new mpz_t[arraySize];

            for (Int32 i = 0; i < arraySize; i++)
            {
                numbersArray[i] = i*i+i+1; 
            }

            //Methode #1
            for (Int32 j = 0; j < arraySize; j++)
            { 
                mpz_t p, q;

                for (Int32 m = 0; m < 16; m++)
                {
                    if (checkDivisible(numbersArray[j]*m, out p, out q))
                        pairs.Add(new numbers(p, q));

                    unique.Add(new numbers(p, q));
                }



            }
            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());

            pairs = new HashSet<numbers>();
            unique = new HashSet<numbers>();
            //Methode #2
            Parallel.For(0, arraySize, j => 
            {
                mpz_t p, q;

                for (Int32 m = 0; m < 16; m++)
                {
                    if (checkDivisible(numbersArray[j]*m, out p, out q))
                        pairs.Add(new numbers(p, q));

                    unique.Add(new numbers(p, q));
                }


            }
            );

            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());


            Console.ReadKey();
        }

        private static Boolean checkDivisible(mpz_t n, out mpz_t p, out mpz_t q)
        {
            p = 1; 
            q = 1;    

            for (Int32 i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    q = i;
                    p = n / i;
                    return true;
                }
            }

            return false;
        }
    }
}

下面是Tuple的代码。同样的问题:(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Mpir.NET;

namespace ConsoleApplication4
{
    class Program
    {
        public struct numbers
        {
            public numbers(mpz_t p, mpz_t q)
            {
                this.q = q;
                this.p = p;
            }
            mpz_t q;
            mpz_t p;
        };

        static void Main(string[] args)
        {
            Int32 arraySize = 100;
            HashSet<numbers> pairs = new HashSet<numbers>();
            HashSet<numbers> unique = new HashSet<numbers>();

            mpz_t[] numbersArray = new mpz_t[arraySize];

            for (Int32 i = 0; i < arraySize; i++)
            {
                numbersArray[i] = i*i+i+1; 
            }

            //Methode #1
            for (Int32 j = 0; j < arraySize; j++)
            { 
                mpz_t p = 1, q = 1;

                for (Int32 m = 0; m < 16; m++)
                {
                    Tuple<Boolean, mpz_t, mpz_t> t = checkDivisible(numbersArray[j] * m);
                    if (t.Item1 == true)
                        pairs.Add(new numbers(t.Item2, t.Item3));

                    unique.Add(new numbers(t.Item2, t.Item3));
                }



            }
            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());

            pairs = new HashSet<numbers>();
            unique = new HashSet<numbers>();
            //Methode #2
            Parallel.For(0, arraySize, j => 
            {
                mpz_t p = 1, q = 1;

                for (Int32 m = 0; m < 16; m++)
                {
                    Tuple<Boolean, mpz_t, mpz_t> t = checkDivisible(numbersArray[j] * m);
                    if (t.Item1 == true)
                        pairs.Add(new numbers(t.Item2, t.Item3));

                    unique.Add(new numbers(t.Item2, t.Item3));
                }


            }
            );

            Console.WriteLine("Count pairs {0}\t{1}", pairs.Count(), unique.Count());


            Console.ReadKey();
        }

        private static Tuple<Boolean, mpz_t, mpz_t> checkDivisible(mpz_t n)
        {
            mpz_t p = 1; 
            mpz_t q = 1;    

            for (Int32 i = 2; i < n; i++)
            {
                if (n % i == 0)
                {
                    q = i;
                    p = n / i;
                    return new Tuple<Boolean, mpz_t, mpz_t>(true, p, q);
                }
            }

            return new Tuple<Boolean, mpz_t, mpz_t>(false, p, q);;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

为了解决Hashtable的并行问题lopp使用ConcurrentDictionary作为线程部分。