context.h 1.8 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
S
superjomn 已提交
19
#include <paddle/fluid/lite/cuda/blas.h>
S
superjomn 已提交
20 21
#include "paddle/fluid/lite/cuda/cuda_utils.h"
#endif
S
superjomn 已提交
22
#include <memory>
S
superjomn 已提交
23
#include <set>
S
superjomn 已提交
24
#include <vector>
S
superjomn 已提交
25
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
26 27 28 29

namespace paddle {
namespace lite {

30 31 32 33 34 35
struct HostContext {};

#ifdef LITE_WITH_ARM
struct ARMContext {};
#endif

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

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

#ifdef LITE_WITH_X86
struct X86Context {
  // overall information
55

S
superjomn 已提交
56 57 58 59 60 61 62 63
  // kernel information
};
#endif

// Context for running a kernel.
// Holds the necessary resource and information.
class KernelContext {
 public:
64 65 66 67
  template <typename ContextT>
  ContextT& As() {
    if (!ctx_.valid()) {
      ctx_.set<ContextT>();
68
    }
69
    return *ctx_.get_mutable<ContextT>();
70 71 72
  }

 private:
73
  Any ctx_;
S
superjomn 已提交
74 75
};

S
superjomn 已提交
76 77
}  // namespace lite
}  // namespace paddle