<commit_before>/*
 * M*LIB - common functions for bench module
 *
 * Copyright (c) 2017-2019, <NAME>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * + Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string.h>
#include "common.h"

// The global result
unsigned long g_result;

// The rand value
#if defined(MULTI_THREAD_MEASURE)
M_THREAD_ATTR
#endif
unsigned int randValue = 0;

void compiler_barrier(void *p)
{
  // Do nothing, but let the compiler thinks that we did.
  (void) p;
  // Can be used for FLTO building
  //  asm volatile("": "+r" (p) : :"memory");
}

struct parse_opt_s {
  int test_function;
  double from, to, step, grow;
  unsigned repeat;
  bool graph;
};

static void
parse_config(struct parse_opt_s *opt, int argc, const char *argv[])
{
  char *end;
  
  opt->test_function = 0;
  opt->from = 0.0;
  opt->to   = 0.0;
  opt->step = 0.0;
  opt->grow = 1.1;
  opt->graph = false;
  opt->repeat = 1;
  
  for(int i = 1; i < argc ; i++)
    {
      if (argv[i][0] == '-') {
	if (strcmp(argv[i], "--from") == 0){
	  i++;
	  opt->from = strtod(argv[i], &end);
	  opt->to   = opt->from;
	} else if (strcmp(argv[i], "--to") == 0) {
	  i++;
	  opt->to = strtod(argv[i], &end);
	  opt->grow = 1.1;
	} else if (strcmp(argv[i], "--step") == 0) {
	  i++;
	  opt->step = strtod(argv[i], &end);
	  opt->grow = 0;
	} else if (strcmp(argv[i], "--grow") == 0) {
	  i++;
	  opt->grow = strtod(argv[i], &end);
	  opt->step = 0;
	} else if (strcmp(argv[i], "--repeat") == 0) {
	  i++;
	  opt->repeat = strtol(argv[i], &end, 10);
	} else if (strcmp(argv[i], "--graph") == 0) {
	  opt->graph = true;
	} else {
	  fprintf(stderr, "ERROR: Option unkown: %s.\n",
		  argv[i]);
	  exit(-1);
	}
	if (*end != 0) {
	  fprintf(stderr, "ERROR: Cannot parse %s. Expected float\n",
		  argv[i]);
	  exit(-1);
	}

      } else {
	opt->test_function = strtol (argv[i], &end, 10);
	if (*end != 0) {
	  fprintf(stderr, "ERROR: Cannot parse %s. Expected function number\n",
		  argv[i]);
	  exit(-1);
	}
      }
    }
}

static int
select_config(int func, size_t n, const config_func_t functions[])
{
  for(size_t i = 0; i < n;i++) {
    if (functions[i].num == func)
      return i;
  }
  fprintf(stderr, "ERROR: Function number %d not found\n", func);
  exit(-1);
}

void
test(const char library[], size_t n, const config_func_t functions[], int argc, const char *argv[])
{
  // options
  // NUMBER
  // --from NUMBER --to NUMBER --step NUMBER --grow NUMBER
  // --graph
  // --repeat N
  struct parse_opt_s arg;
  parse_config(&arg, argc, argv);
  int i = select_config(arg.test_function, n, functions);
  double from = arg.from == 0 ? functions[i].default_n : arg.from;
  double to   = arg.from == 0 ? functions[i].default_n : arg.to;
  FILE *graph_file = NULL;
  if (arg.graph) {
    char filename[100];
    sprintf(filename, "plot-%s-%d.dat", library, arg.test_function);
    graph_file = fopen(filename, "wt");
    if (!graph_file) {
      fprintf(stderr, "ERROR: Cannot create plot.dat\n");
      exit(-2);
    }
    fprintf(graph_file, "# plotting %s-%d : %s\n# N T", library, arg.test_function, functions[i].funcname);
  }
  // Do the bench
  for(double n = from; n <= to ; n = (arg.grow == 0 ? n + arg.step : n * arg.grow))
    {
      double t = (1.0/0.0);
      double avg = 0.0;
      if (functions[i].init != NULL)
	functions[i].init(n);
      for(unsigned r = 0; r < arg.repeat; r++) {
	double t0 = test_function(arg.graph ? NULL : functions[i].funcname, (size_t) n, functions[i].func);
	t = t0 < t ? t0 : t;
	avg += t0;
      }
      if (functions[i].clear != NULL)
	functions[i].clear();
      if (arg.graph)
	fprintf(graph_file, "%f %f\n", n, t);
      else if (arg.repeat > 1) {
	printf ("%20.20s time %lu ms for n = %lu ***   BEST  ***\n", functions[i].funcname, (unsigned long) t, (unsigned long) n);
	printf ("%20.20s time %lu ms for n = %lu *** AVERAGE ***\n", functions[i].funcname, (unsigned long) (avg/arg.repeat), (unsigned long) n);
      }
    }
  if (arg.graph) {
    fclose(graph_file);<commit_msg>Add option to print only the best or only the average<commit_after>/*
 * M*LIB - common functions for bench module
 *
 * Copyright (c) 2017-2019, <NAME>
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * + Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * + Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string.h>
#include "common.h"

// The global result
unsigned long g_result;

// The rand value
#if defined(MULTI_THREAD_MEASURE)
M_THREAD_ATTR
#endif
unsigned int randValue = 0;

void compiler_barrier(void *p)
{
  // Do nothing, but let the compiler thinks that we did.
  (void) p;
  // Can be used for FLTO building
  //  asm volatile("": "+r" (p) : :"memory");
}

struct parse_opt_s {
  int test_function;
  double from, to, step, grow;
  unsigned repeat;
  bool graph, best, average;
};

static void
parse_config(struct parse_opt_s *opt, int argc, const char *argv[])
{
  char *end;
  
  opt->test_function = 0;
  opt->from = 0.0;
  opt->to   = 0.0;
  opt->step = 0.0;
  opt->grow = 1.1;
  opt->graph = false;
  opt->best = false;
  opt->average = false;
  opt->repeat = 1;
  
  for(int i = 1; i < argc ; i++)
    {
      if (argv[i][0] == '-') {
	if (strcmp(argv[i], "--from") == 0){
	  i++;
	  opt->from = strtod(argv[i], &end);
	  opt->to   = opt->from;
	} else if (strcmp(argv[i], "--to") == 0) {
	  i++;
	  opt->to = strtod(argv[i], &end);
	  opt->grow = 1.1;
	} else if (strcmp(argv[i], "--step") == 0) {
	  i++;
	  opt->step = strtod(argv[i], &end);
	  opt->grow = 0;
	} else if (strcmp(argv[i], "--grow") == 0) {
	  i++;
	  opt->grow = strtod(argv[i], &end);
	  opt->step = 0;
	} else if (strcmp(argv[i], "--repeat") == 0) {
	  i++;
	  opt->repeat = strtol(argv[i], &end, 10);
	} else if (strcmp(argv[i], "--graph") == 0) {
	  opt->graph = true;
	} else if (strcmp(argv[i], "--best") == 0) {
	  opt->best = true;
	  opt->average = false;
	} else if (strcmp(argv[i], "--average") == 0) {
	  opt->average = true;
	  opt->best = false;
	} else {
	  fprintf(stderr, "ERROR: Option unkown: %s.\n",
		  argv[i]);
	  exit(-1);
	}
	if (*end != 0) {
	  fprintf(stderr, "ERROR: Cannot parse %s. Expected float\n",
		  argv[i]);
	  exit(-1);
	}

      } else {
	opt->test_function = strtol (argv[i], &end, 10);
	if (*end != 0) {
	  fprintf(stderr, "ERROR: Cannot parse %s. Expected function number\n",
		  argv[i]);
	  exit(-1);
	}
      }
    }
}

static int
select_config(int func, size_t n, const config_func_t functions[])
{
  for(size_t i = 0; i < n;i++) {
    if (functions[i].num == func)
      return i;
  }
  fprintf(stderr, "ERROR: Function number %d not found\n", func);
  exit(-1);
}

void
test(const char library[], size_t n, const config_func_t functions[], int argc, const char *argv[])
{
  // options
  // NUMBER
  // --from NUMBER --to NUMBER --step NUMBER --grow NUMBER
  // --graph
  // --repeat N
  struct parse_opt_s arg;
  parse_config(&arg, argc, argv);
  int i = select_config(arg.test_function, n, functions);
  double from = arg.from == 0 ? functions[i].default_n : arg.from;
  double to   = arg.from == 0 ? functions[i].default_n : arg.to;
  FILE *graph_file = NULL;
  if (arg.graph) {
    char filename[100];
    sprintf(filename, "plot-%s-%d.dat", library, arg.test_function);
    graph_file = fopen(filename, "wt");
    if (!graph_file) {
      fprintf(stderr, "ERROR: Cannot create plot.dat\n");
      exit(-2);
    }
    fprintf(graph_file, "# plotting %s-%d : %s\n# N T", library, arg.test_function, functions[i].funcname);
  }
  // Do the bench
  for(double n = from; n <= to ; n = (arg.grow == 0 ? n + arg.step : n * arg.grow))
    {
      double best = (1.0/0.0);
      double avg  = 0.0;
      if (functions[i].init != NULL)
	functions[i].init(n);
      for(unsigned r = 0; r < arg.repeat; r++) {
	double t0 = test_function(arg.graph|arg.best|arg.average ? NULL : functions[i].funcname, (size_t) n, functions[i].func);
	best = t0 < best ? t0 : best;
	avg += t0;
      }
      if (functions[i].clear != NULL)
	functions[i].clear();
      avg /= arg.repeat;
      if (arg.graph) {
	fprintf(graph_file, "%f %f\n", n, arg.average ? avg : best);
      } else if (arg.repeat > 1) {
	if (arg.average == false)
	  printf ("%20.20s time %lu ms for n = %lu ***   BEST  ***\n", functions[i].funcname, (unsigned long) best, (unsigned long) n);
	if (arg.best == false)
	  printf ("%20.20s time %lu ms for n = %lu *** AVERAGE ***\n", functions[i].funcname, (unsigned long) avg, (unsigned long) n);
      }
    }
  if (arg.graph) {
    fclose(graph_file);
{'fraction_non_alphanumeric': 0.15890688259109312, 'fraction_numerical': 0.012881854987118146, 'mean_word_length': 3.145308924485126, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3164727', 'n_tokens_mistral': 3990, 'n_tokens_neox': 3749, 'n_words': 1239}