sct_mach.h 6.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * sct_mach.h: abstraction of machine dependencies, including special libraries
 *
 */

#ifndef _H_SCT_MACH
#define _H_SCT_MACH

#if HAVE_CONFIG_H
#  ifndef _H_CONFIG
#    define _H_CONFIG
#    include <config.h>
#  endif
#endif

#if HAVE_STDIO_H
#  include <stdio.h>
#endif

// system includes:
#if HAVE_SYS_TIME_H
#  include <sys/time.h>
#endif
#if HAVE_TIME_H
#  include <time.h>
#endif

#if (defined(_AIX) && defined(HAVE_SYS_SYSTEMCFG_H))
#  include <sys/systemcfg.h>
#elif (defined (__linux) && (defined(__x86_64__) || defined(__i686__) || defined(__i386__)))
#  if HAVE_STDINT_H
#    include <stdint.h>
33
34
#  endif
#  if HAVE_INTTYPES_H
35
36
#    include <inttypes.h>
#  endif
37
38
39
40
41
42
43
#elif (defined(__APPLE__) && defined(__x86_64__))
#  if HAVE_STDINT_H
#    include <stdint.h>
#  endif
#  if HAVE_INTTYPES_H
#    include <inttypes.h>
#  endif
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#else
#  error unknown system
#endif

//config:
#include "sct_config.h"

void sct_mach_init();

//public+

// OMP:
#ifdef _OPENMP
#  include <omp.h>
#else
/*! \brief fall back definition if we don't have OpenMP:
           we are never inside parallel region */
static int inline omp_in_parallel() { return 0; }
/*! \brief fall back definition if we don't have OpenMP:
           only thread id 0 is present */
static int inline omp_get_thread_num() { return 0; }
/*! \brief fall back definition if we don't have OpenMP:
           only 1 thread is defined */
static int inline omp_get_max_threads() { return 1; }
#endif

// MPI:
#ifdef HAVE_MPI
#  include <mpi.h>
#else
/*! \brief fall back definitions if we don't have MPI:
           use int for communicator */
typedef int MPI_Comm;
enum { MPI_COMM_WORLD };
enum { MPI_COMM_SELF };
enum { MPI_DOUBLE };
#endif
//public-

// PAPI:
#ifdef HAVE_LIBPAPI
#  include "papi.h"
typedef long long eval_type;
#  define SCT_MAX_EVAL LLONG_MAX
#endif

// time mark type
#if   (SCT_RTM == SCT_RTM_READ_REAL_TIME)

typedef  timebasestruct_t tmark_type;

#elif ( SCT_RTM & (SCT_RTM_OMP_GET_WTIME | SCT_RTM_MPI_WTIME) )

typedef double tmark_type;

#elif ( SCT_RTM & (SCT_RTM_CLOCK_GETTIME_MONOTONIC | SCT_RTM_CLOCK_GETTIME_REALTIME) )

typedef struct timespec tmark_type;

#elif ( SCT_RTM & SCT_RTM_GETTIMEOFDAY )

typedef struct timeval tmark_type;

107
108
109
110
#elif ( SCT_RTM & SCT_RTM_RDTSCP )

typedef uint64_t tmark_type;

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#else
#error unknown read time method
#endif

static inline int equal_tmarks(tmark_type *ptm1, tmark_type *ptm2) {
#if   (SCT_RTM == SCT_RTM_READ_REAL_TIME)

  return ( (ptm1->tb_high == ptm2->tb_high) && (ptm1->tb_low == ptm2->tb_low) );

#elif ( SCT_RTM & (SCT_RTM_OMP_GET_WTIME | SCT_RTM_MPI_WTIME) )

  return ( *ptm1 == *ptm2 );

#elif ( SCT_RTM & (SCT_RTM_CLOCK_GETTIME_MONOTONIC | SCT_RTM_CLOCK_GETTIME_REALTIME ) )

  return ( (ptm1->tv_sec == ptm2->tv_sec) && (ptm1->tv_nsec == ptm2->tv_nsec) );

#elif ( SCT_RTM & SCT_RTM_GETTIMEOFDAY )

  return ( (ptm1->tv_sec == ptm2->tv_sec) && (ptm1->tv_usec == ptm2->tv_usec) );

132
133
134
135
#elif ( SCT_RTM & SCT_RTM_RDTSCP )

  return ( *ptm1 == *ptm2 );

136
137
138
139
140
#else
#error unknown read time method
#endif
}

141
142
#if ( SCT_RTM & SCT_RTM_RDTSCP )

