## Yael Library: ‘Segmentation Fault’ while running kmeans algorithm

Yael is a library of highly optimized, multi-threaded implementations of some computationally demanding problems like clustering and nearest neighbor search. Several common vector and matrix operations are also provided in the library.

Sometimes, I found that there is a “Segmentation Fault” error while running the kmeans algorithm from Yael. This particularly occurs while using the hierarchical kmeans algorithm. The problem can be solved very simply in two steps:

1) Go to the file kmeans.c in the yael source and find the function called “random_init” which is usually the first function in that file. It should look something like this:

```
static void random_init(long d, int n, int k, const float * v, int * sel,
unsigned int seed) {
int *perm=ivec_new_random_perm_r(n,seed);
ivec_cpy(sel,perm,k);
free(perm);
}
```

2) Replace the whole function with the following code:

```
static void random_init(long d, int n, int k, const float * v, int * sel,
unsigned int seed) {
int *perm;
if(n<k)
perm=ivec_new_random_perm_r(k,seed);
else
perm=ivec_new_random_perm_r(n, seed);
ivec_cpy(sel,perm,k);
free(perm);
}
```

Voila!! Your code should be running seamlessly now.