Skip to content

Commit

Permalink
[SYCL][E2E] Add helper functions to access environment variables (#15226
Browse files Browse the repository at this point in the history
)

The usage of `getenv` is deprecated on Windows in favor of `_dupenv_s`,
this change adds helper functions to use the appropriate function
depending on the OS.

Should resolve the non XPTI test failures from #15026
  • Loading branch information
ayylol committed Aug 30, 2024
1 parent 02a20e5 commit d91c49d
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 54 deletions.
14 changes: 2 additions & 12 deletions sycl/test-e2e/CompositeDevice/composite_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// RUN: env ZE_FLAT_DEVICE_HIERARCHY=COMPOSITE %{run} %t.out
// RUN: env ZE_FLAT_DEVICE_HIERARCHY=FLAT %{run} %t.out

#include "../helpers.hpp"
#include <sycl/detail/core.hpp>
#include <sycl/ext/oneapi/experimental/composite_device.hpp>

Expand All @@ -15,18 +16,7 @@ bool isL0Backend(sycl::backend backend) {
}

bool isCombinedMode() {
char *Mode = nullptr;
bool Res = false;
#ifdef _WIN32
size_t Size = 0;
auto Err = _dupenv_s(&Mode, &Size, "ZE_FLAT_DEVICE_HIERARCHY");
Res = (Mode != nullptr) && (std::strcmp(Mode, "COMBINED") == 0);
free(Mode);
#else
Mode = std::getenv("ZE_FLAT_DEVICE_HIERARCHY");
Res = (Mode != nullptr) && (std::strcmp(Mode, "COMBINED") == 0);
#endif
return Res;
return env::getVal("ZE_FLAT_DEVICE_HIERARCHY") == "COMBINED";
}

int main() {
Expand Down
11 changes: 6 additions & 5 deletions sycl/test-e2e/Config/allowlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// RUN: env TEST_DEVICE_IS_NOT_AVAILABLE=1 env SYCL_DEVICE_ALLOWLIST="PlatformName:{{SUCH NAME DOESN'T EXIST}}" ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run-unfiltered-devices} %t.out
// RUN: env TEST_INCORRECT_VALUE=1 env SYCL_DEVICE_ALLOWLIST="IncorrectKey:{{.*}}" ONEAPI_DEVICE_SELECTOR=opencl:cpu %{run-unfiltered-devices} %t.out

#include "../helpers.hpp"
#include <algorithm>
#include <cstdlib>
#include <exception>
Expand All @@ -33,7 +34,7 @@ static void replaceSpecialCharacters(std::string &Str) {
int main() {

// Expected that the allowlist filter is not set
if (getenv("PRINT_PLATFORM_INFO")) {
if (env::isDefined("PRINT_PLATFORM_INFO")) {
for (const sycl::platform &Platform : sycl::platform::get_platforms()) {
std::string Name = Platform.get_info<sycl::info::platform::name>();
std::string Ver = Platform.get_info<sycl::info::platform::version>();
Expand All @@ -51,7 +52,7 @@ int main() {
}

// Expected that the allowlist filter is not set
if (getenv("PRINT_DEVICE_INFO")) {
if (env::isDefined("PRINT_DEVICE_INFO")) {
for (const sycl::platform &Platform : sycl::platform::get_platforms()) {
const sycl::device Dev = Platform.get_devices().at(0);
std::string Name = Dev.get_info<sycl::info::device::name>();
Expand All @@ -71,7 +72,7 @@ int main() {
}

// Expected the allowlist to be set with the "PRINT_DEVICE_INFO" run result
if (getenv("TEST_DEVICE_AVAILABLE")) {
if (env::isDefined("TEST_DEVICE_AVAILABLE")) {
for (const sycl::platform &Platform : sycl::platform::get_platforms()) {
if (Platform.get_devices().size() != 1)
throw std::runtime_error("Expected only one device.");
Expand All @@ -82,13 +83,13 @@ int main() {
}

// Expected the allowlist to be set but empty
if (getenv("TEST_DEVICE_IS_NOT_AVAILABLE")) {
if (env::isDefined("TEST_DEVICE_IS_NOT_AVAILABLE")) {
if (!sycl::platform::get_platforms().empty())
throw std::runtime_error("Expected no device is available");
return 0;
}

if (getenv("TEST_INCORRECT_VALUE")) {
if (env::isDefined("TEST_INCORRECT_VALUE")) {
try {
sycl::platform::get_platforms();
} catch (sycl::exception &E) {
Expand Down
3 changes: 2 additions & 1 deletion sycl/test-e2e/Config/env_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// RUN: %if cpu %{ env SYCL_PROGRAM_COMPILE_OPTIONS="-enable-link-options -cl-denorms-are-zero" SHOULD_CRASH=1 %{run} %t.out %}
// RUN: %if cpu %{ env SYCL_PROGRAM_APPEND_COMPILE_OPTIONS="-enable-link-options -cl-denorms-are-zero" SHOULD_CRASH=1 %{run} %t.out %}

#include "../helpers.hpp"
#include <cassert>
#include <sycl/detail/core.hpp>

Expand All @@ -22,7 +23,7 @@ int main() {
int data = 5;
buffer<int, 1> buf(&data, range<1>(1));
queue myQueue;
bool shouldCrash = getenv("SHOULD_CRASH");
bool shouldCrash = env::isDefined("SHOULD_CRASH");
try {
myQueue.submit([&](handler &cgh) {
auto B = buf.get_access<access::mode::read_write>(cgh);
Expand Down
3 changes: 2 additions & 1 deletion sycl/test-e2e/DeviceLib/assert-windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//
// CHECK-FALLBACK: ---> urProgramLink

#include "../helpers.hpp"
#include <array>
#include <assert.h>
#include <iostream>
Expand All @@ -47,7 +48,7 @@ void simple_vadd(const std::array<T, N> &VA, const std::array<T, N> &VB,
}
});

int shouldCrash = getenv("SHOULD_CRASH") ? 1 : 0;
bool shouldCrash = env::isDefined("SHOULD_CRASH");

sycl::range<1> numOfItems{N};
sycl::buffer<T, 1> bufferA(VA.data(), numOfItems);
Expand Down
14 changes: 8 additions & 6 deletions sycl/test-e2e/DeviceLib/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
// Note that the work-item that hits the assert first may vary, since the order
// of execution is undefined. We catch only the first one (whatever id it is).

#include "../helpers.hpp"
#include "sycl/backend/opencl.hpp"
#include <array>
#include <assert.h>
Expand Down Expand Up @@ -128,7 +129,7 @@ void simple_vadd(const std::array<T, N> &VA, const std::array<T, N> &VB,
if (sycl::opencl::has_extension(dev, "cl_intel_devicelib_assert")) {
unsupported = false;
}
if (unsupported && getenv("SKIP_IF_NO_EXT")) {
if (unsupported && env::isDefined("SKIP_IF_NO_EXT")) {
fprintf(stderr, "Device has no support for cl_intel_devicelib_assert, "
"skipping the test\n");
exit(EXIT_SKIP_TEST);
Expand Down Expand Up @@ -160,7 +161,7 @@ int main() {
if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SKIP_TEST) {
return 0;
}
if (getenv("SHOULD_CRASH")) {
if (env::isDefined("SHOULD_CRASH")) {
if (!WIFSIGNALED(status)) {
fprintf(stderr, "error: process did not terminate by a signal\n");
return 1;
Expand All @@ -176,12 +177,13 @@ int main() {
}
int sig = WTERMSIG(status);
int expected = 0;
if (const char *env = getenv("EXPECTED_SIGNAL")) {
if (0 == strcmp(env, "SIGABRT")) {
const std::string envVal = env::getVal("EXPECTED_SIGNAL");
if (!envVal.empty()) {
if (envVal == "SIGABRT") {
expected = SIGABRT;
} else if (0 == strcmp(env, "SIGSEGV")) {
} else if (envVal == "SIGSEGV") {
expected = SIGSEGV;
} else if (0 == strcmp(env, "SIGIOT")) {
} else if (envVal == "SIGIOT") {
expected = SIGIOT;
}
if (!expected) {
Expand Down
19 changes: 10 additions & 9 deletions sycl/test-e2e/FilterSelector/select_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,45 @@

#include <iostream>

#include "../helpers.hpp"
#include <sycl/detail/core.hpp>

using namespace sycl;
using namespace std;

int main() {
const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
std::string forcedPIs;
if (envVal) {
if (envVal.empty()) {
forcedPIs = envVal;
}
if (!envVal || forcedPIs == "*" ||
if (!envVal.empty() || forcedPIs == "*" ||
forcedPIs.find("level_zero:gpu") != std::string::npos) {
default_selector ds;
device d = ds.select_device();
string name = d.get_platform().get_info<info::platform::name>();
assert(name.find("Level-Zero") != string::npos);
}
if (envVal && forcedPIs != "*" &&
if (envVal.empty() && forcedPIs != "*" &&
forcedPIs.find("opencl:gpu") != std::string::npos) {
gpu_selector gs;
device d = gs.select_device();
string name = d.get_platform().get_info<info::platform::name>();
assert(name.find("OpenCL") != string::npos);
}
if (!envVal || forcedPIs == "*" ||
if (!envVal.empty() || forcedPIs == "*" ||
forcedPIs.find("cpu") != std::string::npos) {
cpu_selector cs;
device d = cs.select_device();
}
if (!envVal || forcedPIs == "*" ||
if (!envVal.empty() || forcedPIs == "*" ||
forcedPIs.find("fpga") != std::string::npos) {
accelerator_selector as;
device d = as.select_device();
}
if (envVal && (forcedPIs.find("cpu") == std::string::npos &&
forcedPIs.find("opencl") == std::string::npos &&
forcedPIs.find("*") == std::string::npos)) {
if (envVal.empty() && (forcedPIs.find("cpu") == std::string::npos &&
forcedPIs.find("opencl") == std::string::npos &&
forcedPIs.find("*") == std::string::npos)) {
try {
cpu_selector cs;
device d = cs.select_device();
Expand Down
5 changes: 3 additions & 2 deletions sycl/test-e2e/FilterSelector/select_device_acc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

#include <iostream>

#include "../helpers.hpp"
#include <sycl/detail/core.hpp>

using namespace sycl;
using namespace std;

int main() {
const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
std::string forcedPIs;
if (envVal) {
if (envVal.empty()) {
forcedPIs = envVal;
}
{
Expand Down
5 changes: 3 additions & 2 deletions sycl/test-e2e/FilterSelector/select_device_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

#include <iostream>

#include "../helpers.hpp"
#include <sycl/ext/oneapi/filter_selector.hpp>

using namespace sycl;
using namespace std;

int main() {
const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
std::string forcedPIs;
if (envVal) {
if (envVal.empty()) {
forcedPIs = envVal;
}
{
Expand Down
5 changes: 3 additions & 2 deletions sycl/test-e2e/FilterSelector/select_device_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@

#include <iostream>

#include "../helpers.hpp"
#include <sycl/detail/core.hpp>

using namespace sycl;
using namespace std;

int main() {
const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
string forcedPIs;
if (envVal) {
if (envVal.empty()) {
forcedPIs = envVal;
}

Expand Down
5 changes: 3 additions & 2 deletions sycl/test-e2e/FilterSelector/select_device_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
//
// REQUIRES: level_zero,gpu

#include "../helpers.hpp"
#include <iostream>
#include <sycl/detail/core.hpp>

using namespace sycl;
using namespace std;

int main() {
const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
string forcedPIs;
if (envVal) {
if (envVal.empty()) {
forcedPIs = envVal;
}

Expand Down
5 changes: 3 additions & 2 deletions sycl/test-e2e/FilterSelector/select_device_opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
//
// REQUIRES: opencl,gpu,cpu,accelerator

#include "../helpers.hpp"
#include <iostream>
#include <sycl/detail/core.hpp>

using namespace sycl;
using namespace std;

int main() {
const char *envVal = getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
string forcedPIs;
if (envVal) {
if (envVal.empty()) {
forcedPIs = envVal;
}

Expand Down
3 changes: 2 additions & 1 deletion sycl/test-e2e/Plugin/interop-level-zero-thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// CHECK: Running iteration 18
// CHECK: Running iteration 19

#include "../helpers.hpp"
#include <atomic>
#include <condition_variable>
#include <deque>
Expand Down Expand Up @@ -144,7 +145,7 @@ void worker() {
sycl::info::event::command_execution_status>() ==
sycl::info::event_command_status::complete;

if (getenv("QUERY_STATUS") != nullptr) {
if (env::isDefined("QUERY_STATUS")) {
auto ev1 = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
op.sycl_event_sync);
auto ev2 = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(
Expand Down
7 changes: 4 additions & 3 deletions sycl/test-e2e/Plugin/level_zero_ext_intel_cslice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@
// RUN: %{setup_env} env SYCL_PI_LEVEL_ZERO_USE_IMMEDIATE_COMMANDLISTS=0 SYCL_PI_LEVEL_ZERO_EXPOSE_CSLICE_IN_AFFINITY_PARTITIONING=1 \
// RUN: UR_L0_DEBUG=1 %{run} %t.out 2>&1 | FileCheck %s --check-prefixes=CHECK-PVC

#include "../helpers.hpp"
#include <sycl/detail/core.hpp>

using namespace sycl;

// Specified in the RUN line.
static constexpr int NumCSlices = 4;
static const bool ExposeCSliceInAffinityPartitioning = [] {
const char *Flag =
std::getenv("SYCL_PI_LEVEL_ZERO_EXPOSE_CSLICE_IN_AFFINITY_PARTITIONING");
return Flag ? std::atoi(Flag) != 0 : false;
std::string Flag =
env::getVal("SYCL_PI_LEVEL_ZERO_EXPOSE_CSLICE_IN_AFFINITY_PARTITIONING");
return !Flag.empty() ? std::stoi(Flag) != 0 : false;
}();

template <typename RangeTy, typename ElemTy>
Expand Down
14 changes: 8 additions & 6 deletions sycl/test-e2e/Regression/device_num.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Temporarily disable on L0 due to fails in CI
// UNSUPPORTED: level_zero

#include "../helpers.hpp"
#include <fstream>
#include <iostream>
#include <map>
Expand Down Expand Up @@ -76,7 +77,7 @@ void PrintSystemConfiguration() {

using DevInfo = std::pair<info::device_type, backend>;
using DevInfoMap = std::map<int, std::vector<DevInfo>>;
bool ReadInitialSystemConfiguration(char *fileName, DevInfoMap &devices) {
bool ReadInitialSystemConfiguration(const char *fileName, DevInfoMap &devices) {
fstream confFile;
confFile.open(fileName, ios::in);
if (!confFile.is_open())
Expand Down Expand Up @@ -133,20 +134,21 @@ int GetPreferredDeviceIndex(const std::vector<device> &devices,

int main() {
// Expected that the sycl device filter is not set
if (getenv("PRINT_FULL_DEVICE_INFO")) {
if (env::isDefined("PRINT_FULL_DEVICE_INFO")) {
PrintSystemConfiguration();
return 0;
}

DevInfoMap unfilteredDevices;
assert(ReadInitialSystemConfiguration(getenv("TEST_DEV_CONFIG_FILE_NAME"),
unfilteredDevices) &&
assert(ReadInitialSystemConfiguration(
env::getVal("TEST_DEV_CONFIG_FILE_NAME").c_str(),
unfilteredDevices) &&
"Failed to parse file with initial system configuration data");

const char *envVal = std::getenv("ONEAPI_DEVICE_SELECTOR");
std::string envVal = env::getVal("ONEAPI_DEVICE_SELECTOR");
int deviceNum;
std::cout << "ONEAPI_DEVICE_SELECTOR=" << envVal << std::endl;
deviceNum = std::atoi(std::string(envVal).substr(2).c_str());
deviceNum = std::stoi(envVal.substr(2));

auto devices = device::get_devices();
std::cout << "Device count to analyze =" << devices.size() << std::endl;
Expand Down
Loading

0 comments on commit d91c49d

Please sign in to comment.