Пример #1
0
void
runtime·helpgc(int32 nproc)
{
    M *mp;
    int32 n;

    runtime·lock(&runtime·sched);
    for(n = 1; n < nproc; n++) { // one M is currently running
        mp = mget(nil);
        if(mp == nil)
            runtime·throw("runtime·gcprocs inconsistency");
        mp->helpgc = 1;
        mp->waitnextg = 0;
        runtime·notewakeup(&mp->havenextg);
    }
    runtime·unlock(&runtime·sched);
}
Пример #2
0
int
get8(Map *map, uvlong addr, uvlong *x)
{
	if (!map) {
		werrstr("get8: invalid map");
		return -1;
	}

	if (map->nsegs == 1 && map->seg[0].fd < 0) {
		*x = addr;
		return 1;
	}
	if (mget(map, addr, x, 8) < 0)
		return -1;
	*x = machdata->swav(*x);
	return 1;
}
Пример #3
0
int
get4(Map *map, uint64_t addr, uint32_t *x)
{
	if (!map) {
		werrstr("get4: invalid map");
		return -1;
	}

	if (map->nsegs == 1 && map->seg[0].fd < 0) {
		*x = addr;
		return 1;
	}
	if (mget(map, addr, x, 4) < 0)
		return -1;
	*x = machdata->swal(*x);
	return 1;
}
Пример #4
0
// Kick off new ms as needed (up to mcpumax).
// There are already `other' other cpus that will
// start looking for goroutines shortly.
// Sched is locked.
static void
matchmg(void)
{
	G *g;

	if(m->mallocing || m->gcing)
		return;
	while(runtime·sched.mcpu < runtime·sched.mcpumax && (g = gget()) != nil){
		M *m;

		// Find the m that will run g.
		if((m = mget(g)) == nil){
			m = runtime·malloc(sizeof(M));
			// Add to runtime·allm so garbage collector doesn't free m
			// when it is just in a register or thread-local storage.
			m->alllink = runtime·allm;
			runtime·allm = m;
			m->id = runtime·sched.mcount++;

			if(runtime·iscgo) {
				CgoThreadStart ts;

				if(libcgo_thread_start == nil)
					runtime·throw("libcgo_thread_start missing");
				// pthread_create will make us a stack.
				m->g0 = runtime·malg(-1);
				ts.m = m;
				ts.g = m->g0;
				ts.fn = runtime·mstart;
				runtime·asmcgocall(libcgo_thread_start, &ts);
			} else {
				if(Windows)
					// windows will layout sched stack on os stack
					m->g0 = runtime·malg(-1);
				else
					m->g0 = runtime·malg(8192);
				runtime·newosproc(m, m->g0, m->g0->stackbase, runtime·mstart);
			}
		}
		mnextg(m, g);
	}
}
Пример #5
0
// Kick off new m's as needed (up to mcpumax).
// Sched is locked.
static void
matchmg(void)
{
	G *gp;
	M *mp;

	if(m->mallocing || m->gcing)
		return;

	while(haveg() && canaddmcpu()) {
		gp = gget();
		if(gp == nil)
			runtime·throw("gget inconsistency");

		// Find the m that will run gp.
		if((mp = mget(gp)) == nil)
			mp = runtime·newm();
		mnextg(mp, gp);
	}
}
Пример #6
0
int
get1(Map *map, uint64_t addr, uint8_t *x, int size)
{
	uint8_t *cp;

	if (!map) {
		werrstr("get1: invalid map");
		return -1;
	}

	if (map->nsegs == 1 && map->seg[0].fd < 0) {
		cp = (uint8_t*)&addr;
		while (cp < (uint8_t*)(&addr+1) && size-- > 0)
			*x++ = *cp++;
		while (size-- > 0)
			*x++ = 0;
	} else
		return mget(map, addr, x, size);
	return 1;
}
double lda_e_step(lda* model,
		  corpus_t* data,
		  lda_suff_stats* ss) {
    int d, k, n;

    if (ss != NULL) reset_lda_suff_stats(ss);
    lda_post* p = new_lda_post(model->ntopics, data->max_unique);
    p->model = model;
    double lhood = 0;

    // for each document

    for (d = 0; d < data->ndocs; d++)
    {
        if (((d % 1000) == 0) && (d > 0))
        {
            outlog( "e-step: processing doc %d", d);
        }

        // fit posterior

        p->doc = data->doc[d];
        lhood += fit_lda_post(d, 0, p, NULL, NULL, NULL, NULL, NULL);

        // update expected sufficient statistics

        if (ss != NULL)
            for (k = 0; k < model->ntopics; k++)
                for (n = 0; n < p->doc->nterms; n++)
                    minc(ss->topics_ss,
                         p->doc->word[n], k,
                         mget(p->phi, n, k) * p->doc->count[n]);
    }

    // !!! FREE POSTERIOR

    return(lhood);
}
Пример #8
0
void c_ctr::read_init_information(const char* theta_init_path, 
                                  const char* beta_init_path,
                                  const c_corpus* c,
                                  double alpha_smooth) {
  int num_topics = m_num_factors;
  m_theta = gsl_matrix_alloc(c->m_num_docs, num_topics);
  printf("\nreading theta initialization from %s\n", theta_init_path);
  FILE * f = fopen(theta_init_path, "r");
  mtx_fscanf(f, m_theta);
  fclose(f);

  //smoothing
  gsl_matrix_add_constant(m_theta, alpha_smooth);

  //normalize m_theta, in case it's not
  for (size_t j = 0; j < m_theta->size1; j ++) {
    gsl_vector_view theta_v = gsl_matrix_row(m_theta, j);
    vnormalize(&theta_v.vector);
  }

  m_beta = gsl_matrix_alloc(num_topics, c->m_size_vocab);
  printf("reading beta initialization from %s\n", beta_init_path);
  f = fopen(beta_init_path, "r");
  mtx_fscanf(f, m_beta);
  fclose(f);

  // exponentiate if it's not
  if (mget(m_beta, 0, 0) < 0) {
    mtx_exp(m_beta);
  }
  else {
    gsl_matrix_add_constant(m_beta, beta_smooth);
    for (size_t j = 0; j < m_beta->size1; j ++) {
      gsl_vector_view beta_v = gsl_matrix_row(m_beta, j);
      vnormalize(&beta_v.vector);
    }
  }
}
Пример #9
0
int DPMHC_S_smplr(struct str_DPMHC *ptr_DPMHC_data)
{
    int i_K = ptr_DPMHC_data->i_K;
    gsl_vector *v_y = ptr_DPMHC_data->v_y;
    gsl_vector *v_w = ptr_DPMHC_data->v_w;
    gsl_vector *v_u = ptr_DPMHC_data->v_u;
    gsl_vector_int *vi_S = ptr_DPMHC_data->vi_S;
    gsl_vector_int *vi_n = ptr_DPMHC_data->vi_n;
    gsl_matrix *m_DPtheta = ptr_DPMHC_data->m_DPtheta;




    size_t i_n=v_y->size;
    gsl_vector *v_p = gsl_vector_calloc (i_K);
    gsl_vector_int *vi_cnt = gsl_vector_int_calloc (i_K);
    int i,j,st,ism;
    double sm,dn;

    double d_yi,d_muj,d_xij,d_tauj;

    gsl_vector_int_set_zero ( vi_cnt );

    gsl_vector_int_set_zero ( vi_n );

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

    //printf("i=%d\n",i);

        d_yi = v_y->data[i];


        sm=0.0;
        for(j=0;j<i_K;j++){

            if( vget(v_w,j) <= vget(v_u,i) ){
                vset(v_p,j,0.0);
        //      printf("w_j,u_i %g %g",vget(w,j),vget(u,i));
            }else{
        //  printf("K=%d\n",i_K);
        //        dn = den_norm_prec ( vget(y,i), mget(theta,j,0), mget(theta,j,1) );

                d_muj = mget(m_DPtheta,j,0);
                d_xij = mget(m_DPtheta,j,1);
                d_tauj = mget(m_DPtheta,j,2);

                dn = exp( log_nor(d_yi, d_muj, fabs(d_xij) * fabs(d_xij) / d_tauj  ));
                vset(v_p,j,dn);
                sm+=dn;
        //printf("p>0\n");
            }
        }

        /* standardize */
        gsl_vector_scale( v_p, 1.0/sm );

      //      pvec(p);

        /* take draw */
        st = (int)ran_multinomial( v_p, i_K-1 );
        vset_int(vi_S,i,st);
        vset_int(vi_cnt,st,1);
        (vi_n->data[st])++;
    }



    /* find number of clusters with at least one observation assigned to it */
    ism=0;
    for(j=0;j<i_K;j++){
        if( vget_int(vi_cnt,j) == 1)ism++;
    }

    ptr_DPMHC_data->i_m = ism;

    gsl_vector_free (v_p);
    gsl_vector_int_free (vi_cnt);


    return 0;
}
Пример #10
0
int main(int argc, char *argv[]){

	char buff[N], *param; //1024
	int j = 0, nb;

	if( argc != 2){

		printf("Usage: %s <server host>\n", argv[0]);
		exit(1);
	}

	printf("commands\n");
	do{

		memset(buff, '\0', N);
		
		prompt();	
		gets(buff);
	
		param = strchr(buff,' '); // param <- commands
		if (param) {
    			*param=0;
    			param++;
  		}

		for(j = 0; j < NB_CMDS; j++)
			if( !strcmp(buff, commandes[j]))
				break;	
			
		switch(j){

 			case(OPEN):	if(connection) printf("already connected\n");
					else myconnect(argv[1]);
					break;
		
			case(GET):	if(!connection) printf("Not connected!\n"); 
					else get(param);
					break;

			case(PUT):	if(!connection) printf("Not connected!\n"); 
					else put(param);
					break;
				
			case(MGET):	if(!connection) printf("Not connected!\n"); 
					else mget(param);
					break;		
			case(MPUT):	if(!connection) printf("Not connected!\n"); 
					else mput(param);
					break;
			
			case(CD):	if(!connection) printf("NoT connecteD!\n"); 
					else cd(param);
					break;
			
			case(LCD):	lcd(param);
					break;
							
			case(MGET_):	if(!connection) printf("Not connected!\n"); 
					else mget_(param);
					break;		
			case(MPUT_):	if(!connection) printf("Not connected!\n"); 
					else mput_(param);
					break;
			

 			case(DIR):	if(!connection) printf("Not connecte!\n"); 
					else dir();
					break;
			case(LDIR):
					ldir();
					break;
			case(RGET):	if(!connection) printf("Not connected!\n"); 
					else rget(param);
					break;		
			case(RPUT):	if(!connection) printf("Not connected!\n"); 
					else rput(param);
					break;		

			//case (HELP):	aide();
			//		break;

			default:	printf("%s command invalid\n", buff);
					printf("Commands are as follows\n");
					printf(" get : To Get A file from Server\n");
					printf(" put : To Put A file to Server\n");
					printf(" cd : change Server's Current Directory\n");
					printf(" lcd : Change Client's current Directory\n");
					printf(" mget : To Get multiple files from Server\n");
					printf(" mput : To put multiple files from Server\n");
					printf(" dir : List the Server's current Directory\n");
					printf(" ldir : List the Client's current Directory\n");
					printf(" mget_ : Put files with wildcard support\n");
					printf("mput_ : get files with wildcard support\n");
					printf("rget : Get a directory recursively\n");
					printf("rput : Put a Directory recursiovely\n");
					break;
		}
		
	}
	while(1);
 	
return(0);
}
Пример #11
0
void update_phi_fixed(int doc_number, int time,
		      lda_post* p, lda_seq* var,
		      gsl_matrix* g3_matrix,
		      gsl_matrix* g4_matrix,
		      gsl_matrix* g5_matrix) {
    // Hate to do this, but I had problems allocating this data
    // structure.
    if (scaled_influence == NULL) {
      scaled_influence = NewScaledInfluence(FLAGS_max_number_time_points);
    }

    int i, k, n, K = p->model->ntopics, N = p->doc->nterms;
    double dig[p->model->ntopics];

    double k_sum = 0.0;
    for (k = 0; k < K; k++) {
        double gamma_k = vget(p->gamma, k);
        dig[k] = gsl_sf_psi(gamma_k);
	k_sum += gamma_k;
    }
    double dig_sum = gsl_sf_psi(k_sum);

    gsl_vector_view document_weights;
    if (var && var->influence) {
      document_weights = gsl_matrix_row(
      var->influence->doc_weights[time], doc_number);
    }

    for (n=0; n < N; ++n) {
      int w = p->doc->word[n];
      // We have info. about the topics. Use them!
      // Try two alternate approaches.  We compare results of the new
      // algorithm with the old to make sure we're not doing anything
      // silly.

      for (k = 0; k < K; ++k) {
	// Find an estimate for log_phi_nk.
	double doc_weight = 0.0;
	sslm_var* topic = var->topic[k];
	const double chain_variance = topic->chain_variance;

	// These matrices are already set up for the correct time.
	double g3 = mget(g3_matrix, w, k);
	double g4 = mget(g4_matrix, w, k);
	double g5 = mget(g5_matrix, w, k);
	double w_phi_sum = gsl_matrix_get(
	    var->topic[k]->w_phi_sum, w, time);

	// Only set these variables if we are within the correct
	// time window.
	if (time < var->nseq) {
	  doc_weight = gsl_vector_get(&document_weights.vector, k);
	}
	
	double term_weight;
	if (FLAGS_normalize_docs == "normalize") {
	  term_weight = ((double) p->doc->count[n]
			 / (double) p->doc->total);
	} else if (FLAGS_normalize_docs == "log") {
	  term_weight = log(p->doc->count[n] + 1.0);
	} else if (FLAGS_normalize_docs == "log_norm") {
	  term_weight = log(p->doc->count[n] / p->doc->total);
	} else if (FLAGS_normalize_docs == "identity") {
	  term_weight = p->doc->count[n];
	} else if (FLAGS_normalize_docs == "occurrence") {
	  term_weight = ((double) p->doc->count[n]
			 / (double) p->doc->total);
	} else {
	  assert(0);
	}

	assert(var != NULL);
	
	double total, term1, term2, term3, term4;
	double phi_last = 0.0;

	// It's unnecessary to always multiply by 1/chain_variance
	// this deep in a loop, but it's likely not a major
	// bottleneck.
	term1 = dig[k] + mget(p->model->topics, w, k);
	term2 = (g3
		 * term_weight
		 * doc_weight
		 / chain_variance);
	term3 = (term_weight
		 * doc_weight
		 * g4
		 / chain_variance);
	term4 = (term_weight * term_weight
		 * (phi_last * (doc_weight * doc_weight)
		    - (doc_weight * doc_weight
		       + FLAGS_sigma_l * FLAGS_sigma_l))
		 * g5
		 / chain_variance);

	// Note: we're ignoring term3.  sgerrish: 18 July 2010:
	// Changing term2 to have a positive coefficient (instead of
	// negative) to be consistent with parallel version.
	// sgerrish: 23 July 2010: changing term2 back to negative,
	// to try to reproduce earlier results.
	total = term1 - term2 - term3 + term4;
	mset(p->log_phi, n, k, total);
      }
      
      // Normalize in log space.
      gsl_vector log_phi_row = gsl_matrix_row(p->log_phi, n).vector;
      gsl_vector phi_row = gsl_matrix_row(p->phi, n).vector;
      log_normalize(&log_phi_row);
      
      for (i = 0; i < K; i++) {
	vset(&phi_row, i, exp(vget(&log_phi_row, i)));
      }
    }
}
Пример #12
0
void posterior_summary(const gsl_matrix *theta, FILE *ofile, long M)
{

  size_t T=theta->size1;
  size_t npar=theta->size2;
  gsl_vector *tmp=gsl_vector_alloc(T);
  int i,j;
  double median,lower,upper;

  printf("\n Writing MCMC draws to out\n\n");
  FILE *file = fopen("out","w");
  for(i=0;i<T;i++){
    for(j=0;j<npar;j++)
      fprintf(file,"%14.6e ",mget(theta,i,j));
    fprintf(file,"\n");
  }

  fprintf(ofile,"\n\n Posterior Summary \n");
  fprintf(ofile,"\n T=%lu\n\n",T);
  fprintf(ofile,"\n      Mean          Median         Stdev           0.95 DI\n\n");
  for(i=0;i<npar;i++){
    gsl_matrix_get_col( tmp, theta, i);
    gsl_sort_vector(tmp);
    median=gsl_stats_median_from_sorted_data(tmp->data,tmp->stride,tmp->size);
    lower=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.025);
    upper=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.975);

    fprintf(ofile,"%2d %14.7e %14.7e %14.7e (%14.7e,%14.7e)\n"
	   ,i,mean(tmp),median,sqrt(var(tmp)),lower,upper);
  }

  long tau;
  if( M < 0 )
    tau=1000;
  else
    tau=M;

  gsl_vector *rho=gsl_vector_alloc(tau);

  fprintf(ofile,"\n                                ACF");
  fprintf(ofile,"\n      NSE          Ineff        1            50           100          500\n");
  for(i=0;i<npar;i++){
    gsl_matrix_get_col( tmp, theta, i);
    acf(tmp,tau,rho);

    /* write out ACF for each parameter */
    char file_name[20] = "acf.dat";
    sprintf( &file_name[7],"%d",i);
    FILE *fp_acf = fopen( file_name, "w");

    for(j=0;j<tau;j++)
      fprintf(fp_acf,"%g\n",vget(rho,j));

    fclose(fp_acf);


    /* get inefficiency factor using Newey-West estimate of Long-run var*/
    double ineff=1.0;
    for(j=0;j<tau-1;j++){
      ineff += 2.0*(tau-j-1)/tau*vget(rho,j);
      }
    /* numerical standard error for posterior mean */
    double nse=sqrt(var(tmp)*ineff/T);

    fprintf(ofile,"%2d %12.5e %12.5e %12.5e %12.5e %12.5e %12.5e\n"
	    ,i,nse,ineff,vget(rho,0),vget(rho,49),vget(rho,99),vget(rho,499));

    /* produce kernel density plot for each parameter */
    char file_name2[20] = "den.dat";

    sprintf( &file_name2[7],"%d",i);
    FILE *fp_den = fopen( file_name2, "w");

    double stdev = sqrt(var(tmp));
    lower = gsl_vector_min(tmp) - stdev;
    upper = gsl_vector_max(tmp) + stdev;


    den_est_file(tmp, lower , upper ,100, fp_den, -1.0);


  }

  fprintf(ofile,"\n\n");
  gsl_vector_free(rho);
  gsl_vector_free(tmp);
}
Пример #13
0
/*
 * Retrieve multiple files from the command line, transferring
 * files of the form "host:path", "ftp://host/path" using the
 * ftp protocol, and files of the form "http://host/path" using
 * the http protocol.
 * If path has a trailing "/", then return (-1);
 * the path will be cd-ed into and the connection remains open,
 * and the function will return -1 (to indicate the connection
 * is alive).
 * If an error occurs the return value will be the offset+1 in
 * argv[] of the file that caused a problem (i.e, argv[x]
 * returns x+1)
 * Otherwise, 0 is returned if all files retrieved successfully.
 */
