-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix relocate empty clusters #2028
Open
lordoz234
wants to merge
5
commits into
uxlfoundation:main
Choose a base branch
from
lordoz234:dev/vdevlika-fix-kmeans
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,15 +51,16 @@ struct TaskKMeansLloyd | |
{ | ||
DAAL_NEW_DELETE(); | ||
|
||
TaskKMeansLloyd(int _dim, int _clNum, algorithmFPType * _centroids, const size_t max_block_size) | ||
TaskKMeansLloyd(int _dim, int _clNum, int _nSamples, algorithmFPType * _centroids, const size_t max_block_size) | ||
{ | ||
dim = _dim; | ||
clNum = _clNum; | ||
cCenters = _centroids; | ||
dim = _dim; | ||
clNum = _clNum; | ||
nSamples = _nSamples; | ||
cCenters = _centroids; | ||
|
||
/* Allocate memory for all arrays inside TLS */ | ||
tls_task = new daal::static_tls<TlsTask<algorithmFPType, cpu> *>([=]() -> TlsTask<algorithmFPType, cpu> * { | ||
return TlsTask<algorithmFPType, cpu>::create(dim, clNum, max_block_size); | ||
return TlsTask<algorithmFPType, cpu>::create(dim, clNum, nSamples, max_block_size); | ||
}); /* Allocate memory for all arrays inside TLS: end */ | ||
|
||
clSq = service_scalable_calloc<algorithmFPType, cpu>(clNum); | ||
|
@@ -92,9 +93,9 @@ struct TaskKMeansLloyd | |
} | ||
} | ||
|
||
static SharedPtr<TaskKMeansLloyd<algorithmFPType, cpu> > create(int dim, int clNum, algorithmFPType * centroids, const size_t max_block_size) | ||
static SharedPtr<TaskKMeansLloyd<algorithmFPType, cpu> > create(int dim, int clNum, int nSamples, algorithmFPType * centroids, const size_t max_block_size) | ||
{ | ||
SharedPtr<TaskKMeansLloyd<algorithmFPType, cpu> > result(new TaskKMeansLloyd<algorithmFPType, cpu>(dim, clNum, centroids, max_block_size)); | ||
SharedPtr<TaskKMeansLloyd<algorithmFPType, cpu> > result(new TaskKMeansLloyd<algorithmFPType, cpu>(dim, clNum, nSamples, centroids, max_block_size)); | ||
if (result.get() && (!result->tls_task || !result->clSq)) | ||
{ | ||
result.reset(); | ||
|
@@ -115,8 +116,11 @@ struct TaskKMeansLloyd | |
template <typename centroidsFPType> | ||
int kmeansUpdateCluster(int jidx, centroidsFPType * s1); | ||
|
||
template <typename centroidsFPType> | ||
int kmeansUpdatePoints(int jidx); | ||
|
||
template <Method method> | ||
void kmeansComputeCentroids(int * clusterS0, algorithmFPType * clusterS1, double * auxData); | ||
void kmeansComputeCentroids(int * clusterS0, int * clusterS2, algorithmFPType * clusterS1, double * auxData); | ||
|
||
void kmeansInsertCandidate(TlsTask<algorithmFPType, cpu> * tt, algorithmFPType value, size_t index); | ||
|
||
|
@@ -130,6 +134,7 @@ struct TaskKMeansLloyd | |
|
||
int dim; | ||
int clNum; | ||
int nSamples; | ||
|
||
typedef typename Fp2IntSize<algorithmFPType>::IntT algIntType; | ||
}; | ||
|
@@ -162,6 +167,7 @@ Status TaskKMeansLloyd<algorithmFPType, cpu>::addNTToTaskThreadedDense(const Num | |
algorithmFPType * x_clusters = tt->mklBuff; | ||
|
||
int * cS0 = tt->cS0; | ||
int * cS2 = tt->cS2; | ||
algorithmFPType * cS1 = tt->cS1; | ||
|
||
int * assignments = nullptr; | ||
|
@@ -231,7 +237,9 @@ Status TaskKMeansLloyd<algorithmFPType, cpu>::addNTToTaskThreadedDense(const Num | |
} | ||
|
||
kmeansInsertCandidate(tt, minGoalVal, k * blockSizeDefault + i); | ||
|
||
cS0[minIdx]++; | ||
cS2[i] = minIdx; | ||
|
||
goal += minGoalVal; | ||
|
||
|
@@ -281,6 +289,7 @@ Status TaskKMeansLloyd<algorithmFPType, cpu>::addNTToTaskThreadedCSR(const Numer | |
algorithmFPType * x_clusters = tt->mklBuff; | ||
|
||
int * cS0 = tt->cS0; | ||
int * cS2 = tt->cS2; | ||
algorithmFPType * cS1 = tt->cS1; | ||
|
||
int * assignments = nullptr; | ||
|
@@ -332,6 +341,7 @@ Status TaskKMeansLloyd<algorithmFPType, cpu>::addNTToTaskThreadedCSR(const Numer | |
*trg += minGoalVal; | ||
|
||
cS0[minIdx]++; | ||
cS2[i] = minIdx; | ||
|
||
if (ntAssign) | ||
{ | ||
|
@@ -381,16 +391,30 @@ int TaskKMeansLloyd<algorithmFPType, cpu>::kmeansUpdateCluster(int jidx, centroi | |
return s0; | ||
} | ||
|
||
template <typename algorithmFPType, CpuType cpu> | ||
template <typename centroidsFPType> | ||
int TaskKMeansLloyd<algorithmFPType, cpu>::kmeansUpdatePoints(int jidx) | ||
{ | ||
int idx = (int)jidx; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
int ji = 0; | ||
|
||
tls_task->reduce([&](TlsTask<algorithmFPType, cpu> * tt) -> void { ji += tt->cS2[tt->cIndices[idx]]; }); | ||
|
||
return ji; | ||
} | ||
|
||
template <typename algorithmFPType, CpuType cpu> | ||
template <Method method> | ||
void TaskKMeansLloyd<algorithmFPType, cpu>::kmeansComputeCentroids(int * clusterS0, algorithmFPType * clusterS1, double * auxData) | ||
void TaskKMeansLloyd<algorithmFPType, cpu>::kmeansComputeCentroids(int * clusterS0, int * clusterS2, algorithmFPType * clusterS1, double * auxData) | ||
{ | ||
if (method == defaultDense && auxData) | ||
{ | ||
for (size_t i = 0; i < clNum; i++) | ||
{ | ||
service_memset_seq<double, cpu>(auxData, 0.0, dim); | ||
clusterS0[i] = kmeansUpdateCluster<double>(i, auxData); | ||
clusterS2[i] = kmeansUpdatePoints<double>(i); | ||
|
||
PRAGMA_IVDEP | ||
PRAGMA_VECTOR_ALWAYS | ||
|
@@ -406,6 +430,7 @@ void TaskKMeansLloyd<algorithmFPType, cpu>::kmeansComputeCentroids(int * cluster | |
{ | ||
service_memset_seq<algorithmFPType, cpu>(&clusterS1[i * dim], 0.0, dim); | ||
clusterS0[i] = kmeansUpdateCluster<algorithmFPType>(i, &clusterS1[i * dim]); | ||
clusterS2[i] = kmeansUpdatePoints<double>(i); | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{
should be on separate row. Useclang-format -i {files}
to apply code style:conda install -c conda-forge clang-tools
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed