none
Need Help With Sample Program in Visual C# Step By Step 2010

    Question

  • I have no idea where to get help for this. I am trying to run a C# program that came on an accompaning CD. It is the first sample of Chapter 27 which begins the "Introducing the Task Parallel Library. This first sample does NOT contain any "Tasks". It's just a benchmark to get you started so you can compare results later when you change it to run in parallel.

    I am getting an "OutofMemory" error which the author says could happen if there isn't enough memory. (It builds a complex graphic and puts it on the screen.) I have 2 gigs RAM and really shouldn't be running out but I have tried what he says to do in case we get the error and it has not solved the problem. I sent an email to "book support" but haven't heard anything back yet. I have no idea how to fix this because building the graph is complex and I wouldn't no where to begin.

    Any suggestions would be appreciated. Thanks.

     

    Tuesday, May 18, 2010 7:09 PM

Answers

  • I downloaded the sample code, loaded the Chapter 27\GraphDemo into VC#2010 express and ran it without problems.  The graph was plotted in 3319 ms.

    Edit:  GraphDemo Using the Parallel Class - Complete took 660 ms.

    • Marked as answer by haydnguy Wednesday, May 19, 2010 1:00 PM
    Tuesday, May 18, 2010 8:21 PM

All replies

  • Hi,

    Is your code complex or we can see that code here?! I guess that the code allocates some resources in a loop but doesn't free them gracefully!

    Thank you.

    [ALSO YOU CAN TRY YOUR POST AT http://social.msdn.microsoft.com/Forums/en/parallelextensions/threads (Parallel Extensions to the .NET Framework) TOO TO GETTING MORE ANSWERS]


    WHAT'S NEW IN THE .NET FRAMEWORK 4:
    Article: Comparison of parallel and sequential computing in .NET Framework
    Tuesday, May 18, 2010 7:35 PM
  • I downloaded the sample code, loaded the Chapter 27\GraphDemo into VC#2010 express and ran it without problems.  The graph was plotted in 3319 ms.

    Edit:  GraphDemo Using the Parallel Class - Complete took 660 ms.

    • Marked as answer by haydnguy Wednesday, May 19, 2010 1:00 PM
    Tuesday, May 18, 2010 8:21 PM
  • Here is the code: notice my comments about the divisors.

     

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Diagnostics;
    
    namespace GraphDemo
    {
      public partial class GraphWindow : Window
      {
        private static ulong availableMemorySize = 0;
        private int pixelWidth = 0;
        private int pixelHeight = 0;
        private double dpiX = 96.0;
        private double dpiY = 96.0;
        private WriteableBitmap graphBitmap = null;
     
        public GraphWindow()
        {
          InitializeComponent();
    
          PerformanceCounter memCounter = new PerformanceCounter("Memory", "Available Bytes");
          availableMemorySize = Convert.ToUInt64(memCounter.NextValue());
    
          this.pixelWidth = (int)availableMemorySize / 20000;  // instructions were for Out of Memory exceptions to increase the 20000 and 40000 divisors
          if (this.pixelWidth < 0 || this.pixelWidth > 15000)
            this.pixelWidth = 15000;
    
          this.pixelHeight = (int)availableMemorySize / 40000; // 40000 divisor.
          if (this.pixelHeight < 0 || this.pixelHeight > 7500)
            this.pixelHeight = 7500;
        }
    
        private void plotButton_Click(object sender, RoutedEventArgs e)
        {
          
         
            if (graphBitmap == null)
            {
              graphBitmap = new WriteableBitmap(pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Gray8, null);
            }
            int bytesPerPixel = (graphBitmap.Format.BitsPerPixel + 7) / 8;
            int stride = bytesPerPixel * pixelWidth;
            int dataSize = stride * pixelHeight;
            byte[] data = new byte[dataSize];
    
            Stopwatch watch = Stopwatch.StartNew();
            generateGraphData(data);
    
            duration.Content = string.Format("Duration (ms): {0}", watch.ElapsedMilliseconds);
            graphBitmap.WritePixels(new Int32Rect(0, 0, pixelWidth, pixelHeight), data, stride, 0);
            graphImage.Source = graphBitmap;
          
          
            
        }
    
        private void generateGraphData(byte[] data)
        {
          
        
            int a = pixelWidth / 2;
            int b = a * a;
            int c = pixelHeight / 2;
    
            for (int x = 0; x < a; x++)
            {
              int s = x * x;
              double p = Math.Sqrt(b - s);
              for (double i = -p; i < p; i += 3)
              {
                double r = Math.Sqrt(s + i * i) / a;
                double q = (r - 1) * Math.Sin(24 * r);
                double y = i / 3 + (q * c);
                plotXY(data, (int)(-x + (pixelWidth / 2)), (int)(y + (pixelHeight / 2)));
                plotXY(data, (int)(x + (pixelWidth / 2)), (int)(y + (pixelHeight / 2)));
              }
            }
                
        }
    
        private void plotXY(byte[] data, int x, int y)
        {
          data[x + y * pixelWidth] = 0xFF;
        }
      }
    }
    
    Tuesday, May 18, 2010 8:22 PM
  • Yasser and John,

    For some reason, I came in this morning and ran the program successfully the first time. I had disabled some things on my computer yesterday so maybe that had something to do with it but in any case the free memory shows it's way up from yesterday. Without the parallel library it took 1123 ms. (I got a screenshot so I could show it worked!).

    I will post future questions to the Parallel Extensions thread since I will be using it from now on.

    Thanks again!!

     

    Wednesday, May 19, 2010 12:59 PM