int
auto_fetch(int argc, char *argv[], char *outfile)
{
	char *xargv[5];
	char *cp, *url, *host, *dir, *file, *portnum;
	char *username, *pass, *pathstart;
	char *ftpproxy, *httpproxy;
	int rval, xargc;
	volatile int argpos;
	int dirhasglob, filehasglob, oautologin;
	char rempath[MAXPATHLEN];

	argpos = 0;

	if (setjmp(toplevel)) {
		if (connected)
			disconnect(0, NULL);
		return (argpos + 1);
	}
	(void)signal(SIGINT, (sig_t)intr);
	(void)signal(SIGPIPE, (sig_t)lostpeer);

	if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
		ftpproxy = NULL;
	if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
		httpproxy = NULL;

	/*
	 * Loop through as long as there's files to fetch.
	 */
	for (rval = 0; (rval == 0) && (argpos < argc); free(url), argpos++) {
		if (strchr(argv[argpos], ':') == NULL)
			break;
		host = dir = file = portnum = username = pass = NULL;

		/*
		 * We muck with the string, so we make a copy.
		 */
		url = strdup(argv[argpos]);
		if (url == NULL)
			errx(1, "Can't allocate memory for auto-fetch.");

		/*
		 * Try HTTP URL-style arguments first.
		 */
		if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
#ifndef SMALL
		    /* even if we compiled without SSL, url_get will check */
		    strncasecmp(url, HTTPS_URL, sizeof(HTTPS_URL) -1) == 0 ||
#endif /* !SMALL */
		    strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
			redirect_loop = 0;
			if (url_get(url, httpproxy, outfile) == -1)
				rval = argpos + 1;
			continue;
		}

		/*
		 * Try FTP URL-style arguments next. If ftpproxy is
		 * set, use url_get() instead of standard ftp.
		 * Finally, try host:file.
		 */
		host = url;
		if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
			char *passend, *passagain, *userend;

			if (ftpproxy) {
				if (url_get(url, ftpproxy, outfile) == -1)
					rval = argpos + 1;
				continue;
			}
			host += sizeof(FTP_URL) - 1;
			dir = strchr(host, '/');

			/* Look for [user:pass@]host[:port] */

			/* check if we have "user:pass@" */
			userend = strchr(host, ':');
			passend = strchr(host, '@');
			if (passend && userend && userend < passend &&
			    (!dir || passend < dir)) {
				username = host;
				pass = userend + 1;
				host = passend + 1;
				*userend = *passend = '\0';
				passagain = strchr(host, '@');
				if (strchr(pass, '@') != NULL ||
				    (passagain != NULL && passagain < dir)) {
					warnx(at_encoding_warning);
					goto bad_ftp_url;
				}

				if (EMPTYSTRING(username)) {
bad_ftp_url:
					warnx("Invalid URL: %s", argv[argpos]);
					rval = argpos + 1;
					continue;
				}
				username = urldecode(username);
				pass = urldecode(pass);
			}

#ifdef INET6
			/* check [host]:port, or [host] */
			if (host[0] == '[') {
				cp = strchr(host, ']');
				if (cp && (!dir || cp < dir)) {
					if (cp + 1 == dir || cp[1] == ':') {
						host++;
						*cp++ = '\0';
					} else
						cp = NULL;
				} else
					cp = host;
			} else
				cp = host;
#else
			cp = host;
#endif

			/* split off host[:port] if there is */
			if (cp) {
				portnum = strchr(cp, ':');
				pathstart = strchr(cp, '/');
				/* : in path is not a port # indicator */
				if (portnum && pathstart &&
				    pathstart < portnum)
					portnum = NULL;

				if (!portnum)
					;
				else {
					if (!dir)
						;
					else if (portnum + 1 < dir) {
						*portnum++ = '\0';
						/*
						 * XXX should check if portnum
						 * is decimal number
						 */
					} else {
						/* empty portnum */
						goto bad_ftp_url;
					}
				}
			} else
				portnum = NULL;
		} else {			/* classic style `host:file' */
			dir = strchr(host, ':');
		}
		if (EMPTYSTRING(host)) {
			rval = argpos + 1;
			continue;
		}

		/*
		 * If dir is NULL, the file wasn't specified
		 * (URL looked something like ftp://host)
		 */
		if (dir != NULL)
			*dir++ = '\0';

		/*
		 * Extract the file and (if present) directory name.
		 */
		if (!EMPTYSTRING(dir)) {
			cp = strrchr(dir, '/');
			if (cp != NULL) {
				*cp++ = '\0';
				file = cp;
			} else {
				file = dir;
				dir = NULL;
			}
		}
