Ejemplo n.º 1
0
double compute_utilization(flexsched_solution_t flex_soln)
{
    int i;
    double total_capacity = 0.0;
    double total_alloc = 0.0;

    for (i = 0; i < flex_soln->prob->num_nodes; i++) {
        total_capacity +=
            double_array_sum(flex_soln->prob->nodes[i]->total_capacities,
                flex_soln->prob->num_resources);
    }

    if (total_capacity <= 0.0) return 1.0;

    for (i = 0; i < flex_soln->prob->num_jobs; i++) {
        total_alloc +=
            double_array_sum(
                flex_soln->prob->jobs[i]->total_rigid_requirements, 
                flex_soln->prob->num_resources) + 
            flex_soln->yields[i] * 
              double_array_sum(flex_soln->prob->jobs[i]->total_fluid_needs,
                    flex_soln->prob->num_resources);
    }

    return (total_alloc / total_capacity);
}
Ejemplo n.º 2
0
void crf_context_alpha_score(crf_context_t *self) {
    double *scale = self->scale_factor->a;

    const double *prev = NULL;
    const double *trans = NULL;
    const double *state = NULL;
    const double *state_trans = NULL;

    const size_t T = self->num_items;
    const size_t L = self->num_labels;

    /* Compute the alpha scores on nodes 0, *).
       alpha[0][j] = state[0][j]

       At this point we have no transition features.
    */
    double *cur = alpha_score(self, 0);
    state = exp_state_score(self, 0);
    double_array_raw_copy(cur, state, L);
    double sum = double_array_sum(cur, L);
    double scale_t = !double_equals(sum, 0.) ? 1. / sum : 1.;
    scale[0] = scale_t;
    double_array_mul(cur, scale[0], L);

    /* Compute the alpha scores on nodes (t, *).
       alpha[t][j] = state[t][j] * \sum_{i} alpha[t-1][i] * state_trans[t][i][j] * trans[i][j]
    */
    for (size_t t = 1; t < T; t++) {
        prev = alpha_score(self, t - 1);
        cur = alpha_score(self, t);
        state = exp_state_score(self, t);

        double_array_zero(cur, L);
        for (size_t i = 0; i < L; i++) {
            trans = exp_trans_score(self, i);
            state_trans = exp_state_trans_score(self, t, i);
            for (size_t j = 0; j < L; j++) {
                cur[j] += prev[i] * trans[j] * state_trans[j];
            }
        }

        double_array_mul_array(cur, state, L);
        sum = double_array_sum(cur, L);
        scale[t] = scale_t = !double_equals(sum, 0.) ? 1. / sum : 1.;
        double_array_mul(cur, scale_t, L);
    }

    /* Compute the logarithm of the normalization factor here.
       norm = 1. / (C[0] * C[1] ... * C[T-1])
       log(norm) = - \sum_{t = 0}^{T-1} log(C[t])
    */
    self->log_norm = -double_array_sum_log(scale, T);
}
Ejemplo n.º 3
0
double LDA::Likelihood(const Corpus &cor, int d, const LdaModel &m, VRealC &gamma,VVRealC &phi) const {
  double alpha_sum = double_array_sum(m.alpha,m.num_topics);
  double gamma_sum = std::accumulate(gamma.begin(), gamma.end(), 0.0);
  double digsum = DiGamma(gamma_sum);
  const int &num = m.num_topics;
  VReal expect(num);
  for (int k = 0; k < num; k++) {
    expect.at(k) = DiGamma(gamma.at(k)) - digsum;
  }

  double l = lgamma(alpha_sum) - lgamma(gamma_sum);
  
  for (int k = 0; k < num; k++) {
    l += ((m.alpha[k] - gamma.at(k)) * expect[k] + lgamma(gamma.at(k)) - lgamma(m.alpha[k]));
    for (size_t n = 0; n < cor.ULen(d); n++) {
      if (phi[n][k] > 0) {
        l += cor.Count(d, n) * phi[n][k] * (expect[k] - log(phi[n][k]) + m.log_prob_w[k][cor.Word(d, n)]);
      }
    }
  }
  return l;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
    FILE *input, *output, *estimates;
    call_scheduler_t *schedulers, *sched;

    int i, j;
    struct timespec time1, time2;
    flexsched_problem_t flex_prob = NULL;
    flexsched_solution_t flex_soln = NULL;
    double elapsed_seconds;
    double expected_minimum_yield, expected_average_yield;
    double *actual_yields;
    double cap_minimum_yield, cap_average_yield;
    double wgt_minimum_yield, wgt_average_yield;
    double equi_minimum_yield, equi_average_yield;

    if ((argc < 2) || (argc > 5)) {
        fprintf(stderr,
            "Usage: %s <scheduler list> [<instance file> [result file]]\n",
            argv[0]);
        fprintf(stderr,"  Known schedulers:  "); 
        for (i = 0; implemented_schedulers[i].name; i++) {
            fprintf(stderr, "%s ", implemented_schedulers[i].name);
        }
        fprintf(stderr, "\n  See README file for scheduler descriptions\n");
	    exit(1);
    }

    // schedulers
    schedulers = parse_scheduler_list(argv[1]);

    // input
    if (argc < 3) {
	    input = stdin;
    } else if (!(input = fopen(argv[2], "r"))) {
	    fprintf(stderr, "Can't open file '%s' for reading\n", argv[2]);
	    exit(1);
    }

    // output 
    if (argc < 4 || !strcmp(argv[3], "-")) {
	    output = stdout;
    } else {
        deactivate_unneeded_schedulers(schedulers, argv[3]);
        if (!(output = fopen(argv[3], "a"))) {
            fprintf(stderr, "Can't open file '%s' for writing/appending\n", 
            argv[3]);
            exit(1);
        }
    }

    if (argc < 5) {
        estimates = NULL;
    } else if (!(estimates = fopen(argv[4], "r"))) {
	    fprintf(stderr, "Can't open file '%s' for reading\n", argv[4]);
	    exit(1);
    }

    flex_prob = new_flexsched_problem(input, estimates);

    fclose(input);
    if (estimates) fclose(estimates);

    actual_yields = calloc(flex_prob->num_jobs, sizeof(double));

    for (sched = schedulers; *sched; sched++) {

        // if not needed, then skip
        if (!(*sched)->active) continue;

        // if we did find it, then run it
        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

        // Call the scheduler
        flex_soln = 
            (*sched)->func(flex_prob, (*sched)->name, (*sched)->options);

        clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

        if (flex_soln->success) {

            // Sanity check the allocation
            if ((*sched)->sanitycheck && 
                sanity_check(flex_prob, flex_soln->mapping, flex_soln->yields)) 
            {
                fprintf(stderr, 
                    "Invalid allocation by algorithm %s on file %s\n",
                    (*sched)->string, argv[2]);
                exit(1);
            }

            if (strcmp((*sched)->name, "LPBOUND")) {
                maximize_minimum_yield(flex_soln);
                if ((*sched)->sanitycheck && 
                    sanity_check(flex_prob, flex_soln->mapping, flex_soln->yields)) 
                {
                    fprintf(stderr, 
                        "Invalid allocation by algorithm %s on file %s after optimization\n",
                        (*sched)->string, argv[2]);
                    exit(1);
                }
                expected_minimum_yield = compute_minimum_yield(flex_soln);
                expected_average_yield = compute_average_yield(flex_soln);

                compute_cap_yields(flex_soln, actual_yields);
                if ((*sched)->sanitycheck && 
                    sanity_check2(flex_prob, flex_soln->mapping, actual_yields))
                {
                    fprintf(stderr, 
                        "Invalid allocation by algorithm %s on file %s after caps\n",
                        (*sched)->string, argv[2]);
                    exit(1);
                }
                cap_minimum_yield = double_array_min(actual_yields, 
                    flex_prob->num_jobs);
                cap_average_yield = double_array_sum(actual_yields,
                        flex_prob->num_jobs) / flex_prob->num_jobs;

                compute_wgt_yields(flex_soln, actual_yields);
                if ((*sched)->sanitycheck && 
                    sanity_check2(flex_prob, flex_soln->mapping, actual_yields)) 
                {
                    fprintf(stderr, 
                        "Invalid allocation by algorithm %s on file %s after weights\n",
                        (*sched)->string, argv[2]);
                    exit(1);
                }
                wgt_minimum_yield = double_array_min(actual_yields, 
                    flex_prob->num_jobs);
                wgt_average_yield = double_array_sum(actual_yields,
                        flex_prob->num_jobs) / flex_prob->num_jobs;

                compute_equi_yields(flex_soln, actual_yields);
                if ((*sched)->sanitycheck && 
                    sanity_check2(flex_prob, flex_soln->mapping, actual_yields)) 
                {
                    fprintf(stderr, 
                        "Invalid allocation by algorithm %s on file %s after equi\n",
                        (*sched)->string, argv[2]);
                    exit(1);
                }
                equi_minimum_yield = double_array_min(actual_yields, 
                    flex_prob->num_jobs);
                equi_average_yield = double_array_sum(actual_yields,
                        flex_prob->num_jobs) / flex_prob->num_jobs;

            }
        }

        // Compute elapsed time
        elapsed_seconds  = (double)(time2.tv_sec - time1.tv_sec);
        elapsed_seconds += (double)(time2.tv_nsec - time1.tv_nsec) / 1e9;
  
        // Print output
        fprintf(output, "%s|", (*sched)->string);
        if (!strcmp((*sched)->name, "LPBOUND")) {
            fprintf(output, "%.3f|", compute_minimum_yield(flex_soln));
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
        } else if (flex_soln->success) {
            fprintf(output, "%.3f|", expected_minimum_yield);
            fprintf(output, "%.3f|", expected_average_yield);
            fprintf(output, "%.3f|", cap_minimum_yield);
            fprintf(output, "%.3f|", cap_average_yield);
            fprintf(output, "%.3f|", wgt_minimum_yield);
            fprintf(output, "%.3f|", wgt_average_yield);
            fprintf(output, "%.3f|", equi_minimum_yield);
            fprintf(output, "%.3f|", equi_average_yield);
        } else {
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
            fprintf(output, "%.3f|", -1.0);
        }
        fprintf(output, "%.3f|", elapsed_seconds);
        fprintf(output, "%s\n", flex_soln->misc_output);

        // clean up
        free_flexsched_solution(flex_soln);

    }

    fclose(output);

    free_flexsched_problem(flex_prob);

    if (*schedulers) {
        free((*schedulers)->string);
    }

    for (sched = schedulers; *sched; sched++) {
        free((*sched)->name);
        free((*sched)->options);
        free(*sched);
    }
    free(schedulers);

    return 0;
}