4
$\begingroup$

It's easy to launch a frontend under Rosetta, which will naturally start its kernels under Rosetta as well. For example, you can check a box in the Get Info window of Mathematica in the Finder, among a bunch of other solutions. But what if you want to work in a native FrontEnd and kernel, and just occasionally launch a kernel under Rosetta in the same FrontEnd? Perhaps you use a package that depends on Intel-only code, so you want to be to run that in one notebook while all your others are running natively. This turned out to be a bit more interesting than I thought, so here's the question with two different forthcoming answers.

$\endgroup$

2 Answers 2

4
$\begingroup$

The more obvious way to do this is to create a script the launches the kernel and then point the FE to that. The following is a simple script for this purpose. Create it using your favourite text editor and make it executable using chmod a+x scriptname.

#!/bin/sh

/usr/bin/arch -x86_64 /Applications/Mathematica.app/Contents/MacOS/WolframKernel $@

It's important to use full paths. The FE, being a GUI application, likely has no nontrivial entries on its path for the script to inherit. Obviously, adjust the path to match your machine.

Now, we just need to get the FE to use it. We could use the Evaluation > Kernel Configuration Options to create a new kernel, replace WolframKernel with the complete path to the script, and hit OK. We now have a kernel that will always launch in Rosetta mode. You can also create this entry programmatically, where here I've named my script rosettaKernel and put it in /usr/local/bin/:

CurrentValue[$FrontEnd, {EvaluatorNames, "Rosetta Kernel"}] = {"AutoStartOnLaunch" -> False, 
"Executable" -> "/usr/local/bin/rosettaMath"}}

I find the need for the script disappointing (though if a typo hadn't prevented it from working, I probably would have stopped there and you wouldn't have this post to read). I wouldn't really use it from the command-line, I would just do arch -x86_64 myFavouriteKernel and easily launch in Rosetta mode all the kernels I normally run natively. Also, it means I can't just add and remove kernel configuration options--I have to plan ahead and create a script.

So let's try to do this without a script. In this case, we cannot use the simple settings, aka the "Executable" field--we'll obviously need multiple parameters. Also, it's better to split the labor between the FE and Kernel, effectively using LinkCreate + LinkConnect as opposed the LinkLaunch utilized by the "Executable" type. This isn't strictly necessary--see PS1. Here's the command that does that:

CurrentValue[$FrontEnd, {EvaluatorNames, "Rosetta Kernel"}] = {
  "AutoStartOnLaunch" -> False, 
  "MLOpenArguments" -> "-LinkMode Listen -LinkOptions MLDontInteract",
  "LoginScript" -> 
       "/usr/bin/arch -x86_64 /Applications/Mathematica.app/Contents/MacOS/WolframKernel -wstp -linkconnect -linkname `linkname`"
}

The "MLOpenArguments" and "LoginScript" fields correspond directly to the values of "Arguments to MLOpen" and "Launch Command" of the kernel configuration GUI, which you can see by opening it after evaluating the previous command. Now we have a kernel that will always run in Rosetta mode! And if we want to temporarily edit it to point to a different kernel, we can easily do that within the frontend, either programmatically or using the menu options. A much better solution.

PS 1 UPDATED: I originally thought that using that using Launch mode require duplication between the fields, using something like the following:

CurrentValue[$FrontEnd, {EvaluatorNames, "Rosetta Kernel"}] = {
   "AutoStartOnLaunch" -> False, 
   "MLOpenArguments" -> 
       "-LinkMode Launch -LinkProtocol SharedMemory -LinkName \"'/Applications/Mathematica.app/Contents/MacOS/WolframKernel' -wstp\" -LinkOptions 3", 
  "LoginScript" -> 
        "/usr/bin/arch -x86_64 /Applications/Mathematica.app/Contents/MacOS/WolframKernel -wstp -linklaunch"}

What I failed to fully appreciate (thanks to ihojnicki pointing this out in the comments) was that the LinkName doubles as the base command to execute, so if we update it correctly (and carefully!) we don't need the "LoginScript" field at all. See my other answer for those details. I've personally chosen to stick with the create / listen approach as it strikes as a bit cleaner, but now that we know exactly how to edit the standard configuration to add the arch -x86_64, that's probably an easier, direct approach.

PS2: You can decouple the FE and kernel pieces if you want. This is useful, for example, when you're trying to diagnose issues with the kernel launching in the FE! Here's how to that:

CurrentValue[$FrontEnd, {EvaluatorNames, 
   "Listening Link"}] = {"AutoStartOnLaunch" -> False, 
  "MLOpenArguments" -> "-LinkMode Listen"}

Now, evaluate a an expression in a notebook using this kernel. A pop-up will appear with the name of a link. Do not dismiss the popup. Rather, copy the name of the link, and launch a kernel that connects to the link (using the options show above). Now go back and dismiss the popup. The FrontEnd will start using the kernel. I'm indebted to John Fultz for explaining this trick to me.

$\endgroup$
1
  • 1
    $\begingroup$ You can also add arch -x86_64 to the beginning of LinkName. "Rosetta" ->{"AutoStartOnLaunch" -> False, "MLOpenArguments" ->"-LinkMode Launch -LinkProtocol SharedMemory -LinkName \"arch -arch x86_64 '/Applications/Mathematica.app/Contents/MacOS/WolframKernel' -wstp\" -LinkOptions 3"} $\endgroup$
    – ihojnicki
    Mar 24 at 13:21
3
$\begingroup$

I'm adapting ihojnicki's comment into a second, independent answer, because this is more of a "grab and go" answer. I'll leave my original answer more or less unchanged for greater context.

  1. Open Evaluation > Kernel Configuration Options and and click the + sign to create a new kernel
  2. Keep the default setting for the executable at WolframKernel and click on the Advanced Options radio button
  3. Between the double quotes an single quote that precede the path to WolframKernel in Arguments to MLOpen, insert arch -x86_64 . The text field should now read something like (with the path to the kernel dependent on where you installed Mathematica):
-LinkMode Launch -LinkProtocol SharedMemory -LinkName "arch -x86_64 '/Applications/Mathematica.app/Contents/MacOS/WolframKernel' -wstp" -LinkOptions 3

Save the kernel and use it. You can also create this kernel programmatically:

CurrentValue[$FrontEnd, {EvaluatorNames, "Rosetta Kernel"}] = {
  "AutoStartOnLaunch" -> False, 
  "MLOpenArguments" -> 
    "-LinkMode Launch -LinkProtocol SharedMemory -LinkName \"arch -x86_64 '/Applications/Mathematica.app/Contents/MacOS/WolframKernel' -wstp\" -LinkOptions 3"
}
$\endgroup$

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.