1
Fork 0

fixed some mistakes in BRZ algorithm

This commit is contained in:
fc_botelho 2005-07-29 00:00:01 +00:00
parent 3e4b8fdfcb
commit 77d05871bf
3 changed files with 75 additions and 33 deletions

View File

@ -99,7 +99,7 @@ cmph_t *bmz_new(cmph_config_t *mph, float c)
bmz->hashes[0] = hash_state_new(bmz->hashfuncs[0], bmz->n); bmz->hashes[0] = hash_state_new(bmz->hashfuncs[0], bmz->n);
DEBUGP("hash function 2\n"); DEBUGP("hash function 2\n");
bmz->hashes[1] = hash_state_new(bmz->hashfuncs[1], bmz->n); bmz->hashes[1] = hash_state_new(bmz->hashfuncs[1], bmz->n);
DEBUGP("Generating edges\n"); DEBUGP("Generating edges\n");
ok = bmz_gen_edges(mph); ok = bmz_gen_edges(mph);
if (!ok) if (!ok)
{ {
@ -413,7 +413,6 @@ static int bmz_gen_edges(cmph_config_t *mph)
cmph_uint32 e; cmph_uint32 e;
bmz_config_data_t *bmz = (bmz_config_data_t *)mph->data; bmz_config_data_t *bmz = (bmz_config_data_t *)mph->data;
cmph_uint8 multiple_edges = 0; cmph_uint8 multiple_edges = 0;
DEBUGP("Generating edges for %u vertices\n", bmz->n); DEBUGP("Generating edges for %u vertices\n", bmz->n);
graph_clear_edges(bmz->graph); graph_clear_edges(bmz->graph);
mph->key_source->rewind(mph->key_source->data); mph->key_source->rewind(mph->key_source->data);
@ -421,8 +420,10 @@ static int bmz_gen_edges(cmph_config_t *mph)
{ {
cmph_uint32 h1, h2; cmph_uint32 h1, h2;
cmph_uint32 keylen; cmph_uint32 keylen;
char *key; char *key = NULL;
mph->key_source->read(mph->key_source->data, &key, &keylen); mph->key_source->read(mph->key_source->data, &key, &keylen);
// if (key == NULL)fprintf(stderr, "key = %s -- read BMZ\n", key);
h1 = hash(bmz->hashes[0], key, keylen) % bmz->n; h1 = hash(bmz->hashes[0], key, keylen) % bmz->n;
h2 = hash(bmz->hashes[1], key, keylen) % bmz->n; h2 = hash(bmz->hashes[1], key, keylen) % bmz->n;
if (h1 == h2) if (++h2 >= bmz->n) h2 = 0; if (h1 == h2) if (++h2 >= bmz->n) h2 = 0;
@ -434,6 +435,7 @@ static int bmz_gen_edges(cmph_config_t *mph)
} }
DEBUGP("Adding edge: %u -> %u for key %s\n", h1, h2, key); DEBUGP("Adding edge: %u -> %u for key %s\n", h1, h2, key);
mph->key_source->dispose(mph->key_source->data, key, keylen); mph->key_source->dispose(mph->key_source->data, key, keylen);
// fprintf(stderr, "key = %s -- dispose BMZ\n", key);
multiple_edges = graph_contains_edge(bmz->graph, h1, h2); multiple_edges = graph_contains_edge(bmz->graph, h1, h2);
if (mph->verbosity && multiple_edges) fprintf(stderr, "A non simple graph was generated\n"); if (mph->verbosity && multiple_edges) fprintf(stderr, "A non simple graph was generated\n");
if (multiple_edges) return 0; // checking multiple edge restriction. if (multiple_edges) return 0; // checking multiple edge restriction.

View File

@ -19,7 +19,7 @@
static int brz_before_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint32 * diskoffset); static int brz_before_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint32 * diskoffset);
static void brz_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint32 * diskoffset, FILE * graphs_fd); static void brz_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint32 * diskoffset, FILE * graphs_fd);
static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys); static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys, cmph_uint32 h3);
static void brz_destroy_keys_vd(char ** keys_vd, cmph_uint8 nkeys); static void brz_destroy_keys_vd(char ** keys_vd, cmph_uint8 nkeys);
static void brz_copy_partial_mphf(brz_config_data_t *brz, bmz_data_t * bmzf, cmph_uint32 index); static void brz_copy_partial_mphf(brz_config_data_t *brz, bmz_data_t * bmzf, cmph_uint32 index);
@ -78,9 +78,9 @@ cmph_t *brz_new(cmph_config_t *mph, float c)
DEBUGP("c: %f\n", c); DEBUGP("c: %f\n", c);
brz_config_data_t *brz = (brz_config_data_t *)mph->data; brz_config_data_t *brz = (brz_config_data_t *)mph->data;
brz->m = mph->key_source->nkeys; brz->m = mph->key_source->nkeys;
DEBUGP("m: %f\n", brz->m); DEBUGP("m: %u\n", brz->m);
brz->k = ceil(brz->m/128); brz->k = ceil(brz->m/128);
DEBUGP("k: %f\n", brz->k); DEBUGP("k: %u\n", brz->k);
brz->size = (cmph_uint8 *) malloc(sizeof(cmph_uint8)*brz->k); brz->size = (cmph_uint8 *) malloc(sizeof(cmph_uint8)*brz->k);
brz->offset = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*brz->k); brz->offset = (cmph_uint32 *)malloc(sizeof(cmph_uint32)*brz->k);
@ -128,7 +128,7 @@ cmph_t *brz_new(cmph_config_t *mph, float c)
return NULL; return NULL;
} }
graphs_fd = fopen("/colecao/fbotelho/cmph.tmp", "wb+"); graphs_fd = fopen("/colecao/fbotelho/cmph.tmp", "wb");
if (graphs_fd == NULL) if (graphs_fd == NULL)
{ {
free(brz->size); free(brz->size);
@ -138,12 +138,13 @@ cmph_t *brz_new(cmph_config_t *mph, float c)
fprintf(stderr, "Unable to open file %s\n", "/colecao/fbotelho/cmph.tmp"); fprintf(stderr, "Unable to open file %s\n", "/colecao/fbotelho/cmph.tmp");
return NULL; return NULL;
} }
// Clustering the keys by graph id. // Clustering the keys by graph id.
brz_gen_graphs(mph, disksize, diskoffset, graphs_fd); brz_gen_graphs(mph, disksize, diskoffset, graphs_fd);
free(disksize); free(disksize);
free(diskoffset); free(diskoffset);
DEBUGP("Graphs generated\n"); DEBUGP("Graphs generated\n");
fclose(graphs_fd);
graphs_fd = fopen("/colecao/fbotelho/cmph.tmp", "rb");
// codigo do algoritmo... // codigo do algoritmo...
brz->h1 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); brz->h1 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k);
brz->h2 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k); brz->h2 = (hash_state_t **)malloc(sizeof(hash_state_t *)*brz->k);
@ -151,29 +152,26 @@ cmph_t *brz_new(cmph_config_t *mph, float c)
for(i = 0; i < brz->k; i++) for(i = 0; i < brz->k; i++)
{ {
cmph_uint32 j;
bmz_data_t * bmzf = NULL; bmz_data_t * bmzf = NULL;
if (brz->size[i] == 0) continue; if (brz->size[i] == 0) continue;
keys_vd = brz_read_keys_vd(graphs_fd, brz->size[i]); keys_vd = brz_read_keys_vd(graphs_fd, brz->size[i], i);
// Source of keys // Source of keys
source = cmph_io_vector_adapter(keys_vd, (cmph_uint32)brz->size[i]); source = cmph_io_vector_adapter(keys_vd, (cmph_uint32)brz->size[i]);
config = cmph_config_new(source); config = cmph_config_new(source);
cmph_config_set_algo(config, CMPH_BMZ); cmph_config_set_algo(config, CMPH_BMZ);
cmph_config_set_graphsize(config, c); cmph_config_set_graphsize(config, c);
mphf_tmp = cmph_new(config); mphf_tmp = cmph_new(config);
bmzf = (bmz_data_t *)mphf_tmp->data; bmzf = (bmz_data_t *)mphf_tmp->data;
brz_copy_partial_mphf(brz, bmzf, i); // implementar brz_copy_partial_mphf(brz, bmzf, i); // implementar
cmph_config_destroy(config); cmph_config_destroy(config);
brz_destroy_keys_vd(keys_vd, brz->size[i]); brz_destroy_keys_vd(keys_vd, brz->size[i]);
free(keys_vd); free(keys_vd);
cmph_destroy(mphf_tmp); cmph_destroy(mphf_tmp);
free(source); free(source);
} }
fclose(graphs_fd); fclose(graphs_fd);
// Generating a mphf // Generating a mphf
mphf = (cmph_t *)malloc(sizeof(cmph_t)); mphf = (cmph_t *)malloc(sizeof(cmph_t));
@ -216,15 +214,29 @@ static int brz_before_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmp
char *key; char *key;
mph->key_source->read(mph->key_source->data, &key, &keylen); mph->key_source->read(mph->key_source->data, &key, &keylen);
h3 = hash(brz->h3, key, keylen) % brz->k; h3 = hash(brz->h3, key, keylen) % brz->k;
if(h3 == 6)
{
DEBUGP("key = %s\n", key);
DEBUGP("keylen = %u\n", keylen + 1);
}
mph->key_source->dispose(mph->key_source->data, key, keylen); mph->key_source->dispose(mph->key_source->data, key, keylen);
if (brz->size[h3] == 255) return 0; if (brz->size[h3] == 255) return 0;
brz->size[h3] = brz->size[h3] + 1; brz->size[h3] = brz->size[h3] + 1;
disksize[h3] = disksize[h3] + keylen + 1; disksize[h3] = disksize[h3] + keylen + 1;
// if(h3 == 6)
// {
// DEBUGP("disksize[%u]=%u \n", h3, disksize[h3]);
// }
} }
fprintf(stderr, "size:%u offset: %u\n", brz->size[0], brz->offset[0]);
for (e = 1; e < brz->k; ++e) for (e = 1; e < brz->k; ++e)
{ {
brz->offset[e] = brz->size[e-1] + brz->size[e]; brz->offset[e] = brz->size[e-1] + brz->offset[e-1];
diskoffset[e] = disksize[e-1] + disksize[e]; diskoffset[e] = disksize[e-1] + diskoffset[e-1];
DEBUGP("disksize[%u]=%u diskoffset[%u]: %u\n", e, disksize[e], e, diskoffset[e]);
DEBUGP("size[%u]=%u offset[%u]: %u\n", e, brz->size[e], e, brz->offset[e]);
} }
return 1; return 1;
} }
@ -243,14 +255,23 @@ static void brz_gen_graphs(cmph_config_t *mph, cmph_uint32 * disksize, cmph_uint
char *key; char *key;
mph->key_source->read(mph->key_source->data, &key, &keylen); mph->key_source->read(mph->key_source->data, &key, &keylen);
h3 = hash(brz->h3, key, keylen) % brz->k; h3 = hash(brz->h3, key, keylen) % brz->k;
mph->key_source->dispose(mph->key_source->data, key, keylen); if(h3 == 6)
{
DEBUGP("key = %s\n", key);
DEBUGP("keylen = %u\n", keylen + 1);
}
fseek(graphs_fd, diskoffset[h3], SEEK_SET); fseek(graphs_fd, diskoffset[h3], SEEK_SET);
fwrite(key, sizeof(char), keylen + 1, graphs_fd); fwrite(key, sizeof(char), keylen + 1, graphs_fd);
if(h3 == 6)
{
DEBUGP("diskoffset[%u]=%u \n", h3, diskoffset[h3]);
}
diskoffset[h3] = diskoffset[h3] + keylen + 1; diskoffset[h3] = diskoffset[h3] + keylen + 1;
mph->key_source->dispose(mph->key_source->data, key, keylen);
} }
} }
static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys) static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys, cmph_uint32 h3)
{ {
char ** keys_vd = (char **)malloc(sizeof(char *)*nkeys); char ** keys_vd = (char **)malloc(sizeof(char *)*nkeys);
cmph_uint8 i; cmph_uint8 i;
@ -270,8 +291,9 @@ static char ** brz_read_keys_vd(FILE * graphs_fd, cmph_uint8 nkeys)
} }
keys_vd[i] = (char *)malloc(strlen(buf) + 1); keys_vd[i] = (char *)malloc(strlen(buf) + 1);
strcpy(keys_vd[i], buf); strcpy(keys_vd[i], buf);
free(buf); if(h3 == 6) DEBUGP("key = %s\n", keys_vd[i]);
} free(buf);
}
return keys_vd; return keys_vd;
} }
@ -389,14 +411,19 @@ void brz_destroy(cmph_t *mphf)
{ {
cmph_uint32 i; cmph_uint32 i;
brz_data_t *data = (brz_data_t *)mphf->data; brz_data_t *data = (brz_data_t *)mphf->data;
fprintf(stderr, "MERDAAAAAA %u\n", data->h3->hashfunc);
for(i = 0; i < data->k; i++) for(i = 0; i < data->k; i++)
{ {
free(data->g[i]); free(data->g[i]);
fprintf(stderr, "MERDAAAAAA1 %u\n", data->h3->hashfunc);
hash_state_destroy(data->h1[i]); hash_state_destroy(data->h1[i]);
fprintf(stderr, "MERDAAAAAA2 %u\n", data->h3->hashfunc);
hash_state_destroy(data->h2[i]); hash_state_destroy(data->h2[i]);
fprintf(stderr, "MERDAAAAAA3 %u\n", data->h3->hashfunc);
} }
fprintf(stderr, "MERDAAAAAA %u\n", data->h3->hashfunc);
hash_state_destroy(data->h3); hash_state_destroy(data->h3);
fprintf(stderr, "MERDAAAAAA1FAB\n");
free(data->g); free(data->g);
free(data->h1); free(data->h1);
free(data->h2); free(data->h2);

View File

@ -41,12 +41,19 @@ static int key_nlfile_read(void *data, char **key, cmph_uint32 *keylen)
static int key_vector_read(void *data, char **key, cmph_uint32 *keylen) static int key_vector_read(void *data, char **key, cmph_uint32 *keylen)
{ {
char **keys = (char **)data; char **keys_vd = (char **)data;
if (keys + position == NULL) return -1; if (keys_vd + position == NULL) return -1;
*keylen = strlen(*(keys + position)); *keylen = strlen(*(keys_vd + position));
*key = (char *)malloc(*keylen); *key = (char *)malloc(*keylen + 1);
strcpy(*key, *(keys + position)); strcpy(*key, *(keys_vd + position));
position ++; position ++;
/* char **keys_vd = (char **)data;
if (keys_vd[position] == NULL) return -1;
*keylen = strlen(keys_vd[position]);
*key = (char *)malloc(*keylen + 1);
strcpy(*key, keys_vd[position]);
position ++;*/
return *keylen; return *keylen;
} }
@ -56,6 +63,11 @@ static void key_nlfile_dispose(void *data, char *key, cmph_uint32 keylen)
free(key); free(key);
} }
static void key_vector_dispose(void *data, char *key, cmph_uint32 keylen)
{
free(key);
}
static void key_nlfile_rewind(void *data) static void key_nlfile_rewind(void *data)
{ {
FILE *fd = (FILE *)data; FILE *fd = (FILE *)data;
@ -67,6 +79,7 @@ static void key_vector_rewind(void *data)
position = 0; position = 0;
} }
static cmph_uint32 count_nlfile_keys(FILE *fd) static cmph_uint32 count_nlfile_keys(FILE *fd)
{ {
cmph_uint32 count = 0; cmph_uint32 count = 0;
@ -114,7 +127,7 @@ cmph_io_adapter_t *cmph_io_vector_adapter(char ** vector, cmph_uint32 nkeys)
key_source->data = (void *)vector; key_source->data = (void *)vector;
key_source->nkeys = nkeys; key_source->nkeys = nkeys;
key_source->read = key_vector_read; key_source->read = key_vector_read;
key_source->dispose = key_nlfile_dispose; key_source->dispose = key_vector_dispose;
key_source->rewind = key_vector_rewind; key_source->rewind = key_vector_rewind;
return key_source; return key_source;
} }
@ -136,13 +149,13 @@ void cmph_config_set_algo(cmph_config_t *mph, CMPH_ALGO algo)
switch (mph->algo) switch (mph->algo)
{ {
case CMPH_CHM: case CMPH_CHM:
chm_config_destroy(mph->data); chm_config_destroy(mph);
break; break;
case CMPH_BMZ: case CMPH_BMZ:
bmz_config_destroy(mph->data); bmz_config_destroy(mph);
break; break;
case CMPH_BRZ: case CMPH_BRZ:
brz_config_destroy(mph->data); brz_config_destroy(mph);
break; break;
default: default:
assert(0); assert(0);