I'm working on image processing and I try to do median filter where we use sort.

My median.cl file structure is as following :

Code :
#define TYPE int
#define FILTERSIZE 1000
void quickSort(int *array, int length)//non-recursive algorithm
   int  piv, begin[MAX], end[MAX];
    int i=0 ;
    //quick sort
void Sort(TYPE *tab, int tailleTableau){ //bubble sort
    int i, k;
    TYPE temp;
    for(k = 0; k < (tailleTableau/2)+1; k++) {
        for(i = 0; i < tailleTableau-1; i++) {
            if(tab[i] > tab[i+1])
__kernel void median(__global const TYPE *inputBuffer,
                     __global TYPE *outputBuffer,
                     __global const int *filterIndex,
                     const int filterIndexSize,
                     const int InputBufferSize)
    unsigned int x = get_global_id(0);
    TYPE pixel;
    unsigned int i;
    long offsetIndex;
    TYPE neighborhood[FILTERSIZE];
    pixel = 0;
    for(i=0; i< filterIndexSize; i++)
        offsetIndex  = x + filterIndex[i];
        if(offsetIndex >= 0 && offsetIndex < InputBufferSize )  // in image buffer bound
            neighborhood[i] = inputBuffer[offsetIndex];
//    Sort(neighborhood, filterIndexSize);
    quickSort(neighborhood, filterIndexSize);
    pixel = neighborhood[(int)(filterIndexSize/2)];
    outputBuffer[x] = pixel;   

Some little median values works, some others not. I have clEnqueueReadBuffer : CL_INVALID_COMMAND_QUEUE(-36) error.
How can I fixe this issue? Is it an allocation faillure on GPU? Too big allocation on sort function?

Note that sort algorithms are tested and work well. Kernel works well too without call to sort function.