#ifndef SMALL
		if (debug)
			fprintf(ttyout,
			    "user %s:%s host %s port %s dir %s file %s\n",
			    username, pass ? "XXXX" : NULL, host, portnum,
			    dir, file);
#endif /* !SMALL */

		/*
		 * Set up the connection.
		 */
		if (connected)
			disconnect(0, NULL);
		xargv[0] = __progname;
		xargv[1] = host;
		xargv[2] = NULL;
		xargc = 2;
		if (!EMPTYSTRING(portnum)) {
			xargv[2] = portnum;
			xargv[3] = NULL;
			xargc = 3;
		}
		oautologin = autologin;
		if (username != NULL)
			autologin = 0;
		setpeer(xargc, xargv);
		autologin = oautologin;
		if ((connected == 0) ||
		    ((connected == 1) && !ftp_login(host, username, pass))) {
			warnx("Can't connect or login to host `%s'", host);
			rval = argpos + 1;
			continue;
		}

		/* Always use binary transfers. */
		setbinary(0, NULL);

		dirhasglob = filehasglob = 0;
		if (doglob) {
			if (!EMPTYSTRING(dir) &&
			    strpbrk(dir, "*?[]{}") != NULL)
				dirhasglob = 1;
			if (!EMPTYSTRING(file) &&
			    strpbrk(file, "*?[]{}") != NULL)
				filehasglob = 1;
		}

		/* Change directories, if necessary. */
		if (!EMPTYSTRING(dir) && !dirhasglob) {
			xargv[0] = "cd";
			xargv[1] = dir;
			xargv[2] = NULL;
			cd(2, xargv);
			if (!dirchange) {
				rval = argpos + 1;
				continue;
			}
		}

		if (EMPTYSTRING(file)) {
			rval = -1;
			continue;
		}

		if (verbose)
			fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);

		if (dirhasglob) {
			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
			file = rempath;
		}

		/* Fetch the file(s). */
		xargc = 2;
		xargv[0] = "get";
		xargv[1] = file;
		xargv[2] = NULL;
		if (dirhasglob || filehasglob) {
			int ointeractive;

			ointeractive = interactive;
			interactive = 0;
			xargv[0] = "mget";
#ifndef SMALL
			if (resume) {
				xargc = 3;
				xargv[1] = "-c";
				xargv[2] = file;
				xargv[3] = NULL;
			}
#endif /* !SMALL */
			mget(xargc, xargv);
			interactive = ointeractive;
		} else {
			if (outfile != NULL) {
				xargv[2] = outfile;
				xargv[3] = NULL;
				xargc++;
			}
#ifndef SMALL
			if (resume)
				reget(xargc, xargv);
			else
#endif /* !SMALL */
				get(xargc, xargv);
		}

		if ((code / 100) != COMPLETE)
			rval = argpos + 1;
	}
	if (connected && rval != -1)
		disconnect(0, NULL);
	return (rval);
}
Пример #14
0
int DPMHC_xi_smplr(struct str_DPMHC *ptr_DPMHC_data, int i_J, struct str_firm_data *a_firm_data)
{
    int j,i;
    int i_K = ptr_DPMHC_data->i_K;
    int i_n = ptr_DPMHC_data->v_y->size; // is this the same as i_J???
    if (i_n != i_J){
        fprintf(stderr,"Error in  DPMN_xi_smplr(): DPMHC.v_y length does not equal i_J\n");
        exit(1);
    }
    double d_sumT_si;

    gsl_vector_int *vi_S = ptr_DPMHC_data->vi_S;
    gsl_matrix *m_theta = ptr_DPMHC_data->m_DPtheta;
    double d_A = ptr_DPMHC_data->d_A;
    double d_mu_si, d_tau_si, d_ei;
    double d_mean_j, d_var_j;
    double d_xi_j;

    int i_Ti;
    int i_factors = (a_firm_data[0].v_beta)->size;
    gsl_vector *v_ret;
    gsl_matrix *m_factors;
    gsl_vector *v_betai;
    gsl_vector *v_rstar_i;
    gsl_matrix *m_Xi;
    gsl_matrix_view mv_Xi;
    double d_rstar_j;
    double d_s2i;


    for(j=0;j<i_K;j++){

        d_mu_si = mget(m_theta,j,0);
        d_tau_si = mget(m_theta,j,2);


        d_rstar_j = 0.;
        d_sumT_si = 0;
        for(i=0;i<i_J;i++){
            if( vget_int(vi_S,i) == j ){

                d_ei = vget(ptr_DPMHC_data->v_e,i);

                m_factors = a_firm_data[i].m_factors;
                i_Ti = a_firm_data[i].i_ni;
                d_s2i = a_firm_data[i].d_s2;
                m_Xi = gsl_matrix_alloc(i_Ti,i_factors);
                mv_Xi = gsl_matrix_submatrix(m_factors,0,0,i_Ti,i_factors);
                gsl_matrix_memcpy(m_Xi,&mv_Xi.matrix);

                v_betai = a_firm_data[i].v_beta;
                v_ret = a_firm_data[i].v_ret;
                v_rstar_i = gsl_vector_alloc(i_Ti);
                gsl_vector_memcpy(v_rstar_i,v_ret);
                gsl_blas_dgemv(CblasNoTrans, -1.0, m_Xi, v_betai, 1.0, v_rstar_i);

                gsl_vector_add_constant(v_rstar_i, -d_mu_si);
                gsl_vector_scale(v_rstar_i, 1./(sqrt(d_tau_si) * d_ei) );

                d_rstar_j += sum(v_rstar_i);

                d_sumT_si += i_Ti/(d_s2i/(d_tau_si * d_ei * d_ei) );

                gsl_matrix_free(m_Xi);
                gsl_vector_free(v_rstar_i);
            }
        }

        d_var_j = 1./( 1./(d_A * d_A) + d_sumT_si);
        d_mean_j = d_rstar_j * d_var_j;

        d_xi_j = d_mean_j + gsl_ran_gaussian_ziggurat(rng, sqrt(d_var_j) );

        mset(m_theta, j, 1, d_xi_j);

       // printf("%d: eta = %g lambda^2 = %g\n",j, mget(m_theta,j,0), mget(m_theta,j,1) );
     }


    return 0;
}
Пример #15
0
void minc(gsl_matrix* m, int i, int j, double x)
{
    mset(m, i, j, mget(m, i, j) + x);
}
Пример #16
0
/*
 * Go through the whole list, stopping if you find a match.  Process all
 * the continuations of that match before returning.
 *
 * We support multi-level continuations:
 *
 *	At any time when processing a successful top-level match, there is a
 *	current continuation level; it represents the level of the last
 *	successfully matched continuation.
 *
 *	Continuations above that level are skipped as, if we see one, it
 *	means that the continuation that controls them - i.e, the
 *	lower-level continuation preceding them - failed to match.
 *
 *	Continuations below that level are processed as, if we see one,
 *	it means we've finished processing or skipping higher-level
 *	continuations under the control of a successful or unsuccessful
 *	lower-level continuation, and are now seeing the next lower-level
 *	continuation and should process it.  The current continuation
 *	level reverts to the level of the one we're seeing.
 *
 *	Continuations at the current level are processed as, if we see
 *	one, there's no lower-level continuation that may have failed.
 *
 *	If a continuation matches, we bump the current continuation level
 *	so that higher-level continuations are processed.
 */
