Example #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;
}
Example #2
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;
}
Example #3
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;
}
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;
}