CPU的线程时间

时间:2014-08-25 16:57:46

标签: fortran openmp

我如何计算每个帖子的时间?在这种情况下,CPU_time不起作用,因为如果进程是多线程的,则CPU时间是所有线程的总和。 伪代码示例:

   PROGRAM MAIN
    implicit none
    REAL Times_thread1_Started,Times_thread2_Started,....
    REAL Times_thread1_finiched

    !$OMP PARALLEL
    !$OMP DO   !for each thread do :
    call CPU_TIME_thread1(Times_thread1_Started)
    call CPU_TIME_thread2(Times_thread2_Started)
    ..........
    ..........
    !$OMP END DO
    ......................
    ......................
    processing multithread
    ............
    ............
    !$OMP PARALLEL
    !$OMP DO   !for each thread do :
    call CPU_TIME_thread1(Times_thread1_finiched)
    write(*,*) 'Thread1 times:',Times_thread1_finiched-Times_thread1_Started
    call CPU_TIMEE_thread2(Times_thread2)
    write(*,*) 'Thread1 times:',Times_thread1_finiched-Times_thread1_Started
    ..........
    ..........

    !$OMP END DO
    !$OMP END PARALLEL
    END

1 个答案:

答案 0 :(得分:0)

在c ++中:

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <omp.h>

//---------------------------------------------------------
#ifdef __cplusplus
extern "C" {
#endif              /* __cplusplus */
 extern void UseTiming1();
#ifdef __cplusplus
}                               /* extern "C" */
#endif              /* __cplusplus */
struct Thread_time {
    double* Thread;
};
class Timing {
public:
  Timing();
  ~Timing();
  void StartTiming();
  void StopTiming();
  Thread_time GetUserSeconds() const {
      for (int i = 0; i < nthreads; i++){
      time.Thread[i]=double(m_userTime[i])/ 10000000.0;
      }
      //delete[] m_userTime;
    return (time);
  }
private:
  __int64* GetUserTime() const;
  __int64* m_userTime;
  Thread_time time;
  int nthreads;
};

Timing::Timing(){
    #pragma omp parallel
    {
    nthreads=omp_get_num_threads();
    }
    printf("numbzer thread = %d\n",nthreads);
    m_userTime=new __int64[nthreads];
    time.Thread=new double[nthreads];



}
Timing::~Timing(){
    delete[] m_userTime;
    delete[] time.Thread;
}
__int64* Timing::GetUserTime() const {
  FILETIME creationTime;
  FILETIME exitTime;
  FILETIME kernelTime;
  FILETIME userTime;
  __int64 *CurTime;
  CurTime=new __int64[nthreads];
  #pragma omp parallel for  private(creationTime,exitTime,kernelTime,userTime)
  for (int i = 0; i < nthreads; i++){
  GetThreadTimes(GetCurrentThread(),
                 &creationTime, &exitTime,
                 &kernelTime, &userTime);
  CurTime[i] = userTime.dwHighDateTime;
  CurTime[i] <<= 32;
  CurTime[i] += userTime.dwLowDateTime;
  }
  return CurTime;
}

void Timing::StartTiming() {
  m_userTime = GetUserTime();
}

void Timing::StopTiming() {
    //for (int i = 0; i < Number_Thread; i++)
    __int64 *curUserTime;
     curUserTime = GetUserTime();
  for (int i = 0; i < nthreads; i++){
  m_userTime[i] = curUserTime[i] - m_userTime[i];
  }
}
//---------------------------------------------------------

void Calc()
{
  unsigned sum = 0;

 // #pragma omp parallel for reduction(+:sum) num_threads(2)
  for (int i = 0; i < 1000000; i++)
  {
    char str[1000];
    for (int j = 0; j < 999; j++)
      str[j] = char(((i + j) % 254) + 1);
    str[999] = 0;
    for (char c = 'a'; c <= 'z'; c++)
      if (strchr(str, c) != NULL)
        sum += 1;
  }

  printf("sum = %u\n", sum);
}

void UseTiming1()
{
  Timing t;
  t.StartTiming();

  Calc();

  t.StopTiming();
  for (int i = 0; i < 2; i++)
  printf("Thread %d Timing: %.3G seconds.\n", i,t.GetUserSeconds().Thread[i]);
}