public static void Unload()
        {
            TerminateThreads();

            if (!PlatformApi.NativeLibrary.Free(Kernel32.Native.GetModuleHandle(KokkosContainersLibraryName)))
            {
                KokkosLibraryException.Throw(KokkosContainersLibraryName + "failed to unload.");
            }
            else
            {
                KokkosContainersHandle       = 0;
                KokkosContainersModuleHandle = 0;
            }

            if (!PlatformApi.NativeLibrary.Free(Kernel32.Native.GetModuleHandle(KokkosCoreLibraryName)))
            {
                KokkosLibraryException.Throw(KokkosCoreLibraryName + "failed to unload.");
            }
            else
            {
                KokkosCoreHandle       = 0;
                KokkosCoreModuleHandle = 0;
            }

            IsLoaded = false;
        }
        static KokkosCoreLibrary()
        {
            //string operatingSystem      = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "linux";
            //string platformArchitecture = RuntimeInformation.ProcessArchitecture == Architecture.X64 ? "x64" : "x86";

            //string libraryPath = GetLibraryPath() ?? throw new NullReferenceException("typeof(KokkosCoreLibrary).Assembly.Location is empty.");

            // #if DEBUG
            //             Console.WriteLine("libraryPath: " + libraryPath);
            // #endif

            //Path.Combine(libraryPath,
            //nativeLibraryPath = $"runtimes\\{operatingSystem}-{platformArchitecture}\\native";

            //nativeLibraryPath = Kernel32.GetShortPath(nativeLibraryPath);

            //Kernel32.AddToPath(nativeLibraryPath);
#if DEBUG
            Console.WriteLine("nativeLibraryPath: " + KokkosCoreLibraryName);
#endif

            MpiHandle = PlatformApi.NativeLibrary.Load(MpiLibraryName);

            if (MpiHandle == 0)
            {
                MpiHandle = Kernel32.Native.LoadLibrary(Path.Combine(AppDomain.CurrentDomain.BaseDirectory !, PlatformApi.NativeLibrary.GetRuntimeLibraryPath(), MpiLibraryName + ".dll"));

                if (MpiHandle == 0)
                {
                    KokkosLibraryException.Throw(MpiLibraryName + " failed to load.");
                }
            }

            OpenMpHandle = PlatformApi.NativeLibrary.Load(OpenMpLibraryName);
#if DEBUG
            Console.WriteLine($"OpenMpHandle: 0x{OpenMpHandle.ToString("X")}");
#endif
            CudaHandle = PlatformApi.NativeLibrary.LoadLibrary(CudaLibraryName);
#if DEBUG
            Console.WriteLine($"CudaHandle: 0x{CudaHandle.ToString("X")}");
#endif
            CudartHandle = PlatformApi.NativeLibrary.Load(CudartLibraryName);
#if DEBUG
            Console.WriteLine($"CudartHandle: 0x{CudartHandle.ToString("X")}");
#endif
            CublasLtHandle = PlatformApi.NativeLibrary.Load(CublasLtLibraryName);
#if DEBUG
            Console.WriteLine($"CublasLtHandle: 0x{CublasLtHandle.ToString("X")}");
#endif
            CublasHandle = PlatformApi.NativeLibrary.Load(CublasLibraryName);
#if DEBUG
            Console.WriteLine($"CublasHandle: 0x{CublasHandle.ToString("X")}");
#endif
            CusparseHandle = PlatformApi.NativeLibrary.Load(CusparseLibraryName);
#if DEBUG
            Console.WriteLine($"CusparseHandle: 0x{CusparseHandle.ToString("X")}");
#endif
        }
        private static void RuntimeTest()
        {
            //if(Thread.CurrentThread.GetApartmentState() != ApartmentState.STA || Thread.CurrentThread.IsBackground || Thread.CurrentThread.IsThreadPoolThread || !Thread.CurrentThread.IsAlive)
            //{
            //    KokkosLibraryException.Throw("Kokkos Library must be initialized on the main (STA) thread.");
            //}

            if (KokkosLibrary.IsLoaded)
            {
                KokkosLibraryException.Throw("Kokkos Library has not been initialized.");
            }
        }
        public static void Load()
        {
            if (!IsLoaded)
            {
                if (OpenMpHandle == 0)
                {
                    KokkosLibraryException.Throw(OpenMpLibraryName + "failed to load.");
                }

                if (CudartHandle == 0)
                {
                    KokkosLibraryException.Throw(CudartLibraryName + "failed to load.");
                }

                KokkosCoreHandle = PlatformApi.NativeLibrary.Load(KokkosCoreLibraryName);

                Kernel32.Native.DisableThreadLibraryCalls(KokkosCoreHandle);

                KokkosCoreModuleHandle = Kernel32.Native.GetModuleHandle(KokkosCoreLibraryName);

#if DEBUG
                Console.WriteLine($"KokkosCoreHandle: 0x{KokkosCoreHandle.ToString("X")}");
#endif

                if (KokkosCoreHandle == 0)
                {
                    KokkosLibraryException.Throw(KokkosCoreLibraryName + "failed to load.");
                }

                KokkosContainersHandle = PlatformApi.NativeLibrary.Load(KokkosContainersLibraryName);

                Kernel32.Native.DisableThreadLibraryCalls(KokkosContainersHandle);

                KokkosContainersModuleHandle = Kernel32.Native.GetModuleHandle(KokkosContainersLibraryName);

#if DEBUG
                Console.WriteLine($"KokkosContainersHandle: 0x{KokkosContainersHandle.ToString("X")}");
#endif
                if (KokkosContainersHandle == 0)
                {
                    KokkosLibraryException.Throw(KokkosContainersLibraryName + "failed to load.");
                }

                //KokkosKernelsHandle = PlatformApi.NativeLibrary.Load(KokkosKernelsLibraryName);

                //Kernel32.Native.DisableThreadLibraryCalls(KokkosKernelsHandle);

                //KokkosKernelsModuleHandle = Kernel32.Native.GetModuleHandle(KokkosKernelsLibraryName);

                IsLoaded = true;
            }
        }
