Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
January 30, 2013 Looking for command for synchronization of threads | ||||
---|---|---|---|---|
| ||||
Background: I am implementing an iterative algorithm in parallel manner. The algorithm iteratively updates a matrix (2D grid) of data. So, I will "divide" the grid to different threads, which will work on it for single iteration. After each iteration, all threads should wait since next iteration depends on previous iteration. My issue: To achieve synchronization, I am looking for an equivalent of sync in Cilk or cudaEventSynchronize in CUDA. I saw "synchronized", but was not sure, if that is the answer. Please help me. I will put that command at end of "for" loop and it will be executed once per iteration. |
January 31, 2013 Re: Looking for command for synchronization of threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sparsh Mittal | On Jan 30, 2013, at 2:58 PM, Sparsh Mittal <sparsh0mittal@gmail.com> wrote:
> Background:
> I am implementing an iterative algorithm in parallel manner. The algorithm iteratively updates a matrix (2D grid) of data. So, I will "divide" the grid to different threads, which will work on it for single iteration. After each iteration, all threads should wait since next iteration depends on previous iteration.
>
> My issue:
> To achieve synchronization, I am looking for an equivalent of sync in Cilk or cudaEventSynchronize in CUDA. I saw "synchronized", but was not sure, if that is the answer. Please help me. I will put that command at end of "for" loop and it will be executed once per iteration.
I suggest looking at std.parallelism since it's designed for this kind of thing. That aside, all traditional synchronization methods are in core.sync. The equivalent of "sync" in Cylk would be core.sync.barrier.
|
January 31, 2013 Re: Looking for command for synchronization of threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | > > I suggest looking at std.parallelism since it's designed for this kind of thing. That aside, all traditional synchronization methods are in core.sync. The equivalent of "sync" in Cylk would be core.sync.barrier. Thanks. I wrote this: #!/usr/bin/env rdmd import std.stdio; import std.concurrency; import std.algorithm; import core.sync.barrier; import core.thread; void sorter(Tid owner, shared(int)[] sliceToSort, int mynumber) { writefln("Came inside %s", mynumber); sort(sliceToSort); writefln("Going out of %s", mynumber); } void main() { shared numbers = [ 6, 5, 4, 3, 2, 1 ]; auto barrier = new Barrier(2); spawn(&sorter, thisTid, numbers[0 .. $ / 2], 0); spawn(&sorter, thisTid, numbers[$ / 2 .. $],1 ); writefln("Waiting for barrier in main"); barrier.wait(); writeln(numbers); } It compiles but barrier does not get released. Can you please point out the fault. Pardon my mistake. I searched whole web, there are almost no examples of it online. I saw this: http://www.digitalmars.com/d/archives/digitalmars/D/bugs/Issue_9005_New_std.concurrency.spawn_should_allow_void_delegate_Args_shared_for_new_Tid_44426.html but it does not compile. |
January 31, 2013 Re: Looking for command for synchronization of threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sparsh Mittal | On Wednesday, 30 January 2013 at 22:58:36 UTC, Sparsh Mittal wrote: > > Background: > I am implementing an iterative algorithm in parallel manner. The algorithm iteratively updates a matrix (2D grid) of data. So, I will "divide" the grid to different threads, which will work on it for single iteration. After each iteration, all threads should wait since next iteration depends on previous iteration. > > My issue: > To achieve synchronization, I am looking for an equivalent of sync in Cilk or cudaEventSynchronize in CUDA. I saw "synchronized", but was not sure, if that is the answer. Please help me. I will put that command at end of "for" loop and it will be executed once per iteration. You could look at this Rosetta Code snippet, which does something similiar for parallelism. http://rosettacode.org/wiki/Checkpoint_synchronization#D |
January 31, 2013 Re: Looking for command for synchronization of threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sparsh Mittal | On 2013-01-31 03:29, Sparsh Mittal wrote:
> Thanks. I wrote this: [...]
> It compiles but barrier does not get released. Can you please point out the
> fault. Pardon my mistake. I searched whole web, there are almost no examples of
> it online.
Barrier doesn't release because you've only called wait() from one thread, while initializing the Barrier to use two threads. The following code works for me. Notice that the Barrier and wait() are used in 3 threads. But better have this verified by someone who had done more threading in D. :)
#!/usr/bin/env rdmd
import std.stdio;
import std.concurrency;
import std.algorithm;
import core.sync.barrier;
import core.thread;
__gshared Barrier barrier = null;
void sorter(Tid owner, shared(int)[] sliceToSort, int mynumber)
{
writefln("Came inside %s", mynumber);
sort(sliceToSort);
writefln("Going out of %s", mynumber);
barrier.wait();
}
void main()
{
shared numbers = [ 6, 5, 4, 3, 2, 1 ];
barrier = new Barrier(3);
spawn(&sorter, thisTid, numbers[0 .. $ / 2], 0);
spawn(&sorter, thisTid, numbers[$ / 2 .. $], 1);
writefln("Waiting for barrier in main");
barrier.wait();
writefln("All done");
writeln(numbers);
}
|
Copyright © 1999-2021 by the D Language Foundation