<commit_before>#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "hpcq.h"
#include "rand.h"
#include "time.h"

static size_t ntimes = 10000000;
static size_t niters = 10;
static size_t max_wait = 64;
static size_t max_nprocs;
static pthread_barrier_t barrier;
static hpcq_t hpcq;
static int nprocs;
static int res[1024];

static inline void delay(size_t cycles)
{
  int i;

  for (i = 0; i < cycles; ++i) {
    __asm__ ("pause");
  }
}

static int compare(const void * a, const void * b)
{
  return *(int *) a - *(int *) b;
}

static int verify()
{
  qsort(res, nprocs, sizeof(int), compare);

  int i;
  for (i = 0; i < nprocs; ++i) {
    if (res[i] != i + 1) {
      fprintf(stderr, "error: expected %d but get %d\n", i + 1, res[i]);
      return 1;
    }
  }

  return 0;
}

static void thread_pin(int id)
{
  pthread_setconcurrency(max_nprocs);

  cpu_set_t set;
  CPU_ZERO(&set);
  int core_id = (id * 2) % max_nprocs;
  int core_offset = (id * 2) / max_nprocs % 2;
  int thread_id = core_id + core_offset;

  printf("pin thread %d to cpu %d\n", id, thread_id);

  CPU_SET(thread_id, &set);
  sched_setaffinity(0, sizeof(set), &set);
}

static void * thread_main(void * val)
{
  int empty = 0;
  rand_state_t state = rand_seed((size_t) val);
  size_t elapsed, wait, average = 0;
  int id = (size_t) val - 1;
  int i, j;
  hpcq_handle_t * node = malloc(sizeof(hpcq_handle_t [ntimes]));

  thread_pin(id);

  for (i = 1; i <= niters; ++i) {
    pthread_barrier_wait(&barrier);

    /** Timed region start. */
    if (id == 0) {
      elapsed = time_elapsed(0);
    }

    for (j = 0; j < ntimes; ++j) {
      node[j].data = val;
      hpcq_put(&hpcq, &node[j]);
      delay(rand_next(state) % max_wait);

      val = hpcq_take(&hpcq);
      delay(rand_next(state) % max_wait);
    }

    pthread_barrier_wait(&barrier);

    /** Timed region end. */
    if (id == 0) {
      elapsed = time_elapsed(elapsed);
      printf("time elapsed #%d: %ld ms\n", i, elapsed / 1000000);
      average += elapsed;
    }
  }

  free(node);

  if (id == 0) {
    printf("time elapsed average: %ld ms\n", average / niters / 1000000);
  }
  return val;
}

int main(int argc, const char *argv[])
{
  nprocs = 0;
  max_nprocs = sysconf(_SC_NPROCESSORS_ONLN);

  if (argc > 1) {
    nprocs = atoi(argv[1]);
  }

  if (nprocs == 0) {
    nprocs = max_nprocs;
  }

  printf("number of procs: %d\n", nprocs);
  ntimes = ntimes / nprocs;

  pthread_barrier_init(&barrier, NULL, nprocs);
  pthread_t hds[nprocs];
  int i;

  for (i = 1; i < nprocs; ++i) {
    pthread_create(hds + i, NULL, thread_main, (void *) (long) (i + 1));
  }

  res[0] = (size_t) thread_main((void *) 1);

  for (i = 1; i < nprocs; ++i) {
    void * retval;
    pthread_join(hds[i], &retval);
    res[i] = (size_t) retval;
  }
  pthread_barrier_destroy(&barrier);

  return verify();
}

<commit_msg>[MS] Improve benchmar printing.<commit_after>#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "hpcq.h"
#include "rand.h"
#include "time.h"

static size_t ntimes = 10000000;
static size_t niters = 10;
static size_t max_wait = 64;
static size_t max_nprocs;
static pthread_barrier_t barrier;
static hpcq_t hpcq;
static int nprocs;
static int res[1024];

static inline void delay(size_t cycles)
{
  int i;

  for (i = 0; i < cycles; ++i) {
    __asm__ ("pause");
  }
}

static int compare(const void * a, const void * b)
{
  return *(int *) a - *(int *) b;
}

static int verify()
{
  qsort(res, nprocs, sizeof(int), compare);

  int i;
  for (i = 0; i < nprocs; ++i) {
    if (res[i] != i + 1) {
      fprintf(stderr, "error: expected %d but get %d\n", i + 1, res[i]);
      return 1;
    }
  }

  return 0;
}

static void thread_pin(int id)
{
  pthread_setconcurrency(max_nprocs);

  cpu_set_t set;
  CPU_ZERO(&set);

  CPU_SET(id % max_nprocs, &set);
  sched_setaffinity(0, sizeof(set), &set);
}

static void * thread_main(void * val)
{
  int empty = 0;
  rand_state_t state = rand_seed((size_t) val);
  size_t elapsed, wait;
  double average = 0.0;
  int id = (size_t) val - 1;
  int i, j;
  hpcq_handle_t * node = malloc(sizeof(hpcq_handle_t [ntimes]));

  thread_pin(id);

  for (i = 1; i <= niters; ++i) {
    pthread_barrier_wait(&barrier);

    /** Timed region start. */
    if (id == 0) {
      elapsed = time_elapsed(0);
    }

    for (j = 0; j < ntimes; ++j) {
      node[j].data = val;
      hpcq_put(&hpcq, &node[j]);
      delay(rand_next(state) % max_wait);

      val = hpcq_take(&hpcq);
      delay(rand_next(state) % max_wait);
    }

    pthread_barrier_wait(&barrier);

    /** Timed region end. */
    if (id == 0) {
      elapsed = time_elapsed(elapsed);
      printf("time elapsed #%d: %ld ms\n", i, elapsed / 1000000);
      average += elapsed;
    }
  }

  free(node);
  average /= niters;

  if (id == 0) {
    printf("time elapsed average: %.3f ms\n", average / 1e6);
    printf("Mops / second: %.3lf\n", ntimes * nprocs * 2 / (average / 1e3));
    printf("latency: %.3lf us\n", (average / 1e3) / (ntimes * 2));
  }
  return val;
}

int main(int argc, const char *argv[])
{
  nprocs = 0;
  max_nprocs = sysconf(_SC_NPROCESSORS_ONLN);

  if (argc > 1) {
    nprocs = atoi(argv[1]);
  }

  if (nprocs == 0) {
    nprocs = max_nprocs;
  }

  printf("number of procs: %d\n", nprocs);
  ntimes = ntimes / nprocs;

  pthread_barrier_init(&barrier, NULL, nprocs);
  pthread_t hds[nprocs];
  int i;

  for (i = 1; i < nprocs; ++i) {
    pthread_create(hds + i, NULL, thread_main, (void *) (long) (i + 1));
  }

  res[0] = (size_t) thread_main((void *) 1);

  for (i = 1; i < nprocs; ++i) {
    void * retval;
    pthread_join(hds[i], &retval);
    res[i] = (size_t) retval;
  }
  pthread_barrier_destroy(&barrier);

  return verify();
}


{'fraction_non_alphanumeric': 0.1644489935175708, 'fraction_numerical': 0.02012964858410099, 'mean_word_length': 3.0658807212205272, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2022971', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2416, 'n_words': 606}