Ejemplo n.º 1
0
 public void StartDevice()
 {
     //Open device.
     HOperatorSet.OpenComputeDevice(
         hv_DeviceIdentifier.TupleSelect(DeviceIndex),
         out hv_DeviceHandle);
     HOperatorSet.ActivateComputeDevice(hv_DeviceHandle);
     //DeviceName = hv_DeviceName.TupleSelect(DeviceIndex);
     //HOperatorSet.SetComputeDeviceParam(hv_DeviceHandle, "alloc_pinned", "false");
     //HOperatorSet.SetComputeDeviceParam(hv_DeviceHandle, "asynchronous_execution", "true");
     //HOperatorSet.SetComputeDeviceParam(hv_DeviceHandle, "image_cache_capacity", 0);
     //HOperatorSet.SetComputeDeviceParam(hv_DeviceHandle, "buffer_cache_capacity", 0);
 }
Ejemplo n.º 2
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image = null, ho_ImageAffinTrans = null;

        // Local control variables

        HTuple hv_WindowHandle = new HTuple(), hv_DeviceIdentifier = null;
        HTuple hv_Index = null, hv_DeviceName = new HTuple(), hv_DeviceVendor = new HTuple();
        HTuple hv_Message = new HTuple(), hv_DeviceIndex = null;
        HTuple hv_DeviceHandle = new HTuple(), hv_HomMat2DIdentity = new HTuple();
        HTuple hv_HomMat2DScale = new HTuple(), hv_HomMat2D = new HTuple();
        HTuple hv_Loops = new HTuple(), hv_Before = new HTuple();
        HTuple hv_After = new HTuple(), hv_TimeGPU = new HTuple();
        HTuple hv_Grayval = new HTuple(), hv_Pointer = new HTuple();
        HTuple hv_Type = new HTuple(), hv_Width = new HTuple();
        HTuple hv_Height = new HTuple(), hv_TimeGPUinclTransfer = new HTuple();
        HTuple hv_TimeCPU = new HTuple(), hv_SpeedUp = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_ImageAffinTrans);
        try
        {
            //This example shows how to use compute devices with HALCON.
            //
            dev_update_off();
            //dev_close_window(...);
            dev_open_window_fit_size(0, 0, 640, 480, -1, -1, out hv_WindowHandle);
            set_display_font(hv_ExpDefaultWinHandle, 16, "mono", "true", "false");
            //
            //Get list of all available compute devices.
            HOperatorSet.QueryAvailableComputeDevices(out hv_DeviceIdentifier);
            //
            //End example if no device could be found.
            if ((int)(new HTuple((new HTuple(hv_DeviceIdentifier.TupleLength())).TupleEqual(
                                     0))) != 0)
            {
                ho_Image.Dispose();
                ho_ImageAffinTrans.Dispose();

                return;
            }
            //
            //Display basic information on detected devices.
            disp_message(hv_ExpDefaultWinHandle, new HTuple(new HTuple("Found ") + (new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                                               ))) + " Compute Device(s):", "window", 12, 12, "black", "true");
            for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                  )) - 1); hv_Index = (int)hv_Index + 1)
            {
                HOperatorSet.GetComputeDeviceInfo(hv_DeviceIdentifier.TupleSelect(hv_Index),
                                                  "name", out hv_DeviceName);
                HOperatorSet.GetComputeDeviceInfo(hv_DeviceIdentifier.TupleSelect(hv_Index),
                                                  "vendor", out hv_DeviceVendor);
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[hv_Index] = (((("Device #" + hv_Index) + ": ") + hv_DeviceVendor) + " ") + hv_DeviceName;
            }
            disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 42, 12, "white",
                         "false");
            disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
            HDevelopStop();
            //
            //Perform a small benchmark for all devices.
            for (hv_DeviceIndex = 0; (int)hv_DeviceIndex <= (int)((new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                              )) - 1); hv_DeviceIndex = (int)hv_DeviceIndex + 1)
            {
                HOperatorSet.ClearWindow(hv_ExpDefaultWinHandle);
                disp_message(hv_ExpDefaultWinHandle, ("Activating Device #" + hv_DeviceIndex) + " and performing benchmark...",
                             "window", 12, 12, "black", "true");
                //Open device.
                HOperatorSet.OpenComputeDevice(hv_DeviceIdentifier.TupleSelect(hv_DeviceIndex),
                                               out hv_DeviceHandle);
                //Deactivate asynchronous execution to obtain reliable benchmarks.
                HOperatorSet.SetComputeDeviceParam(hv_DeviceHandle, "asynchronous_execution",
                                                   "false");
                //Call affine_trans_image on this device and measure the obtained speed-up.
                //Activate the compute device and perform the initialization of affine_trans_image.
                //If this is omitted, the initialization is performed with the first
                //call of affine_trans_image.
                HOperatorSet.InitComputeDevice(hv_DeviceHandle, "affine_trans_image");
                HOperatorSet.ActivateComputeDevice(hv_DeviceHandle);
                //create benchmark input data
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, "rings_and_nuts");
                HOperatorSet.HomMat2dIdentity(out hv_HomMat2DIdentity);
                HOperatorSet.HomMat2dScale(hv_HomMat2DIdentity, 0.9, 0.9, 320, 240, out hv_HomMat2DScale);
                HOperatorSet.HomMat2dRotate(hv_HomMat2DScale, 0.78, 320, 240, out hv_HomMat2D);
                //Set number of benchmark loops.
                hv_Loops = 200;
                //One call to fill caches.
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                              "constant", "false");
                HOperatorSet.CountSeconds(out hv_Before);
                HTuple end_val50  = hv_Loops;
                HTuple step_val50 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val50, step_val50); hv_Index = hv_Index.TupleAdd(step_val50))
                {
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                }
                HOperatorSet.CountSeconds(out hv_After);
                hv_TimeGPU = ((hv_After - hv_Before) * 1000.0) / hv_Loops;
                //
                //Perform a slightly modified version of this benchmark to include
                //host->device and device->host transfer.
                HOperatorSet.GetGrayval(ho_Image, 0, 0, out hv_Grayval);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                              "constant", "false");
                HOperatorSet.CountSeconds(out hv_Before);
                HTuple end_val61  = hv_Loops;
                HTuple step_val61 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val61, step_val61); hv_Index = hv_Index.TupleAdd(step_val61))
                {
                    //set_grayval ensures that Image must be transferred to the device again.
                    HOperatorSet.SetGrayval(ho_Image, 0, 0, hv_Grayval);
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                    //get_image_pointer1 ensures that ImageAffinTrans is transferred back to host.
                    HOperatorSet.GetImagePointer1(ho_ImageAffinTrans, out hv_Pointer, out hv_Type,
                                                  out hv_Width, out hv_Height);
                }
                HOperatorSet.CountSeconds(out hv_After);
                hv_TimeGPUinclTransfer = ((hv_After - hv_Before) * 1000.0) / hv_Loops;
                //
                //Deactivate the device and perform the same benchmark on the CPU.
                HOperatorSet.DeactivateComputeDevice(hv_DeviceHandle);
                ho_ImageAffinTrans.Dispose();
                HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                              "constant", "false");
                HOperatorSet.CountSeconds(out hv_Before);
                HTuple end_val75  = hv_Loops;
                HTuple step_val75 = 1;
                for (hv_Index = 1; hv_Index.Continue(end_val75, step_val75); hv_Index = hv_Index.TupleAdd(step_val75))
                {
                    ho_ImageAffinTrans.Dispose();
                    HOperatorSet.AffineTransImage(ho_Image, out ho_ImageAffinTrans, hv_HomMat2D,
                                                  "constant", "false");
                }
                HOperatorSet.CountSeconds(out hv_After);
                hv_TimeCPU = ((hv_After - hv_Before) * 1000.0) / hv_Loops;
                hv_SpeedUp = hv_TimeCPU / hv_TimeGPU;
                hv_Message = "affine_trans_image runtimes:";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[1] = ((("Compute Device #" + hv_DeviceIndex) + " (excl. transfer): ") + (hv_TimeGPU.TupleString(
                                                                                                        ".2f"))) + " ms";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[2] = ((("Compute Device #" + hv_DeviceIndex) + " (incl. transfer): ") + (hv_TimeGPUinclTransfer.TupleString(
                                                                                                        ".2f"))) + " ms";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[3] = ("CPU:                                " + (hv_TimeCPU.TupleString(
                                                                               ".2f"))) + " ms";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[4] = " ";
                if (hv_Message == null)
                {
                    hv_Message = new HTuple();
                }
                hv_Message[5] = "Potential speedup: " + (hv_SpeedUp.TupleString(".1f"));
                disp_message(hv_ExpDefaultWinHandle, hv_Message, "window", 42, 12, "white",
                             "false");
                if ((int)(new HTuple(hv_DeviceIndex.TupleLess((new HTuple(hv_DeviceIdentifier.TupleLength()
                                                                          )) - 1))) != 0)
                {
                    disp_continue_message(hv_ExpDefaultWinHandle, "black", "true");
                    HDevelopStop();
                }
            }
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();
            ho_ImageAffinTrans.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Image.Dispose();
        ho_ImageAffinTrans.Dispose();
    }