CPU的读取时间 [英] CPU TIME OF THREAD

查看:140
本文介绍了CPU的读取时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How I calculate the time in each thread ? the CPU_time not work in this case , because If the process is multithreaded, the CPU time is the sum for all threads. Pseudocode example:

   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

推荐答案

在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]);
}

这篇关于CPU的读取时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