forked from kaigai/pg_strom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencl_common.h
More file actions
1625 lines (1457 loc) · 49.4 KB
/
opencl_common.h
File metadata and controls
1625 lines (1457 loc) · 49.4 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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* opencl_common.h
*
* A common header for OpenCL device code
* --
* Copyright 2011-2014 (C) KaiGai Kohei <kaigai@kaigai.gr.jp>
* Copyright 2014 (C) The PG-Strom Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef OPENCL_COMMON_H
#define OPENCL_COMMON_H
/*
* OpenCL intermediator always adds -DOPENCL_DEVICE_CODE on kernel build,
* but not for the host code, so this #if ... #endif block is available
* only OpenCL device code.
*/
#ifdef OPENCL_DEVICE_CODE
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
/* NULL definition */
#ifndef NULL
#define NULL ((__global void *) 0UL)
#endif
/* Misc definitions */
#define FLEXIBLE_ARRAY_MEMBER 0
#define offsetof(TYPE, FIELD) ((uintptr_t) &((TYPE *)0)->FIELD)
#define lengthof(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0]))
#define BITS_PER_BYTE 8
/* basic type definitions */
typedef bool cl_bool;
typedef char cl_char;
typedef uchar cl_uchar;
typedef short cl_short;
typedef ushort cl_ushort;
typedef int cl_int;
typedef uint cl_uint;
typedef long cl_long;
typedef ulong cl_ulong;
typedef float cl_float;
typedef double cl_double;
#if HOSTPTRLEN == 8
typedef cl_ulong hostptr_t;
typedef cl_ulong Datum;
#elif HOSTPTRLEN == 4
typedef cl_uint hostptr_t;
typedef cl_uint Datum;
#else
#error unexpected host pointer length
#endif /* HOSTPTRLEN */
#define INT64CONST(x) ((cl_long) x##L)
#define UINT64CONST(x) ((cl_ulong) x##UL)
/*
* Alignment macros
*/
#define TYPEALIGN(ALIGNVAL,LEN) \
(((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
#define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
(((uintptr_t) (LEN)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
#define INTALIGN(LEN) TYPEALIGN(sizeof(cl_int), (LEN))
#define INTALIGN_DOWN(LEN) TYPEALIGN_DOWN(sizeof(cl_int), (LEN))
#define LONGALIGN(LEN) TYPEALIGN(sizeof(cl_long), (LEN))
#define LONGALIGN_DOWN(LEN) TYPEALIGN_DOWN(sizeof(cl_long), (LEN))
#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
/*
* MEMO: We takes dynamic local memory using cl_ulong data-type because of
* alignment problem. The nvidia's driver adjust alignment of local memory
* according to the data type; 1byte for cl_char, 4bytes for cl_uint and
* so on. Unexpectedly, void * pointer has 1byte alignment even if it is
* expected to be casted another data types.
* A pragma option __attribute__((aligned)) didn't work at least driver
* version 340.xx. So, we declared the local_workmem as cl_ulong * pointer
* as a workaround.
*/
#define KERN_DYNAMIC_LOCAL_WORKMEM_ARG \
__local cl_ulong *__pgstrom_local_workmem
#define LOCAL_WORKMEM (__local void *)(__pgstrom_local_workmem)
#else /* OPENCL_DEVICE_CODE */
#include "access/htup_details.h"
#include "storage/itemptr.h"
#define __global /* address space qualifier is noise on host */
#define __local /* address space qualifier is noise on host */
#define __private /* address space qualifier is noise on host */
#define __constant /* address space qualifier is noise on host */
typedef uintptr_t hostptr_t;
#endif
/*
* Error code definition
*/
#define StromError_Success 0 /* OK */
#define StromError_RowFiltered 1 /* Row-clause was false */
#define StromError_CpuReCheck 2 /* To be re-checked by CPU */
#define StromError_ServerNotReady 100 /* OpenCL server is not ready */
#define StromError_BadRequestMessage 101 /* Bad request message */
#define StromError_OpenCLInternal 102 /* OpenCL internal error */
#define StromError_OutOfSharedMemory 105 /* out of shared memory */
#define StromError_OutOfMemory 106 /* out of host memory */
#define StromError_DataStoreCorruption 300 /* Row/Column Store Corrupted */
#define StromError_DataStoreNoSpace 301 /* No Space in Row/Column Store */
#define StromError_DataStoreOutOfRange 302 /* Out of range in Data Store */
#define StromError_DataStoreReCheck 303 /* Row/Column Store be rechecked */
#define StromError_SanityCheckViolation 999 /* SanityCheckViolation */
/* significant error; that abort transaction on the host code */
#define StromErrorIsSignificant(errcode) ((errcode) >= 100 || (errcode) < 0)
#ifdef OPENCL_DEVICE_CODE
/*
* It sets an error code unless no significant error code is already set.
* Also, CpuReCheck has higher priority than RowFiltered because CpuReCheck
* implies device cannot run the given expression completely.
* (Usually, due to compressed or external varlena datum)
*/
static inline void
STROM_SET_ERROR(__private cl_int *p_error, cl_int errcode)
{
cl_int oldcode = *p_error;
if (StromErrorIsSignificant(errcode))
{
if (!StromErrorIsSignificant(oldcode))
*p_error = errcode;
}
else if (errcode > oldcode)
*p_error = errcode;
}
/*
* Data type definitions for row oriented data format
* ---------------------------------------------------
*/
/*
* Local definition of PageHeaderData and related
*/
typedef cl_ushort LocationIndex;
typedef struct
{
cl_uint xlogid; /* high bits */
cl_uint xrecoff; /* low bits */
} PageXLogRecPtr;
typedef cl_uint TransactionId;
/*
* NOTE: ItemIdData is defined using bit-fields in PostgreSQL, however,
* OpenCL does not support bit-fields. So, we deal with ItemIdData as
* a 32bit width integer variable, even though lower 15bits are lp_off,
* higher 15bits are lp_len, and rest of 2bits are lp_flags.
*
* FIXME: Host should tell how to handle bitfield layout
*/
typedef cl_uint ItemIdData;
typedef __global ItemIdData ItemId;
#define ItemIdGetLength(itemId) (((itemId) >> ITEMID_LENGTH_SHIFT) & 0x7fff)
#define ItemIdGetOffset(itemId) (((itemId) >> ITEMID_OFFSET_SHIFT) & 0x7fff)
#define ItemIdGetFlags(itemId) (((itemId) >> ITEMID_FLAGS_SHIFT) & 0x0003)
typedef struct PageHeaderData
{
/* XXX LSN is member of *any* block, not only page-organized ones */
PageXLogRecPtr pd_lsn; /* LSN: next byte after last byte of xlog
* record for last change to this page */
cl_ushort pd_checksum; /* checksum */
cl_ushort pd_flags; /* flag bits, see below */
LocationIndex pd_lower; /* offset to start of free space */
LocationIndex pd_upper; /* offset to end of free space */
LocationIndex pd_special; /* offset to start of special space */
cl_ushort pd_pagesize_version;
TransactionId pd_prune_xid; /* oldest prunable XID, or zero if none */
ItemIdData pd_linp[1]; /* beginning of line pointer array */
} PageHeaderData;
typedef __global PageHeaderData *PageHeader;
#define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp))
#define PageGetMaxOffsetNumber(page) \
(((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData ? 0 : \
((((PageHeader) (page))->pd_lower - SizeOfPageHeaderData) \
/ sizeof(ItemIdData)))
/*
* We need to re-define HeapTupleHeaderData and t_infomask related stuff
*/
typedef struct {
struct {
cl_ushort bi_hi;
cl_ushort bi_lo;
} ip_blkid;
cl_ushort ip_posid;
} ItemPointerData;
typedef struct {
union {
struct {
cl_uint t_xmin; /* inserting xact ID */
cl_uint t_xmax; /* deleting or locking xact ID */
union {
cl_uint t_cid; /* inserting or deleting command ID, or both */
cl_uint t_xvac; /* old-style VACUUM FULL xact ID */
} t_field3;
} t_heap;
struct {
cl_uint datum_len_; /* varlena header (do not touch directly!) */
cl_uint datum_typmod; /* -1, or identifier of a record type */
cl_uint datum_typeid; /* composite type OID, or RECORDOID */
} t_datum;
} t_choice;
ItemPointerData t_ctid; /* current TID of this or newer tuple */
cl_ushort t_infomask2; /* number of attributes + various flags */
cl_ushort t_infomask; /* various flag bits, see below */
cl_uchar t_hoff; /* sizeof header incl. bitmap, padding */
/* ^ - 23 bytes - ^ */
cl_uchar t_bits[1]; /* bitmap of NULLs -- VARIABLE LENGTH */
} HeapTupleHeaderData;
#define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
#define bitmaplen(NATTS) (((int)(NATTS) + BITS_PER_BYTE - 1) / BITS_PER_BYTE)
/*
* information stored in t_infomask:
*/
#define HEAP_HASNULL 0x0001 /* has null attribute(s) */
#define HEAP_HASVARWIDTH 0x0002 /* has variable-width attribute(s) */
#define HEAP_HASEXTERNAL 0x0004 /* has external stored attribute(s) */
#define HEAP_HASOID 0x0008 /* has an object-id field */
#define HEAP_XMAX_KEYSHR_LOCK 0x0010 /* xmax is a key-shared locker */
#define HEAP_COMBOCID 0x0020 /* t_cid is a combo cid */
#define HEAP_XMAX_EXCL_LOCK 0x0040 /* xmax is exclusive locker */
#define HEAP_XMAX_LOCK_ONLY 0x0080 /* xmax, if valid, is only a locker */
/*
* information stored in t_infomask2:
*/
#define HEAP_NATTS_MASK 0x07FF /* 11 bits for number of attributes */
#define HEAP_KEYS_UPDATED 0x2000 /* tuple was updated and key cols
* modified, or tuple deleted */
#define HEAP_HOT_UPDATED 0x4000 /* tuple was HOT-updated */
#define HEAP_ONLY_TUPLE 0x8000 /* this is heap-only tuple */
#define HEAP2_XACT_MASK 0xE000 /* visibility-related bits */
#endif
/*
* alignment for pg-strom
*/
#define STROMALIGN_LEN 16
#define STROMALIGN(LEN) TYPEALIGN(STROMALIGN_LEN,LEN)
#define STROMALIGN_DOWN(LEN) TYPEALIGN_DOWN(STROMALIGN_LEN,LEN)
/*
* kern_data_store
*
* It stores row- and column-oriented values in the kernel space.
*
* +---------------------------------------------------+
* | length |
* +---------------------------------------------------+
* | ncols |
* +---------------------------------------------------+
* | nitems |
* +---------------------------------------------------+
* | nrooms |
* +---------------------------------------------------+
* | format |
* +---------------------------------------------------+
* | colmeta[0] | aligned to
* | colmeta[1] | STROMALIGN()
* | : | |
* | colmeta[M-1] | V
* +----------------+-----------------+----------------+-------
* | <row-format> | <row-flat-form> |<tupslot-format>|
* +----------------+-----------------+----------------+
* | blkitems[0] | rowitems[0] | values/isnull |
* | blkitems[1] | rowitems[1] | pair of the |
* | : | rowitems[2] | 1st row |
* | blkitems[N-1] | : | +--------------+
* +----------------+ rowitems[N-2] | values[0] |
* | rowitems[0] | rowitems[N-1] | : |
* | rowitems[1] +-----------------+ values[M-1] |
* | rowitems[2] | | +--------------+
* | : +-----------------+ | isnull[0] |
* | rowitems[K-1] | HeapTupleData | | : |
* +----------------+ tuple[N-1] | | isnull[M-1] |
* | alignment to ..| <contents> +-+--------------+
* | BLCKSZ.........+-----------------+ values/isnull |
* +----------------+ : | pair of the |
* | blocks[0] | : | 2nd row |
* | PageHeaderData | Row-flat form | +--------------+
* | : | consumes buffer | | values[0] |
* | pd_linep[] | | | : |
* | : | | | values[M-1] |
* +----------------+ ^ | +--------------+
* | blocks[1] | : | | isnull[0] |
* | PageHeaderData | : | | : |
* | : | : | | isnull[M-1] |
* | pd_linep[] | : +-+--------------+
* | : | : | : |
* +----------------+ : +----------------+
* | : +-----------------+ values/isnull |
* | : | HeapTupleData | pair of the |
* +----------------+ tuple[1] | Nth row |
* | blocks[N-1] | <contents> | +--------------+
* | PageHeaderData +-----------------+ | values[0] |
* | : | HeapTupleData | | : |
* | pd_linep[] | tuple[0] | | values[M-1] |
* | : | <contents> | | : |
* +----------------+-----------------+-+--------------+
*/
typedef struct {
/* true, if column is held by value. Elsewhere, a reference */
cl_char attbyval;
/* alignment; 1,2,4 or 8, not characters in pg_attribute */
cl_char attalign;
/* length of attribute */
cl_short attlen;
/* attribute number */
cl_short attnum;
/* offset of attribute location, if deterministic */
cl_short attcacheoff;
} kern_colmeta;
/*
* kern_rowitem packs an index of block and tuple item.
* block_id points a particular block in the block array of this data-store.
* item_id points a particular tuple item within the block.
*/
typedef union {
struct {
cl_ushort blk_index; /* if ROW format */
cl_ushort item_offset; /* if ROW format */
};
cl_uint htup_offset; /* if FLAT_ROW format */
} kern_rowitem;
typedef struct {
#ifdef OPENCL_DEVICE_CODE
cl_int buffer;
hostptr_t page;
#else
Buffer buffer;
Page page;
#endif
} kern_blkitem;
#define KDS_FORMAT_ROW 1
#define KDS_FORMAT_ROW_FLAT 2
#define KDS_FORMAT_TUPSLOT 3
typedef struct {
hostptr_t hostptr; /* address of kds on the host */
cl_uint length; /* length of this data-store */
cl_uint usage; /* usage of this data-store */
cl_uint ncols; /* number of columns in this store */
cl_uint nitems; /* number of rows in this store */
cl_uint nrooms; /* number of available rows in this store */
cl_uint nblocks; /* number of blocks in this store */
cl_uint maxblocks; /* max available blocks in this store */
cl_char format; /* one of KDS_FORMAT_* above */
cl_char tdhasoid; /* copy of TupleDesc.tdhasoid */
cl_uint tdtypeid; /* copy of TupleDesc.tdtypeid */
cl_int tdtypmod; /* copy of TupleDesc.tdtypmod */
kern_colmeta colmeta[FLEXIBLE_ARRAY_MEMBER]; /* metadata of columns */
} kern_data_store;
/* access macro for row-format */
#define KERN_DATA_STORE_BLKITEM(kds,blk_index) \
(((__global kern_blkitem *) \
((__global cl_char *)(kds) + \
STROMALIGN(offsetof(kern_data_store, \
colmeta[(kds)->ncols])))) \
+ (blk_index))
#define KERN_DATA_STORE_ROWITEM(kds,row_index) \
(((__global kern_rowitem *) \
((__global cl_char *)(kds) + \
STROMALIGN(offsetof(kern_data_store, \
colmeta[(kds)->ncols])) + \
STROMALIGN(sizeof(kern_blkitem) * (kds)->maxblocks))) \
+ (row_index))
#define KERN_DATA_STORE_ROWBLOCK(kds,blk_index) \
((__global PageHeader) \
((__global cl_char *)(kds) + \
(TYPEALIGN(BLCKSZ, \
STROMALIGN(offsetof(kern_data_store, \
colmeta[(kds)->ncols])) + \
STROMALIGN(sizeof(kern_blkitem) * (kds)->maxblocks) + \
STROMALIGN(sizeof(kern_rowitem) * (kds)->nitems)) \
+ BLCKSZ * (blk_index))))
/* access macro for tuple-slot format */
#define KERN_DATA_STORE_VALUES(kds,row_index) \
((__global Datum *) \
(((__global cl_char *)(kds) + \
STROMALIGN(offsetof(kern_data_store, \
colmeta[(kds)->ncols]))) + \
LONGALIGN((sizeof(Datum) + \
sizeof(cl_char)) * (kds)->ncols) * (row_index)))
#define KERN_DATA_STORE_ISNULL(kds,row_index) \
((__global cl_char *) \
(KERN_DATA_STORE_VALUES((kds),(row_index)) + (kds)->ncols))
/* length of kern_data_store */
#define KERN_DATA_STORE_LENGTH(kds) \
((kds)->format == KDS_FORMAT_ROW ? \
((uintptr_t)KERN_DATA_STORE_ROWBLOCK((kds), (kds)->nblocks) - \
(uintptr_t)(kds)) : \
STROMALIGN((kds)->length))
/*
* kern_parambuf
*
* Const and Parameter buffer. It stores constant values during a particular
* scan, so it may make sense if it is obvious length of kern_parambuf is
* less than constant memory (NOTE: not implemented yet).
*/
typedef struct {
cl_uint length; /* total length of parambuf */
cl_uint nparams; /* number of parameters */
cl_uint poffset[FLEXIBLE_ARRAY_MEMBER]; /* offset of params */
} kern_parambuf;
static inline __global void *
kparam_get_value(__global kern_parambuf *kparams, cl_uint pindex)
{
if (pindex >= kparams->nparams)
return NULL;
if (kparams->poffset[pindex] == 0)
return NULL;
return (__global char *)kparams + kparams->poffset[pindex];
}
/*
* kern_resultbuf
*
* Output buffer to write back calculation results on a parciular chunk.
* 'errcode' informs a significant error that shall raise an error on
* host side and abort transactions. 'results' informs row-level status.
*/
typedef struct {
cl_uint nrels; /* number of relations to be appeared */
cl_uint nrooms; /* max number of results rooms */
cl_uint nitems; /* number of results being written */
cl_int errcode; /* chunk-level error */
cl_char has_rechecks;
cl_char all_visible;
cl_char __padding__[2];
cl_int results[FLEXIBLE_ARRAY_MEMBER];
} kern_resultbuf;
/*
* kern_row_map
*
* It informs kernel code which rows are valid, and which ones are not, if
* kern_data_store contains mixed
*/
typedef struct {
cl_int nvalids; /* # of valid rows. -1 means all visible */
cl_int rindex[FLEXIBLE_ARRAY_MEMBER];
} kern_row_map;
#ifdef OPENCL_DEVICE_CODE
/*
* PostgreSQL Data Type support in OpenCL kernel
*
* A stream of data sequencea are moved to OpenCL kernel, according to
* the above row-/column-store definition. The device code being generated
* by PG-Strom deals with each data item using the following data type;
*
* typedef struct
* {
* BASE value;
* bool isnull;
* } pg_##NAME##_t
*
* PostgreSQL has three different data classes:
* - fixed-length referenced by value
* - fixed-length referenced by pointer
* - variable-length value
*
* Right now, we support the two except for fixed-length referenced by
* pointer (because these are relatively minor data type than others).
* BASE reflects the data type in PostgreSQL; may be an integer, a float
* or something others, however, all the variable-length value has same
* BASE type; that is an offset of associated toast buffer, to reference
* varlena structure on the global memory.
*/
/* forward declaration of access interface to kern_data_store */
static __global void *kern_get_datum(__global kern_data_store *kds,
__global kern_data_store *ktoast,
cl_uint colidx, cl_uint rowidx);
/* forward declaration of writer interface to kern_data_store */
static __global Datum *pg_common_vstore(__global kern_data_store *kds,
__global kern_data_store *ktoast,
__private int *errcode,
cl_uint colidx, cl_uint rowidx,
cl_bool isnull);
/*
* Template of variable classes: fixed-length referenced by value
* ---------------------------------------------------------------
*/
#define STROMCL_SIMPLE_DATATYPE_TEMPLATE(NAME,BASE) \
typedef struct { \
BASE value; \
bool isnull; \
} pg_##NAME##_t;
#define STROMCL_SIMPLE_VARREF_TEMPLATE(NAME,BASE) \
static pg_##NAME##_t \
pg_##NAME##_vref(__global kern_data_store *kds, \
__global kern_data_store *ktoast, \
__private int *errcode, \
cl_uint colidx, \
cl_uint rowidx) \
{ \
pg_##NAME##_t result; \
__global BASE *addr \
= kern_get_datum(kds,ktoast,colidx,rowidx); \
\
if (!addr) \
result.isnull = true; \
else \
{ \
result.isnull = false; \
result.value = *addr; \
} \
return result; \
}
#define STROMCL_SIMPLE_VARSTORE_TEMPLATE(NAME,BASE) \
static void \
pg_##NAME##_vstore(__global kern_data_store *kds, \
__global kern_data_store *ktoast, \
__private int *errcode, \
cl_uint colidx, \
cl_uint rowidx, \
pg_##NAME##_t datum) \
{ \
__global Datum *daddr; \
union { \
BASE v_base; \
Datum v_datum; \
} temp; \
daddr = pg_common_vstore(kds, ktoast, errcode, \
colidx, rowidx, \
datum.isnull); \
if (daddr) \
{ \
temp.v_datum = 0; \
temp.v_base = datum.value; \
*daddr = temp.v_datum; \
} \
}
#define STROMCL_SIMPLE_PARAMREF_TEMPLATE(NAME,BASE) \
static pg_##NAME##_t \
pg_##NAME##_param(__global kern_parambuf *kparams, \
__private int *errcode, \
cl_uint param_id) \
{ \
pg_##NAME##_t result; \
__global BASE *addr; \
\
if (param_id < kparams->nparams && \
kparams->poffset[param_id] > 0) \
{ \
result.value = *((__global BASE *) \
((__global char *)kparams + \
kparams->poffset[param_id])); \
result.isnull = false; \
} \
else \
result.isnull = true; \
\
return result; \
}
#define STROMCL_SIMPLE_NULLTEST_TEMPLATE(NAME) \
static pg_bool_t \
pgfn_##NAME##_isnull(__private int *errcode, \
pg_##NAME##_t arg) \
{ \
pg_bool_t result; \
\
result.isnull = false; \
result.value = arg.isnull; \
return result; \
} \
\
static pg_bool_t \
pgfn_##NAME##_isnotnull(__private int *errcode, \
pg_##NAME##_t arg) \
{ \
pg_bool_t result; \
\
result.isnull = false; \
result.value = !arg.isnull; \
return result; \
}
#define STROMCL_SIMPLE_TYPE_TEMPLATE(NAME,BASE) \
STROMCL_SIMPLE_DATATYPE_TEMPLATE(NAME,BASE) \
STROMCL_SIMPLE_VARREF_TEMPLATE(NAME,BASE) \
STROMCL_SIMPLE_VARSTORE_TEMPLATE(NAME,BASE) \
STROMCL_SIMPLE_PARAMREF_TEMPLATE(NAME,BASE) \
STROMCL_SIMPLE_NULLTEST_TEMPLATE(NAME)
/* pg_bool_t */
#ifndef PG_BOOL_TYPE_DEFINED
#define PG_BOOL_TYPE_DEFINED
STROMCL_SIMPLE_TYPE_TEMPLATE(bool, cl_bool)
#endif
/* pg_int2_t */
#ifndef PG_INT2_TYPE_DEFINED
#define PG_INT2_TYPE_DEFINED
STROMCL_SIMPLE_TYPE_TEMPLATE(int2, cl_short)
#endif
/* pg_int4_t */
#ifndef PG_INT4_TYPE_DEFINED
#define PG_INT4_TYPE_DEFINED
STROMCL_SIMPLE_TYPE_TEMPLATE(int4, cl_int)
#endif
/* pg_int8_t */
#ifndef PG_INT8_TYPE_DEFINED
#define PG_INT8_TYPE_DEFINED
STROMCL_SIMPLE_TYPE_TEMPLATE(int8, cl_long)
#endif
/* pg_float4_t */
#ifndef PG_FLOAT4_TYPE_DEFINED
#define PG_FLOAT4_TYPE_DEFINED
STROMCL_SIMPLE_TYPE_TEMPLATE(float4, cl_float)
#endif
/* pg_float8_t */
#ifndef PG_FLOAT8_TYPE_DEFINED
#define PG_FLOAT8_TYPE_DEFINED
STROMCL_SIMPLE_TYPE_TEMPLATE(float8, cl_double)
#endif
/*
* Template of variable classes: variable-length variables
* ---------------------------------------------------------------
*
* Unlike host code, device code cannot touch external and/or compressed
* toast datum. All the format device code can understand is usual
* in-memory form; 4-bytes length is put on the head and contents follows.
* So, it is a responsibility of host code to decompress the toast values
* if device code may access compressed varlena.
* In case when device code touches unsupported format, calculation result
* shall be postponed to calculate on the host side.
*
* Note that it is harmless to have external and/or compressed toast datam
* unless it is NOT referenced in the device code. It can understand the
* length of these values, unlike contents.
*/
typedef struct {
cl_int vl_len;
cl_char vl_dat[1];
} varlena;
#define VARHDRSZ ((int) sizeof(cl_int))
#define VARDATA(PTR) VARDATA_4B(PTR)
#define VARSIZE(PTR) VARSIZE_4B(PTR)
#define VARSIZE_EXHDR(PTR) (VARSIZE(PTR) - VARHDRSZ)
typedef union
{
struct /* Normal varlena (4-byte length) */
{
cl_uint va_header;
cl_char va_data[1];
} va_4byte;
struct /* Compressed-in-line format */
{
cl_uint va_header;
cl_uint va_rawsize; /* Original data size (excludes header) */
cl_char va_data[1]; /* Compressed data */
} va_compressed;
} varattrib_4b;
typedef struct
{
cl_uchar va_header;
cl_char va_data[1]; /* Data begins here */
} varattrib_1b;
/* inline portion of a short varlena pointing to an external resource */
typedef struct
{
cl_uchar va_header; /* Always 0x80 or 0x01 */
cl_uchar va_tag; /* Type of datum */
cl_char va_data[1]; /* Data (of the type indicated by va_tag) */
} varattrib_1b_e;
typedef enum vartag_external
{
VARTAG_INDIRECT = 1,
VARTAG_ONDISK = 18
} vartag_external;
#define VARHDRSZ_SHORT offsetof(varattrib_1b, va_data)
#define VARATT_SHORT_MAX 0x7F
typedef struct varatt_external
{
cl_int va_rawsize; /* Original data size (includes header) */
cl_int va_extsize; /* External saved size (doesn't) */
cl_int va_valueid; /* Unique ID of value within TOAST table */
cl_int va_toastrelid; /* RelID of TOAST table containing it */
} varatt_external;
typedef struct varatt_indirect
{
hostptr_t pointer; /* Host pointer to in-memory varlena */
} varatt_indirect;
#define VARTAG_SIZE(tag) \
((tag) == VARTAG_INDIRECT ? sizeof(varatt_indirect) : \
(tag) == VARTAG_ONDISK ? sizeof(varatt_external) : \
0 /* should not happen */)
#define VARHDRSZ_EXTERNAL offsetof(varattrib_1b_e, va_data)
#define VARTAG_EXTERNAL(PTR) VARTAG_1B_E(PTR)
#define VARSIZE_EXTERNAL(PTR) \
(VARHDRSZ_EXTERNAL + VARTAG_SIZE(VARTAG_EXTERNAL(PTR)))
#define VARATT_IS_4B(PTR) \
((((__global varattrib_1b *) (PTR))->va_header & 0x01) == 0x00)
#define VARATT_IS_4B_U(PTR) \
((((__global varattrib_1b *) (PTR))->va_header & 0x03) == 0x00)
#define VARATT_IS_4B_C(PTR) \
((((__global varattrib_1b *) (PTR))->va_header & 0x03) == 0x02)
#define VARATT_IS_1B(PTR) \
((((__global varattrib_1b *) (PTR))->va_header & 0x01) == 0x01)
#define VARATT_IS_1B_E(PTR) \
((((__global varattrib_1b *) (PTR))->va_header) == 0x01)
#define VARATT_IS_COMPRESSED(PTR) VARATT_IS_4B_C(PTR)
#define VARATT_IS_EXTERNAL(PTR) VARATT_IS_1B_E(PTR)
#define VARATT_NOT_PAD_BYTE(PTR) \
(*((__global cl_uchar *) (PTR)) != 0)
#define VARSIZE_4B(PTR) \
((((__global varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
#define VARSIZE_1B(PTR) \
((((__global varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
#define VARTAG_1B_E(PTR) \
(((__global varattrib_1b_e *) (PTR))->va_tag)
#define VARSIZE_ANY_EXHDR(PTR) \
(VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR)-VARHDRSZ_EXTERNAL : \
(VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR)-VARHDRSZ_SHORT : \
VARSIZE_4B(PTR)-VARHDRSZ))
#define VARSIZE_ANY(PTR) \
(VARATT_IS_1B_E(PTR) ? VARSIZE_EXTERNAL(PTR) : \
(VARATT_IS_1B(PTR) ? VARSIZE_1B(PTR) : \
VARSIZE_4B(PTR)))
#define VARDATA_4B(PTR) (((__global varattrib_4b *) (PTR))->va_4byte.va_data)
#define VARDATA_1B(PTR) (((__global varattrib_1b *) (PTR))->va_data)
#define VARDATA_ANY(PTR) \
(VARATT_IS_1B(PTR) ? VARDATA_1B(PTR) : VARDATA_4B(PTR))
#define SET_VARSIZE(PTR, len) \
(((__global varattrib_4b *) \
(PTR))->va_4byte.va_header = (((cl_uint) (len)) << 2))
/*
* kern_get_datum
*
* Reference to a particular datum on the supplied kernel data store.
* It returns NULL, if it is a really null-value in context of SQL,
* or in case when out of range with error code
*
* NOTE: We are paranoia for validation of the data being fetched from
* the kern_data_store in row-format because we may see a phantom page
* if the source transaction that required this kernel execution was
* aborted during execution.
* Once a transaction gets aborted, shared buffers being pinned are
* released, even if DMA send request on the buffers are already
* enqueued. In this case, the calculation result shall be discarded,
* so no need to worry about correctness of the calculation, however,
* needs to be care about address of the variables being referenced.
*/
static inline __global void *
kern_get_datum_tuple(__global kern_colmeta *colmeta,
__global HeapTupleHeaderData *htup,
cl_uint colidx)
{
cl_bool heap_hasnull = ((htup->t_infomask & HEAP_HASNULL) != 0);
cl_uint offset = htup->t_hoff;
cl_uint i, ncols = (htup->t_infomask2 & HEAP_NATTS_MASK);
/* shortcut if colidx is obviously out of range */
if (colidx >= ncols)
return NULL;
/* shortcut if tuple contains no NULL values */
if (!heap_hasnull)
{
kern_colmeta cmeta = colmeta[colidx];
if (cmeta.attcacheoff >= 0)
return (__global char *)htup + cmeta.attcacheoff;
}
/* regular path that walks on heap-tuple from the head */
for (i=0; i < ncols; i++)
{
if (heap_hasnull && att_isnull(i, htup->t_bits))
{
if (i == colidx)
return NULL;
}
else
{
__global char *addr;
kern_colmeta cmeta = colmeta[i];
if (cmeta.attlen > 0)
offset = TYPEALIGN(cmeta.attalign, offset);
else if (!VARATT_NOT_PAD_BYTE((__global char *)htup + offset))
offset = TYPEALIGN(cmeta.attalign, offset);
/* TODO: overrun checks here */
addr = ((__global char *) htup + offset);
if (i == colidx)
return addr;
offset += (cmeta.attlen > 0
? cmeta.attlen
: VARSIZE_ANY(addr));
}
}
return NULL;
}
static inline __global HeapTupleHeaderData *
kern_get_tuple_rs(__global kern_data_store *kds, cl_uint rowidx,
__private cl_uint *p_blk_index)
{
__global kern_rowitem *kritem;
PageHeader page;
cl_ushort blk_index;
cl_ushort item_offset;
cl_ushort item_max;
ItemIdData item_id;
if (rowidx >= kds->nitems)
return NULL; /* likely a BUG */
kritem = KERN_DATA_STORE_ROWITEM(kds, rowidx);
blk_index = kritem->blk_index;
item_offset = kritem->item_offset;
if (blk_index >= kds->nblocks)
return NULL; /* likely a BUG */
page = KERN_DATA_STORE_ROWBLOCK(kds, blk_index);
item_max = PageGetMaxOffsetNumber(page);
if (offsetof(PageHeaderData, pd_linp[item_max + 1]) >= BLCKSZ ||
item_offset == 0 || item_offset > item_max)
return NULL; /* likely a BUG */
item_id = page->pd_linp[item_offset - 1];
if (ItemIdGetOffset(item_id) +
offsetof(HeapTupleHeaderData, t_bits) >= BLCKSZ)
return NULL; /* likely a BUG */
/* also set additional properties */
*p_blk_index = blk_index;
return (__global HeapTupleHeaderData *)
((__global char *)page + ItemIdGetOffset(item_id));
}
static inline __global void *
kern_get_datum_rs(__global kern_data_store *kds,
cl_uint colidx, cl_uint rowidx)
{
__global HeapTupleHeaderData *htup;
cl_uint blkidx;
if (colidx >= kds->ncols)
return NULL; /* likely a BUG */
htup = kern_get_tuple_rs(kds, rowidx, &blkidx);
if (!htup)
return NULL;
return kern_get_datum_tuple(kds->colmeta, htup, colidx);
}
static inline __global HeapTupleHeaderData *
kern_get_tuple_rsflat(__global kern_data_store *kds, cl_uint rowidx)
{
__global kern_rowitem *kritem;
if (rowidx >= kds->nitems)
return NULL; /* likely a BUG */
kritem = KERN_DATA_STORE_ROWITEM(kds, rowidx);
/* simple sanity check */
if (kritem->htup_offset >= kds->length)
return NULL;
return (__global HeapTupleHeaderData *)
((__global char *)kds + kritem->htup_offset);
}
static inline __global void *
kern_get_datum_rsflat(__global kern_data_store *kds,
cl_uint colidx, cl_uint rowidx)
{
__global HeapTupleHeaderData *htup;
if (colidx >= kds->ncols)
return NULL; /* likely a BUG */
htup = kern_get_tuple_rsflat(kds, rowidx);
if (!htup)
return NULL;
return kern_get_datum_tuple(kds->colmeta, htup, colidx);
}
static __global void *
kern_get_datum_tupslot(__global kern_data_store *kds,
__global kern_data_store *ktoast,
cl_uint colidx, cl_uint rowidx)
{
__global Datum *values = KERN_DATA_STORE_VALUES(kds,rowidx);
__global cl_char *isnull = KERN_DATA_STORE_ISNULL(kds,rowidx);
kern_colmeta cmeta = kds->colmeta[colidx];
if (isnull[colidx])
return NULL;
if (cmeta.attlen > 0)
return values + colidx;
return (__global char *)ktoast + values[colidx];
}
static inline __global void *
kern_get_datum(__global kern_data_store *kds,
__global kern_data_store *ktoast,
cl_uint colidx, cl_uint rowidx)
{
/* is it out of range? */
if (colidx >= kds->ncols || rowidx >= kds->nitems)
return NULL;
if (kds->format == KDS_FORMAT_ROW)
return kern_get_datum_rs(kds, colidx, rowidx);
if (kds->format == KDS_FORMAT_ROW_FLAT)
return kern_get_datum_rsflat(kds, colidx, rowidx);
if (kds->format == KDS_FORMAT_TUPSLOT)
return kern_get_datum_tupslot(kds,ktoast,colidx,rowidx);
/* TODO: put StromError_DataStoreCorruption error here */
return NULL;
}
/*
* common function to store a value on tuple-slot format
*/
static __global Datum *
pg_common_vstore(__global kern_data_store *kds,
__global kern_data_store *ktoast,
__private int *errcode,
cl_uint colidx, cl_uint rowidx,
cl_bool isnull)
{
kern_colmeta cmeta;
__global Datum *slot_values;
__global cl_char *slot_isnull;
/*
* Only tuple-slot is acceptable destination format.
* Only row- and row-flat are acceptable source format.
*/
if (kds->format != KDS_FORMAT_TUPSLOT ||