You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory.c 34 kB

9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /*****************************************************************************
  2. Copyright (c) 2011-2014, 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
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. /*********************************************************************/
  29. /* Copyright 2009, 2010 The University of Texas at Austin. */
  30. /* All rights reserved. */
  31. /* */
  32. /* Redistribution and use in source and binary forms, with or */
  33. /* without modification, are permitted provided that the following */
  34. /* conditions are met: */
  35. /* */
  36. /* 1. Redistributions of source code must retain the above */
  37. /* copyright notice, this list of conditions and the following */
  38. /* disclaimer. */
  39. /* */
  40. /* 2. Redistributions in binary form must reproduce the above */
  41. /* copyright notice, this list of conditions and the following */
  42. /* disclaimer in the documentation and/or other materials */
  43. /* provided with the distribution. */
  44. /* */
  45. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  46. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  47. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  48. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  49. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  50. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  51. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  52. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  53. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  54. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  55. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  56. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  57. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  58. /* POSSIBILITY OF SUCH DAMAGE. */
  59. /* */
  60. /* The views and conclusions contained in the software and */
  61. /* documentation are those of the authors and should not be */
  62. /* interpreted as representing official policies, either expressed */
  63. /* or implied, of The University of Texas at Austin. */
  64. /*********************************************************************/
  65. //#undef DEBUG
  66. #include "common.h"
  67. #include <errno.h>
  68. #ifdef OS_WINDOWS
  69. #define ALLOC_WINDOWS
  70. #ifndef MEM_LARGE_PAGES
  71. #define MEM_LARGE_PAGES 0x20000000
  72. #endif
  73. #else
  74. #define ALLOC_MMAP
  75. #define ALLOC_MALLOC
  76. #endif
  77. #include <stdlib.h>
  78. #include <stdio.h>
  79. #include <fcntl.h>
  80. #ifndef OS_WINDOWS
  81. #include <sys/mman.h>
  82. #ifndef NO_SYSV_IPC
  83. #include <sys/shm.h>
  84. #endif
  85. #include <sys/ipc.h>
  86. #endif
  87. #include <sys/types.h>
  88. #ifdef OS_LINUX
  89. #include <sys/sysinfo.h>
  90. #include <sched.h>
  91. #include <errno.h>
  92. #include <linux/unistd.h>
  93. #include <sys/syscall.h>
  94. #include <sys/time.h>
  95. #include <sys/resource.h>
  96. #endif
  97. #if defined(OS_FREEBSD) || defined(OS_DARWIN)
  98. #include <sys/sysctl.h>
  99. #include <sys/resource.h>
  100. #endif
  101. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  102. #include <conio.h>
  103. #undef printf
  104. #define printf _cprintf
  105. #endif
  106. #ifdef OS_LINUX
  107. #ifndef MPOL_PREFERRED
  108. #define MPOL_PREFERRED 1
  109. #endif
  110. #endif
  111. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  112. #define NO_WARMUP
  113. #endif
  114. #ifndef SHM_HUGETLB
  115. #define SHM_HUGETLB 04000
  116. #endif
  117. #ifndef FIXED_PAGESIZE
  118. #define FIXED_PAGESIZE 4096
  119. #endif
  120. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  121. #if defined(_MSC_VER) && !defined(__clang__)
  122. #define CONSTRUCTOR __cdecl
  123. #define DESTRUCTOR __cdecl
  124. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  125. #define CONSTRUCTOR __attribute__ ((constructor))
  126. #define DESTRUCTOR __attribute__ ((destructor))
  127. #else
  128. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  129. #define DESTRUCTOR __attribute__ ((destructor(101)))
  130. #endif
  131. #ifdef DYNAMIC_ARCH
  132. gotoblas_t *gotoblas = NULL;
  133. #endif
  134. extern void openblas_warning(int verbose, const char * msg);
  135. #ifndef SMP
  136. #define blas_cpu_number 1
  137. #define blas_num_threads 1
  138. /* Dummy Function */
  139. int goto_get_num_procs (void) { return 1;};
  140. void goto_set_num_threads(int num_threads) {};
  141. #else
  142. #if defined(OS_LINUX) || defined(OS_SUNOS)
  143. #ifndef NO_AFFINITY
  144. int get_num_procs(void);
  145. #else
  146. int get_num_procs(void) {
  147. static int nums = 0;
  148. if (!nums) nums = sysconf(_SC_NPROCESSORS_ONLN);
  149. return nums;
  150. }
  151. #endif
  152. #endif
  153. #ifdef OS_ANDROID
  154. int get_num_procs(void) {
  155. static int nums = 0;
  156. if (!nums) nums = sysconf(_SC_NPROCESSORS_ONLN);
  157. return nums;
  158. }
  159. #endif
  160. #ifdef OS_WINDOWS
  161. int get_num_procs(void) {
  162. static int nums = 0;
  163. if (nums == 0) {
  164. SYSTEM_INFO sysinfo;
  165. GetSystemInfo(&sysinfo);
  166. nums = sysinfo.dwNumberOfProcessors;
  167. }
  168. return nums;
  169. }
  170. #endif
  171. #if defined(OS_FREEBSD)
  172. int get_num_procs(void) {
  173. static int nums = 0;
  174. int m[2];
  175. size_t len;
  176. if (nums == 0) {
  177. m[0] = CTL_HW;
  178. m[1] = HW_NCPU;
  179. len = sizeof(int);
  180. sysctl(m, 2, &nums, &len, NULL, 0);
  181. }
  182. return nums;
  183. }
  184. #endif
  185. #if defined(OS_DARWIN)
  186. int get_num_procs(void) {
  187. static int nums = 0;
  188. size_t len;
  189. if (nums == 0){
  190. len = sizeof(int);
  191. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  192. }
  193. return nums;
  194. }
  195. /*
  196. void set_stack_limit(int limitMB){
  197. int result=0;
  198. struct rlimit rl;
  199. rlim_t StackSize;
  200. StackSize=limitMB*1024*1024;
  201. result=getrlimit(RLIMIT_STACK, &rl);
  202. if(result==0){
  203. if(rl.rlim_cur < StackSize){
  204. rl.rlim_cur=StackSize;
  205. result=setrlimit(RLIMIT_STACK, &rl);
  206. if(result !=0){
  207. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  208. }
  209. }
  210. }
  211. }
  212. */
  213. #endif
  214. /*
  215. OpenBLAS uses the numbers of CPU cores in multithreading.
  216. It can be set by openblas_set_num_threads(int num_threads);
  217. */
  218. int blas_cpu_number = 0;
  219. /*
  220. The numbers of threads in the thread pool.
  221. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  222. */
  223. int blas_num_threads = 0;
  224. int goto_get_num_procs (void) {
  225. return blas_cpu_number;
  226. }
  227. void openblas_fork_handler()
  228. {
  229. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  230. // built with "make USE_OPENMP=0".
  231. // Hanging can still happen when OpenBLAS is built against the libgomp
  232. // implementation of OpenMP. The problem is tracked at:
  233. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  234. // In the mean time build with USE_OPENMP=0 or link against another
  235. // implementation of OpenMP.
  236. #if !(defined(OS_WINDOWS) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  237. int err;
  238. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
  239. if(err != 0)
  240. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  241. #endif
  242. }
  243. extern int openblas_num_threads_env();
  244. extern int openblas_goto_num_threads_env();
  245. extern int openblas_omp_num_threads_env();
  246. int blas_get_cpu_number(void){
  247. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  248. int max_num;
  249. #endif
  250. int blas_goto_num = 0;
  251. int blas_omp_num = 0;
  252. if (blas_num_threads) return blas_num_threads;
  253. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  254. max_num = get_num_procs();
  255. #endif
  256. blas_goto_num = 0;
  257. #ifndef USE_OPENMP
  258. blas_goto_num=openblas_num_threads_env();
  259. if (blas_goto_num < 0) blas_goto_num = 0;
  260. if (blas_goto_num == 0) {
  261. blas_goto_num=openblas_goto_num_threads_env();
  262. if (blas_goto_num < 0) blas_goto_num = 0;
  263. }
  264. #endif
  265. blas_omp_num = 0;
  266. blas_omp_num=openblas_omp_num_threads_env();
  267. if (blas_omp_num < 0) blas_omp_num = 0;
  268. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  269. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  270. else blas_num_threads = MAX_CPU_NUMBER;
  271. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  272. if (blas_num_threads > max_num) blas_num_threads = max_num;
  273. #endif
  274. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  275. #ifdef DEBUG
  276. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  277. #endif
  278. blas_cpu_number = blas_num_threads;
  279. return blas_num_threads;
  280. }
  281. #endif
  282. int openblas_get_num_procs(void) {
  283. #ifndef SMP
  284. return 1;
  285. #else
  286. return get_num_procs();
  287. #endif
  288. }
  289. int openblas_get_num_threads(void) {
  290. #ifndef SMP
  291. return 1;
  292. #else
  293. // init blas_cpu_number if needed
  294. blas_get_cpu_number();
  295. return blas_cpu_number;
  296. #endif
  297. }
  298. struct release_t {
  299. void *address;
  300. void (*func)(struct release_t *);
  301. long attr;
  302. };
  303. int hugetlb_allocated = 0;
  304. static struct release_t release_info[NUM_BUFFERS];
  305. static int release_pos = 0;
  306. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  307. static int hot_alloc = 0;
  308. #endif
  309. #ifdef ALLOC_MMAP
  310. static void alloc_mmap_free(struct release_t *release){
  311. if (munmap(release -> address, BUFFER_SIZE)) {
  312. printf("OpenBLAS : munmap failed\n");
  313. }
  314. }
  315. #ifdef NO_WARMUP
  316. static void *alloc_mmap(void *address){
  317. void *map_address;
  318. if (address){
  319. map_address = mmap(address,
  320. BUFFER_SIZE,
  321. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  322. } else {
  323. map_address = mmap(address,
  324. BUFFER_SIZE,
  325. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  326. }
  327. if (map_address != (void *)-1) {
  328. release_info[release_pos].address = map_address;
  329. release_info[release_pos].func = alloc_mmap_free;
  330. release_pos ++;
  331. }
  332. #ifdef OS_LINUX
  333. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  334. #endif
  335. return map_address;
  336. }
  337. #else
  338. #define BENCH_ITERATION 4
  339. #define SCALING 2
  340. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  341. BLASULONG original, *p;
  342. BLASULONG start, stop, min;
  343. int iter, i, count;
  344. min = (BLASULONG)-1;
  345. original = *(BLASULONG *)(address + size - PAGESIZE);
  346. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  347. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  348. p = (BLASULONG *)address;
  349. count = size / PAGESIZE;
  350. start = rpcc();
  351. for (i = 0; i < count; i ++) {
  352. p = (BLASULONG *)(*p);
  353. }
  354. stop = rpcc();
  355. if (min > stop - start) min = stop - start;
  356. }
  357. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  358. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  359. return min;
  360. }
  361. static void *alloc_mmap(void *address){
  362. void *map_address, *best_address;
  363. BLASULONG best, start, current;
  364. BLASULONG allocsize;
  365. if (address){
  366. /* Just give up use advanced operation */
  367. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  368. #ifdef OS_LINUX
  369. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  370. #endif
  371. } else {
  372. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  373. if (hot_alloc == 0) {
  374. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  375. #ifdef OS_LINUX
  376. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  377. #endif
  378. } else {
  379. #endif
  380. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  381. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  382. if (map_address != (void *)-1) {
  383. #ifdef OS_LINUX
  384. #ifdef DEBUG
  385. int ret=0;
  386. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  387. if(ret==-1){
  388. int errsv=errno;
  389. perror("OpenBLAS alloc_mmap:");
  390. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  391. }
  392. #else
  393. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  394. #endif
  395. #endif
  396. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  397. start = (BLASULONG)map_address;
  398. current = (SCALING - 1) * BUFFER_SIZE;
  399. while(current > 0) {
  400. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  401. start += PAGESIZE;
  402. current -= PAGESIZE;
  403. }
  404. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  405. start = (BLASULONG)map_address;
  406. best = (BLASULONG)-1;
  407. best_address = map_address;
  408. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  409. current = run_bench(start, allocsize);
  410. if (best > current) {
  411. best = current;
  412. best_address = (void *)start;
  413. }
  414. start += PAGESIZE;
  415. }
  416. if ((BLASULONG)best_address > (BLASULONG)map_address)
  417. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  418. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  419. map_address = best_address;
  420. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  421. hot_alloc = 2;
  422. #endif
  423. }
  424. }
  425. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  426. }
  427. #endif
  428. if (map_address != (void *)-1) {
  429. release_info[release_pos].address = map_address;
  430. release_info[release_pos].func = alloc_mmap_free;
  431. release_pos ++;
  432. }
  433. return map_address;
  434. }
  435. #endif
  436. #endif
  437. #ifdef ALLOC_MALLOC
  438. static void alloc_malloc_free(struct release_t *release){
  439. free(release -> address);
  440. }
  441. static void *alloc_malloc(void *address){
  442. void *map_address;
  443. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  444. if (map_address == (void *)NULL) map_address = (void *)-1;
  445. if (map_address != (void *)-1) {
  446. release_info[release_pos].address = map_address;
  447. release_info[release_pos].func = alloc_malloc_free;
  448. release_pos ++;
  449. }
  450. return map_address;
  451. }
  452. #endif
  453. #ifdef ALLOC_QALLOC
  454. void *qalloc(int flags, size_t bytes);
  455. void *qfree (void *address);
  456. #define QNONCACHE 0x1
  457. #define QCOMMS 0x2
  458. #define QFAST 0x4
  459. static void alloc_qalloc_free(struct release_t *release){
  460. qfree(release -> address);
  461. }
  462. static void *alloc_qalloc(void *address){
  463. void *map_address;
  464. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  465. if (map_address == (void *)NULL) map_address = (void *)-1;
  466. if (map_address != (void *)-1) {
  467. release_info[release_pos].address = map_address;
  468. release_info[release_pos].func = alloc_qalloc_free;
  469. release_pos ++;
  470. }
  471. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  472. }
  473. #endif
  474. #ifdef ALLOC_WINDOWS
  475. static void alloc_windows_free(struct release_t *release){
  476. VirtualFree(release -> address, BUFFER_SIZE, MEM_DECOMMIT);
  477. }
  478. static void *alloc_windows(void *address){
  479. void *map_address;
  480. map_address = VirtualAlloc(address,
  481. BUFFER_SIZE,
  482. MEM_RESERVE | MEM_COMMIT,
  483. PAGE_READWRITE);
  484. if (map_address == (void *)NULL) map_address = (void *)-1;
  485. if (map_address != (void *)-1) {
  486. release_info[release_pos].address = map_address;
  487. release_info[release_pos].func = alloc_windows_free;
  488. release_pos ++;
  489. }
  490. return map_address;
  491. }
  492. #endif
  493. #ifdef ALLOC_DEVICEDRIVER
  494. #ifndef DEVICEDRIVER_NAME
  495. #define DEVICEDRIVER_NAME "/dev/mapper"
  496. #endif
  497. static void alloc_devicedirver_free(struct release_t *release){
  498. if (munmap(release -> address, BUFFER_SIZE)) {
  499. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  500. }
  501. if (close(release -> attr)) {
  502. printf("OpenBLAS : Bugphysarea close failed.\n");
  503. }
  504. }
  505. static void *alloc_devicedirver(void *address){
  506. int fd;
  507. void *map_address;
  508. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  509. return (void *)-1;
  510. }
  511. map_address = mmap(address, BUFFER_SIZE,
  512. PROT_READ | PROT_WRITE,
  513. MAP_FILE | MAP_SHARED,
  514. fd, 0);
  515. if (map_address != (void *)-1) {
  516. release_info[release_pos].address = map_address;
  517. release_info[release_pos].attr = fd;
  518. release_info[release_pos].func = alloc_devicedirver_free;
  519. release_pos ++;
  520. }
  521. return map_address;
  522. }
  523. #endif
  524. #ifdef ALLOC_SHM
  525. static void alloc_shm_free(struct release_t *release){
  526. if (shmdt(release -> address)) {
  527. printf("OpenBLAS : Shared memory unmap failed.\n");
  528. }
  529. }
  530. static void *alloc_shm(void *address){
  531. void *map_address;
  532. int shmid;
  533. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  534. map_address = (void *)shmat(shmid, address, 0);
  535. if (map_address != (void *)-1){
  536. #ifdef OS_LINUX
  537. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  538. #endif
  539. shmctl(shmid, IPC_RMID, 0);
  540. release_info[release_pos].address = map_address;
  541. release_info[release_pos].attr = shmid;
  542. release_info[release_pos].func = alloc_shm_free;
  543. release_pos ++;
  544. }
  545. return map_address;
  546. }
  547. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  548. static void alloc_hugetlb_free(struct release_t *release){
  549. #if defined(OS_LINUX) || defined(OS_AIX)
  550. if (shmdt(release -> address)) {
  551. printf("OpenBLAS : Hugepage unmap failed.\n");
  552. }
  553. #endif
  554. #ifdef __sun__
  555. munmap(release -> address, BUFFER_SIZE);
  556. #endif
  557. #ifdef OS_WINDOWS
  558. VirtualFree(release -> address, BUFFER_SIZE, MEM_LARGE_PAGES | MEM_DECOMMIT);
  559. #endif
  560. }
  561. static void *alloc_hugetlb(void *address){
  562. void *map_address = (void *)-1;
  563. #if defined(OS_LINUX) || defined(OS_AIX)
  564. int shmid;
  565. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  566. #ifdef OS_LINUX
  567. SHM_HUGETLB |
  568. #endif
  569. #ifdef OS_AIX
  570. SHM_LGPAGE | SHM_PIN |
  571. #endif
  572. IPC_CREAT | SHM_R | SHM_W);
  573. if (shmid != -1) {
  574. map_address = (void *)shmat(shmid, address, SHM_RND);
  575. #ifdef OS_LINUX
  576. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  577. #endif
  578. if (map_address != (void *)-1){
  579. shmctl(shmid, IPC_RMID, 0);
  580. }
  581. }
  582. #endif
  583. #ifdef __sun__
  584. struct memcntl_mha mha;
  585. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  586. mha.mha_flags = 0;
  587. mha.mha_pagesize = HUGE_PAGESIZE;
  588. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  589. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  590. #endif
  591. #ifdef OS_WINDOWS
  592. HANDLE hToken;
  593. TOKEN_PRIVILEGES tp;
  594. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  595. tp.PrivilegeCount = 1;
  596. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  597. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  598. CloseHandle(hToken);
  599. return (void*)-1;
  600. }
  601. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  602. CloseHandle(hToken);
  603. return (void*)-1;
  604. }
  605. map_address = (void *)VirtualAlloc(address,
  606. BUFFER_SIZE,
  607. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  608. PAGE_READWRITE);
  609. tp.Privileges[0].Attributes = 0;
  610. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  611. if (map_address == (void *)NULL) map_address = (void *)-1;
  612. #endif
  613. if (map_address != (void *)-1){
  614. release_info[release_pos].address = map_address;
  615. release_info[release_pos].func = alloc_hugetlb_free;
  616. release_pos ++;
  617. }
  618. return map_address;
  619. }
  620. #endif
  621. #endif
  622. #ifdef ALLOC_HUGETLBFILE
  623. static int hugetlb_pid = 0;
  624. static void alloc_hugetlbfile_free(struct release_t *release){
  625. if (munmap(release -> address, BUFFER_SIZE)) {
  626. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  627. }
  628. if (close(release -> attr)) {
  629. printf("OpenBLAS : HugeTLBfs close failed.\n");
  630. }
  631. }
  632. static void *alloc_hugetlbfile(void *address){
  633. void *map_address = (void *)-1;
  634. int fd;
  635. char filename[64];
  636. if (!hugetlb_pid) hugetlb_pid = getpid();
  637. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  638. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  639. return (void *)-1;
  640. }
  641. unlink(filename);
  642. map_address = mmap(address, BUFFER_SIZE,
  643. PROT_READ | PROT_WRITE,
  644. MAP_SHARED,
  645. fd, 0);
  646. if (map_address != (void *)-1) {
  647. release_info[release_pos].address = map_address;
  648. release_info[release_pos].attr = fd;
  649. release_info[release_pos].func = alloc_hugetlbfile_free;
  650. release_pos ++;
  651. }
  652. return map_address;
  653. }
  654. #endif
  655. /* Global lock for memory allocation */
  656. #if defined(USE_PTHREAD_LOCK)
  657. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  658. #elif defined(USE_PTHREAD_SPINLOCK)
  659. static pthread_spinlock_t alloc_lock = 0;
  660. #else
  661. static BLASULONG alloc_lock = 0UL;
  662. #endif
  663. #ifdef SEEK_ADDRESS
  664. static BLASULONG base_address = 0UL;
  665. #else
  666. static BLASULONG base_address = BASE_ADDRESS;
  667. #endif
  668. static volatile struct {
  669. BLASULONG lock;
  670. void *addr;
  671. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  672. int pos;
  673. #endif
  674. int used;
  675. #ifndef __64BIT__
  676. char dummy[48];
  677. #else
  678. char dummy[40];
  679. #endif
  680. } memory[NUM_BUFFERS];
  681. static int memory_initialized = 0;
  682. /* Memory allocation routine */
  683. /* procpos ... indicates where it comes from */
  684. /* 0 : Level 3 functions */
  685. /* 1 : Level 2 functions */
  686. /* 2 : Thread */
  687. void *blas_memory_alloc(int procpos){
  688. int position;
  689. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  690. int mypos;
  691. #endif
  692. void *map_address;
  693. void *(*memoryalloc[])(void *address) = {
  694. #ifdef ALLOC_DEVICEDRIVER
  695. alloc_devicedirver,
  696. #endif
  697. /* Hugetlb implicitly assumes ALLOC_SHM */
  698. #ifdef ALLOC_SHM
  699. alloc_shm,
  700. #endif
  701. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  702. alloc_hugetlb,
  703. #endif
  704. #ifdef ALLOC_MMAP
  705. alloc_mmap,
  706. #endif
  707. #ifdef ALLOC_QALLOC
  708. alloc_qalloc,
  709. #endif
  710. #ifdef ALLOC_WINDOWS
  711. alloc_windows,
  712. #endif
  713. #ifdef ALLOC_MALLOC
  714. alloc_malloc,
  715. #endif
  716. NULL,
  717. };
  718. void *(**func)(void *address);
  719. if (!memory_initialized) {
  720. LOCK_COMMAND(&alloc_lock);
  721. if (!memory_initialized) {
  722. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  723. for (position = 0; position < NUM_BUFFERS; position ++){
  724. memory[position].addr = (void *)0;
  725. memory[position].pos = -1;
  726. memory[position].used = 0;
  727. memory[position].lock = 0;
  728. }
  729. #endif
  730. #ifdef DYNAMIC_ARCH
  731. gotoblas_dynamic_init();
  732. #endif
  733. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  734. gotoblas_affinity_init();
  735. #endif
  736. #ifdef SMP
  737. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  738. #endif
  739. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64)
  740. #ifndef DYNAMIC_ARCH
  741. blas_set_parameter();
  742. #endif
  743. #endif
  744. memory_initialized = 1;
  745. }
  746. UNLOCK_COMMAND(&alloc_lock);
  747. }
  748. #ifdef DEBUG
  749. printf("Alloc Start ...\n");
  750. #endif
  751. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  752. mypos = WhereAmI();
  753. position = mypos;
  754. while (position > NUM_BUFFERS) position >>= 1;
  755. do {
  756. if (!memory[position].used && (memory[position].pos == mypos)) {
  757. blas_lock(&memory[position].lock);
  758. if (!memory[position].used) goto allocation;
  759. blas_unlock(&memory[position].lock);
  760. }
  761. position ++;
  762. } while (position < NUM_BUFFERS);
  763. #endif
  764. position = 0;
  765. do {
  766. if (!memory[position].used) {
  767. blas_lock(&memory[position].lock);
  768. if (!memory[position].used) goto allocation;
  769. blas_unlock(&memory[position].lock);
  770. }
  771. position ++;
  772. } while (position < NUM_BUFFERS);
  773. goto error;
  774. allocation :
  775. #ifdef DEBUG
  776. printf(" Position -> %d\n", position);
  777. #endif
  778. memory[position].used = 1;
  779. blas_unlock(&memory[position].lock);
  780. if (!memory[position].addr) {
  781. do {
  782. #ifdef DEBUG
  783. printf("Allocation Start : %lx\n", base_address);
  784. #endif
  785. map_address = (void *)-1;
  786. func = &memoryalloc[0];
  787. while ((func != NULL) && (map_address == (void *) -1)) {
  788. map_address = (*func)((void *)base_address);
  789. #ifdef ALLOC_DEVICEDRIVER
  790. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  791. fprintf(stderr, "OpenBLAS Warning ... Physically contigous allocation was failed.\n");
  792. }
  793. #endif
  794. #ifdef ALLOC_HUGETLBFILE
  795. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  796. #ifndef OS_WINDOWS
  797. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  798. #endif
  799. }
  800. #endif
  801. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  802. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  803. #endif
  804. func ++;
  805. }
  806. #ifdef DEBUG
  807. printf(" Success -> %08lx\n", map_address);
  808. #endif
  809. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  810. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  811. } while ((BLASLONG)map_address == -1);
  812. memory[position].addr = map_address;
  813. #ifdef DEBUG
  814. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  815. #endif
  816. }
  817. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  818. if (memory[position].pos == -1) memory[position].pos = mypos;
  819. #endif
  820. #ifdef DYNAMIC_ARCH
  821. if (memory_initialized == 1) {
  822. LOCK_COMMAND(&alloc_lock);
  823. if (memory_initialized == 1) {
  824. if (!gotoblas) gotoblas_dynamic_init();
  825. memory_initialized = 2;
  826. }
  827. UNLOCK_COMMAND(&alloc_lock);
  828. }
  829. #endif
  830. #ifdef DEBUG
  831. printf("Mapped : %p %3d\n\n",
  832. (void *)memory[position].addr, position);
  833. #endif
  834. return (void *)memory[position].addr;
  835. error:
  836. printf("BLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  837. return NULL;
  838. }
  839. void blas_memory_free(void *free_area){
  840. int position;
  841. #ifdef DEBUG
  842. printf("Unmapped Start : %p ...\n", free_area);
  843. #endif
  844. position = 0;
  845. while ((memory[position].addr != free_area)
  846. && (position < NUM_BUFFERS)) position++;
  847. if (memory[position].addr != free_area) goto error;
  848. #ifdef DEBUG
  849. printf(" Position : %d\n", position);
  850. #endif
  851. // arm: ensure all writes are finished before other thread takes this memory
  852. WMB;
  853. memory[position].used = 0;
  854. #ifdef DEBUG
  855. printf("Unmap Succeeded.\n\n");
  856. #endif
  857. return;
  858. error:
  859. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  860. #ifdef DEBUG
  861. for (position = 0; position < NUM_BUFFERS; position++)
  862. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  863. #endif
  864. return;
  865. }
  866. void *blas_memory_alloc_nolock(int unused) {
  867. void *map_address;
  868. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  869. return map_address;
  870. }
  871. void blas_memory_free_nolock(void * map_address) {
  872. free(map_address);
  873. }
  874. void blas_shutdown(void){
  875. int pos;
  876. #ifdef SMP
  877. BLASFUNC(blas_thread_shutdown)();
  878. #endif
  879. LOCK_COMMAND(&alloc_lock);
  880. for (pos = 0; pos < release_pos; pos ++) {
  881. release_info[pos].func(&release_info[pos]);
  882. }
  883. #ifdef SEEK_ADDRESS
  884. base_address = 0UL;
  885. #else
  886. base_address = BASE_ADDRESS;
  887. #endif
  888. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  889. memory[pos].addr = (void *)0;
  890. memory[pos].used = 0;
  891. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  892. memory[pos].pos = -1;
  893. #endif
  894. memory[pos].lock = 0;
  895. }
  896. UNLOCK_COMMAND(&alloc_lock);
  897. return;
  898. }
  899. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  900. #ifdef SMP
  901. #if defined(USE_PTHREAD_LOCK)
  902. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  903. #elif defined(USE_PTHREAD_SPINLOCK)
  904. static pthread_spinlock_t init_lock = 0;
  905. #else
  906. static BLASULONG init_lock = 0UL;
  907. #endif
  908. #endif
  909. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  910. void *sa, void *sb, BLASLONG pos) {
  911. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  912. size_t size;
  913. BLASULONG buffer;
  914. size = BUFFER_SIZE - PAGESIZE;
  915. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  916. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  917. if (hot_alloc != 2) {
  918. #endif
  919. #ifdef SMP
  920. LOCK_COMMAND(&init_lock);
  921. #endif
  922. while (size > 0) {
  923. *(int *)buffer = size;
  924. buffer += PAGESIZE;
  925. size -= PAGESIZE;
  926. }
  927. #ifdef SMP
  928. UNLOCK_COMMAND(&init_lock);
  929. #endif
  930. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  931. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  932. while (size > 0) {
  933. *(int *)buffer = size;
  934. buffer += 64;
  935. size -= 64;
  936. }
  937. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  938. }
  939. #endif
  940. #endif
  941. }
  942. #ifdef SMP
  943. static void _init_thread_memory(void *buffer) {
  944. blas_queue_t queue[MAX_CPU_NUMBER];
  945. int num_cpu;
  946. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  947. blas_queue_init(&queue[num_cpu]);
  948. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  949. queue[num_cpu].routine = &_touch_memory;
  950. queue[num_cpu].args = NULL;
  951. queue[num_cpu].next = &queue[num_cpu + 1];
  952. }
  953. queue[num_cpu - 1].next = NULL;
  954. queue[0].sa = buffer;
  955. exec_blas(num_cpu, queue);
  956. }
  957. #endif
  958. static void gotoblas_memory_init(void) {
  959. void *buffer;
  960. hot_alloc = 1;
  961. buffer = (void *)blas_memory_alloc(0);
  962. #ifdef SMP
  963. if (blas_cpu_number == 0) blas_get_cpu_number();
  964. #ifdef SMP_SERVER
  965. if (blas_server_avail == 0) blas_thread_init();
  966. #endif
  967. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  968. #else
  969. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  970. #endif
  971. blas_memory_free(buffer);
  972. }
  973. #endif
  974. /* Initialization for all function; this function should be called before main */
  975. static int gotoblas_initialized = 0;
  976. extern void openblas_read_env();
  977. void CONSTRUCTOR gotoblas_init(void) {
  978. if (gotoblas_initialized) return;
  979. #ifdef SMP
  980. openblas_fork_handler();
  981. #endif
  982. openblas_read_env();
  983. #ifdef PROFILE
  984. moncontrol (0);
  985. #endif
  986. #ifdef DYNAMIC_ARCH
  987. gotoblas_dynamic_init();
  988. #endif
  989. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  990. gotoblas_affinity_init();
  991. #endif
  992. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  993. gotoblas_memory_init();
  994. #endif
  995. //#if defined(OS_LINUX)
  996. #if 0
  997. struct rlimit curlimit;
  998. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  999. {
  1000. if ( curlimit.rlim_cur != curlimit.rlim_max )
  1001. {
  1002. curlimit.rlim_cur = curlimit.rlim_max;
  1003. setrlimit(RLIMIT_STACK, &curlimit);
  1004. }
  1005. }
  1006. #endif
  1007. #ifdef SMP
  1008. if (blas_cpu_number == 0) blas_get_cpu_number();
  1009. #ifdef SMP_SERVER
  1010. if (blas_server_avail == 0) blas_thread_init();
  1011. #endif
  1012. #endif
  1013. #ifdef FUNCTION_PROFILE
  1014. gotoblas_profile_init();
  1015. #endif
  1016. gotoblas_initialized = 1;
  1017. #ifdef PROFILE
  1018. moncontrol (1);
  1019. #endif
  1020. }
  1021. void DESTRUCTOR gotoblas_quit(void) {
  1022. if (gotoblas_initialized == 0) return;
  1023. blas_shutdown();
  1024. #ifdef PROFILE
  1025. moncontrol (0);
  1026. #endif
  1027. #ifdef FUNCTION_PROFILE
  1028. gotoblas_profile_quit();
  1029. #endif
  1030. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1031. gotoblas_affinity_quit();
  1032. #endif
  1033. #ifdef DYNAMIC_ARCH
  1034. gotoblas_dynamic_quit();
  1035. #endif
  1036. gotoblas_initialized = 0;
  1037. #ifdef PROFILE
  1038. moncontrol (1);
  1039. #endif
  1040. }
  1041. #if defined(_MSC_VER) && !defined(__clang__)
  1042. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  1043. {
  1044. switch (ul_reason_for_call)
  1045. {
  1046. case DLL_PROCESS_ATTACH:
  1047. gotoblas_init();
  1048. break;
  1049. case DLL_THREAD_ATTACH:
  1050. break;
  1051. case DLL_THREAD_DETACH:
  1052. break;
  1053. case DLL_PROCESS_DETACH:
  1054. gotoblas_quit();
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. return TRUE;
  1060. }
  1061. /*
  1062. This is to allow static linking.
  1063. Code adapted from Google performance tools:
  1064. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  1065. Reference:
  1066. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  1067. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  1068. */
  1069. static int on_process_term(void)
  1070. {
  1071. gotoblas_quit();
  1072. return 0;
  1073. }
  1074. #ifdef _WIN64
  1075. #pragma comment(linker, "/INCLUDE:_tls_used")
  1076. #else
  1077. #pragma comment(linker, "/INCLUDE:__tls_used")
  1078. #endif
  1079. #pragma data_seg(push, old_seg)
  1080. #pragma data_seg(".CRT$XLB")
  1081. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  1082. #pragma data_seg(".CRT$XTU")
  1083. static int(*p_process_term)(void) = on_process_term;
  1084. #pragma data_seg(pop, old_seg)
  1085. #endif
  1086. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  1087. /* Don't call me; this is just work around for PGI / Sun bug */
  1088. void gotoblas_dummy_for_PGI(void) {
  1089. gotoblas_init();
  1090. gotoblas_quit();
  1091. #if 0
  1092. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  1093. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  1094. #else
  1095. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  1096. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  1097. #endif
  1098. }
  1099. #endif