Previous | Next --- Slide 9 of 92
Back to Lecture Thumbnails
timothyyeo

I have a question on the code above. Specifically, the following for loop for thread 0 computes global diff.

for (int i = 1; i < get_num_threads() -1; i++) { recv(&remote_diffm, sizeof(float), i, MSG_ID_DIFF); my_diff += remote_diff; }

Why do we need to iterate through i = 1 to i < get_num_threads() - 1 instead of i < get_num_thread()?

sagoyal

@timothyyeo hmm I'm also confused now about why we don't loop over the last thread when we are waiting for thread 0 to receive messages. In all other cases it seems we use the condition that tid != get_num_threads() - 1, just to double check that with the last thread we don't try to send information to the next thread (ie. one that doesn't exist). I'm not sure why edge case this is preventing. Perhaps a TA can comment.

orz

Same confusion

kevtan

I think this has to be a typo and @timothyyeo is right. The code should either keep the < but change get_num_threads() - 1 to get_num_threads() or change the < into <=. As written, thread 0 will wait for get_num_threads() - 2 different messages, which would mean it's fundamentally missing a partial diff.

weimin

Is it because thread 0 is doing the aggregation so we start from 1 and then we are 0 indexed so we need to subtract 1 from get_num_threads()?

Please log in to leave a comment.