Message Boards Message Boards

Parallelization and NMinimize

Posted 10 years ago

I have a problem with NMinimize function that does not seem able to use the 8 processors of my machine (16 kernels with hyperthreading) to calculate the function to be minimized. If I calculated for given values of the parameters the same function Mathematica uses several processors. To do that I use ParallelTable function. But when I use NMinimize my computer use only one processor.

here is the function to minimize :

func = Total[
  ParallelTable[(mmoyen0[datamhtcor[[j, 1]], datamhtcor[[j, 2]], 
       datamhtcor[[j, 3]], -10, 0., 2, 2, 2] - 
      datamhtcor[[j, 4]])^2, {j, 1, Length[datamhtcor]}]]

*where mmoyen0 is another function which diagonalize matrices. It depends on variables (b, db, t) and parameters (dist,ee, gx,gy,gz) datamhtcor are a list of experimental data {magnetic field B, small variation of B,Temperature, axial anisotropy, rhombic anisotropy, Lande factor x, Lande factor y, Lande factor x}

SetAttributes[mmoyen0, Listable]
mmoyen0[(b_)?NumericQ, (db_)?NumericQ, (t_)?NumericQ, (dist_)?
    NumericQ, (ee_)?NumericQ, (gx_)?NumericQ, (gy_)?NumericQ, (gz_)?
    NumericQ] := 
  Module[{i, mm}, 
    mm = Table[
      msurh0[b, db, t, dist, ee, gx, gy, gz, cossintetaphi[[i, 1]], 
       cossintetaphi[[i, 2]], cossintetaphi[[i, 3]]], {i, 1, 
       Length[cossintetaphi]}];
    Total[mm, {1}]]/Length[cossintetaphi];

msurh0 is the following function

msurh0[(b_)?NumericQ, (db_)?NumericQ, (t_)?NumericQ, (dist_)?
   NumericQ, (ee_)?NumericQ, (gx_)?NumericQ, (gy_)?NumericQ, (gz_)?
   NumericQ, (cos_)?NumericQ, (sincos_)?NumericQ, (sinsin_)?
   NumericQ] := 
 mx20[b, db, t, dist, ee, gx, gy, gz, cos, sincos, sinsin] sincos + 
  my20[b, db, t, dist, ee, gx, gy, gz, cos, sincos, sinsin] sinsin + 
  mz20[b, db, t, dist, ee, gx, gy, gz, cos, sincos, sinsin] cos

and mx20 is

mx20[(b_)?NumericQ, (db_)?NumericQ, (t_)?NumericQ, (dist_)?
    NumericQ, (ee_)?NumericQ, (gx_)?NumericQ, (gy_)?NumericQ, (gz_)?
    NumericQ, (cos_)?NumericQ, (sincos_)?NumericQ, (sinsin_)?
    NumericQ] := 
  Module[{kk, nak, \[Beta], i, som1, som2, en1, en2}, 
   nak = 8.314282370753999 10^7; \[Beta] = 4.66864374 10^-5; 
   kk = 0.695054;
   (*calcul de la dérivée suivant l'axe x*)
        som1 = 0; som2 = 0;
        en1 = 
    Sort[Eigenvalues[
      matx20[b, db/2, gx, gy, gz, dist, ee, cos, sincos, 
       sinsin, \[Beta]]]];
        en2 = 
    Sort[Eigenvalues[
      matx20[b, -db/2, gx, gy, gz, dist, ee, cos, sincos, 
       sinsin, \[Beta]]]];
        som1 = Total[Exp[-en1/(kk t)]];
        som2 = Total[Exp[-en2/(kk t)]];
        nak t  (Log[som1] - Log[som2])/db/5585];

To calculate the function func Mathematica use several processors but when this function is used in NMinimize, Mathematica use only one processor

Do you have any suggestions to force Mathematica to use several processors to calculate the func function. It is really a pity to see that Mathematica only use 6% of power of my computer (I have 16 possible kernels on my machine)

Regards

Yves

Attachments:
POSTED BY: Yves Journaux

Cher Yves, I took at look at your notebook. I am uncertain what you are asking, but I think the question is why the built-in function NMinimize is not parallelized. Is that correct?

I agree is would be nice if some of the numerically intensive functions in Mathematica were designed to take an option such as Parallelize->True, but it doesn't exist (it is one of the suggestions here: http://community.wolfram.com/groups/-/m/t/181759?ppauth=bCSNFi3t).

However, in your example you have this construct:

es0 = NMinimize[{Total[
    ParallelTable[(mmoyen0[datamhtcor[[j, 1]], datamhtcor[[j, 2]], 
         datamhtcor[[j, 3]], dist, ee, gx, gy, gz] - 
        datamhtcor[[j, 4]])^2, {j, 1, Length[datamhtcor]}]], 
   gx > 2. && gy > 2. && gz > 2 && ee < Abs[dist/3]}, {{gx, 2, 
    2.2}, {gy, 2.1, 2.2}, {gz, 2.1, 
    2.2}, {dist, -5, -2}, {ee, .3, .5}} , Method -> "NelderMead", 
  MaxIterations -> 100]

If NMinimize were parallelized, then this isn't likely to work because you would be wrapping on parallelization inside another. You would be better off precomputing (e.g,)

minimizationTarget =Total[ParallelTable[(mmoyen0[datamhtcor[[j, 1]], datamhtcor[[j, 2]], 
      datamhtcor[[j, 3]], dist, ee, gx, gy, gz] - 
     datamhtcor[[j, 4]])^2, {j, 1, Length[datamhtcor]}]]

and then minimizing that in a subsequent step:

NMinimize[{minimizationTarget,, gx > 2. && gy > 2. && gz > 2 && ee < Abs[dist/3]}, {{gx, 2, 
   2.2}, {gy, 2.1, 2.2}, {gz, 2.1, 
   2.2}, {dist, -5, -2}, {ee, .3, .5}} , Method -> "NelderMead", 
 MaxIterations -> 100]

One last observation, instead of doing this:

(*test ù ParallelTable est utilisé pour les 60 points expériementaux *)

    t1 = AbsoluteTime[];
    Do[Total[ParallelTable[(mmoyen0[datamhtcor[[j, 1]], 
           datamhtcor[[j, 2]], datamhtcor[[j, 3]], -10, 0., 2, 2, 2] - 
          datamhtcor[[j, 4]])^2, {j, 1, Length[datamhtcor]}]], {i, 1, 
      30}]
    t2 = AbsoluteTime[] - t1

There is a handy function Timing[] which you can wrap around the Do.

POSTED BY: W. Craig Carter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract