I'm trying to speedup the OpenCV SIFT algorithm with OpenMP on a Intel® Core™ i5-6500 CPU # 3.20GHz × 4. You can find the code in sift.cpp.
The most expensive part is the descriptor computaton, in particular:
static void calcDescriptors(const std::vector<Mat>& gpyr, const std::vector<KeyPoint>& keypoints,
Mat& descriptors, int nOctaveLayers, int firstOctave )
{
int d = SIFT_DESCR_WIDTH, n = SIFT_DESCR_HIST_BINS;
for( size_t i = 0; i < keypoints.size(); i++ )
{
KeyPoint kpt = keypoints[i];
int octave, layer;
float scale;
unpackOctave(kpt, octave, layer, scale);
CV_Assert(octave >= firstOctave && layer <= nOctaveLayers+2);
float size=kpt.size*scale;
Point2f ptf(kpt.pt.x*scale, kpt.pt.y*scale);
const Mat& img = gpyr[(octave - firstOctave)*(nOctaveLayers + 3) + layer];
float angle = 360.f - kpt.angle;
if(std::abs(angle - 360.f) < FLT_EPSILON)
angle = 0.f;
calcSIFTDescriptor(img, ptf, angle, size*0.5f, d, n, descriptors.ptr<float>((int)i));
}
}
The serial version of this function take 52 ms on average.
This for has an high granulatiy: it's executed 604 times (which is keypoints.size() ). The main time consuming component inside the for is calcSIFTDescriptor which takes most of the cycle time computation and it takes on 105 us on average, but it often happens that it can take 200usor 50us.
However, we are incredibly lucky: there is no dependency between each for cycle, so we can just add:
#pragma omp parallel for schedule(dynamic,8)
and obtain an initial speedup. The dynamic option is introduced since it seems it give little better performances than static (don't know why).
The problem is that it's really unstable and doesn't scale. This is the time needed to compute the function in parallel mode:
25ms 43ms 32ms 15ms 27ms 53ms 21ms 24ms
As you can see only once the optimal speedup in a quad-core system is reached (15ms). Most of the times we reach half of the optimal speedup: 25ms in a quadcore system is only half of the theoretical optimal speedup.
Why this happens? How can we improve this?
UPDATE:
As suggested in the comments, I tried to use a bigger dataset. Using an huge image, the serial version takes 13574ms to compute the descriptors, while the parallel version 3704ms with the same quad-core of before. Much better: even if it's not the best theoretical result, it actually scales well. But actually the problem remain, since the previous results are obtained from a typical image.
UPDATE 1: as suggested by the comment, I tried to benchmark without any interval between the execution in an "hot mode" (see comment for more details). Better results are achieved more frequently, but still there is a lot of variations. This are the times (in ms) for 100 runs in hot mode:
43 42 14 26 14 43 13 26 15 51 15 20 14 40 34 15 15 31 15 22 14 21 17 15 14 27 14 16 14 22 14 22 15 15 14 43 16 16 15 28 14 24 14 36 15 32 13 21 14 23 14 15 13 26 15 35 13 32 14 36 14 34 15 40 28 14 14 15 15 35 15 22 14 17 15 23 14 24 17 16 14 35 14 29 14 25 14 32 14 28 14 34 14 30 22 14 15 24 14 31
You can see a lot of good results (14ms, 15ms) but a lot of horrible results also (>40ms). The average is 22ms Notice that there is no at most 4ms of variation in the sequential mode:
52 54 52 52 51 52 52 53 53 52 53 51 52 53 53 54 53 53 53 53 54 53 54 54 53 53 53 52 53 52 51 52 52 53 54 54 54 55 55 55 54 54 54 53 53 52 52 52 51 52 54 53 54 54 54 55 54 54 52 55 52 52 52 51 52 51 52 52 51 51 52 52 53 53 53 53 55 54 55 54 54 54 55 52 52 52 51 51 52 51 51 51 52 53 53 54 53 54 53 55
UPDATE 2:
I've noticed that each CPU utilization during the "hot mode" benchmarking is quite random and also it never reach more than 80%, as shown in the image below:
Instead the image below shows the CPUs utilization while I compile OpenCV through make -j4. As you can see it more stable and used almost 100% of it:
I think that this is variation in the first image are normal since we execute the same short program many times, which is more unstable than one big program. What I don't understand is why we never reach more than 80% of CPU utilization.
I strongly suggest you to use some performance tools such as Paraver (http://www.bsc.es/paraver), TAU (http://www.cs.uoregon.edu/research/tau/home.php) Vampir (https://tu-dresden.de/die_tu_dresden/zentrale_einrichtungen/zih/forschung/projekte/vampir) or even Intel's Vtune (https://software.intel.com/en-us/intel-vtune-amplifier-xe).
These tools will help you understand where threads spends their cycles. With them, you can find whether the application is unbalanced (either by IPC or instructions), whether there is any limitation due to memory bandwidth or false sharing problems, among many other issues.
Related
I was trying to create a gameloop with fps dependent on the speed of its iterations. To achieve this I wanted to use a platform specific timer that (in case of windows) used the timeGetTime function (https://learn.microsoft.com/en-us/windows/desktop/api/timeapi/nf-timeapi-timegettime) to calculate how much time has passed since the last iteration. But I found that the time it costs to call this function is already quite a lot (for a computer). Now I'm wondering if this is the right approach.
I created a simple test that looks like this:
Timer timer();
for (int i=0; i < 60; i++)
cout << timer->get_elt() << endl;
delete timer;
The timer class looks like this: (begin is a DWORD)
Timer::Timer()
{
begin = timeGetTime();
}
int Timer::get_elt()
{
return timeGetTime() - begin;
}
Not very interesting, but here is a example of the result:
0 0 1 3 4 14 15 15 15 16 16 17 17 17 17 18 19 19 19 19 20 20 20 20 20 21 21 21 21 21 22 22 22 22 22 22 23 23 23 25 38 39 39 55 56 56 66 68 68 69 71 71 72 73 73 73 73 73 74 74
I was expecting this to take about 10 milliseconds at most, but on average it took about 64.
What surprised me most about it was how erratic the results were. Sometimes it prints up to 7 times the same number, whereas at other times there are gaps of 12 milliseconds between iterations. I realize this is also because the timer is not accurate, but still. As far as I know your pc should execute this program as fast as it possibly can, is that even true?
If you want to run your game at say 60 fps, you'd have about 16 milliseconds for every loop, and if calling the timer alone takes about 2 milliseconds on average every time, and you still need to process input, update, and render, how is that even possible?
So what should I do here, is timeGetTime something you could use in a gameloop (it's been suggested a lot), or should I think of another function?
I would suggest using the QueryPerformanceCounter instead
https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx
The Timers from Windows Multimedia API is a good choice for animation, games, etc.
The have greatest precision on Windows Platform.
Qt use and qualifies this timers also as precise ones.
http://doc.qt.io/qt-5/qt.html#TimerType-enum
On Windows, Qt will use Windows's Multimedia timer facility (if
available) for Qt::PreciseTimer and normal Windows timers for
Qt::CoarseTimer and Qt::VeryCoarseTimer.
I need to extract a data structure from the memory of an application I am debugging that is a specific amount of bytes wide, preferably in the form of a series of hex pairs. I want to get this data from the command or immediate window in the Visual Studio Debugger. I could achieve this in windbg via the db command, but I am having trouble finding the specific command for Visual Studio. Debug.Print is insufficient, as it stops printing as soon as it encounters a null character.
I know such a command exists as I have used it before, but I can't for the life of me find it. This is what I get for not writing things down.
I was able to find the answer to this after digging through some documentation. The command I wanted was Debug.ListMemory, which is aliased to the d command. The command to print bytes in hex pairs is specifically db /Count:[number of bytes to print] [memory address].
>db /Count:1686 0x0000021f7102d4d0
0x0000021F7102D4D0 48 72 2f 50 73 36 68 75 4e 6c 59 44 44 56 33 33
0x0000021F7102D4E0 38 78 37 4f 55 65 6c 62 6c 6f 51 78 77 66 4e 68
0x0000021F7102D4F0 35 73 4e 35 42 68 4d 67 54 7a 6e 35 6d 36 52 41
...
Assuming that p is a pointer to the array of bytes you can enter watch like this:
(p + start_pos),[items_count]
The hypothesis for my t-test is:
H0: X1-X2=3
Ha: X1-X2!=3
My code for running this t-test in Stata is ttesti 52 12 5.1 57 8 5.1,
which only applies to when the null hypothesis is X1-X2=0.
Is there any option command that I can use to specify the mean difference?
Let me post my comment (related with timat's comment) as an answer:
ttesti 52 12 5.1 57 11 5.1
This corresponds to transforming X2 to Z2 = X2+3 so that the null hypothesis is H0: E(X1-Z2)=0. This transformation is legitimate because sd(X2) equals sd(Z2). Alternatively you can transform X1 to Z1 = X1-3, which corresponds to ttesti 52 9 5.1 57 8 5.1. In fact, any of the following works and gives the same result:
ttesti 52 12 5.1 57 11 5.1
ttesti 52 9 5.1 57 8 5.1
ttesti 52 1 5.1 57 0 5.1
ttesti 52 0 5.1 57 -1 5.1
or whatever as long as the mean differential is 1.
I've got gperftools installed and collecting data, which looks reasonable so far. I see one node (?) that gets sampled a lot - but I'm interested in the callers to that node - I don't see them? I've tried callgrind/kcachegrind also, I feel like I'm missing something? Here's a snippet of the output when using --text
Total: 1844 samples
573 31.1% 31.1% 573 31.1% US_strcpy
185 10.0% 41.1% 185 10.0% US_strstr
167 9.1% 50.2% 167 9.1% US_strlen
63 3.4% 53.6% 63 3.4% PS_CompressTable
58 3.1% 56.7% 58 3.1% LX_LexInternal
51 2.8% 59.5% 51 2.8% US_CStrEql
47 2.5% 62.0% 47 2.5% 0x40472984
40 2.2% 64.2% 40 2.2% PS_DoSets
38 2.1% 66.3% 38 2.1% LX_ProcessCatRange
So I'm interested in seeing the callers to US_strcpy, but I don't seem to have any? I do get a nice call graph from kcachegrind for 0x40472984 (still trying to match that to a symbol)
There are several ways:
a) pprof --web or kcachgrind will show you callers nicely if it is captured correctly. It is sometimes useful to do pprof --traces (only with github.com/google/pprof version). Which will be somewhat like low-tech method that Mike mentioned above.
b) if the data is really unavailable, you're having problem with stacktrace capturing and/or symbolization. For that, build gperftools with libunwind and build all of your program with debug info.
I am using XBee DigiMesh 2.4 API-2 and Raspberry Pi. I broadcast a frame from one node to another.
Frame to transmit:
7e 0 12 10 1 0 0 0 0 0 0 ff ff ff fe 0 0 41 6c 65 78 69
Frame received in the other node:
7e 0 10 90 0 7d 33 a2 0 40 91 57 26 ff fe c2 41 6c 65 78 1e
Byte which is bothering me, is c2. It should be 02. Why it appears in this way?
What is more, checksum is not correct (I read how the checksum should be calculated in API 2 mode).
With byte 0x02 it should be 0xe3 or with byte c2 it should be 0x23. I was trying to obtain the result 0x1e in many ways but I never got this value.
When I broadcast the packet in opposite direction (from second node to the first one) the same problems appear.
Both XBee´s are configurated with 9600 baudrate, no parity. Raspberry Pi UART as well.
----- Edit: I found the answer regarding to C2 byte. C2 is a bit field. C2 = 1100 0010.
Bits 7 and 6 are 11, it means here that it is Digimesh. Bit 1 is set so it is a broadcast packet.
https://dl.dropboxusercontent.com/u/318853/XBee%20900.PNG
Still looking for the reason of this checksum.
You can simplify your code by using API mode 1 and eliminating the need to escape and unescape values as you send and receive them. It really isn't that difficult to have your code figure out framing and ignore 0x7E in the middle of a frame: If you see a 0x7E followed by an invalid length, keep looking. If your frame has a bad checksum, skip the 0x7E and look for the next one.
If you absolutely must use escaping, ensure that the length value and checksum in your frame don't include the escaping bytes, and that you're properly escaping the necessary bytes as you send them.
On the receiving end, unescape the bytes and then calculate the checksum.