Ejemplo n.º 1
0
void MAIAllocator::Setup() {

//////// initialization of dynamic data structures

  Hmat = gsl_matrix_complex_alloc(N(),M());
  Hchan = gsl_vector_complex_alloc(N());
  Hperm = gsl_matrix_uint_alloc(N(),M());
  p = gsl_permutation_alloc(N());
  huserabs = gsl_vector_alloc(N());
  nextcarr = gsl_vector_uint_alloc(M());
  usedcarr = gsl_vector_uint_alloc(N());
  errs = gsl_vector_uint_alloc(M());
  habs = gsl_matrix_alloc(N(),M());
  huu = gsl_matrix_complex_alloc(N(),M());

  framecount = 0;
  ericount = 0;
  csicount = 0;
  noDecisions = 0;
  ostringstream cmd;

  //
  // time
  //
  time(&reporttime);

  //
  // Random Generator
  //
  ran = gsl_rng_alloc( gsl_rng_default );

  // SIGNATURE FREQUENCIES INITIAL SETUP

  signature_frequencies = gsl_matrix_uint_alloc(M(),J());
  signature_frequencies_init = gsl_matrix_uint_alloc(M(),J());
  signature_powers = gsl_matrix_alloc(M(),J());


  for (int i=0; i<M(); i++)
    for (int j=0; j<J(); j++)
      gsl_matrix_uint_set(signature_frequencies_init,i,j,(j*M()+i) % N());

  //
  // INITIAL ALLOCATION
  //
  gsl_matrix_uint_memcpy(signature_frequencies,
			 signature_frequencies_init);

  // maximum initial powers for all carriers
  gsl_matrix_set_all(signature_powers,INIT_CARR_POWER); 

  gsl_vector_uint_set_zero(errs);


  //
  //
  //  FFT Transform Matrix
  //  
  // 
  transform_mat = gsl_matrix_complex_calloc(N(),N());
  double fftarg=-2.0*double(M_PI/N());
  double fftamp=1.0/sqrt(double(N()));

  for (int i=0; i<N(); i++)
    for (int j=0; j<N(); j++)
      gsl_matrix_complex_set(transform_mat,i,j,
			     gsl_complex_polar(fftamp,fftarg*i*j) );



  switch (Mode()) {
  case 0:
    cout << BlockName << " - Allocator type FIXED_ALLOCATION selected" << endl;
    break;
  case 1:
    cout << BlockName << " - Allocator type GIVE_BEST_CARR selected" << endl;
    break;
  case 2:
    cout << BlockName << " - Allocator type SWAP_BAD_GOOD selected" << endl;
    break;
  case 3:
    cout << BlockName << " - Allocator type BEST_OVERLAP selected" << endl;
    break;
  case 4:
    cout << BlockName << " - Allocator type SOAR_AI selected" << endl;

    //
    // SOAR INITIALIZATION
    //
    max_errors = MAX_ERROR_RATE * ERROR_REPORT_INTERVAL * Nb() * K();
    cout << BlockName << " - Max errors tuned to " << max_errors << " errors/frame." << endl;

    //
    // first we initialize the vectors and matrices
    // in the order of appearance in the header file.
    //
    umapUserVec =  vector < Identifier * > (M());
    umapUserUidVec = vector < IntElement * > (M());
    umapUserErrsVec = vector < IntElement * > (M());
    umapUserPowerVec = vector < FloatElement * > (M());
    umapUserCarrMat  = vector < Identifier * > (M()*J());
    umapUserCarrCidMat = vector < IntElement * > (M()*J());
    umapUserCarrPowerMat = vector < FloatElement * > (M()*J());
    
    chansCoeffMat = vector < Identifier * > (M()*N());
    chansCoeffUserMat = vector < IntElement * > (M()*N());
    chansCoeffCarrMat = vector < IntElement * > (M()*N());
    chansCoeffValueMat = vector < FloatElement * > (M()*N());
    
    carmapCarrVec = vector < Identifier * >  (N());
    carmapCarrCidVec = vector < IntElement * >   (N());
    

    //
    // then we create an instance of the Soar kernel in our process
    //

    pKernel = Kernel::CreateKernelInNewThread() ;
    //pKernel = Kernel::CreateRemoteConnection() ;
    
    // Check that nothing went wrong.  We will always get back a kernel object
    // even if something went wrong and we have to abort.
    if (pKernel->HadError())
      {
	cerr << BlockName << ".SOAR - " 
	     << pKernel->GetLastErrorDescription() << endl ;
	exit(1);
      }
    
    // We check if an agent has been prevoiusly created, otherwise we create it 
    // NOTE: We don't delete the agent pointer.  It's owned by the kernel
    pAgent = pKernel->GetAgent("AIAllocator") ;
    if (! pKernel->IsAgentValid(pAgent)) {
      pAgent = pKernel->CreateAgent("AIAllocator") ;
    }
    
    
    // Check that nothing went wrong
    // NOTE: No agent gets created if there's a problem, so we have to check for
    // errors through the kernel object.
    if (pKernel->HadError())
      {
	cerr << BlockName << ".SOAR - " << pKernel->GetLastErrorDescription() << endl ;
	exit(1);
      }
    
    //
    // load productions
    //
    pAgent->LoadProductions(SoarFn());

    // spawn debugger
#ifdef SPAWN_DEBUGGER
    pAgent->SpawnDebugger();
#endif
    
    // Check that nothing went wrong
    // NOTE: No agent gets created if there's a problem, so we have to check for
    // errors through the kernel object.
    if (pKernel->HadError())
      {
	cerr << BlockName << ".SOAR - " 
	     << pKernel->GetLastErrorDescription() << endl ;
	exit(1);
      }

    // keypress 
    //cout << "pause maillocator:203 ... (press ENTER key)" << endl;
    //cin.ignore();

    //
    // we can now generate initial input link structure
    //

    // NO MORE adjust max-nil-output-cycle
    //cmd << "max-nil-output-cycles " << 120;
    //pAgent->ExecuteCommandLine(cmd.str().c_str());

    // the input-link
    pInputLink = pAgent->GetInputLink();

    // input-time
    input_time = 0;
    inputTime = pAgent->CreateIntWME(pInputLink,"input-time",input_time);

    // the usrmap structure (common wmes)
    umap = pAgent->CreateIdWME(pInputLink,"usrmap");

    // BITS_PER_REPORT = ERROR_REPORT_INTERVAL * Nb() * K()
    // MAX_ERRORS = MAX_ERROR_RATE * BITS_PER_REPORT
    umapMaxerr = pAgent->CreateIntWME(umap,"maxerr",max_errors);
    umapPstep = pAgent->CreateFloatWME(umap,"pstep",POWER_STEP);
    umapPmax = pAgent->CreateFloatWME(umap,"pmax",MAX_POWER);
    // the channels
    chans = pAgent->CreateIdWME(pInputLink,"channels");
    // the carmap
    carmap = pAgent->CreateIdWME(pInputLink,"carmap");
 
    // the usrmap structure (users substructure)
    for (int i=0;i<M();i++) { // user loop
      umapUserVec[i] = pAgent->CreateIdWME(umap,"user");
      umapUserUidVec[i] = pAgent->CreateIntWME(umapUserVec[i],"uid",i);
      umapUserErrsVec[i] = pAgent->CreateIntWME(umapUserVec[i],"errs",int(0));
      umapUserPowerVec[i] = pAgent->CreateFloatWME(umapUserVec[i],"power",J());
      // update the current allocation 
      for (int j=0;j<J();j++) { // allocated carriers loop
	unsigned int usedcarr = gsl_matrix_uint_get(signature_frequencies,i,j);
	double usedpow = gsl_matrix_get(signature_powers,i,j);
	umapUserCarrMat[i*J()+j] = pAgent->CreateIdWME(umapUserVec[i],"carr");
	umapUserCarrCidMat[i*J()+j] = 
	  pAgent->CreateIntWME(umapUserCarrMat[i*J()+j],"cid",usedcarr);
	umapUserCarrPowerMat[i*J()+j] = 
	  pAgent->CreateFloatWME(umapUserCarrMat[i*J()+j],"power",usedpow);
      } // allocated carriers loop
      // the channels
      for (int j=0;j<N();j++) { // all channels loop
	chansCoeffMat[i*N()+j] = pAgent->CreateIdWME(chans,"coeff");
	chansCoeffUserMat[i*N()+j] = pAgent->CreateIntWME(chansCoeffMat[i*N()+j],"user",i);
	chansCoeffCarrMat[i*N()+j] = pAgent->CreateIntWME(chansCoeffMat[i*N()+j],"carr",j);
	chansCoeffValueMat[i*N()+j] = pAgent->CreateFloatWME(chansCoeffMat[i*N()+j],"value",0.0);	
      } // all channels loop
    } // user loop

    // the carmap structure
    for (int j=0;j<N();j++) { // all carriers loop
	carmapCarrVec[j] = pAgent->CreateIdWME(carmap,"carr");
	carmapCarrCidVec[j] = pAgent->CreateIntWME(carmapCarrVec[j],"cid",j);
      } // all carriers loop
     
    //
    // END OF SOAR INITIALIZAZION
    //
   
    break;  
  default:
    cerr << BlockName << " - Unhandled allocator type !" << endl;
    exit(1);
  }
  

  //////// rate declaration for ports


}
Ejemplo n.º 2
0
/*!
Computes assignment matrix for M.
*/
int Hungarian(const gsl_matrix * const M, const bool convToMinAsg,
              gsl_matrix * const Assignment)
{
	int res, z0_r, z0_c;
	bool done = false;
	unsigned int next = STEP1;
	gsl_vector_uint *rowCov, *colCov;
	gsl_matrix_uint *mask;
	gsl_matrix_int  *path;
	gsl_matrix *MCopy;

	MCopy = gsl_matrix_alloc(M->size1, M->size2);
	gsl_matrix_memcpy(MCopy, M);
	if(convToMinAsg == true)
	{
		res = ConvertToMinAsg(MCopy);
		if(res != GSL_SUCCESS) return res;
	}

	// Allocate memory
	rowCov = gsl_vector_uint_alloc(M->size1);
	colCov = gsl_vector_uint_alloc(M->size2);
	mask = gsl_matrix_uint_alloc(M->size1, M->size2);
	path = gsl_matrix_int_calloc(ceil(((float)(mask->size1*mask->size2))/2), 2 );

	// Initialize
	gsl_vector_uint_set_all(rowCov, UNCOVERED);
	gsl_vector_uint_set_all(colCov, UNCOVERED);
	gsl_matrix_uint_set_all(mask, UNMASKED);

	while(done == false)
	{
		switch(next)
		{
			case STEP1:
				next = Step1(MCopy);
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 1");
					PrintCover( rowCov, colCov, "Post Step 1 Cover");
				#endif
				break;
			case STEP2:
				next = Step2(MCopy, mask, rowCov, colCov);
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 2");
					PrintCover( rowCov, colCov, "Post Step 2 Cover");
				#endif
				break;
			case STEP3:
				next = Step3(MCopy, mask, colCov);
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 3");
					PrintCover( rowCov, colCov, "Post Step 3 Cover");
				#endif
				break;
			case STEP4:
				next = Step4(MCopy, mask, rowCov, colCov, &z0_r, &z0_c);
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 4");
					PrintCover( rowCov, colCov, "Post Step 4 Cover");
				#endif
				break;
			case STEP5:
				next = Step5(mask, path, rowCov, colCov, z0_r, z0_c); 
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 5");
					PrintCover( rowCov, colCov, "Post Step 5 Cover");
				#endif
				break;
			case STEP6:
				next = Step6(MCopy, rowCov, colCov);
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "Post Step 6");
					PrintCover( rowCov, colCov, "Post Step 6 Cover");
				#endif
				break;
			case DONE:
				#ifdef VERBOSE
					Matrix_print_with_mask(MCopy, mask, rowCov, colCov, "DONE");
				#endif
				UpdateAssignment(mask, Assignment);
				done = true;
				break;
			default:
				done = true;
				fprintf(stderr, "Error!\n");
		}
	}

	// Release memory
	gsl_matrix_free(MCopy);
	gsl_vector_uint_free(rowCov);
	gsl_vector_uint_free(colCov);
	gsl_matrix_uint_free(mask);
	gsl_matrix_int_free(path);

	return GSL_SUCCESS;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
	FILE *fid;
	int choice;
	// gsl_matrix_complex *A, *B;
	gsl_matrix *A = NULL, *B = NULL;
	gsl_matrix_uint *Assignment;
	float score;
	unsigned int testcount, steps;
	struct timeval start, stop, elapsed;

	//if((argc != 2 && atoi(argv[1]) != 8) || (argc != 3 && atoi(argv[1]) == 8))
	if(argc != 2)
	{
		printf("Enter test case #.\n");
		return 0;
	}

	fid = fopen("perfd.log", "w");
	if(fid == NULL)
		return 0;

	for(steps = 50; steps <= 1000; steps += 50)
	{
		for(testcount = 0; testcount < 10; testcount++)
		{
			if((gettimeofday(&start, NULL)) == -1)
			{
				perror("gettimeofday");
				exit(1);
			}

			choice = atoi(argv[1]);
			switch(choice)
			{
				case 1:
					//testcase1(&A, &B);
					break;
				case 2:	testcase2(&A, &B); break;
				case 3: testcase3(&A, &B); break;
				case 4: testcase4(&A, &B); break;
				case 5: testcase5(&A, &B); break;
				case 6: testcase6(&A, &B); break;
				case 7: testcase7(&A, &B); break;
				case 8: testcase8(&A, &B, steps); break;
				default:
					printf("Invalid choice.\n");
					return 0;
			}

			if(A != NULL && B != NULL)
			{	
				//PrintGSLMatrixComplex(A, "Input A");
				//PrintGSLMatrixComplex(B, "Input B");
				Assignment = gsl_matrix_uint_alloc(A->size1, A->size2);
				//EigenDecomp(A, B, P);
				score = 0;
				Umeyama(A, B, Assignment, &score);
				//PrintGSLMatrixUint(Assignment, "Assignment");
	
				//printf("SCORE: %f\n", score);
	
				if((gettimeofday(&stop, NULL)) == -1)
				{
					perror("gettimeofday");
					exit(1);
				}

				// Compute elapsed time
				timeval_subtract(&elapsed, &stop, &start);
				fprintf(stderr, "%d\t%d\t%ld\n", steps, testcount, elapsed.tv_sec*1000000 + elapsed.tv_usec);
				fprintf(fid, "%d\t%d\t%ld\n", steps, testcount, elapsed.tv_sec*1000000 + elapsed.tv_usec);
				fflush(stderr); fflush(fid);

				gsl_matrix_uint_free(Assignment);
				//gsl_matrix_complex_free(A);
				//gsl_matrix_complex_free(B);
				gsl_matrix_free(A);
				gsl_matrix_free(B);
			}
		}
	}
	fclose(fid);

	return 0;
}