Commit b5745fa4f1bdc3521153ca4fb51c6e7bae5b1511

Authored by Jim Plank
0 parents
Exists in master and in 1 other branch v2

Adding the current jerasure 1.2A

Examples/.swp 0 → 100755
No preview for this file type
Examples/cauchy_01.c 0 → 100755
  1 +++ a/Examples/cauchy_01.c
... ... @@ -0,0 +1,90 @@
  1 +/* Examples/cauchy_01.c
  2 +
  3 +Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
  4 +
  5 +Revision 1.2A
  6 +May 24, 2011
  7 +
  8 +James S. Plank
  9 +Department of Electrical Engineering and Computer Science
  10 +University of Tennessee
  11 +Knoxville, TN 37996
  12 +plank@cs.utk.edu
  13 +
  14 +Copyright (c) 2011, James S. Plank
  15 +All rights reserved.
  16 +
  17 +Redistribution and use in source and binary forms, with or without
  18 +modification, are permitted provided that the following conditions
  19 +are met:
  20 +
  21 + - Redistributions of source code must retain the above copyright
  22 + notice, this list of conditions and the following disclaimer.
  23 +
  24 + - Redistributions in binary form must reproduce the above copyright
  25 + notice, this list of conditions and the following disclaimer in
  26 + the documentation and/or other materials provided with the
  27 + distribution.
  28 +
  29 + - Neither the name of the University of Tennessee nor the names of its
  30 + contributors may be used to endorse or promote products derived
  31 + from this software without specific prior written permission.
  32 +
  33 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37 +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  38 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  39 +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40 +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  41 +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42 +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  43 +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  44 +POSSIBILITY OF SUCH DAMAGE.
  45 +
  46 + */
  47 +
  48 +#include <stdio.h>
  49 +#include <stdlib.h>
  50 +#include <string.h>
  51 +#include "jerasure.h"
  52 +#include "reed_sol.h"
  53 +
  54 +#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
  55 +
  56 +usage(char *s)
  57 +{
  58 + fprintf(stderr, "usage: cauchy_01 n w - Prints the number of ones in the bitmatrix representation of n in GF(2^w).\n");
  59 + fprintf(stderr, " \n");
  60 + fprintf(stderr, " It also prints out the bit-matrix and confirms that the number of ones is correct.\n");
  61 + fprintf(stderr, " \n");
  62 + fprintf(stderr, "This demonstrates: cauchy_n_ones()\n");
  63 + fprintf(stderr, " jerasure_matrix_to_bitmatrix()\n");
  64 + fprintf(stderr, " jerasure_print_bitmatrix()\n");
  65 + if (s != NULL) fprintf(stderr, "%s\n", s);
  66 + exit(1);
  67 +}
  68 +
  69 +int main(int argc, char **argv)
  70 +{
  71 + int n, i, no, w;
  72 + int *bitmatrix;
  73 +
  74 + if (argc != 3) usage(NULL);
  75 + if (sscanf(argv[1], "%d", &n) == 0 || n <= 0) usage("Bad n");
  76 + if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
  77 + if (w < 30 && n >= (1 << w)) usage("n is too big");
  78 +
  79 + bitmatrix = jerasure_matrix_to_bitmatrix(1, 1, w, &n);
  80 + no = 0;
  81 + for (i = 0; i < w*w; i++) no += bitmatrix[i];
  82 +
  83 + printf("# Ones: %d\n", cauchy_n_ones(n, w));
  84 + printf("\n");
  85 + printf("Bitmatrix has %d ones\n", no);
  86 + printf("\n");
  87 + jerasure_print_bitmatrix(bitmatrix, w, w, w);
  88 +
  89 + return 0;
  90 +}
... ...
Examples/cauchy_02.c 0 → 100755
  1 +++ a/Examples/cauchy_02.c
