3
$\begingroup$

I am following this answer to evaluate certain cells in a separate kernel. I have created a separate kernel called K2 and have the following MWE:

myvar = 1;
DistributeDefinitions[myvar]

{myvar}

Which - I believe - makes the definition of myvar available in all kernels.

Now I am running e.g.

CellPrint@Cell[BoxData[MakeBoxes[Defer[1 + 1]]], "Input", Evaluator -> "K2"]

which produces a cell

1+1

that can be evaluated (in kernel K2) and yields 2 (unsurprisingly). So far, so good.

Now, if I run

CellPrint@Cell[BoxData[MakeBoxes[Defer[myvar]]], "Input", Evaluator -> "K2"]

a cell

myvar

is created. But evaluating it still yields only myvar rather than 1. (The same holds if I omit the Defer).

I believe I am misunderstanding the concept of separate kernel evaluation? Ultimately, I want to send not only a variable, but a custom function to K2.

$\endgroup$

1 Answer 1

3
$\begingroup$

You are confusing parallel kernels (subkernels) with the ability to have multiple evaluators set up in the front end.


DistributeDefinitions is part of the parallel tools. Mathematica can run code in parallel by launching several "slave kernels" or subkernels and sending jobs to each. The subkernels are controlled from by the main kernel.


We can also create several kernel setups through the Evaluation -> Kernel Configuration Options menu item. Then we can assign a different evaluator to each notebook or each cell within a single notebook. This is what the Evaluator option does.

This is a Front End feature and has absolutely no relationship to the parallel tools. The Front End communicates directly with each evaluator you set up. They are all "main kernels", to use the parallel tools terminology.

There is no ready made mechanism to communicate between these. You can try to set up some sort of manual communication using MathLink. Whether this is a reasonable idea at all depends on what you are trying to achieve. In most cases I can imagine it isn't a good idea to do this.

$\endgroup$
4
  • $\begingroup$ Thank you! So just to understand, can I also address "slave kernels" (your option 1) from the Front End? I.e. send some cell evaluation to a subkernel such that multiple evaluations at the same time are possible (chosen from the Front End)? Or do I need to use the Evaluator option (your option 2) for this? $\endgroup$
    – Bernd
    Nov 15, 2016 at 12:55
  • $\begingroup$ No, it is not possible to send evaluations directly to parallel kernels. It can only be done using special commands evaluated in the main kernel, such as ParallelEvaluate and ParallelSubmit. ParallelEvaluate will block, ParallelSubmit doesn't. $\endgroup$
    – Szabolcs
    Nov 15, 2016 at 13:05
  • $\begingroup$ But (sorry for the follow up questions...) with ParallelSubmit I need to know in advance which evaluations I want to run in parallel, correct? I.e. I cant set up some lengthy evaluation and then, while it runs, start further ones (that are evaluated simultaneously)? $\endgroup$
    – Bernd
    Nov 15, 2016 at 14:11
  • 1
    $\begingroup$ No, ParallelSubmit doesn't block. It returns immediately, and you can do something else while it is working. But it is designed for parallelization, which isn't exactly what you are doing. When I do what you describe, I also set up different kernels for different notebooks. But then I can't share data between them, at least not automatically. $\endgroup$
    – Szabolcs
    Nov 15, 2016 at 14:13

Your Answer

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

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