Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions include/af/opencl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ AFAPI af_err afcl_set_device_id(cl_device_id id);
parameter is NULL, then we create a command queue for the user using the OpenCL
context they provided us.

\note The cl_* objects are passed onto c++ objects (cl::Device, cl::Context & cl::CommandQueue)
that are defined in the `cl.hpp` OpenCL c++ header provided by Khronos Group Inc. Therefore, please
be aware of the lifetime of the cl_* objects before passing them to ArrayFire.
\note ArrayFire does not take control of releasing the objects passed to it. The user needs to release them appropriately.
*/
AFAPI af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que);
#endif
Expand All @@ -127,9 +125,7 @@ AFAPI af_err afcl_set_device_context(cl_device_id dev, cl_context ctx);
\param[in] dev is the OpenCL device id that has to be popped
\param[in] ctx is the cl_context object to be removed from ArrayFire pool

\note Any reference counts incremented for cl_* objects by ArrayFire internally are decremented
by this func call and you won't be able to call `afcl_set_device_context` on these objects after
this function has been called.
\note ArrayFire does not take control of releasing the objects passed to it. The user needs to release them appropriately.
*/
AFAPI af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx);
#endif
Expand Down Expand Up @@ -245,9 +241,7 @@ namespace afcl
parameter is NULL, then we create a command queue for the user using the OpenCL
context they provided us.

