|
-
Jan 11th, 2011, 04:40 PM
#1
Thread Starter
Addicted Member
[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?
-
Jan 11th, 2011, 04:53 PM
#2
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?
-
Jan 11th, 2011, 04:57 PM
#3
Thread Starter
Addicted Member
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
-
Jan 11th, 2011, 05:33 PM
#4
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.
-
Jan 11th, 2011, 06:47 PM
#5
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
-
Jan 12th, 2011, 09:36 AM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|