示例#1
0
int main (int argc, char *argv[])
{
    int n;
    double wtime;
    long primes;

    printf ("Number of processors available = %d\n", omp_get_num_procs ( ) );
    printf ("Number of threads =              %d\n", omp_get_max_threads ( ) );
    printf("\n\tn\t\tprime_num\ttime(second)\n");
    n = 100000;

    omp_set_num_threads(NUM_THREAD);  

    wtime = omp_get_wtime();
    primes = prime_number(n);
    wtime = omp_get_wtime() - wtime;
    printf("\t%d\t\t%d\t\t%lf\n", n, primes, wtime);

    n = 500000;

    wtime = omp_get_wtime();
    primes = prime_number(n);
    wtime = omp_get_wtime() - wtime;
    printf("\t%d\t\t%d\t\t%lf\n", n, primes, wtime);

    n = 1000000;

    wtime = omp_get_wtime();
    primes = prime_number(n);
    wtime = omp_get_wtime() - wtime;
    printf("\t%d\t\t%d\t\t%lf\n", n, primes, wtime);

    return 0;
}
/******************************************************************************
 * PRIME_NUMBER_SWEEP does repeated calls to PRIME_NUMBER.
 *
 * @param n_lo		- the first value of n
 * @param n_hi		- the last value of n
 * @param n_factor	- factor by which to increase n after each iteration
 * @return void
 */
void prime_number_sweep ( int n_lo, int n_hi, int n_factor )
{
  int i;
  int n;
  int primes;
  double wtime;
  struct timeval start,end;

  printf ( "\n   Call PRIME_NUMBER to count the primes from 1 to N.\n" );
  printf ( "\n" );
  printf ( "         N        Pi          Time\n" );
  printf ( "\n" );

  n = n_lo;

  while ( n <= n_hi )
  {
    gettimeofday(&start,NULL); //Start timing the computation

    primes = prime_number ( n );

    gettimeofday(&end,NULL); //Stop timing the computation
    wtime = (end.tv_sec+(double)end.tv_usec/1000000) - (start.tv_sec+(double)start.tv_usec/1000000);

    printf ( "  %8d  %8d  %14f\n", n, primes, wtime );

    n = n * n_factor;
  }
  return;
}
示例#3
0
文件: prime.c 项目: Brenchley/backup
void prime_number_sweep ( int n_lo, int n_hi, int n_factor )

/******************************************************************************/

{
  int i;
  int n;
  int primes;
  double wtime;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Call PRIME_NUMBER to count the primes from 1 to N.\n" );
  printf ( "\n" );
  printf ( "         N        Pi          Time\n" );
  printf ( "\n" );

  n = n_lo;

  while ( n <= n_hi )
  {
    wtime = omp_get_wtime ( );

    primes = prime_number ( n );

    wtime = omp_get_wtime ( ) - wtime;

    printf ( "  %8d  %8d  %14f\n", n, primes, wtime );

    n = n * n_factor;
  }
 
  return;
}
void prime_number_sweep ( int n_lo, int n_hi, int n_factor )

/******************************************************************************/
/*
  Purpose:

   PRIME_NUMBER_SWEEP does repeated calls to PRIME_NUMBER.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    06 August 2009

  Author:

    John Burkardt

  Parameters:

    Input, int N_LO, the first value of N.

    Input, int N_HI, the last value of N.

    Input, int N_FACTOR, the factor by which to increase N after
    each iteration.

*/
{
  int i;
  int n;
  int primes;
  double ctime;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Call PRIME_NUMBER to count the primes from 1 to N.\n" );
  printf ( "\n" );
  printf ( "         N        Pi          Time\n" );
  printf ( "\n" );

  n = n_lo;

  while ( n <= n_hi )
  {
    ctime = cpu_time ( );

    primes = prime_number ( n );

    ctime = cpu_time ( ) - ctime;

    printf ( "  %8d  %8d  %14f\n", n, primes, ctime );
    n = n * n_factor;
  }
 
  return;
}
int is_prime_number(int n){
  if(n == 1){
    return 0;
  } else if (n < 0){
    return 0;
  }
  return prime_number(n, (n - 1)); /* This calls the recursive function */

}
int prime_number(int n, int i){  /* This is the recursive function */

  if(i == 1){   /* When i reaches 1, then it means n is prime */
    return 1;
  } else if (n % i == 0){  /* If n is divisible by any value of i */
    return 0;
  }
  return prime_number(n, (i - 1));
}
示例#7
0
int main ( int argc, char *argv[] )

