Skip to content

Commit f4b7b8d

Browse files
committed
enable CUDA provider on Windows
1 parent fa08100 commit f4b7b8d

File tree

8 files changed

+166
-77
lines changed

8 files changed

+166
-77
lines changed

.github/workflows/pr_push.yml

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,5 @@ jobs:
2424
name: Fast builds
2525
needs: [CodeChecks, DocsBuild]
2626
uses: ./.github/workflows/reusable_fast.yml
27-
Build:
28-
name: Basic builds
29-
needs: [FastBuild]
30-
uses: ./.github/workflows/reusable_basic.yml
31-
DevDax:
32-
needs: [FastBuild]
33-
uses: ./.github/workflows/reusable_dax.yml
34-
Sanitizers:
35-
needs: [FastBuild]
36-
uses: ./.github/workflows/reusable_sanitizers.yml
37-
Qemu:
38-
needs: [FastBuild]
39-
uses: ./.github/workflows/reusable_qemu.yml
40-
Benchmarks:
41-
needs: [Build]
42-
uses: ./.github/workflows/reusable_benchmarks.yml
43-
ProxyLib:
44-
needs: [Build]
45-
uses: ./.github/workflows/reusable_proxy_lib.yml
4627
GPU:
47-
needs: [Build]
4828
uses: ./.github/workflows/reusable_gpu.yml
49-
Valgrind:
50-
needs: [Build]
51-
uses: ./.github/workflows/reusable_valgrind.yml
52-
MultiNuma:
53-
needs: [Build]
54-
uses: ./.github/workflows/reusable_multi_numa.yml
55-
Coverage:
56-
# total coverage (on upstream only)
57-
if: github.repository == 'oneapi-src/unified-memory-framework'
58-
needs: [Build, DevDax, GPU, MultiNuma, Qemu, ProxyLib]
59-
uses: ./.github/workflows/reusable_coverage.yml
60-
secrets: inherit
61-
with:
62-
trigger: "${{github.event_name}}"
63-
Coverage_partial:
64-
# partial coverage (on forks)
65-
if: github.repository != 'oneapi-src/unified-memory-framework'
66-
needs: [Build, Qemu, ProxyLib]
67-
uses: ./.github/workflows/reusable_coverage.yml
68-
CodeQL:
69-
needs: [Build]
70-
permissions:
71-
contents: read
72-
security-events: write
73-
uses: ./.github/workflows/reusable_codeql.yml
74-
Trivy:
75-
needs: [Build]
76-
permissions:
77-
contents: read
78-
security-events: write
79-
uses: ./.github/workflows/reusable_trivy.yml

.github/workflows/reusable_gpu.yml

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,28 @@ jobs:
126126
name: ${{env.COVERAGE_NAME}}-shared-${{matrix.shared_library}}
127127
path: ${{env.COVERAGE_DIR}}
128128

129+
# TODO merge with above
129130
gpu-CUDA:
130131
name: CUDA
131132
env:
132133
COVERAGE_NAME : "exports-coverage-gpu-CUDA"
134+
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;"
135+
CUDA_PATH: "c:/cuda"
136+
133137
# run only on upstream; forks will not have the HW
134138
if: github.repository == 'oneapi-src/unified-memory-framework'
135139
strategy:
136140
matrix:
137141
shared_library: ['ON', 'OFF']
138142
build_type: ['Debug', 'Release']
139-
# TODO add windows
140-
os: ['Ubuntu']
143+
os: ['Windows']
141144
include:
142-
- os: 'Ubuntu'
143-
compiler: {c: gcc, cxx: g++}
144-
number_of_processors: '$(nproc)'
145+
- os: 'Windows'
146+
compiler: {c: cl, cxx: cl}
147+
number_of_processors: '$Env:NUMBER_OF_PROCESSORS'
148+
exclude:
149+
- os: 'Windows'
150+
build_type: 'Debug'
145151

