2
$\begingroup$

I have written below code to launch n kernels connected to one another in series using mathlinks. Before you read the code allow me to be more precise what i want to achieve:

Source kernel(k1) will launch kernel2, kernel2 will launch kernel3, kernel3 will launch kernel4...and so on upto n.

So basically I want links to be connected like k1->k2->k3.....i.e. k2 can only communicate with k3..

In the current implementation I end up calling LaunchSerialLinks[] function recursively without knowing the fact whether its a good practice to use.

Current implementation only opens two kernels k1 and k2, even if nodeCount input is more than 2.

So what my code package will look like in order to launch kernels in series?

LineTopology::usage = "LineTopology[n] opens n nodes/kernels connected in series"

LineTopology[nodeCount_]:= Module[{link},
    (
    link = LinkLaunch[First[$CommandLine] <> " -wstp"];
    Print["Launching Links!!"];
    LinkWrite[link,Unevaluated@EvaluatePacket[LaunchSerialLinks[2,nodeCount]]]
    )
]

LaunchSerialLinks[currentNode_,totalNodes_] := 
    If[currentNode < totalNodes,
        link =  LinkLaunch[First[$CommandLine]<> " -wstp"];
        (*Create a forward link*)
        LinkWrite[link,Unevaluated@Evaluate[LaunchSerialLinks[currentNode+1,totalNodes]]];
        Pause[2]
        ,Null
     ]
$\endgroup$
  • $\begingroup$ I have edited thanks for pointing it out $\endgroup$ – dsingh May 9 '15 at 23:45
  • 1
    $\begingroup$ Yours seems like a reasonable approach to me. Out of curiosity, what do you want this for? It seems like a very inconvenient topology to work with. $\endgroup$ – Oleksandr R. May 10 '15 at 4:31

Your Answer

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

Browse other questions tagged or ask your own question.