/******************************************************************************/
/*
  Purpose:

    MAIN is the main program for PRIME_MPI.

  Discussion:

    This program calls a version of PRIME_NUMBER that includes
    MPI calls for parallel processing.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    07 August 2009

  Author:

    John Burkardt
*/
{
  int i;
  int id;
  int ierr;
  int n;
  int n_factor;
  int n_hi;
  int n_lo;
  int p;
  int primes;
  int primes_part;
  double wtime;

  n_lo = 1;
  n_hi = 262144;
  n_factor = 2;
/*
  Initialize MPI.
*/
  ierr = MPI_Init ( &argc, &argv );
/*
  Get the number of processes.
*/
  ierr = MPI_Comm_size ( MPI_COMM_WORLD, &p );
/*
  Determine this processes's rank.
*/
  ierr = MPI_Comm_rank ( MPI_COMM_WORLD, &id );

  if ( id == 0 )
  {
    timestamp ( );
    printf ( "\n" );
    printf ( "PRIME_MPI\n" );
    printf ( "  C/MPI version\n" );
    printf ( "\n" );
    printf ( "  An MPI example program to count the number of primes.\n" );
    printf ( "  The number of processes is %d\n", p );
    printf ( "\n" );
    printf ( "         N        Pi          Time\n" );
    printf ( "\n" );
  }

  n = n_lo;

  while ( n <= n_hi )
  {
    if ( id == 0 )
    {
      wtime = MPI_Wtime ( );
    }
    ierr = MPI_Bcast ( &n, 1, MPI_INT, 0, MPI_COMM_WORLD );

    primes_part = prime_number ( n, id, p );

    ierr = MPI_Reduce ( &primes_part, &primes, 1, MPI_INT, MPI_SUM, 0, 
      MPI_COMM_WORLD );

    if ( id == 0 )
    {
      wtime = MPI_Wtime ( ) - wtime;
      printf ( "  %8d  %8d  %14f\n", n, primes, wtime );
    }
    n = n * n_factor;
  }
/*
  Terminate MPI.
*/
  ierr = MPI_Finalize ( );
/*
  Terminate.
*/
  if ( id == 0 ) 
  {
    printf ( "\n");         
    printf ( "PRIME_MPI - Master process:\n");         
    printf ( "  Normal end of execution.\n"); 
    printf ( "\n" );
    timestamp ( );        
  }

  return 0;
}
示例#8
0
文件: primeMPI.c 项目: gokul04/thesis
int main ( int argc, char *argv[] )

