Beispiel #1
0
void sfmt_init_stable_r(sfmt_t *sfmt){
  uint32_t init_array[SFMT_N32];
  struct timeval tp;
  union lcg_state lcg_init;
  lcg_init.X_i=time_seed();
  int i;
  for(i=0;i<SFMT_N32;i++){
    init_array[i]=nrand48(lcg_init.state);
  }
  sfmt_init_by_array(sfmt,init_array,SFMT_N32);
}
inline void init_mt(char *seed) {
	char *pEnd; //!< Used to check if user seed is parsable
	/* Seed from 
	 * http://www.random.org/integers/?num=9&min=1&max=1000000000&col=9&base=16&format=plain&rnd=date.2014-05-01
	 */
	// Seed the MT using 9 random integers and 1 from user input
	uint32_t init[10] =	{	0x012d661f, 0x399ba1e1, 0x01d033e9,
							0x24ab21da, 0x3a1b142b, 0x21109a50,
							0x20d005a4, 0x35215de9, 0x091c98f1};
	// Parse user input
	init[9] = strtol(seed, &pEnd, 10);
	// Check if all was parsed
	if(pEnd[0] != '\0') {
		fprintf(stderr, "Could not convert user seed to an integer ('%c' is not in base 10)\n", pEnd[0]);
		exit(1);
	}
	// Init SFMT
	sfmt_init_by_array(&sfmt, init, 10);
}
Beispiel #3
0
int main(int argc, char * const argv[])
{
  char *output = NULL;
  char buffer[1024];
  unsigned long gen = 100;
  long seed[33];
  char c;
  int mul = 0, seedpos = 0;
  FILE *out;
  int verbosity = 1, running = 0;
  struct timespec tp;
  char mode[] = "w";
  sfmt_t sfmt;
  int cores =
#if defined(WIN32) || defined(__WIN32)
    1;
#else
    sysconf (_SC_NPROCESSORS_CONF);
#endif
  unsigned long blocksize;
  struct process *process;

#ifdef _WIN32
  seed[0] = time(NULL);
#else
  clock_gettime(CLOCK_REALTIME, &tp);

  seed[0] = tp.tv_nsec + tp.tv_sec * 1000 * 1000 * 1000;
#endif
  while ((c = getopt(argc, argv, "hc:a:o:s:g:qv")) != -1) {
    switch (c) {
    default:
      usage(argv[0], 1, "ERROR: Unknown parameter %c\n", c);
      break;
    case 'h':
    case '?':
      usage(argv[0], 0, NULL);
      break;
    case 'g':
      gen = atoi(optarg);
      if (gen > INT_MAX)
        usage(argv[0], 2, "ERROR: Generate %lu is over max %d\n", gen, INT_MAX);
      break;
    case 'q':
      verbosity = 0;
      break;
    case 'c':
      cores = atoi(optarg);
      if (cores <= 0)
        usage(argv[0], 2, "ERROR: %d cores must be over zero", cores);
      break;
    case 'v':
      verbosity++;
      break;
    case 's':
      {
        char *startptr = optarg;
        /* Read seed from /dev/urandom */
        if (strcmp("dev", optarg) == 0) {
          FILE *dev = fopen("/dev/urandom", "r");
          int r;
          if (!dev) {
            perror("opening /dev/urandom");
            return 12;
          }
          do {
            r = fread(seed, sizeof seed[0], sizeof seed/sizeof seed[0], dev);
          } while (r != sizeof seed/sizeof seed[0] && errno == EINTR);

          if (r != sizeof seed/sizeof seed[0]) {
            perror("fread /dev/random");
            return 13;
          }
          seedpos = sizeof seed/sizeof seed[0];
          fclose(dev);
          break;
        }
        for (; seedpos < 33; seedpos++) {
          char *endptr;
          unsigned long s;
          errno = 0;
          s = strtol(startptr, &endptr, 10);
          if (endptr == startptr) {
            break;
          }
          if (errno != 0) {
            perror("sttrol");
            return errno;
          }
          startptr = endptr + 1;
          seed[seedpos] = s;
          if (*endptr == '\0') {
            seedpos++;
            break;
          }
        }
      }
      break;
    case 'a':
      mode[0] = 'a';
    case 'o':
      output = strdup(optarg);
      break;
    }
  }

  freopen("/dev/null", "r", stdin);

  process = alloca(sizeof(process[0])*cores);
  memset(process, 0, sizeof(process[0])*cores);

  /* Calculate reasonable block sizes for the cpu configuration */
  blocksize = gen;
  for (mul = 20; mul > 0; mul--) {
    long high = 1 << mul;

    if (gen >= (unsigned)(cores*high)) {
      blocksize = gen / (cores * mul);
      break;
    }
  }

  if (blocksize >= (1 << 20)/20)
    blocksize = (1 << 20)/20;

  int i, pos = 0;
  sfmt_init_by_array(&sfmt, (uint32_t*)&seed[0], (seedpos - 1)*(sizeof(seed[0])/sizeof(uint32_t)));
  pos = sprintf(buffer, "%ld", seed[0]);
  for (i = 1; i < seedpos; i++)
    pos += sprintf(&buffer[pos], ",%ld", seed[i]);


  if (verbosity >= 1)
    fprintf(stderr, "Generating %ld deals with %s seed. Each subprocess does %ld deals.\n",
        gen, buffer, blocksize);

  out = output ? fopen(output, mode) : stdout;

  if (!out) {
    perror("Can't open output file");
    return 10;
  }

  {
    int fd, maxfd = -1;
    int c;
    fd_set fds;
    fd_set rfds;

    FD_ZERO(&fds);

    for (c = 0; c < cores; c++) {
      unsigned long b = gen - scheduled > blocksize ?
                blocksize : gen - scheduled;

      if (b == 0)
        break;

      if ((fd = makeprocess(&process[c], b, sfmt_genrand_uint64(&sfmt), verbosity)) < 0)
        return 20;
      running++;

      if (fd > maxfd)
        maxfd = fd;

      FD_SET(fd, &fds);
    }

    /* Loop as long as we have subprocess active */
    while (running > 0) {
      int active;
      int p = 0;
      rfds = fds;
      /* Wait for any input */
      do {
        active = select(maxfd + 1, &rfds, NULL, NULL, NULL);
      } while (active == -1 && errno == EINTR);

      if (active <= 0) {
        perror("select");
        return 60;
      }
      /* Check which subprocess provided the input */
      for (; p < cores; p++) {
        if (FD_ISSET(process[p].fd, &rfds)) {
          errno = 0;
          while (fgets(buffer, sizeof buffer, process[p].f))
            parseLine(buffer, out, &process[p], gen, verbosity);

          /* Has the process exited? */
          if (feof(process[p].f)) {
            FD_CLR(process[p].fd, &fds);
            closeprocess(&process[p]);
            running--;
            /* Is there more blocks to shedule? */
            if (scheduled < gen) {
              unsigned long b = gen - scheduled > blocksize ?
                blocksize : gen - scheduled;

              if ((fd = makeprocess(&process[p], b, sfmt_genrand_uint64(&sfmt), verbosity)) < 0)
                return 20;

              running++;

              if (fd > maxfd)
                maxfd = fd;

              FD_SET(fd, &fds);
            }
            continue;
          }
          /* EWOULDBLOCK and EINTR can happen during normal operation */
          if (errno != EWOULDBLOCK && errno != EINTR) {
            perror("fgets");
            return 50;
          }
        }
      }
    }
  }

  fclose(out);
  return 0;
}