... ... @@ -0,0 +1,210 @@
  1 +/* Examples/cauchy_02.c
  2 +Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
  3 +
  4 +Revision 1.2A
  5 +May 24, 2011
  6 +
  7 +James S. Plank
  8 +Department of Electrical Engineering and Computer Science
  9 +University of Tennessee
  10 +Knoxville, TN 37996
  11 +plank@cs.utk.edu
  12 +
  13 +Copyright (c) 2011, James S. Plank
  14 +All rights reserved.
  15 +
  16 +Redistribution and use in source and binary forms, with or without
  17 +modification, are permitted provided that the following conditions
  18 +are met:
  19 +
  20 + - Redistributions of source code must retain the above copyright
  21 + notice, this list of conditions and the following disclaimer.
  22 +
  23 + - Redistributions in binary form must reproduce the above copyright
  24 + notice, this list of conditions and the following disclaimer in
  25 + the documentation and/or other materials provided with the
  26 + distribution.
  27 +
  28 + - Neither the name of the University of Tennessee nor the names of its
  29 + contributors may be used to endorse or promote products derived
  30 + from this software without specific prior written permission.
  31 +
  32 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  35 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  36 +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  37 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  38 +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  39 +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  40 +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41 +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  42 +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  43 +POSSIBILITY OF SUCH DAMAGE.
  44 +
  45 + */
  46 +
  47 +
  48 +/*
  49 + revised by S. Simmerman
  50 + 2/25/08
  51 +*/
  52 +#include <stdio.h>
  53 +#include <stdlib.h>
  54 +#include <string.h>
  55 +#include "jerasure.h"
  56 +#include "cauchy.h"
  57 +
  58 +#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
  59 +
  60 +usage(char *s)
  61 +{
  62 + fprintf(stderr, "usage: cauchy_02 k m w - Scheduled CRS coding example with the original matrix in GF(2^w).\n");
  63 + fprintf(stderr, " \n");
  64 + fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix using the original\n");
  65 + fprintf(stderr, " Cauchy Distribution matrix construction algorithm, then it encodes\n");
  66 + fprintf(stderr, " k devices of w*%d bytes using smart bit-matrix scheduling.\n", sizeof(long));
  67 + fprintf(stderr, " It decodes using bit-matrix scheduling as well.\n");
  68 + fprintf(stderr, " \n");
  69 + fprintf(stderr, "This demonstrates: cauchy_original_coding_matrix()\n");
  70 + fprintf(stderr, " cauchy_xy_coding_matrix()\n");
  71 + fprintf(stderr, " cauchy_n_ones()\n");
  72 + fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
  73 + fprintf(stderr, " jerasure_schedule_encode()\n");
  74 + fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
  75 + fprintf(stderr, " jerasure_print_matrix()\n");
  76 + fprintf(stderr, " jerasure_get_stats()\n");
  77 + if (s != NULL) fprintf(stderr, "%s\n", s);
  78 + exit(1);
  79 +}
  80 +
  81 +static void print_data_and_coding(int k, int m, int w, int psize,
  82 + char **data, char **coding)
  83 +{
  84 + int i, j, x, n, sp;
  85 + long l;
  86 +
  87 + if(k > m) n = k;
  88 + else n = m;
  89 + sp = psize * 2 + (psize/4) + 12;
  90 +
  91 + printf("%-*sCoding\n", sp, "Data");
  92 + for(i = 0; i < n; i++) {
  93 + for (j = 0; j < w; j++) {
  94 + if(i < k) {
  95 +
  96 + if(j==0) printf("D%-2d p%-2d:", i,j);
  97 + else printf(" p%-2d:", j);
  98 + for(x = 0; x < psize; x +=4) {
  99 + memcpy(&l, data[i]+j*psize+x, sizeof(long));
  100 + printf(" %08lx", l);
  101 + }
  102 + printf(" ");
  103 + }
  104 + else printf("%*s", sp, "");
  105 + if(i < m) {
  106 + if(j==0) printf("C%-2d p%-2d:", i,j);
  107 + else printf(" p%-2d:", j);
  108 + for(x = 0; x < psize; x +=4) {
  109 + memcpy(&l, coding[i]+j*psize+x, sizeof(long));
  110 + printf(" %08lx", l);
  111 + }
  112 + }
  113 + printf("\n");
  114 + }
  115 + }
  116 +
  117 + printf("\n");
  118 +}
  119 +
  120 +int main(int argc, char **argv)
  121 +{
  122 + long l;
  123 + int k, w, i, j, m;
  124 + int *matrix, *bitmatrix, *m2, *x, *y;
  125 + char **data, **coding, **ptrs;
  126 + int **smart;
  127 + int no;
  128 + int *erasures, *erased;
  129 + double stats[3];
  130 +
  131 + if (argc != 4) usage(NULL);
  132 + if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  133 + if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  134 + if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
  135 + if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
  136 +
  137 + matrix = cauchy_original_coding_matrix(k, m, w);
  138 + if (matrix == NULL) {
  139 + usage("couldn't make coding matrix");
  140 + }
  141 +
  142 + no = 0;
  143 + for (i = 0; i < k*m; i++) {
  144 + no += cauchy_n_ones(matrix[i], w);
  145 + }
  146 + printf("Matrix has %d ones\n\n", no);
  147 + jerasure_print_matrix(matrix, m, k, w);
  148 + printf("\n", no);
  149 + bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
  150 +
  151 + smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
  152 +
  153 + srand48(0);
  154 + data = talloc(char *, k);
  155 + for (i = 0; i < k; i++) {
  156 + data[i] = talloc(char, sizeof(long)*w);
  157 + for (j = 0; j < w; j++) {
  158 + l = lrand48();
  159 + memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
  160 + }
  161 + }
  162 +
  163 + coding = talloc(char *, m);
  164 + for (i = 0; i < m; i++) {
  165 + coding[i] = talloc(char, sizeof(long)*w);
  166 + }
  167 +
  168 + jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
  169 + jerasure_get_stats(stats);
  170 + printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
  171 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  172 +
  173 + erasures = talloc(int, (m+1));
  174 + erased = talloc(int, (k+m));
  175 + for (i = 0; i < m+k; i++) erased[i] = 0;
  176 + for (i = 0; i < m; ) {
  177 + erasures[i] = lrand48()%(k+m);
  178 + if (erased[erasures[i]] == 0) {
  179 + erased[erasures[i]] = 1;
  180 + bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
  181 + i++;
  182 + }
  183 + }
  184 + erasures[i] = -1;
  185 +
  186 + printf("Erased %d random devices:\n\n", m);
  187 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  188 +
  189 + jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
  190 + jerasure_get_stats(stats);
  191 +
  192 + printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
  193 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  194 +
  195 + x = talloc(int, m);
  196 + y = talloc(int, k);
  197 + if (x == NULL || y == NULL) { perror("malloc"); exit(1); }
  198 + for (i = 0; i < m; i++) x[i] = i;
  199 + for (i = 0; i < k; i++) y[i] = m+i;
  200 + m2 = cauchy_xy_coding_matrix(k, m, w, x, y);
  201 + if (memcmp(matrix, m2, sizeof(int)*k*m) != 0) {
  202 + printf("Error -- the matrices made by original and xy don't match\n");
  203 + exit(1);
  204 + } else {
  205 + printf("Generated the identical matrix using cauchy_xy_coding_matrix()\n");
  206 + }
  207 +
  208 +
  209 + return 0;
  210 +}
