-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
76 lines (68 loc) · 1.81 KB
/
Copy pathutils.cpp
File metadata and controls
76 lines (68 loc) · 1.81 KB
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
#include "utils.h"
#include <chrono>
#include <cstdlib>
#include <cuda.h>
#include <iostream>
#include <tuple>
using namespace std;
tuple<CUcontext, CUstream_st *> setup_cuda() {
CUresult res = cuInit(0);
if (res != CUDA_SUCCESS) {
cerr << "Failed to initialize CUDA.\n";
exit(EXIT_FAILURE);
}
// Get the first CUDA device
CUdevice device;
res = cuDeviceGet(&device, 0);
if (res != CUDA_SUCCESS) {
cerr << "Failed to get CUDA device.\n";
exit(EXIT_FAILURE);
}
// Create a CUDA context
CUcontext context;
res = cuCtxCreate(&context, 0, device);
if (res != CUDA_SUCCESS) {
cerr << "Failed to create CUDA context.\n";
exit(EXIT_FAILURE);
}
// Create a CUDA stream
CUstream_st *stream;
res = cuStreamCreate(&stream, CU_STREAM_DEFAULT);
if (res != CUDA_SUCCESS) {
cerr << "Failed to create CUDA stream.\n";
// cuMemFree(d_X);
exit(EXIT_FAILURE);
}
return make_tuple(context, stream);
}
CUdeviceptr make_data(size_t size) {
// Allocate device memory
CUdeviceptr d_X;
// size_t size = 1024 * sizeof(float); // Example size
CUresult res = cuMemAlloc(&d_X, size);
if (res != CUDA_SUCCESS) {
cerr << "Failed to allocate device memory.\n";
exit(EXIT_FAILURE);
}
return d_X;
}
void deepcopyCUdeviceptr(CUdeviceptr src, CUdeviceptr *dst, size_t size) {
// Allocate memory for the destination
CUresult result = cuMemAlloc(dst, size);
if (result != CUDA_SUCCESS) {
exit(EXIT_FAILURE);
}
// Copy data from source to destination
result = cuMemcpyDtoD(*dst, src, size);
if (result != CUDA_SUCCESS) {
// Free memory if the copy fails
cuMemFree(*dst);
*dst = 0;
exit(EXIT_FAILURE);
}
}
void cleanup(CUcontext context, CUstream_st *stream, CUdeviceptr d_X) {
cuStreamDestroy(stream);
cuMemFree(d_X);
cuCtxDestroy(context);
}