【问题标题】:Call functions with output parameters in Parallel.For在 Parallel.For 中调用带有输出参数的函数
【发布时间】:2023-03-08 07:54:01
【问题描述】:

如何使用输出参数修改方法#2?方法 #1 和方法 #2 具有相同的功能,但给出不同的结果。我不知道为什么。可能是带有输出参数的调用函数或 Parallel.For 中的 Mpir.NET 类型或 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;
        }
    }
}

下面的代码与元组。同样的问题:(

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);;
        }
    }
}

【问题讨论】:

    标签: c# parameters output parallel.for


    【解决方案1】:

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

    【讨论】:

    • 但我想排除重复项。 ConcurrentDictionary 可以吗?
    • @YuriyTigiev - 你需要检查包含的键值而不是不插入
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2018-08-04
    • 2017-08-15
    • 1970-01-01
    • 2017-02-17
    相关资源
    最近更新 更多