Fork join_none inside for loop

In reply to ssubramaniam1990@gmail.com:

Thanks Dave and Subramaniam. All 3 cases were helpful.

Thanks Dave and Subramaniam the concept of fork join_none was very useful.

In reply to dave_59:

Hi Dave ,
For The Following Code


for( int j = 1; j <= 3; ++j )
 fork

 begin
  
  automatic int m = j; // the value of m is undetermined
  #k $write( "%0d", k );
 
 end

 join_none

The LRM Says the Value of m is undetermined . Why is That so ?

In reply to Etrx91:

This is an error in the LRM. See 0004176: erroneous comment in example of fork-end block? - Accellera Mantis

hello Dave sir, i got the concept what you have explained above but I have a small doubt
I have implemented the following code

module fork_test;
initial 
    begin
    for (int j=0; j<3; j++)
        begin
        automatic int k=j;//(1)
        fork
         //automatic int k=j;//(2)
           $display(k,$time );
        join_none
        end
    end
endmodule

I got the result i=3 thrice … i would like to know as I have put condition on i in for loop that it should be less than 3 still why its coming i =3.
another thing is when i implements number (1) statement it display result 2,1,0 why in reverse order? while for(2) it comes in order 0,1,2. I have run this code in EDA tool cadence incisive 15.20

thanks and regards

In reply to suman_vip:

I also ran this code on EDA playground and did not see the results you are getting. However, the order the $display statements execute is indeterminate.

There is no “i” in your example. But if you meant you were displaying “j” inside the fork/join_none loops, then j would be 3 since the for loop would terminate before any statements within the fork/join_none starts executing. That’s the point of giving each iteration of the for loop a local copy of k.

In reply to dave_59:

sorry sir , my mistake it was “j” .thanku sir

for(j=0;j<3;j++)
as per i understand , for loop will be suspended whenever j exceeds 2 ie if j=3 loop suspended but if i will display j it will show 3 is it so sir.

In reply to dave_59:

Hello Dave,
While going through this informative post. I had a question related to lifetime of control variable of for-loop.

initial
begin
  for(int j=1; j<=3; j++)
  begin
    fork
      automatic int k = j;
      #k $write($time,"k=%0d\n",k);
      begin
        automatic int m =j;
        $display("m=%0d",m);
      end
    join_none  
  end
#100;
end

I want to understand the lifetime of j in this example. for-loop execution will get over at time zero and it will schedule 6 process which will spawn once simulator hit #100.

In above example j is available(m=j) irrespective of for-loop execution is over. Does that means j lifetime is also dependent on the process which are schedule while for-loop is executing ?

In reply to gauravsi:

The lifetime of any automatic variable ends when the life of its block and all its nested blocks ends.

In reply to dave_59:

Thanks Dave !!

In reply to dave_59:

Hi Dave,

I have a question in mind, how does this piece of code or logic suitable for clock-based and event based simulations.

I am particularly looking for to understand clock-based simulation on above explained code by you.

Can you give some information.

sreeni.

In reply to nivas:

It’s hard to answer your question because this is a long thread of responses and there’s no clock in any of these examples. Perhaps you should try asking as a new question.

In reply to dave_59:

I have a doubt :

module fork_test1;
  initial begin
    for (int j=0; j<3; j++)
      begin
        #1;
        fork

          $display(j);

        join_none

      end
  end
endmodule:fork_test1

the output for the above code is :

1
2
3

Why is that> Why it is not printing 0?

In reply to SanjanaDhiran:

https://verificationacademy.com/forums/systemverilog/fork-joinnone-delay#reply-93311

What is the difference between the following 2 codes:
code 1:


for (int i = 0; i<3;i=i+1) begin
    fork 
      automatic int k = i;
      $display(k);
   join_none
end

Code 2:


for (int i = 0; i<3;i=i+1) begin
    automatic int k = i;
    fork 
      $display(k);
   join_none
end

In reply to SanjanaDhiran:

There is no functional difference between the two codes you show. An instance of k gets created for each iteration of the for loop, and the lifetime of each k ends when the statement containing the $display finishes.

The only difference would be seen if there were more code inside the begin/end block. In code 1, the visibility of k is limited to inside the fork/join_none block.

In reply to dave_59:

Hello Dave, I modified the code. Now, send is not automatic .why it still displays driving 0,1,2… instead of driving 15,15,15,15…?
Thanks


module test;

  initial begin
    for(int i = 0; i < 16; i++)
      begin
        fork 
	    automatic int index =i  ;	    
          send(index);
	join_none 
      end
      wait fork;
  end
   
        function   send(  int j);

          $display("driving  %0d %t" , j, $realtime);
   endfunction /

        
endmodule


In reply to peter:

Because sand() is a function executing with no delays. In theory it’s possible for an iteration of a process to call send() and then switch to another process before executing the $display statement. But in practice, you will never see that happen on a single core simulation.

In reply to dave_59:

Hi Dave, sixteen send function should be executed parallelly according to the spec. If the
result shows 15,15,15,15… The result should satisfy the spec?

You mention that it’s possible for an iteration of a process to call send() and then switch to another process before executing the $display statement. it seems that it is different with the spec, because send is running sequential instead of parallel. Would you explain more about never see that happen on a single core simulation?

Thank you!!

In reply to peter:

15,15,15, … is a valid result. See Section 4.7 Nondeterminism