Beispiel #1
0
void helpmessage::show_help_message (string cmdValue)
{
  if (!cmdValue.compare("default") ||
      !cmdValue.compare("!help")) {
    cout<<"------------------------------------------------------------"<<endl;
    cout<<"   !help  --- shows this help message!"<<endl;
    cout<<"              example: !help cat"<<endl;
    cout<<"              -- shows the useage of the command cat"<<endl;
    cout<<"------------------------------------------------------------"<<endl;
    cout<<" only follow cmmand can be used:"<<endl;
    cout<<"------------------------------------------------------------"<<endl;
    cout<<"   ls     --- list the all objects in current";
    cout<<" level (directory)!"<<endl;
    cout<<"   cd     --- change into another sub level(directory)"<<endl;
    cout<<"   cat    --- shows the content of the items"<<endl;
    cout<<"   add    --- add an intems into the current level last node"<<endl;
    cout<<"   remove --- remove an items"<<endl;
    cout<<"   !quit  --- quit this program.." <<endl;
    cout<<"------------------------------------------------------------"<<endl;
  } else if (!cmdValue.compare("add")) {
    help_message_add();
  } else if (!cmdValue.compare("ls")) {
    help_message_ls();
  } else  if (!cmdValue.compare("remove")) {
    help_message_remove();
  } else if (!cmdValue.compare("cat")) {
    help_message_cat();
  } else if (!cmdValue.compare("cd")){
    help_message_cd();
  } else {
    show_help_message("default");
    return;
  }
}
Beispiel #2
0
int main(int argc, char** argv) {
    
    FILE *cpuFile = NULL;
    FILE *gpuFile = NULL;
    FILE *jetsonFile=NULL;
    FILE *hybridFile = NULL;
    FILE *tipoFile=NULL;
    FILE *tempiFile=NULL;
    FILE *datiFinali=NULL;
   
    
    int processID;                                                                  // ID del processo
    extern int processNumber;                                                       // numero dei processi coinvolti
    const int processRoot=0;                                                        // rank del processo padre
    struct custom_queue *main_queue = malloc(sizeof(struct custom_queue));          // istanzia in memoria spazio per la coda degli offset
    //extern unsigned long long int LENGTH_OFFSET_ARRAY;                              // lunghezza dell'array degli offset (MPI_Scatter)
    unsigned long long int OFFSET_ARRAY_CLIENT[2];                                  // Array di due elementi (offset inizio e offset fine) da inviare ai client)
    // OFFSET_ARRAY:= ARRAY CONTENTENTE GLI OFFSET
    MPI_Init(&argc, &argv);                                                         // Inizializzazione ambiente MPI
    MPI_Comm_size(MPI_COMM_WORLD, &processNumber);                                  // Numero totale dei processori
    MPI_Comm_rank(MPI_COMM_WORLD, &processID);                                      // Identifichiamo il processo corrente
    MESSAGE_SIZE = getMessageLenght(MESSAGE_CONST);                                 // inizializzo la lunghezza del messaggio
    
    /********************** LEGGO LE OPZIONI DELL'UTENTE **********************/
    //ora ci interessa solo sapere se l'utente ha specificato l'opzione -key
    //le altre opzioni verranno gestite più avanti (in base al rank del processo)
	
	int code = set_options(argc, argv);
	if(key_opt_set) 
		ENCRYPTION_KEY = user_key;
        
    /**************************************************************************/
    encryptMessage(MESSAGE_CONST, MESSAGE_SIZE, ENCRYPTION_KEY,&MODIFIED_MESSAGE);  //Questa funzione deve essere fatta da tutti i processi altrimente non vengono modificate le variabili.
    LENGTH_OFFSET_ARRAY=(processNumber)*2;
    
    //Struct che permette la comunicazione di dati eterogenei.
    int block[4]={1,1,1,1};
    MPI_Datatype types[4]={MPI_LONG_LONG,MPI_LONG_LONG,MPI_DOUBLE,MPI_INT};
    MPI_Aint disallineamentoBIT[4]={offsetof(infoExec,possibileChiave),offsetof(infoExec,processID),offsetof(infoExec,tempo),offsetof(infoExec,tipo)};
    MPI_Type_struct(4, block, disallineamentoBIT, types, &Info_Type);
    MPI_Type_commit(&Info_Type);
    MPI_Type_commit(&Info_Type);
    
    //Fine creazione della struct
    
    unsigned long long int OFFSET_ARRAY[LENGTH_OFFSET_ARRAY];
    
    //Gestione e analisi dei dati
    if(cpu_mode_set)
        cpuFile=fopen("cpuFile.txt","w");
    if(gpu_mode_set)
        gpuFile=fopen("gpuFile.txt","w");
    if(zcmem_opt_set)
        jetsonFile=fopen("jetsonFile.txt","w");
    if(hybrid_mode_set)
        hybridFile=fopen("hybridFile.txt","w");
    tipoFile=fopen("tipoFile.txt","w");
    tempiFile=fopen("tempiFile.txt","w");
    datiFinali=fopen("datiFinali.txt","w");
    //Fine gestione e analisi dei dati
    
   	if(processID==0){
	welcomeMessage(); 
       if(code == -1) {
            printf("\n\nLe opzioni specificate non sono corrette. Il programma terminera'.\n");
            MPI_Abort(MPI_COMM_WORLD, code);
        } 
       else if(code == 1) {
            show_help_message();
            MPI_Abort(MPI_COMM_WORLD, code);
        }
        if(verbose_set){
            printf(BLU"\n\n####### OPZIONI DI AVVIO #######"RESET);
            printf("\nVerbosita' ATTIVA.");
            /* CHECKING OPTIONS */
            if(cpu_mode_set) {
                printf("\nCPU_mode ATTIVA. ");
            }
            else if(gpu_mode_set){
                printf("\nGPU_mode ATTIVA. ");
            }
            else
                printf("\nHybrid_mode ATTIVA. ");
            if(zcmem_opt_set)
                printf("\nZero_Copy_Memory_option ATTIVA. ");
            
            printf("\n\nMessaggio in chiaro:"WHT" %s"RESET,MESSAGE_CONST);
            printf("\n\nChiave utilizzata per cifrare: "WHT"%llu"RESET,ENCRYPTION_KEY);
            printf(BLU"\n\n####### FINE OPZIONI DI AVVIO #######"RESET);
            fflush(stdout);
        }
        
        
        /* ----- Fase di precomputazione ----- */
        if(verbose_set){
            printf(BLU"\n\n####### INIZIO FASE DI PRECOMPUTAZIONE #######"RESET);
            printf("\n\nMessaggio cifrato ASCII:"WHT);
            
            for(int i=0;i<MESSAGE_SIZE;i++)
                printf(WHT"%c"RESET,MODIFIED_MESSAGE[i]);
            printf("\n\n");
            

            fflush(stdout);
        }
        
        initOffsetArray(OFFSET_ARRAY,LENGTH_OFFSET_ARRAY,START_SECOND_OFFSET);    //Inizializzo l'array con gli offset (dalla chiave 0 alla chiave 2^55 - 1)
        main_queue->empty_queue = true;                                           // inizializzazione della coda (dalla chiave 2^55 alla chiave 2^56 - 1)
        main_queue->head = NULL;                                                  //
        main_queue->tail = NULL;
        if(cpu_mode_set) {
            queue_init(main_queue, custom_pow(2,55));
        }
        else if(gpu_mode_set || hybrid_mode_set) {
            queue_init(main_queue, 0);
        }
        last_queue_elem = main_queue->tail->value;                                // inizializzo l'ultimo elemento della coda (variabile globale)
        /* ----- fine fase di precomputazione ----- */
        if(verbose_set){
            printf(BLU"######## FINE FASE DI PRECOMPUTAZIONE ########\n"RESET);
            fflush(stdout);
        }
    }
    if(verbose_set){
        printf("Distribuzione chiavi ai Client ... \n");
    }
    if(cpu_mode_set)
        MPI_Scatter(OFFSET_ARRAY, 2, MPI_UNSIGNED_LONG_LONG, OFFSET_ARRAY_CLIENT, 2, MPI_UNSIGNED_LONG_LONG,processRoot,MPI_COMM_WORLD);
    
// GESTIONE DEI TAG.
    if(processID==0) {
        long double TempoTotale=0.0;
        long long int chiaviProcessateCPU=0;
        long long int chiaviProcessateGPU=0;
        char * tipoElaboratore=malloc(sizeof(char)*4);
        infoExec client_data;// conserverà il messaggio inviato dal client (la chiave, se è stata trovata)
        while(true) {
            MPI_Status recv_status;                                     // struttura che contiene informazioni sul messaggio ricevuto
            MPI_Recv(&client_data, sizeof(Info_Type), Info_Type, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_status);   //ricezione messaggio
            if(client_data.tipo==0)
                strcpy(tipoElaboratore, "gpu");
            else
                strcpy(tipoElaboratore,"cpu");
            int recieved_tag = recv_status.MPI_TAG;                     // viene ricavato il tag del messaggio inviato
            int response_destination = recv_status.MPI_SOURCE;          // viene ricavato il rank del mittente del messaggio
            bool empty_queue = false;                                   // indica se la coda si è svuotata o no. Permette di uscire da while
            switch(recieved_tag) {                                      // in base al tag ricevuto, eseguiamo un'operazione differente
                case 0: {
                    full_decrypt(client_data.possibileChiave, MODIFIED_MESSAGE, MESSAGE_SIZE);
                    if(verbose_set){
                        printf("\n\n Tutti i Client hanno smesso di elaborare le chiavi e hanno rilasciato le risorse utilizzate. Il processo Server terminerà\n\n\n");
                    }
                    fflush(stdout);
                    fprintf(datiFinali,"TEMPO TOTALE D'ESECUZIONE: %Lf \n",TempoTotale);
                    fprintf(datiFinali,"CHIAVI ELABORATE DA CPU: %llu\n", chiaviProcessateCPU);
                    fprintf(datiFinali,"CHIAVI ELABORATE DA GPU: %llu\n", chiaviProcessateGPU);
                    fprintf(datiFinali,"NUMERO DI CHIAVI PROCESSATE: %llu\n", key_counter);
                    fprintf(datiFinali,"CHIAVE TROVATA: %llu\n", client_data.possibileChiave);
                    if(cpu_mode_set){
                        fflush(cpuFile);
                        fclose(cpuFile);
                    }
                    if(gpu_mode_set){
                        fflush(gpuFile);
                        fclose(gpuFile);
                    }
                    if(zcmem_opt_set){
                        fflush(jetsonFile);
                        fclose(jetsonFile);
                    }
                    if(hybrid_mode_set){
                        fflush(hybridFile);
                        fclose(hybridFile);
                    }
                    fclose(tipoFile);
                    fclose(tempiFile);
                    fclose(datiFinali);
                    
                    free_queue(main_queue);             // libero tutte le risorse allocate per la coda
                    MPI_Abort(MPI_COMM_WORLD, 0);
                    break;
                }
                case 1: {
                    long long int new_offset = custom_dequeue(main_queue);
                    if(new_offset == -2) {
                        printf("ERRORE: La coda e' vuota, impossibile eseguire dequeue.");                      // -2 è il valore di ritorno per quando la coda è vuota
                        exit(-2);
                    }
                    if(new_offset == -1) {
                        printf("\nLa coda degli offset e' vuota. Attendere la ricezione della chiave...\n");
                        empty_queue = true;
                        MPI_Send(&new_offset, 1, MPI_LONG_LONG, response_destination, new_offset_to_client_tag, MPI_COMM_WORLD);
                        break;
                    }
                    
                    
                    MPI_Send(&new_offset, 1, MPI_LONG_LONG, response_destination, new_offset_to_client_tag, MPI_COMM_WORLD);
                    long long int new_offset_enqueued = last_queue_elem + LENGTH_INTER_QUEUE;
                    if( new_offset_enqueued >= MAX_KEY || last_queue_elem == -1) // MAX_KEY = 2^56
                        new_offset_enqueued = -1;                  // se siamo arrivati all'offset 2^(56)-1 abbiamo finito gli offset e bsogna mettere -1 nella coda
                    custom_enqueue(new_offset_enqueued, main_queue);
                    TempoTotale+=client_data.tempo;
                    printf("\n"RED"Percentuale Completamento:"WHT" %.10f%c"RED ". Testate: "WHT"%llu"RED". Rimanenti: "WHT"%llu"RED". Elaborate dal nodo numero:"WHT"%llu - %s" RED". Tempo:"WHT"%f"RED". TOTALE:"WHT" %Lf"RESET, current_percentage,'%',key_counter,MAX_KEY-key_counter,client_data.processID,tipoElaboratore,client_data.tempo,TempoTotale);
                    fflush(stdout);
                    key_counter += client_data.possibileChiave;
                    double percentage =(key_counter*100)/(float)MAX_KEY;
                    current_percentage = percentage;
                    last_queue_elem = new_offset_enqueued;
                    if(client_data.tipo==0) //gpu
                        chiaviProcessateGPU+=client_data.possibileChiave;
                    else
                        chiaviProcessateCPU+=client_data.possibileChiave;
                    if(cpu_mode_set)
                        writeDataOnTxt(cpuFile, tipoFile, tempiFile, client_data.tipo, client_data.tempo, client_data.possibileChiave);
                    else if(gpu_mode_set){
                        writeDataOnTxt(gpuFile, tipoFile, tempiFile, client_data.tipo, client_data.tempo, client_data.possibileChiave);
                    }else if(hybrid_mode_set){
                        writeDataOnTxt(hybridFile, tipoFile, tempiFile, client_data.tipo, client_data.tempo, client_data.possibileChiave);
                    }
                    
                    break;
                    
                }
                case 2: {
                    key_counter += client_data.possibileChiave;
                    double percentage =(key_counter*100)/(float)MAX_KEY;
                    current_percentage = percentage;
                    TempoTotale+=client_data.tempo;
                    printf("\n"RED"Percentuale Completamento:"WHT" %.10f%c"RED ". Testate: "WHT"%llu"RED". Rimanenti: "WHT"%llu"RED". Elaborate dal nodo numero:"WHT"%llu - %s" RED". Tempo:"WHT"%f"RED". TOTALE:"WHT" %Lf"RESET, current_percentage,'%',key_counter,MAX_KEY-key_counter,client_data.processID,tipoElaboratore,client_data.tempo,TempoTotale);
                    fflush(stdout);
                    if(client_data.tipo==0) //gpu
                        chiaviProcessateGPU+=client_data.possibileChiave;
                    else
                        chiaviProcessateCPU+=client_data.possibileChiave;
                    if(cpu_mode_set)
                        writeDataOnTxt(cpuFile, tipoFile, tempiFile, client_data.tipo, client_data.tempo, client_data.possibileChiave);
                    else if(gpu_mode_set){
                        writeDataOnTxt(gpuFile, tipoFile, tempiFile, client_data.tipo, client_data.tempo, client_data.possibileChiave);
                    }else if(hybrid_mode_set){
                        writeDataOnTxt(hybridFile, tipoFile, tempiFile, client_data.tipo, client_data.tempo, client_data.possibileChiave);
                    }

                    break;
                }
                default: {
                    printf("\nIl tag ricevuto non e' valido.\n");
                }
            }
            if(empty_queue) {
                free_queue(main_queue);             // libero tutte le risorse allocate per la coda
                break;                              // usciamo dal ciclo se la coda è vuota
                
            }
        }
    }
    
    
    if(processID > 0){//Processo client. Inizio a processare la parte di chivi che mi interessa.
        if(cpu_mode_set){
            testOffset(OFFSET_ARRAY_CLIENT[0], OFFSET_ARRAY_CLIENT[1], &MODIFIED_MESSAGE, &SUCCESS, processID);
        }
#ifdef GPU_BY_SCRIPT
        if(gpu_mode_set){
            if(zcmem_opt_set){
		testOffsetGpuJetson(MODIFIED_MESSAGE, &SUCCESS, processID);
            }else{
                testOffsetGpu(&MODIFIED_MESSAGE, &SUCCESS, processID);
            }
        }
        
        if(hybrid_mode_set) {
            if(processID%2==1)
                testOffsetHybridCpu(&MODIFIED_MESSAGE, &SUCCESS, processID);
            else if(processID%2==0)
                if(zcmem_opt_set)
                    testOffsetGpuJetson(MODIFIED_MESSAGE, &SUCCESS, processID);
                else
                    testOffsetGpu(&MODIFIED_MESSAGE, &SUCCESS, processID);
                    
        }
        
#endif
        if(verbose_set){
            printf("Il processo %d ha smesso di elaborare.\n",processID);
        }
    }
 
    MPI_Finalize();
}
Beispiel #3
0
int argparser(int argc, char **argv,
              char **infile_value,
              char **outfile_value,
              char **config_file_value) {
    int i;
    int index;
    int c;
    int help_flag;
    int version_flag;

    help_flag = 0;
    version_flag = 0;
    *infile_value = NULL;
    *outfile_value = NULL;
    *config_file_value = NULL;
    opterr = 0;

    while ((c = getopt(argc, argv, "hvi:c:o:")) != -1)
        switch (c) {
            case 'h':
                help_flag = 1;
                break;
            case 'v':
                version_flag = 1;
                break;
            case 'i':
                *infile_value = optarg;
                break;
            case 'c':
                *config_file_value = optarg;
                break;
            case 'o':
                *outfile_value = optarg;
                break;
            case '?':
                if (optopt == 'i')
                    fprintf(stderr,
                            "Option -%c requires an argument (input tree file name).\n",
                            optopt);
                else if (optopt == 'c')
                    fprintf(stderr,
                            "Option -%c requires an argument (config file name).\n",
                            optopt);
                else if (optopt == 'o')
                    fprintf(stderr,
                            "Option -%c requires an argument (output tree file name).\n",
                            optopt);
                else if (isprint(optopt))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
                show_help_message();
                return 1;
            default:
                abort();
        }

    if (help_flag)
        show_help_message();

    if (version_flag)
        print_version();

    printf("%s", BAR);
    printf("[COMMAND USED]\n\n    ");
    for (i = 0; i < argc; i++) {
        printf("%s ", argv[i]);
    }
    printf("\n\n");

    if (*infile_value)
        printf("    -i   Input tree file:    %s\n", *infile_value);
    else {
        printf("\nERROR: -i \"input_tree_file\" must be specified\n");
        exit(EXIT_FAILURE);
    }

    if (*config_file_value)
        printf("    -c   Config file:        %s\n", *config_file_value);
    else {
        printf("\nERROR: \"-c config_file\" must be specified\n");
        exit(EXIT_FAILURE);
    }

    if (*outfile_value)
        printf("    -o   Output tree file:   %s\n\n", *outfile_value);
    else {
        printf("\nERROR: \"-o output_tree_filename\" must be specified\n");
        exit(EXIT_FAILURE);
    }

    for (index = optind; index < argc; index++)
        printf("Non-option argument %s\n", argv[index]);
    return 0;
}
/** \brief main loop to run tests.
 *
 * The tests being run are described in the documentation of this file.
 *
 * \return never returns, test loop runs ad infinitum. */
