forked from heterodb/pg-strom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpu_common.h
3394 lines (3095 loc) · 97.5 KB
/
xpu_common.h
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
/*
* xpu_common.h
*
* Common header portion for both of GPU and DPU device code
* --
* Copyright 2011-2023 (C) KaiGai Kohei <[email protected]>
* Copyright 2014-2023 (C) PG-Strom Developers Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the PostgreSQL License.
*/
#ifndef XPU_COMMON_H
#define XPU_COMMON_H
#include <alloca.h>
#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
/*
* Functions with qualifiers
*/
#ifndef PGDLLEXPORT
#define PGDLLEXPORT
#endif
#if defined(__CUDACC__)
/* CUDA C++ */
#define INLINE_FUNCTION(RET_TYPE) \
__device__ __forceinline__ \
static RET_TYPE __attribute__ ((unused))
#define STATIC_FUNCTION(RET_TYPE) __device__ static RET_TYPE
#define PUBLIC_FUNCTION(RET_TYPE) __device__ RET_TYPE
#define EXTERN_FUNCTION(RET_TYPE) extern "C" __device__ RET_TYPE
#define KERNEL_FUNCTION(RET_TYPE) extern "C" __global__ RET_TYPE
#define EXTERN_DATA extern "C" __device__
#define PUBLIC_DATA __device__
#define STATIC_DATA static __device__
#elif defined(__cplusplus)
/* C++ */
#include <cstdio> /* for printf in C++ */
#define INLINE_FUNCTION(RET_TYPE) static inline RET_TYPE
#define STATIC_FUNCTION(RET_TYPE) static RET_TYPE
#define PUBLIC_FUNCTION(RET_TYPE) PGDLLEXPORT RET_TYPE
#define KERNEL_FUNCTION(RET_TYPE) extern "C" RET_TYPE
#define EXTERN_FUNCTION(RET_TYPE) extern "C" RET_TYPE
#define EXTERN_DATA extern "C"
#define PUBLIC_DATA
#define STATIC_DATA static
#else
/* C */
#define INLINE_FUNCTION(RET_TYPE) static inline RET_TYPE
#define STATIC_FUNCTION(RET_TYPE) static RET_TYPE
#define PUBLIC_FUNCTION(RET_TYPE) PGDLLEXPORT RET_TYPE
#define KERNEL_FUNCTION(RET_TYPE) RET_TYPE
#define EXTERN_FUNCTION(RET_TYPE) extern RET_TYPE
#define EXTERN_DATA extern
#define PUBLIC_DATA
#define STATIC_DATA static
#endif /* __CUDACC__ */
/*
* Limitation of types
*/
#ifndef SCHAR_MAX
#define SCHAR_MAX 127
#endif
#ifndef SCHAR_MIN
#define SCHAR_MIN (-128)
#endif
#ifndef UCHAR_MAX
#define UCHAR_MAX 255
#endif
#ifndef SHRT_MAX
#define SHRT_MAX 32767
#endif
#ifndef SHRT_MIN
#define SHRT_MIN (-32767-1)
#endif
#ifndef USHRT_MAX
#define USHRT_MAX 65535
#endif
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef INT_MIN
#define INT_MIN (-INT_MAX - 1)
#endif
#ifndef UINT_MAX
#define UINT_MAX 4294967295U
#endif
#ifndef LONG_MAX
#define LONG_MAX 0x7FFFFFFFFFFFFFFFLL
#endif
#ifndef LONG_MIN
#define LONG_MIN (-LONG_MAX - 1LL)
#endif
#ifndef ULONG_MAX
#define ULONG_MAX 0xFFFFFFFFFFFFFFFFULL
#endif
#ifndef HALF_MAX
#define HALF_MAX __short_as_half__(0x7bff)
#endif
#ifndef FLT_MAX
#define FLT_MAX __int_as_float__(0x7f7fffffU)
#endif
#ifndef DBL_MAX
#define DBL_MAX __longlong_as_double__(0x7fefffffffffffffULL)
#endif
#ifndef DBL_MIN
#define DBL_MIN __longlong_as_double__(0x0010000000000000ULL)
#endif
#ifndef DBL_INF
#define DBL_INF __longlong_as_double__(0x7ff0000000000000ULL)
#endif
#ifndef DBL_NAN
#define DBL_NAN __longlong_as_double__(0x7fffffffffffffffULL)
#endif
#ifndef BITS_PER_BYTE
#define BITS_PER_BYTE 8
#endif
#ifndef SHRT_NBITS
#define SHRT_NBITS (sizeof(int16_t) * BITS_PER_BYTE)
#endif
#ifndef INT_NBITS
#define INT_NBITS (sizeof(int32_t) * BITS_PER_BYTE)
#endif
#ifndef LONG_NBITS
#define LONG_NBITS (sizeof(int64_t) * BITS_PER_BYTE)
#endif
/*
* Several fundamental data types and macros
*/
#ifndef Assert
#define Assert(cond) assert(cond)
#endif
#ifndef Max
#define Max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef Min
#define Min(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef Abs
#define Abs(x) ((x) >= 0 ? (x) : -(x))
#endif
#ifndef POSTGRES_H
typedef uint64_t Datum;
typedef unsigned int Oid;
#define NAMEDATALEN 64 /* must follow the host configuration */
#define BLCKSZ 8192 /* must follow the host configuration */
#ifndef lengthof
#define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
#endif
#define PointerGetDatum(X) ((Datum)(X))
#define DatumGetPointer(X) ((char *)(X))
#define TYPEALIGN(ALIGNVAL,LEN) \
(((uint64_t)(LEN) + ((ALIGNVAL) - 1)) & ~((uint64_t)((ALIGNVAL) - 1)))
#define TYPEALIGN_DOWN(ALIGNVAL,LEN) \
(((uint64_t) (LEN)) & ~((uint64_t) ((ALIGNVAL) - 1)))
#define MAXIMUM_ALIGNOF 8
#define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF,LEN)
#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF,LEN)
#define LONGALIGN(LEN) TYPEALIGN(8,LEN)
#define INTALIGN(LEN) TYPEALIGN(4,LEN)
#endif /* POSTGRES_H */
#define __MAXALIGNED__ __attribute__((aligned(MAXIMUM_ALIGNOF)));
#define MAXIMUM_ALIGNOF_SHIFT 3
/* Definition of several primitive types */
typedef __int128 int128_t;
#include "float2.h"
#ifndef __FILE_NAME__
INLINE_FUNCTION(const char *)
__runtime_file_name(const char *path)
{
const char *s;
for (s = path; *s != '\0'; s++)
{
if (*s == '/')
path = s + 1;
}
return path;
}
#define __FILE_NAME__ __runtime_file_name(__FILE__)
#endif
#ifdef __cplusplus
template <typename T>
INLINE_FUNCTION(T)
__Fetch(const T *ptr)
{
T temp;
#if 0
/*
* MEMO: probably, nvcc expects the 'ptr' is aligned by the caller
* of function, therefore, compiler optimization might consider
* the following if-block is always true, and condition checks can
* be removed. However, __Fetch() is used to the address where we
* cannot guarantee the alignment (e.g, payload of short-varlena).
* So, we always have to use memcpy() for the safe memory access.
*/
if ((sizeof(T) & (sizeof(T)-1)) == 0 &&
(((uintptr_t)ptr) & (sizeof(T)-1)) == 0)
{
return *ptr;
}
#endif
memcpy(&temp, ptr, sizeof(T));
return temp;
}
template <typename T>
INLINE_FUNCTION(void)
__FetchStore(T &dest, const T *ptr)
{
if ((sizeof(T) & (sizeof(T)-1)) == 0 &&
(((uintptr_t)ptr) & (sizeof(T)-1)) == 0)
{
dest = *ptr;
}
else
{
memcpy(&dest, ptr, sizeof(T));
}
}
template <typename T>
INLINE_FUNCTION(T)
__volatileRead(const volatile T *ptr)
{
return *ptr;
}
#else /* __cplusplus */
#define __Fetch(PTR) (*(PTR))
#define __FetchStore(DEST,PTR) do { (DEST) = *(PTR); } while(0)
#define __volatileRead(PTR) (*(PTR))
#endif
INLINE_FUNCTION(int)
__memcmp(const void *__s1, const void *__s2, size_t n)
{
const unsigned char *s1 = (const unsigned char *)__s1;
const unsigned char *s2 = (const unsigned char *)__s2;
for (size_t i=0; i < n; i++)
{
if (s1[i] < s2[i])
return -1;
if (s1[i] > s2[i])
return 1;
}
return 0;
}
INLINE_FUNCTION(int)
__strcmp(const char *s1, const char *s2)
{
unsigned char c1, c2;
do {
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
} while (c1 == c2);
return c1 - c2;
}
/* ----------------------------------------------------------------
*
* Fundamental CUDA definitions
*
* ----------------------------------------------------------------
*/
#define WARPSIZE 32
#define MAXTHREADS_PER_BLOCK 1024
#define CUDA_L1_CACHELINE_SZ 128
#if defined(__CUDACC__)
/* Thread index at CUDA C++ */
#define get_group_id() (blockIdx.x)
#define get_num_groups() (gridDim.x)
#define get_local_id() (threadIdx.x)
#define get_local_size() (blockDim.x)
#define get_global_id() (blockDim.x * blockIdx.x + threadIdx.x)
#define get_global_base() (blockDim.x * blockIdx.x)
#define get_global_size() (blockDim.x * gridDim.x)
/* Dynamic shared memory entrypoint */
extern __shared__ char __pgstrom_dynamic_shared_workmem[] __MAXALIGNED__;
#define SHARED_WORKMEM(OFFSET) \
(__pgstrom_dynamic_shared_workmem + (OFFSET))
/* Reference to the special registers */
INLINE_FUNCTION(uint32_t) LaneId(void)
{
uint32_t rv;
asm volatile("mov.u32 %0, %laneid;" : "=r"(rv) );
return rv;
}
INLINE_FUNCTION(uint32_t) DynamicShmemSize(void)
{
uint32_t rv;
asm volatile("mov.u32 %0, %dynamic_smem_size;" : "=r"(rv) );
return rv;
}
INLINE_FUNCTION(uint32_t) TotalShmemSize(void)
{
uint32_t rv;
asm volatile("mov.u32 %0, %total_smem_size;" : "=r"(rv) );
return rv;
}
#endif /* __CUDACC__ */
/*
* TypeOpCode / FuncOpCode
*/
#define TYPE_OPCODE(NAME,a,b) TypeOpCode__##NAME,
typedef enum {
TypeOpCode__Invalid = 0,
#include "xpu_opcodes.h"
TypeOpCode__composite,
TypeOpCode__array,
TypeOpCode__internal,
TypeOpCode__BuiltInMax,
} TypeOpCode;
#define FUNC_OPCODE(a,b,c,NAME,d,e) FuncOpCode__##NAME,
#define DEVONLY_FUNC_OPCODE(a,NAME,b,c,d) FuncOpCode__##NAME,
#define WITH_DEVICE_ONLY_FUNCTIONS 1
typedef enum {
FuncOpCode__Invalid = 0,
FuncOpCode__ConstExpr,
FuncOpCode__ParamExpr,
FuncOpCode__VarExpr,
FuncOpCode__BoolExpr_And,
FuncOpCode__BoolExpr_Or,
FuncOpCode__BoolExpr_Not,
FuncOpCode__NullTestExpr_IsNull,
FuncOpCode__NullTestExpr_IsNotNull,
FuncOpCode__BoolTestExpr_IsTrue,
FuncOpCode__BoolTestExpr_IsNotTrue,
FuncOpCode__BoolTestExpr_IsFalse,
FuncOpCode__BoolTestExpr_IsNotFalse,
FuncOpCode__BoolTestExpr_IsUnknown,
FuncOpCode__BoolTestExpr_IsNotUnknown,
FuncOpCode__DistinctFrom,
FuncOpCode__CoalesceExpr,
FuncOpCode__LeastExpr,
FuncOpCode__GreatestExpr,
FuncOpCode__CaseWhenExpr,
FuncOpCode__ScalarArrayOpAny,
FuncOpCode__ScalarArrayOpAll,
#include "xpu_opcodes.h"
FuncOpCode__LoadVars = 9999,
FuncOpCode__MoveVars,
FuncOpCode__JoinQuals,
FuncOpCode__HashValue,
FuncOpCode__GiSTEval,
FuncOpCode__SaveExpr,
FuncOpCode__AggFuncs,
FuncOpCode__Projection,
FuncOpCode__Packed, /* place-holder for the stacked expressions */
FuncOpCode__BuiltInMax,
} FuncOpCode;
/*
* Error status
*/
#define ERRCODE_STROM_SUCCESS 0
#define ERRCODE_CPU_FALLBACK 1
#define ERRCODE_WRONG_XPU_CODE 3
#define ERRCODE_VARLENA_UNSUPPORTED 4
#define ERRCODE_RECURSION_TOO_DEEP 5
#define ERRCODE_BUFFER_NO_SPACE 6
#define ERRCODE_GPUDIRECT_READFILE_ASYNC 7
#define ERRCODE_DEVICE_INTERNAL 99
#define ERRCODE_DEVICE_FATAL 999
#define KERN_ERRORBUF_FILENAME_LEN 32
#define KERN_ERRORBUF_FUNCNAME_LEN 64
#define KERN_ERRORBUF_MESSAGE_LEN 200
typedef struct {
uint32_t errcode; /* one of the ERRCODE_* */
int32_t lineno;
char filename[KERN_ERRORBUF_FILENAME_LEN+1];
char funcname[KERN_ERRORBUF_FUNCNAME_LEN+1];
char message[KERN_ERRORBUF_MESSAGE_LEN+1];
} kern_errorbuf;
#if 1
/*
* kern_variable
*/
#define KVAR_CLASS__NULL (-1)
#define KVAR_CLASS__INLINE (-2)
#define KVAR_CLASS__VARLENA (-3)
#define KVAR_CLASS__XPU_DATUM (-4)
typedef union
{
int8_t i8;
uint8_t u8;
int16_t i16;
uint16_t u16;
int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
float2_t fp16;
float4_t fp32;
float8_t fp64;
void *ptr;
} kern_variable;
#endif
/*
* kern_context - a set of run-time information
*/
typedef struct
{
uint32_t errcode;
const char *error_filename;
uint32_t error_lineno;
const char *error_funcname;
const char *error_message;
struct kern_session_info *session;
/* the kernel variables slot */
struct xpu_datum_t **kvars_slot;
const struct kern_varslot_desc *kvars_desc;
uint32_t kvars_nslots; /* length of kvars_values / desc */
uint32_t kvars_nrooms; /* length of kvars_desc (incl. subfields) */
uint32_t kvecs_bufsz; /* kvecs-buffer size per depth */
uint32_t kvecs_ndims; /* number of kvecs-buffer per warp */
char *kvecs_curr_buffer; /* current kvecs-buffer */
uint32_t kvecs_curr_id; /* current kvecs-id */
/*
* GPU shared memory buffer for GPU-PreAgg boosting.
*/
uint32_t groupby_prepfn_bufsz;
uint32_t groupby_prepfn_nbufs;
char *groupby_prepfn_buffer;
/*
* mode control flags
*
* kmode_compare_nulls - if true, equal/not-equal operators compare NULLs.
*/
bool kmode_compare_nulls;
/* variable length buffer */
char *vlpos;
char *vlend;
char vlbuf[1];
} kern_context;
#define INIT_KERNEL_CONTEXT(KCXT,SESSION) \
do { \
const kern_varslot_desc *__vs_desc; \
uint32_t __bufsz = Max(512, (SESSION)->kcxt_extra_bufsz); \
uint32_t __len = offsetof(kern_context, vlbuf) + __bufsz; \
\
KCXT = (kern_context *)alloca(__len); \
memset(KCXT, 0, __len); \
KCXT->session = (SESSION); \
KCXT->kvars_nrooms = (SESSION)->kcxt_kvars_nrooms; \
KCXT->kvars_nslots = (SESSION)->kcxt_kvars_nslots; \
KCXT->kvecs_bufsz = (SESSION)->kcxt_kvecs_bufsz; \
KCXT->kvecs_ndims = (SESSION)->kcxt_kvecs_ndims; \
KCXT->kvecs_curr_buffer = NULL; \
KCXT->kvecs_curr_id = 0; \
KCXT->kvars_slot = (struct xpu_datum_t **) \
alloca(sizeof(struct xpu_datum_t *) * KCXT->kvars_nslots); \
__vs_desc = SESSION_KVARS_SLOT_DESC(SESSION); \
for (int __i=0; __i < KCXT->kvars_nslots; __i++) \
{ \
const xpu_datum_operators *vs_ops = __vs_desc[__i].vs_ops; \
/* alloca() guarantees 16bytes-aligned */ \
assert(vs_ops->xpu_type_alignof <= 16); \
KCXT->kvars_slot[__i] = (struct xpu_datum_t *) \
alloca(vs_ops->xpu_type_sizeof); \
} \
KCXT->kvars_desc = __vs_desc; \
KCXT->vlpos = KCXT->vlbuf; \
KCXT->vlend = KCXT->vlbuf + __bufsz; \
} while(0)
INLINE_FUNCTION(const char *)
__basename(const char *filename)
{
const char *pos;
for (pos = filename; *pos != '\0'; pos++)
{
if (pos[0] == '/' && pos[1] != '\0')
filename = pos + 1;
}
return filename;
}
INLINE_FUNCTION(void)
__STROM_EREPORT(kern_context *kcxt,
uint32_t errcode,
const char *filename,
int lineno,
const char *funcname,
const char *message)
{
if ((kcxt->errcode == ERRCODE_STROM_SUCCESS && errcode != ERRCODE_STROM_SUCCESS) ||
(kcxt->errcode == ERRCODE_CPU_FALLBACK && (errcode != ERRCODE_STROM_SUCCESS &&
errcode != ERRCODE_STROM_SUCCESS)))
{
kcxt->errcode = errcode;
kcxt->error_filename = __basename(filename);
kcxt->error_lineno = lineno;
kcxt->error_funcname = funcname;
kcxt->error_message = message;
}
}
#define STROM_ELOG(kcxt, message) \
__STROM_EREPORT((kcxt),ERRCODE_DEVICE_INTERNAL, \
__FILE__,__LINE__,__FUNCTION__,(message))
#define STROM_EREPORT(kcxt, errcode, message) \
__STROM_EREPORT((kcxt),(errcode), \
__FILE__,__LINE__,__FUNCTION__,(message))
#define STROM_CPU_FALLBACK(kcxt, message) \
__STROM_EREPORT((kcxt),ERRCODE_CPU_FALLBACK, \
__FILE__,__LINE__,__FUNCTION__,(message))
INLINE_FUNCTION(void *)
kcxt_alloc(kern_context *kcxt, size_t len)
{
char *pos = (char *)MAXALIGN(kcxt->vlpos);
if (pos >= kcxt->vlbuf && pos + len <= kcxt->vlend)
{
kcxt->vlpos = pos + len;
return pos;
}
STROM_ELOG(kcxt, "out of kcxt memory");
return NULL;
}
INLINE_FUNCTION(void)
kcxt_reset(kern_context *kcxt)
{
kcxt->vlpos = kcxt->vlbuf;
}
INLINE_FUNCTION(void)
__strncpy(char *d, const char *s, uint32_t n)
{
uint32_t i, m = n-1;
for (i=0; i < m && s[i] != '\0'; i++)
d[i] = s[i];
while (i < n)
d[i++] = '\0';
}
/* ----------------------------------------------------------------
*
* Definitions related to the kernel data store
*
* ----------------------------------------------------------------
*/
#include "arrow_defs.h"
#define TYPE_KIND__NULL 'n' /* unreferenced column */
#define TYPE_KIND__BASE 'b'
#define TYPE_KIND__ARRAY 'a'
#define TYPE_KIND__COMPOSITE 'c'
#define TYPE_KIND__DOMAIN 'd'
#define TYPE_KIND__ENUM 'e'
#define TYPE_KIND__PSEUDO 'p'
#define TYPE_KIND__RANGE 'r'
struct kern_colmeta {
/* true, if column is held by value. Elsewhere, a reference */
bool attbyval;
/* alignment; 1,2,4 or 8, not characters in pg_attribute */
int8_t attalign;
/* length of attribute */
int16_t attlen;
/* attribute number */
int16_t attnum;
/* offset of attribute location, if deterministic */
int16_t attcacheoff;
/* oid of the SQL data type */
Oid atttypid;
/* typmod of the SQL data type */
int32_t atttypmod;
/* one of TYPE_KIND__* */
int8_t atttypkind;
/* copy of kds->format */
char kds_format;
/* copy of sizeof(xpu_xxxx_t) if any */
int16_t dtype_sizeof;
/*
* offset from kds for the reverse reference.
* kds = (kern_data_store *)((char *)cmeta - cmeta->kds_offset)
*/
uint32_t kds_offset;
/*
* (for array and composite types)
* Some of types contain sub-fields like array or composite type.
* We carry type definition information (kern_colmeta) using the
* kds->colmeta[] array next to the top-level fields.
* An array type has relevant element type. So, its @num_subattrs
* is always 1, and kds->colmeta[@idx_subattrs] informs properties
* of the element type.
* A composite type has several fields.
* kds->colmeta[@idx_subattrs ... @idx_subattrs + @num_subattrs -1]
* carries its sub-fields properties.
*/
uint16_t idx_subattrs;
uint16_t num_subattrs;
/* column name */
char attname[NAMEDATALEN];
/*
* (only arrow/column format)
* @attoptions keeps extra information of Apache Arrow type. Unlike
* PostgreSQL types, it can have variation of data accuracy in time
* related data types, or precision in decimal data type.
*/
ArrowTypeOptions attopts;
uint32_t nullmap_offset;
uint32_t nullmap_length;
uint32_t values_offset;
uint32_t values_length;
uint32_t extra_offset;
uint32_t extra_length;
};
typedef struct kern_colmeta kern_colmeta;
#define KDS_FORMAT_ROW 'r' /* normal heap-tuples */
#define KDS_FORMAT_HASH 'h' /* inner hash table for HashJoin */
#define KDS_FORMAT_BLOCK 'b' /* raw blocks for direct loading */
#define KDS_FORMAT_COLUMN 'c' /* columnar based storage format */
#define KDS_FORMAT_ARROW 'a' /* apache arrow format */
struct kern_data_store {
uint64_t length; /* length of this data-store */
/*
* NOTE: {nitems + usage} must be aligned to 64bit because these pair of
* values can be updated atomically using cmpxchg.
*/
uint32_t nitems; /* number of rows in this store */
uint32_t usage; /* usage of this data-store (PACKED) */
uint32_t ncols; /* number of columns in this store */
char format; /* one of KDS_FORMAT_* above */
bool has_varlena; /* true, if any varlena attribute */
bool tdhasoid; /* copy of TupleDesc.tdhasoid */
Oid tdtypeid; /* copy of TupleDesc.tdtypeid */
int32_t tdtypmod; /* copy of TupleDesc.tdtypmod */
Oid table_oid; /* OID of the table (only if GpuScan) */
/* only KDS_FORMAT_HASH */
uint32_t hash_nslots; /* width of the hash-slot */
/* only KDS_FORMAT_BLOCK */
uint32_t block_offset; /* offset of blocks array */
uint32_t block_nloaded; /* number of blocks already loaded by CPU */
/* only KDS_FORMAT_COLUMN */
uint32_t column_nrooms; /* = max_num_rows parameter */
/* column definition */
uint32_t nr_colmeta; /* number of colmeta[] array elements;
* maybe, >= ncols, if any composite types */
kern_colmeta colmeta[1]; /* metadata of columns */
};
typedef struct kern_data_store kern_data_store;
/*
* Layout of KDS_FORMAT_ROW / KDS_FORMAT_HASH
*
* +---------------------+
* | kern_data_store |
* | : |
* | +-------------------+
* | | kern_colmeta |
* | | colmeta[...] |
* +-+-------------------+ <-- KDS_BODY_ADDR(kds)
* | ^ |
* | | Hash slots if any | (*) KDS_FORMAT_ROW always has 'hash_nslots' == 0,
* | | (uint32 * nslots) | thus, this field is only for KDS_FORMAT_HASH
* | v |
* +---------------------+
* | ^ |
* | | Row index o--------+ ((char *)kds + kds->length -
* | | (uint32 * nitems) | | __kds_unpack(row_index[i]))
* | v | |
* +---------------------+ |
* | : | |
* +---------------------+ --- |
* | ^ | ^ |
* | | Buffer for | | |
* | | kern_tupitem, <--------+
* | | or kern_hashitem | | packed 'usage'
* | v | v
* +---------------------+----
*
* Layout of KDS_FORMAT_BLOCK
*
* +-----------------------+
* | kern_data_store |
* | : |
* | +---------------------+
* | | kern_colmeta |
* | | colmeta[...] |
* +-+---------------------+ <-- KDS_BODY_ADDR(kds)
* | | ^
* | Array of BlockNumber | | (BlockNumber * nitems)
* | | v
* +-----------------------+ --- <--- (char *)kds + kds->block_offset
* | | ^
* | Raw blocks loaded by | | (BLCKSZ * block_nloaded)
* | the host module. | |
* | | v
* +-------------+---------+ -----
* | Raw blocks | ^
* | loaded aby | | (BLCKSZ * (nitems - block_nloaded)
* | the device | |
* | module | | (*) available only device side
* | : | v
* +-------------+ -----
*
* Layout of KDS_FORMAT_ARROW
*
* +-----------------------+
* | kern_data_store |
* | : |
* | +---------------------+
* | | kern_colmeta |
* | | colmeta[...] |
* +-+---------------------+ <-- KDS_BODY_ADDR(kds)
* | | ^
* | iovec of chunks to be | | offsetof(strom_io_vector, ioc[nr_chunks])
* | loaded | |
* | | v
* +-----------------------+ ---
*/
/*
* kern_data_extra - extra buffer of KDS_FORMAT_COLUMN
*/
struct kern_data_extra
{
uint64_t length;
uint64_t usage;
uint64_t deadspace;
char data[1];
};
typedef struct kern_data_extra kern_data_extra;
/*
* MEMO: Support of 32GB KDS - KDS with row-, hash- and column-format
* internally uses 32bit offset value from the head or base address.
* We have assumption here - any objects pointed by the offset value
* is always aligned to MAXIMUM_ALIGNOF boundary (64bit).
* It means we can use 32bit offset to represent up to 32GB range (35bit).
*/
#define __KDS_LENGTH_LIMIT (1UL<<35)
INLINE_FUNCTION(uint32_t)
__kds_packed(size_t offset)
{
assert((offset & ~(0xffffffffUL << MAXIMUM_ALIGNOF_SHIFT)) == 0);
return (uint32_t)(offset >> MAXIMUM_ALIGNOF_SHIFT);
}
INLINE_FUNCTION(size_t)
__kds_unpack(uint32_t offset)
{
return (size_t)offset << MAXIMUM_ALIGNOF_SHIFT;
}
/* ----------------------------------------------------------------
*
* Definitions of HeapTuple/IndexTuple and related
*
* ----------------------------------------------------------------
*/
#ifdef POSTGRES_H
#include "access/htup_details.h"
#include "access/itup.h"
#include "access/sysattr.h"
#else
/*
* Attribute numbers for the system-defined attributes
*/
#define SelfItemPointerAttributeNumber (-1)
#define MinTransactionIdAttributeNumber (-2)
#define MinCommandIdAttributeNumber (-3)
#define MaxTransactionIdAttributeNumber (-4)
#define MaxCommandIdAttributeNumber (-5)
#define TableOidAttributeNumber (-6)
#define FirstLowInvalidHeapAttributeNumber (-7)
/*
* ItemPointer:
*/
typedef struct
{
struct {
uint16_t bi_hi;
uint16_t bi_lo;
} ip_blkid;
uint16_t ip_posid;
} ItemPointerData;
INLINE_FUNCTION(bool)
ItemPointerEquals(const ItemPointerData *ip1, const ItemPointerData *ip2)
{
return (ip1->ip_blkid.bi_hi == ip2->ip_blkid.bi_hi &&
ip1->ip_blkid.bi_lo == ip2->ip_blkid.bi_lo &&
ip1->ip_posid == ip2->ip_posid);
}
INLINE_FUNCTION(void)
ItemPointerSetInvalid(ItemPointerData *ip)
{
ip->ip_blkid.bi_hi = 0xffffU;
ip->ip_blkid.bi_lo = 0xffffU;
ip->ip_posid = 0;
}
/*
* HeapTupleHeaderData
*/
typedef struct HeapTupleFields
{
uint32_t t_xmin; /* inserting xact ID */
uint32_t t_xmax; /* deleting or locking xact ID */
union
{
uint32_t t_cid; /* inserting or deleting command ID, or both */
uint32_t t_xvac; /* old-style VACUUM FULL xact ID */
} t_field3;
} HeapTupleFields;
typedef struct DatumTupleFields
{
int32_t datum_len_; /* varlena header (do not touch directly!) */
int32_t datum_typmod; /* -1, or identifier of a record type */
Oid datum_typeid; /* composite type OID, or RECORDOID */
} DatumTupleFields;
typedef struct HeapTupleHeaderData
{
union {
HeapTupleFields t_heap;
DatumTupleFields t_datum;
} t_choice;
ItemPointerData t_ctid; /* current TID of this or newer tuple */
uint16_t t_infomask2; /* number of attributes + various flags */
uint16_t t_infomask; /* various flag bits, see below */
uint8_t t_hoff; /* sizeof header incl. bitmap, padding */
/* ^ - 23 bytes - ^ */
uint8_t t_bits[1]; /* null-bitmap -- VARIABLE LENGTH */
} HeapTupleHeaderData;
/*
* 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 */
#define HEAP_XMIN_COMMITTED 0x0100 /* t_xmin committed */
#define HEAP_XMIN_INVALID 0x0200 /* t_xmin invalid/aborted */
#define HEAP_XMAX_COMMITTED 0x0400 /* t_xmax committed */
#define HEAP_XMAX_INVALID 0x0800 /* t_xmax invalid/aborted */
#define HEAP_XMAX_IS_MULTI 0x1000 /* t_xmax is a MultiXactId */
#define HEAP_UPDATED 0x2000 /* this is UPDATEd version of row */
#define HEAP_MOVED_OFF 0x4000 /* unused in xPU */
#define HEAP_MOVED_IN 0x8000 /* unused in xPU */
/*
* 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 */
/* null-bitmap checker */
#define att_isnull(ATT, BITS) (!((BITS)[(ATT) >> 3] & (1 << ((ATT) & 0x07))))
#define BITMAPLEN(NATTS) (((int)(NATTS) + 7) / 8)
/*
* Index tuple header structure
*
* All index tuples start with IndexTupleData. If the HasNulls bit is set,
* this is followed by an IndexAttributeBitMapData. The index attribute
* values follow, beginning at a MAXALIGN boundary.
*/
typedef struct IndexTupleData
{
ItemPointerData t_tid; /* reference TID to heap tuple */
/* ---------------
* t_info is laid out in the following fashion:
*
* 15th (high) bit: has nulls
* 14th bit: has var-width attributes
* 13th bit: AM-defined meaning
* 12-0 bit: size of tuple
* ---------------
*/
uint16_t t_info;
char data[1]; /* data or IndexAttributeBitMapData */
} IndexTupleData;
#define INDEX_MAX_KEYS 32 /* pg_config_manual.h */
typedef struct IndexAttributeBitMapData
{
uint8_t bits[BITMAPLEN(INDEX_MAX_KEYS)];
} IndexAttributeBitMapData;
#define INDEX_SIZE_MASK 0x1fff
#define INDEX_VAR_MASK 0x4000
#define INDEX_NULL_MASK 0x8000
#define IndexTupleSize(itup) \
((size_t)((itup)->t_info & INDEX_SIZE_MASK))
#define IndexTupleHasNulls(itup) \
((((IndexTupleData *)(itup))->t_info & INDEX_NULL_MASK))
#define IndexTupleHasVarwidths(itup) \
((((IndexTupleData *)(itup))->t_info & INDEX_VAR_MASK))
#endif /* POSTGRES_H */
/* ----------------------------------------------------------------
*
* Definitions of PageHeader/ItemId and related
*
* ----------------------------------------------------------------
*/
#ifdef POSTGRES_H
#include "access/gist.h"
#include "access/transam.h"
#include "storage/bufpage.h"
#include "storage/block.h"
#include "storage/itemid.h"
#include "storage/off.h"
#else
/* definitions in access/transam.h */
typedef uint32_t TransactionId;
#define InvalidTransactionId ((TransactionId) 0)
#define BootstrapTransactionId ((TransactionId) 1)
#define FrozenTransactionId ((TransactionId) 2)
#define FirstNormalTransactionId ((TransactionId) 3)
#define MaxTransactionId ((TransactionId) 0xffffffff)
typedef struct
{
uint64_t value;
} FullTransactionId;
typedef uint32_t CommandId;
#define FirstCommandId ((CommandId) 0)
#define InvalidCommandId (~(CommandId)0)
/* definitions in storage/block.h */
typedef uint32_t BlockNumber;
#define InvalidBlockNumber ((BlockNumber) 0xffffffff)
#define MaxBlockNumber ((BlockNumber) 0xfffffffe)