Pakman
mpi-simulator.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <mpi.h>
5 
6 #include "pakman_mpi_worker.h"
7 
8 /* Define my_simulator */
9 int my_simulator(int argc, char *argv[],
10  const char* input_string, char **p_output_string)
11 {
12  /* Default output string and error code correspond to simulator that always
13  * accepts and exits without error */
14  char *output_string = "accept\n";
15  int error_code = 0;
16 
17  /* Print help */
18  if (argc == 2 &&
19  ( strcmp(argv[1], "--help") == 0
20  || strcmp(argv[1], "-h") == 0 ) )
21  {
22  printf("Usage: %s [OUTPUT_STRING] [ERROR_CODE]\n", argv[0]);
23  return 0;
24  }
25 
26  /* Process given output string */
27  int malloced = 0;
28  if (argc >= 2)
29  {
30  output_string = argv[1];
31 
32  /* If output string does not terminate on newline, add one */
33  size_t len = strlen(output_string);
34  if (output_string[len - 1] != '\n')
35  {
36  output_string = (char *) malloc((len + 2) * sizeof(char));
37  strcpy(output_string, argv[1]);
38  output_string[len] = '\n';
39  output_string[len + 1] = '\0';
40  malloced = 1;
41  }
42  }
43 
44  /* Process given error code */
45  if (argc >= 3)
46  error_code = atoi(argv[2]);
47 
48  /* Throw error if more than two arguments are given */
49  if (argc > 3)
50  {
51  fprintf(stderr, "Error: too many arguments given. Try %s --help.",
52  argv[0]);
53  return 2;
54  }
55 
56  /* Allocate memory for output_string */
57  *p_output_string = malloc((strlen(output_string) + 1) * sizeof(char));
58 
59  /* Copy string */
60  strcpy(*p_output_string, output_string);
61 
62  /* Free memory if malloced */
63  if (malloced)
64  free(output_string);
65 
66  /* Return exit code */
67  return error_code;
68 }
69 
70 int main(int argc, char *argv[])
71 {
72  /* Initialize MPI */
73  MPI_Init(NULL, NULL);
74 
75  /* Run MPI Worker */
76  pakman_run_mpi_worker(argc, argv, &my_simulator);
77 
78  /* Finalize MPI */
79  MPI_Finalize();
80 
81  return 0;
82 }
int pakman_run_mpi_worker(int argc, char *argv[], int(*simulator)(int argc, char *argv[], const char *input_string, char **p_output_string))
int my_simulator(int argc, char *argv[], const std::string &input_string, std::string &output_string)