6
$\begingroup$

Is it possible to simply force Mathematica to evaluate one cell with let's say kernel A and another cell with kernel B etc. so that they can be evaluated in parallel? There is a similar answer here but I prefer avoiding creating cells manually. I would like it either in GUI or in some other simple way. The code is a plot creation. I would prefer each in separate cell. Is this even possible? Why cannot I simply right click a cell and assign a kernel :-(.

$\endgroup$
7
$\begingroup$

Basic approach

At first we configure parallel kernels either via the Evaluation ► Kernel Configuration Options menu item or programmatically via the EvaluatorNames global FrontEnd option. The following code defines 9 additional kernels which you'll able to assign to Cells in a Notebook (warning: if you already changed the Kernel Configuration Options, the following code will override these changes):

krnConfig = 
 Prepend[
  Table["krn" <> ToString[i] -> {"AppendNameToCellLabel" -> True,
                                 "AutoStartOnLaunch" -> False}, {i, 9}], 
        "Local" -> {"AutoStartOnLaunch" -> True}]

SetOptions[$FrontEnd, EvaluatorNames -> krnConfig]
{"Local" -> {"AutoStartOnLaunch" -> True}, 
 "krn1" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn2" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn3" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn4" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn5" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn6" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn7" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn8" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}, 
 "krn9" -> {"AppendNameToCellLabel" -> True, "AutoStartOnLaunch" -> False}}

Then we make a Palette with buttons corresponding to assigning of each of the configured kernels to notebook selection:

CreatePalette[
 Column@Prepend[
   Table[With[{name = "krn" <> ToString[i]}, 
     Button[name, (SetOptions[NotebookSelection[InputNotebook[]], 
         Evaluator -> name];)]], {i, 9}], 
   Button[Clear, (SetOptions[NotebookSelection[InputNotebook[]], 
       Evaluator -> Inherited];)]]]

screenshot

Now you can simply select a Cell (or even several Cells or a cell group) and then press a button in order to assign the corresponding kernel to that Cell. If you wish to revert to the default kernel, press the Clear button.

Finally you can install this palette permanently by using the Palettes ► Install Palette... menu item.


Further elaboration

The above solution can be elaborated further. For example, if we wish to restrict our Palette to affect only "Input" Cells, we can modify it as follows (Mathematica version 9 or higher is required):

CreatePalette[
 Column@Prepend[
   Table[With[{name = "krn" <> ToString[i]}, 
     Button[name, (SetOptions[#, Evaluator -> name] & /@ 
         Cells[NotebookSelection[InputNotebook[]], CellStyle -> "Input"];)]], {i, 9}], 
   Button[Clear, (SetOptions[#, Evaluator -> Inherited] & /@ 
       Cells[NotebookSelection[InputNotebook[]], CellStyle -> "Input"];)]]]

If we wish to assign different individual kernels to selected "Input" Cells automatically, the following Palette will do this (the maximum number of individual kernels is assumed to be 9):

CreatePalette[
 Column[{Button["Assign individual kernels to selected Input Cells", 
    Function[cells, 
      If[TrueQ[1 <= Length[cells] <= 9], 
       MapIndexed[SetOptions[#, Evaluator -> "krn" <> ToString[#2[[1]]]] &, cells], 
       DialogInput[
        DialogNotebook[{TextCell["From 1 to 9 Input Cells must be selected"], 
          Button["OK", DialogReturn[]]}]]]][
     Cells[NotebookSelection[InputNotebook[]], CellStyle -> "Input"]]], 
   Button["Revert selected Input Cells to default kernel", 
    SetOptions[#, Evaluator -> Inherited] & /@ 
     Cells[NotebookSelection[InputNotebook[]], CellStyle -> "Input"]]}]]

screenshot

$\endgroup$
  • $\begingroup$ My license says: Controlling 2, Computing 8; the definition: controlling process indicates the number of simultaneous network users or "seats" allowed and Computation Process indicates the number of computations that may be run simultaneously. According to this #kers=2 but I still should have then have capacity to run 8 calculations in parallel. If I use your definition, assign krn1 to cell 1 and krn2 to cell 2 and run, I get error that it cannot launch a new kernel. Do you know why? $\endgroup$ – leosenko May 7 '16 at 16:31
  • $\begingroup$ With my Palette I can launch 7 kernels or more simultaneously, so you probably have faced a limitation of your license. You should ask the tech support why you can't run more than 1 kernel when your license allows 8 kernels. When you receive the answer, please share it with the community. $\endgroup$ – Alexey Popkov May 7 '16 at 16:53
  • $\begingroup$ The default kernel configuration arguments to MLOpen launch controlling processes instead of computing processes because of the absence of the -subkernel option. Appending this option doesn't work. I suspect computing processes cannot be used as cell evaluators. Computing processes are for parallel computing - I'm uncertain if other uses are licensed. $\endgroup$ – obsolesced Jun 27 '16 at 2:52
  • $\begingroup$ @obsolesced I didn't work with MLOpen and can't say about licensing. I suggest you to contact the tech support. $\endgroup$ – Alexey Popkov Jun 27 '16 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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