Ask a questionAsk a question
 

AnswerScability Question

  • Wednesday, October 28, 2009 7:33 PMykwok Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am using a two Quad-core (8 cores in total) system and my program gives:

            1         2          4         6       8
            31.25   16.77   8.44    8.38    4.24

    I wonder why there is no improvement from CPU=4 to CPU=6.  Thank you.


    The program is:

    namespace Benchmark
    {
        using System;
        using System.Threading;
        using System.Threading.Tasks;

        public class Benchmark
        {
            private static double[] xgk = new double[17];
            private static double[] wgk = new double[17];
            private static double[] wg = new double[8];

            public static double Eval(double x)
            {
                double sum = 0.0;
                int i, j;

                for (i = 3; i < 100000; i++)
                {
                    for (j = 3; j < 100; j++)
                    {
                        sum += Math.Pow(i + 0.01 * j, x);
                    }
                }
                return sum;
            }


            public static double Run(int CPU)
            {
                int order = 15;
                double half, a;
                double res1, res2, res3, res4;
                double[] fv1 = new double[17];
                double[] fv2 = new double[17];

                half = a = 0.5;
                res1 = res2 = res3 = res4 = 0.0;

                ParallelOptions options = new ParallelOptions();
                options.MaxDegreeOfParallelism = CPU;
                Object mutex = new Object();

                long time = (DateTime.Now.Ticks - 621355968000000000) / 10000;
                Parallel.For(0, order / 2, options,
                    () =>
                    {
                        return (new double[3]);
                    }, (j, state, local) =>
                    {
                        double b = half * xgk[2 * j + 1];
                        double fval1 = Eval(a - b);
                        double fval2 = Eval(a + b);
                        fv1[2 * j + 1] = fval1;
                        fv2[2 * j + 1] = fval2;
                        double fsum = fval1 + fval2;
                        // local[0] = res1, local[1] = res2, local[2] = res3
                        local[0] += wg[j] * fsum;
                        local[1] += wgk[2 * j + 1] * fsum;
                        local[2] += wgk[2 * j + 1] * (System.Math.Abs(fval1) + System.Math.Abs(fval2));
                        return local;
                    },
                local =>
                {
                    lock (mutex)
                    {
                        res1 += local[0];
                        res2 += local[1];
                        res3 += local[2];
                    }
                });

                Parallel.For(0, (order + 1) / 2, options,
                    () =>
                    {
                        return (new double[2]);
                    }, (j, state, local) =>
                    {
                        double b = half * xgk[2 * j];
                        double fval1 = Eval(a - b);
                        double fval2 = Eval(a + b);
                        fv1[2 * j] = fval1;
                        fv2[2 * j] = fval2;
                        double fsum = fval1 + fval2;
                        // local[0] = res2, local[1] = res4
                        local[0] += wgk[2 * j] * fsum;
                        local[1] += wgk[2 * j] * (System.Math.Abs(fval1) + System.Math.Abs(fval2));
                        return local;
                    },
                local =>
                {
                    lock (mutex)
                    {
                        res2 += local[0];
                        res4 += local[1];
                    }
                });
                time = (DateTime.Now.Ticks - 621355968000000000) / 10000 - time;

                return ((double) time / 1000.0);
            }


            public static void Main(string[] args)
            {
                int[] cpu = { 1, 2, 4, 6, 8 };
                double[] time = new double[cpu.Length];
                int CPU;
                Random rand = new Random();

                // setup input.
                for (int i = 0; i < wgk.Length; i++)
                {
                    wgk[i] = rand.NextDouble();
                    wgk[i] = rand.NextDouble();
                }

                for (int i = 0; i < wg.Length; i++) wg[i] = rand.NextDouble();

                // Run
                for (int index = 0; index < cpu.Length; index++)
                {
                    CPU = cpu[index];
                    time[index] = Run(CPU);
                }

                Console.WriteLine("\t1\t2\t4\t6\t8");
                for (int j = 0; j < cpu.Length; j++) Console.Write("\t{0, 0:F2}", time[j]);
                Console.WriteLine();
            }
        }
    }

Answers

All Replies