Skip to content

add UR loader, null adapter and basic example #93

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

Merged
merged 1 commit into from
Dec 9, 2022
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
36 changes: 36 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CMake

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
BUILD_TYPE: Release

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Install apt package
run: sudo apt-get install -y doxygen

- name: Install pip packages
run: pip install -r third_party/requirements.txt

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Generate source from spec, check for uncommitted diff
run: cmake --build ${{github.workspace}}/build --target check-generated

- name: Build
run: cmake --build ${{github.workspace}}/build

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
37 changes: 0 additions & 37 deletions .github/workflows/pull_request.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
# Debug files
scripts/**/*.json

# Python cache
__pycache__/
*.py[cod]

# Generated docs
docs/

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Unified Runtime changelog

## v.X.X.X
* Placeholder for first release
33 changes: 32 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)
project(unified-runtime VERSION 0.5.0)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(CTest)

find_package(Python3 COMPONENTS Interpreter)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# define rpath for libraries so that adapters can be found automatically
set(CMAKE_BUILD_RPATH "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")

#Define a path for custom commands to work around MSVC
set(CUSTOM_COMMAND_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
if(MSVC)
Expand Down Expand Up @@ -70,6 +76,8 @@ install(
EXPORT ${PROJECT_NAME}-targets)

add_subdirectory(source)
add_subdirectory(examples)
add_subdirectory(test)

# Add the list of installed targets to the install. This includes the namespace
# which all installed targets will be prefixed with, e.g. for the headers
Expand All @@ -90,9 +98,32 @@ configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config.cmake
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME})

# Add the package files to the install.
install(
FILES
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config.cmake
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config-version.cmake
DESTINATION lib/cmake/${PROJECT_NAME})

set(API_JSON_FILE ${PROJECT_BINARY_DIR}/unified_runtime.json)