... ...
Examples/cauchy_03.c 0 → 100755
  1 +++ a/Examples/cauchy_03.c
... ... @@ -0,0 +1,204 @@
  1 +/* Examples/cauchy_03.c
  2 + * James S. Plank
  3 +
  4 +Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
  5 +
  6 +Revision 1.2A
  7 +May 24, 2011
  8 +
  9 +James S. Plank
  10 +Department of Electrical Engineering and Computer Science
  11 +University of Tennessee
  12 +Knoxville, TN 37996
  13 +plank@cs.utk.edu
  14 +
  15 +Copyright (c) 2011, James S. Plank
  16 +All rights reserved.
  17 +
  18 +Redistribution and use in source and binary forms, with or without
  19 +modification, are permitted provided that the following conditions
  20 +are met:
  21 +
  22 + - Redistributions of source code must retain the above copyright
  23 + notice, this list of conditions and the following disclaimer.
  24 +
  25 + - Redistributions in binary form must reproduce the above copyright
  26 + notice, this list of conditions and the following disclaimer in
  27 + the documentation and/or other materials provided with the
  28 + distribution.
  29 +
  30 + - Neither the name of the University of Tennessee nor the names of its
  31 + contributors may be used to endorse or promote products derived
  32 + from this software without specific prior written permission.
  33 +
  34 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38 +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40 +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  41 +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  42 +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43 +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  44 +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45 +POSSIBILITY OF SUCH DAMAGE.
  46 +
  47 + */
  48 +
  49 +
  50 +/*
  51 + revised by S. Simmerman
  52 + 2/25/08
  53 +*/
  54 +#include <stdio.h>
  55 +#include <stdlib.h>
  56 +#include <string.h>
  57 +#include "jerasure.h"
  58 +#include "cauchy.h"
  59 +
  60 +#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
  61 +
  62 +usage(char *s)
  63 +{
  64 + fprintf(stderr, "usage: cauchy_03 k m w - Scheduled CRS coding example with improved matrix in GF(2^w).\n");
  65 + fprintf(stderr, " \n");
  66 + fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix using the original\n");
  67 + fprintf(stderr, " Cauchy Distribution matrix construction algorithm, then improves it\n");
  68 + fprintf(stderr, " with cauchy_improve_coding_matrix(). Then it does the same encoding and\n");
  69 + fprintf(stderr, " decoding as cauchy_02.\n");
  70 + fprintf(stderr, " \n");
  71 + fprintf(stderr, "This demonstrates: cauchy_original_coding_matrix()\n");
  72 + fprintf(stderr, " cauchy_improve_coding_matrix()\n");
  73 + fprintf(stderr, " cauchy_n_ones()\n");
  74 + fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
  75 + fprintf(stderr, " jerasure_schedule_encode()\n");
  76 + fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
  77 + fprintf(stderr, " jerasure_print_matrix()\n");
  78 + fprintf(stderr, " jerasure_get_stats()\n");
  79 + if (s != NULL) fprintf(stderr, "%s\n", s);
  80 + exit(1);
  81 +}
  82 +
  83 +static void print_data_and_coding(int k, int m, int w, int psize,
  84 + char **data, char **coding)
  85 +{
  86 + int i, j, x, n, sp;
  87 + long l;
  88 +
  89 + if(k > m) n = k;
  90 + else n = m;
  91 + sp = psize * 2 + (psize/4) + 12;
  92 +
  93 + printf("%-*sCoding\n", sp, "Data");
  94 + for(i = 0; i < n; i++) {
  95 + for (j = 0; j < w; j++) {
  96 + if(i < k) {
  97 +
  98 + if(j==0) printf("D%-2d p%-2d:", i,j);
  99 + else printf(" p%-2d:", j);
  100 + for(x = 0; x < psize; x +=4) {
  101 + memcpy(&l, data[i]+j*psize+x, sizeof(long));
  102 + printf(" %08lx", l);
  103 + }
  104 + printf(" ");
  105 + }
  106 + else printf("%*s", sp, "");
  107 + if(i < m) {
  108 + if(j==0) printf("C%-2d p%-2d:", i,j);
  109 + else printf(" p%-2d:", j);
  110 + for(x = 0; x < psize; x +=4) {
  111 + memcpy(&l, coding[i]+j*psize+x, sizeof(long));
  112 + printf(" %08lx", l);
  113 + }
  114 + }
  115 + printf("\n");
  116 + }
  117 + }
  118 +
  119 + printf("\n");
  120 +}
  121 +
  122 +int main(int argc, char **argv)
  123 +{
  124 + long l;
  125 + int k, w, i, j, m;
  126 + int *matrix, *bitmatrix, *m2, *x, *y;
  127 + char **data, **coding, **ptrs;
  128 + int **smart;
  129 + int no;
  130 + int *erasures, *erased;
  131 + double stats[3];
  132 +
  133 + if (argc != 4) usage(NULL);
  134 + if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  135 + if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  136 + if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
  137 + if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
  138 +
  139 + matrix = cauchy_original_coding_matrix(k, m, w);
  140 + if (matrix == NULL) {
  141 + usage("couldn't make coding matrix");
  142 + }
  143 +
  144 + no = 0;
  145 + for (i = 0; i < k*m; i++) {
  146 + no += cauchy_n_ones(matrix[i], w);
  147 + }
  148 + printf("The Original Matrix has %d ones\n", no);
  149 + cauchy_improve_coding_matrix(k, m, w, matrix);
  150 + no = 0;
  151 + for (i = 0; i < k*m; i++) {
  152 + no += cauchy_n_ones(matrix[i], w);
  153 + }
  154 + printf("The Improved Matrix has %d ones\n\n", no);
  155 + jerasure_print_matrix(matrix, m, k, w);
  156 + printf("\n", no);
  157 + bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
  158 +
  159 + smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
  160 +
  161 + srand48(0);
  162 + data = talloc(char *, k);
  163 + for (i = 0; i < k; i++) {
  164 + data[i] = talloc(char, sizeof(long)*w);
  165 + for (j = 0; j < w; j++) {
  166 + l = lrand48();
  167 + memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
  168 + }
  169 + }
  170 +
  171 + coding = talloc(char *, m);
  172 + for (i = 0; i < m; i++) {
  173 + coding[i] = talloc(char, sizeof(long)*w);
  174 + }
  175 +
  176 + jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
  177 + jerasure_get_stats(stats);
  178 + printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
  179 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  180 +
  181 + erasures = talloc(int, (m+1));
  182 + erased = talloc(int, (k+m));
  183 + for (i = 0; i < m+k; i++) erased[i] = 0;
  184 + for (i = 0; i < m; ) {
  185 + erasures[i] = lrand48()%(k+m);
  186 + if (erased[erasures[i]] == 0) {
  187 + erased[erasures[i]] = 1;
  188 + bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
  189 + i++;
  190 + }
  191 + }
  192 + erasures[i] = -1;
  193 +
  194 + printf("Erased %d random devices:\n\n", m);
  195 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  196 +
  197 + jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
  198 + jerasure_get_stats(stats);
  199 +
  200 + printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
  201 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  202 +
  203 + return 0;
  204 +}
