|
| 1 | +/******************************************************************************* |
| 2 | +Copyright (c) 2022, The OpenBLAS Project |
| 3 | +All rights reserved. |
| 4 | +Redistribution and use in source and binary forms, with or without |
| 5 | +modification, are permitted provided that the following conditions are |
| 6 | +met: |
| 7 | +1. Redistributions of source code must retain the above copyright |
| 8 | +notice, this list of conditions and the following disclaimer. |
| 9 | +2. Redistributions in binary form must reproduce the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer in |
| 11 | +the documentation and/or other materials provided with the |
| 12 | +distribution. |
| 13 | +3. Neither the name of the OpenBLAS project nor the names of |
| 14 | +its contributors may be used to endorse or promote products |
| 15 | +derived from this software without specific prior written permission. |
| 16 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE |
| 20 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 25 | +USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*******************************************************************************/ |
| 27 | + |
| 28 | +#include "common.h" |
| 29 | + |
| 30 | +extern gotoblas_t gotoblas_LOONGSON3R5; |
| 31 | +extern gotoblas_t gotoblas_LOONGSON2K1000; |
| 32 | +extern gotoblas_t gotoblas_LOONGSONGENERIC; |
| 33 | + |
| 34 | +extern void openblas_warning(int verbose, const char * msg); |
| 35 | + |
| 36 | +#define NUM_CORETYPES 3 |
| 37 | + |
| 38 | +static char *corename[] = { |
| 39 | + "loongson3r5", |
| 40 | + "loongson2k1000", |
| 41 | + "loongsongeneric", |
| 42 | + "unknown" |
| 43 | +}; |
| 44 | + |
| 45 | +char *gotoblas_corename(void) { |
| 46 | + if (gotoblas == &gotoblas_LOONGSON3R5) return corename[0]; |
| 47 | + if (gotoblas == &gotoblas_LOONGSON2K1000) return corename[1]; |
| 48 | + if (gotoblas == &gotoblas_LOONGSONGENERIC) return corename[2]; |
| 49 | + return corename[NUM_CORETYPES]; |
| 50 | +} |
| 51 | + |
| 52 | +static gotoblas_t *force_coretype(char *coretype) { |
| 53 | + int i; |
| 54 | + int found = -1; |
| 55 | + char message[128]; |
| 56 | + |
| 57 | + for ( i=0 ; i < NUM_CORETYPES; i++) |
| 58 | + { |
| 59 | + if (!strncasecmp(coretype, corename[i], 20)) |
| 60 | + { |
| 61 | + found = i; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + switch (found) |
| 67 | + { |
| 68 | + case 0: return (&gotoblas_LOONGSON3R5); |
| 69 | + case 1: return (&gotoblas_LOONGSON2K1000); |
| 70 | + case 2: return (&gotoblas_LOONGSONGENERIC); |
| 71 | + } |
| 72 | + snprintf(message, 128, "Core not found: %s\n", coretype); |
| 73 | + openblas_warning(1, message); |
| 74 | + return NULL; |
| 75 | +} |
| 76 | + |
| 77 | +#define LASX_MASK 1<<7 |
| 78 | +#define LSX_MASK 1<<6 |
| 79 | +#define LOONGARCH_CFG2 0x02 |
| 80 | + |
| 81 | +static gotoblas_t *get_coretype(void) { |
| 82 | + int ret = 0; |
| 83 | + __asm__ volatile ( |
| 84 | + "cpucfg %0, %1 \n\t" |
| 85 | + : "+&r"(ret) |
| 86 | + : "r"(LOONGARCH_CFG2) |
| 87 | + ); |
| 88 | + |
| 89 | + if (ret & LASX_MASK) |
| 90 | + return &gotoblas_LOONGSON3R5; |
| 91 | + else if (ret & LSX_MASK) |
| 92 | + return &gotoblas_LOONGSON2K1000; |
| 93 | + else |
| 94 | + return &gotoblas_LOONGSONGENERIC; |
| 95 | +} |
| 96 | + |
| 97 | +void gotoblas_dynamic_init(void) { |
| 98 | + char coremsg[128]; |
| 99 | + char coren[22]; |
| 100 | + char *p; |
| 101 | + |
| 102 | + if (gotoblas) return; |
| 103 | + |
| 104 | + p = getenv("OPENBLAS_CORETYPE"); |
| 105 | + if ( p ) |
| 106 | + { |
| 107 | + gotoblas = force_coretype(p); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + gotoblas = get_coretype(); |
| 112 | + } |
| 113 | + |
| 114 | + if (gotoblas && gotoblas->init) { |
| 115 | + strncpy(coren, gotoblas_corename(), 20); |
| 116 | + sprintf(coremsg, "Core: %s\n", coren); |
| 117 | + openblas_warning(2, coremsg); |
| 118 | + gotoblas -> init(); |
| 119 | + } else { |
| 120 | + openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n"); |
| 121 | + exit(1); |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +void gotoblas_dynamic_quit(void) { |
| 127 | + gotoblas = NULL; |
| 128 | +} |
0 commit comments