【问题标题】:Why Multithreading in four cores slower than single threading in one core为什么四核多线程比一核单线程慢
【发布时间】:2019-02-20 08:49:09
【问题描述】:

我正在做一个嵌入式视觉项目,在不使用颜色的情况下识别圆圈,我制作的程序总处理时间为 600 毫秒,但我观察到只有执行 4 个二维卷积的程序块有总处理时间300msec 的时间,这意味着只有该程序块需要总程序时间的 50%。

这是一个使用二维卷积和openCV的filter2D命令的简单示例,我们看到它在main函数中顺序排列,看看执行四次需要多少时间。行代码:cout

#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;



}

由于4个卷积是独立处理的,为了减少处理时间选择使用多线程,但是当我实现它的时候我很惊讶执行多线程的progamma块需要600msec的处理时间,导致现在整个程序处理时间为 900 毫秒,也就是说,它变得更糟,因为实现多线程可能比使用带线程的程序慢。

这是之前用openCV的filter2D命令使用二维卷积但使用多线程的简单程序,但是每个内核或线程中的每个二维卷积,也就是它们是并行处理的,我想应该是并行执行。对于这个具体的简单示例程序,显示程序“Execution time : 0.029157sec”,这比使用单线程的处理时间要长一点,显然是错误的,因为这个时间应该是使用a的执行时间的四分之一单线程,不是这样的。

#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;



}

有人能帮我找出在多线程运行时可能会减慢程序速度的原因吗?也许是一个解决方案?提前感谢您的所有帮助和建议!

【问题讨论】:

  • 是的,一些算法在多线程中的表现比在一个执行线程中的表现更差。多线程并不是让一切运行得更快的神奇按钮。必须了解多线程的工作原理,并实现所使用的任何算法,以便它可以利用多线程。例如,在所示算法中,主计算循环似乎是受互斥保护的。这意味着您肯定不会在代码的主要部分中并行执行多个线程。另外,现在您还要承受互斥锁开销!
  • clock() 测量 CPU 时间。改为测量“墙上时间”。

标签: c++ multithreading opencv raspberry-pi3


【解决方案1】:

您绝对应该阅读一些关于并发的优秀教程或书籍,因为您的程序代码中存在一些严重的问题:

  • 引用 Sam Varshavchik 的评论“多线程不是让一切运行得更快的神奇按钮”:您正在创建四个线程并在所有线程上执行相同的功能。这不会自动在四个线程之间拆分计算,但只会导致相同的函数被执行 4 次,所以你没有得到任何好处。您需要将数据拆分并手动分发到不同的线程。
  • 您正在使用互斥锁保护一切,但您应该只保护在不同线程之间共享的变量。因此,例如int val_local 是一个局部变量,每个线程都有自己的,这里不需要互斥锁。这同样适用于所有其他变量,counter 除外。 for-loop 也不需要互斥体,因为它不使用任何全局变量。互斥锁会导致阻塞和抢占,从而导致性能显着下降,因此您应该只在需要时使用它们。
  • 我不知道下面的代码部分会发生什么,但你在这里使用了全局变量。当它们同时写入时,可能会导致竞争条件,并且需要通过互斥锁进行同步:

    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

【讨论】:

    猜你喜欢
    • 2014-07-21
    • 2015-07-09
    • 2012-01-24
    • 1970-01-01
    • 2022-01-18
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多