// organizo en orden creciente de fvector, indexando en ivector
int gsl_fisort(int dimension,float *fvector,int *ivector)
{
  
  int *aux_ivector,i;
  float *aux_fvector;
  size_t *P;

  P= (size_t *) malloc((size_t) dimension*sizeof(size_t));
  aux_fvector=(float *) malloc(dimension*sizeof(float));
  aux_ivector=(int *) malloc(dimension*sizeof(int));
  
  for(i=0; i<dimension; i++)
    {
      aux_ivector[i]=ivector[i];
      aux_fvector[i]=fvector[i];
    }
  
  gsl_sort_float_index(P,fvector,1,dimension);
  
  for(i=0; i<dimension; i++)
    {
      fvector[i]=aux_fvector[P[i]];
      ivector[i]=aux_ivector[P[i]];
    }

  free(P);
  free(aux_fvector);
  free(aux_ivector);

  return 0;

}
Exemplo n.º 2
0
void bruteKDists(matrix x, matrix q, size_t **NNs, real **D, unint k){
  int i, j;

  float **d;
  d = (float**)calloc(q.pr, sizeof(*d));
  size_t **t;
  t = (size_t**)calloc(q.pr, sizeof(*t));
  for( i=0; i<q.pr; i++){
    d[i] = (float*)calloc(x.pr, sizeof(**d));
    t[i] = (size_t*)calloc(x.pr, sizeof(**t));
  }

#pragma omp parallel for private(j)
  for( i=0; i<q.r; i++){
    for( j=0; j<x.r; j++)
      d[i][j] = distVec( q, x, i, j );
    gsl_sort_float_index(t[i], d[i], 1, x.r);
    for ( j=0; j<k; j++){
      NNs[i][j]=t[i][j];
      D[i][j]=d[i][t[i][j]];
    }
  }

  for( i=0; i<q.pr; i++){
    free(t[i]);
    free(d[i]);
  }
  free(t);
  free(d);
}
Exemplo n.º 3
0
/* 
 * Given an input single-precision array, Haar wavelet transform is  
 * applied to convert it to a set of wavelet coefficients. The quantization is
 * done by making [n - ncoefficients] lowest absolute value coefficients 0. 
 * The resulting coefficients and the position of those coefficients are saved
 * within the compressed buffer.
 * [single-precision floating-point version of compress_wavelets_double]
 */
