Пример #1
0
static void
history_shift(Rune c, void* f, int n){
	if(n){
		history_save();
		history_advance(n);
	}
	history_load();
}
Пример #2
0
/* load the history
 * ----------------------------------------------------------------------- */
void history_load(void) {
  unsigned int i;
  unsigned int hlen;
  char fname[PATH_MAX + 1];

  hlen = str_copyn(fname, sh_home, PATH_MAX);
  if(hlen >= PATH_MAX - 3)
    return;
  
  /* append a trailing slash if not already there */
  if(hlen && fname[hlen - 1] != '/')
    fname[hlen++] = '/';
  
  fname[hlen++] = '.';
  
  /* mapped history entries */
  history_mapped = 0;
  
  for(i = 0; history_files[i]; i++) {
    /* append history file name */
    str_copyn(&fname[hlen], history_files[i], PATH_MAX - hlen);

    /* then try to open the history file */
    if(buffer_mmapread(&history_buffer, fname) == 0)
      break;
  }
  
  /* we tried all files: nothing to load */
  if(history_files[i] == NULL)
    return;

  /* loop through the mapped history file and separate
   * history entries using nul-string-termination 
   *
   * note that quote-balancing is applied because there 
   * may be quoted multiline words in the history 
   */
  while(history_buffer.p < history_buffer.n) {
    unsigned long len;
    
    /* next entry will begin after the termination */
    if(!(len = history_cmdlen(&history_buffer.x[history_buffer.p])))
      break;
    
    while(parse_isspace(history_buffer.x[history_buffer.p]))
      history_buffer.p++;
    
    history_set(&history_buffer.x[history_buffer.p]);
    history_advance();
    history_mapped++;
    
    history_buffer.p += len;
  }
  
  /* unmap the file if it didn't contain any entries */
  if(history_buffer.n && history_mapped == 0)
    buffer_close(&history_buffer);
}
Пример #3
0
static void test_isend1()
{
	double window = 1000;
	const struct var2 *v = design2_add_tvar(DESIGN2, "ISend", VAR2_ISEND, window);
	double t0 = 910930020;
	const double *x;
	const size_t *ind;
	size_t nz;

	assert_true(v);

	design2_get_tvar_matrix(DESIGN2, 137, &x, &ind, &nz); /* -inf */
	assert_int_equal(nz, 0);

	history_advance(HISTORY, t0); /* t */
	design2_get_tvar_matrix(DESIGN2, 137, &x, &ind, &nz);
	assert_int_equal(nz, 0);

	history_advance(HISTORY, double_nextup(t0)); /* (t)+ */
	design2_get_tvar_matrix(DESIGN2, 137, &x, &ind, &nz);
	assert_int_equal(nz, 1);
	assert_int_equal(ind[0], 58);
	assert_real_identical(x[0], 1.0);

	design2_get_tvar_matrix(DESIGN2, 58, &x, &ind, &nz);
	assert_int_equal(nz, 0);

	history_advance(HISTORY, t0 + window); /* t + w */
	design2_get_tvar_matrix(DESIGN2, 137, &x, &ind, &nz);
	assert_int_equal(nz, 1);
	assert_int_equal(ind[0], 58);
	assert_real_identical(x[0], 1.0);

	design2_get_tvar_matrix(DESIGN2, 58, &x, &ind, &nz);
	assert_int_equal(nz, 0);

	history_advance(HISTORY, double_nextup(t0 + window)); /* (t + w)+ */
	design2_get_tvar_matrix(DESIGN2, 137, &x, &ind, &nz);
	assert_int_equal(nz, 1);
	assert_int_equal(ind[0], 58);
	assert_real_identical(x[0], 0.0);

	design2_get_tvar_matrix(DESIGN2, 58, &x, &ind, &nz);
	assert_int_equal(nz, 0);
}
Пример #4
0
static void test_nrecv()
{
	double intvls[] = { INFINITY };
	size_t nintvl = 1;
	const struct var2 *v = design2_add_tvar(DESIGN2, "NRecv",
						VAR2_NRECV, intvls, nintvl);
	size_t ito;
	size_t isend, jrecv;
	const struct message *msgs;
	size_t imsg, nmsg;

	history_get_messages(HISTORY, &msgs, &nmsg);

	double xn[NSEND][NRECV];

	for (isend = 0; isend < NSEND; isend++) {
		for (jrecv = 0; jrecv < NRECV; jrecv++) {
			xn[isend][jrecv] = 0;
		}
	}

	for (imsg = 0; imsg < nmsg; imsg++) {
		const struct message *msg = &msgs[imsg];
		double t = msg->time;
		history_advance(HISTORY, t);

		jrecv = msg->from;
		for (ito = 0; ito < msg->nto; ito++) {
			isend = msg->to[ito];
			const double *x = design2_tvar(DESIGN2, v, isend, jrecv);
			double val = xn[jrecv][isend];

			if (x) {
				assert_real_identical(val, x[0]);
			} else {
				assert_real_identical(val, 0);
			}
		}

		if (imsg + 1 < nmsg && msgs[imsg + 1].time != t) {
			while (msg->time == t) {
				double wt = 1.0 / msg->nto;
				for (ito = 0; ito < msg->nto; ito++) {
					xn[msg->from][msg->to[ito]] += wt;
				}
				if (msg == msgs)
					break;
				msg--;
			}
		}
	}
}
Пример #5
0
static void test_irecv()
{
	double window = 5 * 7 * 24 * 60 * 60;
	const struct var2 *v = design2_add_tvar(DESIGN2, "IRecv",
						VAR2_IRECV, window);
	size_t ito;
	size_t isend, jrecv;
	const struct message *msgs;
	size_t imsg, nmsg;

	history_get_messages(HISTORY, &msgs, &nmsg);


	double tlast[NSEND][NRECV];

	for (isend = 0; isend < NSEND; isend++) {
		for (jrecv = 0; jrecv < NRECV; jrecv++) {
			tlast[isend][jrecv] = -INFINITY;
		}
	}

	for (imsg = 0; imsg < nmsg; imsg++) {
		const struct message *msg = &msgs[imsg];
		double t = msg->time;
		history_advance(HISTORY, t);

		jrecv = msg->from;
		for (ito = 0; ito < msg->nto; ito++) {
			isend = msg->to[ito];
			const double *x = design2_tvar(DESIGN2, v, isend, jrecv);
			double tmsg = tlast[jrecv][isend];

			if (tmsg + window >= t) {
				assert_real_identical(x[0], 1.0);
			} else if (x) {
				assert_real_identical(x[0], 0.0);
			}
		}

		if (imsg + 1 < nmsg && msgs[imsg + 1].time != t) {
			while (msg->time == t) {
				for (ito = 0; ito < msg->nto; ito++) {
					tlast[msg->from][msg->to[ito]] = msg->time;
				}
				if (msg == msgs)
					break;
				msg--;
			}
		}
	}
}
Пример #6
0
static void test_nsib()
{
	double intvls1[] = { 2 * 60 * 60, 3 * 7 * 24 * 60 * 60, 5 * 7 * 24 * 60 * 60, INFINITY };
	size_t nintvl1 = 4;
	double intvls2[] = { 4 * 60 * 60, 7 * 24 * 60 * 60, 4 * 7 * 24 * 60 * 60, 10 * 7 * 24 * 60 * 60 };
	size_t nintvl2 = 4;

	double *zero = xcalloc(nintvl1 * nintvl2, sizeof(double));
	const struct var2 *v1 = design2_add_tvar(DESIGN2, "NRecv", VAR2_NRECV,
						 intvls1, nintvl1);
	const struct var2 *v2 = design2_add_tvar(DESIGN2, "NSend", VAR2_NSEND,
						 intvls2, nintvl2);
	const struct var2 *v = design2_add_tvar(DESIGN2, "NSend2", VAR2_NSIB,
						intvls1, nintvl1, intvls2, nintvl2);
	double *x = xcalloc(nintvl1 * nintvl2, sizeof(double));
	size_t i, j, h;

	assert_true(v);

	history_advance(HISTORY, 987658800.1);

	size_t m = design2_count1(DESIGN2);
	size_t n = design2_count2(DESIGN2);
	assert(m == n);

	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			memset(x, 0, nintvl1 * nintvl2 * sizeof(double));
			for (h = 0; h < n; h++) {
				const double *x1 = design2_tvar(DESIGN2, v1, i, h);
				const double *x2 = design2_tvar(DESIGN2, v2, h, j);
				x1 = design2_tvar(DESIGN2, v1, i, h); // second call to design2_tvar may reallocate

				if (x1 && x2) {
					blas_dger(nintvl2, nintvl1, 1.0, x2, 1, x1, 1, x, nintvl2);
				}
			}
			const double *xij = design2_tvar(DESIGN2, v, i, j);

			if (xij) {
				assert_vec_approx(xij, x, nintvl1 * nintvl2);
			} else {
				assert_vec_approx(zero, x, nintvl1 * nintvl2);
			}
		}
	}

	free(x);
	free(zero);
}
Пример #7
0
static void test_probs()
{
	const struct message *msgs;
	size_t imsg, nmsg;
	size_t i, n = NSEND;
	struct catdist1 *dist;
	double *eta = xmalloc(NSEND * sizeof(double));

	history_get_messages(HISTORY, &msgs, &nmsg);
	for (imsg = 0; imsg < MIN(nmsg, 1000); imsg++) {
		history_advance(HISTORY, msgs[imsg].time);
		design_mul(1.0, DESIGN, &PARAMS->coefs, 0.0, eta);
		dist = send_model_dist(SEND_MODEL);

		for (i = 0; i < n; i++) {
			assert_real_approx(eta[i], catdist1_eta(dist, i));
		}
	}

	free(eta);
}
Пример #8
0
/* main loop, parse lines into trees and execute them
 * ----------------------------------------------------------------------- */
void sh_loop(void) {
  struct parser p;
  union node *list;
  stralloc cmd;

  /* if we're in interactive mode some 
     additional stuff is to be initialized */
  if(source->mode & SOURCE_IACTIVE)
    history_load();
  
  stralloc_init(&cmd);

  parse_init(&p, P_DEFAULT);
  
  while(!(parse_gettok(&p, P_DEFAULT) & T_EOF)) {
    p.pushback++;
    parse_lineno = source->line;

    var_setvint("LINENO", parse_lineno, V_DEFAULT);
    
    /* launch the parser to get a complete command */
    if((list = parse_list(&p)))
    {
      struct eval e;
      
      if(source->mode & SOURCE_IACTIVE)
      {
        tree_printlist(list, &cmd, NULL);
        stralloc_catc(&cmd, '\n');
        stralloc_nul(&cmd);
        history_set(cmd.s);
        cmd.s = NULL;
        history_advance();
      }

#ifdef DEBUG
/*      debug_list(list, 0);
      buffer_putnlflush(fd_err->w);*/
#endif /* DEBUG */
      eval_push(&e, E_JCTL);
      eval_tree(&e, list, E_ROOT|E_LIST);
      sh->exitcode = eval_pop(&e);
      
      stralloc_zero(&cmd);

      tree_free(list);
    } else if(!(p.tok & (T_NL | T_SEMI | T_BGND))) {
      /* we have a parse error */
      if(p.tok != T_EOF)
        parse_error(&p, 0);

      /* exit if not interactive */
      if(!(source->mode & SOURCE_IACTIVE))
        sh_exit(1);

      /* ..otherwise discard the input buffer */
      source_flush();
      p.pushback = 0;
    }
    
    if(p.tok & (T_NL|T_SEMI|T_BGND))
      p.pushback = 0;

    /* reset prompt */
    prompt_number = 0;
  }
}
Пример #9
0
void recv_boot_init(struct recv_boot *boot,
		    const struct recv_model *m,
		    const struct message *msgs,
		    size_t nmsg,
		    dsfmt_t * dsfmt)

