Results 1 to 6 of 6

Thread: [RESOLVED] Parallel For loops Framework 4

  1. #1

    Thread Starter
    Addicted Member cellus205's Avatar
    Join Date
    Dec 2010
    Location
    San Antonio, TX
    Posts
    237

    Resolved [RESOLVED] Parallel For loops Framework 4

    Hey guys,

    Im trying to learn how to use a Parallel.For loop. Ive got a big for loop, that i need to run parallel, and am trying to use a Parallel.For loop for the first time. When I try using this code, I get that it cant be called using these arguments. The old way to write this would be:
    Code:
    For m=1 to rowend
      'extra cpde
    Next
    How Im trying to do it now:
    Code:
    Dim m as Integer = 0
    Parallel.For(1, rowend, Sub(m)
        For ...
     ......
    .....
       Next
    Next
    End Sub)
    I dont need to return anything to the for loop, Im just trying to iterate m, running in parallel. Where am I going wrong?

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Parallel For loops Framework 4

    I've never used the parallel for loop, but it seems to me that you would now have a nested loop. I haven't looked it up but I assume the Parallel.For method simply runs the inline method (I always forget what that's called, lambda method?) as many times as described by the first two parameters. Now you are doing another loop in that loop.

    In any case, you haven't mentioned what is wrong?

  3. #3

    Thread Starter
    Addicted Member cellus205's Avatar
    Join Date
    Dec 2010
    Location
    San Antonio, TX
    Posts
    237

    Re: Parallel For loops Framework 4

    Well, its saying that the for loop cant be called using these parameters. I was trying to follow the example code from msdn here, but I guess I dont understand the correct syntax.

    http://msdn.microsoft.com/en-us/library/dd460713.aspx
    Code:
    #Region "Parallel_Loop"
    
        Private Sub MultiplyMatricesParallel(ByVal matA As Double(,), ByVal matB As Double(,), ByVal result As Double(,))
            Dim matACols As Integer = matA.GetLength(1)
            Dim matBCols As Integer = matB.GetLength(1)
            Dim matARows As Integer = matA.GetLength(0)
    
            ' A basic matrix multiplication.
            ' Parallelize the outer loop to partition the source array by rows.
            Parallel.For(0, matARows, Sub(i)
                                          For j As Integer = 0 To matBCols - 1
                                              ' Use a temporary to improve parallel performance.
                                              Dim temp As Double = 0
                                              For k As Integer = 0 To matACols - 1
                                                  temp += matA(i, k) * matB(k, j)
                                              Next
                                              result(i, j) += temp
                                          Next
                                      End Sub)
        End Sub
    #End Region

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Parallel For loops Framework 4

    The example is using nested loops because that would be what it was doing before parallelisation. It had an outer loop that iterated over the rows and an inner loop that iterated over the columns. Only the outer loop was parallelised.
    It doesn't sound like you need nested loops in the non-parallel situation,. so you shouldn't be introducing them in your parallel code. Rip out the inner For loop and go from there.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Parallel For loops Framework 4

    It should look like this:
    Code:
    Dim m as Integer = 0
    Parallel.For(1, rowend, Sub(idx) 
        'Do your code here for a single iteration
    End Sub)
    Note the change in the sub decl.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member cellus205's Avatar
    Join Date
    Dec 2010
    Location
    San Antonio, TX
    Posts
    237

    Re: Parallel For loops Framework 4

    Thanks a lot, thats exactly what I needed

Posting Permissions

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



Click Here to Expand Forum to Full Width