diff options
| author | 2026-11-10 08:29:58 +0000 | |
|---|---|---|
| committer | 2026-11-10 08:33:56 +0000 | |
| commit | 776276d5c7e67356a880f3e31076d32cdc1c1882 (patch) | |
| tree | cbde131f3d46338adee9c67ac0f988295736bdd4 /test.c | |
| download | coursework-776276d5c7e67356a880f3e31076d32cdc1c1882.tar.gz | |
Diffstat (limited to 'test.c')
| -rw-r--r-- | test.c | 92 |
1 files changed, 92 insertions, 0 deletions
| @@ -0,0 +1,92 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdarg.h> | ||
| 4 | #include <mpi.h> | ||
| 5 | |||
| 6 | #define STRINGIFY(ARG) #ARG | ||
| 7 | #define TO_STRING(ARG) STRINGIFY (ARG) | ||
| 8 | |||
| 9 | const char *mpi_strerror (int errorcode) | ||
| 10 | { | ||
| 11 | static char string[MPI_MAX_ERROR_STRING]; | ||
| 12 | MPI_Error_string (errorcode, string, NULL); | ||
| 13 | return string; | ||
| 14 | } | ||
| 15 | |||
| 16 | void | ||
| 17 | die (const char *fmt, ...) | ||
| 18 | { | ||
| 19 | va_list ap; | ||
| 20 | |||
| 21 | va_start (ap, fmt); | ||
| 22 | vfprintf (stderr, fmt, ap); | ||
| 23 | va_end (ap); | ||
| 24 | |||
| 25 | exit (EXIT_FAILURE); | ||
| 26 | } | ||
| 27 | |||
| 28 | #define DIE(FMT, ...) \ | ||
| 29 | die (__FILE__ ":" TO_STRING (__LINE__) ": %s: die: " FMT, \ | ||
| 30 | __func__ __VA_OPT__ (,) __VA_ARGS__) | ||
| 31 | |||
| 32 | int world_size; | ||
| 33 | int world_rank; | ||
| 34 | char processor_name[MPI_MAX_PROCESSOR_NAME]; | ||
| 35 | int name_len; | ||
| 36 | |||
| 37 | void | ||
| 38 | mpi_init (int *argc, char ***argv) | ||
| 39 | { | ||
| 40 | int err = MPI_Init (argc, argv); | ||
| 41 | if (err) DIE ("MPI_Init: %s", mpi_strerror (err)); | ||
| 42 | } | ||
| 43 | |||
| 44 | int | ||
| 45 | mpi_world_size (void) | ||
| 46 | { | ||
| 47 | int world_size; | ||
| 48 | int err = MPI_Comm_size (MPI_COMM_WORLD, &world_size); | ||
| 49 | if (err) DIE ("MPI_Comm_size: %s", mpi_strerror (err)); | ||
| 50 | return world_size; | ||
| 51 | } | ||
| 52 | |||
| 53 | int | ||
| 54 | mpi_world_rank (void) | ||
| 55 | { | ||
| 56 | int world_rank; | ||
| 57 | int err = MPI_Comm_rank (MPI_COMM_WORLD, &world_rank); | ||
| 58 | if (err) DIE ("MPI_Comm_rank: %s", mpi_strerror (err)); | ||
| 59 | return world_rank; | ||
| 60 | } | ||
| 61 | |||
| 62 | void | ||
| 63 | mpi_get_processor_name (char processor_name[MPI_MAX_PROCESSOR_NAME], | ||
| 64 | int *name_len) | ||
| 65 | { | ||
| 66 | int err = MPI_Get_processor_name (processor_name, name_len); | ||
| 67 | if (err) DIE ("MPI_Get_processor_name: %s", mpi_strerror (err)); | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | mpi_finalize (void) | ||
| 72 | { | ||
| 73 | int err = MPI_Finalize (); | ||
| 74 | if (err) | ||
| 75 | DIE ("MPI_Finalize: %s", mpi_strerror (err)); | ||
| 76 | } | ||
| 77 | |||
| 78 | int | ||
| 79 | main (int argc, char *argv[argc]) | ||
| 80 | { | ||
| 81 | mpi_init (&argc, &argv); | ||
| 82 | world_size = mpi_world_size (); | ||
| 83 | world_rank = mpi_world_rank (); | ||
| 84 | mpi_get_processor_name (processor_name, &name_len); | ||
| 85 | |||
| 86 | printf ("Hello from processor %s, rank %d out of %d processors\n", | ||
| 87 | processor_name, world_rank, world_size); | ||
| 88 | |||
| 89 | mpi_finalize (); | ||
| 90 | |||
| 91 | return 0; | ||
| 92 | } | ||
