Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

gop

Global operation across all workers

Syntax

res = gop(FUN,x)
res = gop(FUN,x,targetlab)

Arguments

FUN

Function to operate across workers.

x

Argument to function F, should be the same variable on all workers, but can have different values.

res

Variable to hold reduction result.

targetlab

Lab to which reduction results are returned. This value is returned by that worker's labindex.

Description

res = gop(FUN,x) is the reduction via the function FUN of the quantities x from each worker. The result is duplicated on all workers.

FUN can be a handle to any function, including user-written functions and user-defined anonymous functions. It should accept two arguments of the same type, and return one result of that same type, so it can be used iteratively in the form:

  FUN(FUN(x1,x2),FUN(x3,x4))

The function FUN should be associative, that is,

FUN(FUN(x1,x2),x3) = FUN(x1,FUN(x2,x3))

res = gop(FUN,x,targetlab) performs the reduction, and places the result into res only on the worker indicated by targetlab. res is set to [ ] on all other workers.

Examples

This example shows how to calculate the sum and maximum values for x among all workers.

p = parpool('local',4);
x = Composite(); 
x{1} = 3;
x{2} = 1;
x{3} = 4;
x{4} = 2;
spmd
    xsum = gop(@plus,x);
    xmax = gop(@max,x);
end
xsum{1}
10
xmax{1}
4

This example shows how to horizontally concatenate the column vectors of x from all workers into a matrix. It uses the same 4-worker parallel pool opened by the previous example.

x{1} = [3;30];
x{2} = [1;10];
x{3} = [4;40];
x{4} = [2;20];
spmd
    res = gop(@horzcat,x);
end
res{1}
     3     1     4     2
    30    10    40    20

This example shows how to use an anonymous function with gop to join character vectors with spaces between them. In this case, the character vectors are created from each worker's labindex value.

afun = @(a,b)[a,' ',b]
spmd
    res = gop(afun,num2str(labindex));
end
res{1}
1 2 3 4

Related Examples

Was this topic helpful?