reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
   33
   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
  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
// RUN: %libomp-compile-and-run
/*
  Test for the 'schedule(simd:guided)' clause.
  Compiler needs to generate a dynamic dispatching and pass the schedule
  value 46 to the OpenMP RTL. Test uses numerous loop parameter combinations.
*/
#include <stdio.h>
#include <omp.h>

#if defined(WIN32) || defined(_WIN32)
#include <windows.h>
#define delay() Sleep(1);
#else
#include <unistd.h>
#define delay() usleep(10);
#endif

// uncomment for debug diagnostics:
//#define DEBUG

#define SIMD_LEN 4

// ---------------------------------------------------------------------------
// Various definitions copied from OpenMP RTL
enum sched {
  kmp_sch_static_balanced_chunked = 45,
  kmp_sch_guided_simd = 46,
  kmp_sch_runtime_simd = 47,
};
typedef unsigned u32;
typedef long long i64;
typedef unsigned long long u64;
typedef struct {
  int reserved_1;
  int flags;
  int reserved_2;
  int reserved_3;
  char *psource;
} id;

extern int __kmpc_global_thread_num(id*);
extern void __kmpc_barrier(id*, int gtid);
extern void __kmpc_dispatch_init_4(id*, int, enum sched, int, int, int, int);
extern void __kmpc_dispatch_init_8(id*, int, enum sched, i64, i64, i64, i64);
extern int __kmpc_dispatch_next_4(id*, int, void*, void*, void*, void*);
extern int __kmpc_dispatch_next_8(id*, int, void*, void*, void*, void*);
// End of definitions copied from OpenMP RTL.
// ---------------------------------------------------------------------------
static id loc = {0, 2, 0, 0, ";file;func;0;0;;"};

// ---------------------------------------------------------------------------
int run_loop_64(i64 loop_lb, i64 loop_ub, i64 loop_st, int loop_chunk) {
  int err = 0;
  static int volatile loop_sync = 0;
  i64 lb;   // Chunk lower bound
  i64 ub;   // Chunk upper bound
  i64 st;   // Chunk stride
  int rc;
  int tid = omp_get_thread_num();
  int gtid = tid;
  int last;
#if DEBUG
  printf("run_loop_<%d>(lb=%d, ub=%d, st=%d, ch=%d)\n",
    (int)sizeof(i64), gtid, tid,
    (int)loop_lb, (int)loop_ub, (int)loop_st, loop_chunk);
#endif
  // Don't test degenerate cases that should have been discovered by codegen
  if (loop_st == 0)
    return 0;
  if (loop_st > 0 ? loop_lb > loop_ub : loop_lb < loop_ub)
    return 0;

  __kmpc_dispatch_init_8(&loc, gtid, kmp_sch_guided_simd,
                         loop_lb, loop_ub, loop_st, loop_chunk);
  if (tid == 0) {
    // Let the master thread handle the chunks alone
    int chunk;      // No of current chunk
    i64 next_lb;    // Lower bound of the next chunk
    i64 last_ub;    // Upper bound of the last processed chunk
    u64 cur;        // Number of interations in  current chunk
    u64 max;        // Max allowed iterations for current chunk
    int undersized = 0;

    chunk = 0;
    next_lb = loop_lb;
    max = (loop_ub - loop_lb) / loop_st + 1;
    // The first chunk can consume all iterations
    while (__kmpc_dispatch_next_8(&loc, gtid, &last, &lb, &ub, &st)) {
      ++ chunk;
#if DEBUG
      printf("chunk=%d, lb=%d, ub=%d\n", chunk, (int)lb, (int)ub);
#endif
      // Check if previous chunk (it is not the final chunk) is undersized
      if (undersized) {
        printf("Error with chunk %d\n", chunk);
        err++;
      }
      // Check lower and upper bounds
      if (lb != next_lb) {
        printf("Error with lb %d, %d, ch %d\n", (int)lb, (int)next_lb, chunk);
        err++;
      }
      if (loop_st > 0) {
        if (!(ub <= loop_ub)) {
          printf("Error with ub %d, %d, ch %d\n", (int)ub, (int)loop_ub, chunk);
          err++;
        }
        if (!(lb <= ub)) {
          printf("Error with bounds %d, %d, %d\n", (int)lb, (int)ub, chunk);
          err++;
        }
      } else {
        if (!(ub >= loop_ub)) {
          printf("Error with ub %d, %d, %d\n", (int)ub, (int)loop_ub, chunk);
          err++;
        }
        if (!(lb >= ub)) {
          printf("Error with bounds %d, %d, %d\n", (int)lb, (int)ub, chunk);
          err++;
        }
      }; // if
      // Stride should not change
      if (!(st == loop_st)) {
        printf("Error with st %d, %d, ch %d\n", (int)st, (int)loop_st, chunk);
        err++;
      }
      cur = (ub - lb) / loop_st + 1;
      // Guided scheduling uses FP computations, so current chunk may
      // be a bit bigger (+1) than allowed maximum
      if (!(cur <= max + 1)) {
        printf("Error with iter %d, %d\n", cur, max);
        err++;
      }
      // Update maximum for the next chunk
      if (cur < max)
        max = cur;
      next_lb = ub + loop_st;
      last_ub = ub;
      undersized = (cur < loop_chunk);
    }; // while
    // Must have at least one chunk
    if (!(chunk > 0)) {
      printf("Error with chunk %d\n", chunk);
      err++;
    }
    // Must have the right last iteration index
    if (loop_st > 0) {
      if (!(last_ub <= loop_ub)) {
        printf("Error with last1 %d, %d, ch %d\n",
               (int)last_ub, (int)loop_ub, chunk);
        err++;
      }
      if (!(last_ub + loop_st > loop_ub)) {
        printf("Error with last2 %d, %d, %d, ch %d\n",
               (int)last_ub, (int)loop_st, (int)loop_ub, chunk);
        err++;
      }
    } else {
      if (!(last_ub >= loop_ub)) {
        printf("Error with last1 %d, %d, ch %d\n",
               (int)last_ub, (int)loop_ub, chunk);
        err++;
      }
      if (!(last_ub + loop_st < loop_ub)) {
        printf("Error with last2 %d, %d, %d, ch %d\n",
               (int)last_ub, (int)loop_st, (int)loop_ub, chunk);
        err++;
      }
    }; // if
    // Let non-master threads go
    loop_sync = 1;
  } else {
    int i;
    // Workers wait for master thread to finish, then call __kmpc_dispatch_next
    for (i = 0; i < 1000000; ++ i) {
      if (loop_sync != 0) {
        break;
      }; // if
    }; // for i
    while (loop_sync == 0) {
      delay();
    }; // while
    // At this moment we do not have any more chunks -- all the chunks already
    // processed by master thread
    rc = __kmpc_dispatch_next_8(&loc, gtid, &last, &lb, &ub, &st);
    if (rc) {
      printf("Error return value\n");
      err++;
    }
  }; // if

  __kmpc_barrier(&loc, gtid);
  if (tid == 0) {
      loop_sync = 0;    // Restore original state
#if DEBUG
      printf("run_loop_64(): at the end\n");
#endif
  }; // if
  __kmpc_barrier(&loc, gtid);
  return err;
} // run_loop