void compress_wavelets_float (const float *original_buffer,
                                uint32_t     num_elements,
                                uint32_t     num_coefficients,
                                uchar         *compressed_buffer,
                                uint32_t     *compressed_buffer_size
                             )
{
    // If the total number of elements is less than the number of coefficients 
    // try to reduce the number of coefficients used. This should happen only
    // for the last linearized window
    if (num_elements == 1) {
        num_coefficients = 1;
    } else if (num_elements <= num_coefficients) {
        num_coefficients = num_elements / 2;
    }

    // Number of bits to use per index element for marking the position of the
    // saved coefficients.
    uint32_t bits_per_index_element = calculate_bits_needed (num_elements - 1);
    uint32_t num_total_elements = 0;

    if ((num_elements & (num_elements - 1)) == 0) {
        num_total_elements = num_elements;
    } else {
        bits_per_index_element ++; 
        num_total_elements = (1 << bits_per_index_element);
    }

    // If the total number of elements is not a multiple of 2^(WAVELET_NLEVEL),
    // then pad values to the array. The padded value is the maximum value in
    // the array.
    uint32_t num_pad_elements = num_total_elements - num_elements;

    float *wavelet_transform_buffer = (float *) malloc (num_total_elements * sizeof (float));
    float *absolute_coefficients    = (float *) malloc (num_total_elements * sizeof (float));

    uint32_t i = 0;
    uchar *compressed_buffer_start = compressed_buffer;

    size_t *index = (size_t *) malloc (num_total_elements * sizeof (size_t));

    assert (wavelet_transform_buffer != 0);
    assert (absolute_coefficients  != 0);
    assert (index != 0);

    memcpy (wavelet_transform_buffer, original_buffer, num_total_elements * sizeof (float));

    // Pad with the maximum value
    for (i = 0; i < num_pad_elements; i ++) {
        wavelet_transform_buffer [i + num_elements] = wavelet_transform_buffer [num_elements - 1];
    }

    // Perform transformation using HAAR wavelets  
    haar_wavelet_transform_forward_float (wavelet_transform_buffer, num_total_elements);

    // Original buffer has been replaced with wavelet coefficients.
    // Get the coefficients whose absolute value is minimal.
    for (i = 0; i < num_total_elements; i++) {
        absolute_coefficients [i] = fabs (wavelet_transform_buffer [i]);
    }
    gsl_sort_float_index (index, absolute_coefficients, 1, num_total_elements);

    SET (uint32_t, compressed_buffer, num_total_elements)
    compressed_buffer += sizeof (uint32_t);

    SET (uint32_t, compressed_buffer, num_coefficients)
    compressed_buffer += sizeof (uint32_t);

    // Save the top n coefficients
    uint32_t top = 0;
    uint32_t *coefficients_position = (uint32_t *) malloc (sizeof (uint32_t) * num_coefficients);
    uint32_t packed_coefficients_position_size = 0;

    for (i = num_total_elements - num_coefficients; i < num_total_elements; i ++) {
        ((float *) compressed_buffer) [top] = wavelet_transform_buffer [index [i]];
        coefficients_position [top] = index [i];
        top ++;
    }

    // Store the locations of top n coefficients in bit-packed form
    compressed_buffer += num_coefficients * sizeof (float);
    write_to_bitstream (num_coefficients, bits_per_index_element, coefficients_position, (uint32_t *) (compressed_buffer + sizeof (uint32_t)), &packed_coefficients_position_size);

    SET (uint32_t, compressed_buffer, packed_coefficients_position_size);

    compressed_buffer += sizeof (uint32_t);
    compressed_buffer += sizeof (uint32_t) * packed_coefficients_position_size;
    *compressed_buffer_size = (compressed_buffer - compressed_buffer_start);

    // Clear buffers
    free (coefficients_position);
    free (absolute_coefficients);
    free (index);
    free (wavelet_transform_buffer);

    return ;
}
Exemplo n.º 4
0
//Builds the RBC for exact (1- or K-) NN search.
//Note: allocates memory for r and ri that must be freed
//externally.  Use freeRBC.
void buildExact(matrix x, matrix *r, rep *ri, unint numReps){
  unint n = x.r;
  unint i, j;
  unint longestLength = 0;

  if( numReps > n ){
    fprintf( stderr, "number of representatives must be less than the DB size\n");
    exit(1);
  }

  initMat(r, numReps, x.c);
  r->mat = (real*)calloc( sizeOfMat(*r), sizeof(*r->mat) );
  
  //pick r random reps
  pickReps(x,r);

  //Compute the rep for each x
  unint *repID = (unint*)calloc(x.pr, sizeof(*repID));
  real *dToReps = (real*)calloc(x.pr, sizeof(*dToReps));
  
  brutePar(*r,x,repID,dToReps);

  //gather the rep info & store it in struct
  for(i=0; i<numReps; i++){
    ri[i].len = 0;
    ri[i].radius = 0;
}    
  
  for(i=0; i<n; i++){
    ri[repID[i]].radius = MAX( dToReps[i], ri[repID[i]].radius );
    ri[repID[i]].len++;
  }
 
  unint **tempI = (unint**)calloc(numReps, sizeof(*tempI));
  real **tempD = (real**)calloc(numReps, sizeof(*tempD));
  for(i=0; i<numReps; i++){
    tempI[i] = (unint*)calloc(ri[i].len, sizeof(**tempI));
    tempD[i] = (real*)calloc(ri[i].len, sizeof(**tempD));
    ri[i].lr = (unint*)calloc(ri[i].len, sizeof(*ri[i].lr));
    ri[i].dists = (real*)calloc(ri[i].len, sizeof(*ri[i].dists));
    longestLength = MAX( longestLength, ri[i].len );
  }
  
  
  unint *tempCount = (unint*)calloc(numReps, sizeof(*tempCount));
  for(i=0; i<n; i++){
    tempI[repID[i]][tempCount[repID[i]]] = i;
    tempD[repID[i]][tempCount[repID[i]]++] = dToReps[i];
  }

  //this stores the owned points in order of distance to the
  //representative.  This ordering is not currently necc. for the search 
  //algorithm, but might be used in the future.
  size_t *p = (size_t*)calloc(longestLength, sizeof(*p));
  for(i=0; i<numReps; i++){
    gsl_sort_float_index( p, tempD[i], 1, ri[i].len );
    for(j=0; j<ri[i].len; j++){
      ri[i].dists[j] = tempD[i][p[j]];
      ri[i].lr[j] = tempI[i][p[j]];
    }
  }
  free(p);
  for(i=0;i<numReps; i++){
    free(tempI[i]);
    free(tempD[i]);
  }
  free(tempI);
  free(tempD);
  free(tempCount);
  free(dToReps);
  free(repID);
}
Exemplo n.º 5
0
// This method is the same as the above bruteList method, but for k-nn search.
// It uses a heap.
void bruteListK(matrix X, matrix Q, rep *ri, intList *toSearch, unint numReps, unint **NNs, float **dToNNs, unint K) {
    float temp;
    unint i, j, k, l;

    unint nt = omp_get_max_threads();
    unint m = Q.r;

    heap **hp;
    hp = (heap**)calloc(nt, sizeof(*hp));
    for(i=0; i<nt; i++) {
        hp[i] = (heap*)calloc(m, sizeof(**hp));
        for(j=0; j<m; j++)
            createHeap(&hp[i][j],K);
    }

    #pragma omp parallel for private(j,k,l,temp)
    for( i=0; i<numReps; i++ ) {
        unint tn = omp_get_thread_num();
        heapEl newEl;

        for( j=0; j< toSearch[i].len/CL; j++) { //toSearch is assumed to be padded
            unint row = j*CL;
            unint qInd[CL];
            for(k=0; k<CL; k++)
                qInd[k] = toSearch[i].x[row+k];
            rep rt = ri[i];
            for(k=0; k<rt.len; k++) {
                for(l=0; l<CL; l++ ) {
                    if(qInd[l]!=DUMMY_IDX) {
                        temp = distVecLB( Q, X, qInd[l], rt.lr[k], hp[tn][qInd[l]].h[0].val );
                        if( temp < hp[tn][qInd[l]].h[0].val ) {
                            newEl.id = rt.lr[k];
                            newEl.val = temp;
                            replaceMax( &hp[tn][qInd[l]], newEl );
                        }
                    }
                }
            }
        }
    }

    // Now merge the NNs found by each thread.  Currently this performs the
    // merge within one thread, but should eventually be done in a proper
    // parallel-reduce fashion.
    unint **tInds;
    float **tVals;
    tInds = (unint**)calloc(nt, sizeof(*tInds));
    tVals = (float**)calloc(nt, sizeof(*tVals));
    for( i=0; i<nt; i++ ) {
        tInds[i] = (unint*)calloc(K, sizeof(**tInds) );
        tVals[i] = (float*)calloc(K, sizeof(**tVals) );
    }

    size_t *indVec = (size_t*)calloc(nt*K, sizeof(*indVec));
    size_t *tempInds = (size_t*)calloc(nt*K, sizeof(*tempInds));
    float *valVec = (float*)calloc(nt*K, sizeof(*valVec));

    for( i=0; i<m; i++) {

        for( j=0; j<nt; j++) {
            heapSort( &hp[j][i], tInds[j], tVals[j] );
            for( k=0; k<K; k++) {
                indVec[j*K + k] = tInds[j][k];
                valVec[j*K + k] = tVals[j][k];
            }
        }

        gsl_sort_float_index(tempInds, valVec, 1, nt*K);
        for( j=0; j<K; j++ ) {
            dToNNs[i][j] = valVec[tempInds[j]];
            NNs[i][j] = indVec[tempInds[j]];
        }
    }

    free(tempInds);
    free(indVec);
    free(valVec);
    for( i=0; i<nt; i++ ) {
        free(tInds[i]);
        free(tVals[i]);
    }
    free(tInds);
    free(tVals);
    for(i=0; i<nt; i++) {
        for(j=0; j<m; j++)
            destroyHeap(&hp[i][j]);
        free(hp[i]);
    }
    free(hp);
}