root/examples/spc_example.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. message_exchange
  2. main

   1 /*
   2  * Copyright (c) 2018      The University of Tennessee and The University
   3  *                         of Tennessee Research Foundation.  All rights
   4  *                         reserved.
   5  *
   6  * Simple example usage of SPCs through MPI_T.
   7  */
   8 
   9 #include <stdio.h>
  10 #include <stdlib.h>
  11 #include <string.h>
  12 
  13 #include "mpi.h"
  14 
  15 /* Sends 'num_messages' messages of 'message_size' bytes from rank 0 to rank 1.
  16  * All messages are send synchronously and with the same tag in MPI_COMM_WORLD.
  17  */
  18 void message_exchange(int num_messages, int message_size)
  19 {
  20     int i, rank;
  21     /* Use calloc to initialize data to 0's */
  22     char *data = (char*)calloc(message_size, sizeof(char));
  23     MPI_Status status;
  24 
  25     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  26 
  27     if(rank == 0) {
  28         for(i = 0; i < num_messages; i++)
  29             MPI_Send(data, message_size, MPI_BYTE, 1, 123, MPI_COMM_WORLD);
  30     } else if(rank == 1) {
  31         for(i = 0; i < num_messages; i++)
  32             MPI_Recv(data, message_size, MPI_BYTE, 0, 123, MPI_COMM_WORLD, &status);
  33     }
  34 
  35     free(data);
  36 }
  37 
  38 int main(int argc, char **argv)
  39 {
  40     int num_messages, message_size;
  41 
  42     if(argc < 3) {
  43         printf("Usage: mpirun -np 2 --mca mpi_spc_attach all --mca mpi_spc_dump_enabled true ./spc_example [num_messages] [message_size]\n");
  44         return -1;
  45     } else {
  46         num_messages = atoi(argv[1]);
  47         message_size = atoi(argv[2]);
  48     }
  49 
  50     int i, rank, size, provided, num, name_len, desc_len, verbosity, bind, var_class, readonly, continuous, atomic, count, index;
  51     MPI_Datatype datatype;
  52     MPI_T_enum enumtype;
  53     MPI_Comm comm;
  54     char name[256], description[256];
  55 
  56     /* Counter names to be read by ranks 0 and 1 */
  57     char *counter_names[] = {"runtime_spc_OMPI_BYTES_SENT_USER",
  58                              "runtime_spc_OMPI_BYTES_RECEIVED_USER" };
  59 
  60     MPI_Init(NULL, NULL);
  61     MPI_T_init_thread(MPI_THREAD_SINGLE, &provided);
  62 
  63     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  64     MPI_Comm_size(MPI_COMM_WORLD, &size);
  65     if(size != 2) {
  66         fprintf(stderr, "ERROR: This test should be run with two MPI processes.\n");
  67         MPI_Abort(MPI_COMM_WORLD, -1);
  68     }
  69 
  70     /* Determine the MPI_T pvar indices for the OMPI_BYTES_SENT/RECIEVED_USER SPCs */
  71     index = -1;
  72     MPI_T_pvar_get_num(&num);
  73     for(i = 0; i < num; i++) {
  74         name_len = desc_len = 256;
  75         PMPI_T_pvar_get_info(i, name, &name_len, &verbosity,
  76                              &var_class, &datatype, &enumtype, description, &desc_len, &bind,
  77                              &readonly, &continuous, &atomic);
  78         if(strcmp(name, counter_names[rank]) == 0) {
  79             index = i;
  80             printf("[%d] %s -> %s\n", rank, name, description);
  81         }
  82     }
  83 
  84     /* Make sure we found the counters */
  85     if(index == -1) {
  86         fprintf(stderr, "ERROR: Couldn't find the appropriate SPC counter in the MPI_T pvars.\n");
  87         MPI_Abort(MPI_COMM_WORLD, -1);
  88     }
  89 
  90     int ret;
  91     long long value;
  92 
  93     MPI_T_pvar_session session;
  94     MPI_T_pvar_handle handle;
  95     /* Create the MPI_T sessions/handles for the counters and start the counters */
  96     ret = MPI_T_pvar_session_create(&session);
  97     ret = MPI_T_pvar_handle_alloc(session, index, NULL, &handle, &count);
  98     ret = MPI_T_pvar_start(session, handle);
  99 
 100     message_exchange(num_messages, message_size);
 101 
 102     ret = MPI_T_pvar_read(session, handle, &value);
 103     /* Print the counter values in order by rank */
 104     for(i = 0; i < 2; i++) {
 105         if(i == rank) {
 106             printf("[%d] Value Read: %lld\n", rank, value);
 107             fflush(stdout);
 108         }
 109         MPI_Barrier(MPI_COMM_WORLD);
 110     }
 111     /* Stop the MPI_T session, free the handle, and then free the session */
 112     ret = MPI_T_pvar_stop(session, handle);
 113     ret = MPI_T_pvar_handle_free(session, &handle);
 114     ret = MPI_T_pvar_session_free(&session);
 115 
 116     MPI_T_finalize();
 117     MPI_Finalize();
 118 
 119     return 0;
 120 }

/* [<][>][^][v][top][bottom][index][help] */