Skip to content

Commit 3e9c66b

Browse files
committed
abstract process id
1 parent f356897 commit 3e9c66b

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

backends/cuda/runtime/cuda_backend.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <executorch/runtime/core/error.h>
1212
#include <executorch/runtime/core/evalue.h>
1313
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
14-
#include <unistd.h>
1514
#include <cstdio>
1615

1716
#include <filesystem>
@@ -35,8 +34,7 @@ namespace executorch::backends::cuda {
3534
return symbol_res.error(); \
3635
} \
3736
handle->member = reinterpret_cast<name##Func>(symbol_res.get()); \
38-
} \
39-
while (0)
37+
} while (0)
4038

4139
using namespace std;
4240
using namespace aoti;
@@ -125,7 +123,7 @@ class ET_EXPERIMENTAL CudaBackend final
125123
// Generate dynamic temporary file path
126124
filesystem::path temp_dir = filesystem::temp_directory_path();
127125
filesystem::path so_path =
128-
temp_dir / (so_blob_key + to_string(getpid()) + ".so");
126+
temp_dir / (so_blob_key + to_string(get_process_id()) + ".so");
129127

130128
// Create a temporary file
131129
ofstream outfile(so_path.c_str(), ios::binary);

backends/cuda/runtime/platform/platform.h

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
* LICENSE file in the root directory of this source tree.
88
*/
99

10-
#include <string>
1110
#include <executorch/runtime/core/error.h>
11+
#include <string>
1212

1313
#ifdef _WIN32
1414
#include <windows.h>
1515
#else
1616
#include <dlfcn.h>
17+
#include <unistd.h>
1718
#endif
1819

1920
namespace executorch {
@@ -23,52 +24,75 @@ namespace cuda {
2324
Result<void*> load_library(const std::string& path) {
2425
#ifdef _WIN32
2526
auto lib_handle = LoadLibrary(path.c_str());
26-
if (hModule == NULL) {
27-
ET_LOG(Error, "Failed to load %s with error: %lu", path.c_str(), GetLastError());
28-
return Error::AccessFailed;
27+
if (lib_handle == NULL) {
28+
ET_LOG(
29+
Error,
30+
"Failed to load %s with error: %lu",
31+
path.c_str(),
32+
GetLastError());
33+
return Error::AccessFailed;
2934
}
3035

3136
#else
3237
void* lib_handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_LOCAL);
33-
if (so_handle == nullptr) {
38+
if (lib_handle == nullptr) {
3439
ET_LOG(Error, "Failed to load %s with error: %s", path.c_str(), dlerror());
3540
return Error::AccessFailed;
3641
}
3742
#endif
38-
return (void*) lib_handle;
43+
return (void*)lib_handle;
3944
}
4045

4146
Error close_library(void* lib_handle) {
4247
#ifdef _WIN32
43-
if (!FreeLibrary((HModule)lib_handle)) {
44-
printf("FreeLibrary failed with error %lu\n", GetLastError());
45-
return Error::Internal
46-
}
48+
if (!FreeLibrary((HMODULE)lib_handle)) {
49+
printf("FreeLibrary failed with error %lu\n", GetLastError());
50+
return Error::Internal;
51+
}
4752
#else
4853
if (dlclose(lib_handle) != 0) {
49-
ET_LOG(Error, "dlclose failed: %s\n", dlerror());
50-
return Error::Internal;
54+
ET_LOG(Error, "dlclose failed: %s\n", dlerror());
55+
return Error::Internal;
5156
}
5257
#endif
5358
return Error::Ok;
5459
}
5560

5661
Result<void*> get_function(void* lib_handle, const std::string& fn_name) {
5762
#ifdef _WIN32
58-
auto fn = GetProcAddress((HModule)lib_handle, fn_name.c_str());
63+
auto fn = GetProcAddress((HMODULE)lib_handle, fn_name.c_str());
5964
if (!fn) {
60-
ET_LOG(Error, "Failed loading symbol %s with error %lu\n", fn_name.c_str(), GetLastError());
61-
return Error::Internal;
65+
ET_LOG(
66+
Error,
67+
"Failed loading symbol %s with error %lu\n",
68+
fn_name.c_str(),
69+
GetLastError());
70+
return Error::Internal;
6271
}
6372
#else
6473
auto fn = dlsym(lib_handle, fn_name.c_str());
6574
if (fn == nullptr) {
66-
ET_LOG(Error, "Failed loading symbol %s with error %s\n", fn_name.c_str(), dlerror());
67-
return Error::Internal;
75+
ET_LOG(
76+
Error,
77+
"Failed loading symbol %s with error %s\n",
78+
fn_name.c_str(),
79+
dlerror());
80+
return Error::Internal;
6881
}
6982
#endif
7083

71-
return (void*)fn; // This I think is technically ub on windows. We should probably explicitly pack the bytes.
84+
return (void*)fn; // This I think is technically ub on windows. We should
85+
// probably explicitly pack the bytes.
86+
}
87+
88+
int32_t get_process_id(void* lib_handle) {
89+
#ifdef _WIN32
90+
return GetCurrentProcessId();
91+
#else
92+
return getpid();
93+
#endif
94+
}
95+
7296
}
7397

7498
} // cuda

0 commit comments

Comments
 (0)