mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-11 23:26:55 +08:00
Use float instead of double for k-means to save memory
This commit is contained in:
@@ -11,7 +11,7 @@
|
|||||||
* https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf
|
* https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lowerBound)
|
InitCenters(Relation index, VectorArray samples, VectorArray centers, float *lowerBound)
|
||||||
{
|
{
|
||||||
FmgrInfo *procinfo;
|
FmgrInfo *procinfo;
|
||||||
Oid collation;
|
Oid collation;
|
||||||
@@ -21,7 +21,7 @@ InitCenters(Relation index, VectorArray samples, VectorArray centers, double *lo
|
|||||||
double sum;
|
double sum;
|
||||||
double choice;
|
double choice;
|
||||||
Vector *vec;
|
Vector *vec;
|
||||||
double *weight = palloc(samples->length * sizeof(double));
|
float *weight = palloc(samples->length * sizeof(float));
|
||||||
int numCenters = centers->maxlen;
|
int numCenters = centers->maxlen;
|
||||||
int numSamples = samples->length;
|
int numSamples = samples->length;
|
||||||
|
|
||||||
@@ -177,11 +177,11 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers)
|
|||||||
VectorArray newCenters;
|
VectorArray newCenters;
|
||||||
int *centerCounts;
|
int *centerCounts;
|
||||||
int *closestCenters;
|
int *closestCenters;
|
||||||
double *lowerBound;
|
float *lowerBound;
|
||||||
double *upperBound;
|
float *upperBound;
|
||||||
double *s;
|
float *s;
|
||||||
double *halfcdist;
|
float *halfcdist;
|
||||||
double *newcdist;
|
float *newcdist;
|
||||||
int changes;
|
int changes;
|
||||||
double minDistance;
|
double minDistance;
|
||||||
int closestCenter;
|
int closestCenter;
|
||||||
@@ -197,13 +197,14 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers)
|
|||||||
collation = index->rd_indcollation[0];
|
collation = index->rd_indcollation[0];
|
||||||
|
|
||||||
/* Allocate space */
|
/* Allocate space */
|
||||||
|
/* Use float instead of double to save memory */
|
||||||
centerCounts = palloc(sizeof(int) * numCenters);
|
centerCounts = palloc(sizeof(int) * numCenters);
|
||||||
closestCenters = palloc(sizeof(int) * numSamples);
|
closestCenters = palloc(sizeof(int) * numSamples);
|
||||||
lowerBound = palloc(sizeof(double) * numSamples * numCenters);
|
lowerBound = palloc(sizeof(float) * numSamples * numCenters);
|
||||||
upperBound = palloc(sizeof(double) * numSamples);
|
upperBound = palloc(sizeof(float) * numSamples);
|
||||||
s = palloc(sizeof(double) * numCenters);
|
s = palloc(sizeof(float) * numCenters);
|
||||||
halfcdist = palloc(sizeof(double) * numCenters * numCenters);
|
halfcdist = palloc(sizeof(float) * numCenters * numCenters);
|
||||||
newcdist = palloc(sizeof(double) * numCenters);
|
newcdist = palloc(sizeof(float) * numCenters);
|
||||||
|
|
||||||
newCenters = VectorArrayInit(numCenters, dimensions);
|
newCenters = VectorArrayInit(numCenters, dimensions);
|
||||||
for (j = 0; j < numCenters; j++)
|
for (j = 0; j < numCenters; j++)
|
||||||
|
|||||||
Reference in New Issue
Block a user