GNUnet  0.11.x
gnunet_mpi_test.c
Go to the documentation of this file.
1 #include "platform.h"
2 #include "gnunet_util_lib.h"
3 #include <mpi.h>
4 
8 #define LOG(kind, ...) \
9  GNUNET_log_from (kind, "gnunet-mpi-test", __VA_ARGS__)
10 
11 int
12 main (int argc, char *argv[])
13 {
14  char *msg;
15  char *filename;
16  char **argv2;
17  struct GNUNET_OS_Process *proc;
18  unsigned long code;
19  pid_t pid;
20  enum GNUNET_OS_ProcessStatusType proc_status;
21  int ntasks;
22  int rank;
23  int msg_size;
24  int ret;
25  unsigned int cnt;
26 
27  ret = GNUNET_SYSERR;
28  if (argc < 2)
29  {
30  printf ("Need arguments: gnunet-mpi-test <cmd> <cmd_args>");
31  return 1;
32  }
33  if (MPI_SUCCESS != MPI_Init (&argc, &argv))
34  {
35  GNUNET_break (0);
36  return 1;
37  }
38  if (MPI_SUCCESS != MPI_Comm_size (MPI_COMM_WORLD, &ntasks))
39  {
40  GNUNET_break (0);
41  goto finalize;
42  }
43  if (MPI_SUCCESS != MPI_Comm_rank (MPI_COMM_WORLD, &rank))
44  {
45  GNUNET_break (0);
46  goto finalize;
47  }
48  pid = getpid ();
49  (void) GNUNET_asprintf (&filename, "%d-%d.mpiout", (int) pid, rank);
50  msg_size = GNUNET_asprintf (&msg, "My rank is: %d\n", rank);
51  printf ("%s", msg);
52  if (msg_size ==
53  GNUNET_DISK_fn_write (filename, msg, msg_size,
58  ret = GNUNET_OK;
59  GNUNET_free (filename);
60  GNUNET_free (msg);
61  if (GNUNET_OK != ret)
62  {
63  GNUNET_break (0);
64  goto finalize;
65  }
66 
67  ret = GNUNET_SYSERR;
68  argv2 = GNUNET_malloc (sizeof(char *) * (argc));
69  for (cnt = 1; cnt < argc; cnt++)
70  argv2[cnt - 1] = argv[cnt];
71  proc =
73  NULL, NULL, argv2[0], argv2);
74  if (NULL == proc)
75  {
76  printf ("Cannot exec\n");
77  GNUNET_free (argv2);
78  goto finalize;
79  }
80  do
81  {
82  (void) sleep (1);
83  ret = GNUNET_OS_process_status (proc, &proc_status, &code);
84  }
85  while (GNUNET_NO == ret);
86  GNUNET_free (argv2);
87  GNUNET_assert (GNUNET_NO != ret);
88  if (GNUNET_OK == ret)
89  {
90  if (0 != code)
91  {
92  LOG (GNUNET_ERROR_TYPE_WARNING, "Child terminated abnormally\n");
93  ret = GNUNET_SYSERR;
94  GNUNET_break (0);
95  goto finalize;
96  }
97  }
98  else
99  GNUNET_break (0);
100 
101 finalize:
102  (void) MPI_Finalize ();
103  if (GNUNET_OK == ret)
104  return 0;
105  printf ("Something went wrong\n");
106  return 1;
107 }
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:604
int GNUNET_OS_process_status(struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
Retrieve the status of a process.
Definition: os_priority.c:946
int main(int argc, char *argv[])
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
ssize_t GNUNET_DISK_fn_write(const char *fn, const void *buffer, size_t n, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file.
Definition: disk.c:879
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Use this option to have all of the standard streams (stdin, stdout and stderror) be inherited...
GNUNET_OS_ProcessStatusType
Process status types.
static char * filename
static char ** argv2
The arguments including the binary to spawn.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
#define LOG(kind,...)
Generic logging shorthand.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.