// ---------------------------------------------------------------------------
int run_loop_32(int loop_lb, int loop_ub, int loop_st, int loop_chunk) {
  int err = 0;
  static int volatile loop_sync = 0;
  int lb;   // Chunk lower bound
  int ub;   // Chunk upper bound
  int st;   // Chunk stride
  int rc;
  int tid = omp_get_thread_num();
  int gtid = tid;
  int last;
#if DEBUG
  printf("run_loop_<%d>(lb=%d, ub=%d, st=%d, ch=%d)\n",
    (int)sizeof(int), gtid, tid,
    (int)loop_lb, (int)loop_ub, (int)loop_st, loop_chunk);
#endif
  // Don't test degenerate cases that should have been discovered by codegen
  if (loop_st == 0)
    return 0;
  if (loop_st > 0 ? loop_lb > loop_ub : loop_lb < loop_ub)
    return 0;

  __kmpc_dispatch_init_4(&loc, gtid, kmp_sch_guided_simd,
                         loop_lb, loop_ub, loop_st, loop_chunk);
  if (tid == 0) {
    // Let the master thread handle the chunks alone
    int chunk;      // No of current chunk
    int next_lb;    // Lower bound of the next chunk
    int last_ub;    // Upper bound of the last processed chunk
    u64 cur;        // Number of interations in  current chunk
    u64 max;        // Max allowed iterations for current chunk
    int undersized = 0;

    chunk = 0;
    next_lb = loop_lb;
    max = (loop_ub - loop_lb) / loop_st + 1;
    // The first chunk can consume all iterations
    while (__kmpc_dispatch_next_4(&loc, gtid, &last, &lb, &ub, &st)) {
      ++ chunk;
#if DEBUG
      printf("chunk=%d, lb=%d, ub=%d\n", chunk, (int)lb, (int)ub);
#endif
      // Check if previous chunk (it is not the final chunk) is undersized
      if (undersized) {
        printf("Error with chunk %d\n", chunk);
        err++;
      }
      // Check lower and upper bounds
      if (lb != next_lb) {
        printf("Error with lb %d, %d, ch %d\n", (int)lb, (int)next_lb, chunk);
        err++;
      }
      if (loop_st > 0) {
        if (!(ub <= loop_ub)) {
          printf("Error with ub %d, %d, ch %d\n", (int)ub, (int)loop_ub, chunk);
          err++;
        }
        if (!(lb <= ub)) {
          printf("Error with bounds %d, %d, %d\n", (int)lb, (int)ub, chunk);
          err++;
        }
      } else {
        if (!(ub >= loop_ub)) {
          printf("Error with ub %d, %d, %d\n", (int)ub, (int)loop_ub, chunk);
          err++;
        }
        if (!(lb >= ub)) {
          printf("Error with bounds %d, %d, %d\n", (int)lb, (int)ub, chunk);
          err++;
        }
      }; // if
      // Stride should not change
      if (!(st == loop_st)) {
        printf("Error with st %d, %d, ch %d\n", (int)st, (int)loop_st, chunk);
        err++;
      }
      cur = (ub - lb) / loop_st + 1;
      // Guided scheduling uses FP computations, so current chunk may
      // be a bit bigger (+1) than allowed maximum
      if (!(cur <= max + 1)) {
        printf("Error with iter %d, %d\n", cur, max);
        err++;
      }
      // Update maximum for the next chunk
      if (cur < max)
        max = cur;
      next_lb = ub + loop_st;
      last_ub = ub;
      undersized = (cur < loop_chunk);
    }; // while
    // Must have at least one chunk
    if (!(chunk > 0)) {
      printf("Error with chunk %d\n", chunk);
      err++;
    }
    // Must have the right last iteration index
    if (loop_st > 0) {
      if (!(last_ub <= loop_ub)) {
        printf("Error with last1 %d, %d, ch %d\n",
               (int)last_ub, (int)loop_ub, chunk);
        err++;
      }
      if (!(last_ub + loop_st > loop_ub)) {
        printf("Error with last2 %d, %d, %d, ch %d\n",
               (int)last_ub, (int)loop_st, (int)loop_ub, chunk);
        err++;
      }
    } else {
      if (!(last_ub >= loop_ub)) {
        printf("Error with last1 %d, %d, ch %d\n",
               (int)last_ub, (int)loop_ub, chunk);
        err++;
      }
      if (!(last_ub + loop_st < loop_ub)) {
        printf("Error with last2 %d, %d, %d, ch %d\n",
               (int)last_ub, (int)loop_st, (int)loop_ub, chunk);
        err++;
      }
    }; // if
    // Let non-master threads go
    loop_sync = 1;
  } else {
    int i;
    // Workers wait for master thread to finish, then call __kmpc_dispatch_next
    for (i = 0; i < 1000000; ++ i) {
      if (loop_sync != 0) {
        break;
      }; // if
    }; // for i
    while (loop_sync == 0) {
      delay();
    }; // while
    // At this moment we do not have any more chunks -- all the chunks already
    // processed by the master thread
    rc = __kmpc_dispatch_next_4(&loc, gtid, &last, &lb, &ub, &st);
    if (rc) {
      printf("Error return value\n");
      err++;
    }
  }; // if

  __kmpc_barrier(&loc, gtid);
  if (tid == 0) {
      loop_sync = 0;    // Restore original state
#if DEBUG
      printf("run_loop<>(): at the end\n");
#endif
  }; // if
  __kmpc_barrier(&loc, gtid);
  return err;
} // run_loop

// ---------------------------------------------------------------------------
int run_64(int num_th)
{
 int err = 0;
#pragma omp parallel num_threads(num_th)
 {
  int chunk;
  i64 st, lb, ub;
  for (chunk = SIMD_LEN; chunk <= 3*SIMD_LEN; chunk += SIMD_LEN) {
    for (st = 1; st <= 3; ++ st) {
      for (lb = -3 * num_th * st; lb <= 3 * num_th * st; ++ lb) {
        for (ub = lb; ub < lb + num_th * (chunk+1) * st; ++ ub) {
          err += run_loop_64(lb, ub,  st, chunk);
          err += run_loop_64(ub, lb, -st, chunk);
        }; // for ub
      }; // for lb
    }; // for st
  }; // for chunk
 }
 return err;
} // run_all

int run_32(int num_th)
{
 int err = 0;
#pragma omp parallel num_threads(num_th)
 {
  int chunk, st, lb, ub;
  for (chunk = SIMD_LEN; chunk <= 3*SIMD_LEN; chunk += SIMD_LEN) {
    for (st = 1; st <= 3; ++ st) {
      for (lb = -3 * num_th * st; lb <= 3 * num_th * st; ++ lb) {
        for (ub = lb; ub < lb + num_th * (chunk+1) * st; ++ ub) {
          err += run_loop_32(lb, ub,  st, chunk);
          err += run_loop_32(ub, lb, -st, chunk);
        }; // for ub
      }; // for lb
    }; // for st
  }; // for chunk
 }
 return err;
} // run_all

// ---------------------------------------------------------------------------
int main()
{
  int n, err = 0;
  for (n = 1; n <= 4; ++ n) {
    err += run_32(n);
    err += run_64(n);
  }; // for n
  if (err)
    printf("failed with %d errors\n", err);
  else
    printf("passed\n");
  return err;
}