Generate unique elements in an array

In reply to dave_59:

@dave_59 - I am aware of the solution with the existing syntax. But the scenario there was different. At that time, I wanted a unique address value for ‘every’ new sequence item that was generated. So we choose to have a static queue that has one instance and is visible to all sequence items.

Now, I want unique elements in a multi-dimensional array ‘contained within a single’ sequence item. The unique constraint addresses this situation (but the above solution does not). How were you dealing with this situation upto now?

In reply to ssingh:

Looking at your original question, you can simply use the array shuffle method if all you want is a random ordering of an exhaustive set of values in an array;

class A;
bit [3:0] data[16]; // do not declare rand
rand int other_data;
function void pre_randomize;
  data.shuffle();
endfunction
function new;
  for(int i=0;i<data.size();i++) data[i]=i;
endfunction
endclass

Now a call to a_h.randomize() will give you a random shuffling of the array.

I don’t understand your last question. Can you give an example with all the data declarations and show what you would like to see as the set of values that randomize() should generate?

In reply to dave_59:

@dave_59 - If ‘data’ is not declared as rand, will pre_randomize be called? In the above case, we can shift the initialization and shuffle part to post_randomize.


class A;
rand bit [11:0] data_in [99:0];
constraint data_range { foreach(data[i]) data_in[i] inside {[0:500]}; }
endclass

I want to make sure that all the 100 elements in ‘data_in’ are unique 12 bit values.

In reply to ssingh:

‘data’ does not need to be declared as rand. pre_randomize() is called when you call randomize() on that object regardless of it having any rand variables. If the object being randomized contains a class variable referencing another class object, that class variable must be declared rand to call the pre_randomize() method of the other class.

In reply to dave_59:

Oops! Thanks! Please let me know about the solution to the problem.

In reply to ssingh:

I still don’t understand your last question. Can you give an example with all the data declarations and show what you would like to see as the set of values that randomize() should generate?

In reply to dave_59:

Please refer to the class declaration in post #6. I can use unique identifier now. I was just interested in the approach adopted earlier. I don’t want to do something like data_in[i] = i and then shuffle.

Following constraint will generate the unique values in array.

module unique_array;
class data_cl;
  rand bit [7:0] data[];
  constraint data_values { foreach(data[i]) 
                             foreach(data[j])
                               if(i != j) data[i] != data [j] ;} 
endclass

  data_cl cl_ob;

  initial
  begin
     cl_ob = new();
     cl_ob.data = new[20];
     assert(randomize(cl_ob));
     foreach(cl_ob.data[i])
      $display("%d",cl_ob.data[i]);
  end
endmodule

In reply to vishnuprasanth:

@vishnu - thanks

SSingh,
I see my previous solution using old SV-2005/9 syntax doesn’t fully meet your requirement, sorry spoke too fast perhaps. However when you say:

Since the order has to be random,
constraint data_values { foreach(data[i]) data[i+1] > data [i] ;}
is not something I want.

Why not use shuffle in post_randomize? Point is - you are asking the solver to go and do all the hardwork by writing some fancy expression to simply do:

  1. Fill all values from 0…15
  2. Shuffle the order

IMHO - this is bad for performance and an unnecessary overkill of available horse power. Sure Vishu’s code looks nice, but hopefully you agree it is not so intuitive. We generally recommend KISS principle (KISS principle - Wikipedia). Consider the below code - this would achieve what you intended in a simpler manner - relatively speaking. But - you do have a fancy code choice, thanks to Vishu!

constraint cst_ascend { foreach(data[i]) 
                               i > 0 -> 
                    (data[i] > data [i-1]) ;} 


  function void post_randomize;
    this.data.shuffle;
    $display("%p",this);
  endfunction : post_randomize

The above code scales nicely for 1 - to - SIZE-1 array size. Since it uses one foreach loop in constraints, tools will have it easy and is easier to read/maintain/explain to others.

Nice topic, BTW.

HTH
Ajeetha, CVC

In reply to ajeetha:

@ajeetha - Please refer to post #6. Now I don’t want to fill the values from 0 to 15. My range of values is now much bigger. I agree with you though.

In reply to ssingh:

Hi Dave,

Reading the above link for I tried to generate unique random numbers, but the following code doesn’t seem to work, dont know why?

package env_constants;
  localparam PDW_INDEX_BITS = 6;
  localparam DCD_INDEX_BITS = 3;
endpackage: env_constants


module dcd2pdw;

  import env_constants::*;

  class A;
     rand bit[PDW_INDEX_BITS-1:0] dcd2pdw[bit[DCD_INDEX_BITS-1:0]];
     constraint uniq {unique {dcd2pdw};}
  endclass:A

 initial begin
   A a;

   a = new();

   assert(a.randomize()) else begin
      $display("Randomization failure...");
   end

   $display("size of array: %d",a.dcd2pdw.size());
   foreach(a.dcd2pdw[i]) begin
     $display("PDW: %0d, at dcd_id: %0d",a.dcd2pdw[i],i);
   end 
 end

endmodule: dcd2pdw

Size of array is getting printed as: zero. What is the problem here ?

In reply to mseyunni:

You have declared dcd2pdw as an associative array whose index type is bit[DCD_INDEX_BITS-1:0]. You cannot randomize the size of an associative array. Did you mean to declare a fixed sized array?

rand bit[PDW_INDEX_BITS-1:0] dcd2pdw[DCD_INDEX_BITS-1:0];

In reply to dave_59:

Ok I have declared it as an associative array.
I actually need 2**DCD_INDEX_BITS as my array depth. But if I say [DCD_INDEX_BITS-1:0], the array depth would be 2 items only.

I also need to generate something like:

DCD PDW
0 3, 4, 5, 6, 8, 9, 49, 50
1 51, 63, 54, 45, 0, 1, 33, 11

So for each DCD I neeed to generate 8-pdw values, such that the PDW values for a DCD should not be repeated for any other DCD.

In reply to mseyunni:

rand bit[PDW_INDEX_BITS-1:0] dcd2pdw[2**DCD_INDEX_BITS];

In reply to dave_59:

Hi Dave,

Thank you. How can I generate 8-PDW values for each DCD such that no PDW value is repeated between two DCD’s ? (explained above).

Thanks in advance,
Madhu

In reply to mseyunni:

class A;
     bit [PDW_INDEX_BITS-1:0] used_dcd2pdw[$][2**DCD_INDEX_BITS];
     rand bit[PDW_INDEX_BITS-1:0] dcd2pdw[2**DCD_INDEX_BITS];
     constraint uniq {unique {dcd2pdw,used_dcd2pdw};}
     function void post_randomize;
	used_dcd2pdw.push_back(dcd2pdw);
     endfunction
   
endclass:A

The uniq constraint will eventually fail after so many calls to randomize(). I’ll leave it to you to figure out what to do when it does.

In reply to dave_59:

Hi Dave, i have a new question that is related but it is a little different. I haven’t found any solution in the forum so maybe you can help.
What i want is to generate an array with unique ranges. This is typically useful for addressing ranges that are not overlapping. What is the best and efficient way to do that?
In a paper of the DVCon from Mentor i have seen: http://proceedings.dvcon-europe.org/2014/presentations_and_papers/T5_1_paper.pdf

There is the following code:



rand bit [31:0] start_addr;
 rand bit [5:0] length;
 bit [31:0] start_end_addr_hash [bit[31:0]];
 constraint c { //Generate Non-Overlapping address ranges
    if (start_end_addr_hash.num()) {
       foreach (start_end_addr_hash *) {
             if (i >= length ) {
                !(start_addr inside {[i-length+1 : start_end_addr_hash [i]]});
             } else {
                 !(start_addr inside {[0 : start_end_addr_hash [i]]});
             }
       }
    }
  length == 6'h10;
 }
 ...
 start_end_addr_hash [start_addr] = start_addr + length - 1;


Is that the best way? Besides, it is not clear for me where i have to put the code.


 ...
 start_end_addr_hash [start_addr] = start_addr + length - 1;

Is it in post_randomization?

Another option is using only random variables. The advantage is that the user can define the start and length by himself for each non overlapping part ([i]notice that it uses a dynamic array and not only one value for start_address and lengt*h). I think the previous example doesn’t allow you to do that because is all coded in the hash iteratively.
Besides, if you do constraint “length[j]==1” then this code generates a vector with unique values in the start_address. That means, this somehow supersedes the codes above at cost of efficiency and memory.


rand bit [31:0] start_address[]; //notice that it is a dynamic array
rand bit [5:0] length[]; //dynamic array, to specify many different ranges

constraint c { 
start_address.size()==length.size();
if (start_address.size()) {
  foreach (start_address[j])  {//j is the chosen start address  
           // end_address [j] == start_address[j] + length[j] - 1;
    foreach (start_address[i])  {//i is the chosen start address 
       if (i!=j) {
            
            ///Constraint start_address (first byte of the buffer)
         if (start_address [j]  >=  length[i] ) {
           !(start_address[i] inside {[start_address [j] - length[i]+1 : start_address [j]+length[j]-1  ]}) ;
         } else {
           !(start_address[i] inside {[0 : start_address [j]+length[j]-1 ]});
         }
       } //end if i!=j
        
     } //end foreach i
   } //end foreach j
 }  //end if

} //end contraint c

What do you think about the implementations?

Thanks in advance

In reply to Jonathan_Alvarez:

