1 | /******************************************************************************* |
2 | * Copyright 2021-2022 Intel Corporation |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | *******************************************************************************/ |
16 | |
17 | #ifdef _WIN32 |
18 | #include <windows.h> |
19 | #endif |
20 | |
21 | #include "stdlib.h" |
22 | |
23 | #include "dnnl_test_common.hpp" |
24 | #include "gtest/gtest.h" |
25 | |
26 | #include "oneapi/dnnl/dnnl.hpp" |
27 | |
28 | // Note: use one non-default value to validate functionality. Rest values, if |
29 | // check in loop will not take effect. |
30 | |
31 | namespace { |
32 | |
33 | void custom_setenv(const char *name, const char *value, int overwrite) { |
34 | #ifdef _WIN32 |
35 | auto status = SetEnvironmentVariable(name, value); |
36 | EXPECT_NE(status, 0); |
37 | #else |
38 | auto status = ::setenv(name, value, overwrite); |
39 | EXPECT_EQ(status, 0); |
40 | #endif |
41 | } |
42 | |
43 | } // namespace |
44 | |
45 | namespace dnnl { |
46 | |
47 | #if DNNL_X64 |
48 | TEST(dnnl_max_cpu_isa_env_var_test, TestEnvVars) { |
49 | custom_setenv("DNNL_MAX_CPU_ISA" , "SSE41" , 1); |
50 | auto got = dnnl_get_effective_cpu_isa(); |
51 | |
52 | #if (DNNL_CPU_RUNTIME != DNNL_RUNTIME_NONE) && defined(DNNL_ENABLE_MAX_CPU_ISA) |
53 | // Expect set values only for X64... |
54 | EXPECT_EQ(got, dnnl_cpu_isa_sse41); |
55 | |
56 | auto st = dnnl_set_max_cpu_isa(dnnl_cpu_isa_default); |
57 | EXPECT_EQ(st, dnnl_invalid_arguments); |
58 | auto func_got = dnnl_get_effective_cpu_isa(); |
59 | EXPECT_EQ(func_got, dnnl_cpu_isa_sse41); |
60 | #else |
61 | // ... while rest should return isa_all |
62 | EXPECT_EQ(got, dnnl_cpu_isa_default); |
63 | #endif |
64 | } |
65 | #endif // DNNL_X64 |
66 | |
67 | #if DNNL_X64 |
68 | TEST(dnnl_cpu_isa_hints_var_test, TestEnvVars) { |
69 | custom_setenv("DNNL_CPU_ISA_HINTS" , "PREFER_YMM" , 1); |
70 | auto got = dnnl_get_cpu_isa_hints(); |
71 | |
72 | #if (DNNL_CPU_RUNTIME != DNNL_RUNTIME_NONE) \ |
73 | && defined(DNNL_ENABLE_CPU_ISA_HINTS) |
74 | // Expect set values only for X64... |
75 | EXPECT_EQ(got, dnnl_cpu_isa_prefer_ymm); |
76 | |
77 | auto st = dnnl_set_cpu_isa_hints(dnnl_cpu_isa_no_hints); |
78 | EXPECT_EQ(st, dnnl_runtime_error); |
79 | auto func_got = dnnl_get_cpu_isa_hints(); |
80 | EXPECT_EQ(func_got, dnnl_cpu_isa_prefer_ymm); |
81 | #else |
82 | // ... while rest should return isa_all |
83 | EXPECT_EQ(got, dnnl_cpu_isa_no_hints); |
84 | #endif |
85 | } |
86 | #endif // DNNL_X64 |
87 | |
88 | TEST(dnnl_primitive_cache_capacity_env_var_test, TestEnvVars) { |
89 | custom_setenv("DNNL_PRIMITIVE_CACHE_CAPACITY" , "10" , 1); |
90 | auto got = get_primitive_cache_capacity(); |
91 | #ifndef DNNL_DISABLE_PRIMITIVE_CACHE |
92 | EXPECT_EQ(got, 10); |
93 | |
94 | set_primitive_cache_capacity(9); |
95 | auto func_got = get_primitive_cache_capacity(); |
96 | EXPECT_EQ(func_got, 9); |
97 | #else |
98 | EXPECT_EQ(got, 0); |
99 | #endif |
100 | } |
101 | |
102 | TEST(dnnl_default_fpmath_mode_env_var_test, TestEnvVars) { |
103 | custom_setenv("DNNL_DEFAULT_FPMATH_MODE" , "ANY" , 1); |
104 | dnnl_fpmath_mode_t got_val; |
105 | auto st = dnnl_get_default_fpmath_mode(&got_val); |
106 | EXPECT_EQ(st, dnnl_success); |
107 | EXPECT_EQ(got_val, dnnl_fpmath_mode_any); |
108 | |
109 | st = dnnl_set_default_fpmath_mode(dnnl_fpmath_mode_strict); |
110 | EXPECT_EQ(st, dnnl_success); |
111 | dnnl_fpmath_mode_t func_got_val; |
112 | st = dnnl_get_default_fpmath_mode(&func_got_val); |
113 | EXPECT_EQ(st, dnnl_success); |
114 | EXPECT_EQ(func_got_val, dnnl_fpmath_mode_strict); |
115 | } |
116 | |
117 | } // namespace dnnl |
118 | |