Home | History | Annotate | Download | only in gpu
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/compiler/xla/service/gpu/conditional_thunk.h"
     17 
     18 #include "tensorflow/compiler/xla/ptr_util.h"
     19 #include "tensorflow/compiler/xla/util.h"
     20 #include "tensorflow/core/lib/core/errors.h"
     21 
     22 namespace xla {
     23 namespace gpu {
     24 
     25 ConditionalThunk::ConditionalThunk(
     26     const BufferAllocation::Slice& predicate_buffer_index,
     27     const BufferAllocation::Slice& true_operand_buffer_index,
     28     const BufferAllocation::Slice& false_operand_buffer_index,
     29     ThunkSequence true_thunk_sequence, ThunkSequence false_thunk_sequence,
     30     const HloInstruction* hlo)
     31     : Thunk(Kind::kConditional, hlo),
     32       predicate_buffer_index_(predicate_buffer_index),
     33       true_operand_buffer_index_(true_operand_buffer_index),
     34       false_operand_buffer_index_(false_operand_buffer_index),
     35       true_thunk_(std::move(true_thunk_sequence), hlo),
     36       false_thunk_(std::move(false_thunk_sequence), hlo) {}
     37 
     38 Status ConditionalThunk::Initialize(const GpuExecutable& executable) {
     39   TF_RETURN_IF_ERROR(true_thunk_.Initialize(executable));
     40   TF_RETURN_IF_ERROR(false_thunk_.Initialize(executable));
     41   return Status::OK();
     42 }
     43 
     44 Status ConditionalThunk::ExecuteOnStream(
     45     const BufferAllocations& buffer_allocations,
     46     perftools::gputools::Stream* stream) {
     47   // Copy the predicate value from device.
     48   bool predicate;
     49   perftools::gputools::DeviceMemoryBase predicate_address =
     50       buffer_allocations.GetDeviceAddress(predicate_buffer_index_);
     51   stream->ThenMemcpy(&predicate, predicate_address, sizeof(bool));
     52 
     53   Status block_status = stream->BlockHostUntilDone();
     54   if (!block_status.ok()) {
     55     return InternalError("Failed to retrieve predicate value on stream %p: %s.",
     56                          stream, block_status.error_message().c_str());
     57   }
     58 
     59   // Execute the true or the false computation depending on the value of the
     60   // predicate.
     61   if (predicate) {
     62     TF_RETURN_IF_ERROR(true_thunk_.ExecuteOnStream(buffer_allocations, stream));
     63   } else {
     64     TF_RETURN_IF_ERROR(
     65         false_thunk_.ExecuteOnStream(buffer_allocations, stream));
     66   }
     67 
     68   return Status::OK();
     69 }
     70 
     71 }  // namespace gpu
     72 }  // namespace xla
     73