In the paper’s example, the constraint about the length (i.e. length==6’h10) is just for demonstration, you can remove that to get the variable length you want. In reality length limitation constraints do exist. And yes you can add the following line in any procedural block after a successful randomization attempt (post_randomize is a good place).


  start_end_addr_hash [start_addr] = start_addr + length - 1; 

Back to the debate about performance, the introduced example (second one) shall generate all address ranges upfront. Although I am fan of late-randomization (randomize at the right time to take the DUT and testbench states into consideration), this is not just the problem I see with the second code. The second code has a nested foreach loops, this is a solver performance overkill, especially if size of the arrays are large enough. An interesting exercise is to measure the time the solver will solve the second example with array sizes of 100 and above.
The first code (code in the paper) kind of escape from this problem by only randomizing a single attempt at a time, then saving the start and end addresses in a hash table acting as a back-log/guiding reference for the next randomization attempt. Hence only one foreach is used in constraint, which still possess a performance bottleneck, however much faster than the second solution.

That said, if I were to write the code I would neither use the first nor the second solutions to solve the simple problem of non-overlapping address ranges. I would just remove the constraint completely and embrace the constraint with a checking loop like so.


class c;
  rand bit [31:0] start_addr;
  rand bit [5:0] length;
  bit [31:0] start_end_addr_hash [bit[31:0]];
endclass

...
c c1 = new;
... 
forever begin
  duplicate = 0;
  assert(c1.randomize());
  foreach (c1.start_end_addr_hash [i])
    if ((c1.start_addr <= c1.start_end_addr_hash [i]) && ((c1.start_addr + c1.length -1) >=i )) begin
      duplicate = 1;
      break;
    end
  if (!duplicate) break;
end
c1.start_end_addr_hash [c1.start_addr] = c1.start_addr + c1.length - 1;


The code above shall free the solver from complicated constraints allowing it to focus on the distribution. If we assumed an ideal solver distribution (e.g. BDD solver will be efficient to solve the simplified problem above), the third solution will be much faster than both solutions above (especially the second one).

Same stands true for the original question of this post; how to generate an array of random unique elements. Well you can also use procedural code and minimal solver work if possible, like so:


class dummy_c;
  randc bit [31:0] val;
endclass

class trans;
  rand bit [31:0] addrs [];

  function void post_randomize();
    dummy_c dc = new;
    foreach (addrs[i]) begin
      assert (dc.randomize);
      addrs[i] = dc.val;
    end
  endfunction
endclass

Thanks,
Ahmed Yehia

In reply to ayehia:

Thanks for your detailed answer. I agree with you. For long vectors your last solution has much better performance. Thanks for the insight in the constraint solver performance.

I have also noticed that if we would have been talking about a transaction item class, then the user can also create random variables and a flag (e.g. fullrandom_or_definedbyuser) so that he can assign their user-values in the post randomization phase. In that way, the user can decide if he want to find a new solution (using your last code) or test a previous solution defined (hardcoded) in the explicit part of the item randomization.

For example:


class c;
  rand bit [31:0] start_addr[];
  rand bit [5:0] length[];//size of 
  rand bit user_define_all_rand_members=0;
  bit [31:0] start_end_addr_hash [bit[31:0]];
  constraint c {
     length.size()==start_addr.size();
     soft user_define_all_rand_members==0; 
  } //end contraint c
endclass
 
...
c c1 = new;
... 
repeat(number_of_blocks2generate) begin
  forever begin
     duplicate = 0;
     assert(c1.randomize( ) with {
	           start_addr.size()==2;
	           user_define_all_rand_members==1;
	           start_addr = { 32'hFFFF, 32'h0000 };
	     } );//try to add a block of 2 ranges, you can define more
     //Start check of the random value
     foreach (c1.start_end_addr_hash [i]) begin
        foreach (c1.start_addr [j]) begin
            if ((c1.start_addr[j] <= c1.start_end_addr_hash [i]) && ((c1.start_addr[j] + c1.length[j] -1) >=i )) begin
                 duplicate = 1;
	         if (user_define_all_rand_members==1) begin
                    $fatal (0, "Randomization defined by user failed")   
                 end
                 break;
            end
            //ADD A NEW RANGE-ELEMENT START->END ADDRESS TO THE ASSOCIATIVE ARRAY
            if (!duplicate) begin
               c1.start_end_addr_hash [c1.start_addr[j]] = c1.start_addr[j] + c1.length[j] - 1;
            end
        end
        if (!duplicate) break;
    end//end for associative array
  end//end forever begin
end//end repeat number of blocks to add to the associative array

With this version, the user can still defines/direct the randomization with several ranges in one shot (using the with clause). These “with” constraints are checked by the forever loop without using the constraint solver, improving the performance.