/******************************************************************************/
/*
    This program calls a version of PRIME_NUMBER that includes
    MPI calls for parallel processing.
*/
{
  int id;
  int ierr;
  int master = 0;
  int n;
  int n_factor;
  int n_hi;
  int n_lo;
  int p;
  int primes;
  int primes_part;
  double wtime;

  n_lo = 1;
  n_hi = 131072;
  n_factor = 2;

  //Initialize MPI.
  ierr = MPI_Init ( &argc, &argv );

  //Get the number of processes.
  ierr = MPI_Comm_size ( MPI_COMM_WORLD, &p );

  //Determine this processes's rank.
  ierr = MPI_Comm_rank ( MPI_COMM_WORLD, &id );

  if ( id == master )
  {
    timestamp ( );
    printf ( "\n" );
    printf ( "'PRIME_MPI\n" );
    printf ( "  An MPI example program to count the number of primes.\n" );
    printf ( "  The number of processes is %d\n", p );
  }

  n = n_lo;

  while ( n <= n_hi )
  {
    if ( id == master )
    {
      wtime = MPI_Wtime ( );
    }
    ierr = MPI_Bcast ( &n, 1, MPI_INT, master, MPI_COMM_WORLD );

    primes_part = prime_number ( n, id, p );

    ierr = MPI_Reduce ( &primes_part, &primes, 1, MPI_INT, MPI_SUM, master, 
      MPI_COMM_WORLD );

    if ( id == master )
    {
      wtime = MPI_Wtime ( ) - wtime;
    }
    n = n * n_factor;
  }

  //Terminate MPI.
  ierr = MPI_Finalize ( );

  
  //Terminate the main program
  if ( id == master ) {
    printf ( "  Terminated.\n"); 
   
  }

  return 0;
}
示例#9
0
/**
 * Set the dimension of the state space for a PNL_QMC rng
 *
 * @param rng a PnlRng
 * @param dim the dimension of the state space
 * @return OK or FAIL
 */
int pnl_rng_sdim (PnlRng *rng, int dim)
{
  rng->counter=1;
  rng->has_gauss=0;
  rng->gauss=0.;
  rng->dimension = dim;
  if ( dim < 1 ) return FAIL;
  switch (rng->type)
    {
      case PNL_RNG_SQRT:
      {
        int i;
        sqrt_state *state;
        if ( dim > PNL_DIM_MAX_QMC ) return FAIL;
        state = (sqrt_state *)(rng->state);
        prime_number(rng->dimension, state->prime);
        for(i=0; i<rng->dimension; i++)
          {
            state->alpha[i]= sqrt((double) state->prime[i]);
          }
      }
      break;
      case PNL_RNG_HALTON:
        if ( dim > PNL_DIM_MAX_QMC ) return FAIL;
        prime_number(rng->dimension, ((halton_state *)(rng->state))->prime);
        break;
      case PNL_RNG_FAURE:
      {
        int prime[PNL_DIM_MAX_QMC];
        faure_state *state;
        if ( dim > DIM_MAX_FAURE ) return FAIL;
        state = (faure_state *)(rng->state);
        binomial(FAURE_MAXI);
        if((rng->dimension == 2)||(rng->dimension == 1))
          state->r= 3;
        else
          {
            prime_number(rng->dimension, prime);
            state->r=search_value(rng->dimension,prime,rng->dimension);
          }
      }
      break;
      case PNL_RNG_NIEDERREITER:
      {
        int i, j;
        nied_state *state;
        if ( dim  > PNL_DIM_MAX_NIED ) return FAIL;
        state = (nied_state *)(rng->state);
        /* Initialization of initX_n[] */
        for (i=1; i<=PNL_DIM_MAX_NIED; i++)
          state->initialX_n[i]= 0;

        state->facteur= 1.0/(double)(1UL << (BIT_MAX_NIED+1));
        state->saut = 1L << 12;
        state->initial_d = state->saut;

        /* Gray code of saut */
        state->gray = state->saut^(state->saut >> 1);
        for (i=0; i<=BIT_MAX_NIED; i++)
          {
            if (state->gray == 0)
              break;
            for (j= 1; j<= PNL_DIM_MAX_NIED; j++)
              {
                if ((state->gray & 1) == 0)
                  break;
                /* XOR sum */
                state->initialX_n[j] ^= NiedC[i][j];

              }
            state->gray >>= 1;
          }
      }
      break;
      case PNL_RNG_SOBOL_I4:
        if ( dim  > PNL_SOBOL_I4_DIM_MAX2) return FAIL;
        i4_sobol_init (rng, dim);
      break;
      case PNL_RNG_SOBOL_I8:
        if ( dim  > PNL_SOBOL_I8_DIM_MAX2) return FAIL;
        i8_sobol_init (rng, dim);
      break;
      
      default:
        printf ("For PNL_MC rng, you shoud use pnl_rng_sseed instead of pnl_rng_sdim\n");
      break;
    }
  return OK;
}