我知道它存在这个线程 openMP 性能
但这里我的例子很简单
C代码:
int MaFunc(size_t szGlobalWorkSize)
{
int iGID = 0;
float *pfResult = (float *)calloc(szGlobalWorkSize * 100, sizeof(float));
float fValue = 0.5f;
struct timeval tim;
gettimeofday(&tim, NULL);
double tLaunch1=tim.tv_sec+(tim.tv_usec/1000000.0);
#pragma omp parallel for
for (iGID = 0; iGID < (int)szGlobalWorkSize * 100; iGID++)
{
pfResult[iGID] = fValue;
// printf("Element %d traité par le thread %d \n",iGID,omp_get_thread_num());
}
gettimeofday(&tim, NULL);
double tLaunch2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf Time OMP\n", tLaunch2-tLaunch1);
}
当我在没有 openMP 的情况下使用 openMP 0.015s 对使用 openMP 的 0.045 秒(szGlobalworkSize = 131072)时,此示例的时间会增加
我使用这行 gcc:gcc -march=native -fopenmp -O3 MyCode.c -lm
gcc (GCC) 4.8.2 20140120 (红帽 4.8.2-15)
编辑1:
int MyFunc2()
{
int iGID = 0;
int j = 0;
//float *pfResult = (float *)calloc(szGlobalWorkSize * 100, sizeof(float));
float *pfResult = (float *)valloc(szGlobalWorkSize * 100* sizeof(float));
float fValue = 0.5f;
struct timeval tim;
gettimeofday(&tim, NULL);
double tLaunch1=tim.tv_sec+(tim.tv_usec/1000000.0);
double time = omp_get_wtime();
int iChunk = getpagesize();
int iSize = ((int)szGlobalWorkSize * 100) / iChunk;
// #pragma omp parallel
#pragma omp parallel for
for (iGID = 0; iGID < iSize; iGID++)
{
for (j = 0; j < iChunk; j++)
{
pfResult[iGID * iChunk + j] = fValue;
//pfResult[iGID] = fValue;
}
// printf("Element %d traité par le thread %d \n",iGID,omp_get_thread_num());
}
time = omp_get_wtime() - time;
gettimeofday(&tim, NULL);
double tLaunch2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf Time OMP\n", tLaunch2-tLaunch1);
printf("Pagesize=%d\n", getpagesize());
printf("%.6lf Time OMP2\n", time);
}
同时与块与 memalign
用线程计时编辑 2
#pragma omp parallel private(dLocalTime)
{
pdTime[omp_get_thread_num()] = omp_get_wtime();
printf("Thread Begin %d Time %f\n", omp_get_thread_num(), pdTime[omp_get_thread_num()] );
#pragma omp for
for (iGID = 0; iGID < iSize; iGID++)
{
// for (j = 0; j < iChunk; j++)
{
// pfResult[iGID * iChunk + j] = fValue;
pfResult[iGID] = fValue;
}
}
//dLocalTime = (omp_get_wtime() - dLocalTime);
pdTime[omp_get_thread_num()] = (omp_get_wtime() - pdTime[omp_get_thread_num()]);
printf("Thread End %d Time %f\n", omp_get_thread_num(), pdTime[omp_get_thread_num()]);
// printf("End Element %d traité par le thread %d \n",0,tid);
}
每个线程需要 0.015,总共需要 0.045,因此 openmp 中有一个 0.03 的修复部分。奇怪的是,即使尺寸很大,我们也看到 openmp 的这个修复部分和工作较少的线程需要与整个大小相同的时间(48 个线程这里)
谢谢