char *
magic_match(u_char *s, int len)
{
	int i, cont_level = 0;
	union VALUETYPE p;
	static int32_t *tmpoff = NULL;
	static size_t tmplen = 0;
	int32_t oldoff = 0;

	Match[0] = '\0';
	
	if (tmpoff == NULL)
		if ((tmpoff = (int32_t *) malloc(tmplen = 20)) == NULL)
			err(1, "malloc");
	
	for (i = 0; i < Magiccnt; i++) {
		/* if main entry matches, print it... */
		if (!mget(&p, s, &Magic[i], len) || !mcheck(&p, &Magic[i])) {
			    /* 
			     * main entry didn't match,
			     * flush its continuations
			     */
			while (i < Magiccnt && Magic[i + 1].cont_level != 0)
				i++;
			continue;
		}
		tmpoff[cont_level] = mprint(&p, &Magic[i]);
		
		/* and any continuations that match */
		if (++cont_level >= tmplen) {
			tmplen += 20;
			if (!(tmpoff = (int32_t *) realloc(tmpoff, tmplen)))
				err(1, "magic_match: malloc");
		}
		while (Magic[i + 1].cont_level != 0 && ++i < Magiccnt) {
			if (cont_level >= Magic[i].cont_level) {
				if (cont_level > Magic[i].cont_level) {
					/*
					 * We're at the end of the level
					 * "cont_level" continuations.
					 */
					cont_level = Magic[i].cont_level;
				}
				if (Magic[i].flag & ADD) {
					oldoff = Magic[i].offset;
					Magic[i].offset +=
						tmpoff[cont_level - 1];
				}
				if (mget(&p, s, &Magic[i], len) &&
				    mcheck(&p, &Magic[i])) {
					/* This continuation matched. */
					tmpoff[cont_level] =
						mprint(&p, &Magic[i]);

					/*
					 * If we see any continuations
					 * at a higher level, process them.
					 */
					if (++cont_level >= tmplen) {
						tmplen += 20;
						if (!(tmpoff = (int32_t *)
						     realloc(tmpoff, tmplen)))
							err(1, "magic_check: "
							    "malloc");
					}
				}
				if (Magic[i].flag & ADD) {
					Magic[i].offset = oldoff;
				}
			}
		}
		return (strlen(Match) ? Match : NULL);	/* all through */
	}
	return (NULL);			/* no match at all */
}
Пример #17
0
/*
 * Go through the whole list, stopping if you find a match.  Process all
 * the continuations of that match before returning.
 *
 * We support multi-level continuations:
 *
 *	At any time when processing a successful top-level match, there is a
 *	current continuation level; it represents the level of the last
 *	successfully matched continuation.
 *
 *	Continuations above that level are skipped as, if we see one, it
 *	means that the continuation that controls them - i.e, the
 *	lower-level continuation preceding them - failed to match.
 *
 *	Continuations below that level are processed as, if we see one,
 *	it means we've finished processing or skipping higher-level
 *	continuations under the control of a successful or unsuccessful
 *	lower-level continuation, and are now seeing the next lower-level
 *	continuation and should process it.  The current continuation
 *	level reverts to the level of the one we're seeing.
 *
 *	Continuations at the current level are processed as, if we see
 *	one, there's no lower-level continuation that may have failed.
 *
 *	If a continuation matches, we bump the current continuation level
 *	so that higher-level continuations are processed.
 */
