Beispiel #1
0
        static void Main(string[] args)
        {
            MIL_ID     MilApplication            = MIL.M_NULL;       // Application identifier.
            MIL_ID     MilSystem                 = MIL.M_NULL;       // System identifier.
            MIL_ID     MilDisplay                = MIL.M_NULL;       // Display identifier.
            MIL_ID     MilDisplayImage           = MIL.M_NULL;       // Image buffer identifier.
            MIL_ID     MilSystemOwnerApplication = MIL.M_NULL;       // System's owner application.
            MIL_ID     MilSystemCurrentThreadId  = MIL.M_NULL;       // System's current thread identifier.
            PROC_PARAM ProcessingParam           = new PROC_PARAM(); // Processing parameters.
            double     TimeAllCores              = 0.0;              // Timer variables.
            double     TimeAllCoresNoCS          = 0.0;
            double     TimeOneCore               = 0.0;
            double     FPSAllCores               = 0.0;     // FPS variables.
            double     FPSAllCoresNoCS           = 0.0;
            double     FPSOneCore                = 0.0;
            MIL_INT    NbCoresUsed               = 0;       // Number of CPU Core used.
            MIL_INT    NbCoresUsedNoCS           = 0;
            bool       UseGPUSystem              = false;

            // Allocate defaults.
            MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);

            // Get the system's owner application.
            MIL.MsysInquire(MilSystem, MIL.M_OWNER_APPLICATION, ref MilSystemOwnerApplication);

            // Get the system's current thread identifier.
            MIL.MsysInquire(MilSystem, MIL.M_CURRENT_THREAD_ID, ref MilSystemCurrentThreadId);

            // Determine if the processing will be done on the GPU.
            UseGPUSystem = MIL.MsysInquire(MilSystem, MIL.M_SYSTEM_TYPE, MIL.M_NULL) == MIL.M_SYSTEM_GPU_TYPE;

            // Restore image into an automatically allocated image buffer.
            MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilDisplayImage);

            // Display the image buffer.
            MIL.MdispSelect(MilDisplay, MilDisplayImage);

            // Allocate the processing objects.
            ProcessingInit(MilSystem, ref ProcessingParam);

            // Pause to show the original image.
            Console.WriteLine();
            Console.WriteLine("PROCESSING FUNCTION BENCHMARKING:");
            Console.WriteLine("---------------------------------");
            Console.WriteLine();
            Console.WriteLine("This program times a processing function under ");
            Console.WriteLine("different conditions.");
            Console.WriteLine("Press <Enter> to start.");
            Console.WriteLine();
            Console.ReadKey();
            Console.WriteLine("PROCESSING TIME FOR {0}x{1}:",
                              MIL.MbufInquire(ProcessingParam.MilDestinationImage, MIL.M_SIZE_X, MIL.M_NULL),
                              MIL.MbufInquire(ProcessingParam.MilDestinationImage, MIL.M_SIZE_Y, MIL.M_NULL));
            Console.WriteLine("------------------------------");
            Console.WriteLine();

            if (!UseGPUSystem)
            {
                // Benchmark the processing function with multi-processing.
                // --------------------------------------------------------
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_ENABLE, MIL.M_NULL);
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_CORE_SHARING, MIL.M_DEFAULT, MIL.M_ENABLE, MIL.M_NULL);
                MIL.MthrInquireMp(MilSystemCurrentThreadId, MIL.M_CORE_NUM_EFFECTIVE, MIL.M_DEFAULT, MIL.M_DEFAULT, ref NbCoresUsed);
                if (NbCoresUsed > 1)
                {
                    Benchmark(ref ProcessingParam, ref TimeAllCores, ref FPSAllCores);

                    // Show the resulting image and the timing results.
                    MIL.MbufCopy(ProcessingParam.MilDestinationImage, MilDisplayImage);

                    Console.WriteLine("Using multi-processing   ({0,3} CPU cores): {1,5:0.000} ms ({2,6:0.0} fps)", NbCoresUsed, TimeAllCores, FPSAllCores);
                }
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NULL);
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_CORE_SHARING, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NULL);

                // Benchmark the processing function with multi-processing but no hyper-threading.
                // --------------------------------------------------------------------------------

                // If Hyper-threading is available on the PC, test if the performance is better
                // with it disabled. Sometimes, too many cores sharing the same CPU resources that
                // are already fully occupied can reduce the overall performance.
                //
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_ENABLE, MIL.M_NULL);
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_CORE_SHARING, MIL.M_DEFAULT, MIL.M_DISABLE, MIL.M_NULL);
                MIL.MthrInquireMp(MilSystemCurrentThreadId, MIL.M_CORE_NUM_EFFECTIVE, MIL.M_DEFAULT, MIL.M_DEFAULT, ref NbCoresUsedNoCS);
                if (NbCoresUsedNoCS != NbCoresUsed)
                {
                    Benchmark(ref ProcessingParam, ref TimeAllCoresNoCS, ref FPSAllCoresNoCS);

                    // Show the resulting image and the timing results.
                    MIL.MbufCopy(ProcessingParam.MilDestinationImage, MilDisplayImage);

                    Console.WriteLine("Using multi-processing   ({0,3} CPU cores): {1,5:0.000} ms ({2,6:0.0} fps), no Hyper-Thread.", NbCoresUsedNoCS, TimeAllCoresNoCS, FPSAllCoresNoCS);
                }
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NULL);
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_CORE_SHARING, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NULL);


                // Benchmark the processing function without multi-processing.
                // -----------------------------------------------------------
                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_DISABLE, MIL.M_NULL);
                Benchmark(ref ProcessingParam, ref TimeOneCore, ref FPSOneCore);

                // Show the resulting image and the timing results.
                MIL.MbufCopy(ProcessingParam.MilDestinationImage, MilDisplayImage);

                Console.WriteLine("Without multi-processing (  1 CPU core ): {0,5:0.000} ms ({1,6:0.0} fps)", TimeOneCore, FPSOneCore);
                Console.WriteLine();

                MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NULL);

                // Show a comparative summary of the timing results.
                if (NbCoresUsed > 1)
                {
                    Console.WriteLine("Benchmark is {0:0.0} times faster with multi-processing.", TimeOneCore / TimeAllCores);
                    Console.WriteLine();
                }

                if (NbCoresUsedNoCS != NbCoresUsed)
                {
                    Console.WriteLine("Benchmark is {0:0.0} times faster with multi-processing and no Hyper-Thread.", TimeOneCore / TimeAllCoresNoCS);
                    Console.WriteLine();
                }
            }
            else
            {
                // Benchmark on GPU system.
                Benchmark(ref ProcessingParam, ref TimeAllCores, ref FPSAllCores);

                // Show the resulting image and the timing results.
                MIL.MbufCopy(ProcessingParam.MilDestinationImage, MilDisplayImage);

                Console.WriteLine("Using GPU: {0,5:0.000} ms ({1,6:0.0} fps)", TimeAllCores, FPSAllCores);
            }

            // Wait for a key press.
            Console.WriteLine("Press <Enter> to end.");
            Console.ReadKey();

            // Free all allocations.
            ProcessingFree(ref ProcessingParam);
            MIL.MdispSelect(MilDisplay, MIL.M_NULL);
            MIL.MbufFree(MilDisplayImage);
            MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
        }