Ejemplo n.º 1
0
int main(int argc, char **argv)
{
  uint32_t a, b, c;
  uint8_t *r1, *r2;
  uint16_t *r16;
  uint32_t *r32;
  int w, i;
  gf_t gf;

  if (argc != 2) usage(NULL);
  w = atoi(argv[1]);
  if (w <= 0 || w > 32) usage("Bad w");

  /* Get two random numbers in a and b */

  MOA_Seed(time(0));
  a = MOA_Random_W(w, 0);
  b = MOA_Random_W(w, 0);

  /* Create the proper instance of the gf_t object using defaults: */

  gf_init_easy(&gf, w);

  /* And multiply a and b using the galois field: */

  c = gf.multiply.w32(&gf, a, b);
  printf("%u * %u = %u\n", a, b, c);

  /* Divide the product by a and b */

  printf("%u / %u = %u\n", c, a, gf.divide.w32(&gf, c, a));
  printf("%u / %u = %u\n", c, b, gf.divide.w32(&gf, c, b));

  /* If w is 4, 8, 16 or 32, do a very small region operation */

  if (w == 4 || w == 8 || w == 16 || w == 32) {
    r1 = (uint8_t *) malloc(16);
    r2 = (uint8_t *) malloc(16);

    if (w == 4 || w == 8) {
      r1[0] = b;
      for (i = 1; i < 16; i++) r1[i] = MOA_Random_W(8, 1);
    } else if (w == 16) {
      r16 = (uint16_t *) r1;
      r16[0] = b;
      for (i = 1; i < 8; i++) r16[i] = MOA_Random_W(16, 1);
    } else {
      r32 = (uint32_t *) r1;
      r32[0] = b;
      for (i = 1; i < 4; i++) r32[i] = MOA_Random_W(32, 1);
    }

    gf.multiply_region.w32(&gf, r1, r2, a, 16, 0);
  
    printf("\nmultiply_region by 0x%x (%u)\n\n", a, a);
    printf("R1 (the source):  ");
    if (w == 4) {
      for (i = 0; i < 16; i++) printf(" %x %x", r1[i] >> 4, r1[i] & 0xf);
    } else if (w == 8) {
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
  uint16_t *a, *b;
  int i, j;
  gf_t gf;

  if (gf_init_hard(&gf, 16, GF_MULT_SPLIT_TABLE, GF_REGION_ALTMAP, GF_DIVIDE_DEFAULT, 
                   0, 16, 4, NULL, NULL) == 0) {
    fprintf(stderr, "gf_init_hard failed\n");
    exit(1);
  }

  a = (uint16_t *) malloc(200);
  b = (uint16_t *) malloc(200);

  a += 6;
  b += 6;

  MOA_Seed(0);

  for (i = 0; i < 30; i++) a[i] = MOA_Random_W(16, 1);

  gf.multiply_region.w32(&gf, a, b, 0x1234, 30*2, 0);

  printf("a: 0x%lx    b: 0x%lx\n", (unsigned long) a, (unsigned long) b);

  for (i = 0; i < 30; i += 10) {
    printf("\n");
    printf("  ");
    for (j = 0; j < 10; j++) printf(" %4d", i+j);
    printf("\n");

    printf("a:");
    for (j = 0; j < 10; j++) printf(" %04x", a[i+j]);
    printf("\n");

    printf("b:");
    for (j = 0; j < 10; j++) printf(" %04x", b[i+j]);
    printf("\n");
    printf("\n");
  }

  for (i = 0; i < 15; i ++) {
    printf("Word %2d: 0x%04x * 0x1234 = 0x%04x    ", i,
           gf.extract_word.w32(&gf, a, 30*2, i),
           gf.extract_word.w32(&gf, b, 30*2, i));
    printf("Word %2d: 0x%04x * 0x1234 = 0x%04x\n", i+15,
           gf.extract_word.w32(&gf, a, 30*2, i+15),
           gf.extract_word.w32(&gf, b, 30*2, i+15));
  }
  return 0;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
  uint64_t a, b, c;
  uint64_t *r1, *r2;
  int i;
  gf_t gf;

  if (argc != 1) usage(NULL);

  /* Get two random numbers in a and b */

  MOA_Seed(time(0));
  a = MOA_Random_64();
  b = MOA_Random_64();

  /* Create the proper instance of the gf_t object using defaults: */

  gf_init_easy(&gf, 64);

  /* And multiply a and b using the galois field: */

  c = gf.multiply.w64(&gf, a, b);
  printf("%llx * %llx = %llx\n", (long long unsigned int) a, (long long unsigned int) b, (long long unsigned int) c);

  /* Divide the product by a and b */

  printf("%llx / %llx = %llx\n", (long long unsigned int) c, (long long unsigned int) a, (long long unsigned int) gf.divide.w64(&gf, c, a));
  printf("%llx / %llx = %llx\n", (long long unsigned int) c, (long long unsigned int) b, (long long unsigned int) gf.divide.w64(&gf, c, b));

  r1 = (uint64_t *) malloc(32);
  r2 = (uint64_t *) malloc(32);

  r1[0] = b;

  for (i = 1; i < 4; i++) r1[i] = MOA_Random_64();

  gf.multiply_region.w64(&gf, r1, r2, a, 32, 0);

  printf("\nmultiply_region by %llx\n\n", (long long unsigned int) a);
  printf("R1 (the source):  ");
  for (i = 0; i < 4; i++) printf(" %016llx", (long long unsigned int) r1[i]);

  printf("\nR2 (the product): ");
  for (i = 0; i < 4; i++) printf(" %016llx", (long long unsigned int) r2[i]);
  printf("\n");

  exit(0);
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
  unsigned char *x, *y;
  unsigned short *xs, *ys;
  unsigned int *xi, *yi;
  uint32_t seed;
  int *a32, *copy;
  int i;
  int w;
  
  if (argc != 3) usage(NULL);
  if (sscanf(argv[1], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
  if (sscanf(argv[2], "%d", &seed) == 0) usage("Bad seed");

  printf("<HTML><TITLE>reed_sol_04 %d %d</title>\n", w, seed);
  printf("<h3>reed_sol_04 %d %d</h3>\n", w, seed);
  printf("<pre>\n");

  MOA_Seed(seed);
  a32 = talloc(int, 4);
  copy = talloc(int, 4);
  y = (unsigned char *) a32;
  for (i = 0; i < 4*sizeof(int); i++) y[i] = MOA_Random_W(8, 1);
  memcpy(copy, a32, sizeof(int)*4);

  if (w == 8) {
    x = (unsigned char *) copy;
    y = (unsigned char *) a32;
    reed_sol_galois_w08_region_multby_2((char *) a32, sizeof(int)*4);
    for (i = 0; i < 4*sizeof(int)/sizeof(char); i++) {
       printf("Char %2d: %3u *2 = %3u\n", i, x[i], y[i]);
    }
  } else if (w == 16) {
    xs = (unsigned short *) copy;
    ys = (unsigned short *) a32;
    reed_sol_galois_w16_region_multby_2((char *) a32, sizeof(int)*4);
    for (i = 0; i < 4*sizeof(int)/sizeof(short); i++) {
       printf("Short %2d: %5u *2 = %5u\n", i, xs[i], ys[i]);
    }
  } else if (w == 32) {
    xi = (unsigned int *) copy;
    yi = (unsigned int *) a32;
    reed_sol_galois_w16_region_multby_2((char *) a32, sizeof(int)*4);
    for (i = 0; i < 4*sizeof(int)/sizeof(int); i++) {
       printf("Int %2d: %10u *2 = %10u\n", i, xi[i], yi[i]);
    }
  } 
}
Ejemplo n.º 5
0
int main(int argc, char **argv)
{
  signal(SIGSEGV, SigHandler);

  int w, i, verbose, single, region, tested, top;
  int s_start, d_start, bytes, xor, alignment_test;
  gf_t   gf, gf_def;
  time_t t0;
  gf_internal_t *h;
  gf_general_t *a, *b, *c, *d, *ai, *bi;
  uint8_t a8, b8, c8, *mult4, *div4, *mult8, *div8;
  uint16_t a16, b16, c16, d16, *log16, *alog16;
  char as[50], bs[50], cs[50], ds[50], ais[50], bis[50];
  uint32_t mask;
  char *ra, *rb, *rc, *rd, *target;
  int align;


  if (argc < 4) usage(NULL);

  if (sscanf(argv[1], "%d", &w) == 0){
    usage("Bad w\n");
  }

  if (sscanf(argv[3], "%ld", &t0) == 0) usage("Bad seed\n");
  if (t0 == -1) t0 = time(0);
  MOA_Seed(t0);

  if (w > 32 && w != 64 && w != 128) usage("Bad w");
  
  if (create_gf_from_argv(&gf, w, argc, argv, 4) == 0) usage(BM);
  printf("Size (bytes): %d\n", gf_size(&gf));

  for (i = 0; i < strlen(argv[2]); i++) {
    if (strchr("ASRV", argv[2][i]) == NULL) usage("Bad test\n");
  }

  h = (gf_internal_t *) gf.scratch;
  a = (gf_general_t *) malloc(sizeof(gf_general_t));
  b = (gf_general_t *) malloc(sizeof(gf_general_t));
  c = (gf_general_t *) malloc(sizeof(gf_general_t));
  d = (gf_general_t *) malloc(sizeof(gf_general_t));
  ai = (gf_general_t *) malloc(sizeof(gf_general_t));
  bi = (gf_general_t *) malloc(sizeof(gf_general_t));

  //15 bytes extra to make sure it's 16byte aligned
  ra = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  rb = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  rc = (char *) malloc(sizeof(char)*REGION_SIZE+15);
  rd = (char *) malloc(sizeof(char)*REGION_SIZE+15);

  //this still assumes 8 byte aligned pointer from malloc
  //(which is usual on 32-bit machines)
  ra += (uint64_t)ra & 0xf;
  rb += (uint64_t)rb & 0xf;
  rc += (uint64_t)rc & 0xf;
  rd += (uint64_t)rd & 0xf;

  if (w <= 32) {
    mask = 0;
    for (i = 0; i < w; i++) mask |= (1 << i);
  }

  verbose = (strchr(argv[2], 'V') != NULL);
  single = (strchr(argv[2], 'S') != NULL || strchr(argv[2], 'A') != NULL);
  region = (strchr(argv[2], 'R') != NULL || strchr(argv[2], 'A') != NULL);

  if (!gf_init_hard(&gf_def, w, GF_MULT_DEFAULT, GF_REGION_DEFAULT, GF_DIVIDE_DEFAULT,
      (h->mult_type != GF_MULT_COMPOSITE) ? h->prim_poly : 0, 0, 0, NULL, NULL))
    problem("No default for this value of w");
  if (w == 4) {
    mult4 = gf_w4_get_mult_table(&gf);
    div4 = gf_w4_get_div_table(&gf);
  }

  if (w == 8) {
    mult8 = gf_w8_get_mult_table(&gf);
    div8 = gf_w8_get_div_table(&gf);
  }

  if (w == 16) {
    log16 = gf_w16_get_log_table(&gf);
    alog16 = gf_w16_get_mult_alog_table(&gf);
  }

  if (verbose) printf("Seed: %ld\n", t0);

  if (single) {
    
    if (gf.multiply.w32 == NULL) problem("No multiplication operation defined.");
    if (verbose) { printf("Testing single multiplications/divisions.\n"); fflush(stdout); }
    if (w <= 10) {
      top = (1 << w)*(1 << w);
    } else {
      top = 1024*1024;
    }
    for (i = 0; i < top; i++) {
      if (w <= 10) {
        a->w32 = i % (1 << w);
        b->w32 = (i >> w);

      //Allen: the following conditions were being run 10 times each. That didn't seem like nearly enough to
      //me for these special cases, so I converted to doing this mod stuff to easily make the number of times
      //run both larger and proportional to the total size of the run.
      } else {
        switch (i % 32)
        {
          case 0: 
            gf_general_set_zero(a, w);
            gf_general_set_random(b, w, 1);
            break;
          case 1:
            gf_general_set_random(a, w, 1);
            gf_general_set_zero(b, w);
            break;
          case 2:
            gf_general_set_one(a, w);
            gf_general_set_random(b, w, 1);
            break;
          case 3:
            gf_general_set_random(a, w, 1);
            gf_general_set_one(b, w);
            break;
          default:
            gf_general_set_random(a, w, 1);
            gf_general_set_random(b, w, 1);
        }
      }

      //Allen: the following special cases for w=64 are based on the code below for w=128.
      //These w=64 cases are based on Dr. Plank's suggestion because some of the methods for w=64
      //involve splitting it in two. I think they're less likely to give errors than the 128-bit case
      //though, because the 128 bit case is always split in two.
      //As with w=128, I'm arbitrarily deciding to do this sort of thing with a quarter of the cases
      if (w == 64) {
        switch (i % 32)
        {
          case 0: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; break;
          case 1: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; break;
          case 2: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
          case 3: if (!gf_general_is_one(a, w)) a->w64 &= RMASK; if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
          case 4: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
          case 5: if (!gf_general_is_one(a, w)) a->w64 &= LMASK; if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
          case 6: if (!gf_general_is_one(b, w)) b->w64 &= RMASK; break;
          case 7: if (!gf_general_is_one(b, w)) b->w64 &= LMASK; break;
        }
      }

      //Allen: for w=128, we have important special cases where one half or the other of the number is all
      //zeros. The probability of hitting such a number randomly is 1^-64, so if we don't force these cases
      //we'll probably never hit them. This could be implemented more efficiently by changing the set-random
      //function for w=128, but I think this is easier to follow.
      //I'm arbitrarily deciding to do this sort of thing with a quarter of the cases
      if (w == 128) {
        switch (i % 32)
        {
          case 0: if (!gf_general_is_one(a, w)) a->w128[0] = 0; break;
          case 1: if (!gf_general_is_one(a, w)) a->w128[1] = 0; break;
          case 2: if (!gf_general_is_one(a, w)) a->w128[0] = 0; if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
          case 3: if (!gf_general_is_one(a, w)) a->w128[0] = 0; if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
          case 4: if (!gf_general_is_one(a, w)) a->w128[1] = 0; if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
          case 5: if (!gf_general_is_one(a, w)) a->w128[1] = 0; if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
          case 6: if (!gf_general_is_one(b, w)) b->w128[0] = 0; break;
          case 7: if (!gf_general_is_one(b, w)) b->w128[1] = 0; break;
        }
      }

      tested = 0;
      gf_general_multiply(&gf, a, b, c);
      
      /* If w is 4, 8 or 16, then there are inline multiplication/division methods.  
         Test them here. */

      if (w == 4 && mult4 != NULL) {
        a8 = a->w32;
        b8 = b->w32;
        c8 = GF_W4_INLINE_MULTDIV(mult4, a8, b8);
        if (c8 != c->w32) {
          printf("Error in inline multiplication. %d * %d.  Inline = %d.  Default = %d.\n",
             a8, b8, c8, c->w32);
          exit(1);
        }
      }

      if (w == 8 && mult8 != NULL) {
        a8 = a->w32;
        b8 = b->w32;
        c8 = GF_W8_INLINE_MULTDIV(mult8, a8, b8);
        if (c8 != c->w32) {
          printf("Error in inline multiplication. %d * %d.  Inline = %d.  Default = %d.\n",
             a8, b8, c8, c->w32);
          exit(1);
        }
      }

      if (w == 16 && log16 != NULL) {
        a16 = a->w32;
        b16 = b->w32;
        c16 = GF_W16_INLINE_MULT(log16, alog16, a16, b16);
        if (c16 != c->w32) {
          printf("Error in inline multiplication. %d * %d.  Inline = %d.  Default = %d.\n",
             a16, b16, c16, c->w32);
          printf("%d %d\n", log16[a16], log16[b16]);
          top = log16[a16] + log16[b16];
          printf("%d %d\n", top, alog16[top]);
          exit(1);
        }
      }

      /* If this is not composite, then first test against the default: */

      if (h->mult_type != GF_MULT_COMPOSITE) {
        tested = 1;
        gf_general_multiply(&gf_def, a, b, d);

        if (!gf_general_are_equal(c, d, w)) {
          gf_general_val_to_s(a, w, as, 1);
          gf_general_val_to_s(b, w, bs, 1);
          gf_general_val_to_s(c, w, cs, 1);
          gf_general_val_to_s(d, w, ds, 1);
          printf("Error in single multiplication (all numbers in hex):\n\n");
          printf("  gf.multiply(gf, %s, %s) = %s\n", as, bs, cs);
          printf("  The default gf multiplier returned %s\n", ds);
          exit(1);
        }
      }

      /* Now, we also need to double-check by other means, in case the default is wanky, 
         and when we're performing composite operations. Start with 0 and 1, where we know
         what the result should be. */

      if (gf_general_is_zero(a, w) || gf_general_is_zero(b, w) || 
          gf_general_is_one(a, w)  || gf_general_is_one(b, w)) {
        tested = 1;
        if (((gf_general_is_zero(a, w) || gf_general_is_zero(b, w)) && !gf_general_is_zero(c, w)) ||
            (gf_general_is_one(a, w) && !gf_general_are_equal(b, c, w)) ||
            (gf_general_is_one(b, w) && !gf_general_are_equal(a, c, w))) {
          gf_general_val_to_s(a, w, as, 1);
          gf_general_val_to_s(b, w, bs, 1);
          gf_general_val_to_s(c, w, cs, 1);
          printf("Error in single multiplication (all numbers in hex):\n\n");
          printf("  gf.multiply(gf, %s, %s) = %s, which is clearly wrong.\n", as, bs, cs);
;
          exit(1);
        }
      }

      /* Dumb check to make sure that it's not returning numbers that are too big: */

      if (w < 32 && (c->w32 & mask) != c->w32) {
        gf_general_val_to_s(a, w, as, 1);
        gf_general_val_to_s(b, w, bs, 1);
        gf_general_val_to_s(c, w, cs, 1);
        printf("Error in single multiplication (all numbers in hex):\n\n");
        printf("  gf.multiply.w32(gf, %s, %s) = %s, which is too big.\n", as, bs, cs);
        exit(1);
      }

      /* Finally, let's check to see that multiplication and division work together */

      if (!gf_general_is_zero(a, w)) {
        gf_general_divide(&gf, c, a, d);
        if (!gf_general_are_equal(b, d, w)) {
          gf_general_val_to_s(a, w, as, 1);
          gf_general_val_to_s(b, w, bs, 1);
          gf_general_val_to_s(c, w, cs, 1);
          gf_general_val_to_s(d, w, ds, 1);
          printf("Error in single multiplication/division (all numbers in hex):\n\n");
          printf("  gf.multiply(gf, %s, %s) = %s, but gf.divide(gf, %s, %s) = %s\n", as, bs, cs, cs, as, ds);
          exit(1);
        }
      }

    }
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
  int k, w, i, m, iterations, bufsize;
  int *matrix;
  char **data, **coding, **old_values;
  int *erasures, *erased;
  uint32_t seed;
  double t = 0, total_time = 0;
  gf_t *gf = NULL;
  
  if (argc < 8) usage(NULL);  
  if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
  if (sscanf(argv[4], "%d", &seed) == 0) usage("Bad seed");
  if (sscanf(argv[5], "%d", &iterations) == 0) usage("Bad iterations");
  if (sscanf(argv[6], "%d", &bufsize) == 0) usage("Bad bufsize");
  if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");

  MOA_Seed(seed);

  gf = get_gf(w, argc, argv, 7); 

  if (gf == NULL) {
    usage("Invalid arguments given for GF!\n");
  }

  galois_change_technique(gf, w); 

  matrix = reed_sol_vandermonde_coding_matrix(k, m, w);

  printf("<HTML><TITLE>reed_sol_time_gf");
  for (i = 1; i < argc; i++) printf(" %s", argv[i]);
  printf("</TITLE>\n");
  printf("<h3>reed_sol_time_gf");
  for (i = 1; i < argc; i++) printf(" %s", argv[i]);
  printf("</h3>\n");
  printf("<pre>\n");

  printf("Last m rows of the generator matrix (G^T):\n\n");
  jerasure_print_matrix(matrix, m, k, w,NULL);
  printf("\n");

  data = talloc(char *, k);
  for (i = 0; i < k; i++) {
    data[i] = talloc(char, bufsize);
    MOA_Fill_Random_Region(data[i], bufsize);
  }

  coding = talloc(char *, m);
  old_values = talloc(char *, m);
  for (i = 0; i < m; i++) {
    coding[i] = talloc(char, bufsize);
    old_values[i] = talloc(char, bufsize);
  }

  for (i = 0; i < iterations; i++) {
    t = timing_now();
    jerasure_matrix_encode(k, m, w, matrix, data, coding, bufsize);
    total_time += timing_now() - t;
  }

  printf("Encode throughput for %d iterations: %.2f MB/s (%.2f sec)\n", iterations, (double)(k*iterations*bufsize/1024/1024) / total_time, total_time);
  
  erasures = talloc(int, (m+1));
  erased = talloc(int, (k+m));
  for (i = 0; i < m+k; i++) erased[i] = 0;
  for (i = 0; i < m; ) {
    erasures[i] = ((unsigned int)MOA_Random_W(w, 1))%(k+m);
    if (erased[erasures[i]] == 0) {
      erased[erasures[i]] = 1;
      memcpy(old_values[i], (erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], bufsize);
      bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], bufsize);
      i++;
    }
  }
  erasures[i] = -1;

  for (i = 0; i < iterations; i++) {
    t = timing_now();
    jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, bufsize);
    total_time += timing_now() - t;
  }
  
  printf("Decode throughput for %d iterations: %.2f MB/s (%.2f sec)\n", iterations, (double)(k*iterations*bufsize/1024/1024) / total_time, total_time);

  for (i = 0; i < m; i++) {
    if (erasures[i] < k) {
      if (memcmp(data[erasures[i]], old_values[i], bufsize)) {
        fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
        exit(1);
      }
    } else {
      if (memcmp(coding[erasures[i]-k], old_values[i], bufsize)) {
        fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
        exit(1);
      }
    }
  }
  
  return 0;
}
Ejemplo n.º 7
0
int main(int argc, char **argv)
{
  int w, j, i, size, iterations;
  gf_t      gf;
  double timer, elapsed, dnum, num;
  uint8_t *ra, *rb, *mult4, *mult8;
  uint16_t *ra16, *rb16, *log16, *alog16;
  time_t t0;
  
  if (argc != 5) usage(NULL);
  if (sscanf(argv[1], "%d", &w) == 0) usage("Bad w\n");
  if (w != 4 && w != 8 && w != 16) usage("Bad w\n");
  if (sscanf(argv[2], "%ld", &t0) == 0) usage("Bad seed\n");
  if (sscanf(argv[3], "%d", &size) == 0) usage("Bad #elts\n");
  if (sscanf(argv[4], "%d", &iterations) == 0) usage("Bad iterations\n");
  if (t0 == -1) t0 = time(0);
  MOA_Seed(t0);

  num = size;

  gf_init_easy(&gf, w);
  
  printf("Seed: %ld\n", t0);

  if (w == 4 || w == 8) {
    ra = (uint8_t *) malloc(size);
    rb = (uint8_t *) malloc(size);

    if (ra == NULL || rb == NULL) { perror("malloc"); exit(1); }
  } else if (w == 16) {
    ra16 = (uint16_t *) malloc(size*2);
    rb16 = (uint16_t *) malloc(size*2);

    if (ra16 == NULL || rb16 == NULL) { perror("malloc"); exit(1); }
  }

  if (w == 4) {
    mult4 = gf_w4_get_mult_table(&gf);
    if (mult4 == NULL) {
      printf("Couldn't get inline multiplication table.\n");
      exit(1);
    }
    elapsed = 0;
    dnum = 0;
    for (i = 0; i < iterations; i++) {
      for (j = 0; j < size; j++) {
        ra[j] = MOA_Random_W(w, 1);
        rb[j] = MOA_Random_W(w, 1);
      }
      timer_start(&timer);
      for (j = 0; j < size; j++) {
        ra[j] = GF_W4_INLINE_MULTDIV(mult4, ra[j], rb[j]);
      }
      dnum += num;
      elapsed += timer_split(&timer);
    }
    printf("Inline mult:   %10.6lf s   Mops: %10.3lf    %10.3lf Mega-ops/s\n",
           elapsed, dnum/1024.0/1024.0, dnum/1024.0/1024.0/elapsed);

  }
  if (w == 8) {
    mult8 = gf_w8_get_mult_table(&gf);
    if (mult8 == NULL) {
      printf("Couldn't get inline multiplication table.\n");
      exit(1);
    }
    elapsed = 0;
    dnum = 0;
    for (i = 0; i < iterations; i++) {
      for (j = 0; j < size; j++) {
        ra[j] = MOA_Random_W(w, 1);
        rb[j] = MOA_Random_W(w, 1);
      }
      timer_start(&timer);
      for (j = 0; j < size; j++) {
        ra[j] = GF_W8_INLINE_MULTDIV(mult8, ra[j], rb[j]);
      }
      dnum += num;
      elapsed += timer_split(&timer);
    }
    printf("Inline mult:   %10.6lf s   Mops: %10.3lf    %10.3lf Mega-ops/s\n",
           elapsed, dnum/1024.0/1024.0, dnum/1024.0/1024.0/elapsed);
  }
  if (w == 16) {
    log16 = gf_w16_get_log_table(&gf);
    alog16 = gf_w16_get_mult_alog_table(&gf);
    if (log16 == NULL) {
      printf("Couldn't get inline multiplication table.\n");
      exit(1);
    }
    elapsed = 0;
    dnum = 0;
    for (i = 0; i < iterations; i++) {
      for (j = 0; j < size; j++) {
        ra16[j] = MOA_Random_W(w, 1);
        rb16[j] = MOA_Random_W(w, 1);
      }
      timer_start(&timer);
      for (j = 0; j < size; j++) {
        ra16[j] = GF_W16_INLINE_MULT(log16, alog16, ra16[j], rb16[j]);
      }
      dnum += num;
      elapsed += timer_split(&timer);
    }
    printf("Inline mult:   %10.6lf s   Mops: %10.3lf    %10.3lf Mega-ops/s\n",
           elapsed, dnum/1024.0/1024.0, dnum/1024.0/1024.0/elapsed);
  }
}
Ejemplo n.º 8
0
int main(int argc, char **argv)
{
  long l;
  int k, w, i, j, m;
  int *matrix;
  char **data, **coding, **dcopy, **ccopy;
  unsigned char uc;
  int *erasures, *erased;
  int *decoding_matrix, *dm_ids;
  uint32_t seed;
  
  if (argc != 5) usage(NULL);
  if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
  if (sscanf(argv[4], "%u", &seed) == 0) usage("Bad seed");
  if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");

  matrix = reed_sol_vandermonde_coding_matrix(k, m, w);

  printf("<HTML><TITLE>reed_sol_01 %d %d %d %d</title>\n", k, m, w, seed);
  printf("<h3>reed_sol_01 %d %d %d %d</h3>\n", k, m, w, seed);
  printf("<pre>\n");
  printf("Last m rows of the generator Matrix (G^T):\n\n");
  jerasure_print_matrix(matrix, m, k, w);
  printf("\n");

  MOA_Seed(seed);
  data = talloc(char *, k);
  dcopy = talloc(char *, k);
  for (i = 0; i < k; i++) {
    data[i] = talloc(char, sizeof(long));
    dcopy[i] = talloc(char, sizeof(long));
    for (j = 0; j < sizeof(long); j++) {
      uc = MOA_Random_W(8, 1);
      data[i][j] = (char) uc;
    }
    memcpy(dcopy[i], data[i], sizeof(long));
  }

  coding = talloc(char *, m);
  ccopy = talloc(char *, m);
  for (i = 0; i < m; i++) {
    coding[i] = talloc(char, sizeof(long));
    ccopy[i] = talloc(char, sizeof(long));
  }

  jerasure_matrix_encode(k, m, w, matrix, data, coding, sizeof(long));

  for (i = 0; i < m; i++) {
    memcpy(ccopy[i], coding[i], sizeof(long));
  }
  
  printf("Encoding Complete:\n\n");
  print_data_and_coding(k, m, w, sizeof(long), data, coding);

  erasures = talloc(int, (m+1));
  erased = talloc(int, (k+m));
  for (i = 0; i < m+k; i++) erased[i] = 0;
  l = 0;
  for (i = 0; i < m; ) {
    erasures[i] = MOA_Random_W(31, 0)%(k+m);
    if (erased[erasures[i]] == 0) {
      erased[erasures[i]] = 1;
      memcpy((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], &l, sizeof(long));
      i++;
    }
  }
  erasures[i] = -1;

  printf("Erased %d random devices:\n\n", m);
  print_data_and_coding(k, m, w, sizeof(long), data, coding);
  
  i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, sizeof(long));

  printf("State of the system after decoding:\n\n");
  print_data_and_coding(k, m, w, sizeof(long), data, coding);
  
  for (i = 0; i < k; i++) if (memcmp(data[i], dcopy[i], sizeof(long)) != 0) {
    printf("ERROR: D%x after decoding does not match its state before decoding!<br>\n", i);
  }
  for (i = 0; i < m; i++) if (memcmp(coding[i], ccopy[i], sizeof(long)) != 0) {
    printf("ERROR: C%x after decoding does not match its state before decoding!<br>\n", i);
  }

  return 0;
}
Ejemplo n.º 9
0
int main (int argc, char **argv) {
	FILE *fp, *fp2;				// file pointers
	char *block;				// padding file
	int size, newsize;			// size of file and temp size 
	struct stat status;			// finding file size

	
	enum Coding_Technique tech;		// coding technique (parameter)
	int k, m, w, packetsize;		// parameters
	int buffersize;					// paramter
	int i;						// loop control variables
	int blocksize;					// size of k+m files
	int total;
	int extra;
	
	/* Jerasure Arguments */
	char **data;				
	char **coding;
	int *matrix;
	int *bitmatrix;
	int **schedule;
	
	/* Creation of file name variables */
	char temp[5];
	char *s1, *s2, *extension;
	char *fname;
	int md;
	char *curdir;
	
	/* Timing variables */
	struct timing t1, t2, t3, t4;
	double tsec;
	double totalsec;
	struct timing start;

	/* Find buffersize */
	int up, down;


	signal(SIGQUIT, ctrl_bs_handler);

	/* Start timing */
	timing_set(&t1);
	totalsec = 0.0;
	matrix = NULL;
	bitmatrix = NULL;
	schedule = NULL;
	
	/* Error check Arguments*/
	if (argc != 8) {
		fprintf(stderr,  "usage: inputfile k m coding_technique w packetsize buffersize\n");
		fprintf(stderr,  "\nChoose one of the following coding techniques: \nreed_sol_van, \nreed_sol_r6_op, \ncauchy_orig, \ncauchy_good, \nliberation, \nblaum_roth, \nliber8tion");
		fprintf(stderr,  "\n\nPacketsize is ignored for the reed_sol's");
		fprintf(stderr,  "\nBuffersize of 0 means the buffersize is chosen automatically.\n");
		fprintf(stderr,  "\nIf you just want to test speed, use an inputfile of \"-number\" where number is the size of the fake file you want to test.\n\n");
		exit(0);
	}
	/* Conversion of parameters and error checking */	
	if (sscanf(argv[2], "%d", &k) == 0 || k <= 0) {
		fprintf(stderr,  "Invalid value for k\n");
		exit(0);
	}
	if (sscanf(argv[3], "%d", &m) == 0 || m < 0) {
		fprintf(stderr,  "Invalid value for m\n");
		exit(0);
	}
	if (sscanf(argv[5],"%d", &w) == 0 || w <= 0) {
		fprintf(stderr,  "Invalid value for w.\n");
		exit(0);
	}
	if (argc == 6) {
		packetsize = 0;
	}
	else {
		if (sscanf(argv[6], "%d", &packetsize) == 0 || packetsize < 0) {
			fprintf(stderr,  "Invalid value for packetsize.\n");
			exit(0);
		}
	}
	if (argc != 8) {
		buffersize = 0;
	}
	else {
		if (sscanf(argv[7], "%d", &buffersize) == 0 || buffersize < 0) {
			fprintf(stderr, "Invalid value for buffersize\n");
			exit(0);
		}
		
	}

	/* Determine proper buffersize by finding the closest valid buffersize to the input value  */
	if (buffersize != 0) {
		if (packetsize != 0 && buffersize%(sizeof(long)*w*k*packetsize) != 0) { 
			up = buffersize;
			down = buffersize;
			while (up%(sizeof(long)*w*k*packetsize) != 0 && (down%(sizeof(long)*w*k*packetsize) != 0)) {
				up++;
				if (down == 0) {
					down--;
				}
			}
			if (up%(sizeof(long)*w*k*packetsize) == 0) {
				buffersize = up;
			}
			else {
				if (down != 0) {
					buffersize = down;
				}
			}
		}
		else if (packetsize == 0 && buffersize%(sizeof(long)*w*k) != 0) {
			up = buffersize;
			down = buffersize;
			while (up%(sizeof(long)*w*k) != 0 && down%(sizeof(long)*w*k) != 0) {
				up++;
				down--;
			}
			if (up%(sizeof(long)*w*k) == 0) {
				buffersize = up;
			}
			else {
				buffersize = down;
			}
		}
	}

	/* Setting of coding technique and error checking */
	
	if (strcmp(argv[4], "no_coding") == 0) {
		tech = No_Coding;
	}
	else if (strcmp(argv[4], "reed_sol_van") == 0) {
		tech = Reed_Sol_Van;
		if (w != 8 && w != 16 && w != 32) {
			fprintf(stderr,  "w must be one of {8, 16, 32}\n");
			exit(0);
		}
	}
	else if (strcmp(argv[4], "reed_sol_r6_op") == 0) {
		if (m != 2) {
			fprintf(stderr,  "m must be equal to 2\n");
			exit(0);
		}
		if (w != 8 && w != 16 && w != 32) {
			fprintf(stderr,  "w must be one of {8, 16, 32}\n");
			exit(0);
		}
		tech = Reed_Sol_R6_Op;
	}
	else if (strcmp(argv[4], "cauchy_orig") == 0) {
		tech = Cauchy_Orig;
		if (packetsize == 0) {
			fprintf(stderr, "Must include packetsize.\n");
			exit(0);
		}
	}
	else if (strcmp(argv[4], "cauchy_good") == 0) {
		tech = Cauchy_Good;
		if (packetsize == 0) {
			fprintf(stderr, "Must include packetsize.\n");
			exit(0);
		}
	}
	else if (strcmp(argv[4], "liberation") == 0) {
		if (k > w) {
			fprintf(stderr,  "k must be less than or equal to w\n");
			exit(0);
		}
		if (w <= 2 || !(w%2) || !is_prime(w)) {
			fprintf(stderr,  "w must be greater than two and w must be prime\n");
			exit(0);
		}
		if (packetsize == 0) {
			fprintf(stderr, "Must include packetsize.\n");
			exit(0);
		}
		if ((packetsize%(sizeof(long))) != 0) {
			fprintf(stderr,  "packetsize must be a multiple of sizeof(long)\n");
			exit(0);
		}
		tech = Liberation;
	}
	else if (strcmp(argv[4], "blaum_roth") == 0) {
		if (k > w) {
			fprintf(stderr,  "k must be less than or equal to w\n");
			exit(0);
		}
		if (w <= 2 || !((w+1)%2) || !is_prime(w+1)) {
			fprintf(stderr,  "w must be greater than two and w+1 must be prime\n");
			exit(0);
		}
		if (packetsize == 0) {
			fprintf(stderr, "Must include packetsize.\n");
			exit(0);
		}
		if ((packetsize%(sizeof(long))) != 0) {
			fprintf(stderr,  "packetsize must be a multiple of sizeof(long)\n");
			exit(0);
		}
		tech = Blaum_Roth;
	}
	else if (strcmp(argv[4], "liber8tion") == 0) {
		if (packetsize == 0) {
			fprintf(stderr, "Must include packetsize\n");
			exit(0);
		}
		if (w != 8) {
			fprintf(stderr, "w must equal 8\n");
			exit(0);
		}
		if (m != 2) {
			fprintf(stderr, "m must equal 2\n");
			exit(0);
		}
		if (k > w) {
			fprintf(stderr, "k must be less than or equal to w\n");
			exit(0);
		}
		tech = Liber8tion;
	}
	else {
		fprintf(stderr,  "Not a valid coding technique. Choose one of the following: reed_sol_van, reed_sol_r6_op, cauchy_orig, cauchy_good, liberation, blaum_roth, liber8tion, no_coding\n");
		exit(0);
	}

	/* Set global variable method for signal handler */
	method = tech;

	/* Get current working directory for construction of file names */
	curdir = (char*)malloc(sizeof(char)*1000);	
	if (! getcwd(curdir, 1000)) {
            curdir[0] = 0;
        }

        if (argv[1][0] != '-') {

		/* Open file and error check */
		fp = fopen(argv[1], "rb");
		if (fp == NULL) {
			fprintf(stderr,  "Unable to open file.\n");
			exit(0);
		}
	
		/* Create Coding directory */
		i = mkdir("Coding", S_IRWXU);
		if (i == -1 && errno != EEXIST) {
			fprintf(stderr, "Unable to create Coding directory.\n");
			exit(0);
		}
	
		/* Determine original size of file */
		stat(argv[1], &status);	
		size = status.st_size;
        } else {
        	if (sscanf(argv[1]+1, "%d", &size) != 1 || size <= 0) {
                	fprintf(stderr, "Files starting with '-' should be sizes for randomly created input\n");
			exit(1);
		}
        	fp = NULL;
		MOA_Seed(time(0));
        }

	newsize = size;
	
	/* Find new size by determining next closest multiple */
	if (packetsize != 0) {
		if (size%(k*w*packetsize*sizeof(long)) != 0) {
			while (newsize%(k*w*packetsize*sizeof(long)) != 0) 
				newsize++;
		}
	}
	else {
		if (size%(k*w*sizeof(long)) != 0) {
			while (newsize%(k*w*sizeof(long)) != 0) 
				newsize++;
		}
	}
	
	if (buffersize != 0) {
		while (newsize%buffersize != 0) {
			newsize++;
		}
	}


	/* Determine size of k+m files */
	blocksize = newsize/k;

	/* Allow for buffersize and determine number of read-ins */
	if (size > buffersize && buffersize != 0) {
		if (newsize%buffersize != 0) {
			readins = newsize/buffersize;
		}
		else {
			readins = newsize/buffersize;
		}
		block = (char *)malloc(sizeof(char)*buffersize);
		blocksize = buffersize/k;
	}
	else {
		readins = 1;
		buffersize = size;
		block = (char *)malloc(sizeof(char)*newsize);
	}
	
	/* Break inputfile name into the filename and extension */	
	s1 = (char*)malloc(sizeof(char)*(strlen(argv[1])+20));
	s2 = strrchr(argv[1], '/');
	if (s2 != NULL) {
		s2++;
		strcpy(s1, s2);
	}
	else {
		strcpy(s1, argv[1]);
	}
	s2 = strchr(s1, '.');
	if (s2 != NULL) {
          extension = strdup(s2);
          *s2 = '\0';
	} else {
          extension = strdup("");
        }
	
	/* Allocate for full file name */
	fname = (char*)malloc(sizeof(char)*(strlen(argv[1])+strlen(curdir)+20));
	sprintf(temp, "%d", k);
	md = strlen(temp);
	
	/* Allocate data and coding */
	data = (char **)malloc(sizeof(char*)*k);
	coding = (char **)malloc(sizeof(char*)*m);
	for (i = 0; i < m; i++) {
		coding[i] = (char *)malloc(sizeof(char)*blocksize);
                if (coding[i] == NULL) { perror("malloc"); exit(1); }
	}

	

	/* Create coding matrix or bitmatrix and schedule */
	timing_set(&t3);
	switch(tech) {
		case No_Coding:
			break;
		case Reed_Sol_Van:
			matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
			break;
		case Reed_Sol_R6_Op:
			break;
		case Cauchy_Orig:
			matrix = cauchy_original_coding_matrix(k, m, w);
			bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
			schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
			break;
		case Cauchy_Good:
			matrix = cauchy_good_general_coding_matrix(k, m, w);
			bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
			schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
			break;	
		case Liberation:
			bitmatrix = liberation_coding_bitmatrix(k, w);
			schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
			break;
		case Blaum_Roth:
			bitmatrix = blaum_roth_coding_bitmatrix(k, w);
			schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
			break;
		case Liber8tion:
			bitmatrix = liber8tion_coding_bitmatrix(k);
			schedule = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
			break;
		case RDP:
		case EVENODD:
			assert(0);
	}
	timing_set(&start);
	timing_set(&t4);
	totalsec += timing_delta(&t3, &t4);

	

	/* Read in data until finished */
	n = 1;
	total = 0;

	while (n <= readins) {
		/* Check if padding is needed, if so, add appropriate 
		   number of zeros */
		if (total < size && total+buffersize <= size) {
			total += jfread(block, sizeof(char), buffersize, fp);
		}
		else if (total < size && total+buffersize > size) {
			extra = jfread(block, sizeof(char), buffersize, fp);
			for (i = extra; i < buffersize; i++) {
				block[i] = '0';
			}
		}
		else if (total == size) {
			for (i = 0; i < buffersize; i++) {
				block[i] = '0';
			}
		}
	
		/* Set pointers to point to file data */
		for (i = 0; i < k; i++) {
			data[i] = block+(i*blocksize);
		}

		timing_set(&t3);
		/* Encode according to coding method */
		switch(tech) {	
			case No_Coding:
				break;
			case Reed_Sol_Van:
				jerasure_matrix_encode(k, m, w, matrix, data, coding, blocksize);
				break;
			case Reed_Sol_R6_Op:
				reed_sol_r6_encode(k, w, data, coding, blocksize);
				break;
			case Cauchy_Orig:
				jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
				break;
			case Cauchy_Good:
				jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
				break;
			case Liberation:
				jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
				break;
			case Blaum_Roth:
				jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
				break;
			case Liber8tion:
				jerasure_schedule_encode(k, m, w, schedule, data, coding, blocksize, packetsize);
				break;
			case RDP:
			case EVENODD:
				assert(0);
		}
		timing_set(&t4);
	
		/* Write data and encoded data to k+m files */
		for	(i = 1; i <= k; i++) {
			if (fp == NULL) {
				bzero(data[i-1], blocksize);
 			} else {
				sprintf(fname, "%s/Coding/%s_k%0*d%s", curdir, s1, md, i, extension);
				if (n == 1) {
					fp2 = fopen(fname, "wb");
				}
				else {
					fp2 = fopen(fname, "ab");
				}
				fwrite(data[i-1], sizeof(char), blocksize, fp2);
				fclose(fp2);
			}
			
		}
		for	(i = 1; i <= m; i++) {
			if (fp == NULL) {
				bzero(data[i-1], blocksize);
 			} else {
				sprintf(fname, "%s/Coding/%s_m%0*d%s", curdir, s1, md, i, extension);
				if (n == 1) {
					fp2 = fopen(fname, "wb");
				}
				else {
					fp2 = fopen(fname, "ab");
				}
				fwrite(coding[i-1], sizeof(char), blocksize, fp2);
				fclose(fp2);
			}
		}
		n++;
		/* Calculate encoding time */
		totalsec += timing_delta(&t3, &t4);
	}

	/* Create metadata file */
        if (fp != NULL) {
		sprintf(fname, "%s/Coding/%s_meta.txt", curdir, s1);
		fp2 = fopen(fname, "wb");
		fprintf(fp2, "%s\n", argv[1]);
		fprintf(fp2, "%d\n", size);
		fprintf(fp2, "%d %d %d %d %d\n", k, m, w, packetsize, buffersize);
		fprintf(fp2, "%s\n", argv[4]);
		fprintf(fp2, "%d\n", tech);
		fprintf(fp2, "%d\n", readins);
		fclose(fp2);
	}


	/* Free allocated memory */
	free(s1);
	free(fname);
	free(block);
	free(curdir);
	
	/* Calculate rate in MB/sec and print */
	timing_set(&t2);
	tsec = timing_delta(&t1, &t2);
	printf("Encoding (MB/sec): %0.10f\n", (((double) size)/1024.0/1024.0)/totalsec);
	printf("En_Total (MB/sec): %0.10f\n", (((double) size)/1024.0/1024.0)/tsec);

	return 0;
}
Ejemplo n.º 10
0
int main(int argc, char **argv)
{
  long l;
  int k, w, i, j, m;
  int *matrix;
  char **data, **coding, **old_values;
  int *erasures, *erased;
  int *decoding_matrix, *dm_ids;
  gf_t *gf = NULL;
  uint32_t seed;
  
  if (argc < 6) usage("Not enough command line arguments");  
  if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
  if (sscanf(argv[4], "%d", &seed) == 0) usage("Bad seed");
  if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");

  MOA_Seed(seed);

  gf = get_gf(w, argc, argv, 5); 

  if (gf == NULL) {
    usage("Invalid arguments given for GF!\n");
  }

  galois_change_technique(gf, w); 

  matrix = reed_sol_vandermonde_coding_matrix(k, m, w);

  printf("<HTML><TITLE>reed_sol_test_gf");
  for (i = 1; i < argc; i++) printf(" %s", argv[i]);
  printf("</TITLE>\n");
  printf("<h3>reed_sol_test_gf");
  for (i = 1; i < argc; i++) printf(" %s", argv[i]);
  printf("</h3>\n");
  printf("<pre>\n");

  printf("Last m rows of the generator matrix (G^T):\n\n");
  jerasure_print_matrix(matrix, m, k, w);
  printf("\n");

  data = talloc(char *, k);
  for (i = 0; i < k; i++) {
    data[i] = talloc(char, BUFSIZE);
    MOA_Fill_Random_Region(data[i], BUFSIZE);
  }

  coding = talloc(char *, m);
  old_values = talloc(char *, m);
  for (i = 0; i < m; i++) {
    coding[i] = talloc(char, BUFSIZE);
    old_values[i] = talloc(char, BUFSIZE);
  }

  jerasure_matrix_encode(k, m, w, matrix, data, coding, BUFSIZE);
  
  erasures = talloc(int, (m+1));
  erased = talloc(int, (k+m));
  for (i = 0; i < m+k; i++) erased[i] = 0;
  l = 0;
  for (i = 0; i < m; ) {
    erasures[i] = ((unsigned int)MOA_Random_W(w,1))%(k+m);
    if (erased[erasures[i]] == 0) {
      erased[erasures[i]] = 1;
      memcpy(old_values[i], (erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], BUFSIZE);
      bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], BUFSIZE);
      i++;
    }
  }
  erasures[i] = -1;

  i = jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, BUFSIZE);

  for (i = 0; i < m; i++) {
    if (erasures[i] < k) {
      if (memcmp(data[erasures[i]], old_values[i], BUFSIZE)) {
        fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
        exit(1);
      }
    } else {
      if (memcmp(coding[erasures[i]-k], old_values[i], BUFSIZE)) {
        fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
        exit(1);
      }
    }
  }
  
  printf("Encoding and decoding were both successful.\n");
  return 0;
}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{
  int k, m, w, size;
  int i, j;
  int *matrix;
  char **data, **coding;
  int *erasures, *erased;
  int *decoding_matrix, *dm_ids;
  uint32_t seed;
  
  if (argc != 6) usage(NULL);
  if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
  if (w < 32 && k + m > (1 << w)) usage("k + m must be <= 2 ^ w");
  if (sscanf(argv[4], "%d", &size) == 0 || size % sizeof(long) != 0) 
		usage("size must be multiple of sizeof(long)");
  if (sscanf(argv[5], "%d", &seed) == 0) usage("Bad seed");

  matrix = talloc(int, m*k);
  for (i = 0; i < m; i++) {
    for (j = 0; j < k; j++) {
      matrix[i*k+j] = galois_single_divide(1, i ^ (m + j), w);
    }
  }

  printf("<HTML><TITLE>jerasure_05");
  for (i = 1; i < argc; i++) printf(" %s", argv[i]);
  printf("</TITLE>\n");
  printf("<h3>jerasure_05");
  for (i = 1; i < argc; i++) printf(" %s", argv[i]);
  printf("</h3>\n");
  printf("<pre>\n");

  printf("The Coding Matrix (the last m rows of the Generator Matrix G^T):\n\n");
  jerasure_print_matrix(matrix, m, k, w);
  printf("\n");

  MOA_Seed(seed);
  data = talloc(char *, k);
  for (i = 0; i < k; i++) {
    data[i] = talloc(char, size);
    MOA_Fill_Random_Region(data[i], size);
  }

  coding = talloc(char *, m);
  for (i = 0; i < m; i++) {
    coding[i] = talloc(char, size);
  }

  jerasure_matrix_encode(k, m, w, matrix, data, coding, size);
  
  printf("Encoding Complete:\n\n");
  print_data_and_coding(k, m, w, size, data, coding);

  erasures = talloc(int, (m+1));
  erased = talloc(int, (k+m));
  for (i = 0; i < m+k; i++) erased[i] = 0;
  for (i = 0; i < m; ) {
    erasures[i] = (MOA_Random_W(w, 1))%(k+m);
    if (erased[erasures[i]] == 0) {
      erased[erasures[i]] = 1;
	  
      bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], size);
      i++;
    }
  }
  erasures[i] = -1;

  printf("Erased %d random devices:\n\n", m);
  print_data_and_coding(k, m, w, size, data, coding);
  
  i = jerasure_matrix_decode(k, m, w, matrix, 0, erasures, data, coding, size);

  printf("State of the system after decoding:\n\n");
  print_data_and_coding(k, m, w, size, data, coding);
  
  decoding_matrix = talloc(int, k*k);
  dm_ids = talloc(int, k);

  for (i = 0; i < m; i++) erased[i] = 1;
  for (; i < k+m; i++) erased[i] = 0;

  jerasure_make_decoding_matrix(k, m, w, matrix, erased, decoding_matrix, dm_ids);

  printf("Suppose we erase the first %d devices.  Here is the decoding matrix:\n\n", m);
  jerasure_print_matrix(decoding_matrix, k, k, w);
  printf("\n");
  printf("And dm_ids:\n\n");
  jerasure_print_matrix(dm_ids, 1, k, w);

  bzero(data[0], size);
  jerasure_matrix_dotprod(k, w, decoding_matrix, dm_ids, 0, data, coding, size);

  printf("\nAfter calling jerasure_matrix_dotprod, we calculate the value of device #0 to be:\n\n");
  printf("D0 :");
  for(i=0;i< size; i+=(w/8)) {
	  printf(" ");
	  for(j=0;j < w/8;j++){
		printf("%02x", (unsigned char)data[0][i+j]);
	  }
  }
  printf("\n\n");

  return 0;
}
Ejemplo n.º 12
0
int main(int argc, char **argv)
{
  int w, it, i, size, iterations, xor;
  char tests[100];
  char test;
  char *single_tests = "MDI";
  char *region_tests = "G012";
  char *tstrings[256];
  void *tmethods[256];
  gf_t      gf;
  double timer, elapsed, ds, di, dnum;
  int num;
  time_t t0;
  uint8_t *ra, *rb;
  gf_general_t a;

  
  if (argc < 6) usage(NULL);
  
  if (sscanf(argv[1], "%d", &w) == 0){
    usage("Bad w[-pp]\n");
  }

  
  if (sscanf(argv[3], "%ld", &t0) == 0) usage("Bad seed\n");
  if (sscanf(argv[4], "%d", &size) == 0) usage("Bad size\n");
  if (sscanf(argv[5], "%d", &iterations) == 0) usage("Bad iterations\n");
  if (t0 == -1) t0 = time(0);
  MOA_Seed(t0);

  ds = size;
  di = iterations;

  if ((w > 32 && w != 64 && w != 128) || w < 0) usage("Bad w");
  if ((size * 8) % w != 0) usage ("Bad size -- must be a multiple of w*8\n");
  
  if (!create_gf_from_argv(&gf, w, argc, argv, 6)) usage(BM);

  strcpy(tests, "");
  for (i = 0; argv[2][i] != '\0'; i++) {
    switch(argv[2][i]) {
      case 'A': strcat(tests, single_tests); 
                strcat(tests, region_tests); 
                break;
      case 'S': strcat(tests, single_tests); break;
      case 'R': strcat(tests, region_tests); break;
      case 'G': strcat(tests, "G"); break;
      case '0': strcat(tests, "0"); break;
      case '1': strcat(tests, "1"); break;
      case '2': strcat(tests, "2"); break;
      case 'M': strcat(tests, "M"); break;
      case 'D': strcat(tests, "D"); break;
      case 'I': strcat(tests, "I"); break;
      default: usage("Bad tests");
    }
  }

  tstrings['M'] = "Multiply";
  tstrings['D'] = "Divide";
  tstrings['I'] = "Inverse";
  tstrings['G'] = "Region-Random";
  tstrings['0'] = "Region-By-Zero";
  tstrings['1'] = "Region-By-One";
  tstrings['2'] = "Region-By-Two";

  tmethods['M'] = (void *) gf.multiply.w32;
  tmethods['D'] = (void *) gf.divide.w32;
  tmethods['I'] = (void *) gf.inverse.w32;
  tmethods['G'] = (void *) gf.multiply_region.w32;
  tmethods['0'] = (void *) gf.multiply_region.w32;
  tmethods['1'] = (void *) gf.multiply_region.w32;
  tmethods['2'] = (void *) gf.multiply_region.w32;

  printf("Seed: %ld\n", t0);

  ra = (uint8_t *) malloc(size);
  rb = (uint8_t *) malloc(size);

  if (ra == NULL || rb == NULL) { perror("malloc"); exit(1); }

  for (i = 0; i < 3; i++) {
    test = single_tests[i];
    if (strchr(tests, test) != NULL) {
      if (tmethods[(int)test] == NULL) {
        printf("No %s method.\n", tstrings[(int)test]);
      } else {
        elapsed = 0;
        dnum = 0;
        for (it = 0; it < iterations; it++) {
          gf_general_set_up_single_timing_test(w, ra, rb, size);
          timer_start(&timer);
          num = gf_general_do_single_timing_test(&gf, ra, rb, size, test);
          dnum += num;
          elapsed += timer_split(&timer);
        }
        printf("%14s:           %10.6lf s   Mops: %10.3lf    %10.3lf Mega-ops/s\n", 
               tstrings[(int)test], elapsed, 
               dnum/1024.0/1024.0, dnum/1024.0/1024.0/elapsed);
      }
    }
  }

  for (i = 0; i < 4; i++) {
    test = region_tests[i];
    if (strchr(tests, test) != NULL) {
      if (tmethods[(int)test] == NULL) {
        printf("No %s method.\n", tstrings[(int)test]);
      } else {
        if (test == '0') gf_general_set_zero(&a, w);
        if (test == '1') gf_general_set_one(&a, w);
        if (test == '2') gf_general_set_two(&a, w);

        for (xor = 0; xor < 2; xor++) {
          elapsed = 0;
          for (it = 0; it < iterations; it++) {
            if (test == 'G') gf_general_set_random(&a, w, 1);
            gf_general_set_up_single_timing_test(8, ra, rb, size);
            timer_start(&timer);
            gf_general_do_region_multiply(&gf, &a, ra, rb, size, xor);
            elapsed += timer_split(&timer);
          }
          printf("%14s: XOR: %d    %10.6lf s     MB: %10.3lf    %10.3lf MB/s\n", 
               tstrings[(int)test], xor, elapsed, 
               ds*di/1024.0/1024.0, ds*di/1024.0/1024.0/elapsed);
        }
      }
    }
  }
  return 0;
}