Beispiel #1
0
CTEST2(io, binary_io)
{
  for(idx_t i=0; i < data->ntensors; ++i) {
    sptensor_t * const gold = data->tensors[i];

    /* write to binary */
    tt_write_binary(gold, TMP_FILE);

    /* now read it back */
    sptensor_t * tt_bin = tt_read(TMP_FILE);

    /* now check for correctness */
    ASSERT_EQUAL(gold->nnz, tt_bin->nnz);
    ASSERT_EQUAL(gold->nmodes, tt_bin->nmodes);
    for(idx_t m=0; m < tt_bin->nmodes; ++m) {
      idx_t const * const gold_ind = gold->ind[m];
      idx_t const * const test_ind = tt_bin->ind[m];

      for(idx_t n=0; n < tt_bin->nnz; ++n) {
        ASSERT_EQUAL(gold_ind[n], test_ind[n]);
      }
    }

    /* values better be exact! */
    val_t const * const gold_vals = gold->vals;
    val_t const * const test_vals = tt_bin->vals;
    for(idx_t n=0; n < tt_bin->nnz; ++n) {
      ASSERT_DBL_NEAR_TOL(gold_vals[n], test_vals[n], 0.);
    }
  }

  /* delete temporary file */
  remove(TMP_FILE);
}
Beispiel #2
0
CTEST2(io, zero_index)
{
  sptensor_t * zero_tt = tt_read(DATASET(small4.tns));
  sptensor_t * one_tt  = tt_read(DATASET(small4_zeroidx.tns));

  ASSERT_EQUAL(one_tt->nnz, zero_tt->nnz);
  ASSERT_EQUAL(one_tt->nmodes, zero_tt->nmodes);

  for(idx_t m=0; m < one_tt->nmodes; ++m) {
    ASSERT_EQUAL(one_tt->dims[m], zero_tt->dims[m]);

    for(idx_t n=0; n < one_tt->nnz; ++n) {
      ASSERT_EQUAL(one_tt->ind[m][n], zero_tt->ind[m][n]);
    }
  }

  tt_free(zero_tt);
  tt_free(one_tt);
}
Beispiel #3
0
/*
* read com bytes
* read len: nbytes
* timeout : ms
*/
int tt_read_nbys(int fd, void *buf, int nbytes, unsigned int timeout)
{
    int nleft;
    int nread;

    nleft = nbytes;
    while(nleft > 0) {
        if((nread = tt_read(fd,buf,nleft,timeout)) < 0) {
			if(errno == EINTR || errno == ETIME)
				continue;
            if(nleft == nbytes)
                return -1; /* error, return -1 */
            else
                break; /* error, return amount read so far */
        } else if(nread == 0) {
            printf("Read EOF!\n");
            break;
        }
        //qDebug("tt read ret=%d,%s\n",nread,buf);
        nleft -= nread;
        buf += nread;
    }
    return (nbytes - nleft); /* return bytes had read , >= 0 */
}
Beispiel #4
0
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
// // char *fname
// return: d, r, n, core
// if d<1, then smth is wrong
{
  tthead head;
  int d, *r, *n, mem, coresize, j;
  char dtype;
  double complex *zcore;
  double *dcore;
  float complex *ccore;
  float *score;
  double *num1, *num2;
  char *filename, *buf;

  if (nrhs<1) {
    mexPrintf("Please specify filename.\n");
    return;
  }

  if (sizeof(int)!=4) {
    mexPrintf("sizeof(int) is not 4. The header will be inconsistent. Try to play with compiler options.\n");
    return;
  }

// get filename
  coresize = mxGetM(prhs[0]);
  if (coresize==1) coresize = mxGetN(prhs[0]);
  filename = (char *)malloc(sizeof(char)*(coresize+1));
  memset(filename, 0, sizeof(char)*(coresize+1));
  mxGetString(prhs[0], filename, coresize+1);

  mem=tt_read(filename, &head, &d, &r, &n, &buf, &dtype, &coresize);

  // create and report d
  plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
  num1 = mxGetPr(plhs[0]);
  num1[0]=(double)d;  // if evrthg is ok - d
  if (mem<0) {
    num1[0]=(double)mem; // if error - return it
    free(filename);
    return;
  }

  // create and report r
  plhs[1] = mxCreateDoubleMatrix(d+1,1,mxREAL);
  num1 = mxGetPr(plhs[1]);
  // create and report n
  plhs[2] = mxCreateDoubleMatrix(d,1,mxREAL);
  num2 = mxGetPr(plhs[2]);
  for (j=0; j<d; j++) {
    num1[j]=(double)r[j];
    num2[j]=(double)n[j];
  }
  num1[d]=(double)r[d];


  switch(dtype) {
    case 0: { // double, real
      dcore = (double *)buf;
      // create and report core
      plhs[3] = mxCreateDoubleMatrix(coresize,1,mxREAL);
      num1 = mxGetPr(plhs[3]);
      memcpy(num1, dcore, sizeof(double)*coresize);
      break;
    }
    case 1: { // double, complex
      zcore = (double complex *)buf;
      // create and report core
      plhs[3] = mxCreateDoubleMatrix(coresize,1,mxCOMPLEX);
      num1 = mxGetPr(plhs[3]);
      num2 = mxGetPi(plhs[3]);
      for (j=0; j<coresize; j++) {
	num1[j] = creal(zcore[j]);
	num2[j] = cimag(zcore[j]);
      }
      break;
    }
    case 2: { // float, real
      score = (float *)buf;
      // create and report core
      plhs[3] = mxCreateDoubleMatrix(coresize,1,mxREAL);
      num1 = mxGetPr(plhs[3]);
      for (j=0; j<coresize; j++) {
	num1[j] = score[j];
      }
      break;
    }
    case 3: { // float, complex
      ccore = (float complex *)buf;
      // create and report core
      plhs[3] = mxCreateDoubleMatrix(coresize,1,mxCOMPLEX);
      num1 = mxGetPr(plhs[3]);
      num2 = mxGetPi(plhs[3]);
      for (j=0; j<coresize; j++) {
	num1[j] = crealf(ccore[j]);
	num2[j] = cimagf(ccore[j]);
      }
      break;
    }
    default: {
      mexPrintf("Wrong datatype %d\n", dtype);
      return;
    }
  }

  free(r);
  free(n);
  free(filename);
  free(buf);
}
Beispiel #5
0
/******************************************************************************
 * SPLATT-CPD
 *****************************************************************************/
