Ejemplo n.º 1
0
int main()
{
  int streamnum, nstreams, seed, i;
  Sprng *stream;
  double rn;
  int j;
  int gtype;  /*---    */


  /*--- reading in a generator type */
#include "gen_types_menu.h"
  printf("Type in a generator type (integers: 0,1,2,3,4,5):  ");
  scanf("%d", &gtype);
 
  /*
for(j = 0; j < 6; j++){
  */
  /************************** Initialization *******************************/

  streamnum = 0;
  nstreams = 1;

  seed = make_sprng_seed();	/* make new seed each time program is run  */
  stream = SelectType(gtype);

  stream->init_sprng(streamnum,nstreams,seed,SPRNG_DEFAULT); /*initialize stream*/
  printf(" Printing information about new stream\n");
  stream->print_sprng();

  /************************ print random numbers ***************************/

  printf(" Printing 3 random numbers in [0,1):\n");

  for (i=0;i<3;i++)
  {
    rn = stream->sprng();		/* generate double precision random number */
    printf("%f\n", rn);
  }

  stream->free_sprng();		/* free memory used to store stream state  */

  return 0;
}
Ejemplo n.º 2
0
int main()
{

  int i;
  Sprng *gen;
  double temp1, temp2, temp3, temp4;
  double temp_mult = TIMING_TRIAL_SIZE/1.0e6;
/*---   */
  int gentype = 5;
  
  //  scanf("%d\n", &gentype);
  gen = SelectType(gentype);

/*---   */  

  gen->init_rng(0,1,0,PARAM);

/*--- Printing generator and stream information */
/*  gen->print_sprng(); */

  temp1 = cputime();

  for(i=0; i<TIMING_TRIAL_SIZE; i++)
    gen->get_rn_int();
  
  temp2 = cputime();
  

  for(i=0; i<TIMING_TRIAL_SIZE; i++)
    gen->get_rn_flt();
  
  temp3 = cputime();
  

  for(i=0; i<TIMING_TRIAL_SIZE; i++)
    gen->get_rn_dbl();
  
  temp4 = cputime();
  
  if(temp2-temp1 < 1.0e-15 || temp3-temp2 < 1.0e-15 ||  temp4-temp3 < 1.0e-15)
  {
    printf("Timing Information not available/not accurate enough.\n\t...Exiting\n");
    exit(1);
  }

  /* The next line is just used to ensure that the optimization does not remove the call to the RNG. Nothing is really printed.             */
  fprintf(stderr,"Last random number generated\n", gen->get_rn_dbl());

  printf("\nUser + System time Information (Note: MRS = Million Random Numbers Per Second)\n");
  printf("\tInteger generator:\t Time = %7.3f seconds => %8.4f MRS\n", 
	 temp2-temp1, temp_mult/(temp2-temp1));
  printf("\tFloat generator:\t Time = %7.3f seconds => %8.4f MRS\n", 
	 temp3-temp2, temp_mult/(temp3-temp2));
  printf("\tDouble generator:\t Time = %7.3f seconds => %8.4f MRS\n", 
	 temp4-temp3, temp_mult/(temp4-temp3));
  putchar('\n');

  return 0;  
}
Ejemplo n.º 3
0
int main()
{
  int streamnum, nstreams;
  double rn;
  int irn;
  int i, j;
  int gtype;  

  Sprng * ptr;

  streamnum = 0;
  nstreams = 1;

#include "gen_types_menu.h"  /* header file generated by Make */

  printf("Type in a generator type (integers: 0,1,2,3,4,5):  ");
  scanf("%d", &gtype);

  ptr = SelectType(gtype);
  ptr->init_sprng(streamnum, nstreams, SEED, SPRNG_DEFAULT);

  printf("\n --------------------------------------------------------\n");
  printf(" Print information about new stream:\n");
  ptr->print_sprng();
  
  printf(" Printing 3 random numbers in [0,1):\n");
  
  for (i=0;i<3;i++){
    rn = ptr->sprng();   	/* generate a double precision random number */
    printf("%f\n",rn);
  }
		
  printf(" Printing 3 random integers in [0,2^31):\n");
  		
  for (i=0;i<3;i++) {
    irn = ptr->isprng();    /* generate an integer random number */
    printf("%16d\n",irn);
  }

  ptr->free_sprng();  /* free memory used to store stream state */
  
  return 0;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
  int streamnum, nstreams;
  Sprng *stream;
  double rn;
  int i, myid, nprocs, len;
  MPI_Status  status;
  char *packed;
  int gtype;  
            
  MPI_Init(&argc, &argv);	
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

  if(nprocs < 2)
  {
    fprintf(stderr,"ERROR: At least 2 processes required\n");
    MPI_Finalize();
    exit(1);
  }

  if(myid == 0)
  {
#include "gen_types_menu.h"
    printf("Type in a generator type (integers: 0,1,2,3,4,5):  ");
    scanf("%d", &gtype);
  }
  MPI_Bcast(&gtype,1,MPI_INT,0,MPI_COMM_WORLD );

  if (myid==0)	
  {
    streamnum = 0;
    nstreams = 1;
    stream = SelectType(gtype);
    stream->init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT);
    printf("\n\nProcess %d: Print information about stream:\n",myid);
    stream->print_sprng();

    printf("Process %d: Print 2 random numbers in [0,1):\n", myid);
    for (i=0;i<2;i++)
    {
      rn = stream->sprng();	
      printf("Process %d: %f\n", myid, rn);
    }

    len = stream->pack_sprng(&packed); 
    MPI_Send(&len, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); 
    MPI_Send(packed, len, MPI_BYTE, 1, 0, MPI_COMM_WORLD); 

    free(packed);
    nstreams = stream->free_sprng();

    printf(" Process 0 sends stream to process 1\n");
    printf(" %d generators now exist on process 0\n", nstreams);
  }
  else if(myid == 1)
  { 
    MPI_Recv(&len, 1, MPI_INT, 0, MPI_ANY_TAG,
             MPI_COMM_WORLD, &status);

    if ((packed = (char *) malloc(len)) == NULL)
    {
      fprintf(stderr,"ERROR: process %d: Cannot allocate memory\n", myid);
      MPI_Finalize();
      exit(1);
    }

    MPI_Recv(packed, len, MPI_BYTE, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); 
    
    stream = SelectType(gtype);
    stream->unpack_sprng(packed); 

    printf(" Process 1 has received the packed stream\n");
    printf("Process %d: Print information about stream:\n",myid);
    stream->print_sprng();
    free(packed);

    printf(" Process 1 prints 2 numbers from received stream:\n");
    for (i=0;i<2;i++)		
      {
	rn = stream->sprng();
	printf("Process %d: %f\n", myid, rn);
      }
   
      stream->free_sprng();   
  }

  MPI_Finalize();		

  return 0;
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
  int streamnum, nstreams;
  Sprng *stream;
  double rn;
  int i, myid, nprocs, j, k;
  int n = 3;
  int gtype;  /*---    */
  
  double *A, *eVec, *eVal;
  
  /*************************** MPI calls ***********************************/

  MPI_Init(&argc, &argv);	/* Initialize MPI                          */
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);	/* find process id                 */
  MPI_Comm_size(MPI_COMM_WORLD, &nprocs); /* find number of processes      */

  /************************** Initialization *******************************/

  streamnum = myid;	
  nstreams = nprocs;		/* one stream per processor                */
