Skip to content

Feature/device manager #277

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 8 commits into from
Mar 8, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __pycache__/
*.so
*.exe
*.lib
*.dll

# CMake build and local install directory
build
Expand Down
8 changes: 4 additions & 4 deletions conda-recipe/bld.bat
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
call "%ONEAPI_ROOT%\compiler\latest\env\vars.bat"
IF %ERRORLEVEL% NEQ 0 (
echo "oneAPI compiler activation failed"
exit /b 1
if errorlevel 1 (
echo "oneAPI compiler activation failed"
exit /b 1
)

"%PYTHON%" setup.py clean --all
"%PYTHON%" setup.py install
IF %ERRORLEVEL% NEQ 0 exit /b 1
if errorlevel 1 exit 1

rem Build wheel package
if NOT "%WHEELS_OUTPUT_FOLDER%"=="" (
Expand Down
6 changes: 3 additions & 3 deletions conda-recipe/run_test.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
call "%ONEAPI_ROOT%\compiler\latest\env\vars.bat"
IF %ERRORLEVEL% NEQ 0 (
if errorlevel 1 (
echo "oneAPI compiler activation failed%"
exit /b 1
)
Expand All @@ -9,7 +9,7 @@ set ERRORLEVEL=
@echo on

"%PYTHON%" -c "import dpctl"
IF %ERRORLEVEL% NEQ 0 exit /b 1
if errorlevel 1 exit 1

pytest -q -ra --disable-warnings --pyargs dpctl -vv
IF %ERRORLEVEL% NEQ 0 exit /b 1
if errorlevel 1 exit 1
8 changes: 3 additions & 5 deletions dpctl-capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if(WIN32)
set(CMAKE_LINKER:PATH "${DPCPP_ROOT}/bin/lld-link")
message(STATUS "Resetting CXX compiler to: " ${CMAKE_CXX_COMPILER})
message(STATUS "Resetting C compiler to: " ${CMAKE_C_COMPILER})
message(STATUS "Resetting Linker to: " ${CMAKE_LINK})
message(STATUS "Resetting Linker to: " ${CMAKE_LINKER})
set(WARNING_FLAGS "-Wall -Wextra -Winit-self -Wunused-function -Wuninitialized -Wmissing-declarations")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WARNING_FLAGS} -Qstd=c++17")
Expand Down Expand Up @@ -108,10 +108,8 @@ if(DPCTL_ENABLE_LO_PROGRAM_CREATION)
endif()

install(
TARGETS
DPCTLSyclInterface
LIBRARY DESTINATION
"${CMAKE_INSTALL_PREFIX}/lib/"
TARGETS DPCTLSyclInterface
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/
)

# Install all headers
Expand Down
2 changes: 2 additions & 0 deletions dpctl-capi/cmake/modules/FindDPCPP.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ if(${dpcpp_result} MATCHES "0")
set(DPCPP_SYCL_LIBRARY ${DPCPP_INSTALL_DIR}/lib/libsycl.so)
set(DPCPP_OPENCL_LIBRARY ${DPCPP_INSTALL_DIR}/lib/libOpenCL.so)
endif()
set(DPCPP_FOUND TRUE)
else()
message(STATUS "DPCPP needed to build dpctl_sycl_interface")
return()
endif()

find_package_handle_standard_args(DPCPP DEFAULT_MSG
DPCPP_FOUND
DPCPP_VERSION
DPCPP_INCLUDE_DIR
DPCPP_SYCL_INCLUDE_DIR
Expand Down
8 changes: 0 additions & 8 deletions dpctl-capi/include/dpctl_sycl_device_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,6 @@ DPCTL_API
__dpctl_give DPCTLSyclDeviceRef DPCTLDevice_CreateFromSelector(
__dpctl_keep const DPCTLSyclDeviceSelectorRef DSRef);

/*!
* @brief Prints out some of the info::deivice attributes for the device.
*
* @param DRef A DPCTLSyclDeviceRef pointer.
*/
DPCTL_API
void DPCTLDevice_DumpInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef);

/*!
* @brief Deletes a DPCTLSyclDeviceRef pointer after casting to to sycl::device.
*
Expand Down
157 changes: 157 additions & 0 deletions dpctl-capi/include/dpctl_sycl_device_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//===-- dpctl_sycl_device_manager.h - A manager for sycl devices -*-C++-*- ===//
//
// Data Parallel Control (dpCtl)
//
// Copyright 2020-2021 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file declares a set of helper functions to query about the available
/// SYCL devices and backends on the system.
///
//===----------------------------------------------------------------------===//

#pragma once

#include "Support/DllExport.h"
#include "Support/ExternC.h"
#include "Support/MemOwnershipAttrs.h"
#include "dpctl_data_types.h"
#include "dpctl_sycl_types.h"
#include "dpctl_vector.h"

DPCTL_C_EXTERN_C_BEGIN

/**
* @defgroup DeviceManager Device management helper functions
*/

/*!
* @brief Contains a #DPCTLSyclDeviceRef and #DPCTLSyclContextRef 2-tuple that
* contains a sycl::device and a sycl::context associated with that device.
*/
typedef struct DeviceAndContextPair
{
DPCTLSyclDeviceRef DRef;
DPCTLSyclContextRef CRef;
} DPCTL_DeviceAndContextPair;

// Declares a set of types abd functions to deal with vectors of
// DPCTLSyclDeviceRef. Refer dpctl_vector_macros.h
DPCTL_DECLARE_VECTOR(Device)

/*!
* @brief Checks if two ::DPCTLSyclDeviceRef objects point to the same
* sycl::device.
*
* DPC++ 2021.1.2 has some bugs that prevent the equality of sycl::device
* objects to work correctly. The DPCTLDeviceMgr_AreEq implements a workaround
* to check if two sycl::device pointers are equivalent. Since, DPC++ uses
* std::shared_pointer wrappers for sycl::device objects we check if the raw
* pointer (shared_pointer.get()) for each device are the same. One caveat is
* that the trick works only for non-host devices. The function evaluates host
* devices separately and always assumes that all host devices are equivalent,
* while checking for the raw pointer equivalent for all other types of devices.
* The workaround will be removed once DPC++ is fixed to correctly check device
* equivalence.
*
* @param DRef1 First opaque pointer to a sycl device.
* @param DRef2 Second opaque pointer to a sycl device.
* @return True if the underlying sycl::device are same, false otherwise.
* @ingroup DeviceManager
*/
bool DPCTLDeviceMgr_AreEq(__dpctl_keep const DPCTLSyclDeviceRef DRef1,
__dpctl_keep const DPCTLSyclDeviceRef DRef2);

/*!
* @brief Returns a pointer to a std::vector<sycl::DPCTLSyclDeviceRef>
* containing the set of ::DPCTLSyclDeviceRef pointers matching the passed in
* device_identifier bit flag.
*
* The device_identifier can be a combination of #DPCTLSyclBackendType and
* #DPCTLSyclDeviceType bit flags. The function returns all devices that
* match the specified bit flags. For example,
*
* @code
* // Returns all opencl devices
* DPCTLDeviceMgr_GetDevices(DPCTLSyclBackendType::DPCTL_OPENCL);
*
* // Returns all opencl gpu devices
* DPCTLDeviceMgr_GetDevices(
* DPCTLSyclBackendType::DPCTL_OPENCL|DPCTLSyclDeviceType::DPCTL_GPU);
*
* // Returns all gpu devices
* DPCTLDeviceMgr_GetDevices(DPCTLSyclDeviceType::DPCTL_GPU);
* @endcode
*
* @param device_identifier A bitflag that can be any combination of
* #DPCTLSyclBackendType and #DPCTLSyclDeviceType
* enum values.
* @return A #DPCTLDeviceVectorRef containing #DPCTLSyclDeviceRef objects
* that match the device identifier bit flags.
* @ingroup DeviceManager
*/
DPCTL_API
__dpctl_give DPCTLDeviceVectorRef
DPCTLDeviceMgr_GetDevices(int device_identifier);

/*!
* @brief Returns the default sycl context inside an opaque DPCTLSyclContextRef
* pointer for the DPCTLSyclDeviceRef input argument.
*
* @param DRef A pointer to a sycl::device that will be used to
* search an internal map containing a cached "default"
* sycl::context for the device.
* @return A #DPCTL_DeviceAndContextPair struct containing the cached
* #DPCTLSyclContextRef associated with the #DPCTLSyclDeviceRef argument passed
* to the function. The DPCTL_DeviceAndContextPair also contains a
* #DPCTLSyclDeviceRef pointer pointing to the same device as the input
* #DPCTLSyclDeviceRef. The returned #DPCTLSyclDeviceRef was cached along with
* the #DPCTLSyclContextRef. This is a workaround till device equality is
* properly fixed in DPC++. If the #DPCTLSyclDeviceRef is not found in the cache
* then DPCTL_DeviceAndContextPair contains a pair of nullptr.
* @ingroup DeviceManager
*/
DPCTL_API
DPCTL_DeviceAndContextPair DPCTLDeviceMgr_GetDeviceAndContextPair(
__dpctl_keep const DPCTLSyclDeviceRef DRef);

/*!
* @brief Get the number of available devices for given backend and device type
* combination.
*
* @param device_identifier Identifies a device using a combination of
* #DPCTLSyclBackendType and #DPCTLSyclDeviceType
* enum values. The argument can be either one of
* the enum values or a bitwise OR-ed combination.
* @return The number of available devices satisfying the condition specified
* by the device_identifier bit flag.
* @ingroup DeviceManager
*/
DPCTL_API
size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier);

/*!
* @brief Prints out the info::deivice attributes for the device that are
* currently supported by dpCtl.
*
* @param DRef A #DPCTLSyclDeviceRef opaque pointer.
* @ingroup DeviceManager
*/
DPCTL_API
void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef);

DPCTL_C_EXTERN_C_END
52 changes: 7 additions & 45 deletions dpctl-capi/source/dpctl_sycl_device_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
#include "dpctl_sycl_device_interface.h"
#include "../helper/include/dpctl_utils_helper.h"
#include "Support/CBindingWrapping.h"
#include "dpctl_sycl_device_manager.h"
#include <CL/sycl.hpp> /* SYCL headers */
#include <cstring>
#include <iomanip>
#include <iostream>

using namespace cl::sycl;

Expand All @@ -41,31 +40,6 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPCTLSyclDeviceRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device_selector, DPCTLSyclDeviceSelectorRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(platform, DPCTLSyclPlatformRef)

/*!
* @brief Helper function to print the metadata for a sycl::device.
*
* @param Device My Param doc
*/
void dump_device_info(const device &Device)
{
std::stringstream ss;

ss << std::setw(4) << " " << std::left << std::setw(16) << "Name"
<< Device.get_info<info::device::name>() << '\n';
ss << std::setw(4) << " " << std::left << std::setw(16) << "Driver version"
<< Device.get_info<info::device::driver_version>() << '\n';
ss << std::setw(4) << " " << std::left << std::setw(16) << "Vendor"
<< Device.get_info<info::device::vendor>() << '\n';
ss << std::setw(4) << " " << std::left << std::setw(16) << "Profile"
<< Device.get_info<info::device::profile>() << '\n';
ss << std::setw(4) << " " << std::left << std::setw(16) << "Device type";

auto devTy = Device.get_info<info::device::device_type>();
ss << DPCTL_DeviceTypeToStr(devTy);

std::cout << ss.str();
}

} /* end of anonymous namespace */

__dpctl_give DPCTLSyclDeviceRef
Expand Down Expand Up @@ -123,18 +97,6 @@ __dpctl_give DPCTLSyclDeviceRef DPCTLDevice_CreateFromSelector(
}
}

/*!
* Prints some of the device info metadata for the device corresponding to the
* specified sycl::queue. Currently, device name, driver version, device
* vendor, and device profile are printed out. More attributed may be added
* later.
*/
void DPCTLDevice_DumpInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef)
{
auto Device = unwrap(DRef);
dump_device_info(*Device);
}

void DPCTLDevice_Delete(__dpctl_take DPCTLSyclDeviceRef DRef)
{
delete unwrap(DRef);
Expand Down Expand Up @@ -432,11 +394,11 @@ bool DPCTLDevice_IsHostUnifiedMemory(__dpctl_keep const DPCTLSyclDeviceRef DRef)
return ret;
}

bool DPCTLDevice_AreEq(__dpctl_keep const DPCTLSyclDeviceRef DevRef1,
__dpctl_keep const DPCTLSyclDeviceRef DevRef2)
bool DPCTLDevice_AreEq(__dpctl_keep const DPCTLSyclDeviceRef DRef1,
__dpctl_keep const DPCTLSyclDeviceRef DRef2)
{
if (!(DevRef1 && DevRef2))
// \todo handle error
return false;
return (*unwrap(DevRef1) == *unwrap(DevRef2));
// Note: DPCPP does not yet support device equality of the form:
// *unwrap(DevRef1) == *unwrap(DevRef2). Till DPCPP is fixed we use the
// custom equality checker implemented inside DPCTLDeviceMgr.
return DPCTLDeviceMgr_AreEq(DRef1, DRef2);
}
Loading