Can't seem to get multiplexing to work

30 views
Skip to first unread message

Alex Grabski

unread,
Aug 19, 2024, 1:57:04 PM8/19/24
to ptools-perfapi
I made a small program to test out the papi library.
In it I found 6 preset events which are available on my machine but are not able to be added together. So I figured this is perfect to test out multiplexing. I tried adding them with multiplexing enabled but it still gives the same error. See picture below:

temp.png

Here's also my code;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "papi.h"
#include <stdbool.h>
#include "sys_basemacros.h"

extern int papi_custom();
int papi_custom() {
   int scanf_warnings;
   int s;
   int i = 0;
   int j = 0;
   int eventset = PAPI_NULL;
   int event_code = 0;
   char str[20];
   bool ok = false;
   bool multiplex = false;
   int retval;
   PAPI_event_info_t info;
   size_t size = 1024; // Assigning memory to measure performance during copying of data

   // Moved these declarations above the memory allocation code.
   char* src;
   char* dst;
   int* events;
   long long(*count)[3];

   // Now you can allocate memory after all declarations are done.
   src = (char*)malloc(size);
   dst = (char*)malloc(size);

   if (src == NULL || dst == NULL) {
      printf("Memory allocation failed!\n");
      free(src);
      free(dst);
      return 0;
   }

   memset(src, 'A', size);

   printf("\nInput the number of events: ");
   scanf_warnings = scanf("%d", &s);

   // Allocate memory for events and count arrays after knowing the size.
   events = (int*)malloc(s * sizeof(int));
   count = (long long(*)[3])malloc(s * 3 * sizeof(long long));

   retval = PAPI_library_init(PAPI_VER_CURRENT); // Initializing PAPI
   if (retval != PAPI_VER_CURRENT) {
      fprintf(stderr, "Error initializing PAPI! %s\n", PAPI_strerror(retval));
      free(src);
      free(dst);
      free(events);
      free(count);
      return 0;
   }

   printf("Do you wish to enable multiplexing y/n: ");
   while (!multiplex) {
      scanf_warnings = scanf("%19s", str);
      if (str[0] == 'y') {  
         multiplex = true;
         retval = PAPI_multiplex_init();
         if (retval != PAPI_OK) {
            fprintf(stderr, "Error initializing multiplexing %s\n", PAPI_strerror(retval));
            free(src);
            free(dst);
            free(events);
            free(count);
            return 0;
         }
      }
      else if (str[0] == 'n') {  
         multiplex = true;
      }
      else {
         printf("Wrong input, try again y/n: ");
      }
   }

   printf("\nInput the events you want to measure, press enter after each event: ");

   while (i < s) {
      while (!ok) {
         scanf_warnings = scanf("%19s", str);
         retval = PAPI_event_name_to_code(str, &event_code);
         if (retval != PAPI_OK) {
            printf("\nEvent unavailable, input again: ");
         }
         else {
            retval = PAPI_get_event_info(event_code, &info);
            if (retval != PAPI_OK) {
               printf("\nEvent unavailable, input again: ");
            }
            else {
               if (info.count > 0) {
                  ok = true;
                  events[i] = event_code;
               }
               else {
                  printf("\nEvent unavailable, input again: ");
               }
            }
         }
      }
      i++;
      ok = false;
   }
   i = 0;

   retval = PAPI_create_eventset(&eventset); // Creating the eventset
   if (retval != PAPI_OK) {
      fprintf(stderr, "Error creating eventset! %s\n", PAPI_strerror(retval));
      free(src);
      free(dst);
      free(events);
      free(count);
      return 0;
   }

   retval = PAPI_add_events(eventset, events, s); // Adding the array of events to the set
   if (retval != PAPI_OK) {
      fprintf(stderr, "Error adding events %s\n", PAPI_strerror(retval));
      free(src);
      free(dst);
      free(events);
      free(count);
      return 0;
   }

   if (multiplex) { // Converts eventset to a multiplexing one
      retval = PAPI_set_multiplex(eventset);
      if (retval != PAPI_OK) {
         fprintf(stderr, "Error converting eventset to a multiplexing eventset %s\n", PAPI_strerror(retval));
         free(src);
         free(dst);
         free(events);
         free(count);
         return 0;
      }
   }

   while (i < 3) {

      PAPI_reset(eventset);
      retval = PAPI_start(eventset); // Starts the eventset measurement
      if (retval != PAPI_OK) {
         fprintf(stderr, "Error starting PAPI: %s\n", PAPI_strerror(retval));
         free(src);
         free(dst);
         free(events);
         free(count);
         return 0;
      }

      memcpy(dst, src, size); // What's being measured

      retval = PAPI_stop(eventset, count[i]);
      if (retval != PAPI_OK) {
         fprintf(stderr, "Error stopping: %s\n", PAPI_strerror(retval));
      }
      i++;
   }
   i = 0;
   while (i < 3) {
      j = 0;  // Reset j for each iteration of i
      while (j < s) {  
         PAPI_get_event_info(events[j], &info);  
         printf("\n%s: %lld", info.symbol, count[i][j]);
         j++;
      }
      printf("\n");
      i++;
   }

   if (scanf_warnings == 1) {
      i++;
   }

   free(src);
   free(dst);
   free(events);
   free(count);
   printf("\n");
   return 0;
}

