Exemplo n.º 1
0
//------------------------------
// Hauptfunktion des Programmes
//------------------------------
int main(int argc, char *argv[]) {
  int i, *fld;
  int count, seed;

  printf("QuickSort.c\n");

  if(argc > 1)
    count = atoi(argv[1]);
  else
    count = 10000;

  if(argc > 2)
    seed = atoi(argv[2]);
  else
    seed = 123456;

  my_srand(seed);

  fld = (void*) malloc(sizeof(*fld) * count);
  for(i = 0; i < count; ++i)
      fld[i] = my_rand();

  printf("Sorting %d random numbers (seed %d)\n",
          count, seed);
  // Sortieren
  quicksort(fld, 0, count-1);

  // Ausgabe
#ifdef COUNTACTIONS
  printf("Sorted. (needed %d comparisons and %d moves.\n", vergleiche, bewegungen);
#else
  printf("Sorted.\n");
#endif

  if(verify(fld, count))
      printf("Verify succeeded.\n");
  else
      printf("Verify failed.\n");

  return 0;
}
Exemplo n.º 2
0
static void* func(void*ptr)
{
	struct timeval start, end;
	gettimeofday( &start, NULL );
	int i;
	int err;
	printf("child tid:%d\n", gettid());

	int max = my_rand(10);

	for(i=0; i<max; i++) {
		//printf("child ok\n");
		sleep(1);
	}

	gettimeofday( &end, NULL );
    printf("tid:%d,max:%d, used-time:%lu.%lu sec\n",\
            gettid(),max,end.tv_sec-start.tv_sec, end.tv_usec-start.tv_usec);
    pthread_exit((void *)123);
	//exit(0);
	//return;
}
Exemplo n.º 3
0
int audio_put (int a, int c)
{
	static short sample16;
	int s;

	if (g_add_noise) {

	  if ((byte_count & 1) == 0) {
	    sample16 = c & 0xff;		/* save lower byte. */
	    byte_count++;
	    return c;
	  }
	  else {
	    float r;

	    sample16 |= (c << 8) & 0xff00;	/* insert upper byte. */
	    byte_count++;
	    s = sample16;  // sign extend.

/* Add random noise to the signal. */
/* r should be in range of -1 .. +1. */

/* Use own function instead of rand() from the C library. */
/* Windows and Linux have different results, messing up my self test procedure. */
/* No idea what Mac OSX and BSD might do. */
 

	    r = (my_rand() - MY_RAND_MAX/2.0) / (MY_RAND_MAX/2.0);

	    s += 5 * r * g_noise_level * 32767;

	    if (s > 32767) s = 32767;
	    if (s < -32767) s = -32767;

	    putc(s & 0xff, out_fp);  
	    return (putc((s >> 8) & 0xff, out_fp));
	  }
	}
	else {
Exemplo n.º 4
0
/**
 * \name   SimulationHeureEnsoleillement
 * \brief  Calcule le nombre d'heures d'ensoleillement de la journée
 * \param  Structure Simulation météo du jour, structures des normales mensuelles
 * \return none
 */
void SimulationHeuresEnsoleillement(ST_DonneeMeteo NormalesMensuelles, PTR_ST_SimuMeteo Meteo_Jour)
{
  //Création des variables locales
  float nombre_aleatoire;
  float nb_hnormales;
  float r;
  r=100;
  
  //Calcul du nombre d'heure d'ensoleillement par jour de non pluie
    if(Meteo_Jour->date.Mois%2==0)
  {
    if(Meteo_Jour->date.Mois==2)
      nb_hnormales=(float)NormalesMensuelles.nb_soleil/28;
    else
      nb_hnormales=(float)NormalesMensuelles.nb_soleil/30;
  }
  else
    nb_hnormales=(float)NormalesMensuelles.nb_soleil/31;
  
  //Génération de nombres aléatoires
  nombre_aleatoire=my_rand();
  
  //Calcul du nombre d'heure d'ensoleillement
  if(Meteo_Jour->condition==0)
  {
    Meteo_Jour->h_soleil = nombre_aleatoire*2*nb_hnormales;
    //printf("0-%2.2f-%2.2f\n",nombre_aleatoire,Meteo_Jour->h_soleil);
  }
  if(Meteo_Jour->condition==1)
  {
    Meteo_Jour->h_soleil = nombre_aleatoire*1.2*nb_hnormales;
    //printf("1-%2.2f-%2.2f\n",nombre_aleatoire,Meteo_Jour->h_soleil);
  }
  if(Meteo_Jour->condition==4)
    Meteo_Jour->h_soleil = 1*nombre_aleatoire*nb_hnormales;
  else
     Meteo_Jour->h_soleil = 0.8*nombre_aleatoire*nb_hnormales;    
}
Exemplo n.º 5
0
/**
 * Ein einfaches Beispielprogramm. Alle Argumente des Programms müssen
 * Zahlen sein.
 * Bsp:<pre>
 * java Heapsort 6 13 17 42 9 3 5
 * array={6,13,17,42,9,3,5}
 * sorted array={3,5,6,9,13,17,42}
 * </pre>
 */
int main (int argc, char *argv[]) {
	// Umwandeln der Argumente in Zahlen
	int *b;
	int i, count, seed;

	printf("Heap.c\n");

	if(argc > 1)
		count = atoi(argv[1]);
	else
		count = 10000;

	if(argc > 2)
		seed = atoi(argv[2]);
	else
		seed = 123456;

	my_srand(seed);

	b = (void*) malloc(sizeof(b[0]) * count);
	for(i = 0; i < count; ++i)
		b[i] = my_rand();

	printf("Sorting %d random numbers (seed %d)\n",
			count, seed);

	// Sortieren
	b = heapsort_(b, count);

	printf("Sorted.\n");

	if(verify(b, count))
		printf("Verify succeeded.\n");
	else
		printf("Verify failed.\n");

	return 0;
}
Exemplo n.º 6
0
/*-----------------------------------------------------------------*/
void* Thread_work(void* rank) {
   long my_rank = (long) rank;
   int i, val;
   double which_op;
   unsigned seed = my_rank + 1;
   int my_member=0, my_insert=0, my_delete=0;
   int ops_per_thread = total_ops/thread_count;

   for (i = 0; i < ops_per_thread; i++) {
      which_op = my_drand(&seed);
      val = my_rand(&seed) % MAX_KEY;
      if (which_op < search_percent) {
         pthread_mutex_lock(&mutex);
         Member(val);
         pthread_mutex_unlock(&mutex);
         my_member++;
      } else if (which_op < search_percent + insert_percent) {
         pthread_mutex_lock(&mutex);
         Insert(val);
         pthread_mutex_unlock(&mutex);
         my_insert++;
      } else { /* delete */
         pthread_mutex_lock(&mutex);
         Delete(val);
         pthread_mutex_unlock(&mutex);
         my_delete++;
      }
   }  /* for */

   pthread_mutex_lock(&count_mutex);
   member_total += my_member;
   insert_total += my_insert;
   delete_total += my_delete;
   pthread_mutex_unlock(&count_mutex);

   return NULL;
}  /* Thread_work */
Exemplo n.º 7
0
void
account_set_password(struct account *account, const char *pw)
{
    char salt[13] = "$6$........$";
    int idx;

    for (idx = 3; idx < 12; idx++) {
        salt[idx] = my_rand() & 63;
        if (salt[idx] < 10)
            salt[idx] += '0';
        else if (salt[idx] < 36)
            salt[idx] = salt[idx] - 10 + 'A';
        else if (salt[idx] < 62)
            salt[idx] = salt[idx] - 36 + 'a';
        else if (salt[idx] == 63)
            salt[idx] = '.';
        else
            salt[idx] = '/';
    }
    free(account->password);
    account->password = strdup(crypt(pw, salt));
    sql_exec("update accounts set password='******' where idnum=%d",
        tmp_sqlescape(account->password), account->id);
}
Exemplo n.º 8
0
//---------------------------------------------------------------------------
void t_mep_chromosome::uniform_crossover(const t_mep_chromosome &parent2, t_mep_chromosome &offspring1, t_mep_chromosome &offspring2, t_mep_parameters *parameters)
{
    offspring1.code_length = code_length;
    offspring2.code_length = code_length;

    offspring1.num_total_variables = num_total_variables;
    offspring2.num_total_variables = num_total_variables;

    for (int i = 0; i < code_length; i++)
        if (my_rand() % 2) {
            offspring1.prg[i] = prg[i];
            offspring2.prg[i] = parent2.prg[i];
        }
        else {
            offspring1.prg[i] = parent2.prg[i];
            offspring2.prg[i] = prg[i];
        }

    if (parameters->constants.constants_can_evolve && parameters->constants.constants_type == AUTOMATIC_CONSTANTS)
        for (int c = 0; c < num_constants; c++) {
            offspring1.constants_double[c] = constants_double[c];
            offspring2.constants_double[c] = parent2.constants_double[c];
        }
}
Exemplo n.º 9
0
void readswrites( PDTV pDisk )
{
  int  i, j;
  int  xfer, num_xfer;      /*to access buffer correct # times*/
  long xfer_amt;            /*amount to transfer to/from buffer*/
  int  fsize_index;			/*file size index (0..8)*/
  int  rnum;				/*rand. num to choose read or write*/
  int  rep1, rep2;			/*indices to loop through each file*/
					          /*set twice, and all sets three times*/

  for( rep1 = 0; rep1 < 3; rep1++ )
    {		/*in order to achieve locality which*/
      for( i = 0; i < NSETS; i++ )
        {		/*is consistent with buffer cache data*/
          for( rep2 = 0; rep2 < 2; rep2++ )
            {	/*of Ousterhout et al (1985)*/
              for( j = 0; j < SET_SIZE; j++ )
                {
                  if( ( pDisk->fd = open( pDisk->files[ i ][ j ], 2)) < 0 )
                    {
                      printf( "readswrites: cannot open file\n" );
                      EndItAll( 1, pDisk );
                    }
                  fsize_index = *( pDisk->files[ i ][ j ] ) - '0';     /*max xfer_amt = BUFFERSIZE*/
                  if( pDisk->bsize[ fsize_index ] >= BUFFERSIZE )
                    {
                      num_xfer = pDisk->bsize[ fsize_index ] / BUFFERSIZE;
                      xfer_amt = BUFFERSIZE;
                    }
                  else
                    {
                      num_xfer = 1;
                      xfer_amt = pDisk->bsize[ fsize_index ];
                    }
                  rnum = my_rand( 3 );
                  if( rnum < 2 )
                    {		/*read:write = 2:1*/
                      lseek( pDisk->fd, 0L, 0 );
                      for( xfer = 0; xfer < num_xfer; xfer++ )
                        {
                          if( ( pDisk->nbytes = read( pDisk->fd, pDisk->buffer, xfer_amt)) < 0 )
                            {
                              printf( "readswrites %d: read error\n", pDisk->ulTask );
                              EndItAll( 1, pDisk );
                             }
                        }
                    }
                  else
                    {
                      lseek( pDisk->fd, 0L, 0 );
                      for( xfer = 0; xfer < num_xfer; xfer++ )
                        {
                          if( ( pDisk->nbytes = write( pDisk->fd, pDisk->buffer, xfer_amt)) < 0 )
                            {
                              printf( "readswrites %d: write error\n", pDisk->ulTask );
                              EndItAll( 1, pDisk );
                            }
                        }
                    }
                  close( pDisk->fd );
                }
            }
        }
    }
}
Exemplo n.º 10
0
void init( PDTV pDisk )
{
  int this_set;                         /*mark the file set (0..NSETS-1)*/
  int bcount;                           /*counter to track #spacer files*/
  int fcount;                           /*a counter to tell where to create*/
  int i, j, k;                                      /*files to flush buffer cache and*/
                                        /*spread other files across disk*/
  pDisk->bsize[ 0 ] = 256;   pDisk->bfreq[ 0 ] = 128;
  pDisk->bsize[ 1 ] = 512;   pDisk->bfreq[ 1 ] = 64;
  pDisk->bsize[ 2 ] = 1024;  pDisk->bfreq[ 2 ] = 64;
  pDisk->bsize[ 3 ] = 2048;  pDisk->bfreq[ 3 ] = 64;
  pDisk->bsize[ 4 ] = 4096;  pDisk->bfreq[ 4 ] = 32;  /*set file block sizes and*/
  pDisk->bsize[ 5 ] = 8192;  pDisk->bfreq[ 5 ] = 32;  /*access frequencies*/
  pDisk->bsize[ 6 ] = 16384; pDisk->bfreq[ 6 ] = 8;
  pDisk->bsize[ 7 ] = 32768; pDisk->bfreq[ 7 ] = 2;
  pDisk->bsize[ 8 ] = 65536; pDisk->bfreq[ 8 ] = 2;

  k = 0;                                  /*set up files*/
  bcount = 0;
  fcount = 0;
  for( i = 0; i < NBLOCKSIZES; i++ )
    {
      for( j = 0; j < pDisk->bfreq[ i ]; j++ )
        {
          if( i < NBLOCKSIZES - 1 )
            this_set = j % NSETS;
          else
            this_set = ( j + 2 ) % NSETS;
          sprintf( pDisk->tmp, "%0d_%0d", i, j );       /*create filename*/
          pDisk->files[ this_set ][ k ] = malloc( 1 + strlen( pDisk->tmp ));
          if( ! pDisk->files[ this_set ][ k ] )
            {
              printf( "Could not allocate string for filename\n" );
              EndItAll( 1, pDisk );
            }
		  strcpy( pDisk->files[ this_set ][ k ], pDisk->tmp );
          initfile( pDisk->tmp, pDisk->bsize[ i ], pDisk );
          if( i < NBLOCKSIZES - 1 && this_set == NSETS - 1 )
            k++;
          if( bcount < NBFLUSH_FILES && fcount % 44 == 0 )
            {
              sprintf( pDisk->tmp, "%bf_%0d", bcount );       /*create spacer file*/
              pDisk->buf_flush_files[ bcount ] = malloc( 1 + strlen( pDisk->tmp ));
              if( ! pDisk->buf_flush_files[ bcount ] )
                {
                  printf( "Could not allocate string for filename\n" );
                  EndItAll( 1, pDisk );
                }
              strcpy( pDisk->buf_flush_files[ bcount ], pDisk->tmp );
              initfile( pDisk->tmp, BFLUSH_FILE_SIZE, pDisk );
              bcount++;
            }
          fcount++;
        }
    }

  for( i = 0; i < NBFLUSH_FILES; i++ )
    {        /*read spacer files to flush buffers*/
      if( ( pDisk->fd = open( pDisk->buf_flush_files[ i ], 2)) < 0 )
        {
          printf( "error opening buffer flush file\n" );
          EndItAll( 1, pDisk );
        }
      lseek( pDisk->fd, 0L, 0 );
      k = BFLUSH_FILE_SIZE / BUFFERSIZE;
      for( j = 0; j < k; j++ )
        {
          if( ( pDisk->nbytes = read( pDisk->fd, pDisk->buffer, BUFFERSIZE )) < 0 )
            {
              printf( "Error reading buffer flush file\n" );
              EndItAll( 1, pDisk );
            }
        }
      close( pDisk->fd );
    }

  srand( SEED );			/*initialize random number generator*/
  for( i = 0; i < NSETS; i++ )
    {		/*permutation for reading/writing*/
      for( j = SET_SIZE; j > 0; j-- )
        {
          k = my_rand( j );
          strcpy( pDisk->tmp, pDisk->files[ i ][ j - 1 ] );
          strcpy( pDisk->files[ i ][ j - 1 ], pDisk->files[ i ][ k ] );
          strcpy( pDisk->files[ i ][ k ], pDisk->tmp );
        }
    }
}
Exemplo n.º 11
0
int main(int argc, char**argv)
{
    int err;
    int i;
    FmmvHandle _FMMV;
    FmmvHandle *FMMV = &_FMMV;
    void (*GEN_M)(FmmvHandle *FMMV, Box *box);
    void (*GEN_L)(FmmvHandle *FMMV, Box *target, Box *source);
    void (*EVAL_L)(FmmvHandle *FMMV, Box *box);
    void (*EVAL_DIRECT)(FmmvHandle *FMMV, Box *target, Box *source) = FMMV->eval_direct;

    _FLOAT_ X1[8*FMM_S_EXP_MAX];
    _FLOAT_ X2[8*FMM_S_EXP_MAX];
    _FLOAT_ X[FMM_S_EXP_MAX];
    _FLOAT_ D[FMM_S_EXP_MAX];


    Box _box_A;
    Box _box_B;
    Box _parent_A;
    Box _parent_B;
    int which_child_A = SWD;
    int which_child_B = SWD;


    Box *box_A = &_box_A;
    Box *box_B = &_box_B;
    Box *parent_A = &_parent_A;
    Box *parent_B = &_parent_B;

    int NParticles = 100;
    int NTargets = 100;
     _FLOAT_ (*particles)[3];
     _FLOAT_ (*targets)[3] = NULL;
     _FLOAT_ *charges;
     _FLOAT_ x, y, z;
     _FLOAT_ x_B, y_B, z_B;
     _FLOAT_ x_A, y_A, z_A;
     _FLOAT_ *potX;
     _FLOAT_ *potL;
     _FLOAT_ *exPot;
     _FLOAT_ errL2;
     double beta = 0.0;
     int pM = 8;
     int pL = 8;
     int s = 8;
     int *perm;
     int *permTargets;
     int printResult=0;

    int level = 2;
    _FLOAT_ size;
    
    _FLOAT_ dx = 1;
    _FLOAT_ dy = 1;
    _FLOAT_ dz = 2;

  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );


    FMMV->dataKind = FMM_ST_STANDARD;
    GEN_M = gen_M_ST_standard;
    GEN_L = gen_L_ST_standard;
    EVAL_L = eval_L_ST_standard;
#if (FMM_PRECISION==0)
    EVAL_DIRECT = eval_direct_ST_standard_acc1; 
#else
    EVAL_DIRECT = eval_direct_ST_standard_acc2;
#endif

    set_from_command_line_double(argc, argv, "beta", &beta);
    set_from_command_line_bool(argc, argv, "printResult", &printResult);
    set_from_command_line_int(argc, argv, "NParticles", &NParticles);
    set_from_command_line_int(argc, argv, "NTargets", &NTargets);
    set_from_command_line_int(argc, argv, "pM", &pM);
    set_from_command_line_int(argc, argv, "pL", &pL);
    set_from_command_line_int(argc, argv, "s", &s);
    set_from_command_line_int(argc, argv, "level", &level);
    size = ldexp(1.0, -level);
    
    particles = (_FLOAT_ (*)[3]) calloc(NParticles, 3*sizeof(_FLOAT_));  
    charges = (_FLOAT_ *) calloc(NParticles, sizeof(_FLOAT_));  
    perm = (int *) calloc(NParticles, sizeof(int));  
    permTargets = (int *) calloc(NTargets, sizeof(int));  
    targets = (_FLOAT_ (*)[3]) calloc(NTargets, 3*sizeof(_FLOAT_));  
    potX = (_FLOAT_ *) calloc(NTargets, sizeof(_FLOAT_));
    potL = (_FLOAT_ *) calloc(NTargets, sizeof(_FLOAT_));
    exPot = (_FLOAT_ *) calloc(NTargets, sizeof(_FLOAT_));

    FMMV->beta = beta;
    FMMV->pM = pM;
    FMMV->pL = pL;
    FMMV->s_eps = s;
    FMMV->particles = particles;
    FMMV->charges = charges;
    FMMV->perm = perm;
    FMMV->NParticles = NParticles;
    FMMV->targets = targets;
    FMMV->permTargets = permTargets;
    FMMV->NTargets = NTargets;
    FMMV->scale = 1.0;
    FMMV->lambda = 0;

    my_srand(1481765933);
    x_A = 0.5*size;
    y_A = 0.5*size;
    z_A = 0.5*size;

    box_A->firstParticle = 0;
    box_A->noOfParticles = NParticles;
    parent_A->firstParticle = 0;
    parent_A->noOfParticles = NParticles;
    box_A->x = x_A;
    box_A->y = y_A;
    box_A->z = z_A;
    box_A->level = level;
    parent_A->level = level-1;
    box_A->M = 0; 
    
    x_B = x_A + size*dx;
    y_B = y_A + size*dy;
    z_B = z_A + size*dz;

    box_B->firstTarget = 0;
    box_B->noOfTargets = NTargets;
    parent_B->firstTarget = 0;
    parent_B->noOfTargets = NTargets;
    box_B->x = x_B;
    box_B->y = y_B;
    box_B->z = z_B;
    box_B->level = level;
    parent_B->level = level-1;
    box_B->L = 0; 
    
    for (i=0; i<8; i++) {
        parent_A->child[i] = 0;
        parent_B->child[i] = 0;
    }
    
    parent_A->child[which_child_A] = box_A;
    parent_B->child[which_child_B] = box_B;
    
    for (i=0;i<NParticles;i++) {
        x = x_A + size*(my_rand() / (_FLOAT_) MY_RAND_MAX - 0.5);
        y = y_A + size*(my_rand() / (_FLOAT_) MY_RAND_MAX - 0.5);
        z = z_A + size*(my_rand() / (_FLOAT_) MY_RAND_MAX - 0.5);
        particles[i][0] = x;
        particles[i][1] = y;
        particles[i][2] = z;
    }    
    for (i=0;i<NParticles;i++) {
        perm[i] = i;
        charges[i] = 1.0;
    }

    for (i=0;i<NTargets;i++) {
        x = x_B + size*(my_rand() / (_FLOAT_) MY_RAND_MAX - 0.5);
        y = y_B + size*(my_rand() / (_FLOAT_) MY_RAND_MAX - 0.5);
        z = z_B + size*(my_rand() / (_FLOAT_) MY_RAND_MAX - 0.5);
        targets[i][0] = x;
        targets[i][1] = y;
        targets[i][2] = z;
    }    

    for (i=0;i<NTargets;i++) {
        permTargets[i] = i;
    }
    
    init_all(FMMV);

    err = ida_allocate(FMMV);
    copy_particles(FMMV);
    copy_charges(FMMV);
    
    zero_pot(FMMV);
    EVAL_DIRECT(FMMV, box_B, box_A);
    FMMV->potentials = exPot;
    backcopy_pot(FMMV);

    GEN_M(FMMV, box_A);

    GEN_L(FMMV, box_B, box_A);

    zero_pot(FMMV);
    EVAL_L(FMMV, box_B);
    FMMV->potentials = potL;
    backcopy_pot(FMMV);

    /**********************************************/
    for (i=0;i<6;i++) {
       box_B->X[i] = 0;
    }
    box_B->X[XU] = X;
    for (i=0;i<pL*(pL+1);i++) {
       box_B->L[i] = 0.0;
    }   

    init_M2L(FMMV, -1);
    init_M2L(FMMV, level);

    M2X(FMMV, 0, parent_A, X1, X2);
    gen_diag_X2X(ldexp(beta,-level), FMMV->lambda, FMMV->M, FMMV->s_eps, FMMV->s_exp, dx, dy, dz, D); 
    VEC_MUL_C(FMMV->s_exp/2, D, X1+FMMV->s_exp*which_child_A, box_B->X[XU]);
    X2L(FMMV, 0, parent_B, 0);

    finish_M2L(FMMV);

    zero_pot(FMMV);
    EVAL_L(FMMV, box_B);
    FMMV->potentials = potX;
    backcopy_pot(FMMV);
    /**********************************************/


    ida_free(FMMV);

    relL2Err(0, 0, 0);
    if (printResult) {
        printf("\n          Pot(L)   Pot(exact)     rel.err.\n");
        printf("============================================\n");
    }
    for (i=0; i<NTargets; i++) {
        relL2Err(1, potL[i], exPot[i]); /* accumulate */
        if (printResult) {
            printf("%3i %12.4e %12.4e %12.4e\n", i, (double) potL[i], (double) exPot[i], (double) relErr(potL[i], exPot[i]));
        }   
    }
    errL2 = relL2Err(2, 0, 0); /*finalize*/ 
    printf ("\nerr_L2(potL) = %.4e\n", errL2);

    relL2Err(0, 0, 0);
    if (printResult) {
        printf("\n          Pot(X)   Pot(exact)     rel.err.\n");
        printf("============================================\n");
    }
    for (i=0; i<NTargets; i++) {
        relL2Err(1, potX[i], exPot[i]); /* accumulate */
        if (printResult) {
            printf("%3i %12.4e %12.4e %12.4e\n", i, (double) potX[i], (double) exPot[i], (double) relErr(potX[i], exPot[i]));
        }   
    }
    errL2 = relL2Err(2, 0, 0); /*finalize*/ 
    printf ("\nerr_L2(potX) = %.4e\n", errL2);
  
    // TODO: deallocate...
    finish_all(FMMV);

    return 0;
}
Exemplo n.º 12
0
int main()
{
#ifdef DEBUG
    logfile_create("test.log", 3);
    //logfile_create("stdout", 4);
#endif
    HashTable* ht;
    char key[64];
    int  valuesize = 8;
    char val[64];
    unsigned int attrformat[3] = {4, 3, 1};
    unsigned int attrarray[3][3] = { {8, 3, 1}, { 7, 2,1}, {6, 2, 1} };
    int num  = 100;
    int attrnum = 3;
    int ret;
    int i = 0;
    char *conffile;
    char *name = "test";
    conffile = "memlink.conf";
    DINFO("config file: %s\n", conffile);
    myconfig_create(conffile);

    my_runtime_create_common("memlink");
    ht = g_runtime->ht;

    hashtable_create_table(ht, name, valuesize, attrformat, attrnum, MEMLINK_LIST, 0);
    Table *tb = hashtable_find_table(ht, name);
    ///////////begin test;
    //test1 : hashtable_add_info_attr - create key
    for (i = 0; i < num; i++) {
        sprintf(key, "heihei%03d", i);
        table_create_node(tb, key);
    }
    for (i = 0; i < num; i++) {
        sprintf(key, "heihei%03d", i);
        HashNode* pNode = table_find(tb, key);
        if (NULL == pNode) {
            DERROR("hashtable_add_info_attr error. can not find %s\n", key);
            return -1;
        }
    }

    ///////test : hashtable_add_attr insert num value
    HashNode    *node = NULL;
    DataBlock   *dbk  = NULL;
    char	    *item = NULL;
    int         pos   = 0;

    for (i = 0; i < num; i++) {
        sprintf(val, "value%03d", i);
        pos = i;
        ret = hashtable_insert(ht, name, key, val, attrarray[i%3], attrnum, pos);
        if (ret < 0) {
            DERROR("add value err: %d, key:%s, value:%s, pos:%d\n", ret, key, val, pos);
            return ret;
        }

        ret = table_find_value(tb, key, val, &node, &dbk, &item);
        if (ret < 0) {
            DERROR("not found value: %d, %s\n", ret, key);
            return ret;
        }
    }
    DINFO("insert %d values ok!\n", num);

    //////////test 4 :tag
    for (i = 0; i < num; i++) {
        HashNode *node = NULL;
        DataBlock *dbk = NULL;
        char	 *item = NULL;
        int v = my_rand(num);
        sprintf(val, "value%03d", v);

        int tag = my_rand(2);

        //printf("val:%s, tag:%d \n", val, tag);
        //hashtable_find_value(ht, key, val, &node, &dbk, &item);
        int ret = hashtable_tag(ht, name, key, val, tag);
        if (MEMLINK_OK != ret) {
            DERROR("err hashtable_tag val:%s, tag:%d\n", val, tag);
            return ret;
        }

        ret = table_find_value(tb, key, val, &node, &dbk, &item);
        if (ret < 0) {
            DERROR("not found value: %d, %s\n", ret, key);
            return ret;
        }
        char attr[HASHTABLE_ATTR_MAX_ITEM * HASHTABLE_ATTR_MAX_BYTE] = {0};
        char *mdata = item + tb->valuesize;
        memcpy(attr, mdata, tb->attrsize);

        char flag = *(attr + 0) & 0x02;
        flag = flag >> 1;
        //printf("flag:%d, tag:%d \n", flag, tag);
        if (flag != tag) {
            DERROR("tag err val:%s, flag:%d, tag:%d!\n", val, flag, tag);
            break;
        }
    }
    DINFO("hashtable_tag test end!\n");

    return 0;
}
Exemplo n.º 13
0
  void PlummerSphere::DumpModel()
  {
    double tm = 1.0;
    double pmas = tm/nall;
    double conv = 3.0*M_PI/16.0;
    std::cout << "pmas = " << pmas << "\n";

    uint64 i = 0;
    while(i < nall) {
      double X1 = my_rand();
      double X2 = my_rand();
      double X3 = my_rand();
      double R = 1.0/sqrt( (pow(X1,-2.0/3.0) - 1.0) );

      if(R < 100.0) {
	double Z = (1.0 - 2.0*X2)*R;
	double X = sqrt(R*R - Z*Z) * cos(2.0*M_PI*X3);
	double Y = sqrt(R*R - Z*Z) * sin(2.0*M_PI*X3);

	double Ve = sqrt(2.0)*pow( (1.0 + R*R), -0.25 );

	double X4 = 0.0;
	double X5 = 0.0;

	while( 0.1*X5 >= X4*X4*pow( (1.0-X4*X4), 3.5) ) {
	  X4 = my_rand(); X5 = my_rand();
	}

	double V = Ve*X4;
	double X6 = my_rand();
	double X7 = my_rand();

	double Vz = (1.0 - 2.0*X6)*V;
	double Vx = sqrt(V*V - Vz*Vz) * cos(2.0*M_PI*X7);
	double Vy = sqrt(V*V - Vz*Vz) * sin(2.0*M_PI*X7);

	X *= conv;
	Y *= conv;
	Z *= conv;    
	Vx /= sqrt(conv);
	Vy /= sqrt(conv);
	Vz /= sqrt(conv);

	ms[i] = pmas;

	px[i] = X;
	py[i] = Y;
	pz[i] = Z;
	
	vx[i] = Vx;
	vy[i] = Vy;
	vz[i] = Vz;

	i++;
	
	} /* if(r < 100.0) */
    } /* while(i < N) */

    std::stringstream new_file;
    new_file << "model_" << nall/1024 << ".cdf";
    WriteCDF(new_file.str().c_str());
  }
Exemplo n.º 14
0
int main()
{
	int array_size = 20;
//	int cycle_length = 128/8;
	int cycle_length = 2;
	TYPE array[array_size];
	TYPE array_check[array_size];
	int i = 0;
	int j = 0;
	int k = 0; 
	TYPE tmp = NULL;
	TYPE tmp_bak = NULL;
	int count = 20;

	printf("-----------------------------\n");
	while(count--)
	{
		for(i = 0; i < array_size; i++)
			array[i] = &array[i];
		for (i = 0; i < array_size; i++)
			printf("0x%lx ", (long)array[i]);	
		printf("\n");

		for(i = 0; i < array_size; i++)
			array_check[i] = NULL;
	
		for (i = array_size-1; i > cycle_length; i--)
		{
			j = my_rand(i/cycle_length) * cycle_length + (i%cycle_length);
//			j = rand()%(i/cycle_length) * cycle_length + (i%cycle_length);
//			j = rand()%i;
			tmp = array[i];
			array[i] = array[j];
			array[j] = tmp;
		}
	
		for (i = 0; i < array_size; i++)
			printf("0x%lx ", (long)array[i]);	
		printf("\n");
		
	for(k = 0; k < cycle_length; k++)
	{	
		array_subscript = k;
		i = cycle_length + k;
		tmp = array[k];
		array_check[k] = tmp;
		while(i < (array_size-cycle_length+k)) // check no loop in the array[]
		{
			tmp_bak = tmp;
			tmp = (void*)*(long*)tmp;
			for(j = k; j < i; j = (j+cycle_length))
			{
			/* test whether have a round in the pointer chasing array, if it does have, pick up one element which not in array_check[], to exchange which array[tmp_bak] */
				if(array_check[j] == tmp)
				{
					printf("?????????????????????????????????????????\n");
					exchange_element(array,find_one_noloop_element(array, array_check, cycle_length, array_size, i, k),tmp_bak);
					tmp = (void*)*(long*)tmp_bak;
				}
					
			}
			array_check[i] = tmp;
			i = i + cycle_length;
		}
	}
	
		for(i = 0; i < array_size; i++)
			printf("0x%lx ", (long)array[i]);
	
		printf("\n");
	
		for (i = 0; i < array_size; i++)
			printf("0x%lx ", (long)array_check[i]);	
		printf("\n");
		printf("-----------------------------\n");
	}
	return 0;
}
Exemplo n.º 15
0
unsigned int sad(int test_blockx, int test_blocky, int *best_block_x, int *best_block_y, int iterations)
{
    unsigned char b[256][256];

    unsigned char a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15;
    unsigned char b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15;

    int i, x, y, blocky;
    unsigned tmp_diff, min_diff = 0xFFFFFFFF; // MAX_UINT

    // Fill in some random values to compare
    for(x = 0; x < 256; x++)
        for(y = 0; y < 256; y++)
            b[y][x] = (unsigned char) my_rand() % 255;

    // Execute Block matching 100 times
    for(i = 0; i < iterations; i++)
    {
        // Iterate over whole frame, x,y=coords of current block
        for(x = 1; x < 256 - 16; x++)
            for(y = 0; y < 256 - 16; y++)
            {
                tmp_diff = 0;

                // Compare current Block with reference block
                for(blocky = 0; blocky < 16; blocky++)
                {
                    // Vektor Loads
                    a0 = b[blocky][0];
                    a1 = b[blocky][1];
                    a2 = b[blocky][2];
                    a3 = b[blocky][3];
                    a4 = b[blocky][4];
                    a5 = b[blocky][5];
                    a6 = b[blocky][6];
                    a7 = b[blocky][7];
                    a8 = b[blocky][8];
                    a9 = b[blocky][9];
                    a10 = b[blocky][10];
                    a11 = b[blocky][11];
                    a12 = b[blocky][12];
                    a13 = b[blocky][13];
                    a14 = b[blocky][14];
                    a15 = b[blocky][15];
                    b0 = b[blocky + y][x + 0];
                    b1 = b[blocky + y][x + 1];
                    b2 = b[blocky + y][x + 2];
                    b3 = b[blocky + y][x + 3];
                    b4 = b[blocky + y][x + 4];
                    b5 = b[blocky + y][x + 5];
                    b6 = b[blocky + y][x + 6];
                    b7 = b[blocky + y][x + 7];
                    b8 = b[blocky + y][x + 8];
                    b9 = b[blocky + y][x + 9];
                    b10 = b[blocky + y][x + 10];
                    b11 = b[blocky + y][x + 11];
                    b12 = b[blocky + y][x + 12];
                    b13 = b[blocky + y][x + 13];
                    b14 = b[blocky + y][x + 14];
                    b15 = b[blocky + y][x + 15];

                    // psadpw, would be nice if this could be done by loop unrolling
                    tmp_diff += ((a0 > b0) ? (a0 - b0) : (b0 - a0))  +
                                ((a1 > b1) ? (a1 - b1) : (b1 - a1)) +
                                ((a2 > b2) ? (a2 - b2) : (b2 - a2)) +
                                ((a3 > b3) ? (a3 - b3) : (b3 - a3)) +
                                ((a4 > b4) ? (a4 - b4) : (b4 - a4)) +
                                ((a5 > b5) ? (a5 - b5) : (b5 - a5)) +
                                ((a6 > b6) ? (a6 - b6) : (b6 - a6)) +
                                ((a7 > b7) ? (a7 - b7) : (b7 - a7)) +
                                ((a8 > b8) ? (a8 - b8) : (b8 - a8)) +
                                ((a9 > b9) ? (a9 - b9) : (b9 - a9)) +
                                ((a10 > b10) ? (a10 - b10) : (b10 - a10)) +
                                ((a11 > b11) ? (a11 - b11) : (b11 - a11)) +
                                ((a12 > b12) ? (a12 - b12) : (b12 - a12)) +
                                ((a13 > b13) ? (a13 - b13) : (b13 - a13)) +
                                ((a14 > b14) ? (a14 - b14) : (b14 - a14)) +
                                ((a15 > b15) ? (a15 - b15) : (b15 - a15));
                }

                // Check if the current block is least different
                if(min_diff > tmp_diff)
                {
                    min_diff = tmp_diff;
                    *best_block_x = x;
                    *best_block_y = y;
                }
            }
    }

    return(min_diff);
}
Exemplo n.º 16
0
inline int		my_rangeRand(int min, int max)
{
	return  ( min + ( my_rand() / D_RAND_MAX * (max - min) ) );
}
Exemplo n.º 17
0
int main(int argc, char *argv[])
{

    if(argc == 2)
    {
        N = atoi(argv[1]);
    }
    else
    {
        printf("usage: ''gen-plum.exe 16'' (16KB particle) \n\n");
        printf("Warning!!! using the default number of stars: 16384 (16KB) \n");
        N = 16;
//  exit(argc);
    }


    N = N*KB;


    /* VIR = 2*EK + EG ~ 0 => ETOT = EK + EG = -EK = EG/2 */

    /* G = M = R = 1 => E_tot = -3*Pi/64 */
//  conv = 1.0;
//  EK_teor = 3.0*Pi/64.0;

    /* G = M = 1; R = 3*Pi/16 => E_tot = -0.25 */
//  conv = 3.0*Pi/16.0;
//  EK_teor = 0.25;

    /* G = M = 1; R = 3*Pi/16 => E_tot = -0.25 */ // DOM STUPID STUFF
    conv = 0.05;
    EK_teor = 0.25;

    /* INIT the rand() !!! */
    srand(19640916);                 /* it is just my birthday :-) */
    /* srand(time(NULL)); */


    i = 0;

    while(i < N)
    {

        X1 = my_rand();
        X2 = my_rand();
        X3 = my_rand();

        R = 1.0/sqrt( (pow(X1,-2.0/3.0) - 1.0) );

        if(R < 100.0)
        {

            Z = (1.0 - 2.0*X2)*R;
            X = sqrt(R*R - Z*Z) * cos(2.0*M_PI*X3);
            Y = sqrt(R*R - Z*Z) * sin(2.0*M_PI*X3);

            Ve = sqrt(2.0)*pow( (1.0 + R*R), -0.25 );


            X4 = 0.0;
            X5 = 0.0;

            while( 0.1*X5 >= X4*X4*pow( (1.0-X4*X4), 3.5) )
            {
                X4 = my_rand();
                X5 = my_rand();
            }


            V = Ve*X4;

            X6 = my_rand();
            X7 = my_rand();

            Vz = (1.0 - 2.0*X6)*V;
            Vx = sqrt(V*V - Vz*Vz) * cos(2.0*M_PI*X7);
            Vy = sqrt(V*V - Vz*Vz) * sin(2.0*M_PI*X7);

            X *= conv;
            Y *= conv;
            Z *= conv;
            Vx /= sqrt(conv);
            Vy /= sqrt(conv);
            Vz /= sqrt(conv);


            m[i] = M/N;

            x[i][0] = X;
            x[i][1] = Y;
            x[i][2] = Z;

            v[i][0] = Vx;
            v[i][1] = Vy;
            v[i][2] = Vz;

            /*
                tmp_i = ldiv(i, 256);
                if(tmp_i.rem == 0) printf("i = %d \n", i);
            */

            tmp_i = ldiv(i, N/64);

            if(tmp_i.rem == 0)
            {
                printf(".");
                fflush(stdout);
            }

            i++;

        } /* if(r < 100.0) */

    } /* while(i < N) */





    if(CMCORR == 1)
    {

        mcm = 0.0;

        for(k=0; k<3; k++)
        {
            xcm[k] = 0.0;
            vcm[k] = 0.0;
        } /* k */

        for(i=0; i<N; i++)
        {

            mcm += m[i];

            for(k=0; k<3; k++)
            {
                xcm[k] += m[i] * x[i][k];
                vcm[k] += m[i] * v[i][k];
            } /* k */

        }  /* i */


        for(k=0; k<3; k++)
        {
            xcm[k] /= mcm;
            vcm[k] /= mcm;
        } /* k */


        for(i=0; i<N; i++)
        {

            for(k=0; k<3; k++)
            {
                x[i][k] -= xcm[k];
                v[i][k] -= vcm[k];
            } /* k */

        } /* i */

    } /* if(CMCORR == 1) */




    out = fopen("data.inp","w");

    fprintf(out,"%04d \n", 0);
    fprintf(out,"%06d \n", N);
    fprintf(out,"%.16E \n", 0.0);

    for(i=0; i<N; i++)
    {
        fprintf(out,"%06d   %.16E   % .16E % .16E % .16E   % .16E % .16E % .16E \n",
                i, m[i], x[i][0], x[i][1], x[i][2], v[i][0], v[i][1], v[i][2]);

        EK += 0.5*m[i] * (v[i][0]*v[i][0] + v[i][1]*v[i][1] + v[i][2]*v[i][2]);
    }



    printf("\n");
    printf("N = %d   EK = % .6E  EK_teor = % .6E  DE/EK = % .6E \n",
           N, EK, EK_teor, (EK-EK_teor)/EK_teor);



    fclose(out);

    return(0);

}
Exemplo n.º 18
0
//done
static void inc_c(cpu *pcpu) {
	pcpu->cx++;
	if (CPU_falla())
		pcpu->cx-=my_rand(-1,1);
	pcpu->ip++;
}
Exemplo n.º 19
0
//done
static void sub_ac(cpu *pcpu) {
	pcpu->ax = pcpu->ax - pcpu->cx;
	if (CPU_falla())
		pcpu->ax-=my_rand(-1,1);
	pcpu->ip++;
}	
Exemplo n.º 20
0
//done
static void dec_c(cpu *pcpu) {
	pcpu->cx--;
	if (CPU_falla())
		pcpu->cx+=my_rand(-1,1);
	pcpu->ip++;
}	
Exemplo n.º 21
0
Arquivo: snake.c Projeto: blygo/test
gboolean play_game(gpointer data)
{
      int i;
      if (playing == FALSE) {	//如果游戏还没开始,初始化游戏并开始  
	    init_game();
	    set_label();
	    playing = TRUE;
	    return TRUE;
      }

      snake_run();

      switch (snake.direction) {
      case UP:
	    snake.body[0].y = snake.body[0].y - 1;
	    //判断是否有向自己身体走的情况,如果有就改变方向  
	    //头部[0]和身体[2]的坐标(x或y)相同,就需要向反方向改变  
	    if (snake.body[0].y == snake.body[2].y) {
		  snake.direction = DOWN;
		  snake.body[0].y = snake.body[0].y + 2;
	    }
	    break;
      case DOWN:
	    snake.body[0].y = snake.body[0].y + 1;
	    if (snake.body[0].y == snake.body[2].y) {
		  snake.direction = UP;
		  snake.body[0].y = snake.body[0].y - 2;
	    }

	    break;
      case LEFT:
	    snake.body[0].x = snake.body[0].x - 1;
	    if (snake.body[0].x == snake.body[2].x) {
		  snake.direction = RIGHT;
		  snake.body[0].x = snake.body[0].x + 2;
	    }
	    break;
      case RIGHT:
	    snake.body[0].x = snake.body[0].x + 1;
	    if (snake.body[0].x == snake.body[2].x) {
		  snake.direction = LEFT;
		  snake.body[0].x = snake.body[0].x - 2;
	    }
	    break;
      default:
	    break;
      }
      //判断蛇的头部是否碰到了自己的身体,如果碰到了就去死!  
      for (i = 4; i <= snake.length; i++)
	    if (snake.body[0].x == snake.body[i].x
		&& snake.body[0].y == snake.body[i].y)
		  snake.live = FALSE;

      //判断蛇是否碰到边界,如果碰到了就去死!  
      if (snake.body[0].x < 0 || snake.body[0].y < 0
	  || snake.body[0].x > 40 || snake.body[0].y > 40) {
	    snake.live = FALSE;
      }
      //判断蛇是否死了  
      if (she.live == FALSE) {
	    live = live - 1;
	    if (live == 0) {	//没命就GAME OVER  
		  speed = 200;
		  level = 1;
		  foods = 20;
		  live = 3;
		  game_stop();
		  gtk_label_set_text(GTK_LABEL(label), " - Game Over -");
		  return FALSE;
	    }
	    //还有命就继续  
	    set_label();
	    init_game();
      }
      //判断蛇是否吃到了蛋,如果吃到了就变长!  
      if (snake.body[0].x == food.x && snake.body[0].y == food.y) {
	    snake.length = snake.length + 1;
	    snake.body[snake.length].x = snake.body[snake.length - 1].x;
	    snake.body[snake.length].y = snake.body[snake.length - 1].y;
	    //重新生成一个蛋  
	    food.x = my_rand(0, 20);
	    food.y = food.x;
	    foods = foods - 1;	//减少一个蛋  
	    if (foods == 0) {	//如果吃完全部的蛋,进入下一局,速度加快  
		  level = level + 1;
		  foods = 20;
		  if (speed > 30)
			speed = speed - 20;
		  game_stop();
		  game_start();
	    }
	    set_label();
      }

      draw();
      return TRUE;
}
Exemplo n.º 22
0
int main(int argc, char**argv)
{
    int err;
    int i;
    double t0, t1;
    FmmvHandle _FMMV;
    FmmvHandle *FMMV = &_FMMV;
    void (*GEN_M)(FmmvHandle *FMMV, Box *box);
    void (*EVAL_M)(FmmvHandle *FMMV, Box *target, Box *source);
    void (*EVAL_L)(FmmvHandle *FMMV, Box *box);
    void (*GEN_L)(FmmvHandle *FMMV, Box *target, Box *source);
    void (*GEN_L_EVAL_M)(FmmvHandle *FMMV, Box *target, Box *source);
    void (*EVAL_DIRECT)(FmmvHandle *FMMV, Box *target, Box *source) = FMMV->eval_direct;

    Box _box_A;
    Box _box_B;

    Box *box_A = &_box_A;
    Box *box_B = &_box_B;

    Box _child_A[4];
    Box _child_B[4];

    int NParticles = 100;
    int NTargets = 100;
     _FLOAT_ (*particles)[2];
     _FLOAT_ (*targets)[2] = NULL;
     _FLOAT_ *charges;
     _FLOAT_ (*dipoleMoments)[2] = NULL;
     _FLOAT_ x, y;
     _FLOAT_ *potM;
     _FLOAT_ *potL;
     _FLOAT_ (*gradM)[2] = NULL;
     _FLOAT_ (*gradL)[2] = NULL;
     _FLOAT_ *exPot;
     _FLOAT_ (*exGrad)[2] = NULL;
     _FLOAT_ errL2, errL2grad;
     double beta = 0.0;
     int pM = 8;
     int pL = 8;
     int s = 8;
     int *perm;
     int *permTargets;
     int with_dipoleSources=0;
     int with_gradients=0;
     int LM_combined=0;
     int with_M2M=0;
     int with_L2L=0;
     int printResult=0;
#ifdef USE_SINGLE_PRECISION 
     int exAcc=1;
#else     
     int exAcc=2;
#endif
     //int exAcc=0;

    _FLOAT_ x_A = .125;
    _FLOAT_ y_A = .125;
    double size_A = .0625;
    int level_A = 2;
    _FLOAT_ x_B = .875;
    _FLOAT_ y_B = .875;
    double size_B = .0625;
    int level_B = 2;

    feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );


    set_from_command_line_double(argc, argv, "beta", &beta);
    set_from_command_line_bool(argc, argv, "dipoleSources", &with_dipoleSources);
    set_from_command_line_bool(argc, argv, "gradients", &with_gradients);
    set_from_command_line_bool(argc, argv, "printResult", &printResult);
    set_from_command_line_bool(argc, argv, "LM_combined", &LM_combined);
    set_from_command_line_bool(argc, argv, "M2M", &with_M2M);
    set_from_command_line_bool(argc, argv, "L2L", &with_L2L);
    set_from_command_line_int(argc, argv, "directEvalAccuracy", &exAcc);
 
        if (with_dipoleSources) {
            if (with_gradients) {
                FMMV->dataKind = FMM_ST_DIPOLE_GRAD;
                GEN_M = gen_M_ST_dipole_grad;
                EVAL_M = eval_M_ST_dipole_grad;
                GEN_L = gen_L_ST_dipole_grad;
                EVAL_L = eval_L_ST_dipole_grad;
                GEN_L_EVAL_M = gen_L_eval_M_dipole_grad;
                switch (exAcc) {
                    case 0: EVAL_DIRECT = eval_direct_ST_dipole_grad_acc0; break;
                    case 1: EVAL_DIRECT = eval_direct_ST_dipole_grad_acc1; break;
#if FMM_PRECISION==1
                    case 2: EVAL_DIRECT = eval_direct_ST_dipole_grad_acc2; break;
#endif                
                }    
            }
            else {
                FMMV->dataKind = FMM_ST_DIPOLE;
                GEN_M = gen_M_ST_dipole;
                EVAL_M = eval_M_ST_dipole;
                GEN_L = gen_L_ST_dipole;
                EVAL_L = eval_L_ST_dipole;
                GEN_L_EVAL_M = gen_L_eval_M_dipole;
                switch (exAcc) {
                    case 0: EVAL_DIRECT = eval_direct_ST_dipole_acc0; break;
                    case 1: EVAL_DIRECT = eval_direct_ST_dipole_acc1; break;
#if FMM_PRECISION==1
                    case 2: EVAL_DIRECT = eval_direct_ST_dipole_acc2; break;
#endif                
                }    
            }
        }
        else {
            if (with_gradients) {
                FMMV->dataKind = FMM_ST_GRAD;
                GEN_M = gen_M_ST_grad;
                EVAL_M = eval_M_ST_grad;
                GEN_L = gen_L_ST_grad;
                EVAL_L = eval_L_ST_grad;
                GEN_L_EVAL_M = gen_L_eval_M_grad;
                switch (exAcc) {
                    case 0: EVAL_DIRECT = eval_direct_ST_grad_acc0; break;
                    case 1: EVAL_DIRECT = eval_direct_ST_grad_acc1; break;
#if FMM_PRECISION==1
                    case 2: EVAL_DIRECT = eval_direct_ST_grad_acc2; break;
#endif                
                }    
            }
            else {
                FMMV->dataKind = FMM_ST_STANDARD;
                GEN_M = gen_M_ST_standard;
                EVAL_M = eval_M_ST_standard;
                GEN_L = gen_L_ST_standard;
                EVAL_L = eval_L_ST_standard;
                GEN_L_EVAL_M = gen_L_eval_M_standard;
                switch (exAcc) {
                    case 0: EVAL_DIRECT = eval_direct_ST_standard_acc0; break;
                    case 1: EVAL_DIRECT = eval_direct_ST_standard_acc1; break;
#if FMM_PRECISION==1
                    case 2: EVAL_DIRECT = eval_direct_ST_standard_acc2; break;
#endif                
                }    
            }
        }

    set_from_command_line_int(argc, argv, "NParticles", &NParticles);
    set_from_command_line_int(argc, argv, "NTargets", &NTargets);
    set_from_command_line_int(argc, argv, "pM", &pM);
    set_from_command_line_int(argc, argv, "pL", &pL);
    set_from_command_line_int(argc, argv, "s", &s);
    set_from_command_line_int(argc, argv, "level_A", &level_A);
    set_from_command_line_int(argc, argv, "level_B", &level_B);
//    set_from_command_line_double(argc, argv, "size_A", &size_A);
//    set_from_command_line_double(argc, argv, "size_B", &size_B);
    size_A = ldexp(1.0, -level_A);
    size_B = ldexp(1.0, -level_B);
    
    particles = (_FLOAT_ (*)[2]) calloc(NParticles, 2*sizeof(_FLOAT_));  
    charges = (_FLOAT_ *) calloc(NParticles, sizeof(_FLOAT_));  
    perm = (int *) calloc(NParticles, sizeof(int));  
    permTargets = (int *) calloc(NTargets, sizeof(int));  
    targets = (_FLOAT_ (*)[2]) calloc(NTargets, 2*sizeof(_FLOAT_));  
    potM = (_FLOAT_ *) calloc(NTargets, sizeof(_FLOAT_));
    potL = (_FLOAT_ *) calloc(NTargets, sizeof(_FLOAT_));
    exPot = (_FLOAT_ *) calloc(NTargets, sizeof(_FLOAT_));
    if (with_gradients) {
	gradM = (_FLOAT_ (*)[2]) calloc(NTargets, 2*sizeof(_FLOAT_));  
	gradL = (_FLOAT_ (*)[2]) calloc(NTargets, 2*sizeof(_FLOAT_));  
	exGrad = (_FLOAT_ (*)[2]) calloc(NTargets, 2*sizeof(_FLOAT_));  
    }	   
    if (with_dipoleSources) {
	dipoleMoments = (_FLOAT_ (*)[2]) calloc(NParticles, 2*sizeof(_FLOAT_));  
    }

    FMMV->beta = beta;
    FMMV->pM = pM;
    FMMV->pL = pL;
    FMMV->s_eps = s;
    FMMV->particles = particles;
    FMMV->charges = charges;
    FMMV->dipoleMoments = dipoleMoments;
    FMMV->perm = perm;
    FMMV->NParticles = NParticles;
    FMMV->targets = targets;
    FMMV->permTargets = permTargets;
    FMMV->NTargets = NTargets;
    FMMV->scale = 1.0;
    FMMV->lambda = 0;

    my_srand(1481765933);

    box_A->firstParticle = 0;
    box_A->noOfParticles = NParticles;
    box_A->x = x_A;
    box_A->y = y_A;
    box_A->level = level_A;
    box_A->M = 0; 
    /* if (with_M2M) */ {
        int m = NParticles/4;
        double d = 0.25*size_A;
        int c;
        for (c=0; c<4; c++) {
            box_A->child[c] = &_child_A[c];
            box_A->child[c]->level = level_A + 1;
            box_A->child[c]->M = 0;
            box_A->child[c]->firstParticle = c*m;
            box_A->child[c]->noOfParticles = m;
        }
        box_A->child[3]->noOfParticles = NParticles - box_A->child[3]->firstParticle;
         
        box_A->child[SW]->x = x_A - d;
        box_A->child[SW]->y = y_A - d;

        box_A->child[NW]->x = x_A - d;
        box_A->child[NW]->y = y_A + d;

        box_A->child[SE]->x = x_A + d;
        box_A->child[SE]->y = y_A - d;

        box_A->child[NE]->x = x_A + d;
        box_A->child[NE]->y = y_A + d;

        for (c=0; c<4; c++) {
            for (i = box_A->child[c]->firstParticle; i < box_A->child[c]->firstParticle+box_A->child[c]->noOfParticles; i++) {
                x = box_A->child[c]->x + 2.0*d*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
                y = box_A->child[c]->y + 2.0*d*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
                particles[i][0] = x;
                particles[i][1] = y;
            }
        }
    }
    /* else {
        double d = size_A/2.0;
        for (i=0;i<NParticles;i++) {
            x = x_A + size_A*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
            y = y_A + size_A*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
            particles[i][0] = x;
            particles[i][1] = y;
        }    
    }*/
    for (i=0;i<NParticles;i++) {
        perm[i] = i;
        charges[i] = 1.0;
    }
    if (with_dipoleSources) {
        for (i=0;i<NParticles;i++) {
            x = (my_rand() / (_FLOAT_) MY_RAND_MAX) - 0.5;
            y = (my_rand() / (_FLOAT_) MY_RAND_MAX) - 0.5;
            dipoleMoments[i][0] = x;
            dipoleMoments[i][1] = y;

        }
    }

    box_B->firstTarget = 0;
    box_B->noOfTargets = NTargets;
    box_B->x = x_B;
    box_B->y = y_B;
    box_B->level = level_B;
    box_B->L = 0; 
    /* if (with_L2L)  */ { 
        int m = NTargets/4;
        double d = 0.25*size_B;
        int c;
        for (c=0; c<4; c++) {
            box_B->child[c] = &_child_B[c];
            box_B->child[c]->level = level_B + 1;
            box_B->child[c]->L = 0;
            box_B->child[c]->firstTarget = c*m;
            box_B->child[c]->noOfTargets = m;
        }
        box_B->child[3]->noOfTargets = NTargets - box_B->child[3]->firstTarget;
         
        box_B->child[SW]->x = x_B - d;
        box_B->child[SW]->y = y_B - d;

        box_B->child[NW]->x = x_B - d;
        box_B->child[NW]->y = y_B + d;

        box_B->child[SE]->x = x_B + d;
        box_B->child[SE]->y = y_B - d;

        box_B->child[NE]->x = x_B + d;
        box_B->child[NE]->y = y_B + d;

        for (c=0; c<4; c++) {
            for (i = box_B->child[c]->firstTarget; i < box_B->child[c]->firstTarget+box_B->child[c]->noOfTargets; i++) {
                x = box_B->child[c]->x + 2.0*d*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
                y = box_B->child[c]->y + 2.0*d*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
                targets[i][0] = x;
                targets[i][1] = y;
            }
        }
    }
    /* else {
        double d = size_B/2.0;
        for (i=0;i<NTargets;i++) {
            x = x_B + size_B*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
            y = y_B + size_B*(my_rand() / (_FLOAT_) MY_RAND_MAX) - d;
            targets[i][0] = x;
            targets[i][1] = y;
        }    
    } */
    for (i=0;i<NTargets;i++) {
        permTargets[i] = i;
    }
    


    init_all(FMMV);

    err = ida_allocate(FMMV);
    copy_particles(FMMV);
    copy_charges(FMMV);
    
    zero_pot(FMMV);
    t0 = (double) clock()/CLOCKS_PER_SEC;
    EVAL_DIRECT(FMMV, box_B, box_A);
    t1 = (double) clock()/CLOCKS_PER_SEC - t0;
    printf("time (eval_direct)  : %7.4f\n", t1);
    FMMV->potentials = exPot;
    FMMV->gradients = exGrad;
    backcopy_pot(FMMV);
    if (with_M2M||with_L2L) { // in init_M2M is initialisation als necessary for L2L
        init_M2M(FMMV, -1);
    }
    if (with_M2M) {
        int c;
        t0 = (double) clock()/CLOCKS_PER_SEC;
        for (c=0; c<4; c++) {
            GEN_M(FMMV, box_A->child[c]);    
        }
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (GEN_M)        : %7.4f\n", t1);
        //init_M2M(FMMV, -1);
        init_M2M(FMMV, level_A);
        t0 = (double) clock()/CLOCKS_PER_SEC;
        M2M(FMMV, box_A);
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (M2M)          : %7.4f\n", t1);
    }
    else {
        t0 = (double) clock()/CLOCKS_PER_SEC;
        GEN_M(FMMV, box_A);
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (GEN_M)        : %7.4f\n", t1);
    }    
/*
    printf("M:\n");
    for (i=0;i<=pM;i++) {
        printf("%3i %24.16e %24.16e\n", i, box_A->M[2*i], box_A->M[2*i+1]);
    }	
*/
    		
        zero_pot(FMMV);
        t0 = (double) clock()/CLOCKS_PER_SEC;
        EVAL_M(FMMV, box_B, box_A);
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (EVAL_M)       : %7.4f\n", t1);
        FMMV->potentials = potM;
        FMMV->gradients = gradM;
        backcopy_pot(FMMV);

        t0 = (double) clock()/CLOCKS_PER_SEC;
        GEN_L(FMMV, box_B, box_A);
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (GEN_L)        : %7.4f\n", t1);

    zero_pot(FMMV);
    if (with_L2L) {
        int c;
        init_L2L(FMMV, -1);
        init_L2L(FMMV, level_B);
        t0 = (double) clock()/CLOCKS_PER_SEC;
        L2L(FMMV, box_B);
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (L2L)          : %7.4f\n", t1);
        t0 = (double) clock()/CLOCKS_PER_SEC;
        for (c=0; c<4; c++) {
            EVAL_L(FMMV, box_B->child[c]);    
        }
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (EVAL_L)       : %7.4f\n", t1);
        printf("L:\n");
        for (i=0;i<=pL;i++) {
            printf("%3i %24.16e %24.16e\n", i, box_B->child[3]->L[2*i], box_B->child[3]->L[2*i+1]);
        }	
    }
    else {
        t0 = (double) clock()/CLOCKS_PER_SEC;
        EVAL_L(FMMV, box_B);
        t1 = (double) clock()/CLOCKS_PER_SEC - t0;
        printf("time (EVAL_L)       : %7.4f\n", t1);
        printf("L:\n");
        for (i=0;i<=pL;i++) {
            printf("%3i %24.16e %24.16e\n", i, box_B->L[2*i], box_B->L[2*i+1]);
        }	
    }
    FMMV->potentials = potL;
    FMMV->gradients = gradL;
    backcopy_pot(FMMV);

    ida_free(FMMV);

    if (with_gradients) {
	relL2Err(0, 0, 0); /* init */
	relL2Err2(0, NULL, NULL); /* init */

	if (printResult) {
  	    printf("\n          Pot(M)   Pot(exact)     rel.err.  rel.err.(grad)\n");
	    printf("============================================================\n");
	}	
	for (i=0; i<NTargets; i++) {
	    relL2Err(1, potM[i], exPot[i]); /* accumulate */
	    relL2Err2(1, gradM[i], exGrad[i]); /* accumulate */
	    if (printResult) {
	        printf("%3i %12.4e %12.4e %12.4e %12.4e\n", i, (double) potM[i], (double) exPot[i], (double) relErr(potM[i], exPot[i]),(_FLOAT_) relErr2(gradM[i], exGrad[i]));
	    }	
  	}
	errL2 = relL2Err(2, 0, 0); /*finalize*/ 
	errL2grad = relL2Err2(2, NULL, NULL); /*finalize*/ 
	printf ("\nerr_L2(potM) = %.4e  err_L2(gradM) = %.4e\n", errL2, errL2grad);

        printf("%24.16e %24.16e\n", exGrad[0][0], exGrad[0][1]);
        printf("%24.16e %24.16e\n", gradM[0][0], gradM[0][1]);
        
	relL2Err(0, 0, 0); /* init */
	relL2Err2(0, NULL, NULL); /* init */

	if (printResult) {
  	    printf("\n          Pot(L)   Pot(exact)     rel.err.  rel.err.(grad)\n");
	    printf("============================================================\n");
	}	
	for (i=0; i<NTargets; i++) {
	    relL2Err(1, potL[i], exPot[i]); /* accumulate */
	    relL2Err2(1, gradL[i], exGrad[i]); /* accumulate */
	    if (printResult) {
	        printf("%3i %12.4e %12.4e %12.4e %12.4e\n", i, (double) potL[i], (double) exPot[i], (double) relErr(potL[i], exPot[i]),(_FLOAT_) relErr2(gradL[i], exGrad[i]));
	    }	
  	}
	errL2 = relL2Err(2, 0, 0); /*finalize*/ 
	errL2grad = relL2Err2(2, NULL, NULL); /*finalize*/ 
	printf ("\nerr_L2(potL) = %.4e  err_L2(gradL) = %.4e\n", errL2, errL2grad);

        printf("%24.16e %24.16e\n", exGrad[0][0], exGrad[0][1]);
        printf("%24.16e %24.16e\n", gradL[0][0], gradL[0][1]);
    }
    else {
        relL2Err(0, 0, 0);
        if (printResult) {
            printf("\n          Pot(M)   Pot(exact)     rel.err.\n");
            printf("============================================\n");
        }
        for (i=0; i<NTargets; i++) {
            relL2Err(1, potM[i], exPot[i]); /* accumulate */
            if (printResult) {
	       printf("%3i %12.4e %12.4e %12.4e\n", i, (double) potM[i], (double) exPot[i], (double) relErr(potM[i], exPot[i]));
            }   
        }
        errL2 = relL2Err(2, 0, 0); /*finalize*/ 
        printf ("\nerr_L2(potM) = %.4e\n", errL2);
    
        relL2Err(0, 0, 0);
        if (printResult) {
            printf("\n          Pot(L)   Pot(exact)     rel.err.\n");
            printf("============================================\n");
        }
        for (i=0; i<NTargets; i++) {
            relL2Err(1, potL[i], exPot[i]); /* accumulate */
            if (printResult) {
	       printf("%3i %12.4e %12.4e %12.4e\n", i, (double) potL[i], (double) exPot[i], (double) relErr(potL[i], exPot[i]));
            }   
        }
        errL2 = relL2Err(2, 0, 0); /*finalize*/ 
        printf ("\nerr_L2(potL) = %.4e\n", errL2);
    }
    
    // TODO: deallocate...
    finish_all(FMMV);

    return 0;
}
Exemplo n.º 23
0
int main()
{
	printf ("%d\n", my_rand());
};
Exemplo n.º 24
0
//---------------------------------------------------------------------------
void t_mep_chromosome::generate_random(t_mep_parameters *parameters, int *actual_operators, int num_actual_operators, int *actual_variables, int num_actual_variables) // randomly initializes the individuals
{
    // I have to generate the constants for this individuals
    if (parameters->constants_probability > 1E-6) {
        if (parameters->constants.constants_type == USER_DEFINED_CONSTANTS) {
            for (int c = 0; c < num_constants; c++)
                constants_double[c] = parameters->constants.constants_double[c];
        }
        else {// automatic constants
            for (int c = 0; c < num_constants; c++)
                constants_double[c] = my_rand() / double(RAND_MAX) * (parameters->constants.max_constants_interval_double - parameters->constants.min_constants_interval_double) + parameters->constants.min_constants_interval_double;
        }
    }

    double sum = parameters->variables_probability + parameters->constants_probability;
    double p = my_rand() / (double)RAND_MAX * sum;

    if (p <= parameters->variables_probability)
        prg[0].op = actual_variables[my_rand() % num_actual_variables];
    else
        prg[0].op = num_total_variables + my_rand() % num_constants;

    for (int i = 1; i < parameters->code_length; i++) {
        double p = my_rand() / (double)RAND_MAX;

        if (p <= parameters->operators_probability)
            prg[i].op = actual_operators[my_rand() % num_actual_operators];
        else if (p <= parameters->operators_probability + parameters->variables_probability)
            prg[i].op = actual_variables[my_rand() % num_actual_variables];
        else
            prg[i].op = num_total_variables + my_rand() % num_constants;

        prg[i].adr1 = my_rand() % i;
        prg[i].adr2 = my_rand() % i;
        prg[i].adr3 = my_rand() % i;
        prg[i].adr4 = my_rand() % i;
    }
}
Exemplo n.º 25
0
void camdtest (cholmod_sparse *A)
{
    double Control [CAMD_CONTROL], Info [CAMD_INFO], alpha ;
    Int *P, *Cp, *Ci, *Sp, *Si, *Bp, *Bi, *Ep, *Ei, *Fp, *Fi,
	*Len, *Nv, *Next, *Head, *Elen, *Deg, *Wi, *W, *Flag, *BucketSet,
	*Constraint ;
    cholmod_sparse *C, *B, *S, *E, *F ;
    Int i, j, n, nrow, ncol, ok, cnz, bnz, p, trial, sorted ;

    /* ---------------------------------------------------------------------- */
    /* get inputs */
    /* ---------------------------------------------------------------------- */

    printf ("\nCAMD test\n") ;

    if (A == NULL)
    {
	return ;
    }

    if (A->stype)
    {
	B = CHOLMOD(copy) (A, 0, 0, cm) ;
    }
    else
    {
	B = CHOLMOD(aat) (A, NULL, 0, 0, cm) ;
    }

    if (A->nrow != A->ncol)
    {
	F = CHOLMOD(copy_sparse) (B, cm) ;
	OK (F->nrow == F->ncol) ;
	CHOLMOD(sort) (F, cm) ;
    }
    else
    {
	/* A is square and unsymmetric, and may have entries in A+A' that
	 * are not in A */
	F = CHOLMOD(copy_sparse) (A, cm) ;
	CHOLMOD(sort) (F, cm) ;
    }

    C = CHOLMOD(copy_sparse) (B, cm) ;

    nrow = C->nrow ;
    ncol = C->ncol ;
    n = nrow ;
    OK (nrow == ncol) ;

    Cp = C->p ;
    Ci = C->i ;

    Bp = B->p ;
    Bi = B->i ;

    /* ---------------------------------------------------------------------- */
    /* S = sorted form of B, using CAMD_preprocess */
    /* ---------------------------------------------------------------------- */

    cnz = CHOLMOD(nnz) (C, cm) ;
    S = CHOLMOD(allocate_sparse) (n, n, cnz, TRUE, TRUE, 0, CHOLMOD_PATTERN,
	    cm);
    Sp = S->p ;
    Si = S->i ;

    W = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    Flag = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    CAMD_preprocess (n, Bp, Bi, Sp, Si, W, Flag) ;

    /* ---------------------------------------------------------------------- */
    /* allocate workspace for camd */
    /* ---------------------------------------------------------------------- */

    P = CHOLMOD(malloc) (n+1, sizeof (Int), cm) ;
    Constraint = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    for (i = 0 ; i < n ; i++)
    {
	Constraint [i] = my_rand () % (MIN (n,6)) ;
    }

    ok = CAMD_cvalid (n, Constraint) ; OK (ok) ;
    if (n > 0)
    {
	Constraint [0] = -1 ;
	ok = CAMD_cvalid (n, Constraint) ; OK (!ok) ;
	Constraint [0] = 0 ;
    }
    ok = CAMD_cvalid (n, Constraint) ; OK (ok) ;
    ok = CAMD_cvalid (n, NULL) ; OK (ok) ;

    Len  = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    Nv   = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    Next = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    Head = CHOLMOD(malloc) (n+1, sizeof (Int), cm) ;
    Elen = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    Deg  = CHOLMOD(malloc) (n, sizeof (Int), cm) ;
    Wi   = CHOLMOD(malloc) (n+1, sizeof (Int), cm) ;
    BucketSet = CHOLMOD(malloc) (n, sizeof (Int), cm) ;

    /* ---------------------------------------------------------------------- */

    for (sorted = 0 ; sorted <= 1 ; sorted++)
    {

	if (sorted) CHOLMOD(sort) (C, cm) ;

	Cp = C->p ;
	Ci = C->i ;

	/* ------------------------------------------------------------------ */
	/* order C with CAMD_order */
	/* ------------------------------------------------------------------ */

	CAMD_defaults (Control) ;
	CAMD_defaults (NULL) ;
	CAMD_control (Control) ;
	CAMD_control (NULL) ;
	CAMD_info (NULL) ;

	ok = CAMD_order (n, Cp, Ci, P, Control, Info, Constraint) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "CAMD permutation", cm)) ;

	ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "CAMD permutation", cm)) ;

	/* no dense rows/cols */
	alpha = Control [CAMD_DENSE] ;
	Control [CAMD_DENSE] = -1 ;
	CAMD_control (Control) ;
	ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "CAMD permutation (alpha=-1)", cm)) ;

	/* many dense rows/cols */
	Control [CAMD_DENSE] = 0 ;
	CAMD_control (Control) ;
	ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "CAMD permutation (alpha=0)", cm)) ;
	Control [CAMD_DENSE] = alpha ;

	/* no aggressive absorption */
	Control [CAMD_AGGRESSIVE] = FALSE ;
	CAMD_control (Control) ;
	ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "CAMD permutation (no agg) ", cm)) ;
	Control [CAMD_AGGRESSIVE] = TRUE ;

	/* ------------------------------------------------------------------ */
	/* order F with CAMD_order */
	/* ------------------------------------------------------------------ */

	Fp = F->p ;
	Fi = F->i ;
	ok = CAMD_order (n, Fp, Fi, P, Control, Info, NULL) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "F: CAMD permutation", cm)) ;

	/* ------------------------------------------------------------------ */
	/* order S with CAMD_order */
	/* ------------------------------------------------------------------ */

	ok = CAMD_order (n, Sp, Si, P, Control, Info, NULL) ;
	printf ("camd return value: "ID"\n", ok) ;
	CAMD_info (Info) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	OK (CHOLMOD(print_perm) (P, n, n, "CAMD permutation", cm)) ;

	/* ------------------------------------------------------------------ */
	/* order E with CAMD_2, which destroys its contents */
	/* ------------------------------------------------------------------ */

	E = CHOLMOD(copy) (B, 0, -1, cm) ;	/* remove diagonal entries */
	bnz = CHOLMOD(nnz) (E, cm) ;

	/* add the bare minimum extra space to E */
	ok = CHOLMOD(reallocate_sparse) (bnz + n, E, cm) ;
	OK (ok) ;
	Ep = E->p ;
	Ei = E->i ;

	for (j = 0 ; j < n ; j++)
	{
	    Len [j] = Ep [j+1] - Ep [j] ;
	}

	printf ("calling CAMD_2:\n") ;
	if (n > 0)
	{
	    CAMD_2 (n, Ep, Ei, Len, E->nzmax, Ep [n], Nv, Next, P, Head, Elen,
		Deg, Wi, NULL, Info, NULL, BucketSet) ;
	    CAMD_info (Info) ;
	    OK (CHOLMOD(print_perm) (P, n, n, "CAMD2 permutation", cm)) ;
	}

	/* ------------------------------------------------------------------ */
	/* error tests */
	/* ------------------------------------------------------------------ */

	ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	ok = CAMD_order (-1, Cp, Ci, P, Control, Info, NULL) ;
	OK (ok == CAMD_INVALID);
	ok = CAMD_order (0, Cp, Ci, P, Control, Info, NULL) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	ok = CAMD_order (n, NULL, Ci, P, Control, Info, NULL) ;
	OK (ok == CAMD_INVALID);
	ok = CAMD_order (n, Cp, NULL, P, Control, Info, NULL) ;
	OK (ok == CAMD_INVALID);
	ok = CAMD_order (n, Cp, Ci, NULL, Control, Info, NULL) ;
	OK (ok == CAMD_INVALID);

	if (n > 0)
	{
	    printf ("CAMD error tests:\n") ;

	    p = Cp [n] ;
	    Cp [n] = -1 ;
	    ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	    OK (ok == CAMD_INVALID) ;

	    if (Size_max/2 == Int_max)
	    {
		Cp [n] = Int_max ;
		ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
		printf ("CAMD status is %d\n", ok) ;
		OK (ok == CAMD_OUT_OF_MEMORY) ;
	    }

	    Cp [n] = p ;
	    ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	    OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	    if (Cp [n] > 0)
	    {
		printf ("Mangle column zero:\n") ;
		i = Ci [0] ;
		Ci [0] = -1 ;
		ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
		CAMD_info (Info) ;
		OK (ok == CAMD_INVALID) ;
		Ci [0] = i ;
	    }
	}

	ok = CAMD_valid (n, n, Sp, Si) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	ok = CAMD_valid (-1, n, Sp, Si) ;	    OK (ok == CAMD_INVALID) ;
	ok = CAMD_valid (n, -1, Sp, Si) ;	    OK (ok == CAMD_INVALID) ;
	ok = CAMD_valid (n, n, NULL, Si) ;	    OK (ok == CAMD_INVALID) ;
	ok = CAMD_valid (n, n, Sp, NULL) ;	    OK (ok == CAMD_INVALID) ;

	if (n > 0 && Sp [n] > 0)
	{

	    p = Sp [n] ;
	    Sp [n] = -1 ;
	    ok = CAMD_valid (n, n, Sp, Si) ; OK (ok == CAMD_INVALID) ;
	    Sp [n] = p ;

	    p = Sp [0] ;
	    Sp [0] = -1 ;
	    ok = CAMD_valid (n, n, Sp, Si) ; OK (ok == CAMD_INVALID) ;
	    Sp [0] = p ;

	    p = Sp [1] ;
	    Sp [1] = -1 ;
	    ok = CAMD_valid (n, n, Sp, Si) ; OK (ok == CAMD_INVALID) ;
	    Sp [1] = p ;

	    i = Si [0] ;
	    Si [0] = -1 ;
	    ok = CAMD_valid (n, n, Sp, Si) ; OK (ok == CAMD_INVALID) ;
	    Si [0] = i ;

	}

	ok = CAMD_valid (n, n, Sp, Si) ;
	OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;
	CAMD_preprocess (n, Bp, Bi, Sp, Si, W, Flag) ;
	ok = CAMD_valid (n, n, Sp, Si) ;
	OK (ok == CAMD_OK) ;

	if (n > 0 && Bp [n] > 0)
	{

	    p = Bp [n] ;
	    Bp [n] = -1 ;
	    ok = CAMD_valid (n, n, Bp, Bi) ;	    OK (ok == CAMD_INVALID) ;
	    Bp [n] = p ;


	    p = Bp [1] ;
	    Bp [1] = -1 ;
	    ok = CAMD_valid (n, n, Bp, Bi) ;	    OK (ok == CAMD_INVALID) ;
	    Bp [1] = p ;

	    i = Bi [0] ;
	    Bi [0] = -1 ;
	    ok = CAMD_valid (n, n, Bp, Bi) ;	    OK (ok == CAMD_INVALID) ;
	    Bi [0] = i ;
	}

	CAMD_preprocess (n, Bp, Bi, Sp, Si, W, Flag) ;

	Info [CAMD_STATUS] = 777 ;
	CAMD_info (Info) ;

	/* ------------------------------------------------------------------ */
	/* memory tests */
	/* ------------------------------------------------------------------ */

	if (n > 0)
	{
	    camd_malloc = cm->malloc_memory ;
	    camd_free = cm->free_memory ;
	    ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
	    OK (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK)) ;

	    test_memory_handler ( ) ;
	    camd_malloc = cm->malloc_memory ;
	    camd_free = cm->free_memory ;
	    for (trial = 0 ; trial < 6 ; trial++)
	    {
		my_tries = trial ;
		printf ("CAMD memory trial "ID"\n", trial) ;
		ok = CAMD_order (n, Cp, Ci, P, Control, Info, NULL) ;
		CAMD_info (Info) ;
		OK (ok == CAMD_OUT_OF_MEMORY
		    || (sorted ? (ok == CAMD_OK) : (ok >= CAMD_OK))) ;
	    }
	    normal_memory_handler ( ) ;
	    OK (CHOLMOD(print_perm) (P, n, n, "CAMD2 permutation", cm)) ;

	    camd_malloc = cm->malloc_memory ;
	    camd_free = cm->free_memory ;
	}

	CHOLMOD(free_sparse) (&E, cm) ;
    }

    /* ---------------------------------------------------------------------- */
    /* free everything */
    /* ---------------------------------------------------------------------- */

    CHOLMOD(free) (n, sizeof (Int), Len,  cm) ;
    CHOLMOD(free) (n, sizeof (Int), Nv,   cm) ;
    CHOLMOD(free) (n, sizeof (Int), Next, cm) ;
    CHOLMOD(free) (n+1, sizeof (Int), Head, cm) ;
    CHOLMOD(free) (n, sizeof (Int), Elen, cm) ;
    CHOLMOD(free) (n, sizeof (Int), Deg,  cm) ;
    CHOLMOD(free) (n+1, sizeof (Int), Wi, cm) ;
    CHOLMOD(free) (n, sizeof (Int), BucketSet, cm) ;

    CHOLMOD(free) (n+1, sizeof (Int), P, cm) ;
    CHOLMOD(free) (n, sizeof (Int), Constraint, cm) ;

    CHOLMOD(free) (n, sizeof (Int), W, cm) ;
    CHOLMOD(free) (n, sizeof (Int), Flag, cm) ;

    CHOLMOD(free_sparse) (&S, cm) ;
    CHOLMOD(free_sparse) (&B, cm) ;
    CHOLMOD(free_sparse) (&C, cm) ;
    CHOLMOD(free_sparse) (&F, cm) ;
}
Exemplo n.º 26
0
bool resource_request(int customer) {
    /*Add Easy Test to see if what you need is DONE!*/
   /* if((need[customer][0] <= 0 ) && 
       (need[customer][1] <= 0 ) && 
       (need[customer][2] <= 0 )) {
        printf("DONE?");
        //return -1; //can I do this?? 
    }*/

    /*Randomize the request of three resource types*/
    int req_a, req_b, req_c;
    req_a = my_rand(need[customer][0]);
    req_b = my_rand(need[customer][1]);
    req_c = my_rand(need[customer][2]);
    /*If request is more than ANY max limit, exit program as there is an error*/
    if (req_a > max[customer][0] || 
        req_b > max[customer][1] ||
        req_c > max[customer][2]) {

        printf("ERROR! Exceeding max claim:\n");
        printf("Res_A: %d <= %d\n", req_a, max[customer][0]);
        printf("Res_B: %d <= %d\n", req_b, max[customer][1]);
        printf("Res_C: %d <= %d\n", req_c, max[customer][2]);
        //exit(1);
        return false;
    }
    /*Lock teller*/
    pthread_mutex_lock(&teller);
    /*Start buffer printout*/
    printf("At time %d Customer %d requesting <%d,%d,%d> Available = <%d,%d,%d>\n",
            currenttime, customer, 
            req_a, req_b, req_c,
            available[0], available[1], available[2]);

    /*See if what your requesting is available*/
    if (req_a <= available[0] && 
        req_b <= available[1] &&   
        req_c <= available[2]) {
        
        /*Create backup array, incase this array wont work*/
        int backup_available[RESOURCES]; 
        int backup_max[CUSTOMERS][RESOURCES];
        int backup_allocation[CUSTOMERS][RESOURCES];
        int backup_need[CUSTOMERS][RESOURCES];
        
        /*Create backupstate, to revert to if the state is NOT safe*/
        copy_array(available, backup_available);
        copy_array((int *) max, (int *)backup_max);
        copy_array((int *) allocation, (int *) backup_allocation);
        copy_array((int *) need, (int *) backup_need);
        
        /*Now update actual main state, to use is_safe*/
        int i;
        int temparr[3] = {req_a, req_b, req_c}; //put requests in an array 
        /*Iterate through three resource types*/
        for(i=0;i<3;i++){
            available[i] = available[i] - temparr[i];
            allocation[customer][i] = allocation[customer][i] + temparr[i];
            need[customer][i] = need[customer][i] - temparr[i];
        }
        /*Now check if backup state is safe*/
        if(is_safe()) {
            /*Second Buffer Printout*/
            printf(">>>Customer %d is granted resources<<<<\n",customer);
            printf("Available = <%d,%d,%d> Allocated = <%d,%d,%d>\n\n",
                 available[0],available[1],available[2],
                 allocation[customer][0],allocation[customer][1],allocation[customer][2]);
        } else {
            printf("!!!Customer %d is denied resources!!!\n\n",customer);
            copy_array(backup_available, available);
            copy_array((int *) backup_max, (int *)max);
            copy_array((int *) backup_allocation, (int *) allocation);
            copy_array((int *) backup_need, (int *) need);
            /*End Critical Section*/
            pthread_mutex_unlock(&teller);
            return false;
        }
        /*End Critical Section*/
        pthread_mutex_unlock(&teller);
       return true; 
    }else { //not enough resources
        printf("===Not Enough Resources===\n");
        pthread_mutex_unlock(&teller);
        return false;
    }
} 
Exemplo n.º 27
0
inline float	my_rangeRand(float min, float max)
{
	return  ( min + ( (float)my_rand() / D_RAND_MAX * (max - min) ) );
}
Exemplo n.º 28
0
//---------------------------------------------------------------------------
void t_mep_chromosome::mutation(t_mep_parameters *parameters, int *actual_operators, int num_actual_operators, int *actual_variables, int num_actual_variables) // mutate the individual
{

    // mutate each symbol with the same pm probability
    double p = my_rand() / (double)RAND_MAX;
    if (p < parameters->mutation_probability) {
        double sum = parameters->variables_probability + parameters->constants_probability;
        double q = my_rand() / (double)RAND_MAX * sum;

        if (q <= parameters->variables_probability)
            prg[0].op = actual_variables[my_rand() % num_actual_variables];
        else
            prg[0].op = num_total_variables + my_rand() % num_constants;
    }

    for (int i = 1; i < code_length; i++) {
        p = my_rand() / (double)RAND_MAX;      // mutate the operator
        if (p < parameters->mutation_probability) {
            double q = my_rand() / (double)RAND_MAX;

            if (q <= parameters->operators_probability)
                prg[i].op = actual_operators[my_rand() % num_actual_operators];
            else if (q <= parameters->operators_probability + parameters->variables_probability)
                prg[i].op = actual_variables[my_rand() % num_actual_variables];
            else
                prg[i].op = num_total_variables + my_rand() % num_constants;
        }

        p = my_rand() / (double)RAND_MAX;      // mutate the first address  (adr1)
        if (p < parameters->mutation_probability)
            prg[i].adr1 = my_rand() % i;

        p = my_rand() / (double)RAND_MAX;      // mutate the second address   (adr2)
        if (p < parameters->mutation_probability)
            prg[i].adr2 = my_rand() % i;
        p = my_rand() / (double)RAND_MAX;      // mutate the second address   (adr2)
        if (p < parameters->mutation_probability)
            prg[i].adr3 = my_rand() % i;
        p = my_rand() / (double)RAND_MAX;      // mutate the second address   (adr2)
        if (p < parameters->mutation_probability)
            prg[i].adr4 = my_rand() % i;
    }
    // lets see if I can evolve constants

    if (parameters->constants.constants_can_evolve && parameters->constants.constants_type == AUTOMATIC_CONSTANTS)
        for (int c = 0; c < num_constants; c++) {
            p = my_rand() / (double)RAND_MAX;      // mutate the operator
            double tmp_cst_d = my_rand() / double(RAND_MAX) * parameters->constants.constants_mutation_max_deviation;

            if (p < parameters->mutation_probability) {
                if (my_rand() % 2) {// coin
                    if (constants_double[c] + tmp_cst_d <= parameters->constants.max_constants_interval_double)
                        constants_double[c] += tmp_cst_d;
                }
                else if (constants_double[c] - tmp_cst_d >= parameters->constants.min_constants_interval_double)
                    constants_double[c] -= tmp_cst_d;
                break;
            }
        }
}
Exemplo n.º 29
0
int main(int argc, char *argv[])
{
    bert_state_t tx_bert;
    bert_state_t rx_bert;
    bert_state_t bert;
    bert_results_t bert_results;
    int i;
    int bit;
    int zeros;
    int max_zeros;
    int failed;

    bert_init(&tx_bert, 0, BERT_PATTERN_ZEROS, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ZEROS, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);        
    }
    bert_result(&rx_bert, &bert_results);
    printf("Zeros:     Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_ONES, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ONES, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);        
    }
    bert_result(&rx_bert, &bert_results);
    printf("Ones:      Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_1_TO_7, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_1_TO_7, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);
    }
    bert_result(&rx_bert, &bert_results);
    printf("1 to 7:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_1_TO_3, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_1_TO_3, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);        
    }
    bert_result(&rx_bert, &bert_results);
    printf("1 to 3:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_1_TO_1, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_1_TO_1, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);
    }
    bert_result(&rx_bert, &bert_results);
    printf("1 to 1:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_3_TO_1, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_3_TO_1, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);
    }
    bert_result(&rx_bert, &bert_results);
    printf("3 to 1:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }
    
    bert_init(&tx_bert, 0, BERT_PATTERN_7_TO_1, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_7_TO_1, 300, 20);
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);
    }
    bert_result(&rx_bert, &bert_results);
    printf("7 to 1:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O153_9, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O153_9, 300, 20);
    for (i = 0;  i < 0x200;  i++)
        test[i] = 0;
    max_zeros = 0;
    zeros = 0;
    for (i = 0;  i < 511*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        if (bit)
        {
            if (zeros > max_zeros)
                max_zeros = zeros;
            zeros = 0;
        }
        else
        {
            zeros++;
        }
        bert_put_bit(&rx_bert, bit);        
        test[tx_bert.tx_reg]++;
    }
    failed = FALSE;
    if (test[0] != 0)
    {
        printf("XXX %d %d\n", 0, test[0]);
        failed = TRUE;
    }
    for (i = 1;  i < 0x200;  i++)
    {
        if (test[i] != 2)
        {
            printf("XXX %d %d\n", i, test[i]);
            failed = TRUE;
        }
    }
    bert_result(&rx_bert, &bert_results);
    printf("O.153(9):  Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 986  ||  failed)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O152_11, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O152_11, 300, 20);
    for (i = 0;  i < 0x800;  i++)
        test[i] = 0;
    max_zeros = 0;
    zeros = 0;
    for (i = 0;  i < 2047*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        if (bit)
        {
            if (zeros > max_zeros)
                max_zeros = zeros;
            zeros = 0;
        }
        else
        {
            zeros++;
        }
        bert_put_bit(&rx_bert, bit);        
        test[tx_bert.tx_reg]++;
    }
    failed = FALSE;
    if (test[0] != 0)
    {
        printf("XXX %d %d\n", 0, test[0]);
        failed = TRUE;
    }
    for (i = 1;  i < 0x800;  i++)
    {
        if (test[i] != 2)
        {
            printf("XXX %d %d\n", i, test[i]);
            failed = TRUE;
        }
    }
    bert_result(&rx_bert, &bert_results);
    printf("O.152(11): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 4052  ||  failed)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O151_15, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O151_15, 300, 20);
    for (i = 0;  i < 0x8000;  i++)
        test[i] = 0;
    max_zeros = 0;
    zeros = 0;
    for (i = 0;  i < 32767*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        if (bit)
        {
            if (zeros > max_zeros)
                max_zeros = zeros;
            zeros = 0;
        }
        else
        {
            zeros++;
        }
        bert_put_bit(&rx_bert, bit);        
        test[tx_bert.tx_reg]++;
    }
    failed = FALSE;
    if (test[0] != 0)
    {
        printf("XXX %d %d\n", 0, test[0]);
        failed = TRUE;
    }
    for (i = 1;  i < 0x8000;  i++)
    {
        if (test[i] != 2)
        {
            printf("XXX %d %d\n", i, test[i]);
            failed = TRUE;
        }
    }
    bert_result(&rx_bert, &bert_results);
    printf("O.151(15): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 65480  ||  failed)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O151_20, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O151_20, 300, 20);
    for (i = 0;  i < 0x100000;  i++)
        test[i] = 0;    
    max_zeros = 0;
    zeros = 0;
    for (i = 0;  i < 1048575*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        if (bit)
        {
            if (zeros > max_zeros)
                max_zeros = zeros;
            zeros = 0;
        }
        else
        {
            zeros++;
        }
        bert_put_bit(&rx_bert, bit);        
        test[tx_bert.tx_reg]++;
    }
    failed = FALSE;
    if (test[0] != 0)
    {
        printf("XXX %d %d\n", 0, test[0]);
        failed = TRUE;
    }
    for (i = 1;  i < 0x100000;  i++)
    {
        if (test[i] != 2)
            printf("XXX %d %d\n", i, test[i]);
    }
    bert_result(&rx_bert, &bert_results);
    printf("O.151(20): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 2097066  ||  failed)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O151_23, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O151_23, 300, 20);
    for (i = 0;  i < 0x800000;  i++)
        test[i] = 0;    
    max_zeros = 0;
    zeros = 0;
    for (i = 0;  i < 8388607*2;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        if (bit)
        {
            if (zeros > max_zeros)
                max_zeros = zeros;
            zeros = 0;
        }
        else
        {
            zeros++;
        }
        bert_put_bit(&rx_bert, bit);        
        test[tx_bert.tx_reg]++;
    }
    failed = FALSE;
    if (test[0] != 0)
    {
        printf("XXX %d %d\n", 0, test[0]);
        failed = TRUE;
    }
    for (i = 1;  i < 0x800000;  i++)
    {
        if (test[i] != 2)
            printf("XXX %d %d\n", i, test[i]);
    }
    bert_result(&rx_bert, &bert_results);
    printf("O.151(23): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 16777136  ||  failed)
    {
        printf("Test failed.\n");
        exit(2);
    }

    bert_init(&tx_bert, 0, BERT_PATTERN_QBF, 300, 20);
    bert_init(&rx_bert, 0, BERT_PATTERN_QBF, 300, 20);
    for (i = 0;  i < 100000;  i++)
    {
        bit = bert_get_bit(&tx_bert);
        bert_put_bit(&rx_bert, bit);        
    }
    bert_result(&rx_bert, &bert_results);
    printf("QBF:       Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
    if (bert_results.bad_bits  ||  bert_results.total_bits != 100000)
    {
        printf("Test failed.\n");
        exit(2);
    }

    /* Test the mechanism for categorising the error rate into <10^x bands */
    /* TODO: The result of this test is not checked automatically */
    bert_init(&bert, 15000000, BERT_PATTERN_ITU_O152_11, 300, 20);
    bert_set_report(&bert, 100000, reporter, (intptr_t) 0);
    for (;;)
    {
        if ((bit = bert_get_bit(&bert)) == PUTBIT_END_OF_DATA)
        {
            bert_result(&bert, &bert_results);
            printf("Rate test: %d bits, %d bad bits, %d resyncs\n", bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
            if (bert_results.total_bits != 15000000 - 42
                ||
                bert_results.bad_bits != 58
                ||
                bert_results.resyncs != 0)
            {
                printf("Tests failed\n");
                exit(2);
            }
            break;
            //bert_init(&bert, 15000000, BERT_PATTERN_ITU_O152_11, 300, 20);
            //bert_set_report(&bert, 100000, reporter, (intptr_t) 0);
            //continue;
        }
        if ((my_rand() & 0x3FFFF) == 0)
            bit ^= 1;
        //if ((my_rand() & 0xFFF) == 0)
        //    bert_put_bit(&bert, bit);
        bert_put_bit(&bert, bit);
    }
    
    printf("Tests passed.\n");
    return  0;
}
Exemplo n.º 30
0
double my_drand(unsigned* seed_p)
{
unsigned x = my_rand(seed_p);
double y = x/MR_DIVISOR;
return y;
}