123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #include <mpi.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #define CHECKPOINT_FILE "checkpoint_file.txt"
- char buffer[100];
- char buffertime[100];
- char checkpointfile[100] = CHECKPOINT_FILE;
- int step_x;
- int step_y;
- int world_rank;
- void elapsedTimeToString(char *output, size_t size) {
- static clock_t start_time = 0;
- if (start_time == 0) {
- // Enregistrez le temps de démarrage lors de la première invocation
- start_time = clock();
- }
- // Calculez le temps écoulé depuis le démarrage du programme
- clock_t current_time = clock();
- double elapsed_time = ((double)(current_time - start_time)) / CLOCKS_PER_SEC;
- // Écrivez le temps écoulé dans la chaîne de texte
- snprintf(output, size, "Temps écoulé : %.2f secondes", elapsed_time);
- }
- // Fonction pour sauvegarder l'état (checkpoint)
- void save_checkpoint(int x, int y) {
- FILE *fp = fopen(checkpointfile, "w");
- if (fp == NULL) {
- perror("Erreur à l'ouverture du fichier pour sauvegarder le checkpoint");
- exit(EXIT_FAILURE);
- }
- fprintf(fp, "%d %d", x, y);
- fclose(fp);
- printf("Checkpoint sauvegardé dans %s\n", checkpointfile);
- }
- // Fonction pour restaurer l'état à partir du checkpoint
- void restore_checkpoint(int *x, int *y) {
- FILE *fp = fopen(checkpointfile, "r");
- if (fp != NULL) {
- // perror("Erreur à l'ouverture du fichier pour restaurer le checkpoint");
- // exit(EXIT_FAILURE);
- if (fscanf(fp, "%d %d", x, y) != 2) {
- fprintf(stderr, "Erreur de lecture dans le fichier de checkpoint\n");
- exit(EXIT_FAILURE);
- }
- fclose(fp);
- printf("Checkpoint restauré : x = %d, y = %d\n", *x, *y);
- } else {
- printf("Pas de fichier checkpoint \n");
- }
- }
- int writeToFile(const char *text) {
- FILE *file;
- // Ouverture du fichier en mode écriture (efface le contenu existant)
- file = fopen("/tmp/output.txt", "a");
- // Vérifier si l'ouverture du fichier a réussi
- if (file == NULL) {
- perror("Erreur lors de l'ouverture du fichier");
- return EXIT_FAILURE;
- }
- // Écriture dans le fichier
- fprintf(file, "rank %i, %s\n", world_rank, text);
- // Fermeture du fichier
- fclose(file);
- printf("Données écrites dans le fichier avec succès.\n");
- return EXIT_SUCCESS;
- }
- void handle_sigterm(int signum) {
- printf("Received SIGTERM (%d).\n", signum);
- snprintf(buffer, sizeof(buffer), "Received SIGTERM (%d).\n", signum);
- writeToFile(buffer);
- elapsedTimeToString(buffertime, sizeof(buffertime));
- writeToFile(buffertime);
- // Ajoutez ici le code de nettoyage ou de gestion du signal SIGKILL
- // Exemple : Fermez les fichiers, libérez la mémoire, etc.
- // ...
- save_checkpoint(step_x, step_y);
- // Terminez proprement le programme
- exit(EXIT_SUCCESS);
- }
- int write_output_with_time(const char *prefix) {
- int error = EXIT_SUCCESS;
- printf("%s\n", prefix);
- snprintf(buffer, sizeof(buffer), "%s\n", prefix);
- error = writeToFile(buffer);
- if (error != EXIT_FAILURE) {
- elapsedTimeToString(buffertime, sizeof(buffertime));
- error = writeToFile(buffertime);
- }
- return error;
- }
- void handle_sigint(int signum) {
- char bufflocal[100];
- int error = EXIT_SUCCESS;
- snprintf(bufflocal, sizeof(bufflocal), "Received SIGINT (%d).\n", signum);
- error = write_output_with_time(bufflocal);
- exit(error);
- }
- int main(int argc, char **argv) {
- // Installez le gestionnaire de signal pour SIGKILL
- elapsedTimeToString(buffertime, sizeof(buffertime));
- if (signal(SIGTERM, handle_sigterm) == SIG_ERR) {
- perror("Erreur lors de l'installation du gestionnaire de signal");
- return EXIT_FAILURE;
- }
- if (signal(SIGINT, handle_sigint) == SIG_ERR) {
- perror(
- "Erreur lors de l'installation du gestionnaire de signal pour SIGINT");
- return EXIT_FAILURE;
- }
- MPI_Init(&argc, &argv);
- // Get the number of processes
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- // Get the rank of the process
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- sprintf(checkpointfile, "%s.%i", checkpointfile, world_rank);
- // Get the name of the processor
- char processor_name[MPI_MAX_PROCESSOR_NAME];
- int name_len;
- MPI_Get_processor_name(processor_name, &name_len);
- step_x = 0;
- step_y = 0;
- restore_checkpoint(&step_x, &step_y);
- // Print off a hello world message
- printf("Hello world from processor %s, rank %d out of %d processors\n",
- processor_name, world_rank, world_size);
- printf("Le programme est en cours d'exécution. PID: %d\n", getpid());
- int counter = 0;
- // Simulation d'une tâche en cours d'exécution
- while (counter < 5) {
- // Votre code ici...
- step_x++;
- sleep(2);
- step_y++;
- // Ajoutez une pause pour éviter une utilisation intensive du processeur
- sleep(1);
- counter++;
- }
- MPI_Finalize();
- write_output_with_time("Réussite du traitement");
- return EXIT_SUCCESS;
- }
|