Page 1 of 1

Parallel For

PostPosted: Mon Feb 01, 2010 3:56 pm
by torial
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

Re: Parallel For

PostPosted: Tue Feb 02, 2010 5:50 am
by Charles
Looks interesting. At the very least it could be added to our existing set of threading examples.

Re: Parallel For

PostPosted: Tue Feb 02, 2010 8:16 pm
by hopscc
Heres a translation of the article code to cobra
"""
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?

Re: Parallel For

PostPosted: Wed Feb 03, 2010 12:15 am
by Charles
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.

Re: Parallel For

PostPosted: Wed Feb 03, 2010 4:18 am
by hopscc
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 ?

Re: Parallel For

PostPosted: Wed Feb 03, 2010 4:53 am
by hopscc
For the above code a workaround using sharp'...' looks like this:
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])'

Re: Parallel For

PostPosted: Thu Feb 04, 2010 11:49 pm
by Charles
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.

Re: Parallel For

PostPosted: Sun Feb 14, 2010 4:02 pm
by hopscc
Patch available for this on the ticket (ticket:196)