Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 2da97d1

Browse files
[C++ API] Step 9: Introduce CPU and CUDA backend files
The CpuBackend and the CudaBackend types contain the dependent types and functions one needs to implement to support a new backend. These types will be used in the next commit to create both the end-to-end CUDA tuner + compiler as well as a mock CPU autotuner + compiler example.
1 parent 384873a commit 2da97d1

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

tc/core/cpu/cpu_backend.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include <glog/logging.h>
22+
23+
#include "tc/core/cpu/cpu_mapping_options.h"
24+
#include "tc/core/cpu/cpu_mapping_options_cpp_printer.h"
25+
#include "tc/core/cpu/cpu_rtc.h"
26+
#include "tc/core/halide_utils.h"
27+
#include "tc/core/tensor.h"
28+
29+
namespace tc {
30+
/**
31+
* RAII placement support, modeled on CUDA getDevice / setDevice support in
32+
* WithCudaDevice.
33+
* TODO: Impl me (NUMA domain/pinned to core/nothing?)
34+
*/
35+
struct WithCpuDevice {
36+
WithCpuDevice(size_t g) {}
37+
};
38+
39+
/**
40+
* Information returned by polyhedral compilation
41+
*/
42+
struct CpuCompilationResult {
43+
std::string source;
44+
std::string specializedName;
45+
std::vector<long> parameters;
46+
};
47+
48+
/**
49+
* Information that can be set at runtime to control placement and
50+
* synchronization information of a kernel.
51+
*/
52+
struct CpuRuntimeInformation {};
53+
54+
struct CpuTcExecutor;
55+
56+
/**
57+
* This type declares the dependent types and static functions needed to
58+
* autotune, compile and run for the CPU backend.
59+
*/
60+
struct CpuBackend {
61+
using ExecutorType = CpuTcExecutor;
62+
using MappingOptionsType = CpuMappingOptions;
63+
using CompilationResultType = CpuCompilationResult;
64+
using OptionsCacheProtoType = CpuOptionsCacheProto;
65+
using OptionsCacheValueProtoType = CpuOptionsCacheValueProto;
66+
using RTCFunctionType = CpuRTCFunction;
67+
68+
using WithDevice = WithCpuDevice;
69+
using RuntimeInformation = CpuRuntimeInformation;
70+
using MappingOptionsAsCpp = CpuMappingOptionsAsCpp;
71+
using MappingOptionsCppPrinter = CpuMappingOptionsCppPrinter;
72+
73+
static inline std::string backendString() {
74+
LOG(ERROR) << "NYI: CpuBackend::backendString";
75+
return "SOME_CPU";
76+
}
77+
static inline std::string makeDeviceFilename(const std::string& fn) {
78+
return fn + ".cpu";
79+
}
80+
81+
/// Main entry point for polyhedral compilation
82+
static CompilationResultType compileWithTcMapper(
83+
const std::string& tcName,
84+
tc2halide::HalideComponents halideComponents,
85+
const std::vector<const DLConstTensor*>& inputs,
86+
/* TODO: in the future also pass outputs for stride and alignment */
87+
const MappingOptionsType& options);
88+
};
89+
} // namespace tc

tc/core/cpu/cpu_rtc.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
namespace tc {
19+
/**
20+
* This is the class that holds a binary object resulting from compilation.
21+
* It must provide a clear function and be callable.
22+
* TODO: Impl me
23+
*/
24+
struct CpuRTCFunction {
25+
void clear() {}
26+
template <typename... Args>
27+
void operator()(Args&... args) {}
28+
};
29+
} // namespace tc

tc/core/cuda/cuda_backend.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include "tc/core/cuda/cuda.h"
22+
#include "tc/core/cuda/cuda_mapping_options.h"
23+
#include "tc/core/cuda/cuda_mapping_options_cpp_printer.h"
24+
#include "tc/core/cuda/cuda_rtc.h"
25+
#include "tc/core/halide_utils.h"
26+
#include "tc/core/tensor.h"
27+
28+
namespace tc {
29+
/**
30+
* Information returned by polyhedral compilation. In particular, since we use
31+
* tightening of loop bounds for CUDA kernels, it includes the actual grid and
32+
* block sizes needed at runtime.
33+
*/
34+
struct CudaCompilationResult {
35+
std::string source;
36+
std::string specializedName;
37+
std::vector<long> parameters;
38+
Grid grid;
39+
Block block;
40+
};
41+
42+
/**
43+
* Information that can be set at runtime to control placement and
44+
* synchronization information of a kernel.
45+
*/
46+
struct CudaRuntimeInformation {
47+
cudaStream_t stream{0};
48+
};
49+
50+
struct CudaTcExecutor;
51+
52+
/**
53+
* This type declares the dependent types and static functions needed to
54+
* autotune, compile and run for the CPU backend.
55+
*/
56+
struct CudaBackend {
57+
using ExecutorType = CudaTcExecutor;
58+
using MappingOptionsType = CudaMappingOptions;
59+
using CompilationResultType = CudaCompilationResult;
60+
using OptionsCacheProtoType = CudaOptionsCacheProto;
61+
using OptionsCacheValueProtoType = CudaOptionsCacheValueProto;
62+
using RTCFunctionType = CudaRTCFunction;
63+
64+
using WithDevice = WithCudaDevice;
65+
using RuntimeInformation = CudaRuntimeInformation;
66+
using MappingOptionsAsCpp = CudaMappingOptionsAsCpp;
67+
using MappingOptionsCppPrinter = CudaMappingOptionsCppPrinter;
68+
69+
static inline std::string backendString() {
70+
return CudaGPUInfo::GPUInfo().getCudaDeviceStr();
71+
}
72+
static inline std::string makeDeviceFilename(const std::string& fn) {
73+
return fn + ".cuda";
74+
}
75+
76+
/// Main entry point for polyhedral compilation
77+
static CompilationResultType compileWithTcMapper(
78+
const std::string& tcName,
79+
tc2halide::HalideComponents halideComponents,
80+
const std::vector<const DLConstTensor*>& inputs,
81+
/* TODO: in the future also pass outputs for stride and alignment */
82+
const MappingOptionsType& options);
83+
};
84+
} // namespace tc

0 commit comments

Comments
 (0)