Compare commits

...

5 Commits

Author SHA1 Message Date
Andrew Kane
191bef7e35 Started ARM support [skip ci] 2024-03-30 09:43:18 -07:00
Andrew Kane
582e6ad821 Keep -march=native for Mac x86-64 [skip ci] 2024-03-28 01:30:56 -07:00
Andrew Kane
d0028ae769 Restored comment [skip ci] 2024-03-27 20:40:39 -07:00
Andrew Kane
81dc35b62b Check x86-64 [skip ci] 2024-03-27 19:16:49 -07:00
Andrew Kane
c82d15acad Test runtime dispatching [skip ci] 2024-03-27 18:30:39 -07:00
2 changed files with 34 additions and 30 deletions

View File

@@ -10,21 +10,15 @@ TESTS = $(wildcard test/sql/*.sql)
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS)) REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS))
REGRESS_OPTS = --inputdir=test --load-extension=$(EXTENSION) REGRESS_OPTS = --inputdir=test --load-extension=$(EXTENSION)
OPTFLAGS = -march=native OPTFLAGS =
# Mac ARM doesn't support -march=native # Since runtime dispatch not supported
ifeq ($(shell uname -s), Darwin) ifeq ($(shell uname -s), Darwin)
ifeq ($(shell uname -p), arm) ifeq ($(shell uname -m), x86_64)
# no difference with -march=armv8.5-a OPTFLAGS = -march=native
OPTFLAGS =
endif endif
endif endif
# PowerPC doesn't support -march=native
ifneq ($(filter ppc64%, $(shell uname -m)), )
OPTFLAGS =
endif
# For auto-vectorization: # For auto-vectorization:
# - GCC (needs -ftree-vectorize OR -O3) - https://gcc.gnu.org/projects/tree-ssa/vectorization.html # - GCC (needs -ftree-vectorize OR -O3) - https://gcc.gnu.org/projects/tree-ssa/vectorization.html
# - Clang (could use pragma instead) - https://llvm.org/docs/Vectorizers.html # - Clang (could use pragma instead) - https://llvm.org/docs/Vectorizers.html

View File

@@ -29,6 +29,15 @@
#define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1) #define STATE_DIMS(x) (ARR_DIMS(x)[0] - 1)
#define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1)) #define CreateStateDatums(dim) palloc(sizeof(Datum) * (dim + 1))
#if defined(__x86_64__) && defined(__gnu_linux__) && defined(__has_attribute) && __has_attribute(target_clones)
#define RUNTIME_DISPATCH __attribute__((target_clones("default", "avx", "fma", "avx512f")))
#elif defined(__aarch64__) && defined(__gnu_linux__) && defined(__has_attribute) && __has_attribute(target_clones)
/* TODO Fix error: target does not support function version dispatcher */
#define RUNTIME_DISPATCH __attribute__((target_clones("default", "arch=armv8.5-a")))
#else
#define RUNTIME_DISPATCH
#endif
PG_MODULE_MAGIC; PG_MODULE_MAGIC;
/* /*
@@ -532,6 +541,23 @@ vector_to_float4(PG_FUNCTION_ARGS)
PG_RETURN_POINTER(result); PG_RETURN_POINTER(result);
} }
RUNTIME_DISPATCH
static float
l2_squared_distance_impl(int16 dim, float *ax, float *bx)
{
float distance = 0.0;
/* Auto-vectorized */
for (int16 i = 0; i < dim; i++)
{
float diff = ax[i] - bx[i];
distance += diff * diff;
}
return distance;
}
/* /*
* Get the L2 distance between vectors * Get the L2 distance between vectors
*/ */
@@ -541,19 +567,11 @@ l2_distance(PG_FUNCTION_ARGS)
{ {
Vector *a = PG_GETARG_VECTOR_P(0); Vector *a = PG_GETARG_VECTOR_P(0);
Vector *b = PG_GETARG_VECTOR_P(1); Vector *b = PG_GETARG_VECTOR_P(1);
float *ax = a->x; float distance;
float *bx = b->x;
float distance = 0.0;
float diff;
CheckDims(a, b); CheckDims(a, b);
/* Auto-vectorized */ distance = l2_squared_distance_impl(a->dim, a->x, b->x);
for (int i = 0; i < a->dim; i++)
{
diff = ax[i] - bx[i];
distance += diff * diff;
}
PG_RETURN_FLOAT8(sqrt((double) distance)); PG_RETURN_FLOAT8(sqrt((double) distance));
} }
@@ -568,19 +586,11 @@ vector_l2_squared_distance(PG_FUNCTION_ARGS)
{ {
Vector *a = PG_GETARG_VECTOR_P(0); Vector *a = PG_GETARG_VECTOR_P(0);
Vector *b = PG_GETARG_VECTOR_P(1); Vector *b = PG_GETARG_VECTOR_P(1);
float *ax = a->x; float distance;
float *bx = b->x;
float distance = 0.0;
float diff;
CheckDims(a, b); CheckDims(a, b);
/* Auto-vectorized */ distance = l2_squared_distance_impl(a->dim, a->x, b->x);
for (int i = 0; i < a->dim; i++)
{
diff = ax[i] - bx[i];
distance += diff * diff;
}
PG_RETURN_FLOAT8((double) distance); PG_RETURN_FLOAT8((double) distance);
} }