Example #5
0
        public static void Unload()
        {
            if (!PlatformApi.NativeLibrary.Free(ModuleHandle))
            {
                KokkosLibraryException.Throw(RuntimeKokkosLibraryName + "failed to unload.");
            }
            else
            {
                Handle       = (nint)0;
                ModuleHandle = (nint)0;
            }

            //KokkosCoreLibrary.Unload();

            //Kernel32.UnmapViewOfFile(Kernel32.GetModuleHandleA(RuntimeKokkosLibraryName + ".dll"));
            //Kernel32.UnmapViewOfFile(Kernel32.GetModuleHandleA(KokkosCoreLibrary.KokkosCoreLibraryName));

            IsLoaded = false;
        }
Example #6
0
        public static unsafe void Load()
        {
            if (!IsLoaded)
            {
                KokkosCoreLibrary.Load();

#if DEBUG
                Console.WriteLine("Loading " + RuntimeKokkosLibraryName);
#endif

                // if(!NativeLibrary.TryLoad(RuntimeKokkosLibraryName,
                //                           typeof(KokkosLibrary).Assembly,
                //                           DllImportSearchPath.UseDllDirectoryForDependencies,
                //                           out Handle))
                // {
                //     KokkosLibraryException.Throw();
                // }

                PlatformApi.NativeLibrary.LoadLibrary("nvcuda");

                Handle = PlatformApi.NativeLibrary.Load(RuntimeKokkosLibraryName);

                ModuleHandle = Kernel32.Native.GetModuleHandle(RuntimeKokkosLibraryName + ".dll");

                nint getApiHandle = PlatformApi.NativeLibrary.GetExport(ModuleHandle, "GetApi");

                if (getApiHandle == 0)
                {
                    getApiHandle = Handle + 0x000019E0;
                }

                if (getApiHandle == 0)
                {
                    KokkosLibraryException.Throw("'runtime.Kokkos.NET::GetApi' not found.");
                }

                if (getApiHandle != 0) //NativeLibrary.TryGetExport(ModuleHandle, "GetApi", out nint getApiHandle))
                {
                    GetApi = Marshal.GetDelegateForFunctionPointer <GetApiDelegate>(getApiHandle);

                    Api = GetApi(1);

                    Allocate = Marshal.GetDelegateForFunctionPointer <AllocateDelegate>(Api.AllocatePtr);

                    Reallocate = Marshal.GetDelegateForFunctionPointer <ReallocateDelegate>(Api.ReallocatePtr);

                    _free = Marshal.GetDelegateForFunctionPointer <FreeDelegate>(Api.FreePtr);

                    _initialize = Marshal.GetDelegateForFunctionPointer <InitializeDelegate>(Api.InitializePtr);

                    InitializeSerial = Marshal.GetDelegateForFunctionPointer <InitializeSerialDelegate>(Api.InitializeSerialPtr);

                    InitializeOpenMP = Marshal.GetDelegateForFunctionPointer <InitializeOpenMPDelegate>(Api.InitializeOpenMPPtr);

                    InitializeCuda = Marshal.GetDelegateForFunctionPointer <InitializeCudaDelegate>(Api.InitializeCudaPtr);

                    _initializeThreads = Marshal.GetDelegateForFunctionPointer <InitializeThreadsDelegate>(Api.InitializeThreadsPtr);

                    _initializeArguments = Marshal.GetDelegateForFunctionPointer <InitializeArgumentsDelegate>(Api.InitializeArgumentsPtr);

                    _finalize = Marshal.GetDelegateForFunctionPointer <FinalizeDelegate>(Api.FinalizePtr);

                    FinalizeSerial = Marshal.GetDelegateForFunctionPointer <FinalizeSerialDelegate>(Api.FinalizeSerialPtr);

                    FinalizeOpenMP = Marshal.GetDelegateForFunctionPointer <FinalizeOpenMPDelegate>(Api.FinalizeOpenMPPtr);

                    FinalizeCuda = Marshal.GetDelegateForFunctionPointer <FinalizeCudaDelegate>(Api.FinalizeCudaPtr);

                    _finalizeAll = Marshal.GetDelegateForFunctionPointer <FinalizeAllDelegate>(Api.FinalizeAllPtr);

                    _isInitialized = Marshal.GetDelegateForFunctionPointer <IsInitializedDelegate>(Api.IsInitializedPtr);

                    PrintConfiguration = Marshal.GetDelegateForFunctionPointer <PrintConfigurationDelegate>(Api.PrintConfigurationPtr);

                    GetComputeCapability = Marshal.GetDelegateForFunctionPointer <CudaGetComputeCapabilityDelegate>(Api.GetComputeCapabilityPtr);

                    GetDeviceCount = Marshal.GetDelegateForFunctionPointer <CudaGetDeviceCountDelegate>(Api.GetDeviceCountPtr);

                    CreateViewRank0 = Marshal.GetDelegateForFunctionPointer <CreateViewRank0Delegate>(Api.CreateViewRank0Ptr);

                    CreateViewRank1 = Marshal.GetDelegateForFunctionPointer <CreateViewRank1Delegate>(Api.CreateViewRank1Ptr);

                    CreateViewRank2 = Marshal.GetDelegateForFunctionPointer <CreateViewRank2Delegate>(Api.CreateViewRank2Ptr);

                    CreateViewRank3 = Marshal.GetDelegateForFunctionPointer <CreateViewRank3Delegate>(Api.CreateViewRank3Ptr);

                    CreateViewRank4 = Marshal.GetDelegateForFunctionPointer <CreateViewRank4Delegate>(Api.CreateViewRank4Ptr);

                    CreateViewRank5 = Marshal.GetDelegateForFunctionPointer <CreateViewRank5Delegate>(Api.CreateViewRank5Ptr);

                    CreateViewRank6 = Marshal.GetDelegateForFunctionPointer <CreateViewRank6Delegate>(Api.CreateViewRank6Ptr);

                    CreateViewRank7 = Marshal.GetDelegateForFunctionPointer <CreateViewRank7Delegate>(Api.CreateViewRank7Ptr);

                    CreateViewRank8 = Marshal.GetDelegateForFunctionPointer <CreateViewRank8Delegate>(Api.CreateViewRank8Ptr);

                    CreateView = Marshal.GetDelegateForFunctionPointer <CreateViewDelegate>(Api.CreateViewPtr);

                    GetLabel = Marshal.GetDelegateForFunctionPointer <GetLabelDelegate>(Api.GetLabelPtr);

                    GetSize = Marshal.GetDelegateForFunctionPointer <GetSizeDelegate>(Api.GetSizePtr);

                    GetStride = Marshal.GetDelegateForFunctionPointer <GetStrideDelegate>(Api.GetStridePtr);

                    GetExtent = Marshal.GetDelegateForFunctionPointer <GetExtentDelegate>(Api.GetExtentPtr);

                    CopyTo = Marshal.GetDelegateForFunctionPointer <CopyToDelegate>(Api.CopyToPtr);

                    GetValue = Marshal.GetDelegateForFunctionPointer <GetValueDelegate>(Api.GetValuePtr);

                    SetValue = Marshal.GetDelegateForFunctionPointer <SetValueDelegate>(Api.SetValuePtr);

                    RcpViewToNdArray = Marshal.GetDelegateForFunctionPointer <RcpViewToNdArrayDelegate>(Api.RcpViewToNdArrayPtr);

                    ViewToNdArray = Marshal.GetDelegateForFunctionPointer <ViewToNdArrayDelegate>(Api.ViewToNdArrayPtr);
                }

                GetNumaCount = Marshal.GetDelegateForFunctionPointer <GetNumaCountDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "GetNumaCount"));

                GetCoresPerNuma = Marshal.GetDelegateForFunctionPointer <GetCoresPerNumaDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "GetCoresPerNuma"));

                GetThreadsPerCore = Marshal.GetDelegateForFunctionPointer <GetThreadsPerCoreDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "GetThreadsPerCore"));

                Shepard2dSingle = Marshal.GetDelegateForFunctionPointer <Shepard2dSingleDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "Shepard2dSingle"));

                Shepard2dDouble = Marshal.GetDelegateForFunctionPointer <Shepard2dDoubleDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "Shepard2dDouble"));

                NearestNeighborSingle = Marshal.GetDelegateForFunctionPointer <NearestNeighborSingleDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "NearestNeighborSingle"));

                NearestNeighborDouble = Marshal.GetDelegateForFunctionPointer <NearestNeighborDoubleDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "NearestNeighborDouble"));

                CountLineEndingsSerial = Marshal.GetDelegateForFunctionPointer <CountLineEndingsSerialDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "CountLineEndingsSerial"));

                CountLineEndingsOpenMP = Marshal.GetDelegateForFunctionPointer <CountLineEndingsOpenMPDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "CountLineEndingsOpenMP"));

                CountLineEndingsCuda = Marshal.GetDelegateForFunctionPointer <CountLineEndingsCudaDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "CountLineEndingsCuda"));

                IpcCreate           = Marshal.GetDelegateForFunctionPointer <IpcCreateDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcCreate"));
                IpcCreateFrom       = Marshal.GetDelegateForFunctionPointer <IpcCreateFromDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcCreateFrom"));
                IpcOpenExisting     = Marshal.GetDelegateForFunctionPointer <IpcOpenExistingDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcOpenExisting"));
                IpcDestory          = Marshal.GetDelegateForFunctionPointer <IpcDestoryDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcDestory"));
                IpcClose            = Marshal.GetDelegateForFunctionPointer <IpcCloseDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcClose"));
                IpcGetMemoryPointer = Marshal.GetDelegateForFunctionPointer <IpcGetMemoryPointerDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcGetMemoryPointer"));
                IpcGetDeviceHandle  = Marshal.GetDelegateForFunctionPointer <IpcGetDeviceHandleDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcGetDeviceHandle"));
                IpcGetSize          = Marshal.GetDelegateForFunctionPointer <IpcGetSizeDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcGetSize"));

                IpcMakeViewFromPointer = Marshal.GetDelegateForFunctionPointer <IpcMakeViewFromPointerDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcMakeViewFromPointer"));

                IpcMakeViewFromHandle = Marshal.GetDelegateForFunctionPointer <IpcMakeViewFromHandleDelegate>(PlatformApi.NativeLibrary.GetExport(ModuleHandle, "IpcMakeViewFromHandle"));

#if DEBUG
                Console.WriteLine("Loaded " + RuntimeKokkosLibraryName + $"@ 0x{Handle.ToString("X")}");
#endif

                IsLoaded = true;
            }
        }