Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

My friend and I are trying to set up remote kernels.

He has a server at home running 1 kernel off of a Linux-based server. We're both on 8.0.

I manually set $RemoteCommand to

"ssh `3`@`1`-l `3` \"math -mathlink -linkmode Connect `4` -linkname \
`2` -subkernel -noinit >& /dev/null &\""`

and manually changed $RemoteUserName to the one he set up for me. The problem is, whenever we try and run the code, we get the following error message:

LaunchRemote::rsh: Command ssh [my username]@[his server address]-l [my username] "math -mathlink -linkmode Connect -linkprotocol TCPIP -linkname 51145@[my public IP address],51146@[my public IP address] -subkernel -noinit >& /dev/null &" may have failed (exit code 1).

The problem is, my public IP address is not the same as my private IP address, so I can't connect. How can I get Mathematica to recognize my private IP instead?

share|improve this question

3 Answers 3

I have never had luck with this directly, since the remote machine has to connect back to the master. However, there is a way to do it with OpenVPN -- make the two machines connect to the VPN appear to be on the same subnet.

share|improve this answer
1  
and just to add -- you might think that you can do port forwarding through your router. But it often doesn't work because of ISP port-blocking policies. Also, I have never gotten the ssh forwarding stuff to work, either. –  Eric Brown Oct 7 '12 at 5:09

I've been successful connecting to my office Mac workstation using SSH port forwarding:

ssh -R 31415:127.0.0.1:31415 -R 31416:127.0.0.1:31416 username@XXX.XXX.XXX.XXX "/Applications/Mathematica.app/Contents/MacOS/MathKernel -mathlink -LinkMode Connect -LinkProtocol TCPIP -LinkName 31415@127.0.0.1,31416@127.0.0.1 -LinkHost 127.0.0.1"

where username@XXX.XXX.XXX.XXX should be replaced with your SSH user name and the IP address of the remote machine. If the remote machine is running Linux, replace

/Applications/Mathematica.app/Contents/MacOS/MathKernel

with the appropriate path of the MathKernel executable.

share|improve this answer
    
So 31415 and 31416 are ports.. ARe these default values? Can I do something similar to start a remote kernel in my notebook/script? –  drN Oct 7 '12 at 13:44
    
You can change those port numbers to anything. Yes, you should be able to use the same to start a remote kernel in your notebook/script. –  Dale Roberts Oct 7 '12 at 23:18
    
can you give more details on how/where you use the command? –  jenson Nov 6 '12 at 0:16

Dale Roberts's solution works fine, but only when the remote machine can be seen directly from the Internet. After connection you can type

Links[]

and see

{LinkObject["31415@127.0.0.1,31416@127.0.0.1", 3, 1], LinkObject["36946@109.1.2.3,51520@109.1.2.3", 56, 2], LinkObject["59488@109.1.2.3,54373@109.1.2.3", 57, 3]}

It means that the main link (the first) was opened through the ssh tunnel, but the preemptive link and the service link (the second and the third) was opened directly to the remote machine (it has 109.1.2.3 ip in this example).

If the remote machine is behind a router with port redirection this approach is unfortunately not worked. It is because MathLink naively try to open the preemptive link and the service link to the remote machine ip which is now something like 192.168.1.123. This behaviour can not be changed from extarnal options like LinkName and LinkHost.

I found great solution here: Remote Kernel Strategies. It simply redefines MathLink`CreateFrontEndLink. It was written for Mathematica 6, but it also works with Mathematica 9.

Now I can connect and see that all connections are through the ssh tunnel

Links[]

{LinkObject["37174@127.0.0.1,39928@127.0.0.1", 3, 1], LinkObject["39929@127.0.0.1,39930@127.0.0.1", 57, 2], LinkObject["39931@127.0.0.1,39932@127.0.0.1", 58, 3]}

For further study, see excellent explanation in init.m in Remote Kernel Strategies.


I want to add some information about connecting to and disconnecting from the remote kernel without it's interrupting. It may be useful if you want to send a long task, turn off local machine and reconnect to the remote machine several days later.

I don't know the best place to post this information. This question is about connecting to the master kernel on the server while the subkernels are running. So I will be happy if you correct me.

I modify the last command in tunnel.sh in Remote Kernel Strategies in the following way:

/usr/bin/ssh \
 $SSH_OPTS \
     -R 127.0.0.1:$MAIN_LINK_DATA_PORT:127.0.0.1:$MAIN_LINK_DATA_PORT \
     -R 127.0.0.1:$MAIN_LINK_MESSAGE_PORT:127.0.0.1:$MAIN_LINK_MESSAGE_PORT \
     -L $PREEMPTIVE_LINK_DATA_PORT:127.0.0.1:$PREEMPTIVE_LINK_DATA_PORT \
     -L $PREEMPTIVE_LINK_MESSAGE_PORT:127.0.0.1:$PREEMPTIVE_LINK_MESSAGE_PORT \
     -L $SERVICE_LINK_DATA_PORT:127.0.0.1:$SERVICE_LINK_DATA_PORT \
     -L $SERVICE_LINK_MESSAGE_PORT:127.0.0.1:$SERVICE_LINK_MESSAGE_PORT \
     $REMOTE_KERNEL_HOST \
 "screen -wipe; \
 screen -ls | grep MathKernel -q || \
 screen -md -S MathKernel $REMOTE_KERNEL_PATH && \
     screen -md -S Respawner screen -x MathKernel; \ 
     sleep 1; \
     screen -S MathKernel -X stuff `echo -e \"\\\\\\\$ParentLink=LinkCreate[\\\\\\\"$LINK_NAME\\\\\\\",LinkMode-\\\\\\\>Connect,LinkProtocol-\\\\\\\>\\\\\\\"TCPIP\\\\\\\"]\\\\\\\;\\\r\"`; \
     sleep 20" >> $LOGFILE 2>&1

It starts screen session with MathKernel name if it isn't exist. Then it connects to the existing session by

$ParentLink=LinkCreate["$LINK_NAME",LinkMode->Connect,LinkProtocol->"TCPIP"];

Without sleep Mathematica doesn't have time to establish the connection. For disconnecting I use

SetAttributes[Disconnect, HoldAll];
Disconnect[expr___] :=
 Module[{links = {$ParentLink, MathLink`$ServiceLink,
     MathLink`$PreemptiveLink}}, $ParentLink = Null;
  LinkClose /@ links; MathLink`$PortNumber =.; SetOptions["stdout", FormatType->OutputForm];
  expr]

Then you can simply run something like this

x = Sum[EulerPhi[n], {n, 10000000}]; // Disconnect

and x=... will be evaluated after disconnection. MathLink`$PortNumber =. is necessary to connect to the same ports after disconnecting. For convenienceDisconnect can be added to init.m.

Update 1: you have to run two screen sessions and attach one to another because you can send commands only to attached sessions. You can directly connect to the screen session by screen -x MathKenel. It is useful if you want to check the state of your program from the mobile phone.

Update 2: LinkClose/@Links[] is bad because it closes links to parallel subkernels. So I explicitly specify $ParentLink, MathLink`$ServiceLink, and MathLink`$PreemptiveLink.

Update 3: Explicit port specification isn't needed.

Update 4: The terminal formatting is restored by SetOptions["stdout", FormatType->OutputForm]. Thanks this answer.

Update 5: After reconnection custom syntax highlighting is dropped. You can repair it by

Scan[ToExpression[#, InputForm, 
    Function[name, 
     SyntaxInformation@Unevaluated[name] = 
      SyntaxInformation@Unevaluated[name], HoldAll]] &, 
  Names["Global`*"]];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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