Skip to content

Commit

Permalink
gpusort: add kern_gpusort_finalize_buffer() kernel function
Browse files Browse the repository at this point in the history
  • Loading branch information
kaigai committed Jan 31, 2025
1 parent c6ba8c8 commit f5cf47f
Show file tree
Hide file tree
Showing 8 changed files with 672 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.cuda
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ NVCC_CFLAGS = $(__NVCC_CFLAGS) $(NVCC_FLAGS_CUSTOM) $(NVCC_CFLAGS_CUSTOM)
NVCC_LDFLAGS = $(__NVCC_LDFLAGS) $(NVCC_FLAGS_CUSTOM) $(NVCC_LDFLAGS_CUSTOM)

# PG-Strom GPU Code
__CUDA_CORE_FILES = xpu_common cuda_gpuscan cuda_gpujoin cuda_gpupreagg \
__CUDA_CORE_FILES = xpu_common cuda_gpuscan cuda_gpujoin cuda_gpupreagg cuda_gpusort \
xpu_basetype xpu_numeric xpu_timelib xpu_textlib \
xpu_misclib xpu_jsonlib xpu_postgis
__CUDA_CORE_HEADERS = cuda_common.h xpu_common.h xpu_opcodes.h xpu_basetype.h \
Expand Down
30 changes: 9 additions & 21 deletions src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -4021,6 +4021,7 @@ codegen_build_gpusort_keydesc(codegen_context *context,
kexp->exptype = TypeOpCode__int4;
kexp->opcode = FuncOpCode__SortKeys;
kexp->u.sort.nkeys = nkeys;
buf.len = sz;

i = 0;
forboth (lc1, pp_info->gpusort_keys_expr,
Expand All @@ -4035,8 +4036,7 @@ codegen_build_gpusort_keydesc(codegen_context *context,
keydesc->kind = kind;
keydesc->nulls_first = ((ival & KSORT_KEY_ATTR__NULLS_FIRST) != 0);
keydesc->desc_order = ((ival & KSORT_KEY_ATTR__DESC_ORDER) != 0);
if (kind == KSORT_KEY_KIND__VREF ||
kind == KSORT_KEY_KIND__COUNT)
if (kind == KSORT_KEY_KIND__VREF)
{
ListCell *cell;
bool found = false;
Expand Down Expand Up @@ -4071,6 +4071,7 @@ codegen_build_gpusort_keydesc(codegen_context *context,
if (!IsA(func, FuncExpr) || list_length(func->args) > 1)
elog(ERROR, "Bug? GPU-SortKey is not unexpected expression: %s",
nodeToString(expr));
kexp->u.sort.needs_finalization = true;
if (func->args == 0)
keydesc->src_anum = 0;
else
Expand Down Expand Up @@ -4107,28 +4108,18 @@ codegen_build_gpusort_keydesc(codegen_context *context,
keydesc->buf_offset = 0; /* no finalization */
keydesc->key_type_code = TypeOpCode__float8;
break;
/* finalization to int64 */
case KSORT_KEY_KIND__PAVG_INT64:
keydesc->buf_offset = usage;
usage += sizeof(bool) + sizeof(int64_t);
keydesc->key_type_code = TypeOpCode__int8;
case KSORT_KEY_KIND__PSUM_NUMERIC:
keydesc->buf_offset = 0; /* no finalization */
keydesc->key_type_code = TypeOpCode__numeric;
break;
/* finalization to fp64 */
case KSORT_KEY_KIND__PAVG_INT64:
case KSORT_KEY_KIND__PAVG_FP64:
case KSORT_KEY_KIND__PAVG_NUMERIC:
keydesc->buf_offset = usage;
usage += (sizeof(bool) + sizeof(float8));
keydesc->key_type_code = TypeOpCode__float8;
break;
/* finalization to numeric */
case KSORT_KEY_KIND__PSUM_INT128:
case KSORT_KEY_KIND__PAVG_INT128:
case KSORT_KEY_KIND__PSUM_NUMERIC:
case KSORT_KEY_KIND__PAVG_NUMERIC:
keydesc->buf_offset = usage;
usage += (sizeof(bool) + sizeof(uint8_t) +
sizeof(uint16_t) + sizeof(int128_t));
keydesc->key_type_code = TypeOpCode__numeric;
break;
/* finalization to fp64 */
case KSORT_KEY_KIND__PVARIANCE_SAMP:
case KSORT_KEY_KIND__PVARIANCE_POP:
Expand Down Expand Up @@ -4628,15 +4619,12 @@ __xpucode_sortkeys_cstring(StringInfo buf,
{
static const char *label[] = {
"vref", /* KSORT_KEY_KIND__VREF */
"count", /* KSORT_KEY_KIND__COUNT */
"min/max[int64]", /* KSORT_KEY_KIND__PMINMAX_INT64 */
"min/max[fp64]", /* KSORT_KEY_KIND__PMINMAX_FP64 */
"sum[int64]", /* KSORT_KEY_KIND__PSUM_INT64 */
"sum[int128]", /* KSORT_KEY_KIND__PSUM_INT128 */
"sum[fp64]", /* KSORT_KEY_KIND__PSUM_FP64 */
"sum[numeric]", /* KSORT_KEY_KIND__PSUM_NUMERIC */
"avg[int64]", /* KSORT_KEY_KIND__PAVG_INT64 */
"avg[int128]", /* KSORT_KEY_KIND__PAVG_INT128 */
"avg[fp64]", /* KSORT_KEY_KIND__PAVG_FP64 */
"avg[numeric]", /* KSORT_KEY_KIND__PAVG_NUMERIC */
"var[samp]", /* KSORT_KEY_KIND__PVARIANCE_SAMP */
Expand All @@ -4656,7 +4644,7 @@ __xpucode_sortkeys_cstring(StringInfo buf,
NULL,
};

appendStringInfo(buf, "{AggFuncs");
appendStringInfo(buf, "{SortKeys");
for (int i=0; i < kexp->u.sort.nkeys; i++)
{
const kern_sortkey_desc *desc = &kexp->u.sort.desc[i];
Expand Down
Loading

0 comments on commit f5cf47f

Please sign in to comment.