Skip to content

Commit 7180719

Browse files
Merge pull request #876 from omarahmed1111/followup-review-feedback-on-e2e-codegen-example
Followup review feedback on e2e codegen example
2 parents c791b8b + d2edad6 commit 7180719

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

examples/codegen/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## End-to-end code generation example
2+
3+
This example demonstrates an end-to-end pipeline of code generation and execution that typically occurs in a JIT engine.
4+
It creates a simple function (adding +1 to an array and storing the result in a second one) via llvm API,
5+
converts it to spirv, and submits it to the runtime.
6+
7+
## Running the example
8+
9+
The following commands are executed from the project root directory.
10+
11+
### Using conda environment
12+
13+
```
14+
$ conda env create -f third_party/deps.yml
15+
$ conda activate examples
16+
$ mkdir build && cd build
17+
# configure with:
18+
$ cmake .. -DUR_BUILD_ADAPTER_L0=ON -DUR_BUILD_EXAMPLE_CODEGEN=ON
19+
$ make
20+
$ ./bin/codegen
21+
```
22+
23+
### Without using conda
24+
25+
To run the example with llvm 13, you will need to make sure that `llvm-13` and `libllvmspirvlib-13-dev` are installed.
26+
27+
**NOTE**: The example could be working with other llvm versions but it was tested with version 13.
28+
29+
```
30+
$ mkdir build && cd build
31+
# configure with:
32+
$ cmake .. -DUR_BUILD_ADAPTER_L0=ON -DUR_BUILD_EXAMPLE_CODEGEN=ON -DLLVM_DIR=/usr/lib/llvm-13/cmake
33+
$ make
34+
$ ./bin/codegen
35+
```

examples/codegen/codegen.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "helpers.h"
2121
#include "ur_api.h"
2222

23+
constexpr unsigned PAGE_SIZE = 4096;
24+
2325
void ur_check(const ur_result_t r) {
2426
if (r != UR_RESULT_SUCCESS) {
2527
urTearDown(nullptr);
@@ -40,6 +42,23 @@ std::vector<ur_adapter_handle_t> get_adapters() {
4042
return adapters;
4143
}
4244

45+
std::vector<ur_adapter_handle_t>
46+
get_supported_adapters(std::vector<ur_adapter_handle_t> &adapters) {
47+
std::vector<ur_adapter_handle_t> supported_adapters;
48+
for (auto adapter : adapters) {
49+
ur_adapter_backend_t backend;
50+
ur_check(urAdapterGetInfo(adapter, UR_ADAPTER_INFO_BACKEND,
51+
sizeof(ur_adapter_backend_t), &backend,
52+
nullptr));
53+
54+
if (backend == UR_ADAPTER_BACKEND_LEVEL_ZERO) {
55+
supported_adapters.push_back(adapter);
56+
}
57+
}
58+
59+
return supported_adapters;
60+
}
61+
4362
std::vector<ur_platform_handle_t>
4463
get_platforms(std::vector<ur_adapter_handle_t> &adapters) {
4564
uint32_t platformCount = 0;
@@ -70,7 +89,7 @@ std::vector<ur_device_handle_t> get_gpus(ur_platform_handle_t p) {
7089
return devices;
7190
}
7291

73-
template <typename T, size_t N> struct alignas(4096) AlignedArray {
92+
template <typename T, size_t N> struct alignas(PAGE_SIZE) AlignedArray {
7493
T data[N];
7594
};
7695

@@ -79,7 +98,8 @@ int main() {
7998
ur_check(urInit(UR_DEVICE_INIT_FLAG_GPU, loader_config));
8099

81100
auto adapters = get_adapters();
82-
auto platforms = get_platforms(adapters);
101+
auto supported_adapters = get_supported_adapters(adapters);
102+
auto platforms = get_platforms(supported_adapters);
83103
auto gpus = get_gpus(platforms.front());
84104
auto spv = generate_plus_one_spv();
85105

@@ -131,10 +151,26 @@ int main() {
131151

132152
ur_check(urQueueFinish(queue));
133153

154+
std::cout << "Input Array: ";
155+
for (int i = 0; i < a_size; ++i) {
156+
std::cout << a.data[i] << " ";
157+
}
158+
std::cout << std::endl;
159+
160+
bool expectedResult = false;
161+
162+
std::cout << "Output Array: ";
134163
for (int i = 0; i < a_size; ++i) {
135164
std::cout << b.data[i] << " ";
165+
expectedResult |= (b.data[i] == a.data[i] + 1);
136166
}
137167
std::cout << std::endl;
138168

139-
return urTearDown(nullptr) == UR_RESULT_SUCCESS ? 0 : 1;
169+
if (expectedResult) {
170+
std::cout << "Results are correct." << std::endl;
171+
} else {
172+
std::cout << "Results are incorrect." << std::endl;
173+
}
174+
175+
return urTearDown(nullptr) == UR_RESULT_SUCCESS && expectedResult ? 0 : 1;
140176
}

scripts/deps.yml renamed to third_party/deps.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# This file is used to initialize a conda environment to run codegen example.
12
name: examples
23
channels:
34
- conda-forge

0 commit comments

Comments
 (0)