-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscale.cc
More file actions
177 lines (146 loc) · 4.52 KB
/
Copy pathscale.cc
File metadata and controls
177 lines (146 loc) · 4.52 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2020 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
// CUDA includes
#include <cuda.h>
#include <cuda_runtime.h>
#include "cufile_sample_utils.h"
#include <chrono>
#include "cufile.h"
/*
* In this sample program, main thread will allocate 100 MB of GPU memory
* The entire GPU memory will be registered using cuFileBufRegister in the main
* thread. Each thread will just read the data at different offsets.
*/
#define TOGB(x) ((x)/(1024*1024*1024L))
#define TOMB(x) ((x)/(1024*1024L))
#define GB(x) ((x)*1024*1024*1024L)
#define MB(x) ((x)*1024*1024L)
#define KB(x) ((x)*1024L)
#define NUM_THREADS 2
//Macro for checking cuda errors following a cuda launch or api call
#define cudaCheckError() { \
cudaError_t e=cudaGetLastError(); \
if(e!=cudaSuccess) { \
printf("Cuda failure %s:%d: '%s'\n",__FILE__,__LINE__,cudaGetErrorString(e)); \
exit(EXIT_FAILURE); \
} \
}
using namespace std;
using namespace chrono;
typedef struct thread_data
{
void *devPtr;
loff_t offset;
loff_t devPtr_offset;
CUfileHandle_t cfr_handle;
size_t sz;
}thread_data_t;
static void *thread_fn(void *data)
{
int ret;
thread_data_t *t = (thread_data_t *)data;
cudaSetDevice(0);
cudaCheckError();
ret = cuFileRead(t->cfr_handle, t->devPtr, t->sz / NUM_THREADS, t->offset, t->devPtr_offset);
if (ret < 0) {
fprintf(stderr, "cuFileRead failed with ret=%d\n", ret);
}
fprintf(stdout, "Read Success file-offset %ld readSize %ld to GPU 0 buffer offset %ld size %ld Bytes\n",
(unsigned long) t->offset, t->sz / NUM_THREADS, (unsigned long) t->offset, t->sz / NUM_THREADS);
return NULL;
}
void help(void) {
printf("\n./cufilesample_017 <file-path-1>\n");
}
int main(int argc, char **argv) {
void *devPtr;
size_t offset = 0;
int fd;
CUfileError_t status;
CUfileDescr_t cfr_descr;
CUfileHandle_t cfr_handle;
thread_data t[NUM_THREADS];
pthread_t thread[NUM_THREADS];
if (argc < 2) {
fprintf(stderr, "Invalid input.\n");
help();
exit(1);
}
fd = open(argv[1], O_RDWR | O_DIRECT);
if (fd < 0) {
fprintf(stderr, "Unable to open file %s fd %d errno %d\n",
argv[1], fd, errno);
exit(1);
}
size_t size = GetFileSize(fd);
memset((void *)&cfr_descr, 0, sizeof(CUfileDescr_t));
cfr_descr.handle.fd = fd;
cfr_descr.type = CU_FILE_HANDLE_TYPE_OPAQUE_FD;
status = cuFileHandleRegister(&cfr_handle, &cfr_descr);
if (status.err != CU_FILE_SUCCESS) {
printf("file register error: %s\n", CUFILE_ERRSTR(status.err));
close(fd);
exit(1);
}
cudaSetDevice(0);
cudaCheckError();
cudaMalloc(&devPtr, size);
cudaCheckError();
/*
* Entire Memory is registered
*/
status = cuFileBufRegister(devPtr, size, 0);
if (status.err != CU_FILE_SUCCESS) {
printf("Buffer register failed :%s\n", CUFILE_ERRSTR(status.err));
cuFileHandleDeregister(cfr_handle);
close(fd);
exit(1);
}
for (int i = 0; i < NUM_THREADS; i++) {
/*
* Every thread will get same devPtr address; additionally, every thread
* will share the same cuFileHandle.
*/
t[i].devPtr = devPtr;
t[i].cfr_handle = cfr_handle;
/*
* Every thread will work on different offset
*/
t[i].offset = offset;
t[i].devPtr_offset = offset;
t[i].sz = size;
offset += size / NUM_THREADS ;
}
auto thread_start = high_resolution_clock::now();
for (int i = 0; i < NUM_THREADS; i++) {
pthread_create(&thread[i], NULL, &thread_fn, &t[i]);
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(thread[i], NULL);
}
cout << "Time with " << NUM_THREADS << " threads: " << (float) duration_cast<milliseconds>(high_resolution_clock::now() - thread_start).count() << "msec\n";
status = cuFileBufDeregister(devPtr);
if (status.err != CU_FILE_SUCCESS) {
fprintf(stderr, "cuFileBufDeregister failed :%s\n", CUFILE_ERRSTR(status.err));
}
cuFileHandleDeregister(cfr_handle);
close(fd);
cudaFree(devPtr);
return 0;
}