File: fence.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (95 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2020-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "level_zero/core/source/fence/fence.h"

#include "shared/source/command_stream/command_stream_receiver.h"

#include "level_zero/core/source/cmdqueue/cmdqueue_imp.h"

#include <limits>

namespace L0 {
namespace FenceDefinition {
static constexpr TaskCountType fenceNotReady = NEO::CompletionStamp::notReady;
} // namespace FenceDefinition

Fence *Fence::create(CommandQueueImp *cmdQueue, const ze_fence_desc_t *desc) {
    auto fence = new Fence(cmdQueue);
    UNRECOVERABLE_IF(fence == nullptr);
    fence->reset(!!(desc->flags & ZE_FENCE_FLAG_SIGNALED));
    return fence;
}

ze_result_t Fence::queryStatus() {
    auto csr = cmdQueue->getCsr();
    csr->downloadAllocations(true);

    auto *hostAddr = csr->getTagAddress();

    return csr->testTaskCountReady(hostAddr, taskCount) ? ZE_RESULT_SUCCESS : ZE_RESULT_NOT_READY;
}

ze_result_t Fence::assignTaskCountFromCsr() {
    auto csr = cmdQueue->getCsr();
    taskCount = csr->peekTaskCount() + 1;
    return ZE_RESULT_SUCCESS;
}

ze_result_t Fence::reset(bool signaled) {
    if (signaled) {
        taskCount = 0;
    } else {
        taskCount = FenceDefinition::fenceNotReady;
    }
    return ZE_RESULT_SUCCESS;
}

ze_result_t Fence::hostSynchronize(uint64_t timeout) {
    std::chrono::high_resolution_clock::time_point waitStartTime, lastHangCheckTime, currentTime;
    uint64_t timeDiff = 0;
    ze_result_t ret = ZE_RESULT_NOT_READY;
    const auto csr = cmdQueue->getCsr();

    if (csr->getType() == NEO::CommandStreamReceiverType::aub) {
        return ZE_RESULT_SUCCESS;
    }

    if (FenceDefinition::fenceNotReady == taskCount) {
        return ZE_RESULT_NOT_READY;
    }

    waitStartTime = std::chrono::high_resolution_clock::now();
    lastHangCheckTime = waitStartTime;
    do {
        ret = queryStatus();
        if (ret == ZE_RESULT_SUCCESS) {
            cmdQueue->printKernelsPrintfOutput(false);
            cmdQueue->checkAssert();
            return ZE_RESULT_SUCCESS;
        }

        currentTime = std::chrono::high_resolution_clock::now();
        if (csr->checkGpuHangDetected(currentTime, lastHangCheckTime)) {
            cmdQueue->printKernelsPrintfOutput(true);
            cmdQueue->checkAssert();
            return ZE_RESULT_ERROR_DEVICE_LOST;
        }

        if (timeout == std::numeric_limits<uint64_t>::max()) {
            continue;
        } else if (timeout == 0) {
            break;
        }

        timeDiff = std::chrono::duration_cast<std::chrono::nanoseconds>(currentTime - waitStartTime).count();
    } while (timeDiff < timeout);

    return ret;
}

} // namespace L0