void car_module::putinfo()
{
	printf("mac=%d\n",mac);
	printf("sumcar=%2d spendcar=%2d\n",sumcar,spendcar);
	printf("rows=%2d cols=%2d\n",rows,cols);
	printf("speed_rows=%.3lf speed_cols=%.3lf\n",speed_rows,speed_cols);

	printf("\n");
	for(int i=rows-1;i>=0;i--)
	{
		for(int j=0;j<cols;j++)
		{
			printf("%2d ",i*cols+j);
		}
		printf("\n");
	}
	printf("\n");

	for(i=0;i<rows*cols;i++)
	{
		printf("rows=%2d cols=%2d id=%2d free=%2d time=%.3lf\n",getrows(map_queue[i].id),getcols(map_queue[i].id),map_queue[i].id,map_queue[i].idle,map_queue[i].time);
	}
	printf("\n");
}
Esempio n. 2
0
int spai_line
(matrix *A,
 int col,
 int spar,
 int lower_diag,
 int upper_diag,
 double tau,
 matrix *M)
{
  int s,nbq,nnz,dimr,block_width;
  double scalar_resnorm,block_resnorm,adjust_epsilon;

  int i,index,pe,len,ierr;
  int row_address;

  int *rptr;
  double *aptr;
  int j, k, ptr, low_c, up_c, ccol, row;
  int rlen;
  int *buf;
  int *rbuf;
  double *vbuf;
  double comp_max, tau_limit = 1 - tau;

  block_width = A->block_sizes[col];
  adjust_epsilon = epsilon*sqrt((double) block_width);

  if (spar == 1)   /* mark elements depending on tau parameter */
   {
    comp_max = 0;
/* find maximum in column resp. row if transposed */
    for (j=0; j<A->lines->len[col]; j++)
      {
       ptr = A->lines->ptrs[col][j];
       if (comp_max < fabs( A->lines->A[col][j]))
           comp_max = fabs( A->lines->A[col][j]);
      }
/* keep diagonal and elements about fraction of maximum */
    for (i=0, j=0; j<A->lines->len[col]; j++)
      {
       ptr = A->lines->ptrs[col][j];
       if (ptr == col + A->my_start_index
           || fabs(A->lines->A[col][j]/comp_max) > tau_limit)
       {
         n1[i] = A->block_sizes[j];
	 J->ptr[i++] = ptr;
	}
      }
     J->len = i;
     J->slen = i;
     dimr = nnz = 0;
    }
  else if (spar == 2)   /* set diagonals - mind switching cols and rows */
    {
     if ((low_c = col-upper_diag) < 0) low_c = 0;
     if ((up_c = col+lower_diag) > A->n-1) up_c = A->n-1;
     for (i=0, j=low_c; j<=up_c; j++,i++)
       {
        J->ptr[i] = j;
        n1[i] = A->block_sizes[j];
       }
     J->len = i;
     J->slen = i;
     dimr = nnz = 0;
    }
  else /* initial sparsity diagonal */
    {
     J->ptr[0] = col;
     J->len = 1;
     J->slen = block_width;
     n1[0] = block_width;
     dimr = nnz = 0;
    }
  /* compute I */
  getrows(A,M,J,I);

  copyvv(J,J_tilde);

  for (s=0,
	 nbq = 0,
	 TAU_ptr[0] = 0,
                            /* effectively infinity */
	 scalar_resnorm=block_resnorm=1000000*epsilon;
       (s < nbsteps);
       s++,
	 nbq++) {

    com_server(A,M);

    full_matrix(A,M,max_dim, Ahat);

    n2[s] = I->slen - dimr;

    /* compute solution -> x, residual, and update QR */
    if ((ierr = qr(A,col,nbq,dimr)) != 0)  return ierr;

    nnz = J->len;
    dimr = J->slen;

    /* is solution good enough? */
    /* Use Froebenius norm */
    convert_to_block
      (res,resb,col,I->ptr,A,max_dim,I->len);
    block_resnorm = frobenius_norm(resb,block_width,I->slen);

    if (debug) {
      fprintf(fptr_dbg,"  s=%d col=%d of %d block_resnorm=%12.4le\n",
	      s,col,A->n,block_resnorm);
      fflush(fptr_dbg);
    }
    if (spar == 1         /* row population with tau parameter */
     || spar == 2) break; /* fixed diagonals - no further ado */
    if (block_resnorm <= adjust_epsilon)  break;

    /* Don't bother with last augment_sparsity */
    if (s == (nbsteps-1)) break;

    if (! augment_sparsity(A,M,col,maxapi,block_resnorm)) break;

    getrows(A,M, J_tilde,I_tilde);

    deleter(I,I_tilde,A);
    if (! append(J,J_tilde)) break;   /* J <- J U J_tilde */
    if (! append(I,I_tilde)) break;   /* I <- I U I_tilde */

  }

  if (block_resnorm > adjust_epsilon && spar == 0) {
    num_bad_cols++;
    if (message) {
      fprintf(message,
	      "could not meet tol, col=%d resnorm = %le, adjust_epsilon = %le\n",
	      col+1,
	      block_resnorm/sqrt((double) block_width),
	      adjust_epsilon);
      fflush(message);
    }
  }

  if (resplot_fptr) {
    for (i=0; i<block_width; i++) {
      if (block_resnorm <= adjust_epsilon) block_flag = " ";
      else block_flag = "*";
      scalar_resnorm = frobenius_norm(&res[i*max_dim],1,I->slen);
      if (scalar_resnorm <= epsilon) scalar_flag = " ";
      else scalar_flag = "*";
      fprintf(resplot_fptr,"%6d   %5.3lf %s %6d   %5.3lf %s\n",
	      start_col+i,
	      scalar_resnorm,
	      scalar_flag,
	      col,
	      block_resnorm/sqrt((double) block_width),
	      block_flag);
    }
    start_col += block_width;
  }

  /* current solution in x, up to nnz, written to M(k,:) */
  /* convert x to block structure */
  convert_to_block
    (x,xb,col,J->ptr,A,max_dim,nnz);

  put_Mline(A,M, col, J->ptr, xb, nnz, J->slen);

  for (i=0; i<nbsteps; i++) {
    if (Qlist[i]) {
      free(Qlist[i]);
      Qlist[i] = NULL;
    }
    else break;
  }
  return 0;
}
Esempio n. 3
0
/*********************************************************************************
 *                                                                               *
 *                           G A T E W A Y   R O U T I N E                       *
 *                                                                               *
 *********************************************************************************/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  /*
   >>>>>>>>>>>>>>>>>>           VARIABLE DECLARATIONS          <<<<<<<<<<<<<<<<<<<
   */
   matrix *PI_vector;
   matrix *NetDef, *W1, *W2, *PHI, *Y, *L_hidden, *H_hidden;
   double *M, lambda;
   int iter, hidden, inputs, outputs, a, n, decays;
   trparmstruct *trparms;
   mxArray  *Matmatrix;
   char *infolevelstr[] = {"infolevel", "Infolevel", "INFOLEVEL", "InfoLevel"};
   char *maxiterstr[] = {"maxiter", "MAXITER", "Maxiter", "MaxIter"};
   char *critminstr[] = {"critmin", "Critmin", "CRITMIN", "CritMin"};
   char *crittermstr[] = {"critterm", "Critterm", "CRITTERM", "CritTerm"};
   char *gradtermstr[] = {"gradterm", "Gradterm", "GRADTERM", "GradTerm"};
   char *paramtermstr[] = {"paramterm", "Paramterm", "PARAMTERM", "ParamTerm"};
   char *Dstr[] = {"D", "d"};
   char *lambdastr[] = {"lambda", "Lambda", "LAMBDA"};
   char *skipstr[] = {"skip", "Skip", "SKIP"};


  /*
   >>>>>>>>>>>>>>>>      CHECK FOR PROPER NUMBER OF ARGUMENTS      <<<<<<<<<<<<<<<
   */
   if (nrhs<5 || nrhs>6)
   { 
       mexErrMsgTxt("Wrong number of input arguments");
   }
   else if (nlhs > 5)
   {
       mexErrMsgTxt("Too many output arguments");
   }


  /*
   >>>>>>>>>>>>>>>>>     CONVERT INPUT ARGUMENTS TO SM FORMAT     <<<<<<<<<<<<<<<<
   */
  NetDef  = matstring2sm(prhs[0]);
  PHI     = mat2sm(prhs[3]);
  Y       = mat2sm(prhs[4]);

  inputs  = mxGetM(prhs[3]);
  outputs = mxGetM(prhs[4]);
  L_hidden = neuvector(NetDef,1,'L');      /* Location of linear hidden units      */
  H_hidden = neuvector(NetDef,1,'H');      /* Location of tanh hidden units        */  
  hidden   = L_hidden->row*L_hidden->col + H_hidden->row*H_hidden->col;
  if(mxGetM(prhs[1])==0 || mxGetN(prhs[1])==0 || mxGetM(prhs[2])==0\
                         || mxGetN(prhs[2])==0){
   	W1 = mmake(hidden,inputs+1);
   	W2 = mmake(outputs,hidden+1);
   	mrand(W1); smul(W1,W1,0.5);
   	mrand(W2); smul(W2,W2,0.5);
  }
  else{
   	if(mxGetM(prhs[1])!=hidden) mexErrMsgTxt("W1 has the wrong dimension");
   	if(mxGetN(prhs[1])!=inputs+1) mexErrMsgTxt("W1 has the wrong dimension");
   	if(mxGetM(prhs[2])!=outputs) mexErrMsgTxt("W2 has the wrong dimension");
   	if(mxGetN(prhs[2])!=hidden+1) mexErrMsgTxt("W2 has the wrong dimension");
   	W1 = mat2sm(prhs[1]);     /* Input-to-hidden layer weights */
   	W2 = mat2sm(prhs[2]);     /* Hidden-to-output layer weights */
  }
 trparms = (trparmstruct*)malloc(sizeof(trparmstruct)); 
 a = 5;
 if (nrhs==6){
    /* INFOLEVEL */
    trparms->infolevel   = TRDINFOLEVEL;    
    for(n=0;n<4;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, infolevelstr[n]))!=NULL){
           trparms->infolevel=(int)(*mxGetPr(Matmatrix));
           break;
        }
    }

    /* MAXITER */
    trparms->maxiter   = TRDMAXITER;    
    for(n=0;n<4;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, maxiterstr[n]))!=NULL){
           trparms->maxiter=(int)(*mxGetPr(Matmatrix));
           break;
        }
    }

    /* CRITMIN */
    trparms->critmin   = TRDCRITMIN;    
    for(n=0;n<4;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, critminstr[n]))!=NULL){
           trparms->critmin=(double)(*mxGetPr(Matmatrix));
           break;
        }
    }

    
    /* CRITTERM */
    trparms->critterm   = TRDCRITTERM;    
    for(n=0;n<4;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, crittermstr[n]))!=NULL){
           trparms->critterm=(double)(*mxGetPr(Matmatrix));
           break;
        }
    }

    /* GRADTERM */
    trparms->gradterm   = TRDGRADTERM;    
    for(n=0;n<4;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, gradtermstr[n]))!=NULL){
           trparms->gradterm=(double)(*mxGetPr(Matmatrix));
           break;
        }
    }

    /* PARAMTERM */
    trparms->paramterm   = TRDPARAMTERM;    
    for(n=0;n<4;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, paramtermstr[n]))!=NULL){
           trparms->paramterm=(double)(*mxGetPr(Matmatrix));
           break;
        }
    }

    /* Lambda */
    trparms->lambda   = TRDLAMBDA;    
    for(n=0;n<3;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, lambdastr[n]))!=NULL){
           trparms->lambda=(double)(*mxGetPr(Matmatrix));
           break;
        }
    }

    /* D */
    for(n=0;n<2;n++){
        if ((Matmatrix=mxGetField(prhs[a], 0, Dstr[n]))!=NULL){
           decays = mxGetM(Matmatrix)*mxGetN(Matmatrix);
           trparms->D         = mmake(1,decays);
           M    = mxGetPr(Matmatrix);
           for(n=0;n<decays;n++){
              rvput(trparms->D,n,M[n]);
           }
           break;
        }
    }
    if(Matmatrix==NULL){
       trparms->D         = mmake(1,1);
       put_val(trparms->D,0,0,TRDD);
    }
}
  else
  {
    trparms->infolevel = TRDINFOLEVEL;
    trparms->maxiter   = TRDMAXITER;
    trparms->critmin   = TRDCRITMIN;
    trparms->critterm  = TRDCRITTERM;
    trparms->gradterm  = TRDGRADTERM;
    trparms->paramterm = TRDPARAMTERM;
    trparms->D         = mmake(1,1);
    put_val(trparms->D,0,0,TRDD);
    trparms->lambda    = TRDLAMBDA;
    trparms->skip      = TRDSKIP;
  }


  /*
   >>>>>>>>>>>>>>>>>>>>>>         CALL THE C-ROUTINE         <<<<<<<<<<<<<<<<<<<<<
   */
  marqc(&PI_vector, &iter, &lambda, NetDef, W1, W2, PHI, Y, trparms);


  /*
   >>>>>>>>>>>>>>>>>>>         CREATE OUTPUT MATICES            <<<<<<<<<<<<<<<<<<
   */
  plhs[0] = mxCreateDoubleMatrix(getrows(W1),getcols(W1),mxREAL);
  plhs[1] = mxCreateDoubleMatrix(getrows(W2),getcols(W2),mxREAL);
  plhs[2] = mxCreateDoubleMatrix(getrows(PI_vector),getcols(PI_vector),mxREAL);
  plhs[3] = mxCreateDoubleMatrix(1,1,mxREAL);
  plhs[4] = mxCreateDoubleMatrix(1,1,mxREAL);

  sm2mat(plhs[0],W1);
  sm2mat(plhs[1],W2);
  sm2mat(plhs[2],PI_vector);
  M = mxGetPr(plhs[3]); M[0] = (double)iter;
  M = mxGetPr(plhs[4]); M[0] = (double)lambda;

  /*
   >>>>>>>>>>>>>>>>>>>>        FREE ARGUMENT MATRICES        <<<<<<<<<<<<<<<<<<<<<
   */
  mfree(NetDef); mfree(PHI); mfree(Y); mfree(L_hidden); mfree(H_hidden);
  mfree(trparms->D);
  free(trparms); 
}