1
$\begingroup$

I have 4 kernels and would like that in ParallelTable:

kernel 1 takes i from   1 to  250
kernel 2 takes i from 251 to  500
kernel 3 takes i from 501 to  755
kernel 4 takes i from 756 to 1000

How can I do that as example for the lower code?

ParallelTable[

 x = i^2;
 Print[x],

 {i, 1, 1000}

 ]
$\endgroup$
  • 3
    $\begingroup$ ParallelTable doesn't seem to have that, maybe something like ParallelEvaluate[ Table[{$KernelID, n}, {n, ($KernelID - 1) 250 + 1, $KernelID 250}]]~Flatten~1 $\endgroup$ – Jason B. Feb 5 '16 at 14:23
  • $\begingroup$ How would the code look like when I want to read images from a List imageNamesList e.g. ParallelTable[ Import[ imageNamesList[[i]] ], {i, 1, 1000} ] $\endgroup$ – mrz Feb 5 '16 at 15:11
  • 1
    $\begingroup$ Like this, right? ParallelEvaluate[ Table[Import[ imageList[[n]]], {n, ($KernelID - 1) 250 + 1, $KernelID 250}]]~Flatten~1 $\endgroup$ – Jason B. Feb 5 '16 at 15:15
  • $\begingroup$ I get the following errors: (kernel 1) Part::partd: Part specification imageNamesList[[1]] is longer than depth of object. (kernel 2) Part::partd: Part specification imageNamesList[[251]] is longer than depth of object. and so on ? $\endgroup$ – mrz Feb 5 '16 at 15:24
  • 1
    $\begingroup$ Then you have to set imageNamesList as a shared variable, right? SetSharedVariable[imageNamesList] $\endgroup$ – Jason B. Feb 5 '16 at 15:26
1
$\begingroup$

Here is the solution of Jason B.

ParallelEvaluate[
  Table[
    {$KernelID, i};
    x = i^2;
    Print["$KernelID = ", $KernelID, ", i = ", 
     i ", x = \!\(\*SuperscriptBox[\(i\), \(2\)]\) = ", x],
    {i, ($KernelID - 1) 250 + 1, $KernelID 250}
    ];
  ]; 

Output is correct: (I only do not understand why for i=1 no output is shown?)

enter image description here

...

enter image description here

$\endgroup$
  • 1
    $\begingroup$ "I only do not understand why for i=1 no output is shown" - when you are building statements like this to print, combining strings and numbers, the best way to do it is by converting everything to a string and using StringJoin to combine them. Print["$KernelID = " <> IntegerString[$KernelID] <> ", i = " <> IntegerString[i] <> ", x = \!\(\*SuperscriptBox[\(i\), \(2\)]\) = " <> IntegerString[x]] $\endgroup$ – Jason B. Feb 8 '16 at 8:14
  • $\begingroup$ @Jason B: can you overtake my written solution (which is yours), so I can accept it as solved? $\endgroup$ – mrz Feb 11 '16 at 12:16
  • $\begingroup$ no worries, you should accept this as is :-) $\endgroup$ – Jason B. Feb 11 '16 at 12:33
  • $\begingroup$ It is not possible to vote for my own answer ... $\endgroup$ – mrz Feb 21 '16 at 13:37
  • 1
    $\begingroup$ I just meant that you should accept it, with the green check mark, since it is the solution to the problem. There isn't a way to transfer the ownership of a post, and it really is not a big deal for the attribution. You could make the answer a community wiki I think, but I'm not familiar with how to do that. Cheers $\endgroup$ – Jason B. Feb 21 '16 at 13:41

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.