using System;
using Matrox.MatroxImagingLibrary;
namespace MAppBenchmark
{
public struct PROC_PARAM
{
public MIL_ID MilSourceImage;
public MIL_ID MilDestinationImage;
}
public class Program
{
private static readonly string IMAGE_FILE = MIL.M_IMAGE_PATH + "LargeWafer.mim";
private const int ROTATE_ANGLE = -15;
private const double MINIMUM_BENCHMARK_TIME = 1.0;
private const int ESTIMATION_NB_LOOP = 5;
private const int DEFAULT_NB_LOOP = 100;
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilDisplayImage = MIL.M_NULL;
MIL_ID MilSystemOwnerApplication = MIL.M_NULL;
MIL_ID MilSystemCurrentThreadId = MIL.M_NULL;
PROC_PARAM ProcessingParam = new PROC_PARAM();
double TimeAllCores = 0.0;
double TimeAllCoresNoCS = 0.0;
double TimeOneCore = 0.0;
double FPSAllCores = 0.0;
double FPSAllCoresNoCS = 0.0;
double FPSOneCore = 0.0;
MIL_INT NbCoresUsed = 0;
MIL_INT NbCoresUsedNoCS = 0;
bool UseGPUSystem = false;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MsysInquire(MilSystem, MIL.M_OWNER_APPLICATION, ref MilSystemOwnerApplication);
MIL.MsysInquire(MilSystem, MIL.M_CURRENT_THREAD_ID, ref MilSystemCurrentThreadId);
UseGPUSystem = MIL.MsysInquire(MilSystem, MIL.M_SYSTEM_TYPE, MIL.M_NULL) == MIL.M_SYSTEM_GPU_TYPE;
MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilDisplayImage);
MIL.MdispSelect(MilDisplay, MilDisplayImage);
ProcessingInit(MilSystem, ref ProcessingParam);
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)
{
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);
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);
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);
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);
MIL.MappControlMp(MilSystemOwnerApplication, MIL.M_MP_USE, MIL.M_DEFAULT, MIL.M_DISABLE, MIL.M_NULL);
Benchmark(ref ProcessingParam, ref TimeOneCore, ref FPSOneCore);
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);
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(ref ProcessingParam, ref TimeAllCores, ref FPSAllCores);
MIL.MbufCopy(ProcessingParam.MilDestinationImage, MilDisplayImage);
Console.WriteLine("Using GPU: {0,5:0.000} ms ({1,6:0.0} fps)", TimeAllCores, FPSAllCores);
}
Console.WriteLine("Press <Enter> to end.");
Console.ReadKey();
ProcessingFree(ref ProcessingParam);
MIL.MdispSelect(MilDisplay, MIL.M_NULL);
MIL.MbufFree(MilDisplayImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
private static void Benchmark(ref PROC_PARAM ProcParamPtr, ref double Time, ref double FramesPerSecond)
{
MIL_INT EstimatedNbLoop = DEFAULT_NB_LOOP;
double StartTime = 0.0;
double EndTime = 0.0;
double MinTime = 0;
MIL_INT n;
MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref StartTime);
ProcessingExecute(ref ProcParamPtr);
MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref EndTime);
MinTime = EndTime - StartTime;
for (n = 0; n < ESTIMATION_NB_LOOP; n++)
{
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref StartTime);
ProcessingExecute(ref ProcParamPtr);
MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref EndTime);
Time = EndTime - StartTime;
MinTime = (Time < MinTime) ? Time : MinTime;
}
if (MinTime > 0)
{
EstimatedNbLoop = (MIL_INT)(MINIMUM_BENCHMARK_TIME / MinTime) + 1;
}
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref StartTime);
for (n = 0; n < EstimatedNbLoop; n++)
{
ProcessingExecute(ref ProcParamPtr);
}
MIL.MthrWait(MIL.M_DEFAULT, MIL.M_THREAD_WAIT, MIL.M_NULL);
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref EndTime);
Time = EndTime - StartTime;
FramesPerSecond = EstimatedNbLoop / Time;
Time = Time * 1000 / EstimatedNbLoop;
}
private static void ProcessingInit(MIL_ID MilSystem, ref PROC_PARAM ProcParamPtr)
{
MIL.MbufAllocColor(MilSystem,
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_BAND, MIL.M_NULL),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_X, MIL.M_NULL),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_Y, MIL.M_NULL),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_BIT, MIL.M_NULL) + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC, ref ProcParamPtr.MilSourceImage );
MIL.MbufLoad(IMAGE_FILE, ProcParamPtr.MilSourceImage);
MIL.MbufAllocColor(MilSystem,
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_BAND, MIL.M_NULL),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_X, MIL.M_NULL),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_Y, MIL.M_NULL),
MIL.MbufDiskInquire(IMAGE_FILE, MIL.M_SIZE_BIT, MIL.M_NULL) + MIL.M_UNSIGNED,
MIL.M_IMAGE + MIL.M_PROC, ref ProcParamPtr.MilDestinationImage);
}
private static void ProcessingExecute(ref PROC_PARAM ProcParamPtr)
{
MIL.MimRotate(ProcParamPtr.MilSourceImage, ProcParamPtr.MilDestinationImage, ROTATE_ANGLE,
MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_BILINEAR + MIL.M_OVERSCAN_CLEAR);
}
private static void ProcessingFree(ref PROC_PARAM ProcParamPtr)
{
MIL.MbufFree(ProcParamPtr.MilSourceImage);
MIL.MbufFree(ProcParamPtr.MilDestinationImage);
}
}
}