This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Linux/PROCESSOR-SDK-AM57X: OpenCL questions

Part Number: PROCESSOR-SDK-AM57X

Tool/software: Linux

Hi, Sir
      I found that use UMat to do remap is much faster than Mat, it is 0.214394 ms using UMat vs. 23.774059ms using Mat, but when I saved the result, the time cost of using  UMat  (about 1156.278259 ms) is much slower than the time cost of using Mat (about 43.951883 ms), I have two questions which bothered me for a long time, the first one is the 0.214394 ms the real time that remap function costed? 
Another one is why UMat cost too much when saving result. It's weird!!! would you please give me any clue or explanation about these two questions? here is my test code:
test result:
 
[Use UMat] remap cost tdiff=6.562949  save file cost tdiff=1156.278259
[Use UMat] remap cost tdiff=0.214394  save file cost tdiff=1148.338498
[Use UMat] remap cost tdiff=0.214557  save file cost tdiff=1147.207314
[Use UMat] remap cost tdiff=0.214394  save file cost tdiff=1147.243101
[Use UMat] remap cost tdiff=0.257014  save file cost tdiff=1147.025940
[Use UMat] remap cost tdiff=0.213419  save file cost tdiff=1153.175880
[Use UMat] remap cost tdiff=0.214070  save file cost tdiff=1147.167787
[Use UMat] remap cost tdiff=0.196501  save file cost tdiff=1147.019434
[Use UMat] remap cost tdiff=0.207562  save file cost tdiff=1164.622407
[Use UMat] remap cost tdiff=0.208051  save file cost tdiff=1146.908983
 
