# Remote Definitions

Parallel kernels do not have access to the values of variables defined in the master kernel, nor do they have access to locally defined functions.

*Mathematica* contains a command DistributeDefinitions that makes it easy to transport local variables and definitions to all parallel kernels. The main advantage of this method is that the application package does not need to be installed on the remote kernels. All definitions are sent through the existing connection to the remote kernels.

## Distributing Definitions

DistributeDefinitions[s_{1},s_{2},...] | distribute all definitions for symbols to all remote kernels |

DistributeDefinitions["Context`"] | distribute definitions for all symbols in the specified context |

DistributeDefinitions has the attribute HoldAll to prevent the evaluation of the symbols.

DistributeDefinitions exports the following kinds of definitions: OwnValues, DownValues, , UpValues, , .

DistributeDefinitions sets the attributes of the remote symbols equal to the locally defined attributes, except for attributes such as Protected and Locked.

Any old definitions existing on the remote side are cleared before the new definitions are made.

### Examples

In[24]:= |

Out[24]= |

In[25]:= |

In[26]:= |

In[28]:= |

Out[28]= |

In[29]:= |

Out[29]= |

The reason is that the remote kernels return the unevaluated expression , because the function and variable are not defined on the remote kernel. The master kernel evaluates the returned results further, but it does so sequentially.

You can easily produce an example where the difference between remote and local evaluation becomes apparent.

In[30]:= |

Out[30]= |

In[31]:= |

Out[31]= |

In[32]:= |

In[33]:= |

Out[33]= |

### Automatic Distribution of Definitions

Higher-level parallel commands, such as Parallelize, ParallelTable, ParallelSum, ... will automatically distribute definitions of symbols occurring in their arguments.

In[1]:= |

In[3]:= |

Out[3]= |

This automatic distribution happens for any functions and variables you define interactively, within the same notebook (technically, for all symbols in the default context). Definitions from other contexts, such as functions from packages, are not distributed automatically.

In[4]:= |

In[5]:= |

Out[5]= |

In[6]:= |

In[7]:= |

Out[7]= |

### Distributing Contexts

DistributeDefinitions["Context`"] exports all definitions for all symbols in the given context. Thus, you can use the following to make all your interactively entered definitions known to the remote kernels.

In[11]:= |

Out[11]= |

Exporting the context of a package you have loaded may not have the same effect on the remote kernels as loading the package on each remote kernel. The reason is that loading a package may perform certain initializations and it may also define auxiliary functions in other contexts (such as a private context). Also, a package may load additional auxiliary packages that establish their own contexts.

DistributeDefinitions["Context`"] is useful for exporting contexts for definitions that you have explicitly set up to be used on remote kernels. There is a separate command ParallelNeeds for remote loading of packages.

clears any definitions for symbols in the context on remote kernels.

## Loading Packages on Remote Kernels

ParallelNeeds["Context`"] | evaluate Needs["Context`"] on all available parallel kernels |

ParallelNeeds["Context`"] is essentially equivalent to ParallelEvaluate[Needs["Context`"]], but it is remembered, and any newly launched remote kernels will be initialized as well.

Exporting the context of a package you have loaded may not have the same effect on the remote kernels as loading the package on each remote kernel with ParallelNeeds[]. The reason is that loading a package may perform certain initializations, and it may also define auxiliary functions in other contexts (such as a private context). Also, a package may load additional auxiliary packages that establish their own contexts.

In[12]:= |

In[13]:= |

Note that *Mathematica* packages available to the master kernel may not be available on remote kernels from older versions of *Mathematica*.

## Example: Eigenvalues of Matrices

### Definitions

In[1]:= |

In[2]:= |

In[3]:= |

In[4]:= |

Out[4]= |

### A Sample Run

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |