How to get number of elements of associative array having 2 dimensional index?

In below code, comp_dev_id is a associative array having 2 dimensional index. I want to get total number of elements of this array.
If I use comp_dev_id.num() then it returns me number of first index(dimension) only, not the sum of both dimension.


class base;
  int unsigned num_components = 3;
  int unsigned num_dev[3];
  bit comp_dev_id[int unsigned][int unsigned];

  function void rand_comp_dev_id(input int unsigned total_dev_num = 0, output int unsigned component_id, output int unsigned device_id);
    int unsigned comp_id;
    int unsigned dev_id;
    forever begin
      void'(std :: randomize(comp_id) with{comp_id inside{[0 : num_components - 1]};}); 
      void'(std :: randomize(dev_id) with{dev_id inside{[0 : num_dev[comp_id] - 1]};}); 
      if (comp_dev_id[comp_id][dev_id] == 1'b0) begin
        comp_dev_id[comp_id][dev_id] = 1'b1;
        component_id = comp_id;
        device_id = dev_id;
        break;
      end
    end
    $display("%0d num reached", comp_dev_id.num());
  endfunction : rand_comp_dev_id
endclass


module top();
  base B;
  int unsigned comp_id;
  int unsigned dev_id;
  initial begin
    B = new();
    B.num_dev[0] = 3;
    B.num_dev[1] = 2;
    B.num_dev[2] = 1;
    repeat(6) begin
      B.rand_comp_dev_id(6, comp_id, dev_id);
    end
  end
endmodule

//Output:
//1 num reached  
//2 num reached
//2 num reached //here I expected 3
//3 num reached //here I expected 4
//3 num reached //here I expected 5
//3 num reached //here I expected 6

In reply to Sagar Shah:

SystemVerilog does not really have multi-dimensional arrays; it only has one-dimensional arrays of arrays. So the num method only works on one dimension at a time.

What you want is

    $display("%0d num reached", comp_dev_id.sum(array) with (array.num()) );

In reply to dave_59:

Questa-sim gives me expected result, but VCS-2016.06 gives me following error,
Error-[XMRE] Cross-module reference resolution error
Error found while trying to resolve cross-module reference.
Originating package ‘$unit’.
Source info: array.num()

In reply to dave_59:

Thanks Dave :)

In reply to Sagar Shah:

In reply to dave_59:
Questa-sim gives me expected result, but VCS-2016.06 gives me following error,
Error-[XMRE] Cross-module reference resolution error
Error found while trying to resolve cross-module reference.
Originating package ‘$unit’.
Source info: array.num()

In reply to Sagar Shah:

We can use following code to get size of associative array having 2 dimensional index.


``` verilog

  function int unsigned get_size();
    foreach (comp_dev_id[i])
      get_size += comp_dev_id[i].num();
  endfunction