extern "C" OFFLOAD OFFLOAD_TARGET_ACQUIRE(
    TARGET_TYPE      target_type,
    int              target_number,
    int              is_optional,
    _Offload_status* status,
    const char*      file,
    uint64_t         line
)
{
    bool retval;
    OFFLOAD ofld;

    // initialize status
    if (status != 0) {
        status->result = OFFLOAD_UNAVAILABLE;
        status->device_number = -1;
        status->data_sent = 0;
        status->data_received = 0;
    }

    // make sure libray is initialized
    retval = __offload_init_library();

    // OFFLOAD_TIMER_INIT must follow call to __offload_init_library
    OffloadHostTimerData * timer_data = OFFLOAD_TIMER_INIT(file, line);

    OFFLOAD_TIMER_START(timer_data, c_offload_host_total_offload);

    OFFLOAD_TIMER_START(timer_data, c_offload_host_initialize);

    // initialize all devices is init_type is on_offload_all
    if (retval && __offload_init_type == c_init_on_offload_all) {
        for (int i = 0; i < mic_engines_total; i++) {
             mic_engines[i].init();
        }
    }
    OFFLOAD_TIMER_STOP(timer_data, c_offload_host_initialize);

    OFFLOAD_TIMER_START(timer_data, c_offload_host_target_acquire);

    if (target_type == TARGET_HOST) {
        // Host always available
        retval = true;
    }
    else if (target_type == TARGET_MIC) {
        if (target_number >= -1) {
            if (retval) {
                if (target_number >= 0) {
                    // User provided the device number
                    target_number = target_number % mic_engines_total;
                }
                else {
                    // use device 0
                    target_number = 0;
                }

                // reserve device in ORSL
                if (is_optional) {
                    if (!ORSL::try_reserve(target_number)) {
                        target_number = -1;
                    }
                }
                else {
                    if (!ORSL::reserve(target_number)) {
                        target_number = -1;
                    }
                }

                // initialize device
                if (target_number >= 0 &&
                    __offload_init_type == c_init_on_offload) {
                    OFFLOAD_TIMER_START(timer_data, c_offload_host_initialize);
                    mic_engines[target_number].init();
                    OFFLOAD_TIMER_STOP(timer_data, c_offload_host_initialize);
                }
            }
            else {
                // fallback to CPU
                target_number = -1;
            }

            if (target_number < 0 || !retval) {
                if (!is_optional && status == 0) {
                    LIBOFFLOAD_ERROR(c_device_is_not_available);
                    exit(1);
                }

                retval = false;
            }
        }
        else {
            LIBOFFLOAD_ERROR(c_invalid_device_number);
            exit(1);
        }
    }

    if (retval) {
        ofld = new OffloadDescriptor(target_number, status,
                                     !is_optional, false, timer_data);
        OFFLOAD_TIMER_HOST_MIC_NUM(timer_data, target_number);
        Offload_Report_Prolog(timer_data);
        OFFLOAD_DEBUG_TRACE_1(2, timer_data->offload_number, c_offload_start,
                              "Starting offload: target_type = %d, "
                              "number = %d, is_optional = %d\n",
                              target_type, target_number, is_optional);

        OFFLOAD_TIMER_STOP(timer_data, c_offload_host_target_acquire);
    }
    else {
        ofld = NULL;

        OFFLOAD_TIMER_STOP(timer_data, c_offload_host_target_acquire);
        OFFLOAD_TIMER_STOP(timer_data, c_offload_host_total_offload);
        offload_report_free_data(timer_data);
    }

    return ofld;
}
Beispiel #2
0
extern void Offload_Report_Epilog(OffloadHostTimerData * timer_data)
{
    double frequency = cpu_frequency;
    std::string       buf;
    std::stringstream ss;

    OffloadHostTimerData *pnode = timer_data;

    if (!pnode) {
        return;
    }
    ss << "[" << report_get_message_str(c_report_offload) << "] [";
    ss << report_get_message_str(c_report_host) << "]  [";
    ss << report_get_message_str(c_report_tag) <<  " ";
    ss << pnode->offload_number << "] [";
    ss << report_get_message_str(c_report_cpu_time) << "]        ";
    ss << std::fixed << std::setprecision(6);
    ss << (double) pnode->phases[0].total / frequency;
    ss << report_get_message_str(c_report_seconds) << "\n";

    if (offload_report_level >= OFFLOAD_REPORT_2) {
        ss << "[" << report_get_message_str(c_report_offload) << "] [";
        ss << report_get_message_str(c_report_mic);
        ss << " " << pnode->card_number;
        ss << "] [" << report_get_message_str(c_report_tag) << " ";
        ss <<  pnode->offload_number << "] [";
        ss << report_get_message_str(c_report_cpu_to_mic_data) << "]   ";
        ss << pnode->sent_bytes << " ";
        ss << report_get_message_str(c_report_bytes) << "\n";
    }

    double time = 0;
    if (pnode->target.frequency != 0) {
        time = (double) pnode->target.phases[0].total /
            (double) pnode->target.frequency;
    }
    ss << "[" << report_get_message_str(c_report_offload) << "] [";
    ss << report_get_message_str(c_report_mic) << " ";
    ss << pnode->card_number<< "] [";
    ss << report_get_message_str(c_report_tag) <<  " ";
    ss << pnode->offload_number << "] [";
    ss << report_get_message_str(c_report_mic_time) << "]        ";
    ss << std::fixed << std::setprecision(6) << time;
    ss << report_get_message_str(c_report_seconds) << "\n";

    if (offload_report_level >= OFFLOAD_REPORT_2) {
        ss << "[" << report_get_message_str(c_report_offload) << "] [";
        ss << report_get_message_str(c_report_mic);
        ss << " " << pnode->card_number;
        ss << "] [" << report_get_message_str(c_report_tag) << " ";
        ss <<  pnode->offload_number << "] [";
        ss << report_get_message_str(c_report_mic_to_cpu_data) << "]   ";
        ss << pnode->received_bytes << " ";
        ss << report_get_message_str(c_report_bytes) << "\n";
    }
    ss << "\n";

    buf = ss.str();
    fprintf(stdout, buf.data());
    fflush(stdout);

    offload_report_free_data(timer_data);
}