2
$\begingroup$
ClearAll["Global`*"]

s[B_] := NMinimize[B*x^4 - 3 x^2 - x, x]

Table[s1[B] = s[B], {B, {1, 3, 10, 7}}];

s1[3]

(*{-1.49476, {x -> 0.779079}}*)

s1[10]

(*{-0.64752, {x -> 0.452979}}*)

ClearAll["Global`*"]

s[B_] := NMinimize[B*x^4 - 3 x^2 - x, x]

ParallelTable[s1[B] = s[B], {B, {1, 3, 10, 7}}];

s1[3]

(*s1[3]*)

s1[10]

(*s1[10]*)

Why, when using ParallelTable, it is not possible to assign s1[B] to the s[B], but in the case of Table it is possible?

How should it be implemented?

$\endgroup$
1
  • 1
    $\begingroup$ It seems a definition distribution problem. I'd assume the s1 definitions are made on the parallel kernels and not accessible to the main kernel. $\endgroup$
    – MarcoB
    Commented Aug 28, 2023 at 13:12

2 Answers 2

7
$\begingroup$

The direct answer to "how should it be implemented" is to use SetSharedFunction. Evaluate SetSharedFunction[s1] before the ParallelTable, and you'll get your expected results.

However, this whole approach seems very misguided. There seems to be no reason to use parallelization for this. Furthermore, Table itself is a strange choice to use for creating down values. So, I think the real answer to "how should it be implemented" is that you should take a completely different approach. We'd need more context to determine that approach, however.

$\endgroup$
1
  • $\begingroup$ Thanks! My real code has the same structure as this example. There is some function that is found by minimization and depends on the parameter (B). Aftrethat I use ParallelTebale for assignment $\endgroup$
    – Mam Mam
    Commented Aug 28, 2023 at 19:07
3
$\begingroup$

If you want to apply a function to every element of some list, there is "Map" or "ParallelMap".

For your example this wouldreadusing ParallelMap):

s[B_] := NMinimize[B*x^4 - 3 x^2 - x, x]
s1 = ParallelMap[s, {1, 3, 10, 7}]

{{-3.51391, {x -> 1.30084}}, {-1.49476, {x -> 
    0.779079}}, {-0.64752, {x -> 0.452979}}, {-0.82037, {x -> 
    0.530649}}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.