Thursday, January 14, 2016

Optimization tips




            


1, the source of my optimized, relatively short, so the display only the modified portion
#include & lt; mkl.h & gt;
// Double distx, disty, distz, dist;
main ()
{
HANDLE hProcess = GetCurrentProcess ();
SetPriorityClass (hProcess, HIGH_PRIORITY_CLASS);
vmlSetMode (VML_NUM_THREADS_OMP_FIXED | VML_LA);
for ()
...
}
int computePot () {
int i, j;
double temp [1000];
#pragma omp parallel num_threads (2) shared (r) private (i, j, temp)
{
#pragma omp for reduction (+: pot) schedule (static, 1)
for (i = 0; i & lt; 1000; i ++) {
for (j = 0; j & lt; i-1; j ++) {
temp [j] = pow ((r [0] [j] - r [0] [i]), 2);
temp [j] + = pow ((r [1] [j] - r [1] [i]), 2);
temp [j] + = pow ((r [2] [j] - r [2] [i]), 2);
}
vdInvSqrt (j, temp, temp);
for (j = 0; j & lt; i-1; j ++)
pot + = temp [j];
}
}
2, using icc compiler parameters
icl / Qopenmp / QaxP / Qipo pl.cpp mkl_c.lib
Because vc2005express version does not support openmp so only a linker
3. Explanation: The program mainly used icc and mkl do optimization, and vtune program for analysis.
Because they can not be compiled (To learn huanyun compilation!), So most of the major tools by friends.

Reply:
4, the use of icc compiler, parameter / Zi can and / QxP / O3, etc. used together, so Vtune where you can see the time statement execution. So start to see major time consumed in computePot () of several statements, the other ignores the use of the icc.
5, take full advantage of the compiler.
2 above, less command line argument / O3
icc There are many parameters and vc compatible. On Optimized for CPU are arch: SSEx (vc there), icc unique / QxX and / QaxX series. Icc32 bit version I used to support the SSE3. For example, when compiled using arch: there is no big difference SSE3 and / QaxP optimization effect, (my estimate).
Use the / after QaxP compiler pot + = and pow's for doing automatic vectorization processing speed increased a lot.
Reply:
6, on mkl has 2.1 is the use of InvSqrt, mkl hand set environmental variable vector function
vmlSetMode (VML_NUM_THREADS_OMP_FIXED | VML_LA);
VML_LA say mkl function takes less accurate algorithm, although the accuracy is low but still able to guarantee the required accuracy contest,
Mkl function shows the really strong!
Fixed number of threads to Openmp pragma
7, for Openmp in for scheduling the use of (static, 1)
This is a check to help get the icc, after use vtune looked at two threads basically balanced.
The following is a specific description, which references the icc help documentation
Use the following general on the parallel construct syntax to instruct OpenMP to loop schedule:

Example
#pragma omp parallel for schedule (kind [, chunk size])


Four different loop scheduling types (kinds) can be provided to OpenMP, as shown in the following table. The optional parameter (chunk), when specified, must be a loop-invariant positive integer.

Kind Description
static
Divide the loop into equal-sized chunks or as equal as possible in the case where the number of loop iterations is not evenly divisible by the number of threads multiplied by the chunk size. By default, chunk size is loop count / number of threads.

Set chunk to 1 to interleave the iterations.

dynamic
Use the internal work queue to give a chunk-sized block of loop iterations to each thread. When a thread is finished, it retrieves the next block of loop iterations from the top of the work queue.

By default, the chunk size is 1. Be careful when using this scheduling hint because of the extra overhead requirement.

guided
Similar to dynamic scheduling, but the chunk size starts off large and shrinks in an effort to reduce the amount of time threads have to go to the work queue to get more work. The optional chunk parameter specifies them minimum size chunk to use.

By default the chunk size is approximately the loop count / number of threads.

runtime
Uses the OMP_SCHEDULE environment variable to specify which one of the three loop-scheduling types should be used.

OMP_SCHEDULE is a string formatted exactly the same as would appear on the parallel construct.


Reply:
guided and dynamic at run time, it will also affect the speed, so I chose static
Reply:
top.
Reply:

Feelings about the use of toolsOptimization of a program mainly depends on the icc, vtune and mkl, really good.
2, icc compiled using the / QaxX argument indeed generate two different code paths. It has a description in the intel Optimization Reference Manual and icc help in order to win the program's compatibility and efficiency, select / QxX parameters can generate one kind of path code, but only upward compatible, intel CPU too old not allowed to run.
Although some cpu instruction set compatible, but the realization of different transport requirements of the new speed-optimized so good, requires a bit harsh.



Reply:
I was like this:
#define THREADS_NUM 2
double dist [NPARTS];
#pragma omp threadprivate (dist)
int main () {
......
}
int computePot ()
{
int thread_id;
omp_set_num_threads (THREADS_NUM);
#pragma omp parallel reduction (+: pot)
{
thread_id = omp_get_thread_num ();
for (int i = thread_id; i & lt; NPARTS; i + = THREADS_NUM)
if (i-1 & gt; 0) pot = loop_vec (i, i-1, pot);
}
return 0;
}
double loop_vec (int i, int count, double pot)
{
double distx, disty, distz;

for (int j = 0; j & lt; count; j ++) {
distx = r [0] [j] - r [0] [i];
disty = r [1] [j] - r [1] [i];
distz = r [2] [j] - r [2] [i];

distx * = distx;
disty * = disty;
distz * = distz;

dist [j] = distx + disty + distz;
}

vdInvSqrt (count, dist, dist);

for (int j = 0; j & lt; count; j ++) pot + = dist [j];
return pot;
}
Reply:
The ideas and I almost ah ^ _ ^
Reply:
The ideas and I almost ah ^ _ ^
-------------------------------------
In fact, this step,
for (int j = 0; j & lt; count; j ++) pot + = dist [j];
There is still much to optimize water ......
Reply:
In fact, the key lies in the treatment of vdInvSqrt, (double * start, double * end) function will replace myinvsqrt if assembler to write, such as direct use flyingdog write void myinvsqrt my vdInvSqrt function (dist, dist + count), the program execution speed and flyingdog comparable. But I still like structure is a good program, you can use the assembler key areas, but the program structure is also very important, a good program structure better play the advantages of automatic optimization compiler, readability good, too.
Reply:
I think this competition program optimization has three main points: First: loop parallelization, second: the cycle automatic vectorization, third: to InvSqrt functions dealing with. In fact, Intel's intention is to let more through the game developers to understand the advantages of Intel development tools, the program gives also specially designed to meet Intel compiler optimization techniques mentioned in the document, the procedure is relatively simple, given ample time , to sell off now completely enough. If the entire program is written in assembler, VC8 and Intel compiler to generate an executable file on the execution speed should be similar. But I still admire can think of to improve the accuracy of the experts by Newton iterative method, really made me learn a lot of things.

No comments:

Post a Comment