root/test/spc/spc_test.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 "mpi.h"
  10 #include <stdio.h>
  11 #include <stdlib.h>
  12 #include <string.h>
  13 
  14 #define MAX_SIZE 1000000
  15 
  16 /* Sends 'num_messages' messages of 'message_size' bytes from rank 0 to rank 1.
  17  * All messages are sent synchronously and with the same tag in MPI_COMM_WORLD.
  18  */
  19 static void message_exchange(int num_messages, int message_size)
  20 {
  21     int i, rank;
  22     /* Use calloc to initialize data to 0's */
  23     char *data = (char*)calloc(message_size, sizeof(char));
  24     MPI_Status status;
  25 
  26     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  27 
  28     if(rank == 0) {
  29         for(i = 0; i < num_messages; i++)
  30             MPI_Send(data, message_size, MPI_BYTE, 1, 123, MPI_COMM_WORLD);
  31     } else if(rank == 1) {
  32         for(i = 0; i < num_messages; i++)
  33             MPI_Recv(data, message_size, MPI_BYTE, 0, 123, MPI_COMM_WORLD, &status);
  34     }
  35 
  36     free(data);
  37 }
  38 
  39 int main(int argc, char **argv)
  40 {
  41     int i, rank, size, provided, num, name_len, desc_len, verbosity, bind, var_class, readonly, continuous, atomic, count, index, MPI_result;
  42     MPI_Datatype datatype;
  43     MPI_T_enum enumtype;
  44     char name[256], description[256];
  45 
  46     /* Counter names to be read by ranks 0 and 1 */
  47     char *counter_names[] = { "runtime_spc_OMPI_BYTES_SENT_USER",
  48                               "runtime_spc_OMPI_BYTES_RECEIVED_USER" };
  49 
  50     MPI_Init(NULL, NULL);
  51     MPI_result = MPI_T_init_thread(MPI_THREAD_SINGLE, &provided);
  52     if(MPI_result != MPI_SUCCESS) {
  53         fprintf(stderr, "Failed to initialize MPI_T thread.\n");
  54         MPI_Abort(MPI_COMM_WORLD, MPI_result);
  55     }
  56 
  57     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  58     MPI_Comm_size(MPI_COMM_WORLD, &size);
  59     if(size != 2) {
  60         fprintf(stderr, "ERROR: This test should be run with two MPI processes.\n");
  61         MPI_Abort(MPI_COMM_WORLD, -1);
  62     }
  63 
  64     /* Determine the MPI_T pvar indices for the OMPI_BYTES_SENT/RECIEVED_USER SPCs */
  65     index = -1;
  66     MPI_result = MPI_T_pvar_get_num(&num);
  67     if(MPI_result != MPI_SUCCESS) {
  68         fprintf(stderr, "Failed to get the number of pvars.\n");
  69         MPI_Abort(MPI_COMM_WORLD, MPI_result);
  70     }
  71 
  72     for(i = 0; i < num; i++) {
  73         name_len = desc_len = 256;
  74         MPI_T_pvar_get_info(i, name, &name_len, &verbosity,
  75                              &var_class, &datatype, &enumtype, description, &desc_len, &bind,
  76                              &readonly, &continuous, &atomic);
  77         if(MPI_result != MPI_SUCCESS || MPI_result == MPI_T_ERR_PVAR_NO_STARTSTOP) {
  78             fprintf(stderr, "Failed to get pvar info.\n");
  79             MPI_Abort(MPI_COMM_WORLD, MPI_result);
  80         }
  81 
  82         if(strcmp(name, counter_names[rank]) == 0) {
  83             index = i;
  84             printf("[%d] %s -> %s\n", rank, name, description);
  85         }
  86     }
  87 
  88     /* Make sure we found the counters */
  89     if(index == -1) {
  90         fprintf(stderr, "ERROR: Couldn't find the appropriate SPC counter in the MPI_T pvars.\n");
  91         MPI_Abort(MPI_COMM_WORLD, -1);
  92     }
  93 
  94     long long value;
  95 
  96     MPI_T_pvar_session session;
  97     MPI_T_pvar_handle handle;
  98     /* Create the MPI_T sessions/handles for the counters and start the counters */
  99     MPI_result = MPI_T_pvar_session_create(&session);
 100     if(MPI_result != MPI_SUCCESS) {
 101         fprintf(stderr, "Failed to create MPI_T pvar session.\n");
 102         MPI_Abort(MPI_COMM_WORLD, MPI_result);
 103     }
 104 
 105     MPI_result = MPI_T_pvar_handle_alloc(session, index, NULL, &handle, &count);
 106     if(MPI_result != MPI_SUCCESS) {
 107         fprintf(stderr, "Failed to allocate the pvar handle.\n");
 108         MPI_Abort(MPI_COMM_WORLD, MPI_result);
 109     }
 110 
 111     MPI_result = MPI_T_pvar_start(session, handle);
 112     if(MPI_result != MPI_SUCCESS) {
 113         if(MPI_result != MPI_T_ERR_PVAR_NO_STARTSTOP) {
 114             fprintf(stderr, "Failed to start the pvar session.\n");
 115             MPI_Abort(MPI_COMM_WORLD, MPI_result);
 116         }
 117     }
 118 
 119     int message_size = 1, expected_bytes = 0;
 120     while(message_size <= MAX_SIZE) {
 121         expected_bytes += message_size;
 122         message_exchange(1, message_size);
 123         message_size *= 10;
 124     }
 125 
 126     MPI_result = MPI_T_pvar_read(session, handle, &value);
 127     if(MPI_result != MPI_SUCCESS) {
 128         fprintf(stderr, "Failed to read the pvar.\n");
 129         MPI_Abort(MPI_COMM_WORLD, MPI_result);
 130     }
 131 
 132     /* Print the counter values in order by rank */
 133     for(i = 0; i < 2; i++) {
 134         if(i == rank) {
 135             printf("[%d] Value Read: %lld\n", rank, value);
 136             fflush(stdout);
 137             if(value != expected_bytes){
 138                 fprintf(stderr, "The counter value is inaccurate!  It is '%lld'.  It should be '%d'\n", value, expected_bytes);
 139                 MPI_Abort(MPI_COMM_WORLD, MPI_ERR_OTHER);
 140             }
 141         }
 142         MPI_Barrier(MPI_COMM_WORLD);
 143     }
 144     /* Stop the MPI_T session, free the handle, and then free the session */
 145     MPI_result = MPI_T_pvar_stop(session, handle);
 146     if(MPI_result != MPI_SUCCESS) {
 147         if(MPI_result != MPI_T_ERR_PVAR_NO_STARTSTOP) {
 148             fprintf(stderr, "Failed to stop the pvar session.\n");
 149             MPI_Abort(MPI_COMM_WORLD, MPI_result);
 150         }
 151     }
 152 
 153     MPI_result = MPI_T_pvar_handle_free(session, &handle);
 154     if(MPI_result != MPI_SUCCESS) {
 155         fprintf(stderr, "Failed to free the pvar handle.\n");
 156         MPI_Abort(MPI_COMM_WORLD, MPI_result);
 157     }
 158 
 159     MPI_result = MPI_T_pvar_session_free(&session);
 160     if(MPI_result != MPI_SUCCESS) {
 161         fprintf(stderr, "Failed to free the pvar session.\n");
 162         MPI_Abort(MPI_COMM_WORLD, MPI_result);
 163     }
 164 
 165     MPI_result = MPI_T_finalize();
 166     if(MPI_result != MPI_SUCCESS) {
 167         fprintf(stderr, "Failed to finalize MPI_T.\n");
 168         MPI_Abort(MPI_COMM_WORLD, MPI_result);
 169     }
 170 
 171     MPI_Finalize();
 172 
 173     return EXIT_SUCCESS;
 174 }

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