Browse Source

init step2 checkpoint

Jean-Michel Batto 2 months ago
parent
commit
35cc521ed5
4 changed files with 134 additions and 90 deletions
  1. 21 0
      step3/Makefile
  2. BIN
      step3/elementary
  3. 113 90
      step3/elementary.c
  4. BIN
      step3/elementary.o

+ 21 - 0
step3/Makefile

@@ -0,0 +1,21 @@
+CC=gcc
+XMPCC=xmpcc
+LIBS=-L/usr/lib -lmpi
+INCL=-I /usr/lib/openmpi/include
+
+OBJP=elementary.o
+
+all: elementary
+
+elementary.o: elementary.c
+	$(XMPCC) -c elementary.c
+
+elementary: $(OBJP)
+	$(XMPCC) -o elementary $(OBJP) $(LIBS)
+
+run: elementary 
+	mpirun --mca orte_base_help_agregate 0 -host mpihead -n 1 /usr/local/var/mpishare/glcs_slurm/step3/elementary
+	 
+
+clean:
+	rm *.o elementary

BIN
step3/elementary


+ 113 - 90
step3/elementary.c

@@ -1,117 +1,140 @@
 #include <mpi.h>
-#include <stdio.h>
 #include <signal.h>
-#include <unistd.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
-
+#include <unistd.h>
+#define CHECKPOINT_FILE "checkpoint.txt"
 char buffer[100];
-
-
+char checkpointfile[100];
 void elapsedTimeToString(char *output, size_t size) {
-    static clock_t start_time = 0;
+  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();
-    }
+  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;
+  // 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);
+  // É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);
+}
+int writeToFile(const char *text) {
+  FILE *file;
 
-int writeToFile(const char* text) {
-    FILE *file;
-
-    // Ouverture du fichier en mode écriture (efface le contenu existant)
-    file = fopen("/tmp/output.txt", "a");
+  // 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 1;
-    }
+  // Vérifier si l'ouverture du fichier a réussi
+  if (file == NULL) {
+    perror("Erreur lors de l'ouverture du fichier");
+    return 1;
+  }
 
-    // Écriture dans le fichier
-    fprintf(file, "%s", text);
+  // Écriture dans le fichier
+  fprintf(file, "%s", text);
 
-    // Fermeture du fichier
-    fclose(file);
+  // Fermeture du fichier
+  fclose(file);
 
-    printf("Données écrites dans le fichier avec succès.\n");
+  printf("Données écrites dans le fichier avec succès.\n");
 
-    return 0;
+  return 0;
 }
 
-
 void handle_sigterm(int signum) {
-    printf("Received SIGTERM (%d).\n", signum);
-    snprintf(buffer,sizeof(buffer), "Received SIGTERM (%d).\n",signum);
-    writeToFile(buffer);
-    elapsedTimeToString(buffer, sizeof(buffer));
-    writeToFile(buffer);    
-    // Ajoutez ici le code de nettoyage ou de gestion du signal SIGKILL
-
-    // Exemple : Fermez les fichiers, libérez la mémoire, etc.
-    // ...
-
-    // Terminez proprement le programme
-    exit(EXIT_SUCCESS);
+  printf("Received SIGTERM (%d).\n", signum);
+  snprintf(buffer, sizeof(buffer), "Received SIGTERM (%d).\n", signum);
+  writeToFile(buffer);
+  elapsedTimeToString(buffer, sizeof(buffer));
+  writeToFile(buffer);
+  // Ajoutez ici le code de nettoyage ou de gestion du signal SIGKILL
+
+  // Exemple : Fermez les fichiers, libérez la mémoire, etc.
+  // ...
+
+  // Terminez proprement le programme
+  exit(EXIT_SUCCESS);
 }
 
 void handle_sigint(int signum) {
-    printf("Received SIGINT (%d).\n", signum);
-    snprintf(buffer,sizeof(buffer), "Received SIGINT (%d).\n",signum);
-    writeToFile(buffer);
-    elapsedTimeToString(buffer, sizeof(buffer));
-    writeToFile(buffer);
-    exit(EXIT_SUCCESS); 
+  printf("Received SIGINT (%d).\n", signum);
+  snprintf(buffer, sizeof(buffer), "Received SIGINT (%d).\n", signum);
+  writeToFile(buffer);
+  elapsedTimeToString(buffer, sizeof(buffer));
+  writeToFile(buffer);
+  exit(EXIT_SUCCESS);
 }
 
 int main() {
-    // Installez le gestionnaire de signal pour SIGKILL
-    elapsedTimeToString(buffer, sizeof(buffer));
-    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(NULL, NULL);
-
-    // Get the number of processes
-    int world_size;
-    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
-
-    // Get the rank of the process
-    int world_rank;
-    MPI_Comm_rank(MPI_COMM_WORLD, &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);
-
-    // 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());
-
-    // Simulation d'une tâche en cours d'exécution
-    while (1) {
-        // Votre code ici...
-
-        // Ajoutez une pause pour éviter une utilisation intensive du processeur
-        sleep(1);
-    }
-    MPI_Finalize();
-    return EXIT_SUCCESS;
+  // Installez le gestionnaire de signal pour SIGKILL
+  elapsedTimeToString(buffer, sizeof(buffer));
+  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(NULL, NULL);
+
+  // Get the number of processes
+  int world_size;
+  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
+
+  // Get the rank of the process
+  int world_rank;
+  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
+
+  sprintf(checkpointfile, "output.txt.%i", 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);
+
+  // 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());
+
+  // Simulation d'une tâche en cours d'exécution
+  while (1) {
+    // Votre code ici...
+
+    // Ajoutez une pause pour éviter une utilisation intensive du processeur
+    sleep(1);
+  }
+  MPI_Finalize();
+  return EXIT_SUCCESS;
 }
-

BIN
step3/elementary.o