146152
runs-on: ["DSS-CUDA", "DSS-${{matrix.os}}"]
147153
steps:
@@ -154,10 +160,53 @@ jobs:
154160
if: matrix.os == 'Ubuntu'
155161
run: .github/scripts/get_system_info.sh
156162

163+
- name: Initialize vcpkg
164+
if: matrix.os == 'Windows'
165+
uses: lukka/run-vcpkg@5e0cab206a5ea620130caf672fce3e4a6b5666a1 # v11.5
166+
with:
167+
vcpkgGitCommitId: 3dd44b931481d7a8e9ba412621fa810232b66289
168+
vcpkgDirectory: ${{env.BUILD_DIR}}/vcpkg
169+
vcpkgJsonGlob: '**/vcpkg.json'
170+
171+
- name: Install dependencies (windows-latest)
172+
if: matrix.os == 'Windows'
173+
run: vcpkg install
174+
shell: pwsh # Specifies PowerShell as the shell for running the script.
175+
176+
- name: Install dependencies (ubuntu-latest)
177+
if: matrix.os == 'ubuntu-latest'
178+
run: |
179+
sudo apt-get update
180+
sudo apt-get install -y cmake libjemalloc-dev libhwloc-dev libnuma-dev libtbb-dev
181+
182+
- name: Configure build for Win
183+
if: matrix.os == 'Windows'
184+
run: >
185+
cmake
186+
-DCMAKE_PREFIX_PATH="${{env.VCPKG_PATH}}${{env.CUDA_PATH}}"
187+
-B ${{env.BUILD_DIR}}
188+
-DCMAKE_INSTALL_PREFIX="${{env.INSTL_DIR}}"
189+
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
190+
-DCMAKE_C_COMPILER=${{matrix.compiler.c}}
191+
-DCMAKE_CXX_COMPILER=${{matrix.compiler.cxx}}
192+
-DUMF_BUILD_SHARED_LIBRARY=${{matrix.shared_library}}
193+
-DUMF_BUILD_BENCHMARKS=ON
194+
-DUMF_BUILD_TESTS=ON
195+
-DUMF_BUILD_GPU_TESTS=ON
196+
-DUMF_BUILD_GPU_EXAMPLES=ON
197+
-DUMF_FORMAT_CODE_STYLE=OFF
198+
-DUMF_DEVELOPER_MODE=ON
199+
-DUMF_BUILD_LIBUMF_POOL_DISJOINT=ON
200+
-DUMF_BUILD_LIBUMF_POOL_JEMALLOC=ON
201+
-DUMF_BUILD_LEVEL_ZERO_PROVIDER=OFF
202+
-DUMF_BUILD_CUDA_PROVIDER=ON
203+
-DUMF_TESTS_FAIL_ON_SKIP=ON
204+
157205
- name: Configure build for Ubuntu
158206
if: matrix.os == 'Ubuntu'
159207
run: >
160-
cmake -B ${{env.BUILD_DIR}}
208+
cmake
209+
-B ${{env.BUILD_DIR}}
161210
-DCMAKE_INSTALL_PREFIX="${{env.INSTL_DIR}}"
162211
-DCMAKE_BUILD_TYPE=${{matrix.build_type}}
163212
-DCMAKE_C_COMPILER=${{matrix.compiler.c}}

cmake/FindCUDA.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ 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 "bin/nvcuda.dll" "nvcuda.dll" "bin/cuda.dll"
15+
"cuda.dll")
1516
get_filename_component(CUDA_DLL_DIR ${CUDA_DLL} DIRECTORY)
1617
set(CUDA_DLL_DIRS ${CUDA_DLL_DIR})
1718
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: 14 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

@@ -259,6 +270,8 @@ static umf_result_t cu_memory_provider_alloc(void *provider, size_t size,
259270
return umf_result;
260271
}
261272

