Skip to content

Commit 3ab25d0

Browse files
Merge pull request #851 from bratpiorka/rrudnick_cuda_win
enable CUDA provider on windows
2 parents 2502dec + e1e70f2 commit 3ab25d0

File tree

9 files changed

+175
-29
lines changed

9 files changed

+175
-29
lines changed

.github/workflows/reusable_gpu.yml

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
name: Level-Zero
2020
env:
2121
VCPKG_PATH: "${{github.workspace}}/../../../../vcpkg/packages/hwloc_x64-windows;${{github.workspace}}/../../../../vcpkg/packages/tbb_x64-windows;${{github.workspace}}/../../../../vcpkg/packages/jemalloc_x64-windows"
22-
COVERAGE_NAME : "exports-coverage-gpu"
22+
COVERAGE_NAME : "exports-coverage-gpu-L0"
2323
# run only on upstream; forks will not have the HW
2424
if: github.repository == 'oneapi-src/unified-memory-framework'
2525
strategy:
@@ -130,18 +130,26 @@ jobs:
130130
name: CUDA
131131
env:
132132
COVERAGE_NAME : "exports-coverage-gpu-CUDA"
133+
VCPKG_PATH: "${{github.workspace}}/build/vcpkg/packages/hwloc_x64-windows;${{github.workspace}}/build/vcpkg/packages/tbb_x64-windows;${{github.workspace}}/build/vcpkg/packages/jemalloc_x64-windows;"
134+
CUDA_PATH: "c:/cuda"
135+
133136
# run only on upstream; forks will not have the HW
134137
if: github.repository == 'oneapi-src/unified-memory-framework'
135138
strategy:
136139
matrix:
137140
shared_library: ['ON', 'OFF']
138141
build_type: ['Debug', 'Release']
139-
# TODO add windows
140-
os: ['Ubuntu']
142+
os: ['Ubuntu', 'Windows']
141143
include:
144+
- os: 'Windows'
145+
compiler: {c: cl, cxx: cl}
146+
number_of_processors: '$Env:NUMBER_OF_PROCESSORS'
142147
- os: 'Ubuntu'
143148
compiler: {c: gcc, cxx: g++}
144149
number_of_processors: '$(nproc)'
150+
exclude:
151+
- os: 'Windows'
152+
build_type: 'Debug'
145153

146154
runs-on: ["DSS-CUDA", "DSS-${{matrix.os}}"]
147155
steps:
@@ -154,10 +162,47 @@ jobs:
154162
if: matrix.os == 'Ubuntu'
155163
run: .github/scripts/get_system_info.sh
156164

165+
- name: Initialize vcpkg
166+
if: matrix.os == 'Windows'
167+
uses: lukka/run-vcpkg@5e0cab206a5ea620130caf672fce3e4a6b5666a1 # v11.5
168+
with:
169+
vcpkgGitCommitId: 3dd44b931481d7a8e9ba412621fa810232b66289
170+
vcpkgDirectory: ${{env.BUILD_DIR}}/vcpkg
171+
vcpkgJsonGlob: '**/vcpkg.json'
172+
173+
- name: Install dependencies (windows-latest)
174+
if: matrix.os == 'Windows'
175+
run: vcpkg install
176+
shell: pwsh # Specifies PowerShell as the shell for running the script.
177+
178+
- name: Configure build for Win
179+
if: matrix.os == 'Windows'
180+
run: >
181+
cmake
182+
-DCMAKE_PREFIX_PATH="${{env.VCPKG_PATH}}${{env.CUDA_PATH}}"
183+
-B ${{env.BUILD_DIR}}
184+
-DCMAKE_INSTALL_PREFIX="${{env.INSTL_DIR}}"
185+
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
186+
-DCMAKE_C_COMPILER=${{matrix.compiler.c}}
187+
-DCMAKE_CXX_COMPILER=${{matrix.compiler.cxx}}
188+
-DUMF_BUILD_SHARED_LIBRARY=${{matrix.shared_library}}
189+
-DUMF_BUILD_BENCHMARKS=ON
190+
-DUMF_BUILD_TESTS=ON
191+
-DUMF_BUILD_GPU_TESTS=ON
192+
-DUMF_BUILD_GPU_EXAMPLES=ON
193+
-DUMF_FORMAT_CODE_STYLE=OFF
194+
-DUMF_DEVELOPER_MODE=ON
195+
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON
196+
-DUMF_BUILD_LIBUMF_POOL_JEMALLOC=ON
197+
-DUMF_BUILD_LEVEL_ZERO_PROVIDER=OFF
198+
-DUMF_BUILD_CUDA_PROVIDER=ON
199+
-DUMF_TESTS_FAIL_ON_SKIP=ON
200+
157201
- name: Configure build for Ubuntu
158202
if: matrix.os == 'Ubuntu'
159203
run: >
160-
cmake -B ${{env.BUILD_DIR}}
204+
cmake
205+
-B ${{env.BUILD_DIR}}
161206
-DCMAKE_INSTALL_PREFIX="${{env.INSTL_DIR}}"
162207
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
163208
-DCMAKE_C_COMPILER=${{matrix.compiler.c}}

cmake/FindCUDA.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ get_filename_component(CUDA_LIB_DIR ${CUDA_LIBRARIES} DIRECTORY)
1111
set(CUDA_LIBRARY_DIRS ${CUDA_LIB_DIR})
1212

1313
if(WINDOWS)
14-
find_file(CUDA_DLL NAMES "bin/cuda.dll" "cuda.dll")
14+
find_file(CUDA_DLL NAMES "nvcuda.dll")
1515
get_filename_component(CUDA_DLL_DIR ${CUDA_DLL} DIRECTORY)
1616
set(CUDA_DLL_DIRS ${CUDA_DLL_DIR})
1717
endif()

examples/cmake/FindCUDA.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ get_filename_component(CUDA_LIB_DIR ${CUDA_LIBRARIES} DIRECTORY)
1111
set(CUDA_LIBRARY_DIRS ${CUDA_LIB_DIR})
1212

1313
if(WINDOWS)
14-
find_file(CUDA_DLL NAMES "bin/cuda.dll" "cuda.dll")
14+
find_file(CUDA_DLL NAMES "nvcuda.dll")
1515
get_filename_component(CUDA_DLL_DIR ${CUDA_DLL} DIRECTORY)
1616
set(CUDA_DLL_DIRS ${CUDA_DLL_DIR})
1717
endif()

examples/cuda_shared_memory/cuda_shared_memory.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@
1414
#include <umf/pools/pool_disjoint.h>
1515
#include <umf/providers/provider_cuda.h>
1616

17+
// disable warning 4201: nonstandard extension used: nameless struct/union
18+
#if defined(_MSC_VER)
19+
#pragma warning(push)
20+
#pragma warning(disable : 4201)
21+
#endif // _MSC_VER
22+
1723
#include <cuda.h>
1824

