From 28e27ee7f7735f4f4d4126fa6c18ca1dffdda71d Mon Sep 17 00:00:00 2001 From: madurais Date: Tue, 13 Sep 2022 06:36:35 +0530 Subject: [PATCH] =?UTF-8?q?Changes=20for=20AIX=20compilation=20to=20get=20?= =?UTF-8?q?CPU=20of=20running=20thread.=20hz=20is=20inter=E2=80=A6=20(#127?= =?UTF-8?q?44)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Changes for AIX compilation to get CPU of running thread. hz is internal variable in AIX, hence changing to hz1 in window_functions.cc so that all OS shall work Co-authored-by: madurais Co-authored-by: tvkai --- onnxruntime/core/common/logging/logging.cc | 6 +- onnxruntime/core/common/threadpool.cc | 4 ++ onnxruntime/core/platform/path_lib.h | 66 ++++++++++++++++++- onnxruntime/core/platform/posix/env.cc | 2 +- onnxruntime/core/platform/posix/stacktrace.cc | 2 +- .../providers/cpu/signal/window_functions.cc | 8 ++- 6 files changed, 79 insertions(+), 9 deletions(-) diff --git a/onnxruntime/core/common/logging/logging.cc b/onnxruntime/core/common/logging/logging.cc index 67f103e5f5..a8e6b87df7 100644 --- a/onnxruntime/core/common/logging/logging.cc +++ b/onnxruntime/core/common/logging/logging.cc @@ -14,7 +14,7 @@ #include #else #include -#if defined(__MACH__) || defined(__wasm__) +#if defined(__MACH__) || defined(__wasm__) || defined(_AIX) #include #else #include @@ -218,7 +218,7 @@ unsigned int GetThreadId() { long tid; thr_self(&tid); return static_cast(tid); -#elif defined(__wasm__) +#elif defined(__wasm__) || defined(_AIX) return static_cast(pthread_self()); #else return static_cast(syscall(SYS_gettid)); @@ -231,7 +231,7 @@ unsigned int GetThreadId() { unsigned int GetProcessId() { #ifdef _WIN32 return static_cast(GetCurrentProcessId()); -#elif defined(__MACH__) || defined(__wasm__) +#elif defined(__MACH__) || defined(__wasm__) || defined(_AIX) return static_cast(getpid()); #else return static_cast(syscall(SYS_getpid)); diff --git a/onnxruntime/core/common/threadpool.cc b/onnxruntime/core/common/threadpool.cc index 979b24bd4c..6e20c21df3 100644 --- a/onnxruntime/core/common/threadpool.cc +++ b/onnxruntime/core/common/threadpool.cc @@ -136,6 +136,8 @@ void ThreadPoolProfiler::MainThreadStat::LogCore() { #endif #elif defined(__wasm__) core_ = emscripten_num_logical_cores(); +#elif defined(_AIX) + core_ = mycpu(); #else core_ = sched_getcpu(); #endif @@ -218,6 +220,8 @@ void ThreadPoolProfiler::LogRun(int thread_idx) { #endif #elif defined(__wasm__) child_thread_stats_[thread_idx].core_ = emscripten_num_logical_cores(); +#elif defined(_AIX) + child_thread_stats_[thread_idx].core_ = mycpu(); #else child_thread_stats_[thread_idx].core_ = sched_getcpu(); #endif diff --git a/onnxruntime/core/platform/path_lib.h b/onnxruntime/core/platform/path_lib.h index 7c6e1bf222..e48c2f32d1 100644 --- a/onnxruntime/core/platform/path_lib.h +++ b/onnxruntime/core/platform/path_lib.h @@ -7,6 +7,10 @@ #include #include #include +#if defined(_AIX) +#include +#include +#endif #ifdef _WIN32 #include #include //strftime @@ -169,7 +173,7 @@ std::basic_string ConcatPathComponent(const std::basic_string GetLastComponent(const std::basic_strin return input.substr(pos); } +#elif defined(_AIX) +inline OrtFileType DTToFileTypeAIX(struct stat st) { + switch (st.st_mode & _S_IFMT) { + case S_IFBLK: + return OrtFileType::TYPE_BLK; + case S_IFCHR: + return OrtFileType::TYPE_CHR; + case S_IFDIR: + return OrtFileType::TYPE_DIR; + case S_IFIFO: + return OrtFileType::TYPE_FIFO; + case S_IFLNK: + return OrtFileType::TYPE_LNK; + case S_IFREG: + return OrtFileType::TYPE_REG; + /* No Socket type */ + default: + return OrtFileType::TYPE_UNKNOWN; + } +} + +template +void LoopDir(const std::string& dir_name, T func) { + DIR* dir = opendir(dir_name.c_str()); + struct stat stats; + if (dir == nullptr) { + auto e = errno; + char buf[1024]; + char* msg; +#if defined(__GLIBC__) && defined(_GNU_SOURCE) + msg = strerror_r(e, buf, sizeof(buf)); +#else + if (strerror_r(e, buf, sizeof(buf)) != 0) { + buf[0] = '\0'; + } + msg = buf; +#endif + std::ostringstream oss; + oss << "couldn't open '" << dir_name << "':" << msg; + std::string s = oss.str(); + ORT_THROW(s); + } + ORT_TRY { + struct dirent* dp; + while ((dp = readdir(dir)) != nullptr) { + std::basic_string filename = ConcatPathComponent(dir_name, dp->d_name); + if (stat(filename.c_str(), &stats) != 0) { + continue; + } + if (!func(dp->d_name, DTToFileTypeAIX(stats))) { + break; + } + } + } + ORT_CATCH(const std::exception& ex) { + closedir(dir); + ORT_RETHROW; + } + closedir(dir); +} #else inline OrtFileType DTToFileType(unsigned char t) { switch (t) { diff --git a/onnxruntime/core/platform/posix/env.cc b/onnxruntime/core/platform/posix/env.cc index cf91759e4a..879bd6598c 100644 --- a/onnxruntime/core/platform/posix/env.cc +++ b/onnxruntime/core/platform/posix/env.cc @@ -172,7 +172,7 @@ class PosixThread : public EnvThread { auto [err_no, err_msg] = GetSystemError(); ORT_THROW("pthread_create failed, error code: ", err_no, " error msg: ", err_msg); } -#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(__wasm__) +#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(__wasm__) && !defined(_AIX) if (!thread_options.affinity.empty()) { cpu_set_t cpuset; CPU_ZERO(&cpuset); diff --git a/onnxruntime/core/platform/posix/stacktrace.cc b/onnxruntime/core/platform/posix/stacktrace.cc index cdf2707c89..76864b2697 100644 --- a/onnxruntime/core/platform/posix/stacktrace.cc +++ b/onnxruntime/core/platform/posix/stacktrace.cc @@ -3,7 +3,7 @@ #include "core/common/common.h" -#if !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) +#if !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) && !defined(_AIX) #include #endif #include diff --git a/onnxruntime/core/providers/cpu/signal/window_functions.cc b/onnxruntime/core/providers/cpu/signal/window_functions.cc index 4ddd76641a..2071fab11d 100644 --- a/onnxruntime/core/providers/cpu/signal/window_functions.cc +++ b/onnxruntime/core/providers/cpu/signal/window_functions.cc @@ -110,7 +110,8 @@ Status BlackmanWindow::Compute(OpKernelContext* ctx) const { return create_cosine_sum_window(ctx, data_type_, a0, a1, a2, is_periodic_); } -static inline double hz_to_mel_scale(double hz) { return 2595 * std::log10(1 + hz / 700); } +// 'hz' is a #define in AIX, hence using hz1 +static inline double hz_to_mel_scale(double hz1) { return 2595 * std::log10(1 + hz1 / 700); } static inline double mel_scale_to_hz(double mels) { return 700 * (pow(10, (mels / 2595)) - 1); } @@ -161,8 +162,9 @@ struct CreateMelWeightMatrix { // Convert each point from mel scale back to hertz, and then compute the corresponding index in the fft for (size_t i = 0; i < frequency_bins.size(); i++) { - auto hz = mel_scale_to_hz(low_frequency_mel + mel_step * i); - frequency_bins[i] = static_cast(std::floor(((dft_length + 1) * hz) / sample_rate)); + // 'hz' is a #define in AIX, hence using hz1 + auto hz1 = mel_scale_to_hz(low_frequency_mel + mel_step * i); + frequency_bins[i] = static_cast(std::floor(((dft_length + 1) * hz1) / sample_rate)); } for (size_t i = 0; i < static_cast(num_mel_bins); i++) {