<commit_before>/*
Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "hip/hip_runtime.h"
#include "hip_hcc_internal.h"
#include "trace_helper.h"

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Events
//---


ihipEvent_t::ihipEvent_t(unsigned flags) : _criticalData(this) { _flags = flags; };


// Attach to an existing completion future:
void ihipEvent_t::attachToCompletionFuture(const hc::completion_future* cf, hipStream_t stream,
                                           ihipEventType_t eventType) {
    LockedAccessor_EventCrit_t crit(_criticalData);
    crit->_eventData.marker(*cf);
    crit->_eventData._type = eventType;
    crit->_eventData._stream = stream;
    crit->_eventData._state = hipEventStatusRecording;
}


std::pair<hipEventStatus_t, uint64_t> ihipEvent_t::refreshEventStatus() {
    auto ecd = locked_copyCrit();
    if (ecd._state == hipEventStatusRecording) {
        bool isReady1 = ecd._stream->locked_eventIsReady(this);
        if (isReady1) {
            LockedAccessor_EventCrit_t eCrit(_criticalData);

            if ((eCrit->_eventData._type == hipEventTypeIndependent) ||
                (eCrit->_eventData._type == hipEventTypeStopCommand)) {
                eCrit->_eventData._timestamp = eCrit->_eventData.marker().get_end_tick();
            } else if (eCrit->_eventData._type == hipEventTypeStartCommand) {
                eCrit->_eventData._timestamp = eCrit->_eventData.marker().get_begin_tick();
            } else {
                eCrit->_eventData._timestamp = 0;
                assert(0);  // TODO - move to debug assert
            }

            eCrit->_eventData._state = hipEventStatusComplete;

            return std::pair<hipEventStatus_t, uint64_t>(eCrit->_eventData._state,
                                                         eCrit->_eventData._timestamp);
        }
    }

    // Not complete path here:
    return std::pair<hipEventStatus_t, uint64_t>(ecd._state, ecd._timestamp);
}


hipError_t ihipEventCreate(hipEvent_t* event, unsigned flags) {
    hipError_t e = hipSuccess;

    // TODO-IPC - support hipEventInterprocess.
    unsigned supportedFlags = hipEventDefault | hipEventBlockingSync | hipEventDisableTiming |
                              hipEventReleaseToDevice | hipEventReleaseToSystem;
    const unsigned releaseFlags = (hipEventReleaseToDevice | hipEventReleaseToSystem);

    const bool illegalFlags =
        (flags & ~supportedFlags) ||             // can't set any unsupported flags.
        (flags & releaseFlags) == releaseFlags;  // can't set both release flags

    if (event && !illegalFlags) {
        *event = new ihipEvent_t(flags);
    } else {
        e = hipErrorInvalidValue;
    }

    return e;
}

hipError_t hipEventCreateWithFlags(hipEvent_t* event, unsigned flags) {
    HIP_INIT_API(hipEventCreateWithFlags, event, flags);

    return ihipLogStatus(ihipEventCreate(event, flags));
}

hipError_t hipEventCreate(hipEvent_t* event) {
    HIP_INIT_API(hipEventCreate, event);

    return ihipLogStatus(ihipEventCreate(event, 0));
}


hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream) {
    HIP_INIT_SPECIAL_API(hipEventRecord, TRACE_SYNC, event, stream);
 
    hipError_t status;
    if (event){
        auto ecd = event->locked_copyCrit();
        if( ecd._state != hipEventStatusUnitialized) {
            stream = ihipSyncAndResolveStream(stream);

            if (HIP_SYNC_NULL_STREAM && stream->isDefaultStream()) {
                // TODO-HIP_SYNC_NULL_STREAM : can remove this code when HIP_SYNC_NULL_STREAM = 0
                //
                // If default stream , then wait on all queues.
                ihipCtx_t* ctx = ihipGetTlsDefaultCtx();
                ctx->locked_syncDefaultStream(true, true);

                { 
                    LockedAccessor_EventCrit_t eCrit(event->criticalData());
                    eCrit->_eventData.marker(hc::completion_future());  // reset event
                    eCrit->_eventData._stream = stream;
                    eCrit->_eventData._timestamp = hc::get_system_ticks();
                    eCrit->_eventData._state = hipEventStatusComplete;
                }
                status = hipSuccess;
            } else {
                // Record the event in the stream:
                // Keep a copy outside the critical section so we lock stream first, then event - to
                // avoid deadlock
                hc::completion_future cf = stream->locked_recordEvent(event);

                {
                    LockedAccessor_EventCrit_t eCrit(event->criticalData());
                    eCrit->_eventData.marker(cf);
                    eCrit->_eventData._stream = stream;
                    eCrit->_eventData._timestamp = 0;
                    eCrit->_eventData._state = hipEventStatusRecording;
                }

                status = hipSuccess;
            }
        } else {
            status = hipErrorInvalidResourceHandle;
        }
    } else {
            status = hipErrorInvalidResourceHandle;
    }
    return ihipLogStatus(status);
}


hipError_t hipEventDestroy(hipEvent_t event) {
    HIP_INIT_API(hipEventDestroy, event);

    if (event) {
        delete event;

        return ihipLogStatus(hipSuccess);
    } else {
        return ihipLogStatus(hipErrorInvalidResourceHandle);
    }
}

hipError_t hipEventSynchronize(hipEvent_t event) {
    HIP_INIT_SPECIAL_API(hipEventSynchronize, TRACE_SYNC, event);

    if (!(event->_flags & hipEventReleaseToSystem)) {
        tprintf(DB_WARN,
                "hipEventSynchronize on event without system-scope fence ; consider creating with "
                "hipEventReleaseToSystem\n");
    }
    auto ecd = event->locked_copyCrit();
<commit_msg>fix race condition in hipEventRecord<commit_after>/*
Copyright (c) 2015 - present Advanced Micro Devices, Inc. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "hip/hip_runtime.h"
#include "hip_hcc_internal.h"
#include "trace_helper.h"

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Events
//---


ihipEvent_t::ihipEvent_t(unsigned flags) : _criticalData(this) { _flags = flags; };


// Attach to an existing completion future:
void ihipEvent_t::attachToCompletionFuture(const hc::completion_future* cf, hipStream_t stream,
                                           ihipEventType_t eventType) {
    LockedAccessor_EventCrit_t crit(_criticalData);
    crit->_eventData.marker(*cf);
    crit->_eventData._type = eventType;
    crit->_eventData._stream = stream;
    crit->_eventData._state = hipEventStatusRecording;
}


std::pair<hipEventStatus_t, uint64_t> ihipEvent_t::refreshEventStatus() {
    auto ecd = locked_copyCrit();
    if (ecd._state == hipEventStatusRecording) {
        bool isReady1 = ecd._stream->locked_eventIsReady(this);
        if (isReady1) {
            LockedAccessor_EventCrit_t eCrit(_criticalData);

            if ((eCrit->_eventData._type == hipEventTypeIndependent) ||
                (eCrit->_eventData._type == hipEventTypeStopCommand)) {
                eCrit->_eventData._timestamp = eCrit->_eventData.marker().get_end_tick();
            } else if (eCrit->_eventData._type == hipEventTypeStartCommand) {
                eCrit->_eventData._timestamp = eCrit->_eventData.marker().get_begin_tick();
            } else {
                eCrit->_eventData._timestamp = 0;
                assert(0);  // TODO - move to debug assert
            }

            eCrit->_eventData._state = hipEventStatusComplete;

            return std::pair<hipEventStatus_t, uint64_t>(eCrit->_eventData._state,
                                                         eCrit->_eventData._timestamp);
        }
    }

    // Not complete path here:
    return std::pair<hipEventStatus_t, uint64_t>(ecd._state, ecd._timestamp);
}


hipError_t ihipEventCreate(hipEvent_t* event, unsigned flags) {
    hipError_t e = hipSuccess;

    // TODO-IPC - support hipEventInterprocess.
    unsigned supportedFlags = hipEventDefault | hipEventBlockingSync | hipEventDisableTiming |
                              hipEventReleaseToDevice | hipEventReleaseToSystem;
    const unsigned releaseFlags = (hipEventReleaseToDevice | hipEventReleaseToSystem);

    const bool illegalFlags =
        (flags & ~supportedFlags) ||             // can't set any unsupported flags.
        (flags & releaseFlags) == releaseFlags;  // can't set both release flags

    if (event && !illegalFlags) {
        *event = new ihipEvent_t(flags);
    } else {
        e = hipErrorInvalidValue;
    }

    return e;
}

hipError_t hipEventCreateWithFlags(hipEvent_t* event, unsigned flags) {
    HIP_INIT_API(hipEventCreateWithFlags, event, flags);

    return ihipLogStatus(ihipEventCreate(event, flags));
}

hipError_t hipEventCreate(hipEvent_t* event) {
    HIP_INIT_API(hipEventCreate, event);

    return ihipLogStatus(ihipEventCreate(event, 0));
}


hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream) {
    HIP_INIT_SPECIAL_API(hipEventRecord, TRACE_SYNC, event, stream);
    if (!event) return ihipLogStatus(hipErrorInvalidResourceHandle);
    stream = ihipSyncAndResolveStream(stream);
    LockedAccessor_EventCrit_t eCrit(event->criticalData());
    if (eCrit->_eventData._state == hipEventStatusUnitialized) return ihipLogStatus(hipErrorInvalidResourceHandle);
    if (HIP_SYNC_NULL_STREAM && stream->isDefaultStream()) {
        // TODO-HIP_SYNC_NULL_STREAM : can remove this code when HIP_SYNC_NULL_STREAM = 0
        // If default stream , then wait on all queues.
        ihipCtx_t* ctx = ihipGetTlsDefaultCtx();
        ctx->locked_syncDefaultStream(true, true);
        eCrit->_eventData.marker(hc::completion_future());  // reset event
        eCrit->_eventData._stream = stream;
        eCrit->_eventData._timestamp = hc::get_system_ticks();
        eCrit->_eventData._state = hipEventStatusComplete;
    }
    else {
        // Record the event in the stream:
        eCrit->_eventData.marker(stream->locked_recordEvent(event));
        eCrit->_eventData._stream = stream;
        eCrit->_eventData._timestamp = 0;
        eCrit->_eventData._state = hipEventStatusRecording;
    }
    return ihipLogStatus(hipSuccess);
}


hipError_t hipEventDestroy(hipEvent_t event) {
    HIP_INIT_API(hipEventDestroy, event);

    if (event) {
        delete event;

        return ihipLogStatus(hipSuccess);
    } else {
        return ihipLogStatus(hipErrorInvalidResourceHandle);
    }
}

hipError_t hipEventSynchronize(hipEvent_t event) {
    HIP_INIT_SPECIAL_API(hipEventSynchronize, TRACE_SYNC, event);

    if (!(event->_flags & hipEventReleaseToSystem)) {
        tprintf(DB_WARN,
                "hipEventSynchronize on event without system-scope fence ; consider creating with "
                "hipEventReleaseToSystem\n");
    }
    auto ecd = event->locked_copyCrit();

{'fraction_non_alphanumeric': 0.114309585295458, 'fraction_numerical': 0.002582409235910679, 'mean_word_length': 2.5857843137254903, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '185684', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3488, 'n_words': 917}