25+
#if defined(_MSC_VER)
26+
#pragma warning(pop)
27+
#endif // _MSC_VER
28+
1929
int main(void) {
2030
// A result object for storing UMF API result status
2131
umf_result_t res;

src/provider/provider_cuda.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ umf_memory_provider_ops_t *umfCUDAMemoryProviderOps(void) {
2121

2222
#else // !defined(UMF_NO_CUDA_PROVIDER)
2323

24+
// disable warning 4201: nonstandard extension used: nameless struct/union
25+
#if defined(_MSC_VER)
26+
#pragma warning(push)
27+
#pragma warning(disable : 4201)
28+
#endif // _MSC_VER
29+
2430
#include "cuda.h"
2531

32+
#if defined(_MSC_VER)
33+
#pragma warning(pop)
34+
#endif // _MSC_VER
35+
2636
#include "base_alloc_global.h"
2737
#include "utils_assert.h"
2838
#include "utils_common.h"
@@ -100,7 +110,7 @@ static umf_result_t cu2umf_result(CUresult result) {
100110

101111
static void init_cu_global_state(void) {
102112
#ifdef _WIN32
103-
const char *lib_name = "cudart.dll";
113+
const char *lib_name = "nvcuda.dll";
104114
#else
105115
const char *lib_name = "libcuda.so";
106116
#endif
@@ -159,6 +169,7 @@ static umf_result_t cu_memory_provider_initialize(void *params,
159169

160170
if (cu_params->memory_type == UMF_MEMORY_TYPE_UNKNOWN ||
161171
cu_params->memory_type > UMF_MEMORY_TYPE_SHARED) {
172+
LOG_ERR("Invalid memory type value");
162173
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
163174
}
164175

src/utils/utils_load_library.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
#include <libloaderapi.h>
1717
// clang-format on
1818

19-
#else
19+
#else // _WIN32
2020

2121
#define _GNU_SOURCE 1
2222

2323
#include <dlfcn.h> // forces linking with libdl on Linux
2424

25-
#endif
25+
#endif // !_WIN32
26+
27+
#include <stddef.h>
2628

2729
#include "utils_load_library.h"
30+
#include "utils_log.h"
2831

2932
#ifdef _WIN32
3033

@@ -47,7 +50,13 @@ void *utils_get_symbol_addr(void *handle, const char *symbol,
4750
}
4851
handle = GetModuleHandle(libname);
4952
}
50-
return (void *)GetProcAddress((HMODULE)handle, symbol);
53+
54+
void *addr = (void *)GetProcAddress((HMODULE)handle, symbol);
55+
if (addr == NULL) {
56+
LOG_ERR("Required symbol not found: %s", symbol);
57+
}
58+
59+
return addr;
5160
}
5261

5362
#else /* Linux */
@@ -68,7 +77,13 @@ void *utils_get_symbol_addr(void *handle, const char *symbol,
6877
if (!handle) {
6978
handle = RTLD_DEFAULT;
7079
}
71-
return dlsym(handle, symbol);
80+
81+
void *addr = dlsym(handle, symbol);
82+
if (addr == NULL) {
83+
LOG_ERR("Required symbol not found: %s", symbol);
84+
}
85+
86+
return addr;
7287
}
7388

7489
#endif

test/providers/cuda_helpers.cpp

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct libcu_ops {
1818
CUresult (*cuCtxCreate)(CUcontext *pctx, unsigned int flags, CUdevice dev);
1919
CUresult (*cuCtxDestroy)(CUcontext ctx);
2020
CUresult (*cuCtxGetCurrent)(CUcontext *pctx);
21+
CUresult (*cuCtxSetCurrent)(CUcontext ctx);
2122
CUresult (*cuDeviceGet)(CUdevice *device, int ordinal);
2223
CUresult (*cuMemAlloc)(CUdeviceptr *dptr, size_t size);
2324
CUresult (*cuMemFree)(CUdeviceptr dptr);
@@ -34,6 +35,7 @@ struct libcu_ops {
3435
CUpointer_attribute *attributes,
3536
void **data, CUdeviceptr ptr);
3637
CUresult (*cuStreamSynchronize)(CUstream hStream);
38+
CUresult (*cuCtxSynchronize)(void);
3739
} libcu_ops;
3840

3941
#if USE_DLOPEN
@@ -48,7 +50,7 @@ struct DlHandleCloser {
4850
std::unique_ptr<void, DlHandleCloser> cuDlHandle = nullptr;
4951
int InitCUDAOps() {
5052
#ifdef _WIN32
51-
const char *lib_name = "cudart.dll";
53+
const char *lib_name = "nvcuda.dll";
5254
#else
5355
const char *lib_name = "libcuda.so";
5456
#endif
@@ -84,6 +86,12 @@ int InitCUDAOps() {
8486
fprintf(stderr, "cuCtxGetCurrent symbol not found in %s\n", lib_name);
8587
return -1;
8688
}
89+
*(void **)&libcu_ops.cuCtxSetCurrent =
90+
utils_get_symbol_addr(cuDlHandle.get(), "cuCtxSetCurrent", lib_name);
91+
if (libcu_ops.cuCtxSetCurrent == nullptr) {
92+
fprintf(stderr, "cuCtxSetCurrent symbol not found in %s\n", lib_name);
93+
return -1;
94+
}
8795
*(void **)&libcu_ops.cuDeviceGet =
8896
utils_get_symbol_addr(cuDlHandle.get(), "cuDeviceGet", lib_name);
8997
if (libcu_ops.cuDeviceGet == nullptr) {
@@ -153,6 +161,12 @@ int InitCUDAOps() {
153161
lib_name);
154162
return -1;
155163
}
164+
*(void **)&libcu_ops.cuCtxSynchronize =
165+
utils_get_symbol_addr(cuDlHandle.get(), "cuCtxSynchronize", lib_name);
166+
if (libcu_ops.cuCtxSynchronize == nullptr) {
167+
fprintf(stderr, "cuCtxSynchronize symbol not found in %s\n", lib_name);
168+
return -1;
169+
}
156170

157171
return 0;
158172
}
@@ -165,6 +179,7 @@ int InitCUDAOps() {
165179
libcu_ops.cuCtxCreate = cuCtxCreate;
166180
libcu_ops.cuCtxDestroy = cuCtxDestroy;
167181
libcu_ops.cuCtxGetCurrent = cuCtxGetCurrent;
182+
libcu_ops.cuCtxSetCurrent = cuCtxSetCurrent;
168183
libcu_ops.cuDeviceGet = cuDeviceGet;
169184
libcu_ops.cuMemAlloc = cuMemAlloc;
170185
libcu_ops.cuMemAllocHost = cuMemAllocHost;
@@ -176,11 +191,31 @@ int InitCUDAOps() {
176191
libcu_ops.cuPointerGetAttribute = cuPointerGetAttribute;
177192
libcu_ops.cuPointerGetAttributes = cuPointerGetAttributes;
178193
libcu_ops.cuStreamSynchronize = cuStreamSynchronize;
194+
libcu_ops.cuCtxSynchronize = cuCtxSynchronize;
179195

180196
return 0;
181197
}
182198
#endif // USE_DLOPEN
183199

200+
static CUresult set_context(CUcontext required_ctx, CUcontext *restore_ctx) {
201+
CUcontext current_ctx = NULL;
202+
CUresult cu_result = libcu_ops.cuCtxGetCurrent(&current_ctx);
203+
if (cu_result != CUDA_SUCCESS) {
204+
fprintf(stderr, "cuCtxGetCurrent() failed.\n");
205+
return cu_result;
206+
}
207+
208+
*restore_ctx = current_ctx;
209+
if (current_ctx != required_ctx) {
210+
cu_result = libcu_ops.cuCtxSetCurrent(required_ctx);
211+
if (cu_result != CUDA_SUCCESS) {
212+
fprintf(stderr, "cuCtxSetCurrent() failed.\n");
213+
}
214+
}
215+
216+
return cu_result;
217+
}
218+
184219
static int init_cuda_lib(void) {
185220
CUresult result = libcu_ops.cuInit(0);
186221
if (result != CUDA_SUCCESS) {
@@ -191,8 +226,6 @@ static int init_cuda_lib(void) {
191226

192227
int cuda_fill(CUcontext context, CUdevice device, void *ptr, size_t size,
193228
const void *pattern, size_t pattern_size) {
194-
195-
(void)context;
196229
(void)device;
197230
(void)pattern_size;
198231

@@ -202,23 +235,40 @@ int cuda_fill(CUcontext context, CUdevice device, void *ptr, size_t size,
202235
return -1;
203236
}
204237

238+
// set required context
239+
CUcontext curr_context = nullptr;
240+
set_context(context, &curr_context);
241+
205242
int ret = 0;
206243
CUresult res =
207244
libcu_ops.cuMemsetD32((CUdeviceptr)ptr, *(unsigned int *)pattern,
208245
size / sizeof(unsigned int));
209246
if (res != CUDA_SUCCESS) {
210-
fprintf(stderr, "cuMemsetD32() failed!\n");
247+
fprintf(stderr, "cuMemsetD32(%llu, %u, %zu) failed!\n",
248+
(CUdeviceptr)ptr, *(unsigned int *)pattern,
249+
size / pattern_size);
250+
return -1;
251+
}
252+
253+
res = libcu_ops.cuCtxSynchronize();
254+
if (res != CUDA_SUCCESS) {
255+
fprintf(stderr, "cuCtxSynchronize() failed!\n");
211256
return -1;
212257
}
213258

259+
// restore context
260+
set_context(curr_context, &curr_context);
214261
return ret;
215262
}
216263

217-
int cuda_copy(CUcontext context, CUdevice device, void *dst_ptr, void *src_ptr,
218-
size_t size) {
219-
(void)context;
264+
int cuda_copy(CUcontext context, CUdevice device, void *dst_ptr,
265+
const void *src_ptr, size_t size) {
220266
(void)device;
221267

268+
// set required context
269+
CUcontext curr_context = nullptr;
270+
set_context(context, &curr_context);
271+
222272
int ret = 0;
223273
CUresult res =
224274
libcu_ops.cuMemcpy((CUdeviceptr)dst_ptr, (CUdeviceptr)src_ptr, size);
@@ -227,12 +277,14 @@ int cuda_copy(CUcontext context, CUdevice device, void *dst_ptr, void *src_ptr,
227277
return -1;
228278
}
229279

230-
res = libcu_ops.cuStreamSynchronize(0);
280+
res = libcu_ops.cuCtxSynchronize();
231281
if (res != CUDA_SUCCESS) {
232-
fprintf(stderr, "cuStreamSynchronize() failed!\n");
282+
fprintf(stderr, "cuCtxSynchronize() failed!\n");
233283
return -1;
234284
}
235285

286+
// restore context
287+
set_context(curr_context, &curr_context);
236288
return ret;
237289
}
238290

0 commit comments

Comments
 (0)