Is it possible to parallelize a code with this structure?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Is it possible to parallelize a code with this structure?
n=0;
Do[If[condition, n++],100]
n
ParallelDo
always returns n=0
.
parallelization
add a comment |Â
up vote
2
down vote
favorite
Is it possible to parallelize a code with this structure?
n=0;
Do[If[condition, n++],100]
n
ParallelDo
always returns n=0
.
parallelization
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Is it possible to parallelize a code with this structure?
n=0;
Do[If[condition, n++],100]
n
ParallelDo
always returns n=0
.
parallelization
Is it possible to parallelize a code with this structure?
n=0;
Do[If[condition, n++],100]
n
ParallelDo
always returns n=0
.
parallelization
asked Sep 5 at 8:32
mattiav27
2,00011428
2,00011428
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n
.
SetSharedVariable
will let you pretend that n
is shared between the parallel kernels, but this is not what really happens. n
will instead be always evaluated in on the main kernel. This means that every time n
is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition
takes a very long time to evaluate).
What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine
does this quite directly, but it can be implemented with other parallel constructs as well.
To give a specific example, the following code counts how many times the condition RandomReal < 0.3
is satisfied out of 10000 trials.
Total@ParallelTable[
If[RandomReal < 0.3, 1, 0],
10000
]
Is something like this correct?ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
@mattiav27 I'm sorry to have to ask this, but have you looked at theParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
– Szabolcs
Sep 5 at 10:09
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use then
variable the way you were trying to. In this comment you are trying to use it the same way.
– Szabolcs
Sep 5 at 10:11
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n
.
SetSharedVariable
will let you pretend that n
is shared between the parallel kernels, but this is not what really happens. n
will instead be always evaluated in on the main kernel. This means that every time n
is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition
takes a very long time to evaluate).
What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine
does this quite directly, but it can be implemented with other parallel constructs as well.
To give a specific example, the following code counts how many times the condition RandomReal < 0.3
is satisfied out of 10000 trials.
Total@ParallelTable[
If[RandomReal < 0.3, 1, 0],
10000
]
Is something like this correct?ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
@mattiav27 I'm sorry to have to ask this, but have you looked at theParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
– Szabolcs
Sep 5 at 10:09
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use then
variable the way you were trying to. In this comment you are trying to use it the same way.
– Szabolcs
Sep 5 at 10:11
add a comment |Â
up vote
6
down vote
accepted
Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n
.
SetSharedVariable
will let you pretend that n
is shared between the parallel kernels, but this is not what really happens. n
will instead be always evaluated in on the main kernel. This means that every time n
is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition
takes a very long time to evaluate).
What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine
does this quite directly, but it can be implemented with other parallel constructs as well.
To give a specific example, the following code counts how many times the condition RandomReal < 0.3
is satisfied out of 10000 trials.
Total@ParallelTable[
If[RandomReal < 0.3, 1, 0],
10000
]
Is something like this correct?ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
@mattiav27 I'm sorry to have to ask this, but have you looked at theParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
– Szabolcs
Sep 5 at 10:09
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use then
variable the way you were trying to. In this comment you are trying to use it the same way.
– Szabolcs
Sep 5 at 10:11
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n
.
SetSharedVariable
will let you pretend that n
is shared between the parallel kernels, but this is not what really happens. n
will instead be always evaluated in on the main kernel. This means that every time n
is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition
takes a very long time to evaluate).
What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine
does this quite directly, but it can be implemented with other parallel constructs as well.
To give a specific example, the following code counts how many times the condition RandomReal < 0.3
is satisfied out of 10000 trials.
Total@ParallelTable[
If[RandomReal < 0.3, 1, 0],
10000
]
Mathematica implements distributed memory parallelism. Memory cannot be shared between parallel threads. Thus, strictly speaking, the answer is no. Each parallel thread will have a separate n
.
SetSharedVariable
will let you pretend that n
is shared between the parallel kernels, but this is not what really happens. n
will instead be always evaluated in on the main kernel. This means that every time n
is read or written, a callback to the main kernel is necessary. This will often have a very large performance impact, and is not typically a workable solution (unless your condition
takes a very long time to evaluate).
What one normally does is think not in terms of a specific algorithm, but in terms of the problem that needs to be solved. Try to come up with a solution that requires no communication between the parallel kernels. It seems to me that you want to count how many times of 100 is a condition satisfied. Well, break that 100 down into 4 groups, each having 25 iterations, and count separately in each group. The add up the counts at the end. ParallelCombine
does this quite directly, but it can be implemented with other parallel constructs as well.
To give a specific example, the following code counts how many times the condition RandomReal < 0.3
is satisfied out of 10000 trials.
Total@ParallelTable[
If[RandomReal < 0.3, 1, 0],
10000
]
edited Sep 5 at 10:03
answered Sep 5 at 8:56
Szabolcs
152k13415896
152k13415896
Is something like this correct?ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
@mattiav27 I'm sorry to have to ask this, but have you looked at theParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
– Szabolcs
Sep 5 at 10:09
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use then
variable the way you were trying to. In this comment you are trying to use it the same way.
– Szabolcs
Sep 5 at 10:11
add a comment |Â
Is something like this correct?ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
@mattiav27 I'm sorry to have to ask this, but have you looked at theParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.
– Szabolcs
Sep 5 at 10:09
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use then
variable the way you were trying to. In this comment you are trying to use it the same way.
– Szabolcs
Sep 5 at 10:11
Is something like this correct?
ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
Is something like this correct?
ParallelCombine[Do[If[condition, n++],100]]
– mattiav27
Sep 5 at 10:06
@mattiav27 I'm sorry to have to ask this, but have you looked at the
ParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.– Szabolcs
Sep 5 at 10:09
@mattiav27 I'm sorry to have to ask this, but have you looked at the
ParallelCombine
documentation? I don't understand how you came to the conclusion that this would be correct based on the documentation page.– Szabolcs
Sep 5 at 10:09
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the
n
variable the way you were trying to. In this comment you are trying to use it the same way.– Szabolcs
Sep 5 at 10:11
@mattiav27 Also, if you don't understand parts of my answer, that's fine. Just ask (be specific), and I'll try to clarify. But please do follow the links and do read through all of the answer. I tried to explain why it is not possible to use the
n
variable the way you were trying to. In this comment you are trying to use it the same way.– Szabolcs
Sep 5 at 10:11
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181256%2fis-it-possible-to-parallelize-a-code-with-this-structure%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password