int splatt_cpd_cmd(
  int argc,
  char ** argv)
{
  /* assign defaults and parse arguments */
  cpd_cmd_args args;
  default_cpd_opts(&args);
  argp_parse(&cpd_argp, argc, argv, ARGP_IN_ORDER, 0, &args);
  srand(args.opts[SPLATT_OPTION_RANDSEED]);

  sptensor_t * tt = NULL;

  print_header();

  tt = tt_read(args.ifname);
  if(tt == NULL) {
    return SPLATT_ERROR_BADINPUT;
  }

  /* print basic tensor stats? */
  splatt_verbosity_type which_verb = args.opts[SPLATT_OPTION_VERBOSITY];
  if(which_verb >= SPLATT_VERBOSITY_LOW) {
    stats_tt(tt, args.ifname, STATS_BASIC, 0, NULL);
  }

  splatt_csf * csf = splatt_csf_alloc(tt, args.opts);

  idx_t nmodes = tt->nmodes;
  tt_free(tt);

  /* print CPD stats? */
  if(which_verb >= SPLATT_VERBOSITY_LOW) {
    cpd_stats(csf, args.nfactors, args.opts);
  }

  splatt_kruskal factored;

  /* do the factorization! */
  int ret = splatt_cpd_als(csf, args.nfactors, args.opts, &factored);
  if(ret != SPLATT_SUCCESS) {
    fprintf(stderr, "splatt_cpd_als returned %d. Aborting.\n", ret);
    return ret;
  }

  printf("Final fit: %0.5"SPLATT_PF_VAL"\n", factored.fit);

  /* write output */
  if(args.write == 1) {
    char * lambda_name = NULL;
    if(args.stem) {
      asprintf(&lambda_name, "%s.lambda.mat", args.stem);
    } else {
      asprintf(&lambda_name, "lambda.mat");
    }
    vec_write(factored.lambda, args.nfactors, lambda_name);
    free(lambda_name);

    for(idx_t m=0; m < nmodes; ++m) {
      char * matfname = NULL;
      if(args.stem) {
        asprintf(&matfname, "%s.mode%"SPLATT_PF_IDX".mat", args.stem, m+1);
      } else {
        asprintf(&matfname, "mode%"SPLATT_PF_IDX".mat", m+1);
      }

      matrix_t tmpmat;
      tmpmat.rowmajor = 1;
      tmpmat.I = csf->dims[m];
      tmpmat.J = args.nfactors;
      tmpmat.vals = factored.factors[m];

      mat_write(&tmpmat, matfname);
      free(matfname);
    }
  }

  /* cleanup */
  splatt_csf_free(csf, args.opts);
  free_cpd_args(&args);

  /* free factor matrix allocations */
  splatt_free_kruskal(&factored);

  return EXIT_SUCCESS;
}