fork

Read(2295) Label: fork,

Here’s how to use fork statment.

fork A i ,…

Description:

Use multithreads to execute the code block in a cellset.

Syntax:

fork  Ai,

Note:

Ai is a sequence parameter, whose length determines the number of parallel threads. The statement makes multiple duplicates of the current cellset and its context to execute them respectively using multiple threads. A sequence parameter will be separated to have its members respectively used as the value of current cell for the execution of each thread; and a single-value parameter will be duplicated to be distributed to each thread. Form a sequence of values returned by the code block and enter them into the fork cell.

 

Use #c to represent parallel thread number in the code block.

Parameter:

Ai

A sequence

Example:

Single parameter:

 

A

B

 

1

fork [[1,20,6,14,5],[32,8]]

 

Return the return results of multithreads once the fork code block is executed.

2

 

=connect("demo")

 

3

 

=B2.query("select * from EMPLOYEE where EID in (?) ",A1)

EID is the parameter.

4

 

=B2.close()

 

5

 

return B3

 

 

Multiple parameters:

 

A

B

 

1

fork [[1,20,6,14,5],[32,8]],"F"

/

Return the return results of multithreads once the fork code block is executed.

2

 

=connect("demo")

 

3

 

=B2.query("select * from EMPLOYEE where EID in (?) and GENDER=?",A1(1),A1(2))

EID is the first sequence parameter, GENDER is the second sequence parameter.

4

 

=B2.close()

 

5

 

return B3

 

 

Use parallel thread number:

 

A

B

 

1

=file("PersonnelInfo.txt")

 

 

2

fork to(4)

 

3

 

=A1.import@t(;A2:4)

Retrieve records from data file A1 using multiple threads

4

 

return #A2|B3

#A2 represents the parallel thread number.

Related functions:

callx()

call()

fork cs

Description:

Perform parallel computation over a multicursor.

Syntax:

fork cs

Note:

The function performs parallel computation over multicursor cs. Within a parallel computing code block, the referenced cell represents the current thread of cursor.

Parameter:

cs

A multicursor

Example:

 

A

B

 

1

=file("employee.txt")

=A1.cursor@mt()

Create a multicursor based on employee.txt and return it.

2

fork B1

 

Perform parallel computation over the multicursor.

3

 

=A2.select().fetch()

Get records from each thread of cursor by loop

fork ....;hs, reduce

Description:

Execute a code block in a node.

Syntax:

fork  ….;hs reduce

Note:

The statement executes fork code block on a sequence of nodes hs while distributing referenced cellset variables (including cell names) among the nodes, and populates the return values into the fork cell. The execution will be given to another node if end msg failure happens. The redistribution will uses the default method.

Parameter:

….

A parameter sequence

hs

The node sequence in which each server is represented by a string in the form of "address:port number", like "192. 168. 0. 86: 4001".

s

A sequence consisting of integer sequences; can be omitted

reduce

The parameter can be absent; when the number of tasks is greater than that of the nodes, perform reduce on subtasks on the same node; store the last cumulative value in fork cell and the current computing result in reduce cell; the referenced cellset variables will be distributed among the nodes

Example:

Single parameter

 

A

B

 

1

fork [[1,20,6,14,5],[32,8]];["192.168.1.100:8989"]

 

Execute the fork code block and return a sequence of results returned by all threads

IMG_256

2

 

=connect("demo")

 

3

 

=B2.query("select * from EMPLOYEE where EID in (?) ",A1)

 

4

 

=B2.close()

 

5

 

return B3

 

 

 

A

B

 

1

[192.168.0.116:8281,192.168.0.129:8281]

 

 

2

[English,Math,PE]

 

 

3

fork  A2;A1

 

Give parameters English and PE to node 192.168.0.116:8281, and parameter Math to node 192.168.0.129:8281. A3 returns result as follows:

4

 

=connect("demo")

 

5

 

=B4.query("select * from SCORES  where SUBJECT=?",A3)

 

6

reduce

=A3|A6

 

 

Multiple parameters

 

A

B

 

1

fork [[1,20,6,14,5],[32,8],[3,4],23,12,89,56,90,231,333,111,231],"F";["192.168.1.100:8989","192.168.1.100:8282","192.168.1.100:8283"]

 

A1 returns result as follows:

2

 

=connect("demo")

 

3

 

=B2.query("select * from EMPLOYEE where EID in (?) and GENDER=?",A1(1),A1(2))

 

4

 

=B2.close()

 

5

 

return B3

 

Related function:

  callx()

  fork Ai,...

fork … fork …

Description:

Execute multiple code blocks in parallel.

Syntax:

fork

fork

Note:

The function executes multiple code blocks in parallel and assigns the result of executing each code block to the corresponding fork cell.

Example:

 

A

B

1

fork [[1,20,6,14,5],[32,8]]

//fork

2

 

=connect("demo")

3

 

=B2.query("select * from EMPLOYEE where EID in (?) ",A1)

4

fork [[1,20,6,14,5],[32,8]],"F"

//fork

5

 

=connect("demo")

6

 

=B5.query("select * from EMPLOYEE where EID in (?) and GENDER=?",A4(1),A4(2))