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