I was thinking a default implementation of this (perhaps in CobraCore) , might be a nice buzz line
http://coding-time.blogspot.com/2008/03 ... -in-c.html
Forums
Parallel For
8 posts
• Page 1 of 1
Re: Parallel For
Looks interesting. At the very least it could be added to our existing set of threading examples.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Parallel For
Heres a translation of the article code to cobra
The only problem is it fails to compile
I think I've got a correct translation ( but I could be wrong about that)
skim reading delegates seems to indicate .beginInvoke and .endInvoke are added by the Compiler/CLR.
Is it possible/likely that cobra is not aware of that?
"""
ParallelFor in cobra from <!-- m --><a class="postlink" href="http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html">http://coding-time.blogspot.com/2008/03 ... -in-c.html</a><!-- m -->
"""
use System.Threading
sig ForDelegate(i as int)
sig ThreadDelegate
class Parallel
"""
Parallel for loop. Invokes given action, passing arguments
fromInclusive - toExclusive on multiple threads.
Returns when loop finished.
"""
def for(fromInclusive as int, toExclusive as int, action as ForDelegate) is shared
"""
ChunkSize = 1 makes items to be processed in order.
Bigger chunk size should reduce lock waiting time and thus increase paralelism.
"""
chunkSize = 4 # number of process() threads
threadCount = Environment.processorCount
cnt = fromInclusive - chunkSize
# processing function
# takes next chunk and processes it using action
process as ThreadDelegate = do
while true
cntMem = 0
lock Parallel.getType
# take next chunk
cnt += chunkSize
cntMem = cnt
# process chunk
# here items can come out of order if chunkSize > 1
for i in cntMem : cntMem + chunkSize
if i >= toExclusive, break
action(i)
# launch process() threads
asyncResults = IAsyncResult[](threadCount)
for i in 0 : threadCount
asyncResults[i] = process.beginInvoke(nil, nil) # line 45
# wait for all threads to complete
for i in 0 : threadCount
process.endInvoke(asyncResults[i]) # line 49
def main is shared
Parallel.for(0, 100, do(i as int))
# Your parallel code
Thread.sleep(100)
Console.writeLine(i)
The only problem is it fails to compile
- Code: Select all
parallelFor.cobra(45): error: Cannot find a definition for "beginInvoke" in "process" whose type is "ThreadDelegate". There is a member named ".invoke" with a similar name.
parallelFor.cobra(49): error: Cannot find a definition for "endInvoke" in "process" whose type is "ThreadDelegate". There is a member named ".invoke" with a similar name.
Compilation failed - 2 errors, 0 warnings
I think I've got a correct translation ( but I could be wrong about that)
skim reading delegates seems to indicate .beginInvoke and .endInvoke are added by the Compiler/CLR.
Is it possible/likely that cobra is not aware of that?
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Parallel For
Looks like Cobra is not picking up on those methods for delegates. You can fill out a ticket (and even fix if you like). In the meantime you may be able to use sharp'blah blah' to workaround the problem.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Parallel For
Reported on ticket:196.
Are there any existing handlers/examples in the cobra compiler (method discovery) where it currently has to
add/insert methods that aren't discoverable by inspecting the assemblies ?
Are there any existing handlers/examples in the cobra compiler (method discovery) where it currently has to
add/insert methods that aren't discoverable by inspecting the assemblies ?
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Parallel For
For the above code a workaround using sharp'...' looks like this:
line 45
line 49
line 45
#asyncResults[i] = process.beginInvoke(nil, nil)
asyncResults[i] = sharp'process.BeginInvoke(null, null)'
line 49
#process.endInvoke(asyncResults[i])
sharp'process.EndInvoke(asyncResults[i])'
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Parallel For
hopscc wrote:Reported on ticket:196.
Are there any existing handlers/examples in the cobra compiler (method discovery) where it currently has to
add/insert methods that aren't discoverable by inspecting the assemblies ?
Not that come to mind. But Cobra often models delegates with a special internal type called MethodSig and that could be the source of the problem.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Parallel For
Patch available for this on the ticket (ticket:196)
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
8 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 98 guests