|
| 1 | +/***************************************************************************** |
| 2 | +Copyright (c) 2026, The OpenBLAS Project |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are |
| 7 | +met: |
| 8 | +
|
| 9 | + 1. Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | + 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in |
| 14 | + the documentation and/or other materials provided with the |
| 15 | + distribution. |
| 16 | + 3. Neither the name of the OpenBLAS project nor the names of its |
| 17 | + contributors may be used to endorse or promote products derived |
| 18 | + from this software without specific prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | +ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE |
| 24 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | +POSSIBILITY OF SUCH DAMAGE. |
| 31 | +*****************************************************************************/ |
| 32 | + |
| 33 | +#include <cblas.h> |
| 34 | +#include <omp.h> |
| 35 | +#include <stdio.h> |
| 36 | +#include <stdlib.h> |
| 37 | +#include <string.h> |
| 38 | + |
| 39 | +static int callback_calls; |
| 40 | +static int callback_numjobs = 1; |
| 41 | + |
| 42 | +static void test_threads_callback(int sync, openblas_dojob_callback dojob, |
| 43 | + int numjobs, size_t jobdata_elsize, |
| 44 | + void *jobdata, int dojob_data) |
| 45 | +{ |
| 46 | + int i; |
| 47 | + |
| 48 | + (void)sync; |
| 49 | + callback_calls++; |
| 50 | + callback_numjobs = numjobs; |
| 51 | + if (omp_in_parallel()) { |
| 52 | + /* |
| 53 | + * Use tasks from the existing team. DGEMM jobs exchange packed blocks, |
| 54 | + * so they must be allowed to make progress concurrently. |
| 55 | + */ |
| 56 | + for (i = 0; i < numjobs; i++) { |
| 57 | +#pragma omp task firstprivate(i, dojob, jobdata_elsize, jobdata, dojob_data) |
| 58 | + dojob(i, (char *)jobdata + (size_t)i * jobdata_elsize, dojob_data); |
| 59 | + } |
| 60 | +#pragma omp taskwait |
| 61 | + } else { |
| 62 | +#pragma omp parallel for num_threads(numjobs) schedule(static, 1) \ |
| 63 | + firstprivate(dojob, jobdata_elsize, jobdata, dojob_data) |
| 64 | + for (i = 0; i < numjobs; i++) |
| 65 | + dojob(i, (char *)jobdata + (size_t)i * jobdata_elsize, dojob_data); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +static int check_dgemm(const char *label, int expected_jobs, blasint n, |
| 70 | + const double *a, const double *b, double *c) |
| 71 | +{ |
| 72 | + size_t i; |
| 73 | + size_t elements = (size_t)n * (size_t)n; |
| 74 | + int failed = 0; |
| 75 | + |
| 76 | + memset(c, 0, elements * sizeof(*c)); |
| 77 | + callback_calls = 0; |
| 78 | + callback_numjobs = 1; |
| 79 | + |
| 80 | + printf("Running %s\n", label); |
| 81 | + fflush(stdout); |
| 82 | + |
| 83 | + /* |
| 84 | + * TN avoids architecture-specific small-matrix kernels that would bypass |
| 85 | + * the threaded DGEMM dispatcher at this deliberately small test size. |
| 86 | + */ |
| 87 | + cblas_dgemm(CblasColMajor, CblasTrans, CblasNoTrans, n, n, n, |
| 88 | + 1.0, a, n, b, n, 0.0, c, n); |
| 89 | + |
| 90 | + if (callback_numjobs != expected_jobs) { |
| 91 | + fprintf(stderr, "%s: expected %d logical job(s), observed %d\n", |
| 92 | + label, expected_jobs, callback_numjobs); |
| 93 | + failed = 1; |
| 94 | + } |
| 95 | + if (expected_jobs > 1 && callback_calls == 0) { |
| 96 | + fprintf(stderr, "%s: threaded callback was not invoked\n", label); |
| 97 | + failed = 1; |
| 98 | + } |
| 99 | + |
| 100 | + for (i = 0; i < elements; i++) { |
| 101 | + if (c[i] != (double)n) { |
| 102 | + fprintf(stderr, |
| 103 | + "%s: incorrect DGEMM result at element %zu: %.17g != %d\n", |
| 104 | + label, i, c[i], (int)n); |
| 105 | + failed = 1; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return failed; |
| 111 | +} |
| 112 | + |
| 113 | +int main(void) |
| 114 | +{ |
| 115 | + const double two_thread_work = |
| 116 | + 2.0 * 65536.0 * (double)GEMM_MULTITHREAD_THRESHOLD; |
| 117 | + blasint n = 1; |
| 118 | + size_t elements; |
| 119 | + double *a; |
| 120 | + double *b; |
| 121 | + double *c; |
| 122 | + int failed = 0; |
| 123 | + int nested_failed = 0; |
| 124 | + int outer_team_size = 0; |
| 125 | + size_t i; |
| 126 | + |
| 127 | + /* |
| 128 | + * This must be the first OpenBLAS call. The callback records the logical |
| 129 | + * job count and runs the jobs without creating a nested OpenMP team. |
| 130 | + */ |
| 131 | + openblas_set_threads_callback_function(test_threads_callback); |
| 132 | + |
| 133 | + omp_set_dynamic(0); |
| 134 | + while ((double)n * (double)n * (double)n <= two_thread_work) |
| 135 | + n++; |
| 136 | + n++; /* Keep the test just beyond the threshold boundary. */ |
| 137 | + |
| 138 | + elements = (size_t)n * (size_t)n; |
| 139 | + a = malloc(elements * sizeof(*a)); |
| 140 | + b = malloc(elements * sizeof(*b)); |
| 141 | + c = malloc(elements * sizeof(*c)); |
| 142 | + if (a == NULL || b == NULL || c == NULL) { |
| 143 | + fprintf(stderr, "failed to allocate DGEMM test matrices\n"); |
| 144 | + free(c); |
| 145 | + free(b); |
| 146 | + free(a); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + |
| 150 | + for (i = 0; i < elements; i++) |
| 151 | + a[i] = b[i] = 1.0; |
| 152 | + |
| 153 | + openblas_set_num_threads(1); |
| 154 | + failed |= check_dgemm("global count 1 outside OpenMP", 1, n, a, b, c); |
| 155 | + |
| 156 | + openblas_set_num_threads(2); |
| 157 | + failed |= check_dgemm("global count 2 outside OpenMP", 2, n, a, b, c); |
| 158 | + |
| 159 | +#pragma omp parallel num_threads(2) shared(outer_team_size, nested_failed) |
| 160 | + { |
| 161 | +#pragma omp single |
| 162 | + { |
| 163 | + outer_team_size = omp_get_num_threads(); |
| 164 | + if (outer_team_size == 2) { |
| 165 | +#pragma omp task |
| 166 | + nested_failed = |
| 167 | + check_dgemm("global count 2 inside OpenMP task", 1, n, a, b, c); |
| 168 | +#pragma omp taskwait |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + if (outer_team_size == 2) |
| 174 | + failed |= nested_failed; |
| 175 | + else |
| 176 | + printf("SKIP: OpenMP runtime formed an outer team of %d thread(s)\n", |
| 177 | + outer_team_size); |
| 178 | + |
| 179 | + failed |= check_dgemm("global count 2 after OpenMP region", 2, n, a, b, c); |
| 180 | + |
| 181 | + free(c); |
| 182 | + free(b); |
| 183 | + free(a); |
| 184 | + |
| 185 | + if (failed == 0) |
| 186 | + printf("OpenMP thread selection test passed (DGEMM dimension %d)\n", |
| 187 | + (int)n); |
| 188 | + return failed; |
| 189 | +} |
0 commit comments