Context update.

- Remove tls_deviceID.
- Add first passing test.

Change-Id: If3e2f254abf589028cfe4f9e6369745f04160de0
This commit is contained in:
Ben Sander
2016-08-09 15:37:19 -05:00
parent 1786b120ed
commit 89164259ab
8 changed files with 107 additions and 64 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ hipError_t hipCtxCreate(hipCtx_t *ctx, unsigned int flags, hipDevice_t device)
hipError_t e = hipSuccess;
*ctx = new ihipCtx_t(device, g_deviceCnt, flags);
tls_defaultCtx = *ctx;
ihipSetTlsDefaultCtx(*ctx);
tls_ctxStack.push(*ctx);
return ihipLogStatus(e);
+33 -5
View File
@@ -26,14 +26,25 @@ THE SOFTWARE.
//-------------------------------------------------------------------------------------------------
//---
/**
* @return #hipSuccess
* @return #hipSuccess, hipErrorInvalidDevice
*/
// TODO - does this initialize HIP runtime?
hipError_t hipGetDevice(int *deviceId)
{
HIP_INIT_API(deviceId);
*deviceId = tls_defaultDeviceId;
return ihipLogStatus(hipSuccess);
hipError_t e = hipSuccess;
auto ctx = ihipGetTlsDefaultCtx();
if (ctx == nullptr) {
e = hipErrorInvalidDevice; // TODO, check error code.
*deviceId = -1;
} else {
*deviceId = ctx->getDevice()->_deviceId;
}
return ihipLogStatus(e);
}
@@ -41,6 +52,7 @@ hipError_t hipGetDevice(int *deviceId)
/**
* @return #hipSuccess, #hipErrorNoDevice
*/
// TODO - does this initialize HIP runtime?
hipError_t hipGetDeviceCount(int *count)
{
HIP_INIT_API(count);
@@ -136,8 +148,7 @@ hipError_t hipSetDevice(int deviceId)
if ((deviceId < 0) || (deviceId >= g_deviceCnt)) {
return ihipLogStatus(hipErrorInvalidDevice);
} else {
tls_defaultDeviceId = deviceId;
tls_defaultCtx = ihipGetPrimaryCtx(deviceId);
ihipSetTlsDefaultCtx(ihipGetPrimaryCtx(deviceId));
return ihipLogStatus(hipSuccess);
}
}
@@ -299,3 +310,20 @@ hipError_t hipSetDeviceFlags( unsigned int flags)
};
hipError_t hipDeviceGetFromId(hipDevice_t *device, int deviceId)
{
HIP_INIT_API(device, deviceId);
hipError_t e = hipSuccess;
*device = ihipGetDevice(deviceId);
if (device == nullptr) {
e = hipErrorInvalidDevice;
}
return ihipLogStatus(e);
}
+16 -9
View File
@@ -102,11 +102,9 @@ hsa_amd_memory_pool_t gpu_pool_;
//=================================================================================================
// Thread-local storage:
//=================================================================================================
thread_local int tls_defaultDeviceId = 0;
// This is the implicit context used by all HIP commands.
// It can be set by hipSetDevice or by the CTX manipulation commands:
thread_local ihipCtx_t *tls_defaultCtx;
thread_local hipError_t tls_lastHipError = hipSuccess;
@@ -139,17 +137,25 @@ ihipCtx_t * ihipGetPrimaryCtx(unsigned deviceIndex)
};
static thread_local ihipCtx_t *tls_defaultCtx = nullptr;
void ihipSetTlsDefaultCtx(ihipCtx_t *ctx)
{
tls_defaultCtx = ctx;
}
//---
//FIXME - this needs to return the active context for this CPU thread - not primary for device.
//TODO - review the context creation strategy here. Really should be:
// - first "non-device" runtime call creates the context for this thread. Allowed to call setDevice first.
// - hipDeviceReset destroys the primary context for device?
// - Then context is created again for next usage.
ihipCtx_t *ihipGetTlsDefaultCtx()
{
// If this is invalid, the TLS state is corrupt.
// This can fire if called before devices are initialized.
// TODO - consider replacing assert with error code
assert (ihipIsValidDevice(tls_defaultDeviceId));
return ihipGetPrimaryCtx(tls_defaultDeviceId);
// Per-thread initialization of the TLS:
if ((tls_defaultCtx == nullptr) && (g_deviceCnt>0)) {
ihipSetTlsDefaultCtx(ihipGetPrimaryCtx(0));
}
return tls_defaultCtx;
}
@@ -1221,6 +1227,7 @@ void ihipInit()
assert(deviceCnt == g_deviceCnt);
}
tprintf(DB_SYNC, "pid=%u %-30s\n", getpid(), "<ihipInit>");
}