static int match(RMagic *ms, struct r_magic *magic, ut32 nmagic, const ut8 *s, size_t nbytes, int mode) {
    ut32 magindex = 0;
    unsigned int cont_level = 0;
    int need_separator = 0;
    int returnval = 0; /* if a match is found it is set to 1*/
    int firstline = 1; /* a flag to print X\n  X\n- X */
    int printed_something = 0;

    if (file_check_mem (ms, cont_level) == -1)
        return -1;

    for (magindex = 0; magindex < nmagic; magindex++) {
        int flush;
        struct r_magic *m = &magic[magindex];

        if ((m->flag & BINTEST) != mode) {
            /* Skip sub-tests */
            while (magic[magindex + 1].cont_level != 0 &&
                    ++magindex < nmagic - 1)
                continue;
            continue; /* Skip to next top-level test*/
        }

        ms->offset = m->offset;
        ms->line = m->lineno;

        /* if main entry matches, print it... */
        flush = !mget(ms, s, m, nbytes, cont_level);
        if (flush) {
            if (m->reln == '!')
                flush = 0;
        } else {
            int ret = magiccheck (ms, m);
            if (ret == -1) return -1;
            if (!ret) flush++;
        }
        if (flush) {
            /*
             * main entry didn't match,
             * flush its continuations
             */
            while (magindex < nmagic - 1 && magic[magindex + 1].cont_level)
                magindex++;
            continue;
        }

        /*
         * If we are going to print something, we'll need to print
         * a blank before we print something else.
         */
        if (*R_MAGIC_DESC) {
            need_separator = 1;
            printed_something = 1;
            if (print_sep(ms, firstline) == -1)
                return -1;
        }

        if ((ms->c.li[cont_level].off = mprint(ms, m)) == -1)
            return -1;

        /* and any continuations that match */
        if (file_check_mem(ms, ++cont_level) == -1)
            return -1;

        while (++magindex < nmagic - 1 && magic[magindex].cont_level != 0) {
            m = &magic[magindex];
            ms->line = m->lineno; /* for messages */

            if (cont_level < m->cont_level)
                continue;
            if (cont_level > m->cont_level) {
                /*
                 * We're at the end of the level
                 * "cont_level" continuations.
                 */
                cont_level = m->cont_level;
            }
            ms->offset = m->offset;
            if (m->flag & OFFADD)
                ms->offset += ms->c.li[cont_level - 1].off;

            if (m->cond == COND_ELSE || m->cond == COND_ELIF) {
                if (ms->c.li[cont_level].last_match == 1)
                    continue;
            }
            flush = !mget(ms, s, m, nbytes, cont_level);
            if (flush && m->reln != '!')
                continue;

            switch (flush ? 1 : magiccheck(ms, m)) {
            case -1:
                return -1;
            case 0:
                ms->c.li[cont_level].last_match = 0;
                break;
            default:
                ms->c.li[cont_level].last_match = 1;
                if (m->type != FILE_DEFAULT)
                    ms->c.li[cont_level].got_match = 1;
                else if (ms->c.li[cont_level].got_match) {
                    ms->c.li[cont_level].got_match = 0;
                    break;
                }
                /*
                 * If we are going to print something,
                 * make sure that we have a separator first.
                 */
                if (*R_MAGIC_DESC) {
                    printed_something = 1;
                    if (print_sep(ms, firstline) == -1)
                        return -1;
                }
                /*
                 * This continuation matched.  Print
                 * its message, with a blank before it
                 * if the previous item printed and
                 * this item isn't empty.
                 */
                /* space if previous printed */
                if (need_separator
                        && ((m->flag & NOSPACE) == 0)
                        && *R_MAGIC_DESC) {
                    if (file_printf (ms, " ") == -1)
                        return -1;
                    need_separator = 0;
                }
                if ((ms->c.li[cont_level].off = mprint(ms, m)) == -1)
                    return -1;
                if (*R_MAGIC_DESC)
                    need_separator = 1;

                /*
                 * If we see any continuations
                 * at a higher level,
                 * process them.
                 */
                if (file_check_mem(ms, ++cont_level) == -1)
                    return -1;
                break;
            }
        }
        if (printed_something) {
            firstline = 0;
            returnval = 1;
        }
        if ((ms->flags & R_MAGIC_CONTINUE) == 0 && printed_something)
            return 1; /* don't keep searching */
    }
    return returnval;  /* This is hit if -k is set or there is no match */
}