sct_mach.c 10 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * \file sct_mach.c
 * abstraction of machine dependencies, including special libraries
 */
#include "sct_mach.h"

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#else
#error missing string.h header
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#else
#error missing strings.h header
#endif
#include <unistd.h>

#ifdef _AIX
22
#if HAVE_TIME_H
23
24
#  include <time.h>
#endif
25
#endif
26
27

#if (defined (__linux) && (defined(__x86_64__) || defined(__i686__) || defined(__i386__)))
28
#if HAVE_SYS_TIME_H
29
30
#  include <sys/time.h>
#endif
31
#endif
32
33
34
35
36
37

#ifdef _AIX
/* default pmapi counter group 127 for POWER6 ... FPU flop events */
static char default_event_list[] =
  "PAPI_FP_OPS,PAPI_FMA_INS,PAPI_TOT_CYC";
//  "PM_FPU_FLOP,PM_RUN_INST_CMPL,PM_RUN_CYC";
Hendryk Bockelmann's avatar
Hendryk Bockelmann committed
38
#elif (defined (__linux) && (defined(__GNUC__) || defined(__ICC) || defined(__SUNPRO_C) || defined(__PGIC__)) && (defined(__x86_64__)||defined(__i686__)||defined(__i386__)))
39
40
41
/* default PAPI FPU flop events */
static char default_event_list[] =
  "PAPI_LD_INS,PAPI_TOT_INS,PAPI_TOT_CYC";
42
43
#elif (defined(__APPLE__) && defined(__GNUC__) && defined(__x86_64__))
static char default_event_list[] = "";
44
45
46
47
#else
#  error unknown system
#endif

48
49
50
// estimated frequency of the system
static double mhz_inv = 1.;

51
52
53
54
55
56
57
58
59
60
static int on_err(const char *funstr, const int errcode, const char *fname, const int line);

#ifdef HAVE_LIBPAPI
#ifdef _OPENMP
static unsigned long wrap_omp_get_thread_num(void);
#endif
static int get_event_names(char ***event_name, int *event_num);
static int gen_eset_id(eset_type *eset);
#endif

61
62
63
64
65
// reference point for absolute time
#if (HAVE_TIME_H || HAVE_SYS_TIME_H)
static struct timeval prg_start_time;
#endif
static char prg_start_time_string[40];
66
67
68

// implementation:
void sct_mach_init() {
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#if ( SCT_RTM & SCT_RTM_RDTSCP )
  // estimate mhz by 1 sec sleep
  int mhz = 0;
  tmark_type tm1;

  tm1 = rdtscp();
  sleep(1);
  mhz = (int) ((rdtscp() - tm1)/1000000);
  mhz_inv = 1. / ((double)mhz);

  fprintf(stderr,"(sct_mach_init) SCT_RTM = %i using mhz = %d\n", SCT_RTM, mhz);
#endif
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
void set_prg_start_time() {
#if (HAVE_TIME_H || HAVE_SYS_TIME_H)
  struct tm * ptm;
  gettimeofday(&prg_start_time, NULL);
  ptm = localtime(&prg_start_time.tv_sec);
  strftime(prg_start_time_string, sizeof(prg_start_time_string), "%Y-%m-%d %H:%M:%S", ptm);
#else
strcpy(prg_start_time_string,"not available");
#endif
}

const char* print_prg_start_time() {
  return prg_start_time_string;
}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

inline double get_tdiff(tmark_type *ptm1, tmark_type *ptm2) {
  double dt;

  // time diff in secs
#if (SCT_RTM == SCT_RTM_READ_REAL_TIME)

  int secs, n_secs;
  // man page recommends to call the conversion routines unconditionally, so we do
  time_base_to_time(ptm1, TIMEBASE_SZ);
  time_base_to_time(ptm2, TIMEBASE_SZ);

  secs   = ptm2->tb_high - ptm1->tb_high;
  n_secs = ptm2->tb_low  - ptm1->tb_low;

  dt = (double)secs + n_secs * 1.e-9;
113

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#elif ( SCT_RTM & (SCT_RTM_OMP_GET_WTIME | SCT_RTM_MPI_WTIME) )

  dt = *ptm2 - *ptm1;

#elif ( SCT_RTM & (SCT_RTM_CLOCK_GETTIME_MONOTONIC | SCT_RTM_CLOCK_GETTIME_REALTIME) )

  dt = (double) (ptm2->tv_sec - ptm1->tv_sec) + 1.e-9 * (double) (ptm2->tv_nsec - ptm1->tv_nsec);

#elif ( SCT_RTM & SCT_RTM_GETTIMEOFDAY )

  dt = (double) (ptm2->tv_sec - ptm1->tv_sec) + 1.e-6 * (double) (ptm2->tv_usec - ptm1->tv_usec);

#elif ( SCT_RTM & SCT_RTM_RDTSCP )

  dt = (double)(*ptm2 - *ptm1) * mhz_inv * 1.e-6;

#else
#error unknown system
#endif
  return dt;
134
135
136
137
138
139
140
141
142
143
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
179
180
181
182
183
184
185
186
187
188
189
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
}


void sct_abort(const char *reason, const char *fname, const int line) {

  if (reason||fname) fprintf(stderr, "[sct] ERROR in file %s, line %d: %s\n",fname, line, reason);

  fflush(stdout);
  fflush(stderr);

#ifdef HAVE_MPI
  int errorcode;
  errorcode = 1;
  MPI_Abort(MPI_COMM_WORLD, errorcode);
#endif

  abort();
}


void sct_warn(const char *reason, const char *fname, const int line) {

  if (reason||fname) fprintf(stderr, "[sct] WARNING in file %s, line %d: %s\n",fname, line, reason);
}


#ifdef HAVE_LIBPAPI
eset_type *sct_new_eventset() {
  eset_type *eset;

  if ( !(eset = calloc(1,sizeof(eset_type))) ) {
    fprintf(stderr,"sct_new_eventdef: calloc failed\n");
    return NULL;
  }

  get_event_names(&eset->name, &eset->en);

  if (eset->en < 1) {
    free(eset);
    fprintf(stderr,"sct_new_eventdef: eset->en < 1\n");
    return NULL;
  }

  eset->valid = 0;
  eset->tn = 0;
  eset->id = NULL;

  if (!gen_eset_id(eset)) {
    sct_del_eventset(eset);
    fprintf(stderr,"sct_new_eventdef: !gen_eset_id(eset)\n");
    return NULL;
  }

  return eset;
}
#endif


#ifdef HAVE_LIBPAPI
void sct_del_eventset(eset_type *eset) {

  if (!eset) return;
  
  for (int ie=eset->en-1; ie>=0; ie--) {
    if (eset->name[ie]) free(eset->name[ie]);
  }

  if (eset->id) free(eset->id);

  free(eset);
}
#endif


#ifdef HAVE_LIBPAPI
int sct_start_eventset(eset_type *eset) {

  if (!eset) return 1;

  int ierr;
  int istate = 0;
  int err = 0;

  if (omp_in_parallel()) sct_abort("sct_start_eventset called in parallel region", __FILE__, __LINE__);

  // start counting; returns true if successful

#ifdef _OPENMP
#pragma omp parallel
#endif
  {
    int tid = omp_get_thread_num();

#ifdef _OPENMP
#pragma omp critical
#endif
    {
#ifdef DEBUG
      printf("start_eventset: tid=%d, id[tid] = %d, en = %d\n",tid, eset->id[tid], eset->en);
#endif
      if ( (ierr = PAPI_start(eset->id[tid])) != PAPI_OK ) err++;
    }
  }

  if (err) return on_err("PAPI_start", ierr, __FILE__, __LINE__);
  
  return 1;

}
#endif


#ifdef HAVE_LIBPAPI
int sct_stop_eventset(eset_type *eset) {

  if (!eset) return 1;

  int ierr;
  eval_type eval[eset->en];

  if (omp_in_parallel()) sct_abort("sct_stop_eventset called in parallel region", __FILE__, __LINE__);

  // stop counting; returns true if successful
  // we throw the end values away

  for (int tid=0; tid< eset->tn; tid++) {
    if ( (ierr = PAPI_stop(eset->id[tid], eval)) != PAPI_OK )
      return on_err("PAPI_stop", ierr, __FILE__, __LINE__);
  }

  return 1;
}
#endif


#ifdef HAVE_LIBPAPI
#ifdef _OPENMP
  static unsigned long wrap_omp_get_thread_num(void) {
    return (unsigned long)omp_get_thread_num();
  }
#endif
#endif


#ifdef HAVE_LIBPAPI
static int get_event_names(char ***event_name, int *event_num) {

  // returns number of successfully parsed tokens
  char *evlist;
  char *t, *w;
  int tn, tlen;
  char *evnames[10];

  *event_num = 0;

  if ( !(evlist = getenv ("SCT_EVENT_LIST")) ) evlist = default_event_list;
  tn = 0;
  t = strtok(evlist," ,:;\t\r\n");
  while(t) {
    if (tn>=10) {
      fprintf(stderr, "ERROR: event_list has too many elements (resticted to 10)\n");
      return 0;
    }
    tlen = strlen(t);
    w = (char*)malloc(tlen+1);
    strncpy(w, t, tlen);
    w[tlen]='\0';
    evnames[tn] = w;
    tn++;
    t = strtok(NULL," ,:;");
  }

  if (!tn) {
    fprintf(stderr, "ERROR: empty event_list\n");
    return 0;
  }

  // now update passed event_name and event_num
  *event_num = tn;

  if ( !(*event_name = calloc(tn, sizeof(char*))) )
    sct_abort("memory allocation for event_names failed", __FILE__, __LINE__);

  for (int i=0; i<tn; i++) (*event_name)[i] = evnames[i];

  return *event_num;
}
#endif



static int on_err(const char *funstr, const int errcode, const char *fname, const int line) {

  // error handler; always returns false
#ifdef HAVE_LIBPAPI
  fprintf(stderr, "PAPI ERROR in file %s line %d: %s failed. %d %s\n",
	  fname, line, funstr, errcode, PAPI_strerror(errcode));
#else
  fprintf(stderr, "PAPI ERROR in file %s line %d: %s failed. %d\n",
	  fname, line, funstr, errcode);

#endif
  return 0;
}


#ifdef HAVE_LIBPAPI
static int gen_eset_id(eset_type *eset) {

  if (!eset) return 1;

  const PAPI_hw_info_t *hw_info = NULL;

  // initialization of counters; returns true if successful
  int ierr, code, retcode;

  if ( (ierr = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
    return on_err("PAPI_library_init", ierr, __FILE__, __LINE__);

#ifdef _OPENMP
  if ( sizeof(unsigned long) == sizeof(int) ) { 
    ierr = PAPI_thread_init((unsigned long (*)(void))(omp_get_thread_num));
  } else {
    fprintf(stderr, "sct - warning: installing wrapper for omp_get_thread_num\n");
    ierr = PAPI_thread_init(wrap_omp_get_thread_num);
  }
  if ( ierr != PAPI_OK ) return on_err("PAPI_thread_init", ierr, __FILE__, __LINE__);
#endif

  // debug setting
#ifdef DEBUG
  if ( (ierr = PAPI_set_debug(PAPI_VERB_ESTOP)) != PAPI_OK )
    return on_err("PAPI_library_init", ierr, __FILE__, __LINE__);
#else
  if ( (ierr = PAPI_set_debug(PAPI_QUIET)) != PAPI_OK )
    return on_err("PAPI_library_init", ierr, __FILE__, __LINE__);
#endif

#ifdef DEBUG
  if ( !(hw_info = PAPI_get_hardware_info()) ) return on_err("PAPI_get_hardware_info", ierr, __FILE__, __LINE__);
  printf("PAPI_get_hardware_info: %d ncpu, %d nnodes, %d totalcpus, %s vendor, %f MHz\n",
         hw_info->ncpu, hw_info->nnodes, hw_info->totalcpus, hw_info->vendor_string, hw_info->mhz);
#endif

  eset->tn = omp_get_max_threads();

  if ( !(eset->id  = calloc(eset->tn, sizeof(int))) ) sct_abort("calloc failed", __FILE__, __LINE__);;

#ifdef _OPENMP
#pragma omp parallel private(ierr) reduction(+:retcode)
#endif
  {
    int tid = omp_get_thread_num();

    eset->id[tid] = PAPI_NULL;
    retcode = 1; // 1==OK, 0==failure
    if ( (ierr = PAPI_create_eventset(&eset->id[tid])) != PAPI_OK ) {
#ifdef DEBUG
      printf("PAPI_create_eventset failed for tid [%d]\n",tid);
#endif
      retcode = on_err("PAPI_create_eventset", ierr, __FILE__, __LINE__);
    }
    else {
      for (int ie=0; ie<eset->en; ie++) {
	if ( (ierr = PAPI_event_name_to_code(eset->name[ie], &code)) != PAPI_OK ) {
#ifdef DEBUG
          printf("PAPI_event_name_to_code failed for event [%s]\n",eset->name[ie]);
#endif
          retcode = on_err("PAPI_event_name_to_code", ierr, __FILE__, __LINE__);
          break;
	}
	if ( (ierr = PAPI_add_event(eset->id[tid], code)) != PAPI_OK ) {
#ifdef DEBUG
          printf("PAPI_add_event failed for code [%d]\n",code);
#endif
          retcode = on_err("PAPI_add_event", ierr, __FILE__, __LINE__);
	  break;
	}
      }
    }
  }
#ifdef DEBUG
  printf("PAPI event init finished with [%d]\n",retcode);
#endif
  if (retcode == eset->tn) eset->valid = 1;
  return retcode;
}
#endif