# generate source from the specification
add_custom_command (
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/scripts
DEPENDS ${CMAKE_SOURCE_DIR}/scripts/*
OUTPUT ${API_JSON_FILE}
COMMAND ${Python3_EXECUTABLE} run.py --api-json ${API_JSON_FILE}
COMMAND ${Python3_EXECUTABLE} json2src.py --api-json ${API_JSON_FILE} ${CMAKE_SOURCE_DIR}
)

add_custom_target(generate ALL
DEPENDS ${API_JSON_FILE}
)

# generate source and check for uncommitted diffs
add_custom_target(check-generated
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMAND git diff --exit-code
DEPENDS generate
)
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Intel Corporation
Copyright (C) 2022 Intel Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Unified Runtime

[![GHA build status](https://github.com/oneapi-src/unified-runtime/actions/workflows/cmake.yml/badge.svg?branch=main)](https://github.com/oneapi-src/unified-runtime/actions)

## Contents

This repo contains the following:

- API specification in YaML
- API programming guide in RST
- Loader and a null adapter implementation (partially generated)
- Example applications
- API C/C++ header files (generated)
- API Python module (generated)
- Sample C++ wrapper (generated)
Expand Down Expand Up @@ -33,7 +37,7 @@ target_link_libraries(example PUBLIC unified-runtime::headers)

## Source Code Generation

Code is generated using included [Python scripts](/scripts/README.md).
Code is generated using included [Python scripts](/scripts/README.md).

## Documentation

Expand All @@ -48,22 +52,31 @@ Tools can be acquired via instructions in [third_party](/third_party/README.md).
Project is defined using [CMake](https://cmake.org/).

**Windows**:

Generating Visual Studio Project. EXE and binaries will be in **build/bin/{build_config}**

~~~~
mkdir build
cd build
cmake {path_to_source_dir} -G "Visual Studio 15 2017 Win64"
$ mkdir build
$ cd build
$ cmake {path_to_source_dir} -G "Visual Studio 15 2017 Win64"
~~~~

**Linux**:

Executable and binaries will be in **build/bin**

~~~~
mkdir build
cd build
cmake {path_to_source_dir}
make
$ mkdir build
$ cd build
$ cmake {path_to_source_dir}
$ make
~~~~

**General**:

If you've made modifications to the specification, you can also run a custom `generate` target prior to building.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to move this section to be one of the first ones, so this will be something that the user will try using first instead of doing this the hard way (by figuring out which script should be run and with which params, see Source Code Generation section and the linked README).

~~~~
$ make generate
~~~~

This call will automatically generate the source code.
11 changes: 11 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Security Policy

## Report a Vulnerability

Please report security issues or vulnerabilities to the [Intel Security Center].

For more information on how Intel works to resolve security issues, see [Vulnerability Handling Guidelines].

[Intel Security Center]:https://www.intel.com/security

[Vulnerability Handling Guidelines]:https://www.intel.com/content/www/us/en/security-center/vulnerability-handling-guidelines.html
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_subdirectory(hello_world)
22 changes: 22 additions & 0 deletions examples/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set(TARGET_NAME hello_world)

add_executable(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/hello_world.cpp
)

target_include_directories(${TARGET_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/include
)

if(MSVC)
set_target_properties(${TARGET_NAME}
PROPERTIES
VS_DEBUGGER_COMMAND_ARGUMENTS ""
VS_DEBUGGER_WORKING_DIRECTORY "$(OutDir)"
)
endif()

target_link_libraries(${TARGET_NAME}
${PROJECT_NAME}::loader
${CMAKE_DL_LIBS}
)
94 changes: 94 additions & 0 deletions examples/hello_world/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
*
* Copyright (C) 2020-2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include <stdlib.h>
#include <memory>
#include <iostream>
#include <vector>

#include "ur_api.h"

//////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
ur_result_t status;

ur_platform_handle_t platform = nullptr;
ur_device_handle_t pDevice = nullptr;

// Initialize the platform
status = urInit(0, 0);
if (status != UR_RESULT_SUCCESS)
{
std::cout << "urInit failed with return code: " << status << std::endl;
return 1;
}
std::cout << "Platform initialized.\n";

uint32_t platformCount = 0;
std::vector<ur_platform_handle_t> platforms;

status = urPlatformGet(1, nullptr, &platformCount);
if (status != UR_RESULT_SUCCESS)
{
std::cout << "urPlatformGet failed with return code: " << status << std::endl;
goto out;
}

platforms.resize(platformCount);
status = urPlatformGet(platformCount, platforms.data(), nullptr);
if (status != UR_RESULT_SUCCESS)
{
std::cout << "urPlatformGet failed with return code: " << status << std::endl;
goto out;
}

for (auto p : platforms)
{
uint32_t deviceCount = 0;
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, 0, nullptr, &deviceCount);
if (status != UR_RESULT_SUCCESS)
{
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
goto out;
}

std::vector<ur_device_handle_t> devices(deviceCount);
status = urDeviceGet(p, UR_DEVICE_TYPE_GPU, deviceCount, devices.data(), nullptr);
if (status != UR_RESULT_SUCCESS)
{
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
goto out;
}
for (auto d : devices)
{
ur_device_type_t device_type;
status = urDeviceGetInfo(d, UR_DEVICE_INFO_TYPE, sizeof(ur_device_type_t), static_cast<void *>(&device_type), nullptr);
if (status != UR_RESULT_SUCCESS)
{
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
goto out;
}
static const size_t DEVICE_NAME_MAX_LEN = 1024;
char device_name[DEVICE_NAME_MAX_LEN] = {0};
status = urDeviceGetInfo(d, UR_DEVICE_INFO_NAME, DEVICE_NAME_MAX_LEN - 1, static_cast<void *>(&device_name), nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be extended by the check on the actual device name length returned from this function (with a last parameter).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather keep the example short. Checking the length here doesn't add much - device_name will be a valid NULL-terminated C string, which cout should output correctly.

In a test, on the other hand, we should verify that if this function returns success, the name is populated with a non-zero length string.

if (status != UR_RESULT_SUCCESS)
{
std::cout << "urDeviceGet failed with return code: " << status << std::endl;
goto out;
}
if (device_type == UR_DEVICE_TYPE_GPU)
{
std::cout << "Found a " << device_name << " gpu.\n";
}
}
}

out:
urTearDown(nullptr);
return status == UR_RESULT_SUCCESS ? 0 : 1;
}
Loading