Skip to content

Commit 8b613b8

Browse files
committed
add parallel kinds
1 parent 65d2cfd commit 8b613b8

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

app/Graph/graph_build.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,39 @@ int main(int argc, char* argv[]) {
1616
model_name = argv[++i];
1717
} else if (std::string(argv[i]) == "--onednn") {
1818
options.backend = Backend::kOneDnn;
19-
} else if (std::string(argv[i]) == "--parallel") {
19+
if (options.parallel) {
20+
std::cout << "Warning: oneDNN backend is not compatible with parallel "
21+
"execution. Disabling parallelism."
22+
<< std::endl;
23+
options.parallel = false;
24+
options.parallel_backend = ParallelBackend::kNone;
25+
}
26+
} else if (std::string(argv[i]) == "--parallel" && i + 1 < argc) {
27+
if (options.backend == Backend::kOneDnn) {
28+
std::cout << "Warning: Parallel execution is not compatible with "
29+
"oneDNN backend. Ignoring --parallel option."
30+
<< std::endl;
31+
i++;
32+
continue;
33+
}
34+
2035
options.parallel = true;
36+
std::string backend_str = argv[++i];
37+
if (backend_str == "tbb") {
38+
options.parallel_backend = ParallelBackend::kTBB;
39+
} else if (backend_str == "stl") {
40+
options.parallel_backend = ParallelBackend::kSTL;
41+
} else if (backend_str == "omp") {
42+
options.parallel_backend = ParallelBackend::kOMP;
43+
} else if (backend_str == "kokkos") {
44+
options.parallel_backend = ParallelBackend::kKokkos;
45+
} else if (backend_str == "sycl") {
46+
options.parallel_backend = ParallelBackend::kSycl;
47+
} else {
48+
std::cerr << "Unknown parallel backend: " << backend_str
49+
<< ". Using default (TBB)." << std::endl;
50+
options.parallel_backend = ParallelBackend::kTBB;
51+
}
2152
} else if (std::string(argv[i]) == "--threads" && i + 1 < argc) {
2253
options.threads = std::stoi(argv[++i]);
2354
}

include/graph/runtime_options.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
#include <cstdint>
33

44
enum class Backend : uint8_t { kNaive, kOneDnn };
5+
enum class ParallelBackend : uint8_t {
6+
kNone,
7+
kTBB,
8+
kSTL,
9+
kOMP,
10+
kKokkos,
11+
kSycl
12+
};
513

614
struct RuntimeOptions {
715
Backend backend{Backend::kNaive};
16+
ParallelBackend parallel_backend{ParallelBackend::kNone};
817
int threads{0};
918
bool parallel{false};
1019
};

include/layers/ConvLayer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ class ConvolutionalLayer : public Layer {
5050
implType_ = implType;
5151
useLegacyImpl_ = useLegacyImpl;
5252
}
53-
5453
void run(const std::vector<Tensor>& input,
5554
std::vector<Tensor>& output) override;
55+
void run(const std::vector<Tensor>& input, std::vector<Tensor>& output,
56+
const RuntimeOptions& options);
5657
#ifdef ENABLE_STATISTIC_WEIGHTS
5758
Tensor get_weights() override { return kernel_; }
5859
#endif

src/layers/ConvLayer.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace it_lab_ai {
44

55
void ConvolutionalLayer::run(const std::vector<Tensor>& input,
66
std::vector<Tensor>& output) {
7+
RuntimeOptions default_options;
8+
run(input, output, default_options);
9+
}
10+
11+
void ConvolutionalLayer::run(const std::vector<Tensor>& input,
12+
std::vector<Tensor>& output,
13+
const RuntimeOptions& options) {
714
if (input.size() != 1) {
815
throw std::runtime_error("ConvolutionalLayer: Input tensors not 1");
916
}
@@ -28,6 +35,29 @@ void ConvolutionalLayer::run(const std::vector<Tensor>& input,
2835
return;
2936
}
3037
}
38+
if (options.parallel) {
39+
switch (options.parallel_backend) {
40+
case ParallelBackend::kTBB:
41+
implType_ = kTBB;
42+
break;
43+
case ParallelBackend::kSTL:
44+
implType_ = kSTL;
45+
break;
46+
// case ParallelBackend::kOMP:
47+
// implType = kOMP;
48+
// break;
49+
// case ParallelBackend::kKokkos:
50+
// implType = kKokkos;
51+
// break;
52+
// case ParallelBackend::kSycl:
53+
// implType = kSycl;
54+
// break;
55+
case ParallelBackend::kNone:
56+
default:
57+
implType_ = kDefault;
58+
break;
59+
}
60+
}
3161
switch (input[0].get_type()) {
3262
case Type::kInt: {
3363
if (kernel_.get_shape().dims() == 2) {
@@ -84,6 +114,11 @@ void ConvolutionalLayer::run(const std::vector<Tensor>& input,
84114
group_, dilations_);
85115
break;
86116
}
117+
/*case kTBB: {
118+
Conv4DTBB<int>(input[0], kernel_, bias_, output[0], stride_, pads_,
119+
group_, dilations_);
120+
break;
121+
}*/
87122
default: {
88123
Conv4D<int>(input[0], kernel_, bias_, output[0], stride_, pads_,
89124
group_, dilations_);
@@ -152,6 +187,10 @@ void ConvolutionalLayer::run(const std::vector<Tensor>& input,
152187
pads_, group_, dilations_);
153188
break;
154189
}
190+
/*case kTBB: {
191+
Conv4DTBB<float>(input[0], kernel_, bias_, output[0], stride_,
192+
pads_, group_, dilations_); break;
193+
}*/
155194
default: {
156195
Conv4D<float>(input[0], kernel_, bias_, output[0], stride_, pads_,
157196
group_, dilations_);

0 commit comments

Comments
 (0)