int main(void) {
   uint8_t switch_mask = PIN6_bm;

   int16_t position = 0;
   int16_t steps = 1000;
   uint16_t accel = 100;
   uint16_t decel = 100;
   uint16_t speed = 800;

   PS_t pressure_sensor;

   LA_t LA_needle;
   LA_t LA_ring;

   command_t command;
   uint16_t steps_left = 0;

   /* call all of the setup_* functions */
   cli();
   setup_clocks();
   setup_LEDs();
   setup_switches(switch_mask);
   setup_pressure_sensor(&pressure_sensor);
   setup_USART_BC();
   setup_linear_actuators(&LA_needle, &LA_ring);
   sei();

   /* shows the help menu */
   show_help_message();

   /* show the current state of the linear actuator */
   show_motor_data(position, accel, decel, speed, steps);

   while (1) {

      switch(command = parse_command(&steps, &accel, &decel, &speed)) {

         case STEP:
            LA_move(&LA_needle, steps, accel, decel, speed);
            LA_move(&LA_ring, steps, accel, decel, speed);
            position += steps;
            printf("\n\n");
            break;

         case MOVE:
            LA_move(&LA_needle, steps, accel, decel, speed);
            LA_move(&LA_ring, steps, accel, decel, speed);
            position += steps;
            printf("\n\n");
            break;

         case ACCEL:
         case DECEL:
         case SPEED:
            printf("\n\n");
            break;

         case REPEAT:
            LA_move(&LA_needle, steps, accel, decel, speed);
            LA_move(&LA_ring, steps, accel, decel, speed);
            position += steps;
            printf("\n\n");
            break;

         case HELP:
            show_help_message();
            break;

         case NONE:
            break;

         default:
            show_help_message();
            break;
      }

      if ((command != HELP) && (command != NONE)) {
         while (LA_get_motor_state(&LA_needle) != SM_STOP) {
            if (READ_SWITCHES & PIN6_bm) {
               LA_brake(&LA_needle);
               LA_brake(&LA_ring);
               printf("motors parked\n");
            }
            if (steps > 0) {
               steps_left = (int32_t)steps * SPR / LA_needle.pitch
                  - LA_needle.motor.speed_ramp.step_count;
            }
            else {
               steps_left = -1 * (int32_t)steps * SPR / LA_needle.pitch
                  - LA_needle.motor.speed_ramp.step_count;
            }
            printf("    Running... Steps Left: %d\n", steps_left);
            delay_ms(250);
         }

         printf("    Done with command\n");

         show_motor_data(position, accel, decel, speed, steps);
      }
   }//end while (1)
}