... ...
Examples/cauchy_04.c 0 → 100755
  1 +++ a/Examples/cauchy_04.c
... ... @@ -0,0 +1,198 @@
  1 +/* Examples/cauchy_04.c
  2 + * James S. Plank
  3 +
  4 +Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
  5 +
  6 +Revision 1.2A
  7 +May 24, 2011
  8 +
  9 +James S. Plank
  10 +Department of Electrical Engineering and Computer Science
  11 +University of Tennessee
  12 +Knoxville, TN 37996
  13 +plank@cs.utk.edu
  14 +
  15 +Copyright (c) 2011, James S. Plank
  16 +All rights reserved.
  17 +
  18 +Redistribution and use in source and binary forms, with or without
  19 +modification, are permitted provided that the following conditions
  20 +are met:
  21 +
  22 + - Redistributions of source code must retain the above copyright
  23 + notice, this list of conditions and the following disclaimer.
  24 +
  25 + - Redistributions in binary form must reproduce the above copyright
  26 + notice, this list of conditions and the following disclaimer in
  27 + the documentation and/or other materials provided with the
  28 + distribution.
  29 +
  30 + - Neither the name of the University of Tennessee nor the names of its
  31 + contributors may be used to endorse or promote products derived
  32 + from this software without specific prior written permission.
  33 +
  34 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38 +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40 +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  41 +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  42 +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43 +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  44 +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45 +POSSIBILITY OF SUCH DAMAGE.
  46 +
  47 +
  48 + */
  49 +
  50 +
  51 +/*
  52 + revised by S. Simmerman
  53 + 2/25/08
  54 +*/
  55 +#include <stdio.h>
  56 +#include <stdlib.h>
  57 +#include <string.h>
  58 +#include "jerasure.h"
  59 +#include "cauchy.h"
  60 +
  61 +#define talloc(type, num) (type *) malloc(sizeof(type)*(num))
  62 +
  63 +usage(char *s)
  64 +{
  65 + fprintf(stderr, "usage: cauchy_04 k m w - Scheduled CRS coding example with a good matrix in GF(2^w).\n");
  66 + fprintf(stderr, " \n");
  67 + fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy distribution matrix using \n");
  68 + fprintf(stderr, " cauchy_good_coding_matrix(), then it encodes\n");
  69 + fprintf(stderr, " k devices of w*%d bytes using smart bit-matrix scheduling.\n", sizeof(long));
  70 + fprintf(stderr, " It decodes using bit-matrix scheduling as well.\n");
  71 + fprintf(stderr, " \n");
  72 + fprintf(stderr, "This demonstrates: cauchy_original_coding_matrix()\n");
  73 + fprintf(stderr, " cauchy_n_ones()\n");
  74 + fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
  75 + fprintf(stderr, " jerasure_schedule_encode()\n");
  76 + fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
  77 + fprintf(stderr, " jerasure_print_matrix()\n");
  78 + fprintf(stderr, " jerasure_get_stats()\n");
  79 + if (s != NULL) fprintf(stderr, "%s\n", s);
  80 + exit(1);
  81 +}
  82 +
  83 +static void print_data_and_coding(int k, int m, int w, int psize,
  84 + char **data, char **coding)
  85 +{
  86 + int i, j, x, n, sp;
  87 + long l;
  88 +
  89 + if(k > m) n = k;
  90 + else n = m;
  91 + sp = psize * 2 + (psize/4) + 12;
  92 +
  93 + printf("%-*sCoding\n", sp, "Data");
  94 + for(i = 0; i < n; i++) {
  95 + for (j = 0; j < w; j++) {
  96 + if(i < k) {
  97 +
  98 + if(j==0) printf("D%-2d p%-2d:", i,j);
  99 + else printf(" p%-2d:", j);
  100 + for(x = 0; x < psize; x +=4) {
  101 + memcpy(&l, data[i]+j*psize+x, sizeof(long));
  102 + printf(" %08lx", l);
  103 + }
  104 + printf(" ");
  105 + }
  106 + else printf("%*s", sp, "");
  107 + if(i < m) {
  108 + if(j==0) printf("C%-2d p%-2d:", i,j);
  109 + else printf(" p%-2d:", j);
  110 + for(x = 0; x < psize; x +=4) {
  111 + memcpy(&l, coding[i]+j*psize+x, sizeof(long));
  112 + printf(" %08lx", l);
  113 + }
  114 + }
  115 + printf("\n");
  116 + }
  117 + }
  118 +
  119 + printf("\n");
  120 +}
  121 +
  122 +int main(int argc, char **argv)
  123 +{
  124 + long l;
  125 + int k, w, i, j, m;
  126 + int *matrix, *bitmatrix;
  127 + char **data, **coding, **ptrs;
  128 + int **smart;
  129 + int no;
  130 + int *erasures, *erased;
  131 + double stats[3];
  132 +
  133 + if (argc != 4) usage(NULL);
  134 + if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
  135 + if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
  136 + if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
  137 + if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
  138 +
  139 + matrix = cauchy_good_general_coding_matrix(k, m, w);
  140 + if (matrix == NULL) {
  141 + usage("couldn't make coding matrix");
  142 + }
  143 +
  144 + no = 0;
  145 + for (i = 0; i < k*m; i++) {
  146 + no += cauchy_n_ones(matrix[i], w);
  147 + }
  148 + printf("Matrix has %d ones\n\n", no);
  149 + jerasure_print_matrix(matrix, m, k, w);
  150 + printf("\n");
  151 + bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
  152 +
  153 + smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
  154 +
  155 + srand48(0);
  156 + data = talloc(char *, k);
  157 + for (i = 0; i < k; i++) {
  158 + data[i] = talloc(char, sizeof(long)*w);
  159 + for (j = 0; j < w; j++) {
  160 + l = lrand48();
  161 + memcpy(data[i]+j*sizeof(long), &l, sizeof(long));
  162 + }
  163 + }
  164 +
  165 + coding = talloc(char *, m);
  166 + for (i = 0; i < m; i++) {
  167 + coding[i] = talloc(char, sizeof(long)*w);
  168 + }
  169 +
  170 + jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
  171 + jerasure_get_stats(stats);
  172 + printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
  173 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  174 +
  175 + erasures = talloc(int, (m+1));
  176 + erased = talloc(int, (k+m));
  177 + for (i = 0; i < m+k; i++) erased[i] = 0;
  178 + for (i = 0; i < m; ) {
  179 + erasures[i] = lrand48()%(k+m);
  180 + if (erased[erasures[i]] == 0) {
  181 + erased[erasures[i]] = 1;
  182 + bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
  183 + i++;
  184 + }
  185 + }
  186 + erasures[i] = -1;
  187 +
  188 + printf("Erased %d random pieces of data/coding:\n\n", m);
  189 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  190 +
  191 + jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
  192 + jerasure_get_stats(stats);
  193 +
  194 + printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
  195 + print_data_and_coding(k, m, w, sizeof(long), data, coding);
  196 +
  197 + return 0;
  198 +}
