context.h 3.2 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
S
superjomn 已提交
16
#include <paddle/fluid/lite/cuda/blas.h>
S
superjomn 已提交
17 18
#include <memory>
#include <vector>
S
superjomn 已提交
19
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
20

S
superjomn 已提交
21 22 23 24
#ifdef PADDLE_WITH_CUDA
#include "paddle/fluid/lite/cuda/cuda_utils.h"
#endif

S
superjomn 已提交
25 26 27 28 29 30 31 32
namespace paddle {
namespace lite {

template <TargetType Target>
class Context {
 public:
  using target_wrapper_t = TargetWrapper<Target>;
  using stream_t = typename TargetWrapper<Target>::stream_t;
S
update  
superjomn 已提交
33
  using event_t = typename TargetWrapper<Target>::event_t;
S
superjomn 已提交
34 35 36 37 38 39 40 41 42 43

  Context() = default;
  Context(int device_id, stream_t compute_stream, stream_t data_stream)
      : device_id_(device_id),
        compute_stream_(compute_stream),
        data_stream_(data_stream) {}

  void SetDeviceId(int device_id) { device_id_ = device_id; }
  void SetComputeStream(stream_t x) { compute_stream_ = x; }
  void SetDataStream(stream_t x) { data_stream_ = x; }
S
update  
superjomn 已提交
44 45 46
  void SetDependEvents(const std::vector<event_t>& events) {
    depend_events_ = events;
  }
S
superjomn 已提交
47 48 49 50

  int device_id() const { return device_id_; }
  stream_t compute_stream() const { return compute_stream_; }
  stream_t data_stream() const { return data_stream_; }
S
update  
superjomn 已提交
51
  const std::vector<event_t>& depend_events() const { return depend_events_; }
S
superjomn 已提交
52 53

 private:
S
update  
superjomn 已提交
54
  int device_id_{0};
S
superjomn 已提交
55 56
  stream_t compute_stream_;
  stream_t data_stream_;
S
update  
superjomn 已提交
57
  std::vector<event_t> depend_events_;
S
superjomn 已提交
58 59 60 61 62 63 64 65 66 67 68
};

class OpContext final {
 public:
  template <TargetType Target>
  using target_ptr_t = std::unique_ptr<Context<Target>>;

  // @param target valid target.
  explicit OpContext(TargetType target)
      : targets_(std::vector<TargetType>({target})) {}
  // @param target valid target.
S
update  
superjomn 已提交
69 70
  explicit OpContext(const std::vector<TargetType>& target)
      : targets_(target) {}
S
superjomn 已提交
71 72 73 74 75 76 77 78 79 80 81 82

  const std::vector<TargetType>& target() const { return targets_; }

  template <TargetType Target>
  target_ptr_t<Target> CreateContext() {
    return target_ptr_t<Target>(new Context<Target>);
  }

 private:
  std::vector<TargetType> targets_;
};

S
superjomn 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#ifdef LITE_WITH_CUDA
// Only works with CUDA kernels.
struct CUDAContext {
  // overall information
  cudaStream_t exec_stream;
  cudaStream_t io_stream;

  // not thread-safe, should allocate for each thread.
  std::shared_ptr<cuda::Blas<float>> bias_fp32;

  // kernel information
  std::vector<cudaEvent_t> input_events;
  std::vector<cudaEvent_t> output_events;
};
#endif

#ifdef LITE_WITH_X86
struct X86Context {
  // overall information
  // kernel information
};
#endif

// Context for running a kernel.
// Holds the necessary resource and information.
class KernelContext {
 public:
#ifdef LITE_WITH_CUDA
  CUDAContext cuda_ctx;
#endif

#ifdef LITE_WITH_X86
  X86Context x86_ctx;
#endif
};

S
superjomn 已提交
119 120
}  // namespace lite
}  // namespace paddle