Cuda calls in DllMain
Hey,
I am building a Dll written using CUDA in the Visual Studio environment. I want to pre-allocate some CUDA memory when the DLL is loaded, and clean them up when the DLL is detached. Here is a sample DllMain call :

[code]BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
//do cuda memory allocation here
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
//do cuda memory deallocation here
break;
}
return TRUE;
}[/code]

I tried adding the memory allocate and deallocate calls in DllMain. But my application crashes, and it crashes when it tries to do any allocation at DLL_PROCESS_ATTACH, or deallocate at DLL_PROCESS_DETACH.

Can some one help me understand what might be the issue here?

Thanks
Goutham
Hey,

I am building a Dll written using CUDA in the Visual Studio environment. I want to pre-allocate some CUDA memory when the DLL is loaded, and clean them up when the DLL is detached. Here is a sample DllMain call :



BOOL APIENTRY DllMain( HMODULE hModule,

DWORD ul_reason_for_call,

LPVOID lpReserved

)

{

switch (ul_reason_for_call)

{

case DLL_PROCESS_ATTACH:

//do cuda memory allocation here

break;

case DLL_THREAD_ATTACH:

break;

case DLL_THREAD_DETACH:

break;

case DLL_PROCESS_DETACH:

//do cuda memory deallocation here

break;

}

return TRUE;

}




I tried adding the memory allocate and deallocate calls in DllMain. But my application crashes, and it crashes when it tries to do any allocation at DLL_PROCESS_ATTACH, or deallocate at DLL_PROCESS_DETACH.



Can some one help me understand what might be the issue here?



Thanks

Goutham

#1
Posted 12/22/2011 09:13 PM   
http://www.lenholgate.com/blog/2004/09/why-does-windows-hold-the-loader-lock-whilst-calling-dllmain.html

do I know this one for sure? nope, but I wouldn't bet against this
http://www.lenholgate.com/blog/2004/09/why-does-windows-hold-the-loader-lock-whilst-calling-dllmain.html



do I know this one for sure? nope, but I wouldn't bet against this

#2
Posted 12/25/2011 05:06 AM   
How are you attempting to allocate the memory? Using the driver or runtime API?

I'll second tmurray on this; there's a good chance it's an issue with the loader lock. Unless absolutely necessary, you should avoid making API calls from within DllMain because there are all kinds of semantic edge-cases which can cause problems. Raymond Chen's blog (see below) is a good source of info on stuff like this:
http://blogs.msdn.com/b/oldnewthing/archive/2004/01/27/63401.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2007/09/04/4731478.aspx

Some info from Microsoft on interactions in DllMain:
http://msdn.microsoft.com/en-us/windows/hardware/gg487379.aspx
How are you attempting to allocate the memory? Using the driver or runtime API?



I'll second tmurray on this; there's a good chance it's an issue with the loader lock. Unless absolutely necessary, you should avoid making API calls from within DllMain because there are all kinds of semantic edge-cases which can cause problems. Raymond Chen's blog (see below) is a good source of info on stuff like this:

http://blogs.msdn.com/b/oldnewthing/archive/2004/01/27/63401.aspx

http://blogs.msdn.com/b/oldnewthing/archive/2007/09/04/4731478.aspx



Some info from Microsoft on interactions in DllMain:

http://msdn.microsoft.com/en-us/windows/hardware/gg487379.aspx

GPU.NET: Write your GPU code in 100% pure C#.

Learn more at tidepowerd.com, and download a free 30-day trial of GPU.NET. Follow @tidepowerd for release updates.



GPU.NET example projects

#3
Posted 12/31/2011 07:47 PM   
Thanks for replying!
Any suggestions for how I can load memory at start up. And then delete/deallocate towards the end!
Thanks for replying!

Any suggestions for how I can load memory at start up. And then delete/deallocate towards the end!

#4
Posted 03/05/2012 12:29 AM   
Scroll To Top