Exemple #1
0
int main(int argc, char **argv) {
    srand(time(0));
    init_synth(argc-1, argv+1);

    double totalcsd = 0.0, totalbits = 0.0, 
	   totalopt = 0.0;

    for(int n = 0; n < NTESTS; ++n) {
	create_random_problem(1);
	if(PRINT_TARGETS) {
	    cout << "  /* ";
	    print_set("targets:", TARGETS, 0, cout); 
	    cout << " */ " << endl;
	}
	totalcsd += csdcost(TARGETS);
	totalbits += bitscost(TARGETS);
	if(USE_TABLE) totalopt += optcost(TARGETS);
    }

    if_verbose(1) cerr << endl;
    double mean_csd = totalcsd / NTESTS;
    double mean_bits = totalbits / NTESTS;
    double mean_opt = 0;
    if(USE_TABLE) mean_opt = totalopt / NTESTS;

    printf("%.2d  %6.2f %6.2f %4.2f%%", MAX_SHIFT-1,
	mean_bits, mean_csd, (mean_bits-mean_csd)/mean_bits*100);
    if(USE_TABLE)
        printf(" %6.2f %4.2f%%",
	   mean_opt, (mean_bits-mean_opt)/mean_bits*100);
    printf("\n");
    return 0;
}
Exemple #2
0
static int test_time_main (unformat_input_t * input)
{
  f64 wait, error;
  f64 t, tu[3], ave, rms;
  clib_time_t c;
  int i, n, j;

  clib_time_init (&c);
  wait = 1e-3;
  n = 1000;
  unformat (input, "%f %d", &wait, &n);
  ave = rms = 0;
  tu[0] = unix_time_now ();
  tu[1] = unix_time_now ();
  for (i = 0; i < n; i++) {
    j = 0;
    t = clib_time_now (&c);
    while (clib_time_now (&c) < t + wait)
      j++;
    t = j;
    ave += t;
    rms += t*t;
  }
  tu[2] = unix_time_now ();
  ave /= n;
  rms = sqrt (rms/n - ave*ave);

  error = ((tu[2] - tu[1]) - 2 * (tu[1] - tu[0]) - n*wait) / n;
  if_verbose   ("tested %d x %.6e sec waits, error %.6e loops %.6e +- %.6e\n",
		n, wait, error, ave, rms);

  return 0;
}
Exemple #3
0
int main (int argc, char * argv[])
{
  word i, j, k, n, seed, check_mask;
  u32 * h = 0;
  uword * objects = 0;
  uword * handles = 0;
  uword objects_used;
  uword align, fixed_size;

  n = 10;
  seed = getpid ();
  check_mask = 0;
  fixed_size = 0;

  if (argc > 1)
    {
      n = atoi (argv[1]);
      verbose = 1;
    }
  if (argc > 2)
    {
      word i = atoi (argv[2]);
      if (i)
	seed = i;
    }
  if (argc > 3)
    check_mask = atoi (argv[3]);

  align = 0;
  if (argc > 4)
    align = 1 << atoi (argv[4]);

  if_verbose   ("testing %wd iterations seed %wd\n", n, seed);

  srandom (seed);

  if (verbose) fformat (stderr, "%U\n", format_clib_mem_usage, /* verbose */ 0);

  vec_resize (objects, 1000);
  memset (objects, ~0, vec_bytes (objects));
  vec_resize (handles, vec_len (objects));

  objects_used = 0;

  if (fixed_size)
    {
      uword max_len = 1024 * 1024;
      void * memory = clib_mem_alloc (max_len * sizeof (h[0]));
      h = heap_create_from_memory (memory, max_len, sizeof (h[0]));
    }

  for (i = 0; i < n; i++)
    {
      while (1)
	{
	  j = random () % vec_len (objects);
	  if (objects[j] != ~0 || i + objects_used < n)
	    break;
	}

      if (objects[j] != ~0)
	{
	  heap_dealloc (h, handles[j]);
	  objects_used--;
	  objects[j] = ~0;
	}
      else
	{
	  u32 * data;
	  uword size;

	  size = 1 + (random () % 100);
	  objects[j] = heap_alloc_aligned (h, size, align, handles[j]);
	  objects_used++;

	  if (align)
	    ASSERT (0 == (objects[j] & (align - 1)));
	  ASSERT (objects[j] < vec_len (h));
	  ASSERT (size <= heap_len (h, handles[j]));

	  /* Set newly allocated object with test data. */
	  if (check_mask & 2)
	    {
	      data = h + objects[j];

	      for (k = 0; k < size; k++)
		data[k] = objects[j] + k;
	    }
	}

      if (check_mask & 1)
	heap_validate (h);

      if (check_mask & 4)
	{
	  /* Duplicate heap at each iteration. */
	  u32 * h1 = heap_dup (h);
	  heap_free (h);
	  h = h1;
	}

      /* Verify that all used objects have correct test data. */
      if (check_mask & 2)
	{
	  for (j = 0; j < vec_len (objects); j++)
	    if (objects[j] != ~0)
	      {
		u32 * data = h + objects[j];
		for (k = 0; k < heap_len (h, handles[j]); k++)
		  ASSERT(data[k] == objects[j] + k);
	      }
	}
    }

  if (verbose) fformat (stderr, "%U\n", format_heap, h, 1);

  {
    u32 * h1 = heap_dup (h);
    if (verbose) fformat (stderr, "%U\n", format_heap, h1, 1);
    heap_free (h1);
  }

  heap_free (h);
  if (verbose) fformat (stderr, "%U\n", format_heap, h, 1);
  ASSERT (objects_used == 0);

  vec_free (objects);
  vec_free (handles);

  if (fixed_size)
    vec_free_h (h, sizeof (heap_header_t));

  if (verbose) fformat (stderr, "%U\n", format_clib_mem_usage, /* verbose */ 0);

  return 0;
}
Exemple #4
0
int
test_mheap_main (unformat_input_t * input)
{
  int i, j, k, n_iterations;
  void *h, *h_mem;
  uword *objects = 0;
  u32 objects_used, really_verbose, n_objects, max_object_size;
  u32 check_mask, seed, trace, use_vm;
  u32 print_every = 0;
  u32 *data;
  mheap_t *mh;

  /* Validation flags. */
  check_mask = 0;
#define CHECK_VALIDITY 1
#define CHECK_DATA     2
#define CHECK_ALIGN    4
#define TEST1	       8

  n_iterations = 10;
  seed = 0;
  max_object_size = 100;
  n_objects = 1000;
  trace = 0;
  really_verbose = 0;
  use_vm = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (0 == unformat (input, "iter %d", &n_iterations)
	  && 0 == unformat (input, "count %d", &n_objects)
	  && 0 == unformat (input, "size %d", &max_object_size)
	  && 0 == unformat (input, "seed %d", &seed)
	  && 0 == unformat (input, "print %d", &print_every)
	  && 0 == unformat (input, "validdata %|",
			    &check_mask, CHECK_DATA | CHECK_VALIDITY)
	  && 0 == unformat (input, "valid %|",
			    &check_mask, CHECK_VALIDITY)
	  && 0 == unformat (input, "verbose %=", &really_verbose, 1)
	  && 0 == unformat (input, "trace %=", &trace, 1)
	  && 0 == unformat (input, "vm %=", &use_vm, 1)
	  && 0 == unformat (input, "align %|", &check_mask, CHECK_ALIGN)
	  && 0 == unformat (input, "test1 %|", &check_mask, TEST1))
	{
	  clib_warning ("unknown input `%U'", format_unformat_error, input);
	  return 1;
	}
    }

  /* Zero seed means use default. */
  if (!seed)
    seed = random_default_seed ();

  if (check_mask & TEST1)
    {
      return test1 ();
    }

  if_verbose
    ("testing %d iterations, %d %saligned objects, max. size %d, seed %d",
     n_iterations, n_objects, (check_mask & CHECK_ALIGN) ? "randomly " : "un",
     max_object_size, seed);

  vec_resize (objects, n_objects);
  if (vec_bytes (objects) > 0)	/* stupid warning be gone */
    clib_memset (objects, ~0, vec_bytes (objects));
  objects_used = 0;

  /* Allocate initial heap. */
  {
    uword size =
      max_pow2 (2 * n_objects * max_object_size * sizeof (data[0]));

    h_mem = clib_mem_alloc (size);
    if (!h_mem)
      return 0;

    h = mheap_alloc (h_mem, size);
  }

  if (trace)
    mheap_trace (h, trace);

  mh = mheap_header (h);

  if (use_vm)
    mh->flags &= ~MHEAP_FLAG_DISABLE_VM;
  else
    mh->flags |= MHEAP_FLAG_DISABLE_VM;

  if (check_mask & CHECK_VALIDITY)
    mh->flags |= MHEAP_FLAG_VALIDATE;

  for (i = 0; i < n_iterations; i++)
    {
      while (1)
	{
	  j = random_u32 (&seed) % vec_len (objects);
	  if (objects[j] != ~0 || i + objects_used < n_iterations)
	    break;
	}

      if (objects[j] != ~0)
	{
	  mheap_put (h, objects[j]);
	  objects_used--;
	  objects[j] = ~0;
	}
      else
	{
	  uword size, align, align_offset;

	  size = (random_u32 (&seed) % max_object_size) * sizeof (data[0]);
	  align = align_offset = 0;
	  if (check_mask & CHECK_ALIGN)
	    {
	      align = 1 << (random_u32 (&seed) % 10);
	      align_offset = round_pow2 (random_u32 (&seed) & (align - 1),
					 sizeof (u32));
	    }

	  h = mheap_get_aligned (h, size, align, align_offset, &objects[j]);

	  if (align > 0)
	    ASSERT (0 == ((objects[j] + align_offset) & (align - 1)));

	  ASSERT (objects[j] != ~0);
	  objects_used++;

	  /* Set newly allocated object with test data. */
	  if (check_mask & CHECK_DATA)
	    {
	      uword len;

	      data = (void *) h + objects[j];
	      len = mheap_len (h, data);

	      ASSERT (size <= mheap_data_bytes (h, objects[j]));

	      data[0] = len;
	      for (k = 1; k < len; k++)
		data[k] = objects[j] + k;
	    }
	}

      /* Verify that all used objects have correct test data. */
      if (check_mask & 2)
	{
	  for (j = 0; j < vec_len (objects); j++)
	    if (objects[j] != ~0)
	      {
		u32 *data = h + objects[j];
		uword len = data[0];
		for (k = 1; k < len; k++)
		  ASSERT (data[k] == objects[j] + k);
	      }
	}
      if (print_every != 0 && i > 0 && (i % print_every) == 0)
	fformat (stderr, "iteration %d: %U\n", i, format_mheap, h,
		 really_verbose);
    }

  if (verbose)
    fformat (stderr, "%U\n", format_mheap, h, really_verbose);
  mheap_free (h);
  clib_mem_free (h_mem);
  vec_free (objects);

  return 0;
}
void   acm_solve(oplist_t &l, const vector<coeff_t> &coeffs,
                 const vector<reg_t> &dests, reg_t src, reg_t (*tmpreg)()) {
    ASSERT(dests.size() == coeffs.size());

    cfset_t problem;
    regmap_t * regs = new regmap_t;

    regs->clear();
    (*regs)[1] = src;

    for(int i = coeffs.size()-1; i >= 0; --i) {
        if(coeffs[i]!=1)
          (*regs)[coeffs[i]] = dests[i];
    }

    /* compute registers for all fundamentals ('reduced' constants) */
    for(size_t i = 0; i < coeffs.size(); ++i) {
        coeff_t full    = coeffs[i];
        coeff_t reduced = fundamental(cfabs(full));
        if(reduced!=1)
            ADD(problem, reduced);
        if(!CONTAINS(*regs, reduced))
            (*regs)[reduced] = tmpreg();
    }

    if_verbose(1) cerr<<"synthesizing "<<problem.size()<<" unique fundamentals\n";

    create_problem(problem);
    GEN_CODE = true;
    solve(&l, regs, tmpreg);

    for(size_t i = 0; i < coeffs.size(); ++i) {
        coeff_t full    = coeffs[i];
        reg_t   dest    = dests[i];
        coeff_t reduced = fundamental(cfabs(full));
        if(full == 1)
            l.push_back(op::shl(dest, src, 0, full)); /* move dest <- src */

        else if(full == -1)
            l.push_back(op::neg(dest, src, full)); /* move dest <- -1*src */

        /* last op */
        else if(dest == (*regs)[full]) {
            if(full != reduced) {
                int t = (*regs)[reduced];

                /* see if we already computed the negative of it */
                int found_neg = -1;
                for(size_t j=0; j<i; ++j) {
                    if(coeffs[j] == -full) {
                        found_neg = j;
                        break;
                    }
                }
                /* if not */
                if(found_neg == -1) {
                    /* this is the first instance */
                    int shr = compute_shr(cfabs(full));
                    if(shr!=0) {
                        reg_t tmp = dest;
                        if(full < 0) {
                            tmp = tmpreg();
                            for(size_t j = i+1; j < coeffs.size(); ++j) {
                                if(coeffs[j] == -full) {
                                    tmp = dests[j];
                                    break;
                                }
                            }
                        }
                        l.push_back(op::shl(tmp, (*regs)[reduced], shr, cfabs(full)));
                        t = tmp;
                    }
                    /* negate if necessary */
                    if(full<0) l.push_back(op::neg(dest,t, full));
                }
                else /* negative was already computed, just negate */
                    if(full < 0) l.push_back(op::neg(dest,dests[found_neg],full));
            }
        }
        else /* we already computed it */
            l.push_back(op::shl(dest, (*regs)[full], 0, full));
    }
    delete regs;
}
Exemple #6
0
int test_socket_main (unformat_input_t * input)
{
  clib_socket_t _s = {0}, * s = &_s;
  char * config;
  clib_error_t * error;

  s->config = "localhost:22";
  s->flags = SOCKET_IS_CLIENT;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "server %s %=", &config,
		    &s->flags, SOCKET_IS_SERVER))
	;
      else if (unformat (input, "client %s %=", &config,
			 &s->flags, SOCKET_IS_CLIENT))
	;
      else
	{
	  error = clib_error_create ("unknown input `%U'\n",
				     format_unformat_error, input);
	  goto done;
	}
    }

  error = clib_socket_init (s);
  if (error)
    goto done;

  if (0)
    {
      struct { int a, b; } * msg;
      msg = clib_socket_tx_add (s, sizeof (msg[0]));
      msg->a = 99;
      msg->b = 100;
    }
  else
    clib_socket_tx_add_formatted (s, "hello there mr server %d\n", 99);

  error = clib_socket_tx (s);
  if (error)
    goto done;

  while (1)
    {
      error = clib_socket_rx (s, 100);
      if (error)
	break;

      if (clib_socket_rx_end_of_file (s))
	break;

      if_verbose   ("%v", s->rx_buffer);
      _vec_len (s->rx_buffer) = 0;
    }

  error = clib_socket_close (s);

 done:
  if (error)
    clib_error_report (error);
  return 0;
}