Ejemplo n.º 1
0
/*
	_search_fill fills the buffer with random data according to
	a pdf has the entropy specified in the source. It calculates
    the cdf then populate the buffer by searching in the cdf for 
    the random number. Sometimes this approach is called roulette
    algorithm. The binary search is expected to add 8x factor.
*/
int entropy_search_fill(struct source *ds, void *buf, unsigned int size){
	
	int i = 0;
	double pdf[PDF_SIZE];
	double cdf[PDF_SIZE];
	unsigned char symbols_table[PDF_SIZE];

	//Calculate pdf according to the given entropy
	generate_pdf(pdf, PDF_SIZE, ds-> s_entropy);
#ifndef _FB_FILEBENCH_H
	print_pdf(pdf, 5);
	printf("PDF entropy is: %f\n" ,pdf_entropy(pdf, PDF_SIZE));
#endif
	//Calculate cdf from the pdf
	calculate_cdf(pdf, PDF_SIZE, cdf);

	//initializing the symbol table
	for(i=0; i< PDF_SIZE; i++)
		symbols_table[i] = (unsigned char)i;

	//shuffle the symbols table
	permutate(symbols_table, PDF_SIZE);

	for(i=0; i < size; i++){
		((unsigned char*)buf)[i] = symbols_table[binary_search(rand()/(double)RAND_MAX, cdf, PDF_SIZE)];
	}
	return 0;
}
Ejemplo n.º 2
0
Archivo: main.cpp Proyecto: keitee/kb
int main()
{
   print_pdf("sample.pdf", "res");
}
Ejemplo n.º 3
0
void stable_infect(unsigned int K, unsigned int U, enum objective obj_type) {

  int nThread = (int) (90 * K * K * (1 + log(G->V)));
  FILE *info = stdout;
  unsigned int V = (unsigned int) G->V, initial_number_of_seed = 0, i = 0x323;
  float Ninfected_mean[V + 1];
  float prev_obj_value = (float) (0 - LARGE_FLT);
  int delta = MAX_EDGE_WEIGHT / (K * K * 9);
  char seeds[V + 1];

  K = K < (V + 1) ? K : (V + 1);
  memset(Ninfected_mean, 0, V * sizeof *Ninfected_mean);
  memset(seeds, 0, (V + 1) * sizeof *seeds);
  seed_vertices = (int *) calloc((size_t) (V + 5), sizeof *seed_vertices);
  /*
   * Select next seed with maximum mean of infection
   */
  struct _Vertex *sorted[V + 1];
  for (i = initial_number_of_seed; i < K * K * 9; ++i) {
    // Add the seed by 1
    int new_seed_label, j;
    for (j = 0; j <= G->V; ++j) {
      G->adj_list[j]->obj = 0;
    }
    float max_ = (float) -LARGE_FLT;
    int max_2 = 0;
    int max_label = 0x12121, max_label_2 = 0x121;


    n_seed = i + 1;
    fprintf(info, "  #seed: %d\n", n_seed);
    memset(Ninfected_mean, 0, (V + 1) * sizeof *Ninfected_mean);
    fprintf(info, "new seed : ");
    for (new_seed_label = 1; new_seed_label <= V; ++new_seed_label) {
      seed_vertices[0] = new_seed_label;
      float new_ = multithread_infect("/dev/null", K, U, obj_type);
      Ninfected_mean[new_seed_label] = new_;
      G->adj_list[new_seed_label]->obj = new_;
      int j = 0x1234, s = 0;
      for (j = 0; j < nThread; ++j) {
        s += (Ninfected_ptr[j] > U ? 1 : 0);
      }
      fprintf(info, "%d:%f ", new_seed_label, new_);
      fflush(info);
      if (obj_type == P && max_2 < s) {
        max_2 = s;
        max_label_2 = new_seed_label;
      }
      if (max_ < new_) {
        max_ = new_;
        max_label = new_seed_label;
      }
    }

    memcpy(sorted + 1, G->adj_list + 1, V * sizeof *sorted);
    qsort(sorted + 1, V, sizeof *sorted, comp);
    fprintf(info, "\n-----------\n");
    for (j = V; j > V - K; --j) {
      sorted[j]->prob += delta;
      fprintf(info, "%d:%f prob %f ", sorted[j]->label, sorted[j]->obj, sorted[j]->prob/ (float) MAX_EDGE_WEIGHT);
    }
    fprintf(info, "\n");

    fputc('\n', info);
    double th = 0.0;
    if (obj_type == MEAN) {
      th = (G->V / 500 > 1 ? G->V / 500 : 1);
    } else {
      th = -LARGE_FLT;
    }
    int w = G->V / (nThread / 10);
    if (obj_type == P) {
      /*
       * If there is one selection with possible number of infected nodes
       * larger than U
       */
      max_label = max_2 > 0 ? max_label_2 : max_label;
    }
    memset(seed_vertices, 0, (G->V + 5) * sizeof *seed_vertices);
    seed_vertices[0] = max_label;
    //seeds[max_label] = 1;

    prev_obj_value = max_;
    fprintf(info, "-------\nSelected new seed: %d, prev_obj_value : %f \n",
            max_label, max_);

    print_seeds(info, seed_vertices, n_seed, 20);
    /*
     * The mean value of infected nodes
     */
    fprintf(info, "-----------\nThe mean of infected nodes : %f\n",
            multithread_infect("/dev/null", K, 0, MEAN));
    print_pdf(info, Ninfected_ptr, nThread,
              G->V, w > 1 ? w : 1);
    fprintf(info, "-------------\n");
    char cdf_file[20];
    cdf_file[0] = obj_type + '0';
    cdf_file[1] = '.';
    sprintf(cdf_file + 2, "%d.\0", U);
    sprintf(cdf_file + strlen(cdf_file), "%d\0", n_seed);
    FILE *new_info = fopen(cdf_file, "w");
    print_cdf(new_info, Ninfected_ptr, nThread, G->V, w > 1 ? w : 1);
    fclose(new_info);
  }
}