Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#ifndef USE_ONNXRUNTIME_DLL
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#else
#endif
#include <google/protobuf/message_lite.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#endif
 
#include "core/session/onnxruntime_cxx_api.h"
#include "gtest/gtest.h"
#include "test/test_environment.h"
#include "test/util/include/telemetry_test_environment.h"
#include <thread>
 
std::unique_ptr<Ort::Env> ort_env;
 
#define ORT_RETURN_IF_NON_NULL_STATUS(arg) \
  if (arg) {                               \
    return -1;                             \
  }
 
#define ORT_RETURN_IF_NULL_STATUS(arg) \
  if (!arg) {                          \
    return -1;                         \
  }
 
namespace TestGlobalCustomThreadHooks {
 
std::vector<std::thread> threads;
int32_t custom_thread_creation_options = 5;
int32_t custom_creation_hook_called = 0;
int32_t custom_join_hook_called = 0;
 
OrtCustomThreadHandle CreateThreadCustomized(void* options, OrtThreadWorkerFn work_loop, void* param) {
  if (*((int32_t*)options) == 5) {
    custom_creation_hook_called += 1;
  }
  threads.push_back(std::thread(work_loop, param));
  return reinterpret_cast<OrtCustomThreadHandle>(threads.back().native_handle());
}
 
void JoinThreadCustomized(OrtCustomThreadHandle handle) {
  for (auto& t : threads) {
    if (reinterpret_cast<OrtCustomThreadHandle>(t.native_handle()) == handle) {
      custom_join_hook_called += 1;
      t.join();
    }
  }
}
 
}  // namespace TestGlobalCustomThreadHooks
 
using namespace TestGlobalCustomThreadHooks;
 
int main(int argc, char** argv) {
  int status = 0;
  const int thread_pool_size = std::thread::hardware_concurrency();
 
  // Fully suppress telemetry for the unit-test process before any Ort::Env (and therefore the platform
  // telemetry provider) is created, so local non-CI runs never spin up the uploader or emit events.
  onnxruntime::test::SuppressTelemetryForTests();
 
  // compose affinity string
  std::stringstream affinity_stream;
  // skip the 1st logical processor
  for (int i = 2; i <= thread_pool_size; ++i) {
    affinity_stream << (i == 2 ? "" : ";") << i;
  }
 
  ORT_TRY {
    ::testing::InitGoogleTest(&argc, argv);
    const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
    std::unique_ptr<OrtStatus, decltype(OrtApi::ReleaseStatus)> st_ptr(nullptr, g_ort->ReleaseStatus);
    OrtThreadingOptions* tp_options;
 
    st_ptr.reset(g_ort->CreateThreadingOptions(&tp_options));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    st_ptr.reset(g_ort->SetGlobalSpinControl(tp_options, 0));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    st_ptr.reset(g_ort->SetGlobalIntraOpNumThreads(tp_options, thread_pool_size));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    // test with an empty affinity string, error status expected
    st_ptr.reset(g_ort->SetGlobalIntraOpThreadAffinity(tp_options, ""));
    ORT_RETURN_IF_NULL_STATUS(st_ptr);
 
    // test with an oversized affinity string, error status expected
    std::string long_affinity_str(onnxruntime::kMaxStrLen + 1, '0');
    st_ptr.reset(g_ort->SetGlobalIntraOpThreadAffinity(tp_options, long_affinity_str.c_str()));
    ORT_RETURN_IF_NULL_STATUS(st_ptr);
 
#if !defined(ORT_MINIMAL_BUILD) && !defined(ORT_EXTENDED_MINIMAL_BUILD)
    st_ptr.reset(g_ort->SetGlobalIntraOpThreadAffinity(tp_options, affinity_stream.str().c_str()));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
#endif
 
    st_ptr.reset(g_ort->SetGlobalCustomCreateThreadFn(tp_options, CreateThreadCustomized));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    st_ptr.reset(g_ort->SetGlobalCustomThreadCreationOptions(tp_options, &custom_thread_creation_options));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    st_ptr.reset(g_ort->SetGlobalCustomJoinThreadFn(tp_options, JoinThreadCustomized));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    st_ptr.reset(g_ort->SetGlobalInterOpNumThreads(tp_options, thread_pool_size));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    st_ptr.reset(g_ort->SetGlobalDenormalAsZero(tp_options));
    ORT_RETURN_IF_NON_NULL_STATUS(st_ptr);
 
    ort_env.reset(new Ort::Env(tp_options, ORT_LOGGING_LEVEL_VERBOSE, "Default"));  // this is the only change from test/providers/test_main.cc
    g_ort->ReleaseThreadingOptions(tp_options);
    status = RUN_ALL_TESTS();
  }
  ORT_CATCH(const std::exception& ex) {
    ORT_HANDLE_EXCEPTION([&]() {
      std::cerr << ex.what();
      status = -1;
    });
  }
 
  // TODO: Fix the C API issue
  ort_env.reset();  // If we don't do this, it will crash
 
#ifndef USE_ONNXRUNTIME_DLL
  // make memory leak checker happy
  ::google::protobuf::ShutdownProtobufLibrary();
#endif
  return status;
}