Using sum() to Calculate the Sum of a 2D Array

I’d like to know how to calculate the sum of a 2d array using sum() method, or any other more efficient methods.

According to the RFM 2017 7.12.3 Array reduction methods, the second example:


logic [7:0] m [2][2] = '{ '{5, 10}, '{15, 20} };
int y;
y = m.sum with (item.sum with (item)); // y becomes 50 => 5+10+15+20

I should be able to get the sum-product this way. However, I was not able to compile my code using Incisive and QuestaSim. Does anyone know what went wrong?

Thanks!

In reply to amy992:

Please show a complete example.

In reply to dave_59:

Hi Dave,

Basically I just put everything there into a module


module array_2d_sum;

  logic [7:0] m [2][2] = '{ '{5, 10}, '{15, 20} };
  int y;

  y = m.sum with (item.sum with (item)); // y becomes 50 => 5+10+15+20

  initial begin
    $display("m is %p", m);
  end

endmodule

The compilation error in QuestaSim is Error: vlog-13069, pointing to this line y = m.sum with (item.sum with (item)); near “=”: syntax error, unexpected ‘=’.
The same line has another error message: vlog-13205, Syntax error found in the scope following ‘y’. Is there a missing ‘::’?

In reply to amy992:

The assignment to y needs to go inside the initial begin/end block. There is nothing wrong with the use of the sum() method.

In reply to dave_59:
I see. Thanks.

I was not able to put this into a constraint. Something like this:


rand int A[2][2];

constraint c1{
  (A.sum with (item.sum with (item))) == 0;
}


The code wouldn’t compile. Can you point out where I did wrong here?

In reply to amy992:

please see How to create a Minimal, Reproducible Example - Help Center - Stack Overflow

In reply to dave_59:

Hi Dave,

Basically, I would like to know how to put a constraint on the sum of a 2d array.



module p1;
  
  class c1;
    
    
    rand int m [2][2];
    
    rand int x;
    

    constraint c1{
      foreach(m[i,j])
        m[i][j] inside {[-1:1]};
    }
    

    constraint c1_2{
      x == m.sum with (item.sum with (item));
      
      //only works in in vcs 
      //x == m.sum();
    }
    

    constraint c1_3{
      x == 2;
      
    }
    
   
    
  endclass
  
  c1 c1_h;
  
  logic [7:0] n [2][2] = '{ '{5, 10}, '{15, 20} };
  int n_sum;
  
  initial begin
    c1_h = new();
    
    if(c1_h.randomize())
      $display("m is %p", c1_h.m);
    
    
    n_sum = n.sum with (item.sum with (item));
    
    //the sum of 2d array can be calculated this way
    $display("n_sum is %d", n_sum);
    
  end
  
endmodule

I am wondering why my code doesn’t work? Is there a more convenient way to put a constraint on the sum of a 2d array?

Thank you.

In reply to amy992:

You need to explain what “doesn’t work” means. When I run this, I get

# m is '{'{1, 1}, '{1, -1}}
# n_sum is          50

In reply to dave_59:

In reply to amy992:
You need to explain what “doesn’t work” means. When I run this, I get

# m is '{'{1, 1}, '{1, -1}}
# n_sum is          50

I meant the constraint c1_2 cannot be satisfied using Incisive or VCS.

Basically what I would like to do is to put a constraint on the sum of a 2D array. Not sure what’s the best way to construct the constraint.

In reply to amy992:

The expression in your constraint is taken straight from the LRM section you mention. If your tool has a problem with it, you’ll have to take it up with your tool vendor. This Mentor sponsored public forum is not for discussing tool specific issues.