-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04_unified_memory_cuda.cu
More file actions
463 lines (358 loc) · 14.6 KB
/
Copy path04_unified_memory_cuda.cu
File metadata and controls
463 lines (358 loc) · 14.6 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <cuda_runtime.h>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <vector>
// Simple kernel for unified memory demonstration
__global__ void vectorAdd(float *a, float *b, float *c, size_t n) {
size_t idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
c[idx] = a[idx] + b[idx];
}
}
// Kernel that modifies data in-place
__global__ void vectorScale(float *data, float scale, size_t n) {
size_t idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
data[idx] *= scale;
}
}
// CPU function that processes the same data
void vectorScaleCPU(float *data, float scale, size_t n) {
for (size_t i = 0; i < n; i++) {
data[i] *= scale;
}
}
// Kernel for demonstrating data migration
__global__ void computeIntensive(float *data, size_t n) {
size_t idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n) {
float value = data[idx];
// Compute-intensive operations
for (int i = 0; i < 100; i++) {
value = sinf(value) + cosf(value);
}
data[idx] = value;
}
}
#define CUDA_CHECK(call) \
do { \
cudaError_t error = call; \
if (error != cudaSuccess) { \
fprintf(stderr, "CUDA error at %s:%d - %s\n", __FILE__, __LINE__, \
cudaGetErrorString(error)); \
exit(EXIT_FAILURE); \
} \
} while(0)
class UnifiedMemoryDemo {
private:
float *data;
size_t size;
size_t elements;
public:
UnifiedMemoryDemo(size_t n) : elements(n), size(n * sizeof(float)) {
// Allocate unified memory
CUDA_CHECK(cudaMallocManaged(&data, size));
// Initialize data on CPU
printf("Initializing %zu elements in unified memory...\n", n);
for (size_t i = 0; i < n; i++) {
data[i] = static_cast<float>(i % 1000);
}
}
~UnifiedMemoryDemo() {
cudaFree(data);
}
void processOnGPU() {
int blockSize = 256;
int gridSize = (elements + blockSize - 1) / blockSize;
printf("Processing on GPU...\n");
// Data automatically migrates to GPU
vectorScale<<<gridSize, blockSize>>>(data, 2.0f, elements);
// Wait for completion
CUDA_CHECK(cudaDeviceSynchronize());
}
void processOnCPU() {
printf("Processing on CPU...\n");
// Data automatically migrates back to CPU
vectorScaleCPU(data, 0.5f, elements);
}
void printSample(int count = 10) {
printf("First %d elements: ", count);
for (int i = 0; i < count && i < elements; i++) {
printf("%.1f ", data[i]);
}
printf("\n");
}
float* getData() { return data; }
size_t getElements() { return elements; }
};
void demonstrateBasicUnifiedMemory() {
printf("=== Basic Unified Memory Example ===\n");
const size_t n = 1024 * 1024; // 1M elements
UnifiedMemoryDemo demo(n);
printf("Initial data:\n");
demo.printSample();
// Process on GPU
demo.processOnGPU();
printf("After GPU processing (scale by 2.0):\n");
demo.printSample();
// Process on CPU
demo.processOnCPU();
printf("After CPU processing (scale by 0.5):\n");
demo.printSample();
printf("Basic unified memory demo completed.\n\n");
}
void compareUnifiedVsExplicit() {
printf("=== Unified Memory vs Explicit Memory Management ===\n");
const size_t n = 8 * 1024 * 1024; // 8M elements
const size_t bytes = n * sizeof(float);
cudaEvent_t start, stop;
CUDA_CHECK(cudaEventCreate(&start));
CUDA_CHECK(cudaEventCreate(&stop));
// Test 1: Unified Memory
float *unified_data;
CUDA_CHECK(cudaMallocManaged(&unified_data, bytes));
// Initialize
for (size_t i = 0; i < n; i++) {
unified_data[i] = static_cast<float>(i);
}
int blockSize = 256;
int gridSize = (n + blockSize - 1) / blockSize;
CUDA_CHECK(cudaEventRecord(start));
// GPU computation
vectorScale<<<gridSize, blockSize>>>(unified_data, 2.0f, n);
CUDA_CHECK(cudaDeviceSynchronize());
// CPU computation
vectorScaleCPU(unified_data, 0.5f, n);
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float unified_time;
CUDA_CHECK(cudaEventElapsedTime(&unified_time, start, stop));
// Test 2: Explicit Memory Management
float *h_data = (float*)malloc(bytes);
float *d_data;
CUDA_CHECK(cudaMalloc(&d_data, bytes));
// Initialize
for (size_t i = 0; i < n; i++) {
h_data[i] = static_cast<float>(i);
}
CUDA_CHECK(cudaEventRecord(start));
// Explicit transfers and computation
CUDA_CHECK(cudaMemcpy(d_data, h_data, bytes, cudaMemcpyHostToDevice));
vectorScale<<<gridSize, blockSize>>>(d_data, 2.0f, n);
CUDA_CHECK(cudaMemcpy(h_data, d_data, bytes, cudaMemcpyDeviceToHost));
// CPU computation
vectorScaleCPU(h_data, 0.5f, n);
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float explicit_time;
CUDA_CHECK(cudaEventElapsedTime(&explicit_time, start, stop));
printf("Data size: %zu MB\n", bytes / (1024 * 1024));
printf("Unified memory time: %.3f ms\n", unified_time);
printf("Explicit memory time: %.3f ms\n", explicit_time);
printf("Performance ratio: %.2fx %s\n",
fmax(unified_time, explicit_time) / fmin(unified_time, explicit_time),
(unified_time < explicit_time) ? "(unified faster)" : "(explicit faster)");
// Cleanup
cudaFree(unified_data);
free(h_data);
cudaFree(d_data);
cudaEventDestroy(start);
cudaEventDestroy(stop);
}
void demonstrateMemoryMigration() {
printf("\n=== Memory Migration and Hints ===\n");
const size_t n = 4 * 1024 * 1024; // 4M elements
const size_t bytes = n * sizeof(float);
float *data;
CUDA_CHECK(cudaMallocManaged(&data, bytes));
// Initialize data
for (size_t i = 0; i < n; i++) {
data[i] = static_cast<float>(i);
}
int device = 0;
// CUDA 13 updated UM APIs use cudaMemLocation instead of raw int device IDs
cudaMemLocation locDevice{};
locDevice.type = cudaMemLocationTypeDevice;
locDevice.id = device;
cudaMemLocation locHost{};
locHost.type = cudaMemLocationTypeHost;
locHost.id = 0; // host id is unused
printf("Testing memory migration with prefetching and hints...\n");
// Set memory advice (location-aware in CUDA 13)
CUDA_CHECK(cudaMemAdvise(data, bytes, cudaMemAdviseSetReadMostly, locDevice));
CUDA_CHECK(cudaMemAdvise(data, bytes, cudaMemAdviseSetPreferredLocation, locDevice));
// Prefetch to GPU (location-aware + explicit stream)
printf("Prefetching to GPU...\n");
CUDA_CHECK(cudaMemPrefetchAsync(data, bytes, locDevice, 0));
CUDA_CHECK(cudaDeviceSynchronize());
int blockSize = 256;
int gridSize = (n + blockSize - 1) / blockSize;
cudaEvent_t start, stop;
CUDA_CHECK(cudaEventCreate(&start));
CUDA_CHECK(cudaEventCreate(&stop));
// GPU computation (data already on GPU)
CUDA_CHECK(cudaEventRecord(start));
computeIntensive<<<gridSize, blockSize>>>(data, n);
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float gpu_time;
CUDA_CHECK(cudaEventElapsedTime(&gpu_time, start, stop));
// Prefetch to CPU (location-aware + explicit stream)
printf("Prefetching to CPU...\n");
CUDA_CHECK(cudaMemPrefetchAsync(data, bytes, locHost, 0));
CUDA_CHECK(cudaDeviceSynchronize());
// CPU computation (data already on CPU)
auto cpu_start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < n; i++) {
float value = data[i];
for (int j = 0; j < 10; j++) { // Less intensive on CPU
value = sinf(value) + cosf(value);
}
data[i] = value;
}
auto cpu_end = std::chrono::high_resolution_clock::now();
double cpu_time = std::chrono::duration<double, std::milli>(cpu_end - cpu_start).count();
printf("GPU computation time: %.3f ms\n", gpu_time);
printf("CPU computation time: %.3f ms\n", cpu_time);
// Test without prefetching for comparison
printf("\nTesting without prefetching...\n");
// Reset memory advice (location-aware in CUDA 13)
CUDA_CHECK(cudaMemAdvise(data, bytes, cudaMemAdviseUnsetReadMostly, locDevice));
CUDA_CHECK(cudaMemAdvise(data, bytes, cudaMemAdviseUnsetPreferredLocation, locDevice));
CUDA_CHECK(cudaEventRecord(start));
computeIntensive<<<gridSize, blockSize>>>(data, n);
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float gpu_time_no_prefetch;
CUDA_CHECK(cudaEventElapsedTime(&gpu_time_no_prefetch, start, stop));
printf("GPU time without prefetching: %.3f ms\n", gpu_time_no_prefetch);
printf("Prefetching benefit: %.2fx speedup\n", gpu_time_no_prefetch / gpu_time);
cudaFree(data);
cudaEventDestroy(start);
cudaEventDestroy(stop);
}
void demonstrateMemoryPool() {
printf("\n=== Memory Pool Management ===\n");
cudaDeviceProp prop;
CUDA_CHECK(cudaGetDeviceProperties(&prop, 0));
if (!prop.memoryPoolsSupported) {
printf("Memory pools not supported on this device.\n");
return;
}
// Create memory pool
cudaMemPool_t mempool;
cudaMemPoolProps poolProps = {};
poolProps.allocType = cudaMemAllocationTypePinned;
poolProps.handleTypes = cudaMemHandleTypeNone;
poolProps.location.type = cudaMemLocationTypeDevice;
poolProps.location.id = 0;
CUDA_CHECK(cudaMemPoolCreate(&mempool, &poolProps));
printf("Created memory pool for efficient allocation/deallocation.\n");
// Allocate multiple arrays from pool
std::vector<float*> arrays;
const size_t array_size = 1024 * 1024 * sizeof(float); // 1M floats each
const int num_arrays = 10;
cudaEvent_t start, stop;
CUDA_CHECK(cudaEventCreate(&start));
CUDA_CHECK(cudaEventCreate(&stop));
// Test pool allocation
CUDA_CHECK(cudaEventRecord(start));
for (int i = 0; i < num_arrays; i++) {
float *ptr;
CUDA_CHECK(cudaMallocFromPoolAsync(&ptr, array_size, mempool, 0));
arrays.push_back(ptr);
}
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float pool_alloc_time;
CUDA_CHECK(cudaEventElapsedTime(&pool_alloc_time, start, stop));
// Deallocate from pool
CUDA_CHECK(cudaEventRecord(start));
for (float *ptr : arrays) {
CUDA_CHECK(cudaFreeAsync(ptr, 0));
}
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float pool_free_time;
CUDA_CHECK(cudaEventElapsedTime(&pool_free_time, start, stop));
// Compare with regular allocation
arrays.clear();
CUDA_CHECK(cudaEventRecord(start));
for (int i = 0; i < num_arrays; i++) {
float *ptr;
CUDA_CHECK(cudaMalloc(&ptr, array_size));
arrays.push_back(ptr);
}
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float regular_alloc_time;
CUDA_CHECK(cudaEventElapsedTime(®ular_alloc_time, start, stop));
CUDA_CHECK(cudaEventRecord(start));
for (float *ptr : arrays) {
CUDA_CHECK(cudaFree(ptr));
}
CUDA_CHECK(cudaEventRecord(stop));
CUDA_CHECK(cudaEventSynchronize(stop));
float regular_free_time;
CUDA_CHECK(cudaEventElapsedTime(®ular_free_time, start, stop));
printf("Allocating %d arrays of %zu MB each:\n", num_arrays, array_size / (1024 * 1024));
printf("Pool allocation time: %.3f ms\n", pool_alloc_time);
printf("Regular allocation time: %.3f ms\n", regular_alloc_time);
printf("Pool deallocation time: %.3f ms\n", pool_free_time);
printf("Regular deallocation time: %.3f ms\n", regular_free_time);
printf("Pool allocation speedup: %.2fx\n", regular_alloc_time / pool_alloc_time);
printf("Pool deallocation speedup: %.2fx\n", regular_free_time / pool_free_time);
CUDA_CHECK(cudaMemPoolDestroy(mempool));
cudaEventDestroy(start);
cudaEventDestroy(stop);
}
int main() {
printf("CUDA Unified Memory Examples\n");
printf("============================\n");
// Check unified memory support
cudaDeviceProp props;
CUDA_CHECK(cudaGetDeviceProperties(&props, 0));
printf("Running on: %s\n", props.name);
printf("Compute Capability: %d.%d\n", props.major, props.minor);
printf("Managed memory supported: %s\n", props.managedMemory ? "YES" : "NO");
printf("Concurrent managed access: %s\n", props.concurrentManagedAccess ? "YES" : "NO");
printf("Page migration supported: %s\n", props.pageableMemoryAccess ? "YES" : "NO");
if (!props.managedMemory) {
printf("Unified memory not supported on this device.\n");
return 1;
}
// Run demonstrations
demonstrateBasicUnifiedMemory();
compareUnifiedVsExplicit();
demonstrateMemoryMigration();
demonstrateMemoryPool();
// Educational summary
printf("\n=== Unified Memory Guidelines ===\n");
printf("✓ ADVANTAGES:\n");
printf(" - Simplified memory management\n");
printf(" - Automatic data migration between CPU and GPU\n");
printf(" - Single address space for CPU and GPU\n");
printf(" - Easier development and debugging\n");
printf(" - Handles complex memory access patterns\n");
printf("\n✓ BEST USE CASES:\n");
printf(" - Prototyping and development\n");
printf(" - Complex data structures with pointers\n");
printf(" - Applications with unpredictable access patterns\n");
printf(" - Sparse data processing\n");
printf(" - Mixed CPU-GPU algorithms\n");
printf("\n⚠ CONSIDERATIONS:\n");
printf(" - Page fault overhead on first access\n");
printf(" - May not achieve peak performance for simple patterns\n");
printf(" - Limited by PCIe bandwidth for migrations\n");
printf(" - Device-dependent behavior\n");
printf("\n💡 OPTIMIZATION TIPS:\n");
printf(" - Use cudaMemPrefetchAsync() to pre-migrate data\n");
printf(" - Apply memory hints with cudaMemAdvise()\n");
printf(" - Minimize CPU-GPU ping-ponging\n");
printf(" - Consider access patterns in algorithm design\n");
printf(" - Profile to identify migration hotspots\n");
printf(" - Use memory pools for frequent allocations\n");
printf("\nUnified memory examples completed successfully!\n");
return 0;
}