[Use Mat] remap cost tdiff=36.088901  save file cost tdiff=43.951883
[Use Mat] remap cost tdiff=28.747755  save file cost tdiff=43.974168
[Use Mat] remap cost tdiff=28.649016  save file cost tdiff=43.874941
[Use Mat] remap cost tdiff=28.727096  save file cost tdiff=51.569075
[Use Mat] remap cost tdiff=29.357917  save file cost tdiff=55.143837
[Use Mat] remap cost tdiff=23.774059  save file cost tdiff=34.686714
[Use Mat] remap cost tdiff=23.295167  save file cost tdiff=34.747389
[Use Mat] remap cost tdiff=23.226523  save file cost tdiff=34.794237
[Use Mat] remap cost tdiff=23.193826  save file cost tdiff=34.747064
[Use Mat] remap cost tdiff=23.192526  save file cost tdiff=34.813432
  • test code:
    int main()
    {
    
    	cv::Mat map_x, map_y,src_img,dst_img;
    	UMat usrc_img,udst_img,umap_x,umap_y;
    	CountTime ct;
    	int m = 10, n = 10;
    	struct timespec tp0, tp1, tp2, tp3,tp4,tp5;
    	//ready data
    	ct.time_start("ready_data");
    	{
    		src_img = imread("back.jpg",1);
    		src_img.copyTo(usrc_img);
    		dst_img = imread("back.jpg",1);
    		dst_img.copyTo(udst_img);
    		FILE* pfile = NULL;
    		//pfile = fopen("backwide.dat", "rb");
    		pfile = fopen("leftright4front.dat", "rb");
    		if (NULL == pfile)
    		{
    			return -1;
    		}
    		read_mat_from_file(pfile, map_x);
    		read_mat_from_file(pfile, map_y);
    		map_x.copyTo(umap_x);
    		map_y.copyTo(umap_y);
    	}
    	ct.time_end("ready_data"):
    	while(m--)
    	{
    		clock_gettime(CLOCK_MONOTONIC, &tp0);
    		cv::remap(usrc_img, udst_img, umap_x, umap_y, cv::INTER_LINEAR);
    		clock_gettime(CLOCK_MONOTONIC, &tp1);   
    		cv::imwrite("./result_test_img.jpg",udst_img);
    		clock_gettime(CLOCK_MONOTONIC, &tp2);
    		printf ("[Use UMat]  remap cost tdiff=%lf  save file cost tdiff=%lf\n", tdiff_calc(tp0, tp1),tdiff_calc(tp1, tp2));
    	}
    	while(n--)
    	{
    		clock_gettime(CLOCK_MONOTONIC, &tp0);
    		cv::remap(src_img, dst_img, map_x, map_y, cv::INTER_LINEAR);
    		clock_gettime(CLOCK_MONOTONIC, &tp1);   
    		cv::imwrite("./result_test_img.jpg",dst_img);
    		clock_gettime(CLOCK_MONOTONIC, &tp2);
    		printf ("[Use Mat]  remap cost tdiff=%lf  save file cost tdiff=%lf\n", tdiff_calc(tp0, tp1),tdiff_calc(tp1, tp2));  
    	}
    	return 0;
    }
    test result:
     
    [Use UMat] remap cost tdiff=6.562949  save file cost tdiff=1156.278259
    [Use UMat] remap cost tdiff=0.214394  save file cost tdiff=1148.338498
    [Use UMat] remap cost tdiff=0.214557  save file cost tdiff=1147.207314
    [Use UMat] remap cost tdiff=0.214394  save file cost tdiff=1147.243101
    [Use UMat] remap cost tdiff=0.257014  save file cost tdiff=1147.025940
    [Use UMat] remap cost tdiff=0.213419  save file cost tdiff=1153.175880
    [Use UMat] remap cost tdiff=0.214070  save file cost tdiff=1147.167787
    [Use UMat] remap cost tdiff=0.196501  save file cost tdiff=1147.019434
    [Use UMat] remap cost tdiff=0.207562  save file cost tdiff=1164.622407
    [Use UMat] remap cost tdiff=0.208051  save file cost tdiff=1146.908983
     
    [Use Mat] remap cost tdiff=36.088901  save file cost tdiff=43.951883
    [Use Mat] remap cost tdiff=28.747755  save file cost tdiff=43.974168
    [Use Mat] remap cost tdiff=28.649016  save file cost tdiff=43.874941
    [Use Mat] remap cost tdiff=28.727096  save file cost tdiff=51.569075
    [Use Mat] remap cost tdiff=29.357917  save file cost tdiff=55.143837
    [Use Mat] remap cost tdiff=23.774059  save file cost tdiff=34.686714
    [Use Mat] remap cost tdiff=23.295167  save file cost tdiff=34.747389
    [Use Mat] remap cost tdiff=23.226523  save file cost tdiff=34.794237
    [Use Mat] remap cost tdiff=23.193826  save file cost tdiff=34.747064
    [Use Mat] remap cost tdiff=23.192526  save file cost tdiff=34.813432
    
     
    
    

  • Part Number: PROCESSOR-SDK-AM57X

    Tool/software: Linux

    Hi, 

    Is there any OpenCL expert help me to figure out this problem, I am stacking at this problem for a long time.

    Regards!

    Adam.

  • Hi, Adam,

    .  Per this stackoverflow post, UMat might involve OpenCL processing underneath: https://stackoverflow.com/questions/33602675/what-is-the-difference-between-umat-and-mat-in-opencv?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

    While I am checking internally with OpenCV person, could you try turn off OpenCV acceleration if you know how and see what happen?

    Rex

  • When I turn off the openCL , the remap cost same as it runs on the A15, about 22 ms,and save file time is about 58 ms

  • Hi, Adam,

    Thanks for the data, I'll check internally with these data to see if there is any explanation to it.

    Rex

  • Hi, Adam,

    When OpenCL is turned on and if data are in UMat, remap is directed to DSP, i.e. offload to DSP happens, but OpenCL in OpenCV has somewhat different call flow. It submits a task (to DSP OpenCL queue) and returns immediately. This allows parallelization between accelerator (DSP) and CPU, as long as data dependency is satisfied.
    So, when save is requested, CPU waits for end of submitted task before it can actually do the save. DSP in many cases is slower than A15 (NEON operation). It means 1147ms includes: waiting time (for DSP to finish) plus actual write and any format conversion (done on CPU). It also depends a lot on data types used, and if floating point operations are involved.
    Anyway, this can be accelerated if DSP optimized implementation of remap() is created.

    If you want, you can optimize it following guideline in processors.wiki.ti.com/.../OpenCV (chapters 13-17)

    Rex
  • Yeah, actually I already thought of this point, and I already know that the saving-time cost much mainly from get Mat from UMat, and what I want to know possibly is there any trick to less the time from UMat convert to Mat, so that I can avoid writing remap kernel!
  • OK, thank u Rex,then I know remap real cost is about 1147ms on DSP core with unoptimized openCL accelerated. It seems that there is no method
    except to write remap kernel.
  • Adam,

    If that answered your question, then I'll close this thread. If you could, please click "Resolved" button.
    Thanks!

    Rex