{
	size_t nsend = recv_model_send_count(m);
	size_t j, nrecv = recv_model_count(m);
	const struct design *r = recv_model_design(m);
	const struct design2 *d = recv_model_design2(m);
	struct history *hr = design_history(r);
	struct history *hd = design2_history(d);
	size_t imsg;

	size_t max_nto = nrecv;
	size_t *to = xcalloc(max_nto, sizeof(to[0]));
	double *p = xmalloc(nrecv * sizeof(p[0]));
	size_t maxntry = 100000000;

	history_init(&boot->history, nsend, nrecv);

	for (imsg = 0; imsg < nmsg; imsg++) {
		const struct message *msg = &msgs[imsg];
		double t = msg->time;
		size_t from = msg->from;
		size_t nto = msg->nto;

		if (t < history_time(hr)) {
			history_reset(hr);
		}
		history_advance(hr, t);

		if (t < history_time(hd)) {
			history_reset(hd);
		}
		history_advance(hd, t);


		memset(p, 0, nrecv * sizeof(p[0]));
		axpy_probs(1.0, m, from, p);

		if (!sample_subset(p, nrecv, dsfmt, maxntry, to, nto)) {
			fprintf(stdout,
				"Failed to sample subset of size %zu\n",
				nto);
			fprintf(stderr,
				"Failed to sample subset of size %zu\n",
				nto);

			printf("probs:\n");
			for (j = 0; j < nrecv; j++) {
				printf("%.10e, ", p[j]);
			}
			exit(1);
		}

		if (t < history_time(&boot->history)) {
			history_reset(&boot->history);
		}
		history_advance(&boot->history, t);
		history_add(&boot->history, from, to, nto, msg->attr);
	}

	free(p);
	free(to);
}