Skip to content
Mahrud Sayrafi edited this page Mar 12, 2021 · 1 revision

title: Parallelization permalink: wiki/Parallelization/ layout: wiki

Notes on how to use parallel code in Macaulay 2:

Important: parallel functionality with the task system is available only with version number at least 1.4.0.1.

The task system schedules functions and inputs to run on a preset number of threads. The number of threads to be used may be changed as follows.

   i1 : allowableThreads
   o1 = 2
   i2 : allowableThreads = 8
   o2 = 8

To run a function in another thread the keyword schedule is used as in the following example:

   i1 : kk = ZZ/32003
   o1 = kk
   o1 : QuotientRing
   i2 : R1 = kk[vars(0..39),MonomialSize=>8];
   i3 : J1 = ideal"b2de+bd2f+a2dg+abdh+ad2i+b2cj+bc2k+bcdl+a2cm+abcn+ac2o+acdp,
              b2dq+bd2r+a2ds+abdt+ad2u+b2cv+bc2w+bcdx+a2cy+abcz+ac2A+acdB,
              b2dC+bd2D+a2dE+abdF+ad2G+b2cH+bc2I+bcdJ+a2cK+abcL+ac2M+acdN";
   o3 : Ideal of R1
   i4 : J1
                2         2     2                   2     2         2             
   o4 = ideal (b d*e + b*d f + a d*g + a*b*d*h + a*d i + b c*j + b*c k + b*c*d*l +
        ---------------------------------------------------------------------------
         2                   2              2         2     2                   2 
        a c*m + a*b*c*n + a*c o + a*c*d*p, b d*q + b*d r + a d*s + a*b*d*t + a*d u
        ---------------------------------------------------------------------------
           2         2               2                   2              2     
        + b c*v + b*c w + b*c*d*x + a c*y + a*b*c*z + a*c A + a*c*d*B, b d*C +
        ---------------------------------------------------------------------------
           2     2                   2     2         2               2             
        b*d D + a d*E + a*b*d*F + a*d G + b c*H + b*c I + b*c*d*J + a c*K + a*b*c*L
        ---------------------------------------------------------------------------
             2
        + a*c M + a*c*d*N)
   o4 : Ideal of R1
   i6 : dogb = (I) -> () -> (numColumns gens gb I)
   o6 = dogb
   o6 : FunctionClosure
   i7 : f1 = dogb I1
   o7 = f1
   o7 : FunctionClosure
   i8 : schedule f1
   o8 = <<task, running>>
   o8 : Thread

Note that it returns a task, not the result itself. As long as the task is running, the result is inaccessible.

   i10 : o8
   o10 = <<task, running>>
   o10 : Thread
   i12 : o8
   o12 = <<task, result available, task done>>
   o12 : Thread

To retrieve the result:

   i13 : taskResult(o8)
   o13 = 44

It is possible to make a task without automatically executing it.

   i14 : createTask(f1)
   o14 = <<task, created>>
   o14 : Thread
   i16 : schedule o14
   o16 = <<task, created>>
   o16 : Thread
   i17 : o16
   o17 = <<task, result available, task done>>
   o17 : Thread

As a simple example of starting one task when another finishes:

   i1 : f=()->print "A"
   o1 = f
   o1 : FunctionClosure
   i3 : g:=()->print "B"
   o3 = {*Function[stdio:3:6-3:8]*}
   o3 : FunctionClosure
   i7 : F=createTask(f)
   o7 = <<task, created>>
   o7 : Thread
   i8 : G=createTask(g)
   o8 = <<task, created>>
   o8 : Thread
   i9 : addStartTask(F,G)
   i10 : schedule(F)
   A
   B
   o10 = <<task, result available, task done>>
   o10 : Thread

There is also functionality to allow F to cancel G (if it is already running) when F finishes. This works by triggering an interrupt exception.

   addCancelTask(onFinishTask,taskToCancel)

There is also functionality to allow you to schedule a function such that it will not run until all dependencies are executed. You may do this using

   addDependencyTask(taskToRun,dependency)

Using these functions essentially any parallel functionality needed can be created. [See Parallel_Toolbox for a start --Tomka 09:18, 1 March 2011 (UTC)] Low level C api functionality using the same scheduler also exists in the Macaulay2/system directory. It works essentially the same way as the Macaulay2 interface.

Warning: Access to external libraries such as singular, etc, may not currently be thread safe.

Return to the CC2010 projects page Return to the main CC2010 page

Clone this wiki locally