test_papi.c 8.53 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
#if HAVE_CONFIG_H
#  ifndef _H_CONFIG
#    define _H_CONFIG
#    include <config.h>
#  endif
#endif

#if HAVE_STDLIB_H
#  include <stdlib.h>
#endif
#if HAVE_STRING_H
#  include <string.h>
#endif
#if HAVE_STRINGS_H
#  include <strings.h>
#endif
#if HAVE_MATH_H
#  include <math.h>
#endif
#if _OPENMP
#  include <omp.h>
#endif
#if HAVE_SYS_TIME_H
#  include <sys/time.h>
#endif
#if HAVE_TIME_H
#  include <time.h>
#endif

#include "sct.h"

#define INDEX 100
33
#define INS_COUNT_TOLERANCE 5.
34
35
36
37
38
39
40
41
42
43
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
#define MY_TIMER_MAX 2
static const int timer_max = MY_TIMER_MAX;
static int timer[MY_TIMER_MAX];


void
dummy( void *array )
{
/* Confuse the compiler so as not to optimize
   away the flops in the calling routine    */
/* Cast the array as a void to eliminate unused argument warning */
        ( void ) array;
}


/* trying to implement rdtsc for each architecture :-)
   to determine actual cpu-frequency */

#if defined(__i386__)

static __inline__ unsigned long long int rdtsc(void)
{
  unsigned long long int x;
     __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
     return x;
}

#elif defined(__x86_64__)

static __inline__ unsigned long long int rdtsc(void)
{
  unsigned int x = 0, y = 0;
  __asm__ __volatile__ ("rdtsc" : "=a"(x), "=d"(y));
  return ( ((unsigned long)x) | (((unsigned long)y)<<32) );
}

#elif defined(__powerpc__)

static __inline__ unsigned long long int rdtsc(void)
{
  timebasestruct_t t;
  unsigned long long int retval;
  int type;

  type = read_real_time(&t, TIMEBASE_SZ);
  time_base_to_time(&t, TIMEBASE_SZ);
  retval = t.tb_high;
  retval = retval<<32;
  retval = retval|t.tb_low;

  return (retval);
}

/* static __inline__ unsigned long long int rdtsc(void) */
/* { */
/*   unsigned long long int result=0; */
/*   unsigned long int upper, lower,tmp; */
/*   __asm__ volatile( */
/*                 "loop:                  \n" */
/*                 "\tmftbu   %0           \n" */
/*                 "\tmftb    %1           \n" */
/*                 "\tmftbu   %2           \n" */
/*                 "\tcmpw    %2,%0        \n" */
/*                 "\tbne     loop         \n" */
/*                 : "=r"(upper),"=r"(lower),"=r"(tmp) */
/*                 ); */
/*   result = upper; */
/*   result = result<<32; */
/*   result = result|lower; */

/*   return(result); */
/* } */

#else

#error "No tick counter is available!"

#endif


static __inline__ double getTime(void) {
  struct timeval tbuf;

  gettimeofday(&tbuf, NULL);

  return ( (double) tbuf.tv_sec + (double) tbuf.tv_usec*1.e-6 );
}


double determineFrequency(void) {
  double start, elapsed, accum=0.0, y;
  int i, flipper=1;
  unsigned long long int x;

  x = rdtsc();
  start = getTime();
  
  while ( (elapsed=getTime()-start) < 1 ) {
    if (flipper == 1) flipper = -1; else flipper = 1;
    for (i=0; i<1000000; i++) {
      accum = accum + (i * (double) flipper);
    }
  }

  x = rdtsc() - x;
  elapsed = getTime() - start;

  y = (double) x/elapsed;

  printf("outputcheck: cpu frequency estimation = %1.3f GHz\n", 1e-9*y);
  return y;
}


int main( int argc, char **argv ) {
  int i, j, k;
  static int pid, tid, nbt, istart, iend;
  static double blocksize, freq;
  int myINDEX;
  double **matrixa;
  double **matrixb;
  static double **mresult;

#pragma omp threadprivate(istart, iend, blocksize, tid, nbt, mresult)

#ifdef HAVE_MPI
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &pid);
#else
  pid = 0;
#endif

  /* init SCT*/
  sct_init(timer_max, "test_papi", SCT_COMM_WORLD);
  for (i=0; i < timer_max; i++) timer[i] = 0;
  char label0[] = "sct-timer";
  char label1[] = "seq-part";
  timer[0] = sct_new_timer(label0);
  timer[1] = sct_new_timer(label1);

  /* Initialize the Matrix arrays */
  myINDEX = (pid+1)*INDEX;

  matrixa = (double **) malloc(myINDEX*sizeof(double *));
  matrixb = (double **) malloc(myINDEX*sizeof(double *));
  for (i=0; i<myINDEX; i++) {
    matrixa[i] = (double *) malloc(myINDEX*sizeof(double));
    matrixb[i] = (double *) malloc(myINDEX*sizeof(double));
  }

  for ( i = 0; i < myINDEX; i++ ) {
    for ( j = 0; j < myINDEX; j++) {
      matrixa[i][j] = ( float ) rand(  ) * ( float ) 1.1;
      matrixb[i][j] = ( float ) rand(  ) * ( float ) 1.1;
    }
  }

#pragma omp parallel private(i,j)
  {
#if _OPENMP
    tid = omp_get_thread_num();
    nbt = omp_get_num_threads();
#else
    tid = 0;
    nbt = 1;
#endif
    iend = 0;
    blocksize = 0.0;
    /* find some blocksize which produces inbalance in threaded region */
    for (i=0; i<nbt; i++) blocksize += 1./pow(1.5,i);
    blocksize = myINDEX / blocksize;
    for (i=0; i<=tid; i++) {
      istart = iend;
      iend += (int)(blocksize/pow(1.5,i));
    }
    if (tid == nbt-1) iend = myINDEX;

    mresult = (double **) malloc((iend-istart)*sizeof(double *));
    for (i=istart; i<iend; i++)
      mresult[i-istart] = (double *) malloc(myINDEX*sizeof(double));

    for (int i=istart; i<iend; i++) {
      for (int j=0; j<myINDEX; j++) {
        mresult[i-istart][j] = 0.0;
      }
    }
  }

