xref: /phasta/phSolver/common/get_time.c (revision 712d3df0b59ebebaaeaea358162c8d2c043c6e08)
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <mpi.h>
4 #include <stdint.h>
5 #include <assert.h>
6 #ifdef __bgq__
7 #include <hwi/include/bqc/A2_inlines.h>
8 #endif
9 #include "FCMangle.h"
10 
11 #define get_time FortranCInterface_GLOBAL_(get_time, GET_TIME)
12 #define get_max_time_diff FortranCInterface_GLOBAL_(get_max_time_diff, GET_MAX_TIME_DIFF)
13 static double multiplier;
14 
15 void get_max_time_diff(uint64_t* first, uint64_t* last, uint64_t* c_first, uint64_t* c_last, char* lbl)
16 {
17 	uint64_t tmp = ((*last)-(*first));
18 	uint64_t iresult;
19 	double result;
20 	uint64_t c_result;
21 	int rank;
22 	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23         assert(tmp > 0);
24 	MPI_Allreduce(&tmp, &iresult, 1, MPI_UINT64_T, MPI_MAX, MPI_COMM_WORLD);
25 	result = iresult*multiplier;
26 	if(__builtin_expect(*c_last < *c_first, 0))
27 	{
28 		tmp = *c_last + (UINT64_MAX - *c_first);
29 	}
30 	else
31 	{
32 		tmp = ((*c_last)-(*c_first));
33 	}
34 	MPI_Allreduce(&tmp, &iresult, 1, MPI_UINT64_T, MPI_MAX, MPI_COMM_WORLD);
35 	c_result = iresult;
36 	if(rank == 0)
37 		printf("%s: %.12g seconds, %llu cycles\n", lbl, result, c_result);
38 }
39 
40 /*
41 #ifdef __APPLE__
42 #include <mach/time.h>
43 //todo
44 #endif
45 */
46 
47 //#if defined(__bgq__) || defined(__APPLE__)
48 #if (1)
49 #include <sys/time.h>
50 void get_time(uint64_t* rv, uint64_t* cycle)
51 {
52 	struct timeval time;
53 	int ret;
54 	ret = gettimeofday(&time, NULL);
55 	if(ret != 0) perror("gettimeofday failed: ");
56 	*rv = ((time.tv_sec*1000000)+(time.tv_usec));
57 	multiplier=0.000001;//10.0e-6;
58 #ifdef __bgq__
59 	*cycle = GetTimeBase();
60 #endif
61 }
62 #else
63 #include <time.h>
64 void get_time(uint64_t* rv)
65 {
66 	struct timespec time;
67         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
68         *rv = ((time.tv_sec*1000000000)+(time.tv_nsec));
69 	multiplier = 0.000000001;
70 }
71 #endif
72