Alex Grabski

unread,
Aug 19, 2024, 1:57:05 PM8/19/24
to ptools-perfapi
I've written a little program to test papi out, I found 6 events which are available on my machine yet they don't work when together. So I figured this is a good opportunity to test multiplexing. However when I enable it it doesn't change anything, just gives out the same error, see picture.

temp.png

Here's my code:

Treece Burgess

unread,
Aug 19, 2024, 3:24:51 PM8/19/24
to Alex Grabski, ptools-perfapi
Hello Alex,

What do you exactly mean by the 6 events "don't work when together"? Specifically, what is the error code/error message that you are seeing when trying to add these 6 events together. Can you also attach your output for papi_avail (e.g. ./path-to-install/bin/papi_avail) to this email? It appears you are adding preset events.

Best wishes,

Treece



--
You received this message because you are subscribed to the Google Groups "ptools-perfapi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ptools-perfap...@icl.utk.edu.
To view this discussion on the web visit https://groups.google.com/a/icl.utk.edu/d/msgid/ptools-perfapi/1a3b1481-655d-4f88-842e-0282f67f9806n%40icl.utk.edu.

Alex Grabski

unread,
Aug 21, 2024, 6:09:26 AM8/21/24
to ptools-perfapi, tbur...@icl.utk.edu, ptools-perfapi, Alex Grabski
Hello Treece,

So I was testing PAPI and I added 6 random preset events. I got the error: Error adding events (NULL) Result - 0x0000000000000.
Then I tested each event separately to make sure all of them are available on my hardware, they all worked. 
Seeing as 6 of them together failed but individually they worked I decided to test if multiplexing would work. But I get the same error.

I am not able to run papi_avail as the installation is custom, however I have made a file that prints out all the available preset events, see attached for the for the file and output.

Thank you for your help.

Kind regards,

Alex
papi_events.c
papi_native().txt
papi_preset().txt

Treece Burgess

unread,
Aug 21, 2024, 12:28:39 PM8/21/24
to Alex Grabski, ptools-perfapi
Hello Alex,

In regards to the PAPI Error Code:

I did see in the original output that you sent the error encountered was: Error adding events (NULL) Result - 0x0000000000000. However, PAPI_add_events should return a PAPI standardized error code of 0 or -1 to -27 and when you call PAPI_strerror it converts it to a c-string. The only instance you will end up with NULL are:
  • error code > 0
  • -error code > max number of papi error codes (28)
You stated you are using a custom installation; therefore, this could be an issue with the custom install since I did not encounter this issue during testing. It helps to know the exact error code so we can see if it occurs for us as well when testing.

In regards to further testing and possible solution:

I was unable to utilize the code that you sent originally due to it using "sys_basemacros.h". However, I have the PAPI preset events of: PAPI_REF_CYC, PAPI_BR_CN, PAPI_FUL_ICY, PAPI_L1_DCM, PAPI_L3_ICR, and PAPI_TOT_INS available on the machine I am using. Like yourself, I was unable to add all 6 PAPI preset events without multiplexing. However, when I go through the process of setting up PAPI multiplexing, I do not run into any issues. I am attaching the script that I used so you can take a look through it. The workflow in the script that you sent seems to be the following: PAPI_create_eventset -> PAPI_add_events -> PAPI_set_multiplex. You need to add a single event, and then call PAPI_set_multiplex followed by the rest of the events you want to add. Therefore, it would look like: PAPI_create_eventset -> PAPI_add_event -> PAPI_set_multiplex -> PAPI_add_events.

If possible I would install and use PAPI from the following link which is what we maintain.

Best wishes,

Treece


test_multiplex.c

Alex Grabski

unread,
Aug 21, 2024, 2:15:27 PM8/21/24
to ptools-perfapi, tbur...@icl.utk.edu, ptools-perfapi, Alex Grabski
Hi,

It seems to work now. I modified my code to look more like yours, however the only thing different was that you first added a single event, converted the set to a multiplexing one and only then added the rest.

I hadn't realized that this is how multiplexing needs to be set up as in the wiki the examples only contained a single event.

Thank you very much for your help.

Kind regards,

Alex

Treece Burgess

unread,
Aug 21, 2024, 2:37:44 PM8/21/24
to Alex Grabski, ptools-perfapi
I am glad to hear that it is working now. Can you link me to the wiki you are referring to that only shows examples with a single event? I would like to update it, if it is incorrect or the documentation can be improved. 

Our main wiki is found on GitHub, specifically the page for PAPI Multiplexing can be found here. The last code block is an example that shows the workflow for PAPI multiplexing and should add more than a single event.

Best wishes,

Treece

Alex Grabski

unread,
Aug 21, 2024, 3:05:18 PM8/21/24
to ptools-perfapi, tbur...@icl.utk.edu, ptools-perfapi, Alex Grabski

I was actually using the wiki you linked, but I guess I didn't realize that an event is added beforehand.
It would nonetheless be nice if it was mentioned in text that a non empty eventset must be converted to a multiplex one before adding more.

Thank you again.

Alex
Reply all
Reply to author
Forward
0 new messages