summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/tracking/src/c/getANMS.c
Commit message (Expand)AuthorAge
* Squashed commit of the sb-vbs branch.Leo Chan2020-10-22
29'>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
/*
 *  Sample code for the DIS Pointer Stressmark
 *
 * This source code is the completely correct source code based on
 * the example codes provided by Atlantic Aerospace Division, Titan
 * Systems Corporation, 2000.
 *
 * If you just compile and generate the executables from this source
 * code, this code would be enough. However, if you wish to get a complete
 * understanding of this stressmark, it is strongly suggested that you
 * read the Benchmark Analysis and Specifications Document Version 1.0
 * before going on since the detailed comments are given in this documents.
 * the comments are not repeated here.
 */
#include "DISstressmarkRNG.h"
#include "extra.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MIN_FIELD_SIZE 16
#define MAX_FIELD_SIZE (16777216 * 4) // Modifed from original
#define MIN_WINDOW_SIZE 1
#define MAX_WINDOW_SIZE 15
#define MIN_HOP_LIMIT 1

#define MAX_HOP_LIMIT 4294967295U

#define MIN_SEED -2147483647
#define MAX_SEED -1
#define MIN_THREADS 1
#define MAX_THREADS 256

/*
 * main()
 */
int main(int argc, char **argv) {

  unsigned int *field;
  unsigned long f;
  unsigned short int w;
  unsigned long maxhops;
  long seed;
  unsigned int n;

  clock_t startTime, endTime;

  struct threadS {
    unsigned long initial;
    unsigned long minStop;
    unsigned long maxStop;
    unsigned long hops;
  } * thread;

  unsigned int l;
  SET_UP

  assert(fscanf(stdin, "%lu %u %lu %ld %u", &f, &l, &maxhops, &seed, &n) == 5);

  assert((f >= MIN_FIELD_SIZE) && (f <= MAX_FIELD_SIZE));
  w = (unsigned int)l;
  assert((w >= MIN_WINDOW_SIZE) && (w <= MAX_WINDOW_SIZE));
  assert(w % 2 == 1);
  assert(f > w);
  assert((maxhops >= MIN_HOP_LIMIT) && (maxhops <= MAX_HOP_LIMIT));
  assert((seed >= MIN_SEED) && (seed <= MAX_SEED));

  assert((n >= MIN_THREADS) && (n <= MAX_THREADS));
  if ((thread = (struct threadS *)malloc(n * sizeof(struct threadS))) == NULL)
    return (-1);

  for (l = 0; l < n; l++) {
    assert(fscanf(stdin, "%lu %lu %lu", &(thread[l].initial),
                  &(thread[l].minStop), &(thread[l].maxStop)) == 3);
    assert((thread[l].initial >= 0) && (thread[l].initial < f));
    assert((thread[l].minStop >= 0) && (thread[l].minStop < f));
    assert((thread[l].maxStop >= 0) && (thread[l].maxStop < f));
  }

  if ((field = (unsigned int *)malloc(f * sizeof(int))) == NULL)
    return (-1);

  randInit(seed);
  for_each_job {
    for (l = 0; l < f; l++) {
      field[l] = randInt(0, f - w);
    }
    startTime = time(NULL);

    for (l = 0; l < n; l++) {
      unsigned int index;
      unsigned int minStop, maxStop;
      unsigned int hops;

      hops = 0;
      minStop = thread[l].minStop;
      maxStop = thread[l].maxStop;
      index = thread[l].initial;
      while ((hops < maxhops) && (!((index >= minStop) && (index < maxStop)))) {

        unsigned int ll, lll;
        unsigned int max, min;
        unsigned int partition;
        unsigned int high;

        partition = field[index];
        max = MAX_FIELD_SIZE;
        min = 0;
        high = 0;

        for (ll = 0; ll < w; ll++) {
          unsigned int balance;
          unsigned int x;
          x = field[index + ll];

          if (x > max)
            high++;
          else if (x > min) { /* start else* */
            partition = x;
            balance = 0;
            for (lll = ll + 1; lll < w; lll++) {
              if (field[index + lll] > partition)
                balance++;
            } /* end for loop */

            if (balance + high == w / 2)
              break;
            else if (balance + high > w / 2) {
              min = partition;
            } /* end if */
            else {
              max = partition;
              high++;
            } /* end else */
          }
          if (min == max)
            break;
        } /* end else* */
        index = (partition + hops) % (f - w);
        hops++;
      } /* end loop ll */
      thread[l].hops = hops;
    } /* end while */
  }

  endTime = time(NULL);

  volatile int _stop_optimizer = 0;
  for (l=0; l<n; l++){
      _stop_optimizer += thread[l].hops + l;
  //  fprintf(stdout, "%lu hops on thread %d\n", thread[l].hops, l);
  }

  fprintf(stderr, "time for pointer stressmark = %f seconds.\n",
          difftime(endTime, startTime));
  free(field);
  free(thread);
  WRITE_TO_FILE
}