\note The cl_* objects are passed onto c++ objects (cl::Device, cl::Context & cl::CommandQueue)
that are defined in the `cl.hpp` OpenCL c++ header provided by Khronos Group Inc. Therefore, please
be aware of the lifetime of the cl_* objects before passing them to ArrayFire.
\note ArrayFire does not take control of releasing the objects passed to it. The user needs to release them appropriately.
*/
static inline void addDevice(cl_device_id dev, cl_context ctx, cl_command_queue que)
{
Expand Down Expand Up @@ -280,9 +274,7 @@ static inline void setDevice(cl_device_id dev, cl_context ctx)
\param[in] dev is the OpenCL device id that has to be popped
\param[in] ctx is the cl_context object to be removed from ArrayFire pool

\note Any reference counts incremented for cl_* objects by ArrayFire internally are decremented
by this func call and you won't be able to call `afcl_set_device_context` on these objects after
this function has been called.
\note ArrayFire does not take control of releasing the objects passed to it. The user needs to release them appropriately.
*/
static inline void deleteDevice(cl_device_id dev, cl_context ctx)
{
Expand Down
5 changes: 2 additions & 3 deletions src/api/c/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ template<af_op_t op>
static af_err af_arith(af_array *out, const af_array lhs, const af_array rhs, const bool batchMode)
{
try {
const af_dtype otype = implicit(lhs, rhs);

ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);

dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

const af_dtype otype = implicit(linfo.getType(), rinfo.getType());
af_array res;
switch (otype) {
case f32: res = arithOp<float , op>(lhs, rhs, odims); break;
Expand Down Expand Up @@ -70,13 +69,13 @@ template<af_op_t op>
static af_err af_arith_real(af_array *out, const af_array lhs, const af_array rhs, const bool batchMode)
{
try {
const af_dtype otype = implicit(lhs, rhs);

ArrayInfo linfo = getInfo(lhs);
ArrayInfo rinfo = getInfo(rhs);

dim4 odims = getOutDims(linfo.dims(), rinfo.dims(), batchMode);

const af_dtype otype = implicit(linfo.getType(), rinfo.getType());
af_array res;
switch (otype) {
case f32: res = arithOp<float , op>(lhs, rhs, odims); break;
Expand Down
12 changes: 6 additions & 6 deletions src/api/cpp/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
namespace af
{

#define INSTANTIATE(cppfunc, cfunc) \
array cppfunc(const array &lhs, const array &rhs) \
{ \
af_array out = 0; \
cfunc(&out, lhs.get(), rhs.get(), gforGet()); \
return array(out); \
#define INSTANTIATE(cppfunc, cfunc) \
array cppfunc(const array &lhs, const array &rhs) \
{ \
af_array out = 0; \
AF_THROW(cfunc(&out, lhs.get(), rhs.get(), gforGet())); \
return array(out); \
}

INSTANTIATE(min , af_minof)
Expand Down
12 changes: 9 additions & 3 deletions src/backend/cuda/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,18 +492,24 @@ bool synchronize_calls() {

af_err afcu_get_stream(cudaStream_t* stream, int id)
{
*stream = cuda::getStream(id);
try{
*stream = cuda::getStream(id);
} CATCHALL;
return AF_SUCCESS;
}

af_err afcu_get_native_id(int* nativeid, int id)
{
*nativeid = cuda::getDeviceNativeId(id);
try {
*nativeid = cuda::getDeviceNativeId(id);
} CATCHALL;
return AF_SUCCESS;
}

af_err afcu_set_native_id(int nativeid)
{
cuda::setDevice(cuda::getDeviceIdFromNativeId(nativeid));
try {
cuda::setDevice(cuda::getDeviceIdFromNativeId(nativeid));
} CATCHALL;
return AF_SUCCESS;
}
50 changes: 39 additions & 11 deletions src/backend/opencl/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ void DeviceManager::markDeviceForInterop(const int device, const fg::Window* wHa
void addDeviceContext(cl_device_id dev, cl_context ctx, cl_command_queue que)
{
try {

clRetainDevice(dev);
clRetainContext(ctx);
clRetainCommandQueue(que);

DeviceManager& devMngr = DeviceManager::getInstance();
cl::Device* tDevice = new cl::Device(dev);
cl::Context* tContext = new cl::Context(ctx);
Expand Down Expand Up @@ -758,6 +763,11 @@ void removeDeviceContext(cl_device_id dev, cl_context ctx)
} else if (deleteIdx == -1) {
AF_ERROR("No matching device found", AF_ERR_ARG);
} else {

clReleaseDevice((*devMngr.mDevices[deleteIdx])());
clReleaseContext((*devMngr.mContexts[deleteIdx])());
clReleaseCommandQueue((*devMngr.mQueues[deleteIdx])());

// FIXME: this case can potentially cause issues due to the
// modification of the device pool stl containers.

Expand Down Expand Up @@ -815,57 +825,75 @@ using namespace opencl;

af_err afcl_get_device_type(afcl_device_type *res)
{
*res = (afcl_device_type)getActiveDeviceType();
try {
*res = (afcl_device_type)getActiveDeviceType();
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_get_platform(afcl_platform *res)
{
*res = (afcl_platform)getActivePlatform();
try {
*res = (afcl_platform)getActivePlatform();
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_get_context(cl_context *ctx, const bool retain)
{
*ctx = getContext()();
if (retain) clRetainContext(*ctx);
try {
*ctx = getContext()();
if (retain) clRetainContext(*ctx);
} CATCHALL;
return AF_SUCCESS;
}


af_err afcl_get_queue(cl_command_queue *queue, const bool retain)
{
*queue = getQueue()();
if (retain) clRetainCommandQueue(*queue);
try {
*queue = getQueue()();
if (retain) clRetainCommandQueue(*queue);
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_get_device_id(cl_device_id *id)
{
*id = getDevice()();
try {
*id = getDevice()();
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_set_device_id(cl_device_id id)
{
setDevice(getDeviceIdFromNativeId(id));
try {
setDevice(getDeviceIdFromNativeId(id));
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_add_device_context(cl_device_id dev, cl_context ctx, cl_command_queue que)
{
addDeviceContext(dev, ctx, que);
try {
addDeviceContext(dev, ctx, que);
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_set_device_context(cl_device_id dev, cl_context ctx)
{
setDeviceContext(dev, ctx);
try {
setDeviceContext(dev, ctx);
} CATCHALL;
return AF_SUCCESS;
}

af_err afcl_delete_device_context(cl_device_id dev, cl_context ctx)
{
removeDeviceContext(dev, ctx);
try {
removeDeviceContext(dev, ctx);
} CATCHALL;
return AF_SUCCESS;
}