/*--- node 0 is reading in a generator type */
  if(myid == 0)
  {
      if(argc < 2){
          gtype = 0;
      }else{
          gtype = atoi(argv[1]);
    }
      
}
  MPI_Bcast(&gtype,1,MPI_INT,0,MPI_COMM_WORLD );
    
  stream = SelectType(gtype);
  stream->init_sprng(streamnum,nstreams,SEED,SPRNG_DEFAULT);	/* initialize stream */
  
  A = (double*)malloc(sizeof(double) * n * n);
  eVec = (double*)malloc(sizeof(double) * n * n);
  eVal = (double*)malloc(sizeof(double) * n);
  
  //printf("\n\nProcess %d, print information about stream:\n", myid);
  //stream->print_sprng();

  /*********************** print random numbers ****************************/

//  for (i=0;i<3;i++)
//  {
//    rn = stream->sprng();		/* generate double precision random number */
//    printf("Process %d, random number %d: %.14f\n", myid, i+1, rn);
//  }

  /*************************** free memory *********************************/

  /******************* Fill Random Matrix *******************************/
  for( i = 0; i < n; i++){
        A[i + i*n] = stream->sprng();
        for(j = 0; j < i; j++){
            A[i + j*n] = stream->sprng();
            A[i*n + j] = A[i + j*n];
        }
      
  }
  
  find_eigen_vectors(A, n, eVal, eVec );

  /***************** Output data into a MATLAB readible file ***********/
  char *outfilename;

  outfilename = (char*)malloc(sizeof(char) * 56);
  
  sprintf( outfilename, "output%d.m", myid);
  
  //char *mode="r";
  FILE *ifp;
  if((ifp = fopen(outfilename, "w"))==NULL){
      printf("Could not open file!!!\n");
       return 2;
    }
 fprintf(ifp, "A%d = [ ", myid);
 for(i = 0; i < n; i++){
	for(j=0;j<n; j++){
		fprintf(ifp, " %1.15lg ", A[i+j*n]); 

	}
	if(i < n-1)
		fprintf(ifp, ";");
	else
		fprintf(ifp, "];\n");
} 

 fprintf(ifp, "eVec%d = [ ", myid);
 for(i = 0; i < n; i++){
        for(j=0;j<n; j++){
                fprintf(ifp, " %1.15lg ", eVec[i+j*n]); 

        }
        if(i < n-1)
                fprintf(ifp, ";");
        else
                fprintf(ifp, "];\n");
}

fprintf(ifp, "eVal%d = [ ", myid);
 for(i = 0; i < n; i++){
                fprintf(ifp, " %1.15lg ", eVal[i]); 

        if(i < n-1)
                fprintf(ifp, ";");
        else
                fprintf(ifp, "];\n");

}



  fclose(ifp);



  /************************* Free Memory and End *******************/
  stream->free_sprng();		/* free memory used to store stream state  */

  free(A);
  free(eVec);
  free(eVal);
  free(outfilename);
  
  
  MPI_Finalize();		/* Terminate MPI                           */

  return 0;
}