malleabilityZombies.c 1.78 KB
Newer Older
Iker Martín's avatar
Iker Martín committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <mpi.h>
//#include <slurm/slurm.h>
#include <signal.h>
#include "../IOcodes/results.h"
#include "malleabilityZombies.h"

#define PIDS_QTY 320

void zombies_suspend();

int offset_pids, *pids = NULL;


void gestor_usr2() {}

void zombies_collect_suspended(MPI_Comm comm, int myId, int numP, int numC, int root, void *results_void, MPI_Comm user_comm) {
  int pid = getpid();
  int *pids_counts = malloc(numP * sizeof(int));
  int *pids_displs = malloc(numP * sizeof(int));
  int i, count=1;

  if(myId < numC) {
    count = 0;
    if(myId == root) {
      for(i=0; i < numC; i++) {
	pids_counts[i] = 0;
      }
      for(i=numC; i<numP; i++) {
  	pids_counts[i] = 1;
	pids_displs[i] = (i + offset_pids) - numC;
      }
      offset_pids += numP - numC;
      }
  }
  MPI_Gatherv(&pid, count, MPI_INT, pids, pids_counts, pids_displs, MPI_INT, root, comm);
  free(pids_counts);
  free(pids_displs);

  if(myId >= numC) {
    // Needed to ensure iteration times are collected before suspending these processes
    results_data *results = (results_data *) results_void;
    compute_results_iter(results, myId, root, user_comm); 
    zombies_suspend();
  }
}

void zombies_service_init() {
  offset_pids = 0;
  pids = malloc(PIDS_QTY * sizeof(int));

  for(int i=0; i<PIDS_QTY; i++) {
    pids[i] = 0;
  }
}

void zombies_service_free() {
  free(pids);
}

void zombies_suspend() {
  struct sigaction act;

  sigemptyset(&act.sa_mask);
  act.sa_flags=0;
  act.sa_handler=gestor_usr2;

  sigaction(SIGUSR2, &act, NULL);

  sigset_t set;
  sigprocmask(SIG_SETMASK,NULL,&set);

  sigsuspend(&set);
}

void zombies_awake() {
  for(int i=0; i < offset_pids; i++) { // Despertar a los zombies
    kill(pids[i], SIGUSR2);
  }
}