I'm doing a project of embedded vision to recognize circles without using color and I have made the program with a total processing time of 600msec but I have observed that only the program block where 4 bidimensional convolutions are executed has a total processing time of 300msec , this means that only that program block demands a time of 50% of the total program time.
This is a simple example of using two-dimensional convolution with the filter2D command of openCV, as we see it arranged sequentially in main function to see how much time it demands when executing four times. the line code: cout << "Execution Time: " << time << endl; in the program display Execution Time: 0.026662sec
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <ctime>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace cv;
using namespace std;
float buf_in[1280] = { 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765, 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765, 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 , 1.3456, -4.75567, 34.4512, 93.65666, -23.564, 0.3445, -20.245, -23.678, 3.5673, -9.4765, -5.643, 10.37422, -43.282, -10.543, 12.567, 23.341, -2.2529, 12.345, 9.5672, -0.2367, 3.3456, -2.75567, 3.4521, 73.2745, 13.7948, 0.9455, 23.6746, -36.678, 28.656, -19.4765, 11.3456, 14.75567, 4.45, 13.65666, -2.564, 4.3445, 0.245, -12.64, 1.5673, -10.4765, -34.3456, -0.75567, 6.45, -1.4566, -3.564, 11.3445, -2.245, -3.678, 31.5673, -1.4765, 71.3456, -14.75567, 14.45, 3.65666, -0.564, 90.3445, 0.245, 39.678, -3.5673, -9.4765, 22.3456, 0.75567, -77.45, 62.65666, -31.564, 1.3445, -0.245, -4.678, 3.5673, 9.4765, 61.3456, 1.75567, -8.45, 4.66, -34.544, 12.3445, -2.245, -3.678, 54.5673, 9.4765 };
float buf_kernel[441] ={ 0.434, 1.4343, 9.434, 5.453, 3.5656, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 6.45,
1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 2.53,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.454,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 4.56,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 9.3434,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 4.656,
1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 3.343,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 2.4545,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.4334,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 7.5665,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 9.4554,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 3.545,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 7.4545,
1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 9.445,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 3.54545,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 4.546,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 6.4545,
5.4343, 9.6556, 5, 34.75, 7.56, 5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.786,
4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 4.556, 5.776, 7.565, 2.545, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 6.5656,
5.56, 3.455, 6.76, 2.334, 8.65, 5.56, 3.455, 6.76, 2.334, 8.65, 4.556, 5.776, 7.565, 2.545, 8.65, 5.4343, 9.6556, 5, 34.75, 7.56, 7.4554,
1.44, 0.5665, 4.566, 7.666, 4.66 , 4.556, 5.776, 7.565, 2.545, 8.65, 1.44, 0.5665, 4.566, 7.666, 4.66, 5.56, 3.455, 6.76, 2.334, 8.65, 7.344};
unsigned t0, t1;
int counter = 0;
void processor(void *arg ){
printf(" Dentro de la funcion\n");
float matriz[1280];
float *buf_out = &matriz[0];
// variable local
int var_local =0;
var_local = var_local +10;
// variable global
counter = counter +10;
Mat mat_out = Mat(32,40, CV_32FC1); //output
Mat mat_flip_kernel; // se cargara la matriz rotada 180°
Mat mat_kernel( Size(21, 21), CV_32FC1, buf_kernel ); // kernel
Mat mat_in( Size(40, 32),CV_32FC1, buf_in); // input
flip(mat_kernel, mat_flip_kernel,-1); // rotando la matriz 180° para la convolucion, porque filter2d solo realiza la correlacion
Point anchor( -1,-1); // para que este centrado en el centro del kerneL
filter2D(mat_in, mat_out, -1, mat_flip_kernel, anchor, 0, BORDER_DEFAULT);
for (int i=0; i<32; i++){
float *ptr_out = mat_out.ptr<float>(i); // ptr_out puntero a float, to 1st element in row i
for (int j=0 ;j<40 ;j++){
buf_out[i*40+j] = ptr_out[j];
cout<<buf_out[i*40 + j]<<"; ";
}
}
// variable local
var_local++;
// variable global
counter++;
printf(" variable local: %d\n",var_local);
printf(" variable global : %d\n",counter);
return ;
}
int main(void){
int i = 0;
t0 = clock();
processor(NULL);
processor(NULL);
processor(NULL);
processor(NULL);
t1 = clock();
double time = (double(t1-t0)/CLOCKS_PER_SEC);
cout << "Execution Time: " << time << endl;
}
As 4 convolutions are processed independently, choose to use multithreading in order to reduce processing time, but when I implement it I am surprised that the progamma block where multithreading is performed requires a processing time of 600msec, causing Now that the total program takes a processing time of 900msec, that is to say, it gets worse, as it is possible that implementing multithreading is slower than using a program with a thread.
This is the previous simple program of using two-dimensional convolution with the filter2D command of openCV but using multithreading, but each bidimensional convolution in each core or thread, that is, they are processed in parallel, I imagine that it should be executed in parallel. for this specific simple example program, the display program" Execution time : 0.029157sec ", which is a bit longer than the processing time using a single thread, obviously it is wrong, because this time should be a quarter of the execution time using a single thread, not it's like that.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <ctime>
#include <opencv2/opencv.hpp>
#include <math.h>
using namespace cv;
using namespace std;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t thread_id[4];
float buf_in[1280] = { for reasons of space limit I do not place the data, but it is the same as the example program above};
float buf_kernel[441] ={for reasons of space limit I do not place the data, but it is the same as the example program above};
unsigned t0, t1;
int counter = 0;
void *thread_processor(void *arg ){
printf(" Dentro de Thread\n");
float matriz[1280];
float *buf_out = &matriz[0];
// variable local
pthread_mutex_lock( &mutex1 );
int var_local =0;
var_local = var_local +10;
pthread_mutex_unlock( &mutex1 );
// variable global
pthread_mutex_lock( &mutex1 );
counter = counter +10;
pthread_mutex_unlock( &mutex1 );
Mat mat_out = Mat(32,40, CV_32FC1); //output
Mat mat_flip_kernel;// = Mat(21,21, CV_32FC1); // se cargara la matriz rotada 180°
Mat mat_kernel( Size(21, 21), CV_32FC1, buf_kernel ); // kernel
Mat mat_in( Size(40, 32),CV_32FC1, buf_in); // input
flip(mat_kernel, mat_flip_kernel,-1); // rotando la matriz 180° para la convolucion, porque filter2d solo realiza la correlacion
Point anchor( -1,-1); // para que este centrado en el centro del kerneL
filter2D(mat_in, mat_out, -1, mat_flip_kernel, anchor, 0, BORDER_DEFAULT);
// pthread_mutex_lock( &mutex1 );
// cout<<"mat_out :"<<mat_out<<endl;
// pthread_mutex_unlock( &mutex1 );
// Pero al usar multithread,por eso cambie a otro tipo de declaracion de puntero que si funciono
pthread_mutex_lock( &mutex1 );
for (int i=0; i<32; i++){
float *ptr_out = mat_out.ptr<float>(i); // ptr_out puntero a float, to 1st element in row 0
for (int j=0 ;j<40 ;j++){
// pthread_mutex_lock( &mutex1 );
buf_out[i*40+j] = ptr_out[j];
// cout<<" Elemento ["<<i<<"] ["<<j<<"] :"<<buf_out[i*40 + j];
cout<<buf_out[i*40 + j]<<"; ";
// pthread_mutex_unlock( &mutex1 );
}
}
pthread_mutex_unlock( &mutex1);
// variable local
pthread_mutex_lock( &mutex1 );
var_local++;
pthread_mutex_unlock( &mutex1 );
// variable global
pthread_mutex_lock( &mutex1 );
counter++;
pthread_mutex_unlock( &mutex1 );
printf(" variable local: %d\n",var_local);
printf(" variable global : %d\n",counter);
printf(" Fuera de Thread\n");
return NULL;
}
int main(void){
int i = 0;
int error;
t0 = clock();
while(i < 4) {
error = pthread_create(&(thread_id[i]), NULL, &thread_processor,NULL);
if (error != 0){
printf("\nthere was a problem creating thread:");
}
else{
printf("\n Thread number %d created.\n", i);
}
i++;
}
for ( i =0 ; i< 4 ; i++){
pthread_join( thread_id[i], NULL);
}
t1 = clock();
double time = (double(t1-t0)/CLOCKS_PER_SEC);
cout << "Execution Time: " << time << endl;
}
Can somebody help me find what might slow the program down when this is ran with multiple threads and maybe a solution? Thank you for all help and advice in advance!