Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Parallel Programming in C#

  1. #11
    Join Date
    Sep 2004
    Posts
    1,735
    Rep Power
    0

    Default

    PrimeHelper.cs
    PrimeHelper.cs
    ......................

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Storage;
    
    namespace ThreadFeeder
    {
        class PrimeHelper
        {
    
            public static bool IsPrime(ushort number)
            {
    
          
    
                return true;
    
            }
    
    
    
            public static uint CountAllPrimesBetweenTwoNums(ushort n1, ushort n2)
            {
                ushort newN1 = Math.Min(n1, n2);
                ushort newN2 = Math.Max(n1, n2);
    
                uint count = 0;
                for (ushort i = newN1; i <= newN2; i++)
                {
                    if (IsPrime(i))
                    {
                        count++;
                    }
                }
                return count;
            }
    
    
        }
    
    }
    Last edited by crosswire; Jul 24, 2017 at 12:53 AM. Reason: code still not uploading. prime code not uploading
    Music, Games, Dancing enhance the Fun Algorithm. The Fun Algorithm can be used to hack the Caring Algorithm. Children should have fun, and watch Sofia the First. Receiving Care develops care. Having fun makes it easier to give care.

  2. #12
    Join Date
    Sep 2004
    Posts
    1,735
    Rep Power
    0

    Default

    MainPage.xaml.cs
    MainPage.xaml.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Popups;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace ThreadFeeder
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            WorkLoad wL = new WorkLoad();
    
            public MainPage()
            {
                this.InitializeComponent();
    
                txtBox.Text = wL.LevelOfParallelism.ToString();
    
                wL.Initialize();
    
                txtStatus.Text = "Initialized with " + WorkLoad.size.ToString() + " data elements";
       
            }
    
            private void ButReInitialize_Click(object sender, RoutedEventArgs e)
            {
                wL.Initialize();
     
                txtStatus.Text = "Re-Initialized with another random set of " + WorkLoad.size.ToString() + " data elements";
            }
    
            private void ButMethod1_Click(object sender, RoutedEventArgs e)
            {
                DateTime t1, t2;
                t1 = DateTime.Now;
                wL.Method1();
                t2 = DateTime.Now;
    
                // MessageDialog msgDialog = new MessageDialog("It took", t2.Subtract(t1).ToString());
                // msgDialog.ShowAsync();
    
                txtStatus.Text = "It took " + t2.Subtract(t1).ToString();
                txtTimeMethod1.Text = t2.Subtract(t1).ToString();
            }
    
            private void ButMethod2_Click(object sender, RoutedEventArgs e)
            {
                DateTime t1, t2;
                t1 = DateTime.Now;
                wL.Method2();
                t2 = DateTime.Now;
    
                // MessageDialog msgDialog = new MessageDialog("It took", t2.Subtract(t1).ToString());
                // msgDialog.ShowAsync();
    
                txtStatus.Text = "It took " + t2.Subtract(t1).ToString() + "\r\n" + wL.LevelOfParallelism.ToString() + " degree of parallelism";
                txtTimeMethod2.Text += t2.Subtract(t1).ToString() + "@" + wL.LevelOfParallelism.ToString() + "\r\n";
            }
    
            private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                int.TryParse(txtBox.Text, out wL.LevelOfParallelism);
            }
        }
    }
    Last edited by crosswire; Jul 24, 2017 at 12:09 AM. Reason: Page was not posting code
    Music, Games, Dancing enhance the Fun Algorithm. The Fun Algorithm can be used to hack the Caring Algorithm. Children should have fun, and watch Sofia the First. Receiving Care develops care. Having fun makes it easier to give care.

  3. #13
    Join Date
    Sep 2004
    Posts
    1,735
    Rep Power
    0

    Default

    MainPage.xaml
    MainPage.xaml

    Code:
    <Page
        x:Class="ThreadFeeder.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:ThreadFeeder"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Button Content="Re-Initialize" HorizontalAlignment="Left" Margin="224,128,0,0" VerticalAlignment="Top" Click="ButReInitialize_Click" Width="115"/>
            <Button Content="Method 1" HorizontalAlignment="Left" Margin="224,192,0,0" VerticalAlignment="Top" Click="ButMethod1_Click" Width="115"/>
            <Button Content="Method 2" HorizontalAlignment="Left" Margin="224,253,0,0" VerticalAlignment="Top" Click="ButMethod2_Click" Width="115"/>
            <TextBox Name="txtBox" HorizontalAlignment="Left" Margin="144,256,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="39"  InputScope="Number"  TextChanged="TextBox_TextChanged"/>
            <TextBlock HorizontalAlignment="Left" Margin="71,207,0,0" TextWrapping="Wrap" Text="1 Thread" VerticalAlignment="Top"/>
            <TextBlock HorizontalAlignment="Left" Margin="71,268,0,0" TextWrapping="Wrap" Text="N Threads" VerticalAlignment="Top"/>
            <TextBlock Name="txtStatus" HorizontalAlignment="Left" Margin="64,362,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="101" RenderTransformOrigin="0.5,0.5" Width="641" FontSize="24"/>
            <TextBlock Name="txtTimeMethod1" HorizontalAlignment="Left" Margin="383,207,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
            <TextBlock Name="txtTimeMethod2" HorizontalAlignment="Left" Margin="383,268,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
    
        </Grid>
    </Page>
    Music, Games, Dancing enhance the Fun Algorithm. The Fun Algorithm can be used to hack the Caring Algorithm. Children should have fun, and watch Sofia the First. Receiving Care develops care. Having fun makes it easier to give care.

  4. #14
    Join Date
    Sep 2004
    Posts
    1,735
    Rep Power
    0

    Default

    OK

    I am getting to like the Parallel class in .Net

    Still need to figure out how to do the revised problem mentioned before (getting the max of the array).

    Well, I did test it on a quad core with hyper-thread.

    I transferred the code to a win form. Build the release version, and ran it on my personal laptop. I ran some test more than once to get more results.

    This is the results:

    Code:
    Method 1
    00:00:52.6917200
    00:00:49.9805704
    00:00:53.8498523
    00:00:49.7089230
    
    
    Method 2
    00:00:13.2342627@16
    00:00:12.6456456@8
    00:00:12.6827754@32
    00:00:12.3579132@64
    00:00:12.2175089@100
    00:00:49.7906013@1
    00:00:26.2037478@2
    00:00:13.5129853@4
    00:00:12.3398655@8
    00:00:48.7658084@1
    00:00:12.3363230@16
    00:00:12.6688846@32
    00:00:26.2846374@2
    00:00:13.4623731@4
    00:00:12.2460636@64
    
    Method 2 (Sorted)
    00:00:48.7658084@1
    00:00:49.7906013@1
    00:00:26.2037478@2
    00:00:26.2846374@2
    00:00:13.4623731@4
    00:00:13.5129853@4
    00:00:12.6456456@8
    00:00:12.3398655@8
    00:00:12.3363230@16
    00:00:13.2342627@16
    00:00:12.6827754@32
    00:00:12.3579132@64
    00:00:12.6688846@32
    00:00:12.2460636@64
    00:00:12.2175089@100
    This is the win form


    I noticed that this particular code did not scale well when moving from 4 to 8 threads. Hyper-thread is not as good as a physical core for this code.

    After the testing above, I went to look on the processor usage graph in task manager

    I noticed that for a single thread, you could see which logical core, the thread was running on as time progressed in its execution. The single thread was scheduled to different cores. Maybe it is the scheduler doing its load balancing. Cool. Bear in mind that the actual granularity
    of the time slices is in milli secs

    Music, Games, Dancing enhance the Fun Algorithm. The Fun Algorithm can be used to hack the Caring Algorithm. Children should have fun, and watch Sofia the First. Receiving Care develops care. Having fun makes it easier to give care.

  5. #15
    Join Date
    Sep 2004
    Posts
    1,735
    Rep Power
    0

    Default

    As the number of threads increases, the graphs for the CPU usage become more even. But no significant difference is seen going beyond the amount of logical cores.

    This can also be seen from the bench timings before.

    2 threads


    4 threads


    8 threads (smooth graph)


    64 threads (also smooth)




    The parallel class can be manipulated to the amount of threads needed. You can use it to run a complex "CPU side" functions. However you may want to code something that the class does not support, maybe, I am still trying to figure out how to use it to do the revised problem.
    Music, Games, Dancing enhance the Fun Algorithm. The Fun Algorithm can be used to hack the Caring Algorithm. Children should have fun, and watch Sofia the First. Receiving Care develops care. Having fun makes it easier to give care.

  6. #16
    Join Date
    Feb 2003
    Posts
    3,007
    Rep Power
    0

    Default

    So your test machine CPU only has 4 cores? maybe that is the 16 thread bottle neck?

  7. #17
    Join Date
    Sep 2004
    Posts
    1,735
    Rep Power
    0

    Default

    Yes.

    Based on the timings

    Method 2 (Sorted)
    00:00:48.7658084@1
    00:00:49.7906013@1
    00:00:26.2037478@2
    00:00:26.2846374@2
    00:00:13.4623731@4
    00:00:13.5129853@4
    00:00:12.6456456@8
    00:00:12.3398655@8
    00:00:12.3363230@16

    It bottlenecks at 8 threads, but even at 4 threads there seems to be a drop afterwards. Hyper-threading is not as effective in this type of code

    Most desktops/laptops I think are quad core or dual core in Jamaica. Hopefully Ryzen will change that.

    I want to change the algorithm from a problem that takes a small input to a problem that takes a large input, so that I can test the effect of the cache when the data that is being worked on is much larger than 3MB/6MB. Then test the impact of having 1000 threads as opposed to 8.

    I am still trying to see if I can use Parallel class to find the max in an array.
    Music, Games, Dancing enhance the Fun Algorithm. The Fun Algorithm can be used to hack the Caring Algorithm. Children should have fun, and watch Sofia the First. Receiving Care develops care. Having fun makes it easier to give care.

  8. #18
    Join Date
    Feb 2003
    Posts
    3,007
    Rep Power
    0

    Default

    Naw, most people are using old single core computers because they can't afford to buy a new computer every month.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •