Skip to content

Commit 3183bb2

Browse files
authored
[Windows] clean up the setting of environment variables cross-platform (#2655)
* clean up the setting of environment variables cross-platform * fix clang-tidy
1 parent a699897 commit 3183bb2

File tree

6 files changed

+80
-77
lines changed

6 files changed

+80
-77
lines changed

test/env_utils.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*******************************************************************************
2+
*
3+
* MIT License
4+
*
5+
* Copyright (c) 2017 Advanced Micro Devices, Inc.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
*******************************************************************************/
26+
27+
#ifndef GUARD_MIOPEN_ENV_UTILS_HPP
28+
#define GUARD_MIOPEN_ENV_UTILS_HPP
29+
30+
#ifdef _WIN32
31+
#define WIN32_LEAN_AND_MEAN
32+
#include <Windows.h>
33+
#endif
34+
35+
#include "test.hpp"
36+
#include <string_view>
37+
38+
inline void setEnvironmentVariable(std::string_view name, std::string_view value)
39+
{
40+
#ifdef _WIN32
41+
BOOL ret = SetEnvironmentVariable(name.data(), value.data());
42+
EXPECT_EQUAL(ret, TRUE);
43+
#else
44+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
45+
int ret = setenv(name.data(), value.data(), 1);
46+
EXPECT_EQUAL(ret, 0);
47+
#endif
48+
}
49+
50+
inline void unsetEnvironmentVariable(std::string_view name)
51+
{
52+
#ifdef _WIN32
53+
BOOL ret = SetEnvironmentVariable(name.data(), nullptr);
54+
EXPECT_EQUAL(ret, TRUE);
55+
#else
56+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
57+
int ret = unsetenv(name.data());
58+
EXPECT_EQUAL(ret, 0);
59+
#endif
60+
}
61+
62+
#endif // GUARD_MIOPEN_ENV_UTILS_HPP

test/find_db.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "driver.hpp"
2929
#include "get_handle.hpp"
3030
#include "workspace.hpp"
31+
#include "env_utils.hpp"
3132

3233
#include <miopen/convolution.hpp>
3334
#include <miopen/conv/problem_description.hpp>
@@ -38,12 +39,6 @@
3839
#include <miopen/hip_build_utils.hpp>
3940

4041
#include <chrono>
41-
#ifdef _WIN32
42-
#define WIN32_LEAN_AND_MEAN
43-
#include <Windows.h>
44-
#else
45-
#include <cstdlib>
46-
#endif
4742
#include <functional>
4843

4944
namespace miopen {
@@ -229,14 +224,8 @@ struct FindDbTest : test_driver
229224

230225
int main(int argc, const char* argv[])
231226
{
232-
#ifdef _WIN32
233-
SetEnvironmentVariable("MIOPEN_LOG_LEVEL", "6");
234-
SetEnvironmentVariable("MIOPEN_COMPILE_PARALLEL_LEVEL", "1");
235-
SetEnvironmentVariable("MIOPEN_ENABLE_LOGGING_ELAPSED_TIME", "1");
236-
#else
237-
setenv("MIOPEN_LOG_LEVEL", "6", 1); // NOLINT (concurrency-mt-unsafe)
238-
setenv("MIOPEN_COMPILE_PARALLEL_LEVEL", "1", 1); // NOLINT (concurrency-mt-unsafe)
239-
setenv("MIOPEN_ENABLE_LOGGING_ELAPSED_TIME", "1", 1); // NOLINT (concurrency-mt-unsafe)
240-
#endif
227+
setEnvironmentVariable("MIOPEN_LOG_LEVEL", "6");
228+
setEnvironmentVariable("MIOPEN_COMPILE_PARALLEL_LEVEL", "1");
229+
setEnvironmentVariable("MIOPEN_ENABLE_LOGGING_ELAPSED_TIME", "1");
241230
test_drive<miopen::FindDbTest>(argc, argv);
242231
}

test/gtest/bad_fusion_plan.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,12 @@
3030
#include "tensor_holder.hpp"
3131
#include "get_handle.hpp"
3232
#include "conv_test_base.hpp"
33+
#include "../env_utils.hpp"
3334

3435
#if MIOPEN_BACKEND_HIP
3536

3637
namespace bad_fusion_plan {
3738

38-
void setEnvironmentVariable(const std::string& name, const std::string& value)
39-
{
40-
int ret = 0;
41-
42-
#ifdef _WIN32
43-
std::string env_var(name + "=" + value);
44-
ret = _putenv(env_var.c_str());
45-
#else
46-
ret = setenv(name.c_str(), value.c_str(), 1);
47-
#endif
48-
EXPECT_EQ(ret, 0);
49-
}
50-
5139
struct ConvTestCaseFusion
5240
{
5341
size_t N;

test/gtest/cba_infer.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "tensor_util.hpp"
3636
#include "get_handle.hpp"
3737
#include "cba.hpp"
38+
#include "../env_utils.hpp"
3839

3940
namespace cba_infer {
4041

@@ -50,19 +51,6 @@ struct ConvBiasActivInferTestHalf : ConvBiasActivInferTest<half_float::half>
5051
{
5152
};
5253

53-
void setEnvironmentVariable(const std::string& name, const std::string& value)
54-
{
55-
int ret = 0;
56-
57-
#ifdef _WIN32
58-
std::string env_var(name + "=" + value);
59-
ret = _putenv(env_var.c_str());
60-
#else
61-
ret = setenv(name.c_str(), value.c_str(), 1);
62-
#endif
63-
EXPECT_EQ(ret, 0);
64-
}
65-
6654
template <typename Solver, typename TestCase>
6755
void RunSolver(miopen::FusionPlanDescriptor& fusePlanDesc,
6856
const std::unique_ptr<miopen::fusion::FusionInvokeParams>& plan_params,

test/gtest/db_sync.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646

4747
#define WORKAROUND_ISSUE_2493 1
4848

49+
#if WORKAROUND_ISSUE_2493 && defined(_WIN32)
50+
#define WIN32_LEAN_AND_MEAN
51+
#include <Windows.h>
52+
#endif
53+
4954
#define WORKAROUND_ISSUE_1987 0 // Allows testing FDB on gfx1030 (legacy fdb).
5055
#define SKIP_KDB_PDB_TESTING 0 // Allows testing FDB on gfx1030.
5156
#define SKIP_CONVOCLDIRECTFWDFUSED 0 // Allows testing FDB on gfx1030 (legacy fdb).
@@ -74,14 +79,10 @@ struct std::hash<KDBKey>
7479
}
7580
};
7681

77-
#if WORKAROUND_ISSUE_2493
78-
static void SetEnvironmentVariable(const std::string& name, const std::string& value)
82+
#if WORKAROUND_ISSUE_2493 && !defined(_WIN32)
83+
static void SetEnvironmentVariable(std::string_view name, std::string_view value)
7984
{
80-
#ifdef _WIN32
81-
const auto ret = _putenv_s(env_var.c_str(), value.c_str());
82-
#else
83-
const auto ret = setenv(name.c_str(), value.c_str(), 1);
84-
#endif
85+
const auto ret = setenv(name.data(), value.data(), 1);
8586
ASSERT_TRUE(ret == 0);
8687
}
8788
#endif // WORKAROUND_ISSUE_2493

test/gtest/log.cpp

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929

3030
#include <miopen/config.h>
3131
#include <miopen/fusion_plan.hpp>
32-
#include <cstdlib>
3332
#include "../random.hpp"
33+
#include "../env_utils.hpp"
3434

3535
#if MIOPEN_BACKEND_OPENCL
3636
#define BKEND "OpenCL"
@@ -57,31 +57,6 @@ const std::string logBnormActiv =
5757

5858
const std::string envConv = "MIOPEN_ENABLE_LOGGING_CMD";
5959

60-
static void setEnvironmentVariable(const std::string& name, const std::string& value)
61-
{
62-
int ret = 0;
63-
64-
#ifdef _WIN32
65-
std::string env_var(name + "=" + value);
66-
ret = _putenv(env_var.c_str());
67-
#else
68-
ret = setenv(name.c_str(), value.c_str(), 1);
69-
#endif
70-
EXPECT_EQ(ret, 0);
71-
}
72-
73-
static void unSetEnvironmentVariable(const std::string& name)
74-
{
75-
int ret = 0;
76-
#ifdef _WIN32
77-
std::string empty_env_var(name + "=");
78-
ret = _putenv(empty_env_var.c_str());
79-
#else
80-
ret = unsetenv(name.c_str());
81-
#endif
82-
EXPECT_EQ(ret, 0);
83-
}
84-
8560
// Captures the std::cerr buffer and store it to a string.
8661
struct CerrRedirect
8762
{
@@ -290,7 +265,7 @@ void TestLogFun(std::function<void(const miopenTensorDescriptor_t&,
290265
if(set_env)
291266
setEnvironmentVariable(env_var, "1");
292267
else
293-
unSetEnvironmentVariable(env_var);
268+
unsetEnvironmentVariable(env_var);
294269

295270
func(test_conv_log.input.desc,
296271
test_conv_log.weights.desc,
@@ -316,7 +291,7 @@ void TestLogCmdCBAFusion(std::function<void(const miopenFusionPlanDescriptor_t)>
316291
if(set_env)
317292
setEnvironmentVariable(env_var, "1");
318293
else
319-
unSetEnvironmentVariable(env_var);
294+
unsetEnvironmentVariable(env_var);
320295

321296
CreateCBAFusionPlan fp_cba_create;
322297
fp_cba_create.CBAPlan();
@@ -341,7 +316,7 @@ void TestLogCmdBNormFusion(std::function<void(const miopenFusionPlanDescriptor_t
341316
if(set_env)
342317
setEnvironmentVariable(env_var, "1");
343318
else
344-
unSetEnvironmentVariable(env_var);
319+
unsetEnvironmentVariable(env_var);
345320

346321
CreateBNormFusionPlan<float> fp_bnorm_create;
347322
fp_bnorm_create.BNormActivation();

0 commit comments

Comments
 (0)