#if _OPENMP
  sct_start(timer[1]);
#endif

#pragma omp parallel private(i,j,k)
  {
    sct_start(timer[0]);

    /* Matrix-Matrix multiply */
    for (i=istart; i<iend; i++)
      for (j=0; j<myINDEX; j++)
        for (k=0; k<myINDEX; k++)
          mresult[i-istart][j] = mresult[i-istart][j] + matrixa[i][k] * matrixb[k][j];

    dummy( ( void * ) mresult );

    sct_stop(timer[0]);
  }

#if _OPENMP
  sct_stop(timer[1]);
#endif

  /* free memory */
  for (i=0; i<myINDEX; i++) {
    free(matrixa[i]);
    free(matrixb[i]);
  }
  free(matrixa);
  free(matrixb);
#pragma omp parallel private(i)
  {
    for (i=istart; i<iend; i++) free(mresult[i-istart]);
    free(mresult);
  }

  sct_report(SCT_GETENV, SCT_GETENV, SCT_GETENV);
#ifdef HAVE_MPI
  MPI_Barrier(MPI_COMM_WORLD);
  fflush(stdout);
#endif

  int evc;
  char *s = getenv ("SCT_EVENTCOUNTERS");
#ifdef HAVE_MPI
  if (!s) evc = 1;
#else
  if (!s) evc = 2;
#endif
  else {
    if ( !strcmp(s,"0") || !strcasecmp(s,"SCT_WITHOUT_EVENTCOUNTERS") ) evc = 0;
    else {
      if ( !strcmp(s,"1") || !strcasecmp(s,"SCT_WITH_EVENTCOUNTERS") ) evc = 1;
      else evc = 2;
    }
  }

  // check for tests that might have failed
  int err = 0;
  double ref, act, diff;
  if (evc == 1) {
#if _OPENMP
#pragma omp parallel private(act, ref)
    {
#pragma omp critical
      {
	/* if FLOPS are counted correct */
        /* ref = (double)2*(iend-istart)*myINDEX*myINDEX; */
        /* act = sct_event(timer[0], "PAPI_FP_OPS"); */
        /* diff = fabs(ref-act)/ref*100.; */
        /* printf("outputcheck: PAPI_FP_OPS for proc %i, thread %i: ref %10.2e act %10.2e diff %5.2f %%\n", */
        /*        pid, tid, ref, act, diff); */
        /* printf("             criterion (diff < 1.) ... is allowed to fail if fma is used !\n"); */
	/* if (diff > 1.) err = 1; */

297
	/* else use 'Instructions completed' */
298
	ref = (double)3*(iend-istart)*myINDEX*myINDEX;
299
        act = sct_event(timer[0], "PAPI_TOT_INS");
300
        diff = fabs(ref-act)/ref*100.;
301
        printf("outputcheck: PAPI_TOT_INS for proc %i, thread %i: ref %10.2e act %10.2e diff %5.2f %%\n",
302
               pid, tid, ref, act, diff);
303
304
        printf("             criterion (diff < %5.2f)\n", INS_COUNT_TOLERANCE);
	if (diff > INS_COUNT_TOLERANCE) err = 1;
305
306
307
308
309
310
311
312
313
314
315
      }
    }
#else
    /* if FLOPS are counted correct */
    /* ref = (double)2*(iend-istart)*myINDEX*myINDEX; */
    /* act = sct_event(timer[0], "PAPI_FP_OPS"); */
    /* diff = fabs(ref-act)/ref*100.; */
    /* printf("outputcheck: PAPI_FP_OPS for proc %i: ref %10.2e act %10.2e diff %5.2f %%\n", pid, ref, act, diff); */
    /* printf("             criterion (diff < 1.) ... is allowed to fail if fma is used !\n"); */
    /* if ( diff > 1. ) err = 1; */

316
    /* else use 'Instructions completed' */
317
    ref = (double)3*(iend-istart)*myINDEX*myINDEX;
318
    act = sct_event(timer[0], "PAPI_TOT_INS");
319
    diff = fabs(ref-act)/ref*100.;
320
    printf("outputcheck: PAPI_TOT_INS for proc %i: ref %10.2e act %10.2e diff %5.2f %%\n",
321
	   pid, ref, act, diff);
322
323
    printf("             criterion (diff < %5.2f)\n", INS_COUNT_TOLERANCE);
    if (diff > INS_COUNT_TOLERANCE) err = 1;
324
325
#endif
  }
326
  /* skip check for clock rate ... not accurate enough on HSW and BDW ...
327
328
329
330
331
332
333
334
335
336
337
338
  else if (evc == 2 && pid == 0) {
    ref = determineFrequency();
#if _OPENMP
    act = sct_event(timer[1], "PAPI_TOT_CYC");
#else
    act = sct_event(timer[0], "PAPI_TOT_CYC");
#endif
    diff = fabs(ref-act)/ref*100.;
    printf("outputcheck: PAPI_TOT_CYC (rate) for all procs and threads: ref %10.2e act %10.2e diff %5.2f %%\n", ref, act, diff);
    printf("             criterion (diff < 25.)\n");
    if ( diff > 25. ) err = 1;
  }
339
  */
340
341
342
343
344
345
346

#ifdef HAVE_MPI
  MPI_Finalize();
#endif

  return err;
}