context.h 2.1 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>
S
superjomn 已提交
29
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
30 31 32 33

namespace paddle {
namespace lite {

34 35 36 37 38 39
struct HostContext {};

#ifdef LITE_WITH_ARM
struct ARMContext {};
#endif

S
superjomn 已提交
40 41 42 43 44 45 46 47
#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.
48
  std::shared_ptr<cuda::Blas<float>> blas_fp32;
S
superjomn 已提交
49 50 51 52 53 54 55 56 57 58

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

#ifdef LITE_WITH_X86
struct X86Context {
  // overall information
59

S
superjomn 已提交
60
  // kernel information
Y
Yan Chunwei 已提交
61 62 63 64

  // legacy info.
  std::unique_ptr<::paddle::platform::CPUDeviceContext> x86_device_context;
  std::unique_ptr<::paddle::framework::ExecutionContext> x86_execution_context;
S
superjomn 已提交
65 66 67 68 69 70 71
};
#endif

// Context for running a kernel.
// Holds the necessary resource and information.
class KernelContext {
 public:
72 73 74 75
  template <typename ContextT>
  ContextT& As() {
    if (!ctx_.valid()) {
      ctx_.set<ContextT>();
76
    }
77
    return *ctx_.get_mutable<ContextT>();
78 79 80
  }

 private:
81
  Any ctx_;
S
superjomn 已提交
82 83
};

S
superjomn 已提交
84 85
}  // namespace lite
}  // namespace paddle