Bell – help with memory exhausted error

Discussions, advice, bug reports and much more about the "bach" environment.
Post Reply
sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Bell – help with memory exhausted error

Post by sydy » Sun Jul 10, 2022 10:22 am

Hi,

In the code cited afterwards I'm getting a "memory exhausted" error on line 28. Can't figure out why. I tried to wrap what ever I could ...
Any help would be appreciated.

Code: Select all

$subrollTOjitmatrix =($subroll->(
  $sub_rll = sort(left(flat($subroll,1),-1));

  ## checking the list for changes
  $addrs;
  for $i $a in 1 ... length($sub_rll) collect(
    $current_onset = ($sub_rll:($i:(1 1)));
    $previous_onset = ($sub_rll:(($i-1):(1 1)));
    if $current_onset != $previous_onset then $addrs _= $a
    );

    ## sorting by regrouping all notes with the same onset
    $grouped = for $i $ad in 1 ... length($addrs) collect(
      $index_start = ($addrs:$i);
      $index_end = ($addrs:($i+1))-1 ;
      if $ad < length($addrs) then
      $note = [$sub_rll:[[$index_start $index_end]]]
      else [$sub_rll:[[($index_start -1)]]]
      );
      ## get the y-dimension of the plane by the chord with the biggest number of notes
      ## get the x-dimension of the plane by accumulating the length of the subsets
      $grouped_len = length($grouped);
      $xdim = $grouped_len ;
      $ydim;
      for $i in 1 ... $grouped_len collect(
        $ydim _= length(flat(($grouped:($i)),1))
        );
        $ydim = maximum($ydim);

        ## avoid empty outputs
        if $xdim != 0 then(
          ## set the dimension of the plane
          directout(2,'dim' $xdim $ydim);

          ## fill the matrix
          for $i in 1 ... $grouped_len do(
            $chord = flat(($grouped:$i),1);
            $chord_len = length($chord);

          for $f in 1 ... $chord_len do(

            $freq = mc2f(($chord:($f:(1 2 1)))) ;
            $dur = ($chord:($f:(1 2 2)));
            $velocity = ($chord:($f:(1 2 3))) / 127.; ##
            directout(1, 'setcell2d' ($i-1) ($f-1) $freq $dur $velocity)
            )
          );
          directout(2,'bang') ## bang the jitter matrix
        )
 )
)

andreaagostini
Posts: 209
Joined: Fri Dec 03, 2010 1:51 pm

Re: Bell – help with memory exhausted error

Post by andreaagostini » Mon Jul 11, 2022 7:45 am

Hi Micha,

Yep, this one looks bad. Interestingly, and unexpectedly, I found out that removing parentheses in this case helps. How did I found out? By experimenting and seeing that, as I removed parentheses, the "memory exhausted" error moved downwards. Anyway, the only solution I found is to split your function in two. Didn't test the code, so there could be mistakes, but this is the idea:

Code: Select all

$helperFunc = ( $xdim, $ydim, $grouped_len, $grouped
  ->(
    ## avoid empty outputs
    if $xdim != 0 then (
      ## set the dimension of the plane
      directout(2,'dim' $xdim $ydim);

      ## fill the matrix
      for $i in 1 ... $grouped_len do(
        $chord = flat($grouped:$i,1);
        $chord_len = length($chord);

      for $f in 1 ... $chord_len do(

        $freq = mc2f($chord:$f:(1 2 1)) ;
        $dur = ($chord:($f:(1 2 2)));
        $velocity = ($chord:$f:(1 2 3)) / 127.; ##
        directout(1, 'setcell2d' ($i-1) ($f-1) $freq $dur $velocity)
        )
      );
      directout(2,'bang') ## bang the jitter matrix
    )
    )
  );


$subrollTOjitmatrix =($subroll -^ $helperFunc ->(
  $sub_rll = sort(left(flat($subroll,1),-1));

  ## checking the list for changes
  $addrs;
  for $i $a in 1 ... length($sub_rll) collect(
    $current_onset = $sub_rll:$i:(1 1);
    $previous_onset = $sub_rll:(($i-1):(1 1));
    if $current_onset != $previous_onset then $addrs _= $a
    );

    ## sorting by regrouping all notes with the same onset
    $grouped = for $i $ad in 1 ... length($addrs) collect(
      $index_start = $addrs:$i;
      $index_end = ($addrs:($i+1))-1 ;
      if $ad < length($addrs) then
      $note = [$sub_rll:[[$index_start $index_end]]]
      else [$sub_rll:[[$index_start -1]]]
      );
      ## get the y-dimension of the plane by the chord with the biggest number of notes
      ## get the x-dimension of the plane by accumulating the length of the subsets
      $grouped_len = length($grouped);
      $xdim = $grouped_len ;
      $ydim;
      for $i in 1 ... $grouped_len collect(
        $ydim _= length(flat($grouped:$i,1))
        );
        $ydim = maximum($ydim);

        $helperFunc($xdim, $ydim, $grouped_len, $grouped)
 )
)
Now listen, I promise that starting from September I'll put myself to work upon the new bell parser. I can't promise you anything, but I hope I'll sort it out...

andrea

sydy
Posts: 156
Joined: Tue Mar 21, 2017 1:34 pm

Re: Bell – help with memory exhausted error

Post by sydy » Mon Jul 11, 2022 10:40 am

Dear Andrea,

Many thanks! This is, again, a good example about the lifting option. Didn't think about it.
The memory exhausted problems occur normally only when I do "longer" snippets of code inside one routine and it looks like using the lifting option and therefore splitting up the routine into a subroutine might help. So for now I might probably just ask in some rare occasion where I don't know how to handle to bug.

BUT Yes, it would be great to have the new parser – also because it's complex to explain this little problem to others that aren't familiar with it.
I understand that writing the parser is a complex and time eating problem. So here's my motivational speech as enduser:
The more I use bell the more I'm convinced by the role it can play inside max. Bell is more than a tool for bach. I actually use it for nearly everything related to data transformation inside max. Whenever the algorithm gets complex its far more handy and you can read yourself after some months in a much more understandable way.
Who would use a cumbersome zl.objects-routine if there's bell around the corner? Want to use Javascript instead? It isn't in the priority thread – so that's no option. For now bell solves many problems I had inside max – way beyond the world of bach. I'm very grateful for it!

I wish you good luck and thank you for your great work in the meantime.

Post Reply