MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tsttiming.c
Go to the documentation of this file.
1 /*
2 Copyright (c) 2015, Lawrence Livermore National Security, LLC.
3 Produced at the Lawrence Livermore National Laboratory.
4 Written by Mark C. Miller
5 
6 LLNL-CODE-676051. All rights reserved.
7 
8 This file is part of MACSio
9 
10 Please also read the LICENSE file at the top of the source code directory or
11 folder hierarchy.
12 
13 This program is free software; you can redistribute it and/or modify it under
14 the terms of the GNU General Public License (as published by the Free Software
15 Foundation) version 2, dated June 1991.
16 
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the terms and conditions of the GNU General
20 Public License for more details.
21 
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24 Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26 
27 #include <stdlib.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 #include <macsio_log.h>
32 #include <macsio_timing.h>
33 
34 void func2();
35 
36 void func4()
37 {
38  static int iter = 0;
39  int nanoSecsOfSleeps = random() % 200000;
40  struct timespec ts = {0, nanoSecsOfSleeps};
42  nanosleep(&ts, 0);
43  MT_StopTimer(tid);
44 }
45 
46 void func3()
47 {
48  static int iter = 0;
50  func4();
51  func2();
52  MT_StopTimer(tid);
53 }
54 
55 void func2()
56 {
57  static int iter = 0;
59  sleep(2);
60  MT_StopTimer(tid);
61 }
62 
63 void func1()
64 {
65  static int iter = 0;
68 
69  func2();
70  sleep(1);
71  tid2 = MT_StartTimer("call to func3 from func1", MACSIO_TIMING_ALL_GROUPS, iter++);
72  func3();
73  MT_StopTimer(tid2);
74  func2();
75 
76  MT_StopTimer(tid);
77 }
78 
79 int main(int argc, char **argv)
80 {
81  int i, rank = 0, size = 1;
83  char **timer_strs;
84  int ntimer_strs, maxstrlen;
85 
86 #ifdef HAVE_MPI
87  MPI_Init(&argc, &argv);
88  MPI_Comm_size(MPI_COMM_WORLD, &size);
89  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
90 #endif
91 
92  MACSIO_LOG_DebugLevel = 1; /* should only see debug messages level 1 and 2 */
93  srandom(0xDeadBeef); /* used to vary length of some timers */
94 
95  if (size > 8)
96  {
97  if (!rank)
98  fprintf(stderr, "This test only appropriate for 8 or fewer processors\n");
99  MPI_Abort(MPI_COMM_WORLD, 1);
100  }
101 
102  if (!rank)
103  {
104  fprintf(stderr, "Note: This test involves several calls to "
105  "sleep. It takes ~9 seconds to complete\n");
106  }
107 
108  a = MT_StartTimer("main", MACSIO_TIMING_ALL_GROUPS, 0);
109 
110  func1();
111 
112  if (rank > 2)
113  func4();
114 
115  b = MT_StartTimer("call to func3 from main", MACSIO_TIMING_ALL_GROUPS, 0);
116  func3();
117  MT_StopTimer(b);
118 
119  if (rank < 2)
120  func1();
121 
122  MT_StopTimer(a);
123 
124  MACSIO_TIMING_DumpTimersToStrings(MACSIO_TIMING_ALL_GROUPS, &timer_strs, &ntimer_strs, &maxstrlen);
125 
126 #ifdef HAVE_MPI
127  {
128  int rbuf[2], sbuf[2] = {ntimer_strs, maxstrlen};
129  MPI_Allreduce(sbuf, rbuf, 2, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
130  MACSIO_LOG_MainLog = MACSIO_LOG_LogInit(MPI_COMM_WORLD, "tsttiming.log", rbuf[1]+32, 2*rbuf[0]+4,0);
131  }
132 #else
133  MACSIO_LOG_MainLog = MACSIO_LOG_LogInit(0, "tsttiming.log", maxstrlen+4, ntimer_strs+4, 0);
134 #endif
135 
136  for (i = 0; i < ntimer_strs; i++)
137  {
138  MACSIO_LOG_MSG(Dbg1, (timer_strs[i]));
139  free(timer_strs[i]);
140  }
141  free(timer_strs);
142 
143  MACSIO_TIMING_ReduceTimers(MPI_COMM_WORLD, 0);
144  if (!rank)
145  {
146  MACSIO_TIMING_DumpReducedTimersToStrings(MACSIO_TIMING_ALL_GROUPS, &timer_strs, &ntimer_strs, &maxstrlen);
147  MACSIO_LOG_MSG(Dbg1, ("#####################Reduced Timer Values######################"));
148  for (i = 0; i < ntimer_strs; i++)
149  {
150  MACSIO_LOG_MSG(Dbg1, (timer_strs[i]));
151  free(timer_strs[i]);
152  }
153  free(timer_strs);
154  }
155 
157 
159 
160 #ifdef HAVE_MPI
161  MPI_Finalize();
162 #endif
163 
164  return 0;
165 }
void MACSIO_TIMING_ReduceTimers(MPI_Comm comm, int root)
Reduce timers across MPI tasks.
#define MT_StopTimer(ID)
Convenience macro for stopping a timer.
void func3()
Definition: tsttiming.c:46
void MACSIO_TIMING_ClearTimers(MACSIO_TIMING_GroupMask_t gmask)
Clear a group of timers.
#define MACSIO_TIMING_ALL_GROUPS
Group mask representing all groups.
MACSIO_LOG_LogHandle_t * MACSIO_LOG_LogInit(MPI_Comm comm, char const *path, int line_len, int lines_per_proc, int extra_lines_proc0)
Initialize a log.
Definition: macsio_log.c:113
void func2()
Definition: tsttiming.c:55
void MACSIO_LOG_LogFinalize(MACSIO_LOG_LogHandle_t *log)
Finalize and close an open log Should be called collectively by all processors that created the log...
Definition: macsio_log.c:295
unsigned int MACSIO_TIMING_TimerId_t
int main(int argc, char **argv)
Definition: tsttiming.c:79
int MACSIO_LOG_DebugLevel
Definition: macsio_log.c:44
MACSIO_LOG_LogHandle_t * MACSIO_LOG_MainLog
Log handle for MACSIO's main log.
Definition: macsio_log.c:45
void MACSIO_TIMING_DumpReducedTimersToStrings(MACSIO_TIMING_GroupMask_t gmask, char ***strs, int *nstrs, int *maxlen)
Dump reduced timers to ascii strings.
#define MT_StartTimer(LAB, GMASK, ITER)
Convenience macro for starting a timer.
void MACSIO_TIMING_DumpTimersToStrings(MACSIO_TIMING_GroupMask_t gmask, char ***strs, int *nstrs, int *maxlen)
Dump timers to ascii strings.
void func4()
Definition: tsttiming.c:36
void func1()
Definition: tsttiming.c:63
#define MACSIO_LOG_MSG(SEV, MSG)
Convenience macro for logging a message to the main log.
Definition: macsio_log.h:133