273+
//fprintf(stderr, "alloc context %p\n", cu_provider->context);
274+
262275
CUresult cu_result = CUDA_SUCCESS;
263276
switch (cu_provider->memory_type) {
264277
case UMF_MEMORY_TYPE_HOST: {

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,6 +191,7 @@ 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
}
@@ -191,8 +207,6 @@ static int init_cuda_lib(void) {
191207

192208
int cuda_fill(CUcontext context, CUdevice device, void *ptr, size_t size,
193209
const void *pattern, size_t pattern_size) {
194-
195-
(void)context;
196210
(void)device;
197211
(void)pattern_size;
198212

@@ -202,23 +216,40 @@ int cuda_fill(CUcontext context, CUdevice device, void *ptr, size_t size,
202216
return -1;
203217
}
204218

219+
// set required context
220+
CUcontext curr_context = nullptr;
221+
set_context(context, &curr_context);
222+
205223
int ret = 0;
206224
CUresult res =
207225
libcu_ops.cuMemsetD32((CUdeviceptr)ptr, *(unsigned int *)pattern,
208226
size / sizeof(unsigned int));
209227
if (res != CUDA_SUCCESS) {
210-
fprintf(stderr, "cuMemsetD32() failed!\n");
228+
fprintf(stderr, "cuMemsetD32(%llu, %u, %zu) failed!\n",
229+
(CUdeviceptr)ptr, *(unsigned int *)pattern,
230+
size / pattern_size);
231+
return -1;
232+
}
233+
234+
res = libcu_ops.cuCtxSynchronize();
235+
if (res != CUDA_SUCCESS) {
236+
fprintf(stderr, "cuCtxSynchronize() failed!\n");
211237
return -1;
212238
}
213239

240+
// restore context
241+
set_context(curr_context, &curr_context);
214242
return ret;
215243
}
216244

217-
int cuda_copy(CUcontext context, CUdevice device, void *dst_ptr, void *src_ptr,
218-
size_t size) {
219-
(void)context;
245+
int cuda_copy(CUcontext context, CUdevice device, void *dst_ptr,
246+
const void *src_ptr, size_t size) {
220247
(void)device;
221248

249+
// set required context
250+
CUcontext curr_context = nullptr;
251+
set_context(context, &curr_context);
252+
222253
int ret = 0;
223254
CUresult res =
224255
libcu_ops.cuMemcpy((CUdeviceptr)dst_ptr, (CUdeviceptr)src_ptr, size);
@@ -227,12 +258,14 @@ int cuda_copy(CUcontext context, CUdevice device, void *dst_ptr, void *src_ptr,
227258
return -1;
228259
}
229260

230-
res = libcu_ops.cuStreamSynchronize(0);
261+
res = libcu_ops.cuCtxSynchronize();
231262
if (res != CUDA_SUCCESS) {
232-
fprintf(stderr, "cuStreamSynchronize() failed!\n");
263+
fprintf(stderr, "cuCtxSynchronize() failed!\n");
233264
return -1;
234265
}
235266

267+
// restore context
268+
set_context(curr_context, &curr_context);
236269
return ret;
237270
}
238271

@@ -287,6 +320,25 @@ CUcontext get_current_context() {
287320
return context;
288321
}
289322

323+
CUresult set_context(CUcontext required_ctx, CUcontext *restore_ctx) {
324+
CUcontext current_ctx = NULL;
325+
CUresult cu_result = libcu_ops.cuCtxGetCurrent(&current_ctx);
326+
if (cu_result != CUDA_SUCCESS) {
327+
fprintf(stderr, "cuCtxGetCurrent() failed.\n");
328+
return cu_result;
329+
}
330+
331+
*restore_ctx = current_ctx;
332+
if (current_ctx != required_ctx) {
333+
cu_result = libcu_ops.cuCtxSetCurrent(required_ctx);
334+
if (cu_result != CUDA_SUCCESS) {
335+
fprintf(stderr, "cuCtxSetCurrent() failed.\n");
336+
}
337+
}
338+
339+
return cu_result;
340+
}
341+
290342
UTIL_ONCE_FLAG cuda_init_flag;
291343
int InitResult;
292344
void init_cuda_once() {

0 commit comments

Comments
 (0)