... ...
Examples/decoder.c 0 → 100755
  1 +++ a/Examples/decoder.c
... ... @@ -0,0 +1,399 @@
  1 +/* Examples/decoder.c
  2 + * Catherine D. Schuman, James S. Plank
  3 +
  4 +Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure Coding Techniques
  5 +
  6 +Revision 1.2A
  7 +May 24, 2011
  8 +
  9 +James S. Plank
  10 +Department of Electrical Engineering and Computer Science
  11 +University of Tennessee
  12 +Knoxville, TN 37996
  13 +plank@cs.utk.edu
  14 +
  15 +Copyright (c) 2011, James S. Plank
  16 +All rights reserved.
  17 +
  18 +Redistribution and use in source and binary forms, with or without
  19 +modification, are permitted provided that the following conditions
  20 +are met:
  21 +
  22 + - Redistributions of source code must retain the above copyright
  23 + notice, this list of conditions and the following disclaimer.
  24 +
  25 + - Redistributions in binary form must reproduce the above copyright
  26 + notice, this list of conditions and the following disclaimer in
  27 + the documentation and/or other materials provided with the
  28 + distribution.
  29 +
  30 + - Neither the name of the University of Tennessee nor the names of its
  31 + contributors may be used to endorse or promote products derived
  32 + from this software without specific prior written permission.
  33 +
  34 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38 +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40 +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  41 +OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  42 +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43 +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  44 +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45 +POSSIBILITY OF SUCH DAMAGE.
  46 +
  47 +
  48 +*/
  49 +
  50 +/*
  51 +This program takes as input an inputfile, k, m, a coding
  52 +technique, w, and packetsize. It is the companion program
  53 +of encoder.c, which creates k+m files. This program assumes
  54 +that up to m erasures have occurred in the k+m files. It
  55 +reads in the k+m files or marks the file as erased. It then
  56 +recreates the original file and creates a new file with the
  57 +suffix "decoded" with the decoded contents of the file.
  58 +
  59 +This program does not error check command line arguments because
  60 +it is assumed that encoder.c has been called previously with the
  61 +same arguments, and encoder.c does error check.
  62 +*/
  63 +
  64 +#include <stdio.h>
  65 +#include <stdlib.h>
  66 +#include <string.h>
  67 +#include <sys/time.h>
  68 +#include <sys/stat.h>
  69 +#include <signal.h>
  70 +#include "jerasure.h"
  71 +#include "reed_sol.h"
  72 +#include "galois.h"
  73 +#include "cauchy.h"
  74 +#include "liberation.h"
  75 +
  76 +#define N 10
  77 +
  78 +enum Coding_Technique {Reed_Sol_Van, Reed_Sol_R6_Op, Cauchy_Orig, Cauchy_Good, Liberation, Blaum_Roth, Liber8tion, RDP, EVENODD, No_Coding};
  79 +
  80 +char *Methods[N] = {"reed_sol_van", "reed_sol_r6_op", "cauchy_orig", "cauchy_good", "liberation", "blaum_roth", "liber8tion", "rdp", "evenodd", "no_coding"};
  81 +
  82 +/* Global variables for signal handler */
  83 +enum Coding_Technique method;
  84 +int readins, n;
  85 +
  86 +/* Function prototype */
  87 +void ctrl_bs_handler(int dummy);
  88 +
  89 +int main (int argc, char **argv) {
  90 + FILE *fp; // File pointer
  91 +
  92 + /* Jerasure arguments */
  93 + char **data;
  94 + char **coding;
  95 + int *erasures;
  96 + int *erased;
  97 + int *matrix;
  98 + int *bitmatrix;
  99 +
  100 + /* Parameters */
  101 + int k, m, w, packetsize, buffersize;
  102 + enum Coding_Technique tech;
  103 + char *c_tech;
  104 +
  105 + int i, j; // loop control variables
  106 + int blocksize; // size of individual files
  107 + int origsize; // size of file before padding
  108 + int total; // used to write data, not padding to file
  109 + struct stat status; // used to find size of individual files
  110 + int numerased; // number of erased files
  111 +
  112 + /* Used to recreate file names */
  113 + char *temp;
  114 + char *cs1, *cs2, *extension;
  115 + char *fname;
  116 + int md;
  117 + char *curdir;
  118 +
  119 + /* Used to time decoding */
  120 + struct timeval t1, t2, t3, t4;
  121 + struct timezone tz;
  122 + double tsec;
  123 + double totalsec;
  124 +
  125 +
  126 + signal(SIGQUIT, ctrl_bs_handler);
  127 +
  128 + matrix = NULL;
  129 + bitmatrix = NULL;
  130 + totalsec = 0.0;
  131 +
  132 + /* Start timing */
  133 + gettimeofday(&t1, &tz);
  134 +
  135 + /* Error checking parameters */
  136 + if (argc != 2) {
  137 + fprintf(stderr, "usage: inputfile\n");
  138 + exit(0);
  139 + }
  140 + curdir = (char *)malloc(sizeof(char)*100);
  141 + getcwd(curdir, 100);
  142 +
  143 + /* Begin recreation of file names */
  144 + cs1 = (char*)malloc(sizeof(char)*strlen(argv[1]));
  145 + cs2 = strrchr(argv[1], '/');
  146 + if (cs2 != NULL) {
  147 + cs2++;
  148 + strcpy(cs1, cs2);
  149 + }
  150 + else {
  151 + strcpy(cs1, argv[1]);
  152 + }
  153 + cs2 = strchr(cs1, '.');
  154 + if (cs2 != NULL) {
  155 + extension = strdup(cs2);
  156 + *cs2 = '\0';
  157 + } else {
  158 + extension = strdup("");
  159 + }
  160 + fname = (char *)malloc(sizeof(char*)*(100+strlen(argv[1])+10));
  161 +
  162 + /* Read in parameters from metadata file */
  163 + sprintf(fname, "%s/Coding/%s_meta.txt", curdir, cs1);
  164 +
  165 + fp = fopen(fname, "rb");
  166 + if (fp == NULL) {
  167 + fprintf(stderr, "Error: no metadata file %s\n", fname);
  168 + exit(1);
  169 + }
  170 + temp = (char *)malloc(sizeof(char)*(strlen(argv[1])+10));
  171 + fscanf(fp, "%s", temp);
  172 +
  173 + if (fscanf(fp, "%d", &origsize) != 1) {
  174 + fprintf(stderr, "Original size is not valid\n");
  175 + exit(0);
  176 + }
  177 + if (fscanf(fp, "%d %d %d %d %d", &k, &m, &w, &packetsize, &buffersize) != 5) {
  178 + fprintf(stderr, "Parameters are not correct\n");
  179 + exit(0);
  180 + }
  181 + c_tech = (char *)malloc(sizeof(char)*(strlen(argv[1])+10));
  182 + fscanf(fp, "%s", c_tech);
  183 + fscanf(fp, "%d", &tech);
  184 + method = tech;
  185 + fscanf(fp, "%d", &readins);
  186 + fclose(fp);
  187 +
  188 + /* Allocate memory */
  189 + erased = (int *)malloc(sizeof(int)*(k+m));
  190 + for (i = 0; i < k+m; i++)
  191 + erased[i] = 0;
  192 + erasures = (int *)malloc(sizeof(int)*(k+m));
  193 +
  194 + data = (char **)malloc(sizeof(char *)*k);
  195 + coding = (char **)malloc(sizeof(char *)*m);
  196 + if (buffersize != origsize) {
  197 + for (i = 0; i < k; i++) {
  198 + data[i] = (char *)malloc(sizeof(char)*(buffersize/k));
  199 + }
  200 + for (i = 0; i < m; i++) {
  201 + coding[i] = (char *)malloc(sizeof(char)*(buffersize/k));
  202 + }
  203 + blocksize = buffersize/k;
  204 + }
  205 +
  206 + sprintf(temp, "%d", k);
  207 + md = strlen(temp);
  208 + gettimeofday(&t3, &tz);
  209 +
  210 + /* Create coding matrix or bitmatrix */
  211 + switch(tech) {
  212 + case No_Coding:
  213 + break;
  214 + case Reed_Sol_Van:
  215 + matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
  216 + break;
  217 + case Reed_Sol_R6_Op:
  218 + matrix = reed_sol_r6_coding_matrix(k, w);
  219 + break;
  220 + case Cauchy_Orig:
  221 + matrix = cauchy_original_coding_matrix(k, m, w);
  222 + bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
  223 + break;
  224 + case Cauchy_Good:
  225 + matrix = cauchy_good_general_coding_matrix(k, m, w);
  226 + bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
  227 + break;
  228 + case Liberation:
  229 + bitmatrix = liberation_coding_bitmatrix(k, w);
  230 + break;
  231 + case Blaum_Roth:
  232 + bitmatrix = blaum_roth_coding_bitmatrix(k, w);
  233 + break;
  234 + case Liber8tion:
  235 + bitmatrix = liber8tion_coding_bitmatrix(k);
  236 + }
  237 + gettimeofday(&t4, &tz);
  238 + tsec = 0.0;
  239 + tsec += t4.tv_usec;
  240 + tsec -= t3.tv_usec;
  241 + tsec /= 1000000.0;
  242 + tsec += t4.tv_sec;
  243 + tsec -= t3.tv_sec;
  244 + totalsec += tsec;
  245 +
  246 + /* Begin decoding process */
  247 + total = 0;
  248 + n = 1;
  249 + while (n <= readins) {
  250 + numerased = 0;
  251 + /* Open files, check for erasures, read in data/coding */
  252 + for (i = 1; i <= k; i++) {
  253 + sprintf(fname, "%s/Coding/%s_k%0*d%s", curdir, cs1, md, i, extension);
  254 + fp = fopen(fname, "rb");
  255 + if (fp == NULL) {
  256 + erased[i-1] = 1;
  257 + erasures[numerased] = i-1;
  258 + numerased++;
  259 + //printf("%s failed\n", fname);
  260 + }
  261 + else {
  262 + if (buffersize == origsize) {
  263 + stat(fname, &status);
  264 + blocksize = status.st_size;
  265 + data[i-1] = (char *)malloc(sizeof(char)*blocksize);
  266 + fread(data[i-1], sizeof(char), blocksize, fp);
  267 + }
  268 + else {
  269 + fseek(fp, blocksize*(n-1), SEEK_SET);
  270 + fread(data[i-1], sizeof(char), buffersize/k, fp);
  271 + }
  272 + fclose(fp);
  273 + }
  274 + }
  275 + for (i = 1; i <= m; i++) {
  276 + sprintf(fname, "%s/Coding/%s_m%0*d%s", curdir, cs1, md, i, extension);
  277 + fp = fopen(fname, "rb");
  278 + if (fp == NULL) {
  279 + erased[k+(i-1)] = 1;
  280 + erasures[numerased] = k+i-1;
  281 + numerased++;
  282 + //printf("%s failed\n", fname);
  283 + }
  284 + else {
  285 + if (buffersize == origsize) {
  286 + stat(fname, &status);
  287 + blocksize = status.st_size;
  288 + coding[i-1] = (char *)malloc(sizeof(char)*blocksize);
  289 + fread(coding[i-1], sizeof(char), blocksize, fp);
  290 + }
  291 + else {
  292 + fseek(fp, blocksize*(n-1), SEEK_SET);
  293 + fread(coding[i-1], sizeof(char), blocksize, fp);
  294 + }
  295 + fclose(fp);
  296 + }
  297 + }
  298 + /* Finish allocating data/coding if needed */
  299 + if (n == 1) {
  300 + for (i = 0; i < numerased; i++) {
  301 + if (erasures[i] < k) {
  302 + data[erasures[i]] = (char *)malloc(sizeof(char)*blocksize);
  303 + }
  304 + else {
  305 + coding[erasures[i]-k] = (char *)malloc(sizeof(char)*blocksize);