-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlibgpmlog.cuh
More file actions
728 lines (629 loc) · 24 KB
/
Copy pathlibgpmlog.cuh
File metadata and controls
728 lines (629 loc) · 24 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#pragma once
#include "libgpm.cuh"
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
enum gpmlog_flags
{
PMEMLOG_UNMANAGED = 1, // User manually specifies where to insert
PMEMLOG_UNSTRICT = 2, // Do not persist after every insert
};
// Persistent metadata to be stored in GPU
struct gpu_gpmlog
{
int partitions;
/* For non-managed gpmlogs, head/tail
* stores an index per partition.
* For managed, it stores one per thread */
size_t *head;
size_t *tail;
size_t byte_size;
int flags;
};
// Other volatile metadata used during execution
struct gpmlog
{
// Careful! Below is a host pointer
// while the other pointers are device
const char *path;
size_t log_size; // non-volatile metadata size + byte_size
struct gpu_gpmlog *plog;
void *start;
int *locks; // Used to serialize access when necessary
};
// There should be a CUDA function to
// calculate warp size, but in meantime...
#define WARP_SIZE 32
#define BLOCK_SIZE 128
#define WORD_SIZE 4
/************************
*
* INTERNAL FUNCTIONS
*
************************/
static __global__ void setup_log(gpmlog *plog, char *start, int len)
{
char *start_copy = start;
size_t num_heads = ((size_t)plog->plog->tail - (size_t)plog->plog->head) / sizeof(size_t);
plog->plog->head = (size_t *)start;
start = (char *)start + sizeof(size_t) * num_heads;
plog->plog->tail = (size_t *)start;
start = (char *)start + sizeof(size_t) * num_heads;
if(!(plog->plog->flags & PMEMLOG_UNMANAGED))
{
size_t header_size = (size_t)start - (size_t)start_copy + sizeof(gpu_gpmlog);
start = (char *)start + (header_size % BLOCK_SIZE != 0 ? BLOCK_SIZE - header_size % BLOCK_SIZE : 0);
}
plog->start = (char *)start;
}
static __device__ int getGlobalIdx()
{
int blockId = blockIdx.x + blockIdx.y * gridDim.x
+ gridDim.x * gridDim.y * blockIdx.z;
int threadId = blockId * (blockDim.x * blockDim.y * blockDim.z)
+ (threadIdx.z * (blockDim.x * blockDim.y))
+ (threadIdx.y * blockDim.x) + threadIdx.x;
return threadId;
}
static __device__ int gpmlog_insert_manual(gpmlog *plog, void *var, size_t size, int partition)
{
if(partition >= plog->plog->partitions) {
return -1;
}
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
__threadfence();
if(!locked) {
done = true;
size_t start = plog->plog->head[partition];
if(start + size > plog->plog->tail[partition]) {
size = plog->plog->tail[partition] - start;
}
// Insert into log
// Only update head after all memory has
// been placed to maintain crash consistency
if(plog->plog->flags & PMEMLOG_UNSTRICT) {
gpm_memcpy_nodrain((char *)plog->start + start, var, size, cudaMemcpyDeviceToDevice);
size_t temp = plog->plog->head[partition] + size;
gpm_memcpy_nodrain((char *)&plog->plog->head[partition], &temp, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
else {
gpm_memcpy((char *)plog->start + start, var, size, cudaMemcpyDeviceToDevice);
size_t temp = plog->plog->head[partition] + size;
gpm_memcpy((char *)&plog->plog->head[partition], &temp, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
__threadfence();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
return size;
}
static __device__ int gpmlog_insert_managed(gpmlog *plog, void *var, size_t size, int partition)
{
int tid;
if(partition == -1)
tid = getGlobalIdx();
else
tid = partition;
size_t temp_head = plog->plog->head[tid];
size_t tail = plog->plog->tail[tid];
int i = 0;
for(; i < size && temp_head < tail;) {
// Write up to a word at a time
int sz = 1;
if(size - i >= WORD_SIZE - (int)(temp_head % WORD_SIZE))
sz = WORD_SIZE - (int)(temp_head % WORD_SIZE);
gpm_memcpy_nodrain((char *)plog->start + temp_head, (char *)var + i, sz, cudaMemcpyDeviceToDevice);
temp_head += (size_t)sz;
i += sz;
// Reached end of WORD_SIZE byte segment
// move to next segment
if(temp_head % WORD_SIZE == 0) {
// next address = current address + BLOCK_SIZE - WORD_SIZE
temp_head += BLOCK_SIZE - WORD_SIZE;
}
}
if(i < size && temp_head >= plog->plog->tail[tid]) {
size = i;
}
// Only update head after all memory has
// been placed to maintain crash consistency
if(PMEMLOG_UNSTRICT & plog->plog->flags) {
gpm_memcpy_nodrain(&plog->plog->head[tid], &temp_head, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
else {
gpm_persist();
gpm_memcpy(&plog->plog->head[tid], &temp_head, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
return i;
}
static __device__ int gpmlog_read_manual(gpmlog *plog, void *var, size_t size, int partition)
{
if(partition >= plog->plog->partitions) {
return -1;
}
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
__threadfence();
if(!locked) {
done = true;
size_t start = plog->plog->head[partition];
if((partition == 0 && start < size) || (partition != 0 && start < plog->plog->tail[partition - 1] + size)) {
if(partition == 0)
size = start;
else
size = start - plog->plog->tail[partition - 1];
}
// Read data stored in log
vol_memcpy(var, (char *)plog->start + start - size, size);
// Unlock
__threadfence();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
return size;
}
static __device__ int gpmlog_read_managed(gpmlog *plog, void *var, size_t size, int partition)
{
int tid = getGlobalIdx();
if(partition != -1)
tid = partition;
// Check for underflow condition
size_t head = plog->plog->head[tid] - (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]);
if(head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE < size) {
size = head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE;
}
size_t i = plog->plog->head[tid], j = size;
for(;j > 0;) {
// Reached end of WORD_SIZE byte segment
// move to previous segment
if(i % WORD_SIZE == 0) {
// next address = current address - BLOCK_SIZE + WORD_SIZE
i -= BLOCK_SIZE - WORD_SIZE;
}
// Read up to a word at a time
size_t sz = 1;
if(i % WORD_SIZE == 0 && j >= WORD_SIZE)
sz = WORD_SIZE;
else if(j >= i % WORD_SIZE)
sz = i % WORD_SIZE;
else
sz = j;
i -= sz;
j -= sz;
vol_memcpy((char *)var + j, (char *)plog->start + i, sz);
}
return size;
}
static __device__ int gpmlog_remove_manual(gpmlog *plog, size_t size, int partition)
{
if(partition >= plog->plog->partitions) {
return -1;
}
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
__threadfence();
if(!locked) {
done = true;
size_t start = plog->plog->head[partition];
if((partition == 0 && start < size) || (partition != 0 && start < plog->plog->tail[partition - 1] + size)) {
if(partition == 0)
size = start;
else
size = start - plog->plog->tail[partition - 1];
}
size_t temp = plog->plog->head[partition] - size;
// Drain if necessary
if(PMEMLOG_UNSTRICT & plog->plog->flags) {
gpm_memcpy_nodrain(&plog->plog->head[partition], &temp, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
else {
gpm_memcpy(&plog->plog->head[partition], &temp, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
// Unlock
__threadfence();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
return size;
}
static __device__ int gpmlog_remove_managed(gpmlog *plog, size_t size)
{
int tid = getGlobalIdx();
// Check for underflow condition
size_t head = plog->plog->head[tid] - (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]);
if(head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE < size) {
size = head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE;
}
// Calculate size of stack after removal
int new_head = head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE - size;
// Move head to appropriate position
int offset = (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]) + (tid % WARP_SIZE) * WORD_SIZE;
new_head = new_head / WORD_SIZE * BLOCK_SIZE + new_head % WORD_SIZE + offset;
// Drain if necessary
if(PMEMLOG_UNSTRICT & plog->plog->flags) {
gpm_memcpy_nodrain(&plog->plog->head[tid], &new_head, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
else {
gpm_memcpy(&plog->plog->head[tid], &new_head, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
return size;
}
static __device__ void gpmlog_clear_manual(gpmlog *plog, int partition)
{
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
if(!locked) {
done = true;
// Update log head
if(partition == 0)
gpm_memset_nodrain(&plog->plog->head[partition], 0, sizeof(size_t));
else {
gpm_memcpy_nodrain(&plog->plog->head[partition], &plog->plog->tail[partition - 1], sizeof(size_t), cudaMemcpyDeviceToDevice);
}
// Drain if necessary
if(!(PMEMLOG_UNSTRICT & plog->plog->flags))
gpm_persist();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
}
static __device__ void gpmlog_clear_managed(gpmlog *plog, int partition)
{
int tid = getGlobalIdx();
if(partition != -1)
tid = partition;
size_t head = (tid % WARP_SIZE) * WORD_SIZE + (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]);
gpm_memcpy_nodrain(&plog->plog->head[tid], &head, sizeof(size_t), cudaMemcpyDeviceToDevice);
// Drain if necessary
if(!(PMEMLOG_UNSTRICT & plog->plog->flags))
gpm_persist();
}
static __global__ void setupPartitions(size_t *head, size_t *tail, int partitions, size_t len)
{
int id = threadIdx.x + blockIdx.x * blockDim.x;
if(id >= partitions)
return;
head[id] = (len / partitions) * id;
tail[id] = (len / partitions) * (id + 1);
if(id == partitions - 1)
tail[id] = len;
}
static __host__ gpmlog *gpmlog_create_conv(const char *path, size_t len, int partitions, int flags = 0)
{
// Mark as unmanaged
flags |= PMEMLOG_UNMANAGED;
// Create volatile metadata
gpmlog *plog, dummy_log;
cudaMalloc((void **)&plog, sizeof(gpmlog));
dummy_log.path = path;
// Calculate log size = size of metadata + (heads + tails) + actual log
dummy_log.log_size = sizeof(gpu_gpmlog) + (2 * sizeof(size_t) * partitions) + len;
// Create persistent memory chunk for persistent part
void *log_pointer = gpm_map_file(path, dummy_log.log_size, 1);
int *locks;
cudaMalloc((void **)&locks, sizeof(int) * partitions);
cudaMemset(locks, 0, sizeof(int) * partitions);
// Set appropriate pointer locations
dummy_log.plog = (gpu_gpmlog *)log_pointer;
dummy_log.locks = locks;
// Shift appropriate amount for next pointer
log_pointer = (char *)log_pointer + sizeof(gpu_gpmlog);
// Assign values for persistent log metadata
gpu_gpmlog temp_log;
temp_log.partitions = partitions;
temp_log.head = (size_t *)log_pointer;
log_pointer = (char *)log_pointer + sizeof(size_t) * partitions;
temp_log.tail = (size_t *)log_pointer;
temp_log.byte_size = len;
temp_log.flags = flags;
log_pointer = (char *)log_pointer + sizeof(size_t) * partitions;
dummy_log.start = log_pointer;
cudaMemcpy(dummy_log.plog, &temp_log, sizeof(gpu_gpmlog), cudaMemcpyHostToDevice);
setupPartitions<<<(partitions + 511) / 512, 512>>> (temp_log.head, temp_log.tail, partitions, len);
cudaMemcpy(plog, &dummy_log, sizeof(gpmlog), cudaMemcpyHostToDevice);
return plog;
}
static __global__ void setupPartitionsManaged(size_t *head, size_t *tail, int blocks, int threads, size_t len)
{
int num_warps = (blocks * ((threads + WARP_SIZE - 1) / WARP_SIZE));
int blk = blockIdx.x;
int thd = threadIdx.x;
if(blk >= blocks || thd >= threads)
return;
size_t regions_per_warp = len / (BLOCK_SIZE * num_warps);
int id = blk * threads + thd;
head[blk * threads + thd] = (regions_per_warp * BLOCK_SIZE) * (id / 32) + (id % 32) * WORD_SIZE;
tail[blk * threads + thd] = (regions_per_warp * BLOCK_SIZE) * (id / 32 + 1);
if(blk == blocks - 1 && thd >= threads - 32)
tail[blk * threads + thd] = len;
}
static __host__ gpmlog *gpmlog_create_hcl(const char *path, size_t &len, int blocks, int threads, int flags = 0)
{
gpmlog *plog, dummy_log;
cudaMalloc((void **)&plog, sizeof(gpmlog));
dummy_log.path = path;
size_t extra_len = len / (blocks * threads);
extra_len = (extra_len + 3) / 4;
// Convert len into BLOCK_SIZE byte blocks
len = (len + BLOCK_SIZE - 1) / BLOCK_SIZE * BLOCK_SIZE;
int num_warps = (blocks * ((threads + WARP_SIZE - 1) / WARP_SIZE));
if(len < BLOCK_SIZE * num_warps * extra_len)
len = BLOCK_SIZE * num_warps * extra_len;
//printf("Len: %lu\n", len);
size_t header_size = sizeof(gpu_gpmlog) + 2 * sizeof(size_t) * blocks * threads;
// Calculate log size = size of metadata + (heads + tails) + offset + actual log (starting at BLOCK_SIZE byte offset)
dummy_log.log_size = header_size + (header_size % BLOCK_SIZE != 0 ? BLOCK_SIZE - header_size % BLOCK_SIZE : 0) + len;
// Create persistent memory chunk for persistent part
void *log_pointer = gpm_map_file(path, dummy_log.log_size, 1);
// Set appropriate pointer locations
dummy_log.plog = (gpu_gpmlog *)log_pointer;
// Shift appropriate amount for next pointer
log_pointer = (char *)log_pointer + sizeof(gpu_gpmlog);
// Assign values for persistent log metadata
gpu_gpmlog temp_log;
temp_log.partitions = blocks * threads;
temp_log.head = (size_t *)log_pointer;
log_pointer = (char *)log_pointer + sizeof(size_t) * blocks * threads;
temp_log.tail = (size_t *)log_pointer;
temp_log.byte_size = len;
temp_log.flags = flags;
log_pointer = (char *)log_pointer + sizeof(size_t) * blocks * threads +
(header_size % BLOCK_SIZE != 0 ? BLOCK_SIZE - header_size % BLOCK_SIZE : 0);
dummy_log.start = log_pointer;
cudaMemcpy(dummy_log.plog, &temp_log, sizeof(gpu_gpmlog), cudaMemcpyHostToDevice);
setupPartitionsManaged<<<blocks, threads>>> (temp_log.head, temp_log.tail, blocks, threads, len);
cudaMemcpy(plog, &dummy_log, sizeof(gpmlog), cudaMemcpyHostToDevice);
return plog;
}
static __host__ gpmlog *gpmlog_open(const char *path)
{
size_t len = 0;
char *start = (char *)gpm_map_file(path, len, false);
// Create volatile metadata
gpmlog *plog, *real_plog;
// TODO: make this cudaMalloc
cudaMallocHost((void **)&plog, sizeof(gpmlog));
plog->path = path;
plog->plog = (gpu_gpmlog *)start;
plog->log_size = len;
start = start + sizeof(gpu_gpmlog);
setup_log<<<1, 1>>>(plog, start, len);
cudaDeviceSynchronize();
// Create locks if necessary
gpu_gpmlog *temp = new gpu_gpmlog;
cudaMemcpy(temp, plog->plog, sizeof(gpu_gpmlog), cudaMemcpyDeviceToHost);
if(temp->flags & PMEMLOG_UNMANAGED)
{
cudaMalloc((void **)&plog->locks, sizeof(int) * temp->partitions);
cudaMemset(plog->locks, 0, sizeof(int) * temp->partitions);
}
cudaMalloc((void**)&real_plog, sizeof(gpmlog));
cudaMemcpy(real_plog, plog, sizeof(gpmlog), cudaMemcpyHostToDevice);
cudaFreeHost(plog);
return real_plog;
}
static __host__ void gpmlog_close(gpmlog *plog)
{
gpmlog dummy;
cudaMemcpy(&dummy, plog, sizeof(gpmlog), cudaMemcpyDeviceToHost);
gpm_unmap(dummy.plog, dummy.log_size);
cudaFree(plog);
}
static __device__ int gpmlog_insert(gpmlog *plog, void *var, size_t size, int partition = -1)
{
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1) {
return -1;
}
return gpmlog_insert_manual(plog, var, size, partition);
}
else {
return gpmlog_insert_managed(plog, var, size, partition);
}
}
static __device__ int gpmlog_read(gpmlog *plog, void *var, size_t size, int partition = -1)
{
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1) {
return -1;
}
return gpmlog_read_manual(plog, var, size, partition);
}
else {
return gpmlog_read_managed(plog, var, size, partition);
}
}
static __device__ int gpmlog_remove(gpmlog *plog, size_t size, int partition = -1)
{
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1) {
return -1;
}
return gpmlog_remove_manual(plog, size, partition);
}
else {
return gpmlog_remove_managed(plog, size);
}
}
static __device__ int gpmlog_read_remove(gpmlog *plog, void *var, size_t size, int partition = -1)
{
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1) {
return -1;
}
if(partition >= plog->plog->partitions) {
return -1;
}
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
__threadfence();
if(!locked) {
done = true;
size_t start = plog->plog->head[partition];
if((partition == 0 && start < size) || (partition != 0 && start < plog->plog->tail[partition - 1] + size)) {
if(partition == 0)
size = start;
else
size = start - plog->plog->tail[partition - 1];
}
// Read data stored in log
vol_memcpy(var, (char *)plog->start + start - size, size);
// Reduce head to required size
size_t temp = plog->plog->head[partition] - size;
if(PMEMLOG_UNSTRICT & plog->plog->flags) {
gpm_memcpy_nodrain(&plog->plog->head[partition], &temp, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
else {
gpm_memcpy(&plog->plog->head[partition], &temp, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
// Unlock
__threadfence();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
return size;
}
else {
int tid = getGlobalIdx();
if(partition != -1)
tid = partition;
// Check for underflow condition
size_t head = plog->plog->head[tid] - (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]);
if(head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE < size) {
size = head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE;
}
int i = plog->plog->head[tid], j = size;
for(;j > 0;) {
// Reached end of WORD_SIZE byte segment
// move to previous segment
if(i % WORD_SIZE == 0) {
// next address = current address - BLOCK_SIZE + WORD_SIZE
i -= BLOCK_SIZE - WORD_SIZE;
}
// Read up to a word at a time
int sz = 1;
if(i % WORD_SIZE == 0 && j >= WORD_SIZE)
sz = WORD_SIZE;
else if(j >= i % WORD_SIZE)
sz = i % WORD_SIZE;
else
sz = j;
i -= sz;
j -= sz;
vol_memcpy((char *)plog->start + i, (char *)var + j, sz);
}
// Calculate size of stack after removal
int new_head = head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE - size;
// Move head to appropriate position
int offset = (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]) + (tid % WARP_SIZE) * WORD_SIZE;
new_head = new_head / WORD_SIZE * BLOCK_SIZE + new_head % WORD_SIZE + offset;
// Drain if necessary
if(PMEMLOG_UNSTRICT & plog->plog->flags) {
gpm_memcpy_nodrain(&plog->plog->head[tid], &new_head, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
else {
gpm_memcpy(&plog->plog->head[tid], &new_head, sizeof(size_t), cudaMemcpyDeviceToDevice);
}
return size;
}
}
static __device__ void gpmlog_clear(gpmlog *plog, int partition = -1)
{
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1) {
return;
}
gpmlog_clear_manual(plog, partition);
}
else {
gpmlog_clear_managed(plog, partition);
}
}
static __device__ int gpmlog_is_empty(gpmlog *plog, int partition = -1)
{
bool empty = false;
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1)
return -1;
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
__threadfence();
if(!locked) {
done = true;
size_t start = plog->plog->head[partition];
if((partition == 0 && start <= 0) || (partition != 0 && start <= plog->plog->tail[partition - 1]))
empty = true;
__threadfence();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
}
else {
int tid = getGlobalIdx();
int head = plog->plog->head[tid] - (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]);
if(head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE <= 0) {
empty = true;
}
}
return empty;
}
static __device__ size_t gpmlog_get_size(gpmlog *plog, int partition = -1)
{
size_t size = 0;
if(plog->plog->flags & PMEMLOG_UNMANAGED) {
if(partition == -1) {
return 0;
}
bool done = false;
do {
// Lock partition
int locked = atomicCAS(&plog->locks[partition], 0, 1);
__threadfence();
if(!locked) {
done = true;
size = plog->plog->head[partition];
if(partition != 0)
size -= plog->plog->tail[partition - 1];
__threadfence();
atomicExch(&plog->locks[partition], 0);
}
} while(!done);
}
else {
int tid;
if(partition == -1)
tid = getGlobalIdx();
else
tid = partition;
size_t head = plog->plog->head[tid] - (tid < WARP_SIZE ? 0 : plog->plog->tail[tid - WARP_SIZE]);
//printf("Head at %lu\n", head);
size = head / BLOCK_SIZE * WORD_SIZE + head % WORD_SIZE;
}
return size;
}
static __host__ __device__ int gpmlog_get_partitions(gpmlog *plog)
{
#if defined(__CUDA_ARCH__)
return plog->plog->partitions;
#else
gpmlog dummy;
cudaMemcpy(&dummy, plog, sizeof(gpmlog), cudaMemcpyDeviceToHost);
gpu_gpmlog nv_log;
cudaMemcpy(&nv_log, dummy.plog, sizeof(gpu_gpmlog), cudaMemcpyDeviceToHost);
return nv_log.partitions;
#endif
}