Typically whenever we require to spawn threads using fork join_none and a for loop, we use "automatic int". This is required to pass on the correct value into the threads. Otherwise, the last value in the loop is passed on to all the threads.
What if an object is passed instead of a data_type like int?
In the below code, we have 3 scenarios
1. Using an array of objects and automatic int whilst spawning threads.
2. Using a queue
3. Not having 'automatic int' to guide the array.
CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | //==================================================// // CLASS : DATA_OBJ // Description : // containts data_id and data // //==================================================// class data_obj; int data_id; string data; //==================================================// // load_val // loads data_id and data //==================================================// function void load_val ( int id, string val); data_id = id; data = val; endfunction endclass //==================================================// // TEST_CLASS // Description: // Contains and array and queue // Intention is to check the automatic function in // SV class //==================================================// class test_class; data_obj obj[3]; data_obj obj_q[$]; //==================================================// // LOAD_OBJ // Fills array and queue with data_obj objects //==================================================// function void load_obj(); for(int i=0;i <3;i++) begin obj[i] = new; obj[i].load_val(i,"pavan"); obj_q.push_back(obj[i]); end endfunction //==================================================// // SPAWN // Uses automatic int approach to load object into // print_data function // TRY : Try replacing 'j' with 'i' //==================================================// function void spawn(); for(int i=0; i<3;i++) begin automatic int j=i; fork print_data(obj[j]); // Replace J with I and check join_none end endfunction //==================================================// // POP_SPAWN // Simply pops the data into the print_data function //==================================================// function void pop_spawn(); for(int i=0; i<3;i++) begin fork print_data(obj_q.pop_front()); join_none end endfunction //==================================================// // Print_data // Prints the data //==================================================// function void print_data(data_obj obj); $display("OBJ ID:%0d VAL:%0s",obj.data_id,obj.data); endfunction endclass //==================================================// // MODULE: TOP //==================================================// module top; test_class test_c; initial begin test_c = new; test_c.load_obj(); test_c.spawn(); // Spawn threads using automatic int approach test_c.pop_spawn(); // Spawn threads using queue pop approach #100ns; end endmodule: top |
RESULTS:
1. Using an array of objects and automatic int whilst spawning threads.
Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Apr 1 22:21 2021
OBJ ID:0 VAL:pavan
OBJ ID:1 VAL:pavan
OBJ ID:2 VAL:pavan
2. Using a queue
Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Apr 1 22:23 2021
OBJ ID:0 VAL:pavan
OBJ ID:1 VAL:pavan
OBJ ID:2 VAL:pavan
3. Not having 'automatic int' to guide the array.
Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Apr 1 22:24 2021
Error-[NOA] Null object access
testbench.sv, 79
The object at dereference depth 0 is being used before it was
constructed/allocated.
Please make sure that the object is allocated before using it.
#0 in \test_class::print_data at testbench.sv:79
#1 in \test_class::spawn at testbench.sv:57
#2 in top at testbench.sv:95
#3 in top
No comments:
Post a Comment