Add, Map, Mul and Seq

December 23 2009 dohashi 1072

I realized the other day that I had not mentioned the Threads:-Add, Threads:-Map, Threads:-Mul and Threads:-Seq functions.  These are parallel implementations of the standard Maple functions, add, map, mul and seq.  They expect the same calling sequence and should return the same result.  Well, sort of, read on.

Given that we have parallel implementations of these functions, why aren't the parallel versions used by default?  The main reason is that the parallel functions require the given expression be thread safe.  The expression will be evaluated in parallel, thus if it is not thread safe we may not get the same result as the single threaded versions of the functions.  Thus you need to explicitly request the parallel versions.

In the future we hope to be able to use of the parallel implementations by default.  How?  If we had a code analysis tool capable of determining if an expression is thread safe, then we could automatically apply the parallel versions when we have a thread safe expression.  Such a code analysis tool will not be perfect, and for correctness we will need to make sure that it will only give false negitives, never false positives.    This will lead to situations where thread safe code is incorrectly identified as thread unsafe and the kernel will not be able to use that code in parallel.  Thus we are also thinking about adding a new option for procedures that would tell the kernel that the user had declared a procedure as thread safe.

This is probably going to be my last post until the new year, so I thought I'd leave you with a little exercise.  I recieved a bug report about the following code not working properly.  It turns out there is a bug in the code itself.  Can you see it?

> p2 := proc(A)
>   local i,j,k,l,m,n,fullL;
>   m,n := op(1,A);
>   fullL := [seq(i,i=1..n)];
>   add((-1)^k*Threads:-Add(mul(add(A[i,j], j in l), i=1..m),
>                  l in combinat:-choose(fullL,k)), k=1..n);
> end proc: > > M := Matrix(15,(i,j)->i+j): > > p2(M); Error, (in Threads:-Add) bad index into Matrix

If you identify the problem, can you fix it?

Please Wait...