Browse Source

evolution checkpoint avec reprise

Jean-Michel Batto 2 tháng trước cách đây
mục cha
commit
4d9783a31f
6 tập tin đã thay đổi với 66 bổ sung35 xóa
  1. 4 3
      step3/Makefile
  2. 1 0
      step3/checkpoint_file.txt.0
  3. 1 0
      step3/checkpoint_file.txt.1
  4. BIN
      step3/elementary
  5. 60 32
      step3/elementary.c
  6. BIN
      step3/elementary.o

+ 4 - 3
step3/Makefile

@@ -1,5 +1,6 @@
 CC=gcc
-XMPCC=xmpcc
+# XMPCC=xmpcc
+XMPCC=gcc
 LIBS=-L/usr/lib -lmpi
 INCL=-I /usr/lib/openmpi/include
 
@@ -14,8 +15,8 @@ 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
+	mpirun --mca orte_base_help_agregate 0 -host mpihead,mpinode1 -n 2 /usr/local/var/mpishare/glcs_slurm/step3/elementary
 	 
 
 clean:
-	rm *.o elementary
+	rm *.o elementary checkpoint_file.txt.* /tmp/output.txt

+ 1 - 0
step3/checkpoint_file.txt.0

@@ -0,0 +1 @@
+4 3

+ 1 - 0
step3/checkpoint_file.txt.1

@@ -0,0 +1 @@
+5 4

BIN
step3/elementary


+ 60 - 32
step3/elementary.c

@@ -2,11 +2,17 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <unistd.h>
-#define CHECKPOINT_FILE "checkpoint.txt"
+#define CHECKPOINT_FILE "checkpoint_file.txt"
 char buffer[100];
-char checkpointfile[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;
 
@@ -37,17 +43,22 @@ void save_checkpoint(int x, int y) {
 // 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 (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");
   }
-  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;
 
@@ -57,47 +68,58 @@ int writeToFile(const char *text) {
   // Vérifier si l'ouverture du fichier a réussi
   if (file == NULL) {
     perror("Erreur lors de l'ouverture du fichier");
-    return 1;
+    return EXIT_FAILURE;
   }
 
   // Écriture dans le fichier
-  fprintf(file, "%s", text);
+  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 0;
+  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(buffer, sizeof(buffer));
-  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) {
-  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);
+  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 main(int argc, char **argv) {
   // Installez le gestionnaire de signal pour SIGKILL
-  elapsedTimeToString(buffer, sizeof(buffer));
+  elapsedTimeToString(buffertime, sizeof(buffertime));
   if (signal(SIGTERM, handle_sigterm) == SIG_ERR) {
     perror("Erreur lors de l'installation du gestionnaire de signal");
     return EXIT_FAILURE;
@@ -107,34 +129,40 @@ int main() {
         "Erreur lors de l'installation du gestionnaire de signal pour SIGINT");
     return EXIT_FAILURE;
   }
-  MPI_Init(NULL, NULL);
+  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
-  int world_rank;
+
   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
 
-  sprintf(checkpointfile, "output.txt.%i", 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 (1) {
+  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;
 }

BIN
step3/elementary.o