Esempio n. 1
0
def eval_classifier_instance(job, db, classifier, instance,
                             err_fn, training):
    # Get relevant values from instance.
    oracle = instance.get_string_value(instance.class_index)
    scenario = instance.get_string_value(0)

    # Get default value.
    try:
        baseline = training.default
    except AttributeError:
        training.default = get_one_r(db, training)
        baseline = training.default

    # Classify instance, and convert to params ID.
    prof.start()
    value = classifier.classify_instance(instance)
    elapsed = prof.elapsed()
    attr = instance.dataset.attribute(instance.class_index)
    predicted = attr.value(value)

    correct = 1 if predicted == oracle else 0
    if correct:
        illegal = 0
        refused = 0
        performance, speedup = perf_fn(db, scenario, predicted,
                                       oracle, baseline)
    else:
        # Determine if predicted workgroup size is valid or not. A
        # valid predicted is one which is within the max_wgsize for
        # that particular instance.
        max_wgsize_attr = instance.dataset.attribute_by_name("kern_max_wg_size")
        max_wgsize_attr_index = max_wgsize_attr.index
        max_wgsize = int(instance.get_value(max_wgsize_attr_index))

        wg_c, wg_r = unhash_params(predicted)

        illegal = 0 if wg_c * wg_r < max_wgsize else 1
        if illegal:
            refused = 0
        else:
            try:
                db.runtime(scenario, predicted)
                refused = 0
            except lab.db.Error:
                refused = 1

        if illegal or refused:
            predicted = err_fn(instance, max_wgsize, wg_c, wg_r, baseline)

        performance, speedup = perf_fn(db, scenario, predicted,
                                       oracle, baseline)

    db.add_classification_result(job, classifier,
                                 err_fn, training, scenario,
                                 oracle, predicted, baseline, correct, illegal,
                                 refused, performance, speedup, elapsed)
Esempio n. 2
0
def reshape(db, scenario, max_wgsize, wg_c, wg_r):
    W_legal = db.W_legal(scenario)

    min_distance = float("inf")
    min_param = None

    # Find the *legal* parameter which is closest to the predicted by
    # calculating the distance to each and returning the smallest.
    for param in W_legal:
        p_c, p_r = unhash_params(param)
        distance = math.sqrt((wg_c - p_c) ** 2 + (wg_r - p_r) ** 2)
        if distance < min_distance:
            min_distance = distance
            min_param = param

    return min_param
Esempio n. 3
0
def run_job(i, n, wgsize, program, args):
    wg_c, wg_r = unhash_params(wgsize)

    # Set environment variable.
    os.environ["OMNITUNE_OFFLINE_TRAINING"] = "1"
    os.environ["OMNITUNE_STENCIL_WG_C"] = str(wg_c)
    os.environ["OMNITUNE_STENCIL_WG_R"] = str(wg_r)

    fs.cd(fs.path(experiment.EXAMPLES_BUILD, program))

    cmd_str = "./{} {}".format(program, args.rstrip())
    cmd = cmd_str.split()

    io.info(i, "of", n, " - ", wgsize, "COMMAND:", io.colourise(io.Colours.RED, cmd_str))
    ret, _, _ = system.run(cmd, stdout=system.STDOUT, stderr=system.STDERR)

    if ret:
        print(ret, wgsize, program, args, sep="\t", file=errlog)
    else:
        print(ret, wgsize, program, args, sep="\t", file=runlog)
Esempio n. 4
0
def eval_speedup_regressors(db, classifiers, baseline, rank_fn,
                            table, job, training, testing):
    maxwgsize_index = testing.attribute_by_name("kern_max_wg_size").index
    wg_c_index = testing.attribute_by_name("wg_c").index
    wg_r_index = testing.attribute_by_name("wg_r").index
    insert_str = ("INSERT INTO {} VALUES "
                  "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)".format(table))

    for classifier in classifiers:
        meta = Classifier(classifier)
        prof.start("train classifier")
        meta.build_classifier(training)
        prof.stop("train classifier")
        basename = ml.classifier_basename(classifier.classname)
        classifier_id = db.classifier_id(classifier)

        io.debug(job, basename, testing.num_instances)
        scenarios = set([instance.get_string_value(0)
                         for instance in testing])
        instances = zip(scenarios, [
            (instance for instance in testing if
             instance.get_string_value(0) == scenario).next()
            for scenario in scenarios
        ])

        for scenario,instance in instances:
            maxwgsize = int(instance.get_value(maxwgsize_index))
            wlegal = space.enumerate_wlegal_params(maxwgsize)
            predictions = []

            elapsed = 0
            for params in wlegal:
                wg_c, wg_r = unhash_params(params)

                instance.set_value(wg_c_index, wg_c)
                instance.set_value(wg_r_index, wg_r)

                # Predict the speedup for a particular set of
                # parameters.
                prof.start()
                predicted = meta.classify_instance(instance)
                elapsed += prof.elapsed()
                predictions.append((params, predicted))

            # Rank the predictions from highest to lowest speedup.
            predictions = sorted(predictions, key=lambda x: x[1], reverse=True)

            row = db.execute(
                "SELECT "
                "    oracle_param,"
                "    ("
                "        SELECT mean FROM runtime_stats "
                "        WHERE scenario=? AND params=?"
                "    ) * 1.0 / oracle_runtime  AS oracle_speedup,"
                "    worst_runtime / oracle_runtime AS actual_range "
                "FROM scenario_stats "
                "WHERE scenario=?",
                (scenario,baseline,scenario)).fetchone()
            actual = row[:2]

            predicted_range = predictions[-1][1] - predictions[0][1]
            actual_range = row[2] - row[1]

            num_attempts = 1
            while True:
                predicted = predictions.pop(0)

                try:
                    speedup = db.speedup(scenario, baseline, predicted[0])
                    perf = db.perf(scenario, predicted[0])

                    try:
                        speedup_he = self.speedup(scenario, HE_PARAM, predicted[0])
                    except:
                        speedup_he = None

                    try:
                        speedup_mo = self.speedup(scenario, MO_PARAM, predicted[0])
                    except:
                        speedup_mo = None

                    db.execute(insert_str,
                               (job, classifier_id, scenario, actual[0],
                                actual[1], predicted[0], predicted[1],
                                actual_range, predicted_range,
                                num_attempts,
                                1 if predicted[0] == actual[0] else 0,
                                perf, speedup, speedup_he, speedup_mo, elapsed))
                    break

                except _db.MissingDataError:
                    num_attempts += 1
                    pass

            db.commit()