143
static inline uint64_t rdtscp(void) {
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  uint32_t low, high;

  /* rdtscp changes RAX,RCX and RDX */
  uint64_t rax, rcx, rdx;
  __asm__ __volatile__( ""
  			: "=a" (rax), "=c" (rcx), "=d" (rdx)/* output*/
  			: /* input */
  			:);

#ifdef DEBUG
  fprintf(stderr,"pre: rax 0x%016"PRIx64"\n",myrax);
  fprintf(stderr,"pre: rcx 0x%016"PRIx64"\n",myrcx);
  fprintf(stderr,"pre: rdx 0x%016"PRIx64"\n",myrdx);
#endif

  __asm__ __volatile__ ("rdtscp"
                        : "=a" (low), "=d" (high));

  /* rewrite RAX,RCX and RDX */
  __asm__ __volatile__ (""
                        :
  			: "a" (rax), "c" (rcx), "d" (rdx)
  			:);

#ifdef DEBUG
  fprintf(stderr,"post: rax 0x%016"PRIx64"\n",myrax);
  fprintf(stderr,"post: rcx 0x%016"PRIx64"\n",myrcx);
  fprintf(stderr,"post: rdx 0x%016"PRIx64"\n",myrdx);
#endif

  return ((low) | ((uint64_t)(high) << 32));
}
#endif


179
180
181
182
183
184
185
186
187
188
// event definition
typedef struct {
  int valid;
  int *id;      // PAPI-id; this is an array with one id per thread
  int tn;       // number of threads
  int en;       // number of events
  char **name;  // array of event-names (size == en)
} eset_type;

// attributes:
Hendryk Bockelmann's avatar
Hendryk Bockelmann committed
189
#if (defined(__linux) && (defined(__GNUC__) || defined(__PGI) || defined(__ICC) || defined(__SUNPRO_C) || defined(__PGIC__)) && (defined(__x86_64__)||defined(__i686__)))
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#   define ATTRIBUTE_NORETURN __attribute__ ((noreturn))
#else
#   define ATTRIBUTE_NORETURN
#endif

// not time critical; implemented in sct_mach.c:
#ifdef HAVE_LIBPAPI
eset_type *sct_new_eventset();
void sct_del_eventset(eset_type *eset);
int sct_start_eventset(eset_type *eset);
int sct_stop_eventset(eset_type *eset);
#endif
void sct_abort(const char *reason, const char *fname, const int line) ATTRIBUTE_NORETURN;
void sct_warn(const char *reason, const char *fname, const int line);

static inline void read_time(tmark_type *tm) {
#if (SCT_RTM == SCT_RTM_READ_REAL_TIME)
  read_real_time(tm, TIMEBASE_SZ);
#elif (SCT_RTM == SCT_RTM_OMP_GET_WTIME)
  *tm = omp_get_wtime();
#elif (SCT_RTM == SCT_RTM_MPI_WTIME)
  *tm = MPI_Wtime();
#elif (SCT_RTM == SCT_RTM_CLOCK_GETTIME_MONOTONIC)
  clock_gettime(CLOCK_MONOTONIC, tm);
#elif (SCT_RTM == SCT_RTM_CLOCK_GETTIME_REALTIME)
  clock_gettime(CLOCK_REALTIME, tm);
#elif (SCT_RTM == SCT_RTM_GETTIMEOFDAY)
  gettimeofday(tm, NULL);
218
219
#elif (SCT_RTM == SCT_RTM_RDTSCP)
  *tm = rdtscp();
220
221
222
223
224
#else
#error unknown read time method
#endif
}

225
226
227
void set_prg_start_time();
const char* print_prg_start_time();

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#ifdef HAVE_LIBPAPI
static inline int sct_read_events(const int tid, const eset_type *eset, eval_type *eval) {
  int err;
  // returns true on success
  if ( (err = PAPI_read(eset->id[tid], eval)) != PAPI_OK) {
#ifdef DEBUG
    fprintf(stderr,"sct_read_events failed, err = %d\n", err);
#endif
    return 0;
  } else {
#ifdef DEBUG
    fprintf(stderr,"sct_read_events: eset->id = %i, eval[0] = %lld\n", *eset->id, eval[0]);
#endif
  return 1;
  }
}
#endif

246
247
// declarations:
double get_tdiff(tmark_type *ptm1, tmark_type *ptm2);
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

#ifdef HAVE_LIBPAPI
static inline void get_ediff(const int evn, const eval_type *evec1, const eval_type *evec2, eval_type *ediff) {
  int ie;

  for (ie = 0; ie < evn; ie++) {
    ediff[ie] = evec2[ie] - evec1[ie];
  }
}
#endif

static inline void update_tsum(tmark_type *ptm1, double *tsum) {
  tmark_type tm2;

  read_time(&tm2);
  *tsum += get_tdiff(ptm1, &tm2);
}

#endif