previous up next
Go backward to 2.1 A Simple Example
Go up to 2 Examples
RISC-Linz logo

2.2 Distributed Factorization of Integers

The following program uses the features of Distributed Mathematica for computing the factorization of a list of cubes of integers.

main[length_,size_]:=Module[
  {numbers,results,ti},
                
  (* load to all machines the Cube function *)
  AllD["<< test.m"];
                                
  (* generate list of numbers *)
  numbers=rands[length,size];
                
  (* compute result *)
  results=dfactors[numbers];
                
  (* return list of argument/result pairs *)
  Table[{numbers[[i]],results[[i]]},{i,length}]
]

dfactors[numb_]:= Block[
  {length,tasks,results, i,t,r,numbers=numb},
                
  (* length of list *)
  length=Length[numbers];
                
  (* generate list of tasks *)
  tasks={};
  For[i=1,i<=length,i++,
    t=StartD["FactorInteger[Cube[numbers[[i]]]]"];
    tasks=Append[tasks,t];
  ];
                
  (* wait for result values *)
  results={};
  For[i=1,i<=length,i++,
    r=WaitD[tasks[[i]]];
    results=Append[results,r];
  ];
                
  (* return results *)
  results
]
rands[length_,size_]:=Module[
  {i,n,numbers},
                
  (* generate list *)
  numbers={};
  For[i=1,i<=length,i++,
    n=Random[Integer,size];
    numbers=Append[numbers,n];
  ];
                
  (* return list *)
  numbers
]

The main function calls AllD, which executes the command given as argument on all connected kernels, and also on the local one. The executed command loads a small program, that contains the definition of the function Cube.

The function dfactors generates a list of tasks, each of it factorizing the cube of an integer number. The tasks are distributed to the connected kernels, and the results are collected in a list. The function dfactors is defined into a Block, and the argument is copied into a local variable to allow the evaluation of the arguments of FactorInteger by the local kernel. The tasks are executed in parallel, using a common input structure, and the results are gathered into a common output structure.

We can use the program in the context of the following Mathematica session:

In[1]:=<< dist.m
       Distributed Mathematica V1.0.0
         (c) 2000 Cleopatra Pau (RISC-Linz)
       See http://www.risc.uni-linz.ac.at/software/distmath
In[2]:=InitializeD[{{deneb,solaris},
                    {andromeda,octane},
                    {pinwheel,octane}}]
       Connecting deneb...
       Connecting andromeda...
       Connecting pinwheel...
Out[2]=okay
In[5]:=<< dfactor2.m
In[6]:=VisualizeD[600, 300, 2, 120]
In[7]:=main[20,2^25]
Out[7]={{26496159,{{3,3},{8832053,3}}}, ...}
In[8]:=TerminateD[]
Out[8]=okay

The call of VisualizeD opens a window, where one can see the status of each machine and the number of active machines in the last 120 seconds, image refreshed every two seconds.


Maintained by: Cleo Pau
Last Modification: July 5, 2000

previous up next