Exemple #1
0
/**
 * Program entry point
 */
int main(int argc, char * * argv)
{
    if(argc != 3) {
    	printUsage();
    	return 0;
    }

    // What was the input number?
    uint128 n = alphaTou128(argv[1]);
    char * n_str = u128ToString(n); 

    // How many concurrent threads?
    errno = 0; // so we know if strtol fails
    int n_threads = strtol(argv[2], NULL, 10);

    // Was there an error in the input arguments?
        int error = FALSE;
    if(errno != 0 || n_threads <= 0) {
	fprintf(stderr, "2nd argument must be a valid integer >= 1, aborting.\n");
	error = TRUE;
    }
    if(n_str && strcmp(n_str, argv[1]) != 0) {
	fprintf(stderr, "1st argument must be a valid 128-bit integer: '%s' != '%s', aborting.\n", n_str, argv[1]);
	error = TRUE;
    }

    if(error) {
	free(n_str);
	exit(1);
    }
    free(n_str);
   
  
    struct timeval time1;
    struct timeval time2;
    gettimeofday(&time1, NULL);
    printf("Testing if '%s' is prime with %d threads: ", argv[1], n_threads);
    fflush(stdout);
    int is_prime = primalityTestParallel(n, n_threads);
    gettimeofday(&time2, NULL);
    if(is_prime) {
	printf("TRUE");
    } else {
	printf("FALSE");
    }
    printf(", %8.4fs\n", timeDiff(time1, time2));

    return EXIT_SUCCESS;
}
Exemple #2
0
int PrimeFinder(char * n_str, char * str_n_threads)
{
  //Initilize Variables
  FILE * fprime;
  char * fprime_str = "PrimeList";
  uint128 n;
  int n_threads;

  //Main Body
  fprime = fopen(fprime_str,"r+");
  if (fprime == NULL)
    {
      printf("File %s failed to open",fprime_str);
      return EXIT_FAILURE;
    }
  n = alphaTou128(n_str);

    // How many concurrent threads?
  errno = 0; // so we know if strtol fails
  n_threads = strtol(str_n_threads, NULL, 10);
  if (n_threads > 10)
    {
      n_threads = 10;
    }

  // Was there an error in the input arguments?
  int error = FALSE;
  if(errno != 0 || n_threads <= 0) {
    fprintf(stderr, "2nd argument must be a valid integer >= 1, aborting.\n");
	error = TRUE;
    }

  /*
    if(n_str && strcmp(n_str, argv[1]) != 0) {
	fprintf(stderr, "1st argument must be a valid 128-bit integer: '%s' != '%s', aborting.\n", n_str, argv[1]);
	error = TRUE;
    }
  */
  if(error) {
    free(n_str);
    fclose(fprime);
    exit(1);
  }
    
  struct timeval time1;
  struct timeval time2;
  gettimeofday(&time1, NULL);
  printf("Testing if '%s' is prime with %d threads: ", n_str, n_threads);
  fflush(stdout);
  int is_prime = primalityTestParallel(n, n_threads, fprime);
  gettimeofday(&time2, NULL);
  if(is_prime) {
    printf("TRUE");
    fprintf(fprime,"\n%s",n_str);
  } else {
    printf("FALSE");
  }
  printf(", %8.4fs\n", timeDiff(time1, time2));

  fclose(fprime);
  free(n_str);
  return EXIT_SUCCESS;
}