PAPI 7.1.0.0
Loading...
Searching...
No Matches
gpu_common_utils.c
Go to the documentation of this file.
1/* Copyright (c) 2020 Intel Corp. All rights reserved
2 * Contributed by Peinan Zhang <peinan.zhang@intel.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 * of the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22/*
23 * This utility functions.
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <string.h>
30#include <stdint.h>
31
32#include "papi.h"
33#include "gpu_common_utils.h"
34
35#if defined(__cplusplus)
36extern "C" {
37#endif
38
39const char *default_metrics[] = {
40 "ComputeBasic.GpuTime",
41 "ComputeBasic.GpuCoreClocks",
42 "ComputeBasic.AvgGpuCoreFrequencyMHz",
43};
44
46
47void
48parseMetricList(char *metric_list, InParams *param) {
49
50 int size = 64;
51 int index = 0;
52 if (!metric_list) {
54 param->metric_names = (char **)default_metrics;
55 } else {
56 char **metrics = (char **)calloc(size, sizeof(char *));
57 char *token = strtok(metric_list, ",");
58 while (token) {
59 if (index >= size) {
60 size += 64;
61 metrics = (char **)realloc(metrics, size * sizeof(char *));
62 }
63 metrics[index++] = token;
64 printf("metric[%d]: %s\n", index-1, metrics[index-1]);
65 token = strtok(NULL, ",");
66 }
67 param->num_metrics = index;
68 param->metric_names = metrics;
69 }
70}
71
72int
73parseInputParam(int argc, char **argv, InParams *param) {
74 char ch;
75 int duration = 3;
76 int loops = 1;
77 int reset = 0;
78 char *metric_list = NULL;
79 char *app_targets = NULL;
80 while ((ch=getopt(argc, argv, "d:l:e:t:m:s")) != -1) {
81 switch(ch) {
82 case 't':
83 app_targets = optarg;
84 break;
85 case 'd':
86 duration = atoi(optarg);
87 if ((duration <= 0) || (duration > 3600)) { // max 3600 seconds
88 printf("invalid input on dueation [1, 3600], use default 3 sec.\n");
89 duration = 3;
90 }
91 break;
92 case 'l':
93 loops = atoi(optarg);
94 if ((loops <= 0) || (loops > 0x100000)) { // max 1M
95 printf("invalid input on loops [1, 1M], use default 1 loop.\n");
96 loops = 1;
97 }
98 break;
99 case 's':
100 reset = 1;
101 break;
102 case 'm':
103 metric_list = strdup(optarg);
104 break;
105 default:
106 return 1;
107 }
108 }
109 param->duration = duration;
110 param->loops = loops;
111 param->reset = reset;
112 param->app_dev = 0;
113 param->app_tile = 0;
114 if (app_targets) {
115 char *str = app_targets;
116 int i=0;
117 if ((str[i]=='d') && (str[i+1] != '\0')) {
118 param->app_dev = atoi(&str[++i]);
119 while ((str[i] != '\0') && ( str[i] < '0') && (str[i] > '9')) {
120 i++;
121 }
122 }
123 if ((str[i] != '\0') && (str[i] == 't') && (str[i+1] != '\0')) {
124 param->app_tile = atoi(&str[i+1])+1;
125 }
126 }
127 parseMetricList(metric_list, param);
128 return 0;
129}
130
131int
133
134 PAPI_component_info_t *aComponent = NULL;
135 int cid = -1;
136
137 // init all components including "intel_gpu"
138 int retVal = PAPI_library_init( PAPI_VER_CURRENT );
139 if( retVal != PAPI_VER_CURRENT ) {
140 fprintf( stderr, "PAPI_library_init failed\n" );
141 return -1;
142 }
143
144 int numComponents = PAPI_num_components();
145 int i = 0;
146 for (i=0; i<numComponents && cid<0; i++) {
147 // get the component info.
149 if (aComponent == NULL) {
150 continue;
151 }
152 if (strcmp(COMP_NAME, aComponent->name) == 0) {
153 cid=i; // If we found our match, record it.
154 } // end search components.
155 }
156 if (cid < 0) {
157 fprintf(stderr, "Failed to find component [%s] in total %i supported components.\n",
158 COMP_NAME, numComponents);
160 return -1;
161 }
162 return cid;
163}
164
165int
166initMetricSet(char **metric_names, int num_metrics, int *eventSet) {
167
168 int retVal = PAPI_create_eventset(eventSet);
169 if (retVal != PAPI_OK) {
170 fprintf(stderr, "Error on PAPI_create_eventset, retVal %d\n", retVal);
172 return retVal;
173 }
174
175 for (int i=0; i<num_metrics; i++) {
176 retVal = PAPI_add_named_event(*eventSet, metric_names[i]);
177 if (retVal < 0) {
178 fprintf(stderr, "Error on PAPI_add_named_event %s, retVal %d\n",
179 metric_names[i], retVal);
180 break;
181 }
182 }
183 if (retVal != PAPI_OK) {
185 return retVal;
186 }
187 return PAPI_OK;
188}
189
190#if defined(__cplusplus)
191}
192#endif
193
int i
add PAPI preset or native hardware event by name to an EventSet
Create a new empty PAPI EventSet.
get information about a specific software component
initialize the PAPI library.
Get the number of components available on the system.
Finish using PAPI and free all related resources.
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
int initPAPIGPUComp()
int parseInputParam(int argc, char **argv, InParams *param)
void parseMetricList(char *metric_list, InParams *param)
int num_default_metrics
const char * default_metrics[]
int initMetricSet(char **metric_names, int num_metrics, int *eventSet)
#define COMP_NAME
Return codes and api definitions.
FILE * stderr
uint32_t num_metrics
uint32_t duration
uint32_t loops
uint32_t reset
char ** metric_names
uint32_t app_dev
uint32_t app_tile
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627