|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <omp.h> |
| 6 | +#include "papi.h" |
| 7 | +#include "matmul.h" |
| 8 | + |
| 9 | +int main(int argc, char *argv[]) |
| 10 | +{ |
| 11 | + int papi_errno; |
| 12 | + hipError_t hip_errno; |
| 13 | + |
| 14 | + int num_threads = 1; |
| 15 | + if (argc > 1) { |
| 16 | + if (strncmp(argv[1], "--threads=", strlen("--threads=")) == 0) { |
| 17 | + num_threads = (int) strtol(argv[1] + strlen("--threads="), NULL, 10); |
| 18 | + } else if (strcmp(argv[1], "--help") == 0) { |
| 19 | + fprintf(stdout, "Usage %s [OPTIONS]\n", argv[0]); |
| 20 | + fprintf(stdout, "[OPTIONS]\n"); |
| 21 | + fprintf(stdout, " --help\n"); |
| 22 | + fprintf(stdout, " --threads=[N]\n"); |
| 23 | + exit(EXIT_FAILURE); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + papi_errno = PAPI_library_init(PAPI_VER_CURRENT); |
| 28 | + if (papi_errno != PAPI_VER_CURRENT) { |
| 29 | + fprintf(stderr, "ERROR: PAPI_library_init: runtime lib ver %d not equal to %d\n", papi_errno, PAPI_VER_CURRENT); |
| 30 | + exit(EXIT_FAILURE); |
| 31 | + } |
| 32 | + |
| 33 | + papi_errno = PAPI_thread_init((unsigned long (*)(void)) omp_get_thread_num); |
| 34 | + if (papi_errno != PAPI_OK) { |
| 35 | + fprintf(stderr, "ERROR: PAPI_thread_init: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 36 | + exit(EXIT_FAILURE); |
| 37 | + } |
| 38 | + |
| 39 | + int num_devices; |
| 40 | + hip_errno = hipGetDeviceCount(&num_devices); |
| 41 | + if (hip_errno != hipSuccess) { |
| 42 | + fprintf(stderr, "ERROR: hipGetDeviceCount: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 43 | + exit(EXIT_FAILURE); |
| 44 | + } |
| 45 | + |
| 46 | + num_threads = (num_threads < num_devices) ? num_threads : num_devices; |
| 47 | + omp_set_num_threads(num_threads); |
| 48 | + fprintf(stdout, "Run rocm test with %d threads\n", num_threads); |
| 49 | + |
| 50 | +#define NUM_EVENTS 2 |
| 51 | + const char *events[NUM_EVENTS] = { |
| 52 | + "rocm:::SQ_WAVES", |
| 53 | + "rocm:::SQ_WAVES_RESTORED", |
| 54 | + }; |
| 55 | + |
| 56 | +#pragma omp parallel |
| 57 | + { |
| 58 | + int eventset = PAPI_NULL; |
| 59 | + papi_errno = PAPI_create_eventset(&eventset); |
| 60 | + if (papi_errno != PAPI_OK) { |
| 61 | + fprintf(stderr, "ERROR: PAPI_create_eventset: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 62 | + exit(EXIT_FAILURE); |
| 63 | + } |
| 64 | + |
| 65 | + int thread_num = omp_get_thread_num(); |
| 66 | + for (int j = 0; j < NUM_EVENTS; ++j) { |
| 67 | + char named_event[PAPI_MAX_STR_LEN] = { 0 }; |
| 68 | + sprintf(named_event, "%s:device=%d", events[j], thread_num); |
| 69 | + papi_errno = PAPI_add_named_event(eventset, (const char *) named_event); |
| 70 | + if (papi_errno != PAPI_OK && papi_errno != PAPI_ENOEVNT) { |
| 71 | + fprintf(stderr, "ERROR: PAPI_add_named_event: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 72 | + exit(EXIT_FAILURE); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + papi_errno = PAPI_start(eventset); |
| 77 | + if (papi_errno != PAPI_OK) { |
| 78 | + fprintf(stderr, "ERROR: PAPI_start: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 79 | + exit(EXIT_FAILURE); |
| 80 | + } |
| 81 | + |
| 82 | + hip_errno = hipSetDevice(thread_num); |
| 83 | + if (hip_errno != hipSuccess) { |
| 84 | + fprintf(stderr, "ERROR: hipSetDevice: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 85 | + exit(EXIT_FAILURE); |
| 86 | + } |
| 87 | + |
| 88 | + hipStream_t stream; |
| 89 | + hip_errno = hipStreamCreate(&stream); |
| 90 | + if (hip_errno != hipSuccess) { |
| 91 | + fprintf(stderr, "ERROR: hipStreamCreate: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 92 | + exit(EXIT_FAILURE); |
| 93 | + } |
| 94 | + |
| 95 | + void *handle; |
| 96 | + int matmul_errno; |
| 97 | + matmul_errno = matmul_init(&handle); |
| 98 | + if (matmul_errno != MATMUL_SUCCESS) { |
| 99 | + fprintf(stderr, "ERROR: matmul_init: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 100 | + exit(EXIT_FAILURE); |
| 101 | + } |
| 102 | + |
| 103 | + matmul_errno = matmul_run(handle, stream); |
| 104 | + if (matmul_errno != MATMUL_SUCCESS) { |
| 105 | + fprintf(stderr, "ERROR: matmul_run: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 106 | + exit(EXIT_FAILURE); |
| 107 | + } |
| 108 | + |
| 109 | + hip_errno = hipStreamSynchronize(stream); |
| 110 | + if (hip_errno != hipSuccess) { |
| 111 | + fprintf(stderr, "ERROR: hipStreamSynchronize: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 112 | + exit(EXIT_FAILURE); |
| 113 | + } |
| 114 | + |
| 115 | + hip_errno = hipStreamDestroy(stream); |
| 116 | + if (hip_errno != hipSuccess) { |
| 117 | + fprintf(stderr, "ERROR: hipStreamDestroy: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 118 | + exit(EXIT_FAILURE); |
| 119 | + } |
| 120 | + |
| 121 | + matmul_errno = matmul_finalize(&handle); |
| 122 | + if (matmul_errno != MATMUL_SUCCESS) { |
| 123 | + fprintf(stderr, "ERROR: matmul_finalize: %d: %s\n", PAPI_EMISC, PAPI_strerror(PAPI_EMISC)); |
| 124 | + exit(EXIT_FAILURE); |
| 125 | + } |
| 126 | + |
| 127 | + long long counters[NUM_EVENTS] = { 0 }; |
| 128 | + papi_errno = PAPI_stop(eventset, counters); |
| 129 | + if (papi_errno != PAPI_OK) { |
| 130 | + fprintf(stderr, "ERROR: PAPI_stop: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 131 | + exit(EXIT_FAILURE); |
| 132 | + } |
| 133 | + |
| 134 | + for (int i = 0; i < NUM_EVENTS; ++i) { |
| 135 | + fprintf(stdout, "[tid:%d] %s:device=%d : %lld\n", |
| 136 | + omp_get_thread_num(), events[i], thread_num, |
| 137 | + counters[i]); |
| 138 | + } |
| 139 | + |
| 140 | + papi_errno = PAPI_cleanup_eventset(eventset); |
| 141 | + if (papi_errno != PAPI_OK) { |
| 142 | + fprintf(stderr, "ERROR: PAPI_cleanup_eventset: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 143 | + exit(EXIT_FAILURE); |
| 144 | + } |
| 145 | + |
| 146 | + papi_errno = PAPI_destroy_eventset(&eventset); |
| 147 | + if (papi_errno != PAPI_OK) { |
| 148 | + fprintf(stderr, "ERROR: PAPI_destroy_eventset: %d: %s\n", papi_errno, PAPI_strerror(papi_errno)); |
| 149 | + exit(EXIT_FAILURE); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + PAPI_shutdown(); |
| 154 | + |
| 155 | + return EXIT_SUCCESS; |
| 156 | +} |
0 commit comments