At the beginning of the algorithm initialize a RingField struct for every independent field of the tensor.
| 169 | // At the beginning of the algorithm initialize a RingField struct for |
| 170 | // every independent field of the tensor. |
| 171 | bool RingReducer::RunAsyncParts() { |
| 172 | // This function orchestrates RingReduce actions on behalf of a |
| 173 | // single device. It is entered by a blockable thread that |
| 174 | // loops within it until all actions assigned to that device |
| 175 | // complete. Hence function local variables are accessible only by that |
| 176 | // one thread and do not require an explicit mutex. |
| 177 | rfv_.clear(); |
| 178 | rfv_.resize(group_size_ * num_subdivs_); |
| 179 | PCQueue ready_queue; |
| 180 | for (int chunk_idx = 0; chunk_idx < group_size_; ++chunk_idx) { |
| 181 | for (int subdiv_idx = 0; subdiv_idx < num_subdivs_; ++subdiv_idx) { |
| 182 | int rf_index = (chunk_idx * num_subdivs_) + subdiv_idx; |
| 183 | InitRingField(&rfv_[rf_index], chunk_idx, subdiv_idx, rf_index); |
| 184 | ready_queue.Enqueue(&rfv_[rf_index]); |
| 185 | } |
| 186 | } |
| 187 | const DeviceBase::GpuDeviceInfo* gpu_info = |
| 188 | col_ctx_->device->tensorflow_gpu_device_info(); |
| 189 | if (gpu_info) { |
| 190 | // Wait for all currently queued events on the CPU compute stream to |
| 191 | // complete before proceeding. The previous InitRingField calls allocated |
| 192 | // temp memory buffers that are not guaranteed to be valid (e.g. for RDMA |
| 193 | // write) unless we do. |
| 194 | profiler::TraceMe activity("WaitForQueuedEvents", |
| 195 | profiler::TraceMeLevel::kInfo); |
| 196 | Notification note; |
| 197 | Status s = gpu_info->default_context->ThenExecute( |
| 198 | col_ctx_->device, gpu_info->stream, [¬e]() { note.Notify(); }); |
| 199 | if (s.ok()) { |
| 200 | note.WaitForNotification(); |
| 201 | } else { |
| 202 | mutex_lock l(status_mu_); |
| 203 | status_ = |
| 204 | errors::Internal("Failed to dispatch ThenExecute in RingReducer"); |
| 205 | return false; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int field_done_count = 0; |
| 210 | int send_pending_count = 0; |
| 211 | int recv_pending_count = 0; |
| 212 | std::atomic<bool> aborted(false); |
| 213 | |
| 214 | { |
| 215 | profiler::TraceMe activity("Loop", profiler::TraceMeLevel::kInfo); |
| 216 | // Loop until all RingFields have advanced to completion. |
| 217 | while (field_done_count < rfv_.size()) { |
| 218 | VLOG(4) << FieldState(); |
| 219 | // Wait for a RingField to appear in the ready_queue. |
| 220 | RingField* rf = ready_queue.Dequeue(); |
| 221 | // Advance the RingField to its next action and execute, repeating |
| 222 | // until either an async action has been started or the RingField |
| 223 | // is done. |
| 224 | bool dispatched = false; // true if async action was initiated |
| 225 | do { |
| 226 | if (aborted) { |
| 227 | // Requeue this RingField to be counted off below. |
| 228 | ready_queue.Enqueue(rf); |
nothing calls this directly
no test coverage detected