Forums

Parallel For

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Parallel For

Postby torial » Mon Feb 01, 2010 3:56 pm

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
torial
 
Posts: 229
Location: IA

Re: Parallel For

Postby Charles » Tue Feb 02, 2010 5:50 am

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

Postby hopscc » Tue Feb 02, 2010 8:16 pm

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?
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Parallel For

Postby Charles » Wed Feb 03, 2010 12:15 am

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

Postby hopscc » Wed Feb 03, 2010 4:18 am

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 ?
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Parallel For

Postby hopscc » Wed Feb 03, 2010 4:53 am

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])'
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Parallel For

Postby Charles » Thu Feb 04, 2010 11:49 pm

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

Postby hopscc » Sun Feb 14, 2010 4:02 pm

Patch available for this on the ticket (ticket:196)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 88 guests

cron