Example #1
0
void
filesrv::gotdds (bool ok)
{
  if (!ok) {
    (*cb) (false);
    return;
  }

  u_int8_t key[sha1::hashsize];
  privkey->get_privkey_hash (key, hostid);
  fhkey.setkey (key, sizeof (key));
  bzero (key, sizeof (key));

  for (filesys *fsp = fstab.base (); fsp < fstab.lim (); fsp++) {
    if (fsp == fsp->parent)
      /* Make sure readdir returns unique fileid for /.., since an
       * accessible copy of /.. may reside somewhere else in the file
       * system and otherwise break pwd. */
      fsp->inotab->insert (fsp->fileid_root_dd, 1);
    else {
      fsp->parent->inotab->insert (fsp->fileid_mntpt,
				   fsidino2ino (fsp->fsid, fsp->fileid_root));
      fsp->inotab->insert (fsp->fileid_root_dd,
			   fsidino2ino (fsp->parent->fsid,
					fsp->fileid_mntpt_dd));
    }
  }

#if 0
  for (filesys *fsp = fstab.base (); fsp < fstab.lim (); fsp++) {
    warnx << fsp->path_mntpt << ":\n";
    warnx ("fsid 0x%qx, / 0x%qx, /.. 0x%qx, mp 0x%qx, mp/.. 0x%qx\n",
	   fsp->fsid, fsp->fileid_root, fsp->fileid_root_dd,
	   fsp->fileid_mntpt, fsp->fileid_mntpt_dd);
    fsp->inotab->traverse (wrap (dumpsubst));
  }
#endif

  fh3trans fht (fh3trans::ENCODE, fhkey, 0);

  fsinfo.set_prog (ex_NFS_PROGRAM);
  fsinfo.nfs->set_vers (ex_NFS_V3);
  fsinfo.nfs->v3->root = fstab[0].fh_root;
  if (!rpc_traverse (fht, fsinfo.nfs->v3->root))
    fatal ("filesrv::finish: nfs3_transres encode failed (err %d)\n", fht.err);

  fsinfo.nfs->v3->subfs.setsize (fstab.size () - 1);
  for (size_t i = 1; i < fstab.size (); i++) {
    fsinfo.nfs->v3->subfs[i-1].path = fstab[i].path_mntpt;
    fsinfo.nfs->v3->subfs[i-1].fh = fstab[i].fh_root;
    fht.srvno = i;
    if (!rpc_traverse (fht, fsinfo.nfs->v3->subfs[i-1].fh))
      fatal ("filesrv::finish: nfs3_transres encode failed (err %d)\n",
	     fht.err);
  }

  cb_t c = cb;
  cb = NULL;
  (*c) (true);
}
Example #2
0
bool
filesrv::fixres (svccb *sbp, void *res, reqstate *rqsp)
{
  filesys *fsp = &fstab[rqsp->fsno];

  if (sbp->proc () == NFSPROC3_READDIR)
    fixrdres (res, fsp, rqsp->rootfh);
  else if (sbp->proc () == NFSPROC3_READDIRPLUS)
    fixrdplusres (res, fsp, rqsp->rootfh);

  bool subst = false;
  fh3trans fht (fh3trans::ENCODE, fhkey, rqsp->fsno,
		wrap (this, &filesrv::fhsubst, &subst, fsp));

  fht.fattr_hook = wrap (xor_ino, this);
  if (!nfs3exp_transres (fht, res, sbp->proc ())) {
    warn ("nfs3reply: nfs3_transres encode failed (err %d)\n", fht.err);
    nfs3exp_err (sbp, nfsstat3 (fht.err));
    return false;
  }

  switch (sbp->proc ()) {
  case NFSPROC3_LOOKUP:
    {
      ex_lookup3res *dor = static_cast<ex_lookup3res *> (res);
      if (rqsp->rootfh) {
	if (dor->status)
	  dor->resfail->set_present (false);
	else
	  dor->resok->dir_attributes.set_present (false);
      }
      if (subst && !dor->status)
	dor->resok->obj_attributes.set_present (false);
      break;
    }
  case NFSPROC3_ACCESS:
    if (!sbp->getaui () && fsp->options != filesys::ANON_READWRITE) {
      ex_access3res *ar = static_cast<ex_access3res *> (res);
      if ((fsp->options & filesys::ANON_READ) != filesys::ANON_READ) {
	if (ar->status)
	  ar->resfail->set_present (false);
	else {
	  ar->resok->access = 0;
	  ar->resok->obj_attributes.set_present (false);
	}
      }
      else if (!ar->status)
	ar->resok->access &= ACCESS3_READ|ACCESS3_LOOKUP|ACCESS3_EXECUTE;
    }
    break;
  }
  return true;
}
void fftlog_ComputeXiLM(int l, int m, int N, const double k[], const double pk[], double r[], double xi[]) {
  double complex* a = malloc(sizeof(complex double)*N);
  double complex* b = malloc(sizeof(complex double)*N);

    for(int i = 0; i < N; i++)
        a[i] = pow(k[i], m - 0.5) * pk[i];
    fht(N, k, a, r, b, l + 0.5, 0, 1, 1, NULL);
    for(int i = 0; i < N; i++)
        xi[i] = creal(pow(2*M_PI*r[i], -1.5) * b[i]);

    free(a);
    free(b);
}
Example #4
0
void psycho_1_fft (FLOAT * x_real, FLOAT * energy, int N)
{
  FLOAT a, b;
  int i, j;

  fht (x_real);

  energy[0] = x_real[0] * x_real[0];

  for (i = 1, j = N - 1; i < N / 2; i++, j--) {
    a = x_real[i];
    b = x_real[j];
    energy[i] = (a * a + b * b) / 2.0;
  }
  energy[N / 2] = x_real[N / 2] * x_real[N / 2];
}
Example #5
0
/* For variations on psycho model 2:
   N always equals 1024
   BUT in the returned values, no energy/phi is used at or above an index of 513 */
void psycho_2_fft (FLOAT * x_real, FLOAT * energy, FLOAT * phi) 
     /* got rid of size "N" argument as it is always 1024 for layerII */
{
  FLOAT a, b;
  int i, j;
#ifdef NEWATAN
  static int init=0;

  if (!init) {
    atan_table_init();
    init++;
  }
#endif


  fht (x_real);


  energy[0] = x_real[0] * x_real[0];

  for (i = 1, j = 1023; i < 512; i++, j--) {
    a = x_real[i];
    b = x_real[j];
    /* MFC FIXME Mar03 Why is this divided by 2.0?
       if a and b are the real and imaginary components then
       r = sqrt(a^2 + b^2),
       but, back in the psycho2 model, they calculate r=sqrt(energy), 
       which, if you look at the original equation below is different */
    energy[i] = (a * a + b * b) / 2.0;
    if (energy[i] < 0.0005) {
      energy[i] = 0.0005;
      phi[i] = 0;
    } else	
#ifdef NEWATAN
      {		
	phi[i] = atan_table(-a, b) + PI/4;
      }
#else
      {
      phi[i] = atan2(-(double)a, (double)b) + PI/4;
      }
#endif
  }
  energy[512] = x_real[512] * x_real[512];
  phi[512] = atan2 (0.0, (double) x_real[512]);
}
Example #6
0
int main(int argc, char **argv)
{
  int i, q;
  double *x;
  gf2_vector_t *index;
  double *y;
  int *s;
  double avg_error=0;
  int loops, unsat;
  int n_error = 0;

  /* allocate arrays */
  x = (double *)malloc(N*sizeof(double));
  index = (gf2_vector_t *)malloc(N*sizeof(gf2_vector_t));
  y = (double *)malloc(K*sizeof(double));
  s = (int *)malloc(K*sizeof(int));
  if (x == NULL || index == NULL || y == NULL || s == NULL)
  {
    fprintf(stderr, "Could not allocate memory.\n");
    exit(1);
  }

  srandom(SEED);

  printf("Test Sparse Hadamard :\n");

  for (i = 0 ; i < N ; i++)
    index[i] = i;

  for (q = 0 ; q < LOOP ; q++)
  {
    /* random permutation */
    for (i = 0 ; i < N-1 ; i++)
    {
      int p = random() % (N-i) + i;
      double t = index[p];
      index[p] = index[i];
      index[i] = t;
    }

    /* create sparse vector */
    for (i = 0 ; i < N ; i++)
      x[i] = 0;
    for (i = 0 ; i < K ; i++)
    {
      x[index[i]] = ((random()%MAX)/(float)MAX);
      s[i] = index[i];
      y[i] = x[index[i]];
    }

    /* prepare TD signal */
    fht(x, N);
    for (i = 0 ; i < N ; i++)
      x[i] /= sqrt(N);

    /* sparse transform */
    int ncoeff = sfht_det_opt(x, (size_t)N, y, s, K, K, C, L, &loops, &unsat);
    //int ncoeff = sfht_deterministic(x, (size_t)N, y, s, K, K, C, L, &loops, &unsat);
    
    if (unsat > 0)
      n_error++;

    /* recover FD signal */
    fht(x, N);
    for (i = 0 ; i < N ; i++)
      x[i] /= sqrt(N);

    sparse_quicksort(y, (gf2_vector_t *)s, 0, K-1);

    int u = 0;
    int v = 0;
    double error = 0.0;
    int biterror = 0;
    quicksort((int *)index, 0, K-1);
    while (u < K && v < K)
    {
      while (u < K && (v >= K || s[u] < index[v]))
      {
        error += fabs(y[u++]);
        biterror++;
      }
      while (v < K && (u >= K || index[v] < s[u]))
      {
        error += fabs(x[index[v++]]);
        biterror++;
      }
      u++;
      v++;
    }
    printf("Error: %.3f BitError: %d (ncoeff=%d, loops=%d, unsat=%d)\n", error, biterror, ncoeff, loops, unsat);
    avg_error += error;
      
  }
  printf("Average Error: %.3f\n", avg_error/LOOP);
  printf("Error rate: %.3f\n", n_error/(float)LOOP);

#if 0
  if (LOOP == 1)
  {
    for (i = 0 ; i < K ; i++)
    {
      printf("O(%d,%.2f) S(%d,%.2f) ", index[i], x[index[i]], s[i], y[i]);
      if (fabs(x[index[i]] - y[i]) > 1e-5)
        printf(" <---- check this");
      printf("\n");
    }
  }
#endif

  /* free mem */
  free(x);
  free(index);
  free(y);
  free(s);

  return 0;
}
Example #7
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    double *N, *K, *B, *C, *L, *R, *SEED;  /* pointers to input matrices */
    double *Tfht, *Tsfht;      /* pointers to output matrices */
    int i, j, p;
    size_t *k, *b;
    size_t n, c, l;
    size_t k_len, b_len, r_len, c_len;
    size_t loop, warm, body;
    int max_mag;

    /* check arguments */
    if (nlhs != 2 || nrhs != 7)
      mexErrMsgTxt("HadamardBenchmark: syntax: [Tfht Tsfht] = HadamardBenchmark(N, K, B, C, L, R, SEED)");

    /* get input data */
    N = mxGetPr(prhs[0]); /* first input matrix */
    K = mxGetPr(prhs[1]); /* K: the sparsity */
    k_len = mxGetM(prhs[1])*mxGetN(prhs[1]);
    B = mxGetPr(prhs[2]); /* ALPHA: the over-binning factor */
    b_len = mxGetM(prhs[2])*mxGetN(prhs[2]);
    C = mxGetPr(prhs[3]); /* C: the oversampling factor */
    c_len = mxGetM(prhs[3])*mxGetN(prhs[3]);
    L = mxGetPr(prhs[4]); /* L: the number of iterations of the decoder */
    R = mxGetPr(prhs[5]); /* R: array with sim repetition parameters */
    r_len = mxGetM(prhs[5])*mxGetN(prhs[5]);
    SEED = mxGetPr(prhs[6]); /* SEED: the random number generator seed */

    /* run checks on input */
    if (k_len != b_len)
      mexErrMsgTxt("HadamardBenchmark: syntax: K and B array needs to be the same length.");
    if (k_len != c_len && c_len != 1)
      mexErrMsgTxt("HadamardBenchmark: syntax: C must either be a scalar or the same size as K.");
    if (r_len != 4)
      mexErrMsgTxt("HadamardBenchmark: syntax: R needs to be a length-4 array.");

    /* init RNG */
    srandom((unsigned)SEED[0]);

    /* transform parameters */
    n = (size_t)N[0];
    l = (size_t)L[0];
    k = (size_t *)malloc(k_len*sizeof(size_t));
    b = (size_t *)malloc(b_len*sizeof(size_t));
    for (p = 0 ; p < k_len ; p++)
    {
      k[p] = (size_t)K[p];
      b[p] = (size_t)B[p];
    }

    /* simulation parameters */
    loop = (size_t)R[0];
    warm = (size_t)R[1];
    body = (size_t)R[2];
    max_mag = (int)R[3];

    /* check dimension is a power of two */
    if (!is_power_of_2(n))
      mexErrMsgTxt("SparseFHT2: Transform size needs to be a power of two.");

    /* create output matrix Y */
    plhs[0] = mxCreateDoubleMatrix(loop, 1, mxREAL);
    plhs[1] = mxCreateDoubleMatrix(loop, k_len, mxREAL);
    Tfht = mxGetPr(plhs[0]);
    Tsfht = mxGetPr(plhs[1]);

    /* allocate permutation vector */
    double *x_fht = (double *)malloc(n*sizeof(double));
    for (p = 0 ; p < n ; p++)
      x_fht[p] = (random() % max_mag) * (1 - 2*random()%2);
    double *x = (double *)malloc(n*sizeof(double));
    int *I = (int *)malloc(n*sizeof(int));
    for (i = 0 ; i < n ; i++)
      I[i] = i;

    /*******/
    /* FHT */
    /*******/
    for (j = 0 ; j < loop ; j++)  /* loop for repetition */
    {
      /* now is the time */
      struct timeval t1, t2;
      double elapsedTime;
        
      for (p = 0 ; p < warm ; p++)
        fht(x_fht, n);

      gettimeofday(&t1, NULL);  // start of time interval

      for (p = 0 ; p < body ; p++)
        fht(x_fht, n);

      gettimeofday(&t2, NULL);  // end of time interval

      // compute elapsed time and save it
      elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
      elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
      Tfht[j] = elapsedTime/(double)body;
    }


    /********/
    /* sFHT */
    /********/
    for (i = 0 ; i < k_len ; i++) /* loop for sparsity */
    {
      /* create the 'dummy' output arrays */
      double *y = (double *)malloc(k[i]*sizeof(double));
      int *s = (int *)malloc(k[i]*sizeof(double));

      /* pick right value of C */
      if (c_len == 1)
        c = (size_t)C[0];
      else
        c = (size_t)C[i];

      /* loop for repetition */
      for (j = 0 ; j < loop ; j++)
      {
        /* create new signal vector */
        for (p = 0 ; p < k[i] ; p++)
        {
          /* shuffle support */
          int rn = (random() % n-p) + p;
          int t = I[rn];
          I[rn] = I[p];
          I[p] = t;
          x[I[p]] = (random() % max_mag) * (1 - 2*random()%2);
        }

        /* create its time-domain version */
        fht(x, n);

        /* now is the time */
        struct timeval t1, t2;
        double elapsedTime;
        
        for (p = 0 ; p < warm ; p++)
          sfht_det_opt(x, n, y, s, k[i], b[i], c, l, NULL, NULL);

        gettimeofday(&t1, NULL);  // start of time interval

        for (p = 0 ; p < body ; p++)
          sfht_det_opt(x, n, y, s, k[i], b[i], c, l, NULL, NULL);

        gettimeofday(&t2, NULL);  // end of time interval

        // comput elapsed time and save it
        elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
        elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
        Tsfht[i*loop + j] = elapsedTime / (double)body;;
        
        /* clear signal vector */
        for (p = 0 ; p < k[i] ; p++)
          x[I[p]] = 0;
      }

      /* free memory */
      free(y);
      free(s);
    }

    /* free more memory */
    free(x);
    free(x_fht);
    free(I);

}
Example #8
0
bool
filesrv::fixarg (svccb *sbp, reqstate *rqsp)
{
  fh3trans fht (fh3trans::DECODE, fhkey);
  if (!nfs3_transarg (fht, sbp->Xtmpl getarg<void> (), sbp->proc ())) {
    nfs3exp_err (sbp, nfsstat3 (fht.err));
    return false;
  }
  if (fht.srvno >= fstab.size ()) {
    nfs3exp_err (sbp, NFS3ERR_BADHANDLE);
    return false;
  }
  rqsp->fsno = fht.srvno;
  filesys *fsp = &fstab[rqsp->fsno];
  rqsp->rootfh = false;
  rqsp->c = fsp->c;

#if 1
  /* We let anonymous users GETATTR any root file handle, not just the
   * root of all exported files.  This is to help client
   * implementations that want to avoid (st_ino, st_dev) conflicts by
   * creating multiple mount points for each server.  Is this bad? */
  if (!sbp->getaui ()
      && !anon_checkperm (sbp, fsp->options,
			  *sbp->Xtmpl getarg<nfs_fh3> () == fsp->fh_root))
    return false;
#else
  /* The other option is to disallow this.  Then commands like "ls
   * -al" will return a bunch of permission denied errors.  */
  if (!sbp->getaui ()
      && !anon_checkperm (sbp, fsp->options,
			  (fsp == fstab.base ()
			   && (*sbp->Xtmpl getarg<nfs_fh3> ()
			       == fsp->fh_root))))
    return false;
#endif

  switch (sbp->proc ()) {
  case NFSPROC3_LOOKUP:
    {
      diropargs3 *doa = sbp->Xtmpl getarg<diropargs3> ();
      if (doa->name == ".." && doa->dir == fsp->fh_root) {
	if (!getfsno (fsp)) {
	  nfs3exp_err (sbp, NFS3ERR_ACCES);
	  return false;
	}
	doa->dir = fsp->fh_mntpt;
	rqsp->fsno = getfsno (fsp->parent);
	fsp = &fstab[rqsp->fsno];
	rqsp->rootfh = true;
	rqsp->c = fsp->c;
      }
      break;
    }
  case NFSPROC3_READDIR:
  case NFSPROC3_READDIRPLUS:
    {
      nfs_fh3 *rpa = sbp->Xtmpl getarg<nfs_fh3> ();
      if (*rpa == fsp->fh_root)
	rqsp->rootfh = true;
      break;
    }
  }
  return true;
}