FreeTensor
Loading...
Searching...
No Matches
target.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_TARGET_H
2#define FREE_TENSOR_TARGET_H
3
4#include <iostream>
5#include <string>
6
7#ifdef FT_WITH_CUDA
8#include <cuda_runtime.h>
9#endif
10
11#include <buffer.h>
12#include <driver/target_type.h>
13#include <ref.h>
14
15namespace freetensor {
16
20class Target {
21
22 public:
23 Target() {}
24
25 virtual ~Target() = default;
26 virtual bool useNativeArch() const = 0;
27 virtual TargetType type() const = 0;
28 virtual std::string toString() const = 0;
29 virtual MemType mainMemType() const = 0;
30};
31
32class CPUTarget : public Target {
33 bool useNativeArch_;
34 // TODO: infoArch
35
36 public:
37 CPUTarget(bool useNativeArch = true) : useNativeArch_(useNativeArch) {}
38
39 void setUseNativeArch(bool useNativeArch = true) {
40 useNativeArch_ = useNativeArch;
41 }
42 bool useNativeArch() const override { return useNativeArch_; }
43 TargetType type() const override { return TargetType::CPU; }
44 std::string toString() const override { return "CPU"; }
45 MemType mainMemType() const override { return MemType::CPU; }
46
47 int nCores() const;
48};
49
50#ifdef FT_WITH_CUDA
51class GPUTarget : public Target {
52 Ref<cudaDeviceProp> infoArch_;
53
54 public:
55 // GPU is constructed from real local Deivce
56 // `infoArch` has no default value
57 // `useNativeArch` is always true
58 GPUTarget(const Ref<cudaDeviceProp> &infoArch) : infoArch_(infoArch) {}
59
60 bool useNativeArch() const override { return true; }
61 TargetType type() const override { return TargetType::GPU; }
62 std::string toString() const override { return "GPU"; }
63 MemType mainMemType() const override { return MemType::GPUGlobal; }
64
65 void setInfoArch(const Ref<cudaDeviceProp> &infoArch) {
66 infoArch_ = infoArch;
67 }
68 const Ref<cudaDeviceProp> &infoArch() const { return infoArch_; }
69
70 std::pair<int, int> computeCapability() const {
71 return std::make_pair(infoArch_->major, infoArch_->minor);
72 }
73
74 int totalGlobalMem() const { return infoArch_->totalGlobalMem; }
75
76 int warpSize() const { return infoArch_->warpSize; }
77
78 int multiProcessorCount() const { return infoArch_->multiProcessorCount; }
79
80 size_t sharedMemPerBlock() const { return infoArch_->sharedMemPerBlock; }
81
82 int regsPerBlock() const { return infoArch_->regsPerBlock; }
83
84 int maxThreadsPerMultiProcessor() const {
85 return infoArch_->maxThreadsPerMultiProcessor;
86 }
87
88 auto maxLocalMemorySizePerThread() const {
89 // CUDA allocate local memory in a MAX-thread-count-sized buffer
90 // https://forums.developer.nvidia.com/t/out-of-memory-when-allocating-local-memory/238615
91 // https://forums.developer.nvidia.com/t/what-is-the-maximum-cuda-stack-frame-size-per-kerenl/31449
92 return std::min<int64_t>(512 * 1024,
93 (int64_t)totalGlobalMem() /
94 ((int64_t)multiProcessorCount() *
95 maxThreadsPerMultiProcessor()));
96 }
97};
98#endif // FT_WITH_CUDA
99
100bool isSameTarget(const Ref<Target> &lhs, const Ref<Target> &rhs);
101
102inline std::ostream &operator<<(std::ostream &os, const Ref<Target> &target) {
103 return os << target->toString();
104}
105
106} // namespace freetensor
107
108#endif // FREE_TENSOR_TARGET_H
Definition: target.h:32
int nCores() const
Definition: target.cc:8
CPUTarget(bool useNativeArch=true)
Definition: target.h:37
void setUseNativeArch(bool useNativeArch=true)
Definition: target.h:39
MemType mainMemType() const override
Definition: target.h:45
TargetType type() const override
Definition: target.h:43
bool useNativeArch() const override
Definition: target.h:42
std::string toString() const override
Definition: target.h:44
Definition: ref.h:24
Definition: target.h:20
virtual MemType mainMemType() const =0
virtual std::string toString() const =0
virtual ~Target()=default
virtual bool useNativeArch() const =0
Target()
Definition: target.h:23
virtual TargetType type() const =0
Definition: allocator.h:9
TargetType
Definition: target_type.h:6
auto && lhs
Definition: const_fold.cc:70
std::string toString(const AST &op)
Definition: print_ast.cc:784
bool isSameTarget(const Ref< Target > &lhs, const Ref< Target > &rhs)
Definition: target.cc:16
auto auto && rhs
Definition: const_fold.cc:70
std::ostream & operator<<(std::ostream &os, const Dependence &dep)
Definition: deps.cc:1404
MemType
Definition: mem_type.h:14