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 33 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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(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. #ifdef OS_LINUX
  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. int blas_get_cpu_number(void){
  244. env_var_t p;
  245. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  246. int max_num;
  247. #endif
  248. int blas_goto_num = 0;
  249. int blas_omp_num = 0;
  250. if (blas_num_threads) return blas_num_threads;
  251. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  252. max_num = get_num_procs();
  253. #endif
  254. blas_goto_num = 0;
  255. #ifndef USE_OPENMP
  256. if (readenv(p,"OPENBLAS_NUM_THREADS")) blas_goto_num = atoi(p);
  257. if (blas_goto_num < 0) blas_goto_num = 0;
  258. if (blas_goto_num == 0) {
  259. if (readenv(p,"GOTO_NUM_THREADS")) blas_goto_num = atoi(p);
  260. if (blas_goto_num < 0) blas_goto_num = 0;
  261. }
  262. #endif
  263. blas_omp_num = 0;
  264. if (readenv(p,"OMP_NUM_THREADS")) blas_omp_num = atoi(p);
  265. if (blas_omp_num < 0) blas_omp_num = 0;
  266. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  267. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  268. else blas_num_threads = MAX_CPU_NUMBER;
  269. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  270. if (blas_num_threads > max_num) blas_num_threads = max_num;
  271. #endif
  272. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  273. #ifdef DEBUG
  274. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  275. #endif
  276. blas_cpu_number = blas_num_threads;
  277. return blas_num_threads;
  278. }
  279. #endif
  280. int openblas_get_num_procs(void) {
  281. #ifndef SMP
  282. return 1;
  283. #else
  284. return get_num_procs();
  285. #endif
  286. }
  287. int openblas_get_num_threads(void) {
  288. #ifndef SMP
  289. return 1;
  290. #else
  291. return blas_get_cpu_number();
  292. #endif
  293. }
  294. struct release_t {
  295. void *address;
  296. void (*func)(struct release_t *);
  297. long attr;
  298. };
  299. int hugetlb_allocated = 0;
  300. static struct release_t release_info[NUM_BUFFERS];
  301. static int release_pos = 0;
  302. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  303. static int hot_alloc = 0;
  304. #endif
  305. #ifdef ALLOC_MMAP
  306. static void alloc_mmap_free(struct release_t *release){
  307. if (munmap(release -> address, BUFFER_SIZE)) {
  308. printf("OpenBLAS : munmap failed\n");
  309. }
  310. }
  311. #ifdef NO_WARMUP
  312. static void *alloc_mmap(void *address){
  313. void *map_address;
  314. if (address){
  315. map_address = mmap(address,
  316. BUFFER_SIZE,
  317. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  318. } else {
  319. map_address = mmap(address,
  320. BUFFER_SIZE,
  321. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  322. }
  323. if (map_address != (void *)-1) {
  324. release_info[release_pos].address = map_address;
  325. release_info[release_pos].func = alloc_mmap_free;
  326. release_pos ++;
  327. }
  328. #ifdef OS_LINUX
  329. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  330. #endif
  331. return map_address;
  332. }
  333. #else
  334. #define BENCH_ITERATION 4
  335. #define SCALING 2
  336. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  337. BLASULONG original, *p;
  338. BLASULONG start, stop, min;
  339. int iter, i, count;
  340. min = (BLASULONG)-1;
  341. original = *(BLASULONG *)(address + size - PAGESIZE);
  342. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  343. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  344. p = (BLASULONG *)address;
  345. count = size / PAGESIZE;
  346. start = rpcc();
  347. for (i = 0; i < count; i ++) {
  348. p = (BLASULONG *)(*p);
  349. }
  350. stop = rpcc();
  351. if (min > stop - start) min = stop - start;
  352. }
  353. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  354. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  355. return min;
  356. }
  357. static void *alloc_mmap(void *address){
  358. void *map_address, *best_address;
  359. BLASULONG best, start, current;
  360. BLASULONG allocsize;
  361. if (address){
  362. /* Just give up use advanced operation */
  363. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  364. #ifdef OS_LINUX
  365. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  366. #endif
  367. } else {
  368. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  369. if (hot_alloc == 0) {
  370. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  371. #ifdef OS_LINUX
  372. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  373. #endif
  374. } else {
  375. #endif
  376. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  377. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  378. if (map_address != (void *)-1) {
  379. #ifdef OS_LINUX
  380. #ifdef DEBUG
  381. int ret=0;
  382. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  383. if(ret==-1){
  384. int errsv=errno;
  385. perror("OpenBLAS alloc_mmap:");
  386. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  387. }
  388. #else
  389. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  390. #endif
  391. #endif
  392. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  393. start = (BLASULONG)map_address;
  394. current = (SCALING - 1) * BUFFER_SIZE;
  395. while(current > 0) {
  396. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  397. start += PAGESIZE;
  398. current -= PAGESIZE;
  399. }
  400. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  401. start = (BLASULONG)map_address;
  402. best = (BLASULONG)-1;
  403. best_address = map_address;
  404. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  405. current = run_bench(start, allocsize);
  406. if (best > current) {
  407. best = current;
  408. best_address = (void *)start;
  409. }
  410. start += PAGESIZE;
  411. }
  412. if ((BLASULONG)best_address > (BLASULONG)map_address)
  413. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  414. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  415. map_address = best_address;
  416. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  417. hot_alloc = 2;
  418. #endif
  419. }
  420. }
  421. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  422. }
  423. #endif
  424. if (map_address != (void *)-1) {
  425. release_info[release_pos].address = map_address;
  426. release_info[release_pos].func = alloc_mmap_free;
  427. release_pos ++;
  428. }
  429. return map_address;
  430. }
  431. #endif
  432. #endif
  433. #ifdef ALLOC_MALLOC
  434. static void alloc_malloc_free(struct release_t *release){
  435. free(release -> address);
  436. }
  437. static void *alloc_malloc(void *address){
  438. void *map_address;
  439. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  440. if (map_address == (void *)NULL) map_address = (void *)-1;
  441. if (map_address != (void *)-1) {
  442. release_info[release_pos].address = map_address;
  443. release_info[release_pos].func = alloc_malloc_free;
  444. release_pos ++;
  445. }
  446. return map_address;
  447. }
  448. #endif
  449. #ifdef ALLOC_QALLOC
  450. void *qalloc(int flags, size_t bytes);
  451. void *qfree (void *address);
  452. #define QNONCACHE 0x1
  453. #define QCOMMS 0x2
  454. #define QFAST 0x4
  455. static void alloc_qalloc_free(struct release_t *release){
  456. qfree(release -> address);
  457. }
  458. static void *alloc_qalloc(void *address){
  459. void *map_address;
  460. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  461. if (map_address == (void *)NULL) map_address = (void *)-1;
  462. if (map_address != (void *)-1) {
  463. release_info[release_pos].address = map_address;
  464. release_info[release_pos].func = alloc_qalloc_free;
  465. release_pos ++;
  466. }
  467. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  468. }
  469. #endif
  470. #ifdef ALLOC_WINDOWS
  471. static void alloc_windows_free(struct release_t *release){
  472. VirtualFree(release -> address, BUFFER_SIZE, MEM_DECOMMIT);
  473. }
  474. static void *alloc_windows(void *address){
  475. void *map_address;
  476. map_address = VirtualAlloc(address,
  477. BUFFER_SIZE,
  478. MEM_RESERVE | MEM_COMMIT,
  479. PAGE_READWRITE);
  480. if (map_address == (void *)NULL) map_address = (void *)-1;
  481. if (map_address != (void *)-1) {
  482. release_info[release_pos].address = map_address;
  483. release_info[release_pos].func = alloc_windows_free;
  484. release_pos ++;
  485. }
  486. return map_address;
  487. }
  488. #endif
  489. #ifdef ALLOC_DEVICEDRIVER
  490. #ifndef DEVICEDRIVER_NAME
  491. #define DEVICEDRIVER_NAME "/dev/mapper"
  492. #endif
  493. static void alloc_devicedirver_free(struct release_t *release){
  494. if (munmap(release -> address, BUFFER_SIZE)) {
  495. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  496. }
  497. if (close(release -> attr)) {
  498. printf("OpenBLAS : Bugphysarea close failed.\n");
  499. }
  500. }
  501. static void *alloc_devicedirver(void *address){
  502. int fd;
  503. void *map_address;
  504. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  505. return (void *)-1;
  506. }
  507. map_address = mmap(address, BUFFER_SIZE,
  508. PROT_READ | PROT_WRITE,
  509. MAP_FILE | MAP_SHARED,
  510. fd, 0);
  511. if (map_address != (void *)-1) {
  512. release_info[release_pos].address = map_address;
  513. release_info[release_pos].attr = fd;
  514. release_info[release_pos].func = alloc_devicedirver_free;
  515. release_pos ++;
  516. }
  517. return map_address;
  518. }
  519. #endif
  520. #ifdef ALLOC_SHM
  521. static void alloc_shm_free(struct release_t *release){
  522. if (shmdt(release -> address)) {
  523. printf("OpenBLAS : Shared memory unmap failed.\n");
  524. }
  525. }
  526. static void *alloc_shm(void *address){
  527. void *map_address;
  528. int shmid;
  529. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  530. map_address = (void *)shmat(shmid, address, 0);
  531. if (map_address != (void *)-1){
  532. #ifdef OS_LINUX
  533. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  534. #endif
  535. shmctl(shmid, IPC_RMID, 0);
  536. release_info[release_pos].address = map_address;
  537. release_info[release_pos].attr = shmid;
  538. release_info[release_pos].func = alloc_shm_free;
  539. release_pos ++;
  540. }
  541. return map_address;
  542. }
  543. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  544. static void alloc_hugetlb_free(struct release_t *release){
  545. #if defined(OS_LINUX) || defined(OS_AIX)
  546. if (shmdt(release -> address)) {
  547. printf("OpenBLAS : Hugepage unmap failed.\n");
  548. }
  549. #endif
  550. #ifdef __sun__
  551. munmap(release -> address, BUFFER_SIZE);
  552. #endif
  553. #ifdef OS_WINDOWS
  554. VirtualFree(release -> address, BUFFER_SIZE, MEM_LARGE_PAGES | MEM_DECOMMIT);
  555. #endif
  556. }
  557. static void *alloc_hugetlb(void *address){
  558. void *map_address = (void *)-1;
  559. #if defined(OS_LINUX) || defined(OS_AIX)
  560. int shmid;
  561. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  562. #ifdef OS_LINUX
  563. SHM_HUGETLB |
  564. #endif
  565. #ifdef OS_AIX
  566. SHM_LGPAGE | SHM_PIN |
  567. #endif
  568. IPC_CREAT | SHM_R | SHM_W);
  569. if (shmid != -1) {
  570. map_address = (void *)shmat(shmid, address, SHM_RND);
  571. #ifdef OS_LINUX
  572. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  573. #endif
  574. if (map_address != (void *)-1){
  575. shmctl(shmid, IPC_RMID, 0);
  576. }
  577. }
  578. #endif
  579. #ifdef __sun__
  580. struct memcntl_mha mha;
  581. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  582. mha.mha_flags = 0;
  583. mha.mha_pagesize = HUGE_PAGESIZE;
  584. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  585. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  586. #endif
  587. #ifdef OS_WINDOWS
  588. HANDLE hToken;
  589. TOKEN_PRIVILEGES tp;
  590. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  591. tp.PrivilegeCount = 1;
  592. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  593. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  594. CloseHandle(hToken);
  595. return (void*)-1;
  596. }
  597. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  598. CloseHandle(hToken);
  599. return (void*)-1;
  600. }
  601. map_address = (void *)VirtualAlloc(address,
  602. BUFFER_SIZE,
  603. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  604. PAGE_READWRITE);
  605. tp.Privileges[0].Attributes = 0;
  606. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  607. if (map_address == (void *)NULL) map_address = (void *)-1;
  608. #endif
  609. if (map_address != (void *)-1){
  610. release_info[release_pos].address = map_address;
  611. release_info[release_pos].func = alloc_hugetlb_free;
  612. release_pos ++;
  613. }
  614. return map_address;
  615. }
  616. #endif
  617. #endif
  618. #ifdef ALLOC_HUGETLBFILE
  619. static int hugetlb_pid = 0;
  620. static void alloc_hugetlbfile_free(struct release_t *release){
  621. if (munmap(release -> address, BUFFER_SIZE)) {
  622. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  623. }
  624. if (close(release -> attr)) {
  625. printf("OpenBLAS : HugeTLBfs close failed.\n");
  626. }
  627. }
  628. static void *alloc_hugetlbfile(void *address){
  629. void *map_address = (void *)-1;
  630. int fd;
  631. char filename[64];
  632. if (!hugetlb_pid) hugetlb_pid = getpid();
  633. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  634. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  635. return (void *)-1;
  636. }
  637. unlink(filename);
  638. map_address = mmap(address, BUFFER_SIZE,
  639. PROT_READ | PROT_WRITE,
  640. MAP_SHARED,
  641. fd, 0);
  642. if (map_address != (void *)-1) {
  643. release_info[release_pos].address = map_address;
  644. release_info[release_pos].attr = fd;
  645. release_info[release_pos].func = alloc_hugetlbfile_free;
  646. release_pos ++;
  647. }
  648. return map_address;
  649. }
  650. #endif
  651. /* Global lock for memory allocation */
  652. #if defined(USE_PTHREAD_LOCK)
  653. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  654. #elif defined(USE_PTHREAD_SPINLOCK)
  655. static pthread_spinlock_t alloc_lock = 0;
  656. #else
  657. static BLASULONG alloc_lock = 0UL;
  658. #endif
  659. #ifdef SEEK_ADDRESS
  660. static BLASULONG base_address = 0UL;
  661. #else
  662. static BLASULONG base_address = BASE_ADDRESS;
  663. #endif
  664. static volatile struct {
  665. BLASULONG lock;
  666. void *addr;
  667. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  668. int pos;
  669. #endif
  670. int used;
  671. #ifndef __64BIT__
  672. char dummy[48];
  673. #else
  674. char dummy[40];
  675. #endif
  676. } memory[NUM_BUFFERS];
  677. static int memory_initialized = 0;
  678. /* Memory allocation routine */
  679. /* procpos ... indicates where it comes from */
  680. /* 0 : Level 3 functions */
  681. /* 1 : Level 2 functions */
  682. /* 2 : Thread */
  683. void *blas_memory_alloc(int procpos){
  684. int position;
  685. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  686. int mypos;
  687. #endif
  688. void *map_address;
  689. void *(*memoryalloc[])(void *address) = {
  690. #ifdef ALLOC_DEVICEDRIVER
  691. alloc_devicedirver,
  692. #endif
  693. /* Hugetlb implicitly assumes ALLOC_SHM */
  694. #ifdef ALLOC_SHM
  695. alloc_shm,
  696. #endif
  697. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  698. alloc_hugetlb,
  699. #endif
  700. #ifdef ALLOC_MMAP
  701. alloc_mmap,
  702. #endif
  703. #ifdef ALLOC_QALLOC
  704. alloc_qalloc,
  705. #endif
  706. #ifdef ALLOC_WINDOWS
  707. alloc_windows,
  708. #endif
  709. #ifdef ALLOC_MALLOC
  710. alloc_malloc,
  711. #endif
  712. NULL,
  713. };
  714. void *(**func)(void *address);
  715. if (!memory_initialized) {
  716. LOCK_COMMAND(&alloc_lock);
  717. if (!memory_initialized) {
  718. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  719. for (position = 0; position < NUM_BUFFERS; position ++){
  720. memory[position].addr = (void *)0;
  721. memory[position].pos = -1;
  722. memory[position].used = 0;
  723. memory[position].lock = 0;
  724. }
  725. #endif
  726. #ifdef DYNAMIC_ARCH
  727. gotoblas_dynamic_init();
  728. #endif
  729. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  730. gotoblas_affinity_init();
  731. #endif
  732. #ifdef SMP
  733. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  734. #endif
  735. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64)
  736. #ifndef DYNAMIC_ARCH
  737. blas_set_parameter();
  738. #endif
  739. #endif
  740. memory_initialized = 1;
  741. }
  742. UNLOCK_COMMAND(&alloc_lock);
  743. }
  744. #ifdef DEBUG
  745. printf("Alloc Start ...\n");
  746. #endif
  747. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  748. mypos = WhereAmI();
  749. position = mypos;
  750. while (position > NUM_BUFFERS) position >>= 1;
  751. do {
  752. if (!memory[position].used && (memory[position].pos == mypos)) {
  753. blas_lock(&memory[position].lock);
  754. if (!memory[position].used) goto allocation;
  755. blas_unlock(&memory[position].lock);
  756. }
  757. position ++;
  758. } while (position < NUM_BUFFERS);
  759. #endif
  760. position = 0;
  761. do {
  762. if (!memory[position].used) {
  763. blas_lock(&memory[position].lock);
  764. if (!memory[position].used) goto allocation;
  765. blas_unlock(&memory[position].lock);
  766. }
  767. position ++;
  768. } while (position < NUM_BUFFERS);
  769. goto error;
  770. allocation :
  771. #ifdef DEBUG
  772. printf(" Position -> %d\n", position);
  773. #endif
  774. memory[position].used = 1;
  775. blas_unlock(&memory[position].lock);
  776. if (!memory[position].addr) {
  777. do {
  778. #ifdef DEBUG
  779. printf("Allocation Start : %lx\n", base_address);
  780. #endif
  781. map_address = (void *)-1;
  782. func = &memoryalloc[0];
  783. while ((func != NULL) && (map_address == (void *) -1)) {
  784. map_address = (*func)((void *)base_address);
  785. #ifdef ALLOC_DEVICEDRIVER
  786. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  787. fprintf(stderr, "OpenBLAS Warning ... Physically contigous allocation was failed.\n");
  788. }
  789. #endif
  790. #ifdef ALLOC_HUGETLBFILE
  791. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  792. #ifndef OS_WINDOWS
  793. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  794. #endif
  795. }
  796. #endif
  797. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  798. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  799. #endif
  800. func ++;
  801. }
  802. #ifdef DEBUG
  803. printf(" Success -> %08lx\n", map_address);
  804. #endif
  805. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  806. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  807. } while ((BLASLONG)map_address == -1);
  808. memory[position].addr = map_address;
  809. #ifdef DEBUG
  810. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  811. #endif
  812. }
  813. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  814. if (memory[position].pos == -1) memory[position].pos = mypos;
  815. #endif
  816. #ifdef DYNAMIC_ARCH
  817. if (memory_initialized == 1) {
  818. LOCK_COMMAND(&alloc_lock);
  819. if (memory_initialized == 1) {
  820. if (!gotoblas) gotoblas_dynamic_init();
  821. memory_initialized = 2;
  822. }
  823. UNLOCK_COMMAND(&alloc_lock);
  824. }
  825. #endif
  826. #ifdef DEBUG
  827. printf("Mapped : %p %3d\n\n",
  828. (void *)memory[position].addr, position);
  829. #endif
  830. return (void *)memory[position].addr;
  831. error:
  832. printf("BLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  833. return NULL;
  834. }
  835. void blas_memory_free(void *free_area){
  836. int position;
  837. #ifdef DEBUG
  838. printf("Unmapped Start : %p ...\n", free_area);
  839. #endif
  840. position = 0;
  841. while ((memory[position].addr != free_area)
  842. && (position < NUM_BUFFERS)) position++;
  843. if (memory[position].addr != free_area) goto error;
  844. #ifdef DEBUG
  845. printf(" Position : %d\n", position);
  846. #endif
  847. // arm: ensure all writes are finished before other thread takes this memory
  848. WMB;
  849. memory[position].used = 0;
  850. #ifdef DEBUG
  851. printf("Unmap Succeeded.\n\n");
  852. #endif
  853. return;
  854. error:
  855. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  856. #ifdef DEBUG
  857. for (position = 0; position < NUM_BUFFERS; position++)
  858. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  859. #endif
  860. return;
  861. }
  862. void *blas_memory_alloc_nolock(int unused) {
  863. void *map_address;
  864. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  865. return map_address;
  866. }
  867. void blas_memory_free_nolock(void * map_address) {
  868. free(map_address);
  869. }
  870. void blas_shutdown(void){
  871. int pos;
  872. #ifdef SMP
  873. BLASFUNC(blas_thread_shutdown)();
  874. #endif
  875. LOCK_COMMAND(&alloc_lock);
  876. for (pos = 0; pos < release_pos; pos ++) {
  877. release_info[pos].func(&release_info[pos]);
  878. }
  879. #ifdef SEEK_ADDRESS
  880. base_address = 0UL;
  881. #else
  882. base_address = BASE_ADDRESS;
  883. #endif
  884. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  885. memory[pos].addr = (void *)0;
  886. memory[pos].used = 0;
  887. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  888. memory[pos].pos = -1;
  889. #endif
  890. memory[pos].lock = 0;
  891. }
  892. UNLOCK_COMMAND(&alloc_lock);
  893. return;
  894. }
  895. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  896. #ifdef SMP
  897. #if defined(USE_PTHREAD_LOCK)
  898. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  899. #elif defined(USE_PTHREAD_SPINLOCK)
  900. static pthread_spinlock_t init_lock = 0;
  901. #else
  902. static BLASULONG init_lock = 0UL;
  903. #endif
  904. #endif
  905. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  906. void *sa, void *sb, BLASLONG pos) {
  907. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  908. size_t size;
  909. BLASULONG buffer;
  910. size = BUFFER_SIZE - PAGESIZE;
  911. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  912. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  913. if (hot_alloc != 2) {
  914. #endif
  915. #ifdef SMP
  916. LOCK_COMMAND(&init_lock);
  917. #endif
  918. while (size > 0) {
  919. *(int *)buffer = size;
  920. buffer += PAGESIZE;
  921. size -= PAGESIZE;
  922. }
  923. #ifdef SMP
  924. UNLOCK_COMMAND(&init_lock);
  925. #endif
  926. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  927. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  928. while (size > 0) {
  929. *(int *)buffer = size;
  930. buffer += 64;
  931. size -= 64;
  932. }
  933. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  934. }
  935. #endif
  936. #endif
  937. }
  938. #ifdef SMP
  939. static void _init_thread_memory(void *buffer) {
  940. blas_queue_t queue[MAX_CPU_NUMBER];
  941. int num_cpu;
  942. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  943. blas_queue_init(&queue[num_cpu]);
  944. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  945. queue[num_cpu].routine = &_touch_memory;
  946. queue[num_cpu].args = NULL;
  947. queue[num_cpu].next = &queue[num_cpu + 1];
  948. }
  949. queue[num_cpu - 1].next = NULL;
  950. queue[0].sa = buffer;
  951. exec_blas(num_cpu, queue);
  952. }
  953. #endif
  954. static void gotoblas_memory_init(void) {
  955. void *buffer;
  956. hot_alloc = 1;
  957. buffer = (void *)blas_memory_alloc(0);
  958. #ifdef SMP
  959. if (blas_cpu_number == 0) blas_get_cpu_number();
  960. #ifdef SMP_SERVER
  961. if (blas_server_avail == 0) blas_thread_init();
  962. #endif
  963. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  964. #else
  965. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  966. #endif
  967. blas_memory_free(buffer);
  968. }
  969. #endif
  970. /* Initialization for all function; this function should be called before main */
  971. static int gotoblas_initialized = 0;
  972. void CONSTRUCTOR gotoblas_init(void) {
  973. if (gotoblas_initialized) return;
  974. #ifdef SMP
  975. openblas_fork_handler();
  976. #endif
  977. #ifdef PROFILE
  978. moncontrol (0);
  979. #endif
  980. #ifdef DYNAMIC_ARCH
  981. gotoblas_dynamic_init();
  982. #endif
  983. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  984. gotoblas_affinity_init();
  985. #endif
  986. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  987. gotoblas_memory_init();
  988. #endif
  989. #if defined(OS_LINUX)
  990. struct rlimit curlimit;
  991. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  992. {
  993. if ( curlimit.rlim_cur != curlimit.rlim_max )
  994. {
  995. curlimit.rlim_cur = curlimit.rlim_max;
  996. setrlimit(RLIMIT_STACK, &curlimit);
  997. }
  998. }
  999. #endif
  1000. #ifdef SMP
  1001. if (blas_cpu_number == 0) blas_get_cpu_number();
  1002. #ifdef SMP_SERVER
  1003. if (blas_server_avail == 0) blas_thread_init();
  1004. #endif
  1005. #endif
  1006. #ifdef FUNCTION_PROFILE
  1007. gotoblas_profile_init();
  1008. #endif
  1009. gotoblas_initialized = 1;
  1010. #ifdef PROFILE
  1011. moncontrol (1);
  1012. #endif
  1013. }
  1014. void DESTRUCTOR gotoblas_quit(void) {
  1015. if (gotoblas_initialized == 0) return;
  1016. blas_shutdown();
  1017. #ifdef PROFILE
  1018. moncontrol (0);
  1019. #endif
  1020. #ifdef FUNCTION_PROFILE
  1021. gotoblas_profile_quit();
  1022. #endif
  1023. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1024. gotoblas_affinity_quit();
  1025. #endif
  1026. #ifdef DYNAMIC_ARCH
  1027. gotoblas_dynamic_quit();
  1028. #endif
  1029. gotoblas_initialized = 0;
  1030. #ifdef PROFILE
  1031. moncontrol (1);
  1032. #endif
  1033. }
  1034. #if defined(_MSC_VER) && !defined(__clang__)
  1035. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  1036. {
  1037. switch (ul_reason_for_call)
  1038. {
  1039. case DLL_PROCESS_ATTACH:
  1040. gotoblas_init();
  1041. break;
  1042. case DLL_THREAD_ATTACH:
  1043. break;
  1044. case DLL_THREAD_DETACH:
  1045. break;
  1046. case DLL_PROCESS_DETACH:
  1047. gotoblas_quit();
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. return TRUE;
  1053. }
  1054. #endif
  1055. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  1056. /* Don't call me; this is just work around for PGI / Sun bug */
  1057. void gotoblas_dummy_for_PGI(void) {
  1058. gotoblas_init();
  1059. gotoblas_quit();
  1060. #if 0
  1061. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  1062. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  1063. #else
  1064. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  1065. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  1066. #endif
  1067. }
  1068. #endif