Write code to execute task A in parallel 10 times, and call task B only after task A has finished executing 10 times

In reply to vk7715:
ON “Can you please explain why the values of i are 9 down to 0 when the loop is incrementing in an ascending fashion?


 task automatic t();
    for (int j = 0; j <= 9; j++) begin
      int k = j;
      // #10;
      fork
        begin
          $display("j %0d k %0d ",j,k);
          A(k);
        end
      join_none
    end
  endtask

Per rag123’s response “fork/join_none does not start any processes until the parent process suspends or terminates”. Thus, the A(k) are queued up by the simulator from 0 to 9
until t() ends and the initial reached the @ (done == 10’b11_1111_1111);
Following that, the simulator processes those A(k) tasks from the queue. I believe that would be up to the tool to decide how it wants to pull the job off the queu. Looks like it did in a LIFO fashion.

On But how would I truly know if all the 10 iterations executed in parallel at the same time? I ask this because since the task is called inside an initial block which takes a simulation time of 0, wouldn’t all 10 iterations complete in time 0 irrespective of the fork join_none? The tasks did complete at time 0. If you put a #3 just after the initial begin the time would be 3.

On task B, the formal needs to be defined as an int, it defaulted to a bit.
Made changes in Edit code - EDA Playground


module m;
  // event a;
  bit [9:0] done;
  task automatic A(input int i);
    $display("Task A. Time = %0t, i = %0d", $time, i);
    done[i] = 1;
  endtask

  task automatic B(int p);
   // int w; 
   // w=p; 
    $display("Task B. Time = %0t, p = %0d", $time, p);
  endtask

  /* task automatic t();
    for (int j = 0; j <= 1; j++) begin
      #10; 
      fork
        A(j);
      join_none
    end
  endtask */ 
  task automatic t();
    for (int j = 0; j <= 9; j++) begin
      int k = j;
      // #10;
      fork
        begin
          $display("j %0d k %0d ",j,k);
          A(k);
        end
      join_none
    end
  endtask

  initial begin
    #3; 
    t();
    $display("sent t()");
    // wait (done == 10'b00_0000_0011);
    @ (done == 10'b11_1111_1111);
    $display("done=%b", done);

    // @(a);
    B(2);
  end

endmodule
//
# sent t()
# j 10 k 9 
# Task A. Time = 3, i = 9
# j 10 k 8 
# Task A. Time = 3, i = 8
# j 10 k 7 
# Task A. Time = 3, i = 7
# j 10 k 6 
# Task A. Time = 3, i = 6
# j 10 k 5 
# Task A. Time = 3, i = 5
# j 10 k 4 
# Task A. Time = 3, i = 4
# j 10 k 3 
# Task A. Time = 3, i = 3
# j 10 k 2 
# Task A. Time = 3, i = 2
# j 10 k 1 
# Task A. Time = 3, i = 1
# j 10 k 0 
# Task A. Time = 3, i = 0
# done=1111111111
# Task B. Time = 3, p = 2
# exit