2016-03-24 07:04:01 -05:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015-2016 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 WARRANNTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INNCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANNY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER INN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR INN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-04 22:17:18 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
|
|
|
|
#include "hip/hcc_detail/hip_hcc.h"
|
|
|
|
|
#include "hip/hcc_detail/trace_helper.h"
|
2016-03-24 04:57:30 -05:00
|
|
|
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
|
//-------------------------------------------------------------------------------------------------
|
|
|
|
|
// Stream
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
//---
|
2016-03-26 12:35:04 -05:00
|
|
|
hipError_t ihipStreamCreate(hipStream_t *stream, unsigned int flags)
|
2016-03-24 09:28:46 -05:00
|
|
|
{
|
2016-08-07 21:46:51 -05:00
|
|
|
ihipCtx_t *ctx = ihipGetTlsDefaultCtx();
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
if (ctx) {
|
|
|
|
|
hc::accelerator acc = ctx->getWriteableDevice()->_acc;
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
// TODO - se try-catch loop to detect memory exception?
|
|
|
|
|
//
|
|
|
|
|
//Note this is an execute_in_order queue, so all kernels submitted will atuomatically wait for prev to complete:
|
|
|
|
|
//This matches CUDA stream behavior:
|
2016-03-26 10:46:20 -05:00
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
auto istream = new ihipStream_t(ctx, acc.create_view(), flags);
|
2016-03-26 10:46:20 -05:00
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
ctx->locked_addStream(istream);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
*stream = istream;
|
|
|
|
|
tprintf(DB_SYNC, "hipStreamCreate, stream=%p\n", *stream);
|
|
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidDevice;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
2016-03-26 12:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
hipError_t hipStreamCreateWithFlags(hipStream_t *stream, unsigned int flags)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(stream, flags);
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(ihipStreamCreate(stream, flags));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---
|
2016-09-02 17:50:31 -05:00
|
|
|
hipError_t hipStreamCreate(hipStream_t *stream)
|
2016-03-26 12:35:04 -05:00
|
|
|
{
|
|
|
|
|
HIP_INIT_API(stream);
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(ihipStreamCreate(stream, hipStreamDefault));
|
2016-03-24 09:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hipError_t hipStreamWaitEvent(hipStream_t stream, hipEvent_t event, unsigned int flags)
|
|
|
|
|
{
|
2016-03-26 12:35:04 -05:00
|
|
|
HIP_INIT_API(stream, event, flags);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
2016-09-01 13:59:55 -05:00
|
|
|
if (event == nullptr) {
|
|
|
|
|
e = hipErrorInvalidResourceHandle;
|
|
|
|
|
|
|
|
|
|
} else if (event->_state != hipEventStatusUnitialized) {
|
|
|
|
|
|
|
|
|
|
bool fastWait = false;
|
|
|
|
|
|
|
|
|
|
if (stream != hipStreamNull) {
|
|
|
|
|
stream->locked_waitEvent(event);
|
|
|
|
|
|
|
|
|
|
fastWait = true; // don't use the slow host-side synchronization.
|
|
|
|
|
}
|
2016-08-30 17:29:50 -05:00
|
|
|
|
2016-09-01 13:59:55 -05:00
|
|
|
if (!fastWait) {
|
|
|
|
|
// TODO-hcc Convert to use create_blocking_marker(...) functionality.
|
|
|
|
|
// Currently we have a super-conservative version of this - block on host, and drain the queue.
|
|
|
|
|
// This should create a barrier packet in the target queue.
|
|
|
|
|
stream->locked_wait();
|
|
|
|
|
e = hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
} // else event not recorded, return immediately and don't create marker.
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-09-04 07:00:59 -05:00
|
|
|
//---
|
|
|
|
|
hipError_t hipStreamQuery(hipStream_t stream)
|
|
|
|
|
{
|
|
|
|
|
HIP_INIT_API(stream);
|
|
|
|
|
|
|
|
|
|
// Use default stream if 0 specified:
|
|
|
|
|
if (stream == hipStreamNull) {
|
|
|
|
|
ihipCtx_t *device = ihipGetTlsDefaultCtx();
|
|
|
|
|
stream = device->_defaultStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LockedAccessor_StreamCrit_t crit(stream->_criticalData);
|
|
|
|
|
int pendingOps = crit->_av.get_pending_async_ops();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hipError_t e = (pendingOps > 0) ? hipErrorNotReady : hipSuccess;
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-03-24 09:28:46 -05:00
|
|
|
//---
|
|
|
|
|
hipError_t hipStreamSynchronize(hipStream_t stream)
|
|
|
|
|
{
|
2016-03-26 12:35:04 -05:00
|
|
|
HIP_INIT_API(stream);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
if (stream == NULL) {
|
2016-08-07 21:46:51 -05:00
|
|
|
ihipCtx_t *ctx = ihipGetTlsDefaultCtx();
|
|
|
|
|
ctx->locked_syncDefaultStream(true/*waitOnSelf*/);
|
2016-03-24 09:28:46 -05:00
|
|
|
} else {
|
2016-03-28 09:46:40 -05:00
|
|
|
stream->locked_wait();
|
2016-03-24 09:28:46 -05:00
|
|
|
e = hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
/**
|
|
|
|
|
* @return #hipSuccess, #hipErrorInvalidResourceHandle
|
|
|
|
|
*/
|
|
|
|
|
hipError_t hipStreamDestroy(hipStream_t stream)
|
|
|
|
|
{
|
2016-03-26 12:35:04 -05:00
|
|
|
HIP_INIT_API(stream);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
hipError_t e = hipSuccess;
|
|
|
|
|
|
|
|
|
|
//--- Drain the stream:
|
|
|
|
|
if (stream == NULL) {
|
2016-08-07 21:46:51 -05:00
|
|
|
ihipCtx_t *ctx = ihipGetTlsDefaultCtx();
|
|
|
|
|
ctx->locked_syncDefaultStream(true/*waitOnSelf*/);
|
2016-03-24 09:28:46 -05:00
|
|
|
} else {
|
2016-03-28 09:46:40 -05:00
|
|
|
stream->locked_wait();
|
2016-03-24 09:28:46 -05:00
|
|
|
e = hipSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-08 11:55:57 -05:00
|
|
|
ihipCtx_t *ctx = stream->getCtx();
|
2016-03-24 09:28:46 -05:00
|
|
|
|
2016-08-07 21:46:51 -05:00
|
|
|
if (ctx) {
|
|
|
|
|
ctx->locked_removeStream(stream);
|
2016-03-24 09:28:46 -05:00
|
|
|
delete stream;
|
|
|
|
|
} else {
|
|
|
|
|
e = hipErrorInvalidResourceHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ihipLogStatus(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
hipError_t hipStreamGetFlags(hipStream_t stream, unsigned int *flags)
|
|
|
|
|
{
|
2016-03-26 12:35:04 -05:00
|
|
|
HIP_INIT_API(stream, flags);
|
2016-03-24 09:28:46 -05:00
|
|
|
|
|
|
|
|
if (flags == NULL) {
|
|
|
|
|
return ihipLogStatus(hipErrorInvalidValue);
|
|
|
|
|
} else if (stream == NULL) {
|
|
|
|
|
return ihipLogStatus(hipErrorInvalidResourceHandle);
|
|
|
|
|
} else {
|
|
|
|
|
*flags = stream->_flags;
|
|
|
|
|
return ihipLogStatus(hipSuccess);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-24 04:57:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|