Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

fortran - CPU TIME OF THREAD

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

in 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
",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
", sum);
}

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

  Calc();

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...