# Debugging parallelized code

Is it possible to add a stack trace for this parallelized code to see where the problems stem from?

a[x_] := 1/x;
DistributeDefinitions[a];
a1 = Parallelize[Map[a, {-1, 0, 1, 2}]];


as you can see in unparallelized version ?

b = Table[1/x, {x, -1, 2}]

• Good question. I tried a[x_] := TracePrint@Last@{\$MachineName, 1/x}; with two remote kernels in different hosts. It does return the TracePrint but strangely it seems to have been evaluate at the local master kernel, not in the remote ones. Therefor I don't think this qualifies as an acceptable answer. – rhermans Aug 11 '17 at 14:41
• @rhermans Correct me if i am wrong but the TracePrint will produce everything occurred in the evaluation of a function. Therefor it is not as neat as the embedded stack trace and can produce a lot of unnecessary junk and cause the kernel to explode – user49047 Aug 12 '17 at 15:16

You have a 0 in your lists, and 1/0==Infinity

Also, if you remove the ; from the first part, you will see the exact same result.

Additionally, you could use ParallelMap instead of Parallelize[Map[x]]

Anyway, your code basically says "Calculate 1/x for -1, 0, 1, and 2" and it does so correctly for each case. The error you see just comes from the fact that you're calculating a 1/0 case.

If you want to debug, go to Evaluation -> Debugger, and click on "Break at Messages." Then run your code. Once it gets to the error, you can click "Continue" or look for the highlighted (green) line where the error occurred. You can also hit "Show Stack" to see variable values.

• i am very well aware of why i get the error as i have deliberately made them up so i would get the error. i have already tried what you have suggested in your second part of the answer(debugging mode) and as far as i know it only works for the unparallelized part of the code. – user49047 Aug 11 '17 at 14:01
• You need to include information like this in your question, especially if you deliberately created an error. It's also helpful to people to know what you've already tried, otherwise you're wasting the time of people who might be trying to help you. – Matt Stein Aug 11 '17 at 15:08
• @MattStein you make a fair point, in general terms, but I think particularly in this case it's quite evident from the question that the main aim is to find a method for "Debugging parallelized code" and not to find the error on a particularly simple example. – rhermans Aug 11 '17 at 15:58
• @MattStein As far as I can tell your answer does not provide a solution for evaluation in a remote kernel called by ParallelMap or Parallelize[Map[x]]. Sorry, the "-1" is mine. Please do correct me if I'm wrong. – rhermans Aug 11 '17 at 16:08