context.h 3.0 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

17
#include "paddle/fluid/lite/utils/any.h"
S
superjomn 已提交
18
#ifdef LITE_WITH_CUDA
Y
Yan Chunwei 已提交
19
#include "paddle/fluid/lite/cuda/blas.h"
S
superjomn 已提交
20 21
#include "paddle/fluid/lite/cuda/cuda_utils.h"
#endif
Y
Yan Chunwei 已提交
22 23 24 25
#ifdef LITE_WITH_X86
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/platform/device_context.h"
#endif
S
superjomn 已提交
26
#include <memory>
S
superjomn 已提交
27
#include <set>
S
superjomn 已提交
28
#include <vector>
T
tensor-tang 已提交
29 30
#include "paddle/fluid/lite/core/cpu_info.h"
#include "paddle/fluid/lite/core/lite_tensor.h"
S
superjomn 已提交
31
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
32 33 34 35

namespace paddle {
namespace lite {

36 37 38
struct HostContext {};

#ifdef LITE_WITH_ARM
T
tensor-tang 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

struct ARMContext {
 public:
  ARMContext();
  ARMContext(PowerMode mode, int threads);
  ARMContext(const ARMContext& ctx);

  ARMContext& operator=(const ARMContext& ctx);

  void SetRunMode(PowerMode mode, int threads);
  void SetCache(int l1size, int l2size, int l3size);
  void SetArch(ARMArch arch);
  void BindDev();

  PowerMode mode() const;
  int threads() const;
  ARMArch arch() const;

  template <typename T>
  T* workspace_data() {
    return workspace_.mutable_data<T>();
  }

  int l1_cache_size() const;
  int l2_cache_size() const;
  int l3_cache_size() const;
  bool ExtendWorkspace(DDimLite dims);

 private:
  // LITE_POWER_HIGH stands for using big cores,
  // LITE_POWER_LOW stands for using small core,
  // LITE_POWER_FULL stands for using all cores
  ARMArch arch_;
  PowerMode mode_;
  std::vector<int> active_ids_;
  TensorLite workspace_;
  int64_t count_{0};
};
77 78
#endif

S
superjomn 已提交
79 80 81 82 83 84 85 86
#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.
87
  std::shared_ptr<cuda::Blas<float>> blas_fp32;
S
superjomn 已提交
88 89 90 91 92 93 94 95 96 97

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

#ifdef LITE_WITH_X86
struct X86Context {
  // overall information
98

S
superjomn 已提交
99
  // kernel information
Y
Yan Chunwei 已提交
100 101 102 103

  // legacy info.
  std::unique_ptr<::paddle::platform::CPUDeviceContext> x86_device_context;
  std::unique_ptr<::paddle::framework::ExecutionContext> x86_execution_context;
S
superjomn 已提交
104 105 106 107 108 109 110
};
#endif

// Context for running a kernel.
// Holds the necessary resource and information.
class KernelContext {
 public:
111 112 113 114
  template <typename ContextT>
  ContextT& As() {
    if (!ctx_.valid()) {
      ctx_.set<ContextT>();
115
    }
116
    return *ctx_.get_mutable<ContextT>();
117 118 119
  }

 private:
120
  Any ctx_;
S
superjomn 已提交
121 122
};

S
superjomn 已提交
123 124
}  // namespace lite
}  // namespace paddle