/* Query one device specified by type, create a context and command
 * queue, as well as retrieve device information
 */
cl_int mwSetupCL(CLInfo* ci, const CLRequest* clr)
{
    cl_int err;

    err = mwGetCLInfo(ci, clr);
    if (err != CL_SUCCESS)
    {
        mw_printf("Failed to get information about device\n");
        return err;
    }

    err = mwGetDevInfo(&ci->di, ci->dev);
    if (err != CL_SUCCESS)
    {
        mw_printf("Failed to get device info\n");
        return err;
    }

    if (mwIsFirstRun())
    {
        if (clr->verbose)
        {
            mwPrintDevInfo(&ci->di);
        }
        else
        {
            mwPrintDevInfoShort(&ci->di);
        }
    }

    return mwCreateCtxQueue(ci, CL_FALSE, clr->enableProfiling);
}
cl_int setupSeparationCL(CLInfo* ci,
                         const AstronomyParameters* ap,
                         const IntegralArea* ias,
                         const CLRequest* clr,
                         cl_int* useImages)
{
    cl_int err;
    char* compileFlags;
    char* kernelSrc;

    err = mwSetupCL(ci, clr);
    if (err != CL_SUCCESS)
    {
        mwCLWarn("Error getting device and context", err);
        return err;
    }

    err = mwGetDevInfo(&ci->di, ci->dev);
    if (err != CL_SUCCESS)
    {
        warn("Failed to get device info\n");
        return err;
    }

    if (clr->verbose)
    {
        mwPrintDevInfo(&ci->di);
    }
    else
    {
        mwPrintDevInfoShort(&ci->di);
    }

    if (!separationCheckDevCapabilities(&ci->di, ap, ias))
    {
        warn("Device failed capability check\n");
        return MW_CL_ERROR;
    }

    *useImages = *useImages && ci->di.imgSupport;

    compileFlags = getCompilerFlags(ap, &ci->di, *useImages);
    if (!compileFlags)
    {
        warn("Failed to get compiler flags\n");
        return MW_CL_ERROR;
    }

    kernelSrc = findKernelSrc();
    if (!kernelSrc)
    {
        warn("Failed to read CL kernel source\n");
        return MW_CL_ERROR;
    }

    warn("\nCompiler flags:\n%s\n\n", compileFlags);
    err = mwSetProgramFromSrc(ci, "mu_sum_kernel", (const char**) &kernelSrc, 1, compileFlags);

    freeKernelSrc(kernelSrc);
    free(compileFlags);

    if (err != CL_SUCCESS)
    {
        mwCLWarn("Error creating program from source", err);
        return err;
    }

    return CL_SUCCESS;
}