## Stencil too heavy

General APL language issues

### Stencil too heavy

Dear All,
To solve a problem, i use stencil, very elegant.

With
`      rank ← 10      cut ← { ⊃,/ ×/ ⍵ (⌽⍵) (⊖⍵) (⌽⊖⍵) }      corner ← { ⍵< (⍳2×⍵) ∘.+ (⍳2×⍵) }      M ← cut corner rank`

i produce a base to find neighbors at the east of each one.

`      East ← { ×/ ⍵[2;2 3] }      possible ← East ⌺ 3 3 ⊣ M`

and i have WS Full with rank = 821, shape of M is (1642 1642).

But with a standard APL method :
`      possible ← { (1↑⍴M)↑[2] (1↓[2]⍵) × (¯1↓[2]⍵) } M`

the message WS Full come when rank=3898 and shape of M go up to (7796 7796).

Stencil is very elegant, but too greedy with wide matrice.

A chance ! at this point, rank=1,000 is enough.

Regards,
Yves
Yves

Posts: 21
Joined: Mon Nov 30, 2015 11:33 am

### Re: Stencil too heavy

If your computation is as simple as you described, you are better off using the standard APL solution.

If there is some benefit to considering the problem as a computation on 3-by-3 neighborhoods, then stencil is worthwhile. Stencil has special code for certain left operand functions and the special code can make a difference in performance. In this case, the applicable special code is:

`      ⊢ A←3 3⍴0 0 0 0 1 1 0 0 00 0 00 1 10 0 0      Est←{+/,A×⍵}`

For example:

`      M ← cut corner 400                                     cmpx'2=Est⌺3 3⊢M' 'East⌺3 3⊢M'  2=Est⌺3 3⊢M → 1.50E¯2 |      0%                                 East⌺3 3⊢M  → 4.55E0  | +30213% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕`

That is, 2=Est⌺3 3⊢M gives the same answer as East⌺3 3⊢M but is faster by a factor of 303.

`      M←1=?8000 8000⍴2      cmpx '2=Est⌺3 3⊢M'1.54E0`

I don't know what your workspace size is, but an 8000 8000 boolean matrix takes 8 MB and stencil with Est should work with a reasonable workspace size.
Roger|Dyalog

Posts: 125
Joined: Thu Jul 28, 2011 10:53 am

### Re: Stencil too heavy

Dear Roger,
Thank you for your light in APL.
Roger|Dyalog wrote:If your computation is as simple as you described, you are better off using the standard APL solution.
Of course, you're right.
But stencil is really elegant, and now i use it in handwrite, naturally.

i work in macOS with Daylog APL 64bits v16.0.30781 unicode edition
`      ⎕wa243902952`
impossible at this time to change it.

Roger|Dyalog wrote:Stencil has special code for certain left operand functions
How can we discover special code or special case ?

Your solution is very efficient. WS is Full with rank = 3845.

Thank You Roger,
Yves
Yves

Posts: 21
Joined: Mon Nov 30, 2015 11:33 am

### Re: Stencil too heavy

By default Dyalog APL sets MAXWS to 256MB in version 16.0 (with the exception of the Pi where it's still 64MB).

To set it to a different value (in this example I'm setting it to 1GB):
edit ~/.dyalog/dyalog.config

export MAXWS=1G

and restart APL .. you should now see ⎕wa return a value close to 1072754280.

Rather than 1G, you could use 1024M for the same size.

AndyS|Dyalog

Posts: 175
Joined: Tue May 12, 2009 6:06 pm

### Re: Stencil too heavy

AndyS|Dyalog wrote:[...] To set it to a different value (in this example I'm setting it to 1GB):
[...] close to 1072754280.
Rather than 1G, you could use 1024M for the same size.

Surely you mean 1GiB Andy?

But then if you did mean 1GB why 1024M? Wouldn't 1000M be nearer the mark?

IEC 60027-2 - ISO/IEC 80000

Phil Last

Posts: 466
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Stencil too heavy

Roger|Dyalog wrote:Stencil has special code for certain left operand functions
How can we discover special code or special case ?

It is our intention to document all the special codes, but this one may have slipped through the cracks for now. In any case, the special codes for stencil are:

Code: Select all
`  {⊢⍵}      {,⍵}      {⊂⍵}  {+/,⍵}  {∧/,⍵}    {∨/,⍵}    {=/,⍵}    {≠/,⍵}          {  +/,A×⍵}    {  +/⍪A×⍤2⊢⍵}  {C<+/,A×⍵}    {C<+/⍪A×⍤2⊢⍵}`

C: a single number or a variable whose value is a single number
A: a variable whose value is a rank-2 or -3 array

The comparison can be < ≤ ≥ > = ≠

odd window size; movement 1; matrix argument

You can use the cmpx (compare expression, from dfns workspace) to confirm the presence of special code. For example:

Code: Select all
`      M←1=?200 200⍴2      cmpx '{ +/,⍵}⌺3 3⊢M' '{⊢+/,⍵}⌺3 3⊢M'  { +/,⍵}⌺3 3⊢M → 6.25E¯5 |       0%                                 {⊢+/,⍵}⌺3 3⊢M → 1.10E¯1 | +175950% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕`

The extra ⊢ in the second expression defeats the special code recognizer. As a result the more general code is used and you can see the difference in execution time.
Roger|Dyalog

Posts: 125
Joined: Thu Jul 28, 2011 10:53 am

### Re: Stencil too heavy

Dear Roger, Andy & All,

Special codes are very intesresting.
Can you explain more about { +/ ⍪ A × ⍤2 ⊢⍵ } ?
i try it with
`      ⊢ A ← ? 5 5 ⍴ 97 7 8 3 32 8 1 4 46 8 8 1 63 9 3 2 73 9 4 2 4      { +/ ⍪ A × ⍤2 ⊢ ⍵ } ⌺ 3 3 ⊢ ARANK ERROR      {+/⍪A×⍤2⊢⍵}⌺3 3⊢A     ∧`
i am not sure to understand why this formula.

Regards,
Yves
Yves

Posts: 21
Joined: Mon Nov 30, 2015 11:33 am

### Re: Stencil too heavy

{+/⍪A×⍤2⊢⍵}⌺ is like {+/,A×⍵}⌺ but you get to do a whole bunch of matrices all at once. For example:

`      b←1=?97 113⍴5      A←?5 5⍴9      ⍴ {+/,A×⍤2⊢⍵}⌺5 5 ⊢b97 113      A3←?3 5 5⍴9      ⍴ {+/⍪A3×⍤2⊢⍵}⌺5 5 ⊢b97 113 3`
Roger|Dyalog

Posts: 125
Joined: Thu Jul 28, 2011 10:53 am