Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Followup review feedback on e2e codegen example #876

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## End-to-end code generation example

This example demonstrates an end-to-end pipeline of code generation and execution that typically occurs in a JIT engine.
It creates a simple function (adding +1 to an array and storing the result in a second one) via llvm API,
converts it to spirv, and submits it to the runtime.

## Running the example

The following commands are executed from the project root directory.

### Using conda environment

```
$ conda env create -f third_party/deps.yml
$ conda activate examples
$ mkdir build && cd build
# configure with:
$ cmake .. -DUR_BUILD_ADAPTER_L0=ON -DUR_BUILD_EXAMPLE_CODEGEN=ON
$ make
$ ./bin/codegen
```

### Without using conda

To run the example with llvm 13, you will need to make sure that `llvm-13` and `libllvmspirvlib-13-dev` are installed.

**NOTE**: The example could be working with other llvm versions but it was tested with version 13.

```
lukaszstolarczuk marked this conversation as resolved.
Show resolved Hide resolved
$ mkdir build && cd build
# configure with:
$ cmake .. -DUR_BUILD_ADAPTER_L0=ON -DUR_BUILD_EXAMPLE_CODEGEN=ON -DLLVM_DIR=/usr/lib/llvm-13/cmake
$ make
$ ./bin/codegen
```
42 changes: 39 additions & 3 deletions examples/codegen/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "helpers.h"
#include "ur_api.h"

constexpr unsigned PAGE_SIZE = 4096;

void ur_check(const ur_result_t r) {
if (r != UR_RESULT_SUCCESS) {
urTearDown(nullptr);
Expand All @@ -40,6 +42,23 @@ std::vector<ur_adapter_handle_t> get_adapters() {
return adapters;
}

std::vector<ur_adapter_handle_t>
get_supported_adapters(std::vector<ur_adapter_handle_t> &adapters) {
std::vector<ur_adapter_handle_t> supported_adapters;
for (auto adapter : adapters) {
ur_adapter_backend_t backend;
ur_check(urAdapterGetInfo(adapter, UR_ADAPTER_INFO_BACKEND,
sizeof(ur_adapter_backend_t), &backend,
nullptr));

if (backend == UR_ADAPTER_BACKEND_LEVEL_ZERO) {
omarahmed1111 marked this conversation as resolved.
Show resolved Hide resolved
supported_adapters.push_back(adapter);
}
}

return supported_adapters;
}

std::vector<ur_platform_handle_t>
get_platforms(std::vector<ur_adapter_handle_t> &adapters) {
uint32_t platformCount = 0;
Expand Down Expand Up @@ -70,7 +89,7 @@ std::vector<ur_device_handle_t> get_gpus(ur_platform_handle_t p) {
return devices;
}

template <typename T, size_t N> struct alignas(4096) AlignedArray {
template <typename T, size_t N> struct alignas(PAGE_SIZE) AlignedArray {
T data[N];
};

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

auto adapters = get_adapters();
auto platforms = get_platforms(adapters);
auto supported_adapters = get_supported_adapters(adapters);
auto platforms = get_platforms(supported_adapters);
auto gpus = get_gpus(platforms.front());
pbalcer marked this conversation as resolved.
Show resolved Hide resolved
auto spv = generate_plus_one_spv();

Expand Down Expand Up @@ -131,10 +151,26 @@ int main() {

ur_check(urQueueFinish(queue));

std::cout << "Input Array: ";
for (int i = 0; i < a_size; ++i) {
std::cout << a.data[i] << " ";
}
std::cout << std::endl;

bool expectedResult = false;

std::cout << "Output Array: ";
for (int i = 0; i < a_size; ++i) {
std::cout << b.data[i] << " ";
expectedResult |= (b.data[i] == a.data[i] + 1);
}
std::cout << std::endl;

return urTearDown(nullptr) == UR_RESULT_SUCCESS ? 0 : 1;
if (expectedResult) {
std::cout << "Results are correct." << std::endl;
} else {
std::cout << "Results are incorrect." << std::endl;
}

return urTearDown(nullptr) == UR_RESULT_SUCCESS && expectedResult ? 0 : 1;
}
1 change: 1 addition & 0 deletions scripts/deps.yml → third_party/deps.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file is used to initialize a conda environment to run codegen example.
name: examples
channels:
- conda-forge
Expand Down
Loading