create child tasks - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Multithreaded Programming : Task : Threads/Task/Continue

Threads[Task][Continue] - create child tasks

Calling Sequence

Continue(fcn, arg1, ..., argN )

Parameters

fcn

-

(appliable) the function to execute as the continuation task

args1..argsN

-

(anything) an argument to fcn or the specification for a child task

Description

• 

The Continue function creates zero or more child tasks and a continuation task.

• 

The Continue function is part of the Task Programming Model.

• 

Continue behaves much like the Start function, with two important differences.  First Continue does not wait for the tasks it creates to complete, instead Continue returns as soon as the tasks are created. Secondly, Continue may only be called from within a invocation of the Task Model.  The Continue function is intended to spread work across multiple tasks as opposed to calculate a particular result.

• 

If child tasks are specified, then Continue creates one task for each child, and a continuation task with fnc as the continuation function. The value of fnc is either a procedure or the name of a predefined continuation function.

• 

The two predefined continuation functions are passed and null

  

passed returns the arguments that were passed into it.

  

null returns NULL.

• 

An argument to Continue of the form

  

Task = [ cfcn, carg1 ... cargn ]

  

creates a child task that executes the following command.

  

cfcn( carg1 ... cargn )

• 

An argument to Continue of the form:

  

Tasks = [ cfcn, [c1args] ... [cnargs] ]

  

behaves as if multiple Task arguments of the form

  

Task = [ cfcn, c1args ], Task = [ cfcn, c2args ], ... Task = [ cfcn, cnargs ]

  

had been given.  Thus, it creates tasks to execute the following commands.

  

cfcn( c1args ), cfcn( c2args ) ... cfcn( cnargs )

• 

The return value of a child task is passed to the continuation function in the child's position in its argument sequence.

• 

Arguments not of the form described above are passed to the continuation function fcn as arguments in the corresponding positions.

• 

If child tasks are created as part of the call to Continue, the continuation task will not start executing until all of its child tasks have completed.

Examples

Solve the N Queens problem: place N queens on an N X N chess board so that no queen can capture any other queen.

nQueens := module()
    local checkBoard,
            completeBoardAndCheck,
            search,
            continuation,
            subInit;
    export ModuleApply;

    checkBoard := proc( n, board::Array )
        local i, j, index;

        for i from 1 to n-1
        do
           index := board[i]+1;

           for j from i+1 to n while index <= n
           do
               if ( index = board[j] ) then
                   return NULL;
               end;

               index := index + 1;
           end do;

           index := board[i] - 1;

           for j from i+1 to n while index >= 0
           do
               if ( index = board[j] ) then
                   return NULL;
               end;

               index := index - 1;
           end do;
       end do;

       return Array( board );
   end proc;

   completeBoardAndCheck := proc( n, board, i, unused )
       local j;

       if ( i < n ) then
           return op( map( proc( j )
                           board[i] := j;
                           completeBoardAndCheck( n, board, i+1,
                               unused minus {j} )
                       end proc, unused ) );
       else
           board[n] := unused[1];
           return checkBoard( n, board );
       end if;
   end proc;

   continuation := proc()
       args;
   end proc;

   search := proc( i::posint, n::posint, m::nonnegint, board::Array )
       local j, k, boards, a, used, unused;

       if ( i <= m ) then
           if ( i > 1 ) then
               used := convert( board[1..i-1], set );
               boards := [ seq( Array( board ), k=1..n-i+1 ) ];

               k := 1;
               for j from 1 to n
               do
                   if ( not j in used ) then
                       boards[k][i] := j;
                       k := k+1;
                   end if;
               end do;
           else
               boards := [ seq( Array( board ), k=1..n ) ];

               for j from 1 to n
               do
                   boards[j][i] := j;
               end do;
           end if;

           Threads:-Task:-Continue( continuation,
               Tasks = [ search, seq( [i+1, n, m, j], j in boards ) ] );
       else
           unused := { seq( j, j=1..n ) } minus convert( board[1..i-1], set );
           return completeBoardAndCheck( n, board, i, unused );
       end if;

       return NULL;
   end proc;

   ModuleApply := proc( n::posint, m::nonnegint )
       local board;

       board := Array( 1..n, datatype=integer[8] );

       Threads:-Task:-Start( search, 1, n, m, board );
   end proc;
end;

nQueens:=modulelocalcheckBoard&comma;completeBoardAndCheck&comma;search&comma;continuation&comma;subInit&semi;exportModuleApply&semi;end module

(1)

boards:=nQueens1&comma;0

boards:=1

(2)

nopsboards

1

(3)

boards:=nQueens2&comma;1

boards:=

(4)

nopsboards

0

(5)

boards:=nQueens3&comma;1

boards:=

(6)

nopsboards

0

(7)

boards:=nQueens4&comma;2

boards:=2413&comma;3142

(8)

nopsboards

2

(9)

boards:=nQueens5&comma;3

boards:=13524&comma;14253&comma;24135&comma;25314&comma;31425&comma;35241&comma;41352&comma;42531&comma;52413&comma;53142

(10)

nopsboards

10

(11)

boards:=nQueens6&comma;4

boards:=246135&comma;362514&comma;415263&comma;531642

(12)

nopsboards

4

(13)

boards:=nQueens7&comma;4&colon;

nopsboards

40

(14)

boards:=nQueens8&comma;4&colon;

nopsboards

92

(15)

See Also

examples,Task, Start, Task, Task Programming Model


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam