hack2 / bench / numeric / arr.c

/* 3 args - array size, number of iterations, number of scenario
   Scenarios:
   0 - a + b
   1 - a + (b + c)
   2 - (a + b) + ((c + d) + e)
 */

#include <stdio.h>
#include <stdlib.h>

double sum(int size, double *res)
{
  int i;
  double s;
  for (i = 0; i < size; ++i)
    s += res[i];
  return s;
}

#define FILENAME "/tmp/a.data"

void init_file(int size, double *arr)
{
  int i;
  int count = 0;
  FILE* f = fopen(FILENAME, "r");
  while (count < size) {
    count += fread(arr + count, 1, size - count, f);
  }
  fclose(f);
}

void init(int size, double *arr, double what)
{
  int i;
  int count;
  for (i = 0; i < size; ++i)
    arr[i] = what;
}

void add(int size, double *a, double *b, double *res)
{
  int i;
  for (i = 0; i < size; i++)
    res[i] = res[i] + a[i] + b[i];
}

double* new_arr(int size, double what)
{
  double *a;
  a = (double*)malloc(sizeof(double) * size);
  if (what == -1) {
    init_file(size, a);
  } else {
    init(size, a, what);
  }
  return a;
}

int main(int argc, char **argv)
{
  double *a, *b, *res, *c, *d, *e, *tmp1, *tmp2, *tmp3;
  int size = atoi(argv[1]);
  int i;
  double what;
  double s;
  int n = atoi(argv[2]);
  int bench = atoi(argv[3]);
  a = new_arr(size, -1);
  b = new_arr(size, -1);
  c = new_arr(size, -1);
  d = new_arr(size, -1);
  e = new_arr(size, -1);
  tmp1 = new_arr(size, 0);
  tmp2 = new_arr(size, 0);
  tmp3 = new_arr(size, 0);
  res = new_arr(size, 0);
  for (i = 0; i < n; ++i) {
    if (bench == 0) {
      add(size, a, b, res);
    } else if (bench == 1) {
      init(size, tmp1, 0);
      add(size, b, c, tmp1);
      add(size, a, tmp1, res);
    } else if (bench == 2) {
      init(size, tmp1, 0);
      add(size, a, b, tmp1);
      init(size, tmp2, 0);
      init(size, tmp3, 0);
      add(size, c, d, tmp2);
      add(size, tmp1, e, tmp3);
      add(size, tmp3, tmp2, res);
    }
  }
  s = sum(size, res);
  printf("%f\n", s);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.