aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/gnunet_mpi_test.c
blob: fded9e2c28ff7c152345529fe1bfea2386763ea6 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "platform.h"
#include "gnunet_util_lib.h"
#include <mpi.h>

/**
 * Generic logging shorthand
 */
#define LOG(kind,...)                                           \
  GNUNET_log_from (kind, "gnunet-mpi-test", __VA_ARGS__)

int
main (int argc, char *argv[])
{
  char *msg;
  char *filename;
  char **argv2;
  struct GNUNET_OS_Process *proc;
  unsigned long code;
  pid_t pid;
  enum GNUNET_OS_ProcessStatusType proc_status;
  int ntasks;
  int rank;
  int msg_size;
  int ret;
  unsigned int cnt;

  ret = GNUNET_SYSERR;
  if (argc < 2)
  {
    printf ("Need arguments: gnunet-mpi-test <cmd> <cmd_args>");
    return 1;
  }
  if (MPI_SUCCESS != MPI_Init (&argc, &argv))
  {
    GNUNET_break (0);
    return 1;
  }
  if (MPI_SUCCESS != MPI_Comm_size (MPI_COMM_WORLD, &ntasks))
  {
    GNUNET_break (0);
    goto finalize;
  }
  if (MPI_SUCCESS != MPI_Comm_rank (MPI_COMM_WORLD, &rank))
  {
    GNUNET_break (0);
    goto finalize;
  }
  pid = getpid ();
  (void) GNUNET_asprintf (&filename, "%d-%d.mpiout", (int) pid, rank);
  msg_size = GNUNET_asprintf (&msg, "My rank is: %d\n", rank);
  printf ("%s", msg);
  if (msg_size ==
      GNUNET_DISK_fn_write (filename, msg, msg_size,
                            GNUNET_DISK_PERM_USER_READ |
                            GNUNET_DISK_PERM_GROUP_READ |
                            GNUNET_DISK_PERM_USER_WRITE |
                            GNUNET_DISK_PERM_GROUP_WRITE))
    ret = GNUNET_OK;
  GNUNET_free (filename);
  GNUNET_free (msg);
  if (GNUNET_OK != ret)
  {
    GNUNET_break (0);
    goto finalize;
  }

  ret = GNUNET_SYSERR;
  argv2 = GNUNET_malloc (sizeof (char *) * (argc));
  for (cnt = 1; cnt < argc; cnt++)
    argv2[cnt - 1] = argv[cnt];
  proc =
      GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
                                   NULL, argv2[0], argv2);
  if (NULL == proc)
  {
    printf ("Cannot exec\n");
    GNUNET_free (argv2);
    goto finalize;
  }
  do
  {
    (void) sleep (1);
    ret = GNUNET_OS_process_status (proc, &proc_status, &code);
  }
  while (GNUNET_NO == ret);
  GNUNET_free (argv2);
  GNUNET_assert (GNUNET_NO != ret);
  if (GNUNET_OK == ret)
  {
    if (0 != code)
    {
      LOG (GNUNET_ERROR_TYPE_WARNING, "Child terminated abnormally\n");
      ret = GNUNET_SYSERR;
      GNUNET_break (0);
      goto finalize;
    }
  }
  else
    GNUNET_break (0);

finalize:
  (void) MPI_Finalize ();
  if (GNUNET_OK == ret)
    return 0;
  printf ("Something went wrong\n");
  return 1;
}