Example #1
0
/**
*   Create a system of size 9-by-9 with known eigenvalues and compare it with the eigenvalues computed from the QR method implemented in ViennaCL.
**/
int main()
{
  // Change to 'double' if supported by your hardware.
  typedef float ScalarType;

  std::cout << "Testing matrix of size " << 9 << "-by-" << 9 << std::endl;

  viennacl::matrix<ScalarType> A_input(9,9);
  viennacl::matrix<ScalarType> Q(9, 9);
  std::vector<ScalarType> eigenvalues_ref(9);
  std::vector<ScalarType> eigenvalues(9);

  viennacl::vector<ScalarType> vcl_eigenvalues(9);

  initialize(A_input, eigenvalues_ref);

  std::cout << std::endl <<"Input matrix: " << std::endl;
  std::cout << A_input << std::endl;

  viennacl::matrix<ScalarType> A_input2(A_input); // duplicate to demonstrate the use with both std::vector and viennacl::vector


  /**
  * Call the function qr_method_sym() to calculate eigenvalues and eigenvectors
  * Parameters:
  *  -     A_input      - input matrix to find eigenvalues and eigenvectors from
  *  -     Q            - matrix, where the calculated eigenvectors will be stored in
  *  -     eigenvalues  - vector, where the calculated eigenvalues will be stored in
  **/

  std::cout << "Calculation..." << std::endl;
  viennacl::linalg::qr_method_sym(A_input, Q, eigenvalues);

  /**
   *  Same as before, but writing the eigenvalues to a ViennaCL-vector:
   **/
  viennacl::linalg::qr_method_sym(A_input2, Q, vcl_eigenvalues);

  /**
  *   Print the computed eigenvalues and eigenvectors:
  **/
  std::cout << std::endl << "Eigenvalues with std::vector<T>:" << std::endl;
  vector_print(eigenvalues);
  std::cout << "Eigenvalues with viennacl::vector<T>: " << std::endl << vcl_eigenvalues << std::endl;
  std::cout << std::endl << "Reference eigenvalues:" << std::endl;
  vector_print(eigenvalues_ref);
  std::cout << std::endl;
  std::cout << "Eigenvectors - each column is an eigenvector" << std::endl;
  std::cout << Q << std::endl;

  /**
  *  That's it. Print a success message and exit.
  **/
  std::cout << std::endl;
  std::cout << "------- Tutorial completed --------" << std::endl;
  std::cout << std::endl;

  return EXIT_SUCCESS;
}
int main()
{
/*
 * Programm zum Testen der einzelnen OpenMP Funktionen
 * vom Ordner /build aus aufrufen!
 *
 * Matrix wird automatisch eingelesen, dann wird die entsprechende
 * Funktion  (z.B. house_update_A_right ) darauf angewendet, dann
 * wird sie wieder ausgegeben
 *
 */

  boost::numeric::ublas::compressed_matrix<ScalarType> ublas_A;


  std::cout << "Reading..." << "\n";
  std::size_t sz;
  // read file
  std::fstream f("../examples/testdata/eigen/symm1.example", std::fstream::in);
  //read size of input matrix
  read_matrix_size(f, sz);
  ublas::vector<ScalarType> D = ublas::scalar_vector<ScalarType>(sz, 0);
  ublas::vector<ScalarType> E = ublas::scalar_vector<ScalarType>(sz, 0);
  std::cout << "Testing matrix of size " << sz << "-by-" << sz << std::endl;

  viennacl::matrix<ScalarType> A_input(sz, sz), A_ref(sz, sz), Q(sz, sz);

  read_matrix_body(f, A_input);
  f.close();
  matrix_print(A_input);


  D[0] = 0;
  D[1] = -5.77735;
  D[2] = -5.77735;
  D[3] =  5.77735;
  //viennacl::linalg::host_based::house_update_A_left(A_input, D, 0);
  //viennacl::linalg::host_based::house_update_A_right(A_input, D, 0);

  viennacl::linalg::cuda::bidiag_pack(A_input, D, 0);
  std::cout << "Testdata wurde gelesen!" << std::endl;
  //vector_print(D);
  //matrix_print(A_input);

}
Example #3
0
void test_eigen(const std::string& fn, bool is_symm)
{
    std::cout << "Reading..." << "\n";
    std::size_t sz;
    // read file
    std::fstream f(fn.c_str(), std::fstream::in);
    //read size of input matrix
    read_matrix_size(f, sz);

    bool is_row = viennacl::is_row_major<MatrixLayout>::value;
    if (is_row)
      std::cout << "Testing row-major matrix of size " << sz << "-by-" << sz << std::endl;
    else
      std::cout << "Testing column-major matrix of size " << sz << "-by-" << sz << std::endl;

    viennacl::matrix<ScalarType> A_input(sz, sz), A_ref(sz, sz), Q(sz, sz);
    // reference vector with reference values from file
    std::vector<ScalarType> eigen_ref_re(sz);
    // calculated real eigenvalues
    std::vector<ScalarType> eigen_re(sz);
    // calculated im. eigenvalues
    std::vector<ScalarType> eigen_im(sz);

    // read input matrix from file
    read_matrix_body(f, A_input);
    // read reference eigenvalues from file
    read_vector_body(f, eigen_ref_re);


    f.close();

    A_ref = A_input;

    std::cout << "Calculation..." << "\n";

    Timer timer;
    timer.start();
    // Start the calculation
    if(is_symm)
        viennacl::linalg::qr_method_sym(A_input, Q, eigen_re);
    else
        viennacl::linalg::qr_method_nsm(A_input, Q, eigen_re, eigen_im);
/*

    std::cout << "\n\n Matrix A: \n\n";
    matrix_print(A_input);
    std::cout << "\n\n";

    std::cout << "\n\n Matrix Q: \n\n";
    matrix_print(Q);
    std::cout << "\n\n";
*/

    double time_spend = timer.get();

    std::cout << "Verification..." << "\n";

    bool is_hessenberg = check_hessenberg(A_input);
    bool is_tridiag = check_tridiag(A_input);

    ublas::matrix<ScalarType> A_ref_ublas(sz, sz), A_input_ublas(sz, sz), Q_ublas(sz, sz), result1(sz, sz), result2(sz, sz);
    viennacl::copy(A_ref, A_ref_ublas);
    viennacl::copy(A_input, A_input_ublas);
    viennacl::copy(Q, Q_ublas);

    // compute result1 = ublas::prod(Q_ublas, A_input_ublas);   (terribly slow when using ublas directly)
    for (std::size_t i=0; i<result1.size1(); ++i)
      for (std::size_t j=0; j<result1.size2(); ++j)
      {
        ScalarType value = 0;
        for (std::size_t k=0; k<Q_ublas.size2(); ++k)
          value += Q_ublas(i, k) * A_input_ublas(k, j);
        result1(i,j) = value;
      }
    // compute result2 = ublas::prod(A_ref_ublas, Q_ublas);   (terribly slow when using ublas directly)
    for (std::size_t i=0; i<result2.size1(); ++i)
      for (std::size_t j=0; j<result2.size2(); ++j)
      {
        ScalarType value = 0;
        for (std::size_t k=0; k<A_ref_ublas.size2(); ++k)
          value += A_ref_ublas(i, k) * Q_ublas(k, j);
        result2(i,j) = value;
      }


    ScalarType prods_diff = matrix_compare(result1, result2);
    ScalarType eigen_diff = vector_compare(eigen_re, eigen_ref_re);


    bool is_ok = is_hessenberg;

    if(is_symm)
        is_ok = is_ok && is_tridiag;

    is_ok = is_ok && (eigen_diff < EPS);
    is_ok = is_ok && (prods_diff < EPS);

    // std::cout << A_ref << "\n";
    // std::cout << A_input << "\n";
    // std::cout << Q << "\n";
    // std::cout << eigen_re << "\n";
    // std::cout << eigen_im << "\n";
    // std::cout << eigen_ref_re << "\n";
    // std::cout << eigen_ref_im << "\n";

    // std::cout << result1 << "\n";
    // std::cout << result2 << "\n";
    // std::cout << eigen_ref << "\n";
    // std::cout << eigen << "\n";

    printf("%6s [%dx%d] %40s time = %.4f\n", is_ok?"[[OK]]":"[FAIL]", (int)A_ref.size1(), (int)A_ref.size2(), fn.c_str(), time_spend);
    printf("tridiagonal = %d, hessenberg = %d prod-diff = %f eigen-diff = %f\n", is_tridiag, is_hessenberg, prods_diff, eigen_diff);
    std::cout << std::endl << std::endl;

    if (!is_ok)
      exit(EXIT_FAILURE);

}
Example #4
0
int main( int argc, char *argv[] )     {
    struct event *eventptr;
    struct msg   msg2give;
    struct pkt   pkt2give;
   
    int          currentEntity;
    int          i,j;
  
//  Do our initialization and any requested by student's Transport Layer 
    CallingArgc = argc;
    CallingArgv = argv;
    init( );
    A_init();
    B_init();
   
    /* ***********************************************************************
      We loop here forever.  During these actions we get an event off the
      event header.  We analyze the action to be performed and go do it.
    *************************************************************************/
    while (TRUE) {
        eventptr = evlist;        // get next event to simulate from Q head 
        if (eventptr==NULL)       //  IF nothing to simulate, we're done  
            break;

        evlist = evlist->next;    // remove this event from event list 
        if (evlist != NULL)       //   and sort out forward and back ptrs
            evlist->prev=NULL;

        // Update simulation time to event time  - by definition, the
        // simulation time is the time of the last event.
        CurrentSimTime = eventptr->evtime;        
        currentEntity = eventptr->eventity;
        // If we've delivered the required number of messages, then we've
        // fullfilled our task and are done.
        if ( NumMsgs4To5 >= MaxMsgsToSimulate )
            break;                            // all done with simulation 
        
        /* *******************************************************************
          Here we've gotten a request to hand data from layer 5 to layer 4.
            Generate the date we want to give to the student.                 
        *********************************************************************/
        if ( eventptr->evtype == FROM_LAYER5 ) {
            // Use the sequence number as starter for the message string
            j = GeneratingSeqNum[currentEntity]++;
            // printf( "%X  %d %d\n", &eventptr, currentEntity, j );
            GetMessageString( currentEntity, j, &(msg2give.data[0]) );
            /*  Always print trace so we know it matches the receive  */
            if ( TraceLevel >= 0 )  {    
                //printf("%c: ", &(EntityLetter[currentEntity]) );
                if ( currentEntity == AEntity )   
                    printf("A: " );
                else
                    printf("B: " );
                printf(" %11.4f,", eventptr->evtime);

                printf("  Layer 5 to 4  Message = ");
                for (i=0; i<MESSAGE_LENGTH; i++) 
                    printf("%c", msg2give.data[i]);
                printf("\n");
            }
            // The simulation will actually end when the requested number of
            // messages has been received back at layer 5.  But there could be
            // many packets in the system, and that total arrival could take
            // a long time.  We want to make sure we down't overwhelm the
            // student code with messages that won't ever be delivered anyway.
            //
            if ( NumMsgs5To4 <= MaxMsgsToSimulate + 3 ) { 
                GenerateNextArrival();           // set up future arrival 
                NumMsgs5To4++;                   // # msgs from layer 5 to 4 
            }
            if ( currentEntity == AEntity )  // Pass the data to layer 4 here 
                A_output(msg2give);  
            else
                B_output(msg2give);  
        }                              /* END of event is from layer 5 */

        /* *******************************************************************
          This is a request to hand data from layer 3 up to student layer 4 
        *********************************************************************/
        else if (eventptr->evtype ==  FROM_LAYER3 ) {
            pkt2give.seqnum   = eventptr->pktptr->seqnum;
            pkt2give.acknum   = eventptr->pktptr->acknum;
            pkt2give.checksum = eventptr->pktptr->checksum;
            for ( i = 0; i < MESSAGE_LENGTH; i++)  
                pkt2give.payload[i] = eventptr->pktptr->payload[i];

            if ( TraceLevel >= 2 )  {     /* Print out trace info if requested */
                if ( eventptr->eventity == AEntity )   
                    printf("A: " );
                else
                    printf("B: " );
                printf(" %11.4f,", eventptr->evtime);

                printf("  Layer 3 to 4  ");
                printf( "Seq/Ack/Check = %d/%d/%d:  ",
                        eventptr->pktptr->seqnum, eventptr->pktptr->acknum,
                        eventptr->pktptr->checksum );
                for (i=0; i<MESSAGE_LENGTH; i++) 
                    printf("%c", eventptr->pktptr->payload[i] );
                printf("\n");
            }
            if (eventptr->eventity == AEntity)   /* deliver packet by calling */
                   A_input(pkt2give);            /* appropriate entity */
            else
                   B_input(pkt2give);
            free(eventptr->pktptr);          /* free the memory for packet */
        }                             /* END of event is from layer 3 */

        /* *******************************************************************
              This is a request for a timer interrupt 
        *********************************************************************/
        else if (eventptr->evtype ==  TIMER_INTERRUPT) {
            if ( TraceLevel >= 2 )  {     /* Print out trace info if requested */
                if ( eventptr->eventity == AEntity )   
                    printf("A: " );
                else
                    printf("B: " );
                printf(" %f,", eventptr->evtime);
                printf("  Timer Interrupt\n");
            }
            if (eventptr->eventity == AEntity) 
                A_timerinterrupt();
            else
                B_timerinterrupt();
        }                             /* END of event is interrupt request */
        else  {
            printf("INTERNAL PANIC: unknown event type \n");
        }
        free(eventptr);
    }                       // End of while

terminate:
    printf("\n\nSimulator terminated at time %f\n after receiving %d msgs at layer5\n",
                  CurrentSimTime, NumMsgs4To5 );
    printf( "Simulator Analysis:\n");
    printf( "  Number of messages sent from 5 to 4: %d\n", NumMsgs5To4 );
    printf( "  Number of messages received at Layer 5, side A: %d\n",
                  ExpectedSeqNum[0]  );
    printf( "  Number of messages received at Layer 5, side B: %d\n",
                  ExpectedSeqNum[1]  );
    printf( "  Number of messages incorrectly received at layer 5: %d\n", 
                  NumMsgs5To4WithErr );
    printf( "  Number of packets entering the network: %d\n", NumMsgs4To3 );
    printf( "  Average number of packets already in network: %6.3f\n",
               (double)NumSimutaneousMsgs /  (double)NumMsgs4To3 );
    printf( "  Number of packets that the network lost: %d\n", NumMsgsLost );
    printf( "  Number of packets that the network corrupted: %d\n", 
                   NumMsgsCorrupt );
    printf( "  Number of packets that the network put out of order: %d\n", 
                   NumMsgsOutOfOrder );
    return 0;
}
int main(int argc, char **argv)
{
	struct event *eventptr;
	struct msg  msg2give;
	struct pkt  pkt2give;

	int i,j;
	char c;

	int opt;
	int seed;

	//Check for number of arguments
	if(argc != 5){
		fprintf(stderr, "Missing arguments\n");
		printf("Usage: %s -s SEED -w WINDOWSIZE\n", argv[0]);
		return -1;
	}

	/*
	 * Parse the arguments
	 * http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
	 */
	while((opt = getopt(argc, argv,"s:w:")) != -1){
		switch (opt){
		case 's':   if(!isNumber(optarg)){
			fprintf(stderr, "Invalid value for -s\n");
			return -1;
		}
		seed = atoi(optarg);
		break;

		case 'w':   if(!isNumber(optarg)){
			fprintf(stderr, "Invalid value for -w\n");
			return -1;
		}
		WINSIZE = atoi(optarg);
		break;

		case '?':   break;

		default:    printf("Usage: %s -s SEED -w WINDOWSIZE\n", argv[0]);
		return -1;
		}
	}

	init(seed);
	A_init();
	B_init();

	while (1) {
		eventptr = evlist;            /* get next event to simulate */
		if (eventptr==NULL)
			goto terminate;
		evlist = evlist->next;        /* remove this event from event list */
		if (evlist!=NULL)
			evlist->prev=NULL;
		if (TRACE>=2) {
			printf("\nEVENT time: %f,",eventptr->evtime);
			printf("  type: %d",eventptr->evtype);
			if (eventptr->evtype==0)
				printf(", timerinterrupt  ");
			else if (eventptr->evtype==1)
				printf(", fromlayer5 ");
			else
				printf(", fromlayer3 ");
			printf(" entity: %d\n",eventptr->eventity);
		}
		time_local = eventptr->evtime;        /* update time to next event time */
		if (nsim==nsimmax)
			break;                        /* all done with simulation */
		if (eventptr->evtype == FROM_LAYER5 ) {
			generate_next_arrival();   /* set up future arrival */
			/* fill in msg to give with string of same letter */
			j = nsim % 26;
			for (i=0; i<20; i++)
				msg2give.data[i] = 97 + j;
			if (TRACE>2) {
				printf("          MAINLOOP: data given to student: ");
				for (i=0; i<20; i++)
					printf("%c", msg2give.data[i]);
				printf("\n");
			}
			nsim++;
			if (eventptr->eventity == A)
				A_output(msg2give);
			else
				B_output(msg2give);
		}
		else if (eventptr->evtype ==  FROM_LAYER3) {
			pkt2give.seqnum = eventptr->pktptr->seqnum;
			pkt2give.acknum = eventptr->pktptr->acknum;
			pkt2give.checksum = eventptr->pktptr->checksum;
			for (i=0; i<20; i++)
				pkt2give.payload[i] = eventptr->pktptr->payload[i];
			if (eventptr->eventity ==A)      /* deliver packet by calling */
				A_input(pkt2give);            /* appropriate entity */
			else
				B_input(pkt2give);
			free(eventptr->pktptr);          /* free the memory for packet */
		}
		else if (eventptr->evtype ==  TIMER_INTERRUPT) {
			if (eventptr->eventity == A)
				A_timerinterrupt();
			else
				B_timerinterrupt();
		}
		else  {
			printf("INTERNAL PANIC: unknown event type \n");
		}
		free(eventptr);
	}

	terminate:
	//Do NOT change any of the following printfs
	printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n",time_local,
nsim);

	printf("\n");
	printf("Protocol: GBN\n");
	printf("[PA2]%d packets sent from the Application Layer of Sender A[/PA2]\n", A_application);
	printf("[PA2]%d packets sent from the Transport Layer of Sender A[/PA2]\n", A_transport);
	printf("[PA2]%d packets received at the Transport layer of Receiver B[/PA2]\n", B_transport);
	printf("[PA2]%d packets received at the Application layer of Receiver B[/PA2]\n", 
B_application);
	printf("[PA2]Total time: %f time units[/PA2]\n", time_local);
	printf("[PA2]Throughput: %f packets/time units[/PA2]\n", B_application/time_local);
	return 0;
}
Example #6
0
int _tmain(int argc, _TCHAR* argv[])
{
   struct event *eventptr;
   struct msg  msg2give;
   struct pkt  pkt2give;
   
   int i,j;
//   char c; 
  
   init();
   A_init();
   B_init();
   
   while (1) {
        eventptr = evlist;            /* get next event to simulate */
        if (eventptr==NULL)
           goto terminate;
        evlist = evlist->next;        /* remove this event from event list */
        if (evlist!=NULL)
           evlist->prev=NULL;
        if (TRACE>=2) {
           printf("\nEVENT time: %f,",eventptr->evtime);
           printf("  type: %d",eventptr->evtype);
           if (eventptr->evtype==0)
	       printf(", timerinterrupt  ");
             else if (eventptr->evtype==1)
               printf(", fromlayer5 ");
             else
	     printf(", fromlayer3 ");
           printf(" entity: %d\n",eventptr->eventity);
           }
        time = eventptr->evtime;        /* update time to next event time */
        if (nsim==nsimmax)
	  break;                        /* all done with simulation */
        if (eventptr->evtype == FROM_LAYER5 ) {
            generate_next_arrival();   /* set up future arrival */
            /* fill in msg to give with string of same letter */    
            j = nsim % 26; 
            for (i=0; i<20; i++)  
               msg2give.data[i] = 97 + j;
            if (TRACE>2) {
               printf("          MAINLOOP: data given to student: ");
                 for (i=0; i<20; i++) 
                  printf("%c", msg2give.data[i]);
               printf("\n");
	     }
            nsim++;
            if (eventptr->eventity == A) 
               A_output(msg2give);  
             else
               B_output(msg2give);  
            }
          else if (eventptr->evtype ==  FROM_LAYER3) {
            pkt2give.seqnum = eventptr->pktptr->seqnum;
            pkt2give.acknum = eventptr->pktptr->acknum;
            pkt2give.checksum = eventptr->pktptr->checksum;
            for (i=0; i<20; i++)  
                pkt2give.payload[i] = eventptr->pktptr->payload[i];
	    if (eventptr->eventity ==A)      /* deliver packet by calling */
   	       A_input(pkt2give);            /* appropriate entity */
            else
   	       B_input(pkt2give);
	    delete(eventptr->pktptr);          /* free the memory for packet */
            }
          else if (eventptr->evtype ==  TIMER_INTERRUPT) {
            if (eventptr->eventity == A) 
	       A_timerinterrupt();
             else
	       B_timerinterrupt();
             }
          else  {
	     printf("INTERNAL PANIC: unknown event type \n");
             }
        delete(eventptr);
        }

terminate:
   printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n",time,nsim);
   return 0;
}