mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-22 03:57:34 +08:00
Compare commits
5 Commits
v0.8.3
...
target-clo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
191bef7e35 | ||
|
|
582e6ad821 | ||
|
|
d0028ae769 | ||
|
|
81dc35b62b | ||
|
|
c82d15acad |
14
Makefile
14
Makefile
@@ -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
|
||||||
|
|||||||
50
src/vector.c
50
src/vector.c
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user