Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTL_RESULT_SUCCESS_STILL_OPEN_BY_ANOTHER_CALLER #78

Open
Valkirie opened this issue Mar 1, 2024 · 3 comments
Open

CTL_RESULT_SUCCESS_STILL_OPEN_BY_ANOTHER_CALLER #78

Valkirie opened this issue Mar 1, 2024 · 3 comments
Assignees

Comments

@Valkirie
Copy link

Valkirie commented Mar 1, 2024

I'm using IGCL within a c# project using DllImport. If I start my project, load IGCL, enumerate devices and adapter count with one GPU plugged in, it'll properly report it. If I plug another GPU while the app is running and call EnumerateDevices once more, IGCL will still only report one GPU.

Calling CloseIgcl() and InitializeIgcl() doesn't change anything as ctlClose() will return CTL_RESULT_SUCCESS_STILL_OPEN_BY_ANOTHER_CALLER. Even though I'm trying to terminate IGCL from my C# app.

extern "C" {

    static ctl_api_handle_t hAPIHandle;
    static ctl_device_adapter_handle_t* hDevices;

ctl_result_t IntializeIgcl()
{
    ctl_result_t Result = CTL_RESULT_SUCCESS;

    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    ctl_init_args_t ctlInitArgs;
    ctlInitArgs.AppVersion = CTL_MAKE_VERSION(CTL_IMPL_MAJOR_VERSION, CTL_IMPL_MINOR_VERSION);
    ctlInitArgs.flags = CTL_INIT_FLAG_USE_LEVEL_ZERO;
    ctlInitArgs.Size = sizeof(ctlInitArgs);
    ctlInitArgs.Version = 0;
    ZeroMemory(&ctlInitArgs.ApplicationUID, sizeof(ctl_application_id_t));
    Result = ctlInit(&ctlInitArgs, &hAPIHandle);

    if (CTL_RESULT_SUCCESS == Result)
    {
        AllocConsole();
        freopen("CONOUT$", "w", stdout); // Redirect stdout to the console
        freopen("CONOUT$", "w", stderr); // Optional: Redirect stderr to the console
        std::ios::sync_with_stdio(); // Sync C++ and C standard streams
        cout << "IGCL Initialized" << hAPIHandle << endl;
    }

    return Result;
}

ctl_result_t CloseIgcl()
{
    ctl_result_t Result = CTL_RESULT_SUCCESS;

    Result = ctlClose(hAPIHandle);
    CTL_FREE_MEM(hDevices);

    cout << "IGCL Terminated" << endl;

    return Result;
}
}
@Valkirie Valkirie changed the title CTL_RESULT_ERROR_OS_CALL on ctlGetDeviceProperties CTL_RESULT_SUCCESS_STILL_OPEN_BY_ANOTHER_CALLER Mar 2, 2024
@Valkirie Valkirie reopened this Mar 31, 2024
@kpsam2000Intel
Copy link
Contributor

kpsam2000Intel commented Apr 4, 2024

Hi @Valkirie... do you close the existing DX adapter/device as well during such an additional GPU plug-in event? Close everything including IGCL using ctlClose and recreate adapter/IGCL. Can you try it and let us know? Thanks

@Valkirie
Copy link
Author

Valkirie commented Apr 8, 2024

Hi @Valkirie... do you close the existing DX adapter/device as well during such an additional GPU plug-in event? Close everything including IGCL using ctlClose and recreate adapter/IGCL. Can you try it and let us know? Thanks

Hey @kpsam2000Intel, sorry I'm slightly confused by what you mean with close the existing DX adapter/device. Whenever a new device is added, I'm closing and (re)opening IGCL. Please see GetDeviceIdx() in the following c# wrapper I made : https://pastebin.com/neu3rK05

GetDeviceIdx() is called anytime a new device has landed.

public static int GetDeviceIdx(string deviceName)
{
	// test
	Terminate();
	Initialize();

	ctl_result_t Result = ctl_result_t.CTL_RESULT_SUCCESS;
	uint adapterCount = 0;

	// Get the number of Intel devices
	IntPtr hDevices = EnumerateDevices(ref adapterCount);
	if (hDevices == IntPtr.Zero)
		return -1;

	// Convert the device handles to an array of IntPtr
	devices = new IntPtr[adapterCount];
	Marshal.Copy(hDevices, devices, 0, (int)adapterCount);
	if (devices.Length == 0)
		return -1;

	for (int idx = 0; idx < devices.Length; idx++)
	{
		ctl_device_adapter_properties_t StDeviceAdapterProperties = new();
		ctl_device_adapter_handle_t hDevice = new()
		{
			handle = devices[idx]
		};

		Result = GetDeviceProperties(hDevice, ref StDeviceAdapterProperties);
		if (Result != ctl_result_t.CTL_RESULT_SUCCESS)
			continue;

		if (deviceName.Equals(StDeviceAdapterProperties.name))
			return idx;
	}

	return -1;
}

public static void Terminate()
{
	if (pDll != IntPtr.Zero)
	{
		CloseIgcl();
		FreeLibrary(pDll);
		pDll = IntPtr.Zero;
	}
}

CasperH2O pushed a commit to CasperH2O/HandheldCompanion that referenced this issue Apr 8, 2024
@Valkirie
Copy link
Author

Valkirie commented Jun 22, 2024

Any update ? It's still impossible to properly detect a hotswapped display while the dll is loaded. Application has to be restarted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants