Esempio n. 1
0
File: setpw.c Progetto: jdreed/moira
void generate_password(char *password)
{
  int   i;
  int   j;
  int   row_position;
  int   nchars;
  int   position;
  int   word;
  int   line;
  char  *pwp;

  for (line = 22; line; --line)
    {
      for (word = 7; word; --word)
        {
          position = myrandom()%total_sum;
          for(row_position = 0, j = 0; position >= row_position; row_position += start_freq[j], j++)
            continue;
          *(pwp = password) = j + 'a' - 1;
          for (nchars = PW_LENGTH-1; nchars; --nchars)
            {
              i = *pwp - 'a';
              pwp++;
              position = myrandom()%row_sums[i];
              for (row_position = 0, j = 0; position >= row_position; row_position += frequency[i][j], j++)
                continue;
              *pwp = j + 'a' - 1;
            }
          *(++pwp)='\0';
          return;
        }
      putchar('\n');
    }
}
Esempio n. 2
0
void init_model(char* output_dir, int num_words, int num_labels, int num_topics){
    char log_theta_file[1000];
    char log_phi_file[1000];
    char pi_file[1000];
    sprintf(log_theta_file, "%s/init.theta", output_dir);
    sprintf(log_phi_file, "%s/init.phi", output_dir);
    sprintf(pi_file, "%s/init.pi", output_dir);
    double* log_theta = (double*) calloc(num_labels * num_topics, sizeof(double));
    double* log_phi = (double*) calloc(num_topics * num_words, sizeof(double));
    double* pi = (double*) calloc(num_labels, sizeof(double));
    for (int i = 0; i < num_labels; i++) {
        pi[i] = myrandom() * 0.5 + 1;
        double temp = 0;
        for (int k = 0; k < num_topics; k++) {
            double v = myrandom();
            temp += v;
            log_theta[i * num_topics + k] = v;
        }
        for (int k = 0; k < num_topics; k++)log_theta[i*num_topics + k] = log(log_theta[i*num_topics + k] / temp);
    }
    for (int k = 0; k < num_topics; k++) {
        for (int i = 0; i < num_words; i++)log_phi[k*num_words + i] = log(1.0/num_words);
    }
    print_mat(log_theta, num_labels, num_topics, log_theta_file);
    print_mat(log_phi, num_topics, num_words, log_phi_file);
    print_mat(pi, num_labels, 1, pi_file);
    char info_file[1000];
    sprintf(info_file, "%s/info.txt", output_dir);
    FILE* info_fp = fopen(info_file,"w");
    fprintf(info_fp, "num_labels: %d\nnum_words: %d\nnum_topics: %d\n", num_labels, num_words, num_topics);
    fclose(info_fp);
    free(log_theta);
    free(pi);
    free(log_phi);
}
Esempio n. 3
0
double
random_normal(int sn, double m, double s)
/* mean and standard deviation */
{
    double        v1, v2, w, z1;
    static double z2 = 0.0;

    if (z2 != 0.0) {
        z1 = z2;
        z2 = 0.0;
    }

    else {
        do {
            v1 = 2.0 * myrandom(sn) - 1.0;
            v2 = 2.0 * myrandom(sn) - 1.0;
            w = v1 * v1 + v2 * v2;
        } while (w >= 1.0);

        w = sqrt((-2.0 * log(w)) / w);
        z1 = v1 * w;
        z2 = v2 * w;
    }

    return(m + z1 * s);
}
Esempio n. 4
0
void SAstoroidBelt::proces(uint32_t delta, Processor* processor ){
	for(list<SAstoroidBeltRoid*>::iterator it = _roids.begin(); it != _roids.end(); it++){
		if((*it)->_roid == NULL && (*it)->_counter == 0 ){
			SPos temppos(myrandom(_pos.x-_size,_pos.x+_size),myrandom(_pos.y-_size,_pos.y+_size),0);
			temppos.grid = world->getGrids().begin()->second;
			(*it)->_roid = processor->createAsteroid(temppos, *(*it)->_type, this);
			//world->getGrids()[1]->addAstoroid((*it)->_roid);
		}else if((*it)->_roid == NULL && (*it)->_counter ){
			(*it)->_counter--;
		}
	}
}
void RandomNumberGrabber::PrintShit() {
    cout << "The fifth number of this random shit is: " << random_numbers[4] << endl;
    for (int i = 0; i < total_numbers + 5; i++)
    {
        cout << myrandom(100) << endl; 
    }
}
Esempio n. 6
0
int main(int argc, char **argv)
{
	int nfaces;
	int i, j, k;

	if (argc != 2) {
		fprintf(stderr, "usage: %s number-die-faces\n", argv[0]);
		exit(1);
	}

	nfaces = atoi(argv[1]);

	if (nfaces <= 0) {
		fprintf(stderr, "usage: %s number-die-faces\n", argv[0]);
		fprintf(stderr, "\tUse a positive number!\n");
		exit(1);
	}

	for (i = 1; i <= nfaces; i++) {
		j = myrandom() % 6;		/* force to range 0 <= j <= 5 */
		printf("+-------+\n");
		for (k = 0; k < 3; k++)
			printf("|%s|\n", die_faces[(j * 3) + k]);
		printf("+-------+\n");
		putchar('\n');
	}

	return 0;
}
Esempio n. 7
0
int mmu_NRU::replacepage()
{
	int index=0;
	int size=0;
   
	//if(test==9)
	//{
		//printf("%d,%d,%d,%d\n",class1.size(),class2.size(),class3.size(),class4.size());
	//}

	if(class1.size())
	{
		//vector<string>::iterator iter;
		sort(class1.begin(),class1.end());
		size=class1.size();
		index=class1[myrandom(size)];
		int pf_index=find_index(index);
		return pf_index;
	}
	if(class2.size())
	{
		sort(class2.begin(),class2.end());
		size=class2.size();
		index=class2[myrandom(size)];
		int pf_index=find_index(index);
		return pf_index;
	}
	if(class3.size())
	{
		sort(class3.begin(),class3.end());
		size=class3.size();
		index=class3[myrandom(size)];
		int pf_index=find_index(index);
	  return pf_index;
	}
	if(class4.size())
	{
		sort(class4.begin(),class4.end());
		size=class4.size();
		index=class4[myrandom(size)];
		int pf_index=find_index(index);
	  return pf_index;
	}

	printf("error!\n");
	exit(-1);
}
Esempio n. 8
0
double
random_uniform_real(int sn, double a, double b)
/* stream number */
/* lower and upper bounds */
{
    if (a > b)
        printf("uniform-real argument error: a > b");
    return(a + (b - a) * myrandom(sn));
}
Esempio n. 9
0
int node(int value,int fa)
{
    tot++;
    l[tot]=r[tot]=0;
    s[tot]=1;
    f[tot]=fa;
    v[tot]=value;
    c[tot]=myrandom();
    return(tot);
}
Esempio n. 10
0
/// Setup node names, with nodes[0]=node1 and nodes[1]=node2
vector<int> get_nodes_branch_random(const TreeInterface& t,int node1,int node2) {

    vector<int> nodes = get_nodes_branch(t, node1, node2);

    // randomize the order here
    if (myrandom(2) == 1)
        std::swap(nodes[2],nodes[3]);

    return nodes;
}
Esempio n. 11
0
static void
DoS_icmp (void)
{  
	struct sockaddr_in to;  
	struct ip *iph;  
	struct icmp *icmph;  
	char *packet;  
	int pktsize = sizeof (struct ip) + sizeof (struct icmp) + 64;  
	packet = malloc (pktsize);  
	iph = (struct ip *) packet;  
	icmph = (struct icmp *) (packet + sizeof (struct ip));  
	memset (packet, 0, pktsize);  
	
	/* IP的版本,IPv4 */
	iph->ip_v = 4;  
	/* IP头部长度,字节数 */
	iph->ip_hl = 5;  
	/* 服务类型 */
	iph->ip_tos = 0;  
	/* IP报文的总长度 */
	iph->ip_len = htons (pktsize);  
	/* 标识,设置为PID */
	iph->ip_id = htons (getpid ());  
	/* 段的便宜地址 */
	iph->ip_off = 0; 
	/* TTL */
	iph->ip_ttl = 0x0;  
	/* 协议类型 */
	iph->ip_p = PROTO_ICMP;  
	/* 校验和,先填写为0 */
	iph->ip_sum = 0;  
	/* 发送的源地址 */
	iph->ip_src = (unsigned long) myrandom(0, 65535); 
	/* 发送目标地址 */
	iph->ip_dst = dest;    
  

	/* ICMP类型为回显请求 */
	icmph->icmp_type = ICMP_ECHO;  
	/* 代码为0 */
	icmph->icmp_code = 0;  
	/* 由于数据部分为0,并且代码为0,直接对不为0即icmp_type部分计算 */
	icmph->icmp_sum = htons(~(ICMP_ECHO << 8));  

	/* 填写发送目的地址部分 */
	to.sin_family =  AF_INET;  
	to.sin_addr.s_addr = iph->ip_dst;
	to.sin_port = htons(0);

	/* 发送数据 */
	sendto (rawsock, packet, pktsize, 0, (struct sockaddr *) &to, sizeof (struct sockaddr));  
	/* 释放内存 */
	free (packet);
}
Esempio n. 12
0
///
//  Update time-based variables
//
void Update ( ESContext *esContext, float deltaTime )
{
   UserData *userData = esContext->userData;
  
   userData->time += deltaTime;

   if ( userData->time >= 1.0f )
   {
      float centerPos[3];
      float color[4];

      userData->time = 0.0f;

      // Pick a new start location and color
      centerPos[0] = myrandom() - 0.5f;
      centerPos[1] = myrandom() - 0.5f;
      centerPos[2] = myrandom() - 0.5f;
      
      glUniform3fv ( userData->centerPositionLoc, 1, &centerPos[0] );

      // Random color
      color[0] = myrandom() * 0.5 + 0.5f;
      color[1] = myrandom() * 0.5 + 0.5f;
      color[2] = myrandom() * 0.5 + 0.5f;
      color[3] = 1.0;

      glUniform4fv ( userData->colorLoc, 1, &color[0] );
   }

   // Load uniform time variable
   glUniform1f ( userData->timeLoc, userData->time );
}
Esempio n. 13
0
int
random_uniform_int(int sn, int a, int b)
/* lower and upper bounds, inclusively */
{
    double b2;

    if (a > b)
        printf("uniform-int argument error: a = %d > b = %d\n", a, b);
    b2 = (double)b - a + 1.0;
    b2 = (double) b2 * myrandom(sn) - 0.5;

    return(a + rint(b2));
}
Esempio n. 14
0
uint32_t CommandQueryCmp::execute() {
	Processable* temp = _processor->getLocalProcssables()[_procesable];
	
	if(!temp )
		return COMMAND_FINAL;
	
	if(temp->isProgrammable())
	{
		temp->isProgrammable()->interrupt(_callbackProgramID,_callbackHandler,_payload, sizeof(uint32_t)*2);
	}
	_time = world->getTime() + myrandom(1500,6000);
	return COMMAND_REPEAT;
}
Esempio n. 15
0
long
random_poisson(int sn, int a)
/* average arrivals in unit time */
{
    long  n = -1;
    double e, p = 1.0;

    e = exp((double) -a);
    do {
        p *= myrandom(sn);
        n++;
    } while (p >= e);
    return(n);
}
Esempio n. 16
0
int main(int argc, char **argv) {
  double baseref[120];
  double testref[120];
  unsigned long long myseed = 123456789, setseed;
  int i, j;

  setseed=myseed;
  seed(setseed, 0.0, 1.1);

  for (i=0; i<120; i++) {
    baseref[i] = myrandom();
  }

  for (j=0; j<6; j++) {
    int numthreads = tests[j];
    double sum;

    omp_set_num_threads(numthreads);
    sum = 0;

#pragma omp parallel reduction(+:sum)
    {
      setseed=myseed;
      seed(setseed,0.0,1.0);

#pragma omp for
      for (i=0; i<120; i++) {
        sum += abs(myrandom() - baseref[i]);
      }
    }    
    printf(" Diff for %i threads is %f.\n", numthreads, sum);

  }

  printf("\n");
}
Esempio n. 17
0
/* Between each WIDTH consecutive characters, the function prints a newline */
static void random_fasta (aminoacid_t const *genelist, size_t count) {
    do {
	size_t line = MIN(WIDTH, count);
	size_t pos = 0;
	char buf[WIDTH + 1];
	do {
	    float r = myrandom (1.0);
	    size_t i = 0;
	    while (genelist[i].p < r)
		++i; /* Linear search */
	    buf[pos++] = genelist[i].c;
	} while (pos < line);
	buf[line] = '\n';
	fwrite (buf, 1, line + 1, stdout);
	count -= line;
    } while (count);
}
Esempio n. 18
0
double
random_erlang(int sn, double m, double s)
/* mean and standard deviation */
{
    int   i, k;
    double z;

    if (s > m)
        printf("erlang argument error: s > m");

    z = m / s;
    k = (int) z * z;
    z = 1.0;
    for (i = 0; i < k; i++)
        z *= myrandom(sn);

    return(-(m/k) * log(z));
}
Esempio n. 19
0
double
random_exp_real(int sn, double m)
/* mean value */
{
    return(-m * log(myrandom(sn)));
}
Esempio n. 20
0
int mmu_NRU::locatepage()
{

	//test if there is free physical frame
	for(int i=0;i<pp_number;i++)
	{
		if(frametable[i]==-1)
		{
			//deference
			if(mmutype!='N')
			{
			  alicia->init(i);
			}
			return i;
		}
	}

	//default is NRU, if type is not NRU, use alicia to switch our algorithm
	//deference
	if(mmutype!='N')
	{
		global_random=myrandom(pp_number);
		//PTE *newpte=new PTE[64];
		for(int k=0;k<64;k++)
		{
			//printf("enter\n");
			newpte[k].f1=pte[k].f1;
			newpte[k].f2=pte[k].f2;
			newpte[k].f3=pte[k].f3;
			newpte[k].f4=pte[k].f4;
			newpte[k].f5=pte[k].f5;
		}
		//memcpy(newpte,pte,64*sizeof(PTE));
		int index= alicia->Process(pte,frametable);

	  for(int k=0;k<64;k++)
		{
			//printf("enter\n");
			pte[k].f1=newpte[k].f1;
			pte[k].f2=newpte[k].f2;
			pte[k].f3=newpte[k].f3;
			pte[k].f4=newpte[k].f4;
			pte[k].f5=newpte[k].f5;
		}

		return index;

	}

	class1.clear();
  class2.clear();	
	class3.clear();
	class4.clear();

	for(int i=0;i<pp_number;i++)
	{

		if((pte[frametable[i]].f2==0) &&(pte[frametable[i]].f3==0))
			class1.push_back(frametable[i]);

		if((pte[frametable[i]].f2==1) &&(pte[frametable[i]].f3==0))
			class2.push_back(frametable[i]);

		if((pte[frametable[i]].f2==0) &&(pte[frametable[i]].f3==1))
			class3.push_back(frametable[i]);

		if((pte[frametable[i]].f2==1) &&(pte[frametable[i]].f3==1))
			class4.push_back(frametable[i]);
	}

	cycle+=1;
	if(cycle==10)
		resetcycle();

  return replacepage();
}
Esempio n. 21
0
void example2(void)
{
   Int i, n;
   Addr a;
   Block* vs[NN];
   Block v, prev;
   Block *pv;

   
   
   OSet* oset = VG_(OSetGen_Create)(offsetof(Block, first),
                                    blockCmp,
                                    allocate_node, "oset_test.3", free_node);

   
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   
   
   for (i = 0; i < NN; i++) {
      vs[i] = VG_(OSetGen_AllocNode)(oset, sizeof(Block));
      vs[i]->b1    = i;
      vs[i]->first = i*10 + 1;
      vs[i]->last  = vs[i]->first + 2;
      vs[i]->b2    = i+1;
   }
   seed = 0;
   for (i = 0; i < NN; i++) {
      Int r1  = myrandom() % NN;
      Int r2  = myrandom() % NN;
      Block* tmp = vs[r1];
      vs[r1]  = vs[r2];
      vs[r2]  = tmp;
   }

   
   for (i = 0; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   
   vg_assert( NN == VG_(OSetGen_Size)(oset) );

   
   
   for (i = 0; i < NN; i++) {
      a = vs[i]->first + 0;    assert( VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 1;    assert( VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 2;    assert( VG_(OSetGen_Contains)(oset, &a) );
   }

   
   
   a = 0;
   assert( ! VG_(OSetGen_Contains)(oset, &a) );
   for (i = 0; i < NN; i++) {
      a = vs[i]->first - 1;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 3;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
   }

   
   
   for (i = 0; i < NN; i++) {
      a = vs[i]->first + 0;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 1;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 2;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      assert( vs[i] == VG_(OSetGen_LookupWithCmp)(oset, &a, blockCmp) );
   }

   
   
   n = 0;
   pv = NULL;
   prev.last = 0;
   VG_(OSetGen_ResetIter)(oset);
   while ( (pv = VG_(OSetGen_Next)(oset)) ) {
      Block curr = *pv;
      assert(prev.last < curr.first); 
      prev = curr;
      n++;
   }
   assert(NN == n);
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );

   
   
   for (i = 0; i < NN; i += 2) {
      a = vs[i]->first;    assert( vs[i] == VG_(OSetGen_Remove)(oset, &a) );
   }

   
   vg_assert( NN/2 == VG_(OSetGen_Size)(oset) );

   
   for (i = 1; i < NN; i += 2) {
      a = vs[i]->first + 0;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 1;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 2;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
   }

   
   for (i = 0; i < NN; i += 2) {
      a = vs[i]->first + 0;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 1;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 2;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
   }

   
   
   for (i = 1; i < NN; i += 2) {
      a = vs[i]->first;    assert( vs[i] == VG_(OSetGen_Remove)(oset, &a) );
   }

   
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   
   for (i = 0; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   
   VG_(OSetGen_Destroy)(oset);
}
Esempio n. 22
0
///
// Initialize the shader and program object
//
int Init ( ESContext *esContext )
{
   UserData *userData = esContext->userData;
   int i;
   
   GLbyte vShaderStr[] =
      "uniform float u_time;		                           \n"
      "uniform vec3 u_centerPosition;                       \n"
      "attribute float a_lifetime;                          \n"
      "attribute vec3 a_startPosition;                      \n"
      "attribute vec3 a_endPosition;                        \n"
      "varying float v_lifetime;                            \n"
      "void main()                                          \n"
      "{                                                    \n"
      "  if ( u_time <= a_lifetime )                        \n"
      "  {                                                  \n"
      "    gl_Position.xyz = a_startPosition +              \n"
      "                      (u_time * a_endPosition);      \n"
      "    gl_Position.xyz += u_centerPosition;             \n"
      "    gl_Position.w = 1.0;                             \n"
      "  }                                                  \n"
      "  else                                               \n"
      "     gl_Position = vec4( -1000, -1000, 0, 0 );       \n"
      "  v_lifetime = 1.0 - ( u_time / a_lifetime );        \n"
      "  v_lifetime = clamp ( v_lifetime, 0.0, 1.0 );       \n"
      "  gl_PointSize = ( v_lifetime * v_lifetime ) * 40.0; \n"
      "}";
      
   GLbyte fShaderStr[] =  
      "precision mediump float;                             \n"
      "uniform vec4 u_color;		                           \n"
      "varying float v_lifetime;                            \n"
      "uniform sampler2D s_texture;                         \n"
      "void main()                                          \n"
      "{                                                    \n"
      "  vec4 texColor;                                     \n"
      "  texColor = texture2D( s_texture, gl_PointCoord );  \n"
      "  gl_FragColor = vec4( u_color ) * texColor;         \n"
      "  gl_FragColor.a *= v_lifetime;                      \n"
      "}                                                    \n";

   // Load the shaders and get a linked program object
   userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );

   // Get the attribute locations
   userData->lifetimeLoc = glGetAttribLocation ( userData->programObject, "a_lifetime" );
   userData->startPositionLoc = glGetAttribLocation ( userData->programObject, "a_startPosition" );
   userData->endPositionLoc = glGetAttribLocation ( userData->programObject, "a_endPosition" );
   
   // Get the uniform locations
   userData->timeLoc = glGetUniformLocation ( userData->programObject, "u_time" );
   userData->centerPositionLoc = glGetUniformLocation ( userData->programObject, "u_centerPosition" );
   userData->colorLoc = glGetUniformLocation ( userData->programObject, "u_color" );
   userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );

   glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );

   // Fill in particle data array
   srand ( 0 );
   for ( i = 0; i < NUM_PARTICLES; i++ )
   {
      float *particleData = &userData->particleData[i * PARTICLE_SIZE];
   
      // Lifetime of particle
      (*particleData++) = myrandom();

      // End position of particle
      (*particleData++) = myrandom() * 2 - 1.0f;
      (*particleData++) = myrandom() * 2 - 1.0f;
      (*particleData++) = myrandom() * 2 - 1.0f;

      // Start position of particle
      (*particleData++) = myrandom() * 0.25 - 0.125f;
      (*particleData++) = myrandom() * 0.25 - 0.125f;
      (*particleData++) = myrandom() * 0.25 - 0.125f;

   }

   // Initialize time to cause reset on first update
   userData->time = 1.0f;

   userData->textureId = LoadTexture ( "smoke.tga" );
   if ( userData->textureId <= 0 )
   {
      return FALSE;
   }
   
   return TRUE;
}
Esempio n. 23
0
static double runtest()
{
	unsigned int seed=1;
	int n, i;
	double opspersec=0;
	THREADVAR threads[THREADS];
	for(n=0; n<THREADS; n++)
	{
		unsigned int *toallocptr;
		int m;
		threadstuff[n].ops=0;
		times[n]=0;
		threadstuff[n].toalloc=toallocptr=calloc(RECORDS, sizeof(unsigned int));
		threadstuff[n].allocs=calloc(RECORDS, sizeof(void *));
		for(m=0; m<RECORDS; m++)
		{
			unsigned int size=myrandom(&seed);
			if(size<(1<<30))
			{   /* Make it two power multiple of less than 512 bytes to
				model frequent C++ new's */
				size=4<<(size & 7);
			}
			else
			{
				size&=0x3FFF;             /* < 16Kb */
				/*size&=0x1FFF;*/			  /* < 8Kb */
				/*size=(1<<6)<<(size & 7);*/  /* < 8Kb */
			}
			*toallocptr++=size;
		}
	}
#ifdef TORTURETEST
	for(n=0; n<THREADS; n++)
	{
		THREADINIT(&threads[n], n);
	}
	for(i=0; i<32; i++)
	{
		int found=-1;
		do
		{
			for(n=0; n<THREADS; n++)
			{
				THREADSLEEP(100);
				if(threadstuff[n].done)
				{
					found=n;
					break;
				}
			}
		} while(found<0);
		THREADWAIT(threads[found]);
		threads[found]=0;
		THREADINIT(&threads[found], found);
		printf("Relaunched thread %d\n", found);
	}
	for(n=THREADS-1; n>=0; n--)
	{
		THREADWAIT(threads[n]);
		threads[n]=0;
	}
#else
#if 1
	for(n=0; n<THREADS; n++)
	{
		THREADINIT(&threads[n], n);
	}
	for(n=THREADS-1; n>=0; n--)
	{
		THREADWAIT(threads[n]);
		threads[n]=0;
	}
#else
	/* Quick realloc() test */
	doRealloc=1;
	for(n=0; n<THREADS; n++)
	{
		THREADINIT(&threads[n], n);
	}
	for(n=THREADS-1; n>=0; n--)
	{
		THREADWAIT(threads[n]);
		threads[n]=0;
	}
#endif
#endif
	{
		usCount totaltime=0;
		int totalops=0;
		for(n=0; n<THREADS; n++)
		{
			totaltime+=times[n];
			totalops+=threadstuff[n].ops;
		}
		opspersec=1000000000000.0*totalops/totaltime*THREADS;
		printf("This allocator achieves %lfops/sec under %d threads\n", opspersec, THREADS);
	}
	for(n=THREADS-1; n>=0; n--)
	{
		free(threadstuff[n].allocs); threadstuff[n].allocs=0;
		free(threadstuff[n].toalloc); threadstuff[n].toalloc=0;
	}
	return opspersec;
}
Esempio n. 24
0
void example1singleset(OSet* oset, char *descr)
{
   Int  i, n;
   Word v, prev;
   Word* vs[NN];
   Word *pv;

   
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   
   
   for (i = 0; i < NN; i++) {
      vs[i] = VG_(OSetGen_AllocNode)(oset, sizeof(Word));
      *(vs[i]) = 2*i;
   }
   seed = 0;
   for (i = 0; i < NN; i++) {
      Word r1  = myrandom() % NN;
      Word r2  = myrandom() % NN;
      Word* tmp= vs[r1];
      vs[r1]   = vs[r2];
      vs[r2]   = tmp;
   }

   
   for (i = 0; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   
   vg_assert( NN == VG_(OSetGen_Size)(oset) );

   
   for (i = 0; i < NN; i++) {
      assert( VG_(OSetGen_Contains)(oset, vs[i]) );
   }

   
   
   v = -1;
   assert( ! VG_(OSetGen_Contains)(oset, &v) );
   for (i = 0; i < NN; i++) {
      v = *(vs[i]) + 1;
      assert( ! VG_(OSetGen_Contains)(oset, &v) );
   }
   v = NN*2;
   assert( ! VG_(OSetGen_Contains)(oset, &v) );

   
   
   for (i = 0; i < NN; i++) {
      assert( vs[i] == VG_(OSetGen_Lookup)(oset, vs[i]) );
   }

   
   
   n = 0;
   pv = NULL;
   prev = -1;
   VG_(OSetGen_ResetIter)(oset);
   while ( (pv = VG_(OSetGen_Next)(oset)) ) {
      Word curr = *pv;
      assert(prev < curr); 
      prev = curr;
      n++;
   }
   assert(NN == n);
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );

   
   
   for (i = 0; i < NN; i += 2) {
      assert( pv = VG_(OSetGen_Remove)(oset, vs[i]) );
      assert( pv == vs[i] );
   }

   
   vg_assert( NN/2 == VG_(OSetGen_Size)(oset) );

   
   for (i = 1; i < NN; i += 2) {
      assert( pv = VG_(OSetGen_LookupWithCmp)(oset, vs[i], NULL) );
      assert( pv == vs[i] );
   }

   
   for (i = 0; i < NN; i += 2) {
      assert( ! VG_(OSetGen_Contains)(oset, vs[i]) );
   }

   
   
   for (i = 1; i < NN; i += 2) {
      assert( pv = VG_(OSetGen_Remove)(oset, vs[i]) );
      assert( pv == vs[i] );
   }

   
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   
   VG_(OSetGen_FreeNode)(oset, vs[0]);
   VG_(OSetGen_FreeNode)(oset, vs[1]);
   VG_(OSetGen_FreeNode)(oset, vs[2]);

   
   
   for (i = 3; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   
   OSet_Print(oset, descr, wordToStr);

}
Esempio n. 25
0
static void threadcode(int threadidx)
{
	int n;
	unsigned int *toallocptr=threadstuff[threadidx].toalloc;
	void **allocptr=threadstuff[threadidx].allocs;
	unsigned int seed=threadidx;
	usCount start;
	threadstuff[threadidx].done=0;
	/*neddisablethreadcache(0);*/
	THREADSLEEP(100);
	start=GetUsCount();
#ifdef TORTURETEST
	/* A randomised malloc/realloc/free test (torture test) */
	for(n=0; n<RECORDS*100; n++)
	{
		unsigned int r=myrandom(&seed), i;
		i=(int)(r % RECORDS);
		if(!allocptr[i])
		{
			allocptr[i]=mallocs[whichmalloc](r & 0x1FFF);
			threadstuff[threadidx].ops++;
		}
		else if(r & (1<<31))
		{
			allocptr[i]=reallocs[whichmalloc](allocptr[i], r & 0x1FFF);
			threadstuff[threadidx].ops++;
		}
		else
		{
			frees[whichmalloc](allocptr[i]);
			allocptr[i]=0;
		}
	}
	for(n=0; n<RECORDS; n++)
	{
		if(allocptr[n])
		{
			frees[whichmalloc](allocptr[n]);
			allocptr[n]=0;
		}
	}
#else
	/* A simple stack which allocates and deallocates off the top (speed test) */
	for(n=0; n<RECORDS;)
	{
#if 1
		r=myrandom(&seed);
		if(allocptr>threadstuff[threadidx].allocs && (r & 65535)<32760) /*<32760)*/
		{	/* free */
			--toallocptr;
			--allocptr;
			--n;
			frees[whichmalloc](*allocptr);
			*allocptr=0;
		}
		else
#endif
		{
			if(doRealloc && allocptr>threadstuff[threadidx].allocs && (r & 1))
			{
	            allocptr[-1]=reallocs[whichmalloc](allocptr[-1], *toallocptr);
			}
			else
			{
	            allocptr[0]=mallocs[whichmalloc](*toallocptr);
				allocptr++;
			}
			n++;
			toallocptr++;
			threadstuff[threadidx].ops++;
		}
	}
	while(allocptr>threadstuff[threadidx].allocs)
	{
		frees[whichmalloc](*--allocptr);
	}
#endif
	times[threadidx]+=GetUsCount()-start;
	neddisablethreadcache(0);
	threadstuff[threadidx].done=1;
}
Esempio n. 26
0
int main(int argc, char *argv[]) {
  int pid=(int)getpid();

  int i,j,s;
  unsigned long randseed;
  int howmany, totalallocs=0;
  size_t size;
  u_long mach,alloc;

  char myhostname[DSM_NAME_LENGTH];
  u_long *my_addresses;
  u_long my_address;
  int my_num_addresses;

  char *p, host[DSM_NAME_LENGTH], allocname[DSM_NAME_LENGTH];
  char *progname;
  
  struct alloc_list_head *alhp=NULL;
  struct class_entry     *clp=NULL;
  struct class_share     *csp=NULL;
  u_long nmachines,nclasses,nclassshares,version;

  int readwait;

  time_t t;

  int is_structure;

#define NSTRUCT 100
  dsm_structure ds[NSTRUCT];

  struct timeval now;
  struct timezone dummy;

  char allocfile[256];

  /*********/
  /* Begin */
  /*********/

  if(argc>1 && argv[1][0]=='-' && argv[1][1]=='v') verbose=DSM_TRUE;

  progname = strrchr(argv[0], '/');

  if(progname == (char *)NULL) progname  = argv[0];
  else                         progname += 1;

  for(i=0; i<NSTRUCT; i++) ds[i].name[0] = '\0';

  /*************************************************/
  /* now get the allocation table and my host info */
  /*************************************************/
  s = dsm_determine_network_info(&my_addresses, &my_num_addresses, myhostname);
  if(s != DSM_SUCCESS) {
    fprintf(stderr,
	    "Failed to determine our network information\n");
    exit(DSM_ERROR);
  }


  allocfile[0] = '\0';
  dsm_determine_alloc_file_location(allocfile);
  if(dsm_read_allocation_file(&alhp, &nmachines,
			      &clp,  &nclasses,
			      &csp,  &nclassshares,
			      &version,
			      my_addresses,
			      my_num_addresses,
			      &my_address,
			      allocfile)
     != DSM_SUCCESS) {
    fprintf(stderr, "Couldn't read allocation file\n");
    exit(DSM_ERROR);
  }

  if(alhp == NULL) {
    printf("No allocations for my host address 0x%lx\n",my_address);
    exit(DSM_ERROR);
  }

  printf("Read allocations for %ld machines\n", nmachines);
  
  totalallocs=0;
  for(i=0; i<nmachines; i++) {
    if(verbose)
      printf("Host #%d %s, %d allocs\n", 
	     i, alhp[i].machine_name, alhp[i].nallocs);
    
    for(j=0; j<alhp[i].nallocs; j++) {
      if(verbose) printf("  alloc #%d size %3d  %s\n",
			 j,
			 alhp[i].allocs[j].size,
			 alhp[i].allocs[j].name);
      totalallocs++;
    }
  }



  /************************/
  /* start dsm operations */
  /************************/
  if( (s=dsm_open())!=DSM_SUCCESS) {
    dsm_error_message(s, "dsm_open()");
    exit(DSM_ERROR);
  }

  randseed = (unsigned long)time(NULL)/* + (unsigned long)pid*/;

  while(1) {
    if(myrandom(&randseed) & 0x2000UL) readwait = DSM_TRUE;
    else                               readwait = DSM_FALSE;

    /* how many allocations do we wait on? */
    if(readwait) {
      howmany = 1 +
	(int)
	((double)totalallocs * ((double)myrandom(&randseed) / (double)UINT_MAX));

      if(howmany>NSTRUCT) howmany = NSTRUCT;
      printf("%s[%d] will monitor %d allocations\n", progname, pid, howmany);
    }
    else {
      howmany = 1;
    }


    size = 0;
    for(i=0; i<howmany; i++) {
      dprintf("%s[%d]: looping to select howmany=%d allocs; doing i=%d\n",
	      progname, pid, howmany, i);

      /* pick one at random */
      mach  = (int)((double)nmachines * myrandom(&randseed) / UINT_MAX);
      alloc = (int)((double)alhp[mach].nallocs * myrandom(&randseed)
		    / UINT_MAX);

      is_structure = alhp[mach].allocs[alloc].is_structure;

      if(is_structure == DSM_TRUE) {
	/* it's a structure; initialize a structure object */
	dprintf("about to call dsm_struct_init(&ds[%d], %s)\n",
		i, alhp[mach].allocs[alloc].name);
	s = dsm_structure_init(&ds[i], alhp[mach].allocs[alloc].name);
	if(s!=DSM_SUCCESS) {
	  dsm_error_message(s, "dsm_structure_init");
	  fprintf(stderr,
		  "can't initialize dsm_struct for %s; skipping\n",
		  alhp[mach].allocs[alloc].name);
	  continue;
	}
	
	size = sizeof(dsm_structure *) > size ? sizeof(dsm_structure *) : size;
      }
      else {
	size =
	  alhp[mach].allocs[alloc].size > size 
	  ?  alhp[mach].allocs[alloc].size
	  :  size;
      }

      if(readwait) {
	dprintf("%s[%d]: mach = %d/%d alloc=%d/%d size=%d\n",
		progname, pid,
		mach, nmachines,
		alloc, alhp[mach].nallocs,
		size);

	dprintf("%s[%d]: calling dsm_monitor for \"%s\":\"%s\"\n",
		progname,pid,
		alhp[mach].machine_name,alhp[mach].allocs[alloc].name);

	if(is_structure==DSM_TRUE) {
	  s = dsm_monitor(alhp[mach].machine_name,
			  alhp[mach].allocs[alloc].name,
			  &ds[i]);
	}
	else {
	  s = dsm_monitor(alhp[mach].machine_name,
			  alhp[mach].allocs[alloc].name);
	}
	if(s!=DSM_SUCCESS) {
	  if(s==DSM_IN_MON_LIST) {
	    if(is_structure==DSM_TRUE) {
	      dsm_structure_destroy(&ds[i]);
	      ds[i].name[0]='\0';
	    }
	    --i;
	    continue;
	  }
	  else {
	    fprintf(stderr, "error: alloc %s/%s\n",
		    alhp[mach].machine_name,alhp[mach].allocs[alloc].name);

	    dsm_error_message(s, "dsm_monitor()");
	    if(s==DSM_RPC_ERROR || s==DSM_MON_LIST_EMPTY) continue;
	    else                                          abort();
	  }
	}
	
	printf("%s[%d]: monitor #%d (sz %d) %s:%s\n",
	       progname,pid,i+1,alhp[mach].allocs[alloc].size,
	       alhp[mach].machine_name,alhp[mach].allocs[alloc].name);
      }
    } /* for loop over howmany */

    dprintf("%s[%d]: allocating %d for return buffer\n",
	    progname,pid,size);
    p = (char *)malloc(size);
    if(p==(char *)NULL) {
      perror("malloc");
      abort();
    }

    if(readwait) {
      gettimeofday(&now, &dummy);
      printf("%s[%d]: calling dsm_read_wait() at %ld.%06ld\n",
	     progname,pid, now.tv_sec, now.tv_usec);
      s = dsm_read_wait(host, allocname, p);
      if(s!=DSM_SUCCESS) {
	dsm_error_message(s, "dsm_read_wait()");
	if(s==DSM_RPC_ERROR) {
	  free(p);
	  
	  s = dsm_clear_monitor();
	  if(s != DSM_SUCCESS) {
	    dsm_error_message(s,"dsm_clear_monitor()");
	    if(s!=DSM_RPC_ERROR) abort();
	  }
	  sleep(2);
	  continue;
	}
	else if(s==DSM_MON_LIST_EMPTY) {
	  free(p);
	  continue;
	}
	else 
	  abort();
      }
      
      if(allocname[strlen(allocname)-1]=='X') {
	dsm_structure *sp = *((dsm_structure **)p);
	printf("%s[%d] **** received %s:%s\n",
	       progname, pid, host, allocname);
	for(i=0; i<sp->n_elements; i++) {
	  printf("                       %s: lval=%d at %d\n",
		 sp->elements[i].name,
		 *(unsigned *)(sp->elements[i].datap),
		 (int)time(NULL));
	}
      }
      else {
	gettimeofday(&now, &dummy);
	printf("%s[%d] **** received %s:%s, lval=%ld at %ld.%06ld\n",
	       progname, pid, host, allocname, *((long *)p),
	       now.tv_sec, now.tv_usec);
      }
      
      dprintf("%s[%d] calling dsm_clear_monitor()\n",
	      progname, pid);
      s = dsm_clear_monitor();
      if(s != DSM_SUCCESS) {
	dsm_error_message(s,"dsm_clear_monitor()");
	if(s==DSM_RPC_ERROR) {
	  free(p);
	  continue;
	}
	else
	  abort();
      }
      
      for(i=0; i<NSTRUCT; i++) {
	if(ds[i].name[0] != '\0') {
	  dsm_structure_destroy(&ds[i]);
	  ds[i].name[0] = '\0';
	}
      }
      
      free(p);
    }
    else {
      dprintf("%s[%d]: calling dsm_read() at %d\n",
	      progname,pid, (int)time(NULL));
      
      printf("%s[%d] **** reading %s:%s\n",
	     progname, pid,
	     alhp[mach].machine_name,
	     alhp[mach].allocs[alloc].name);
      
      if(is_structure) {
	free(p);
	p = (char *)&ds[0];
      }

      s = dsm_read(alhp[mach].machine_name,
		   alhp[mach].allocs[alloc].name,
		   p,
		   &t);
      if(s!=DSM_SUCCESS) {
	dsm_error_message(s, "dsm_read()");
	if(s==DSM_RPC_ERROR) {
	  if(!is_structure) free(p);
	  continue;
	}
	else                 abort();
      }
      
      if(is_structure) {
	for(i=0; i<ds[0].n_elements; i++) {
	  printf("  %30s: lval %ld\n", 
		 ds[0].elements[i].name,
		 *(long *)(ds[0].elements[i].datap));
	}
	dsm_structure_destroy(&ds[0]);
	ds[0].name[0]='\0';
      }
      else {
	printf("%s[%d]           lval=%ld at %d\n",
	       progname, pid, *((long *)p), (int)time(NULL));
	free(p);
      }
    } /* if not read_wait */

    printf("\n\n");
  } /* while 1 */

  return(0);
}
Esempio n. 27
0
int
random_exp_int(int sn, double m)
/* mean value */
{
    return((int) rint((double) - m * log(myrandom(sn))));
}
Esempio n. 28
0
void example1(void)
{
   Int  i, n;
   Word v, prev;
   Word* vs[NN];
   Word *pv;

   // Create a static OSet of Ints.  This one uses fast (built-in)
   // comparisons.
   OSet* oset = VG_(OSetGen_Create)(0,
                                  NULL,
                                  malloc, free);

   // Try some operations on an empty OSet to ensure they don't screw up.
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   // Create some elements, with gaps (they're all even) but no dups,
   // and shuffle them randomly.
   for (i = 0; i < NN; i++) {
      vs[i] = VG_(OSetGen_AllocNode)(oset, sizeof(Word));
      *(vs[i]) = 2*i;
   }
   seed = 0;
   for (i = 0; i < NN; i++) {
      Word r1  = myrandom() % NN;
      Word r2  = myrandom() % NN;
      Word* tmp= vs[r1];
      vs[r1]   = vs[r2];
      vs[r2]   = tmp;
   }

   // Insert the elements
   for (i = 0; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   // Check the size
   vg_assert( NN == VG_(OSetGen_Size)(oset) );

   // Check we can find all the elements we inserted
   for (i = 0; i < NN; i++) {
      assert( VG_(OSetGen_Contains)(oset, vs[i]) );
   }

   // Check we cannot find elements we did not insert, below, within (odd
   // numbers), and above the inserted elements.
   v = -1;
   assert( ! VG_(OSetGen_Contains)(oset, &v) );
   for (i = 0; i < NN; i++) {
      v = *(vs[i]) + 1;
      assert( ! VG_(OSetGen_Contains)(oset, &v) );
   }
   v = NN*2;
   assert( ! VG_(OSetGen_Contains)(oset, &v) );

   // Check we can find all the elements we inserted, and the right values
   // are returned.
   for (i = 0; i < NN; i++) {
      assert( vs[i] == VG_(OSetGen_Lookup)(oset, vs[i]) );
   }

   // Check that we can iterate over the OSet elements in sorted order, and
   // there is the right number of them.
   n = 0;
   pv = NULL;
   prev = -1;
   VG_(OSetGen_ResetIter)(oset);
   while ( (pv = VG_(OSetGen_Next)(oset)) ) {
      Word curr = *pv;
      assert(prev < curr); 
      prev = curr;
      n++;
   }
   assert(NN == n);
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );

   // Check that we can remove half of the elements, and that their values
   // are as expected.
   for (i = 0; i < NN; i += 2) {
      assert( pv = VG_(OSetGen_Remove)(oset, vs[i]) );
      assert( pv == vs[i] );
   }

   // Check the size
   vg_assert( NN/2 == VG_(OSetGen_Size)(oset) );

   // Check we can find the remaining elements (with the right values).
   for (i = 1; i < NN; i += 2) {
      assert( pv = VG_(OSetGen_LookupWithCmp)(oset, vs[i], NULL) );
      assert( pv == vs[i] );
   }

   // Check we cannot find any of the elements we removed.
   for (i = 0; i < NN; i += 2) {
      assert( ! VG_(OSetGen_Contains)(oset, vs[i]) );
   }

   // Check that we can remove the remaining half of the elements, and that
   // their values are as expected.
   for (i = 1; i < NN; i += 2) {
      assert( pv = VG_(OSetGen_Remove)(oset, vs[i]) );
      assert( pv == vs[i] );
   }

   // Try some more operations on the empty OSet to ensure they don't screw up.
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   // Free a few elements
   VG_(OSetGen_FreeNode)(oset, vs[0]);
   VG_(OSetGen_FreeNode)(oset, vs[1]);
   VG_(OSetGen_FreeNode)(oset, vs[2]);

   // Re-insert remaining elements, to give OSetGen_Destroy something to
   // work with.
   for (i = 3; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   // Print the list
   OSet_Print(oset, "oset1", wordToStr);

   // Destroy the OSet
   VG_(OSetGen_Destroy)(oset);
}
Esempio n. 29
0
void example2(void)
{
   Int i, n;
   Addr a;
   Block* vs[NN];
   Block v, prev;
   Block *pv;

   // Create a dynamic OSet of Blocks.  This one uses slow (custom)
   // comparisons.
   OSet* oset = VG_(OSetGen_Create)(offsetof(Block, first),
                                    blockCmp,
                                    malloc, free);

   // Try some operations on an empty OSet to ensure they don't screw up.
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   // Create some inputs, with gaps -- intervals are 1..3, 11..13, ... -- but
   // no dups, and shuffle them randomly.
   for (i = 0; i < NN; i++) {
      vs[i] = VG_(OSetGen_AllocNode)(oset, sizeof(Block));
      vs[i]->b1    = i;
      vs[i]->first = i*10 + 1;
      vs[i]->last  = vs[i]->first + 2;
      vs[i]->b2    = i+1;
   }
   seed = 0;
   for (i = 0; i < NN; i++) {
      Int r1  = myrandom() % NN;
      Int r2  = myrandom() % NN;
      Block* tmp = vs[r1];
      vs[r1]  = vs[r2];
      vs[r2]  = tmp;
   }

   // Insert the elements
   for (i = 0; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   // Check the size
   vg_assert( NN == VG_(OSetGen_Size)(oset) );

   // Check we can find all the elements we inserted, within the full range
   // of each Block.
   for (i = 0; i < NN; i++) {
      a = vs[i]->first + 0;    assert( VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 1;    assert( VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 2;    assert( VG_(OSetGen_Contains)(oset, &a) );
   }

   // Check we cannot find elements we did not insert, below and above the
   // ranges of the inserted elements.
   a = 0;
   assert( ! VG_(OSetGen_Contains)(oset, &a) );
   for (i = 0; i < NN; i++) {
      a = vs[i]->first - 1;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 3;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
   }

   // Check we can find all the elements we inserted, and the right values
   // are returned.
   for (i = 0; i < NN; i++) {
      a = vs[i]->first + 0;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 1;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 2;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      assert( vs[i] == VG_(OSetGen_LookupWithCmp)(oset, &a, blockCmp) );
   }

   // Check that we can iterate over the OSet elements in sorted order, and
   // there is the right number of them.
   n = 0;
   pv = NULL;
   prev.last = 0;
   VG_(OSetGen_ResetIter)(oset);
   while ( (pv = VG_(OSetGen_Next)(oset)) ) {
      Block curr = *pv;
      assert(prev.last < curr.first); 
      prev = curr;
      n++;
   }
   assert(NN == n);
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );

   // Check that we can remove half of the elements, and that their values
   // are as expected.
   for (i = 0; i < NN; i += 2) {
      a = vs[i]->first;    assert( vs[i] == VG_(OSetGen_Remove)(oset, &a) );
   }

   // Check the size
   vg_assert( NN/2 == VG_(OSetGen_Size)(oset) );

   // Check we can find the remaining elements (with the right values).
   for (i = 1; i < NN; i += 2) {
      a = vs[i]->first + 0;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 1;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
      a = vs[i]->first + 2;    assert( vs[i] == VG_(OSetGen_Lookup)(oset, &a) );
   }

   // Check we cannot find any of the elements we removed.
   for (i = 0; i < NN; i += 2) {
      a = vs[i]->first + 0;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 1;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
      a = vs[i]->first + 2;    assert( ! VG_(OSetGen_Contains)(oset, &a) );
   }

   // Check that we can remove the remaining half of the elements, and that
   // their values are as expected.
   for (i = 1; i < NN; i += 2) {
      a = vs[i]->first;    assert( vs[i] == VG_(OSetGen_Remove)(oset, &a) );
   }

   // Try some more operations on the empty OSet to ensure they don't screw up.
   vg_assert( ! VG_(OSetGen_Contains)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Lookup)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Remove)(oset, &v) );
   vg_assert( ! VG_(OSetGen_Next)(oset) );
   vg_assert( 0 == VG_(OSetGen_Size)(oset) );

   // Re-insert all elements, to give OSetGen_Destroy something to work with.
   for (i = 0; i < NN; i++) {
      VG_(OSetGen_Insert)(oset, vs[i]);
   }

   // Destroy the OSet
   VG_(OSetGen_Destroy)(oset);
}
int main(){
	int i,j;
	bool mark;
	int n =8;
    fillBox(n);
	fillBox(n);
    output(n);

    printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);
	printf("%d\n",myrandom() % n);

    while (true){
		char ch;
		scanf("%c%*c", &ch);
		if (ch == 'a'){
			// mark = left(n);
			left_remove_blank(n);
			left(n);
		}
		else if (ch == 'd'){
			// mark = right(n);
			right_remove_blank(n);
			right(n);
		}
		else if (ch == 'w'){
			// mark = up(n);
			up_remove_blank(n);
			up(n);
		}
		else if (ch == 's'){
			// mark = down(n);
			down_remove_blank(n);
			down(n);
		}
		else{
			continue;
		}
		system("clear");
		//printf("Move:\n");
		//output();
		// if (!mark){
		// 	continue;
		// }
		fillBox(n);
		printf("Fill:\n");
		output(n);

		if (isOver(n)){
			printf("\n\nGame Over!\n\n");
			break;
		}
		if (isWin(n))
		{
			printf("\n\n Wow You Win!\n\n");
			break;
			/* code */
		}
	}
	
	return 0;
}