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

9 years ago
9 years ago
9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  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. #if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
  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. cpu_set_t *cpusetp;
  149. size_t size;
  150. int ret;
  151. // int i,n;
  152. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  153. #if !defined(OS_LINUX)
  154. return nums;
  155. #endif
  156. #if !defined(__GLIBC_PREREQ)
  157. return nums;
  158. #else
  159. #if !__GLIBC_PREREQ(2, 3)
  160. return nums;
  161. #endif
  162. #if !__GLIBC_PREREQ(2, 7)
  163. ret = sched_getaffinity(0,sizeof(cpu_set_t), cpusetp);
  164. if (ret!=0) return nums;
  165. n=0;
  166. #if !__GLIBC_PREREQ(2, 6)
  167. for (i=0;i<nums;i++)
  168. if (CPU_ISSET(i,cpusetp)) n++;
  169. nums=n;
  170. #else
  171. nums = CPU_COUNT(sizeof(cpu_set_t),cpusetp);
  172. #endif
  173. return nums;
  174. #else
  175. cpusetp = CPU_ALLOC(nums);
  176. if (cpusetp == NULL) return nums;
  177. size = CPU_ALLOC_SIZE(nums);
  178. ret = sched_getaffinity(0,size,cpusetp);
  179. if (ret!=0) return nums;
  180. nums = CPU_COUNT_S(size,cpusetp);
  181. CPU_FREE(cpusetp);
  182. return nums;
  183. #endif
  184. #endif
  185. }
  186. #endif
  187. #endif
  188. #ifdef OS_ANDROID
  189. int get_num_procs(void) {
  190. static int nums = 0;
  191. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  192. return nums;
  193. }
  194. #endif
  195. #ifdef OS_WINDOWS
  196. int get_num_procs(void) {
  197. static int nums = 0;
  198. if (nums == 0) {
  199. SYSTEM_INFO sysinfo;
  200. GetSystemInfo(&sysinfo);
  201. nums = sysinfo.dwNumberOfProcessors;
  202. }
  203. return nums;
  204. }
  205. #endif
  206. #if defined(OS_FREEBSD)
  207. int get_num_procs(void) {
  208. static int nums = 0;
  209. int m[2];
  210. size_t len;
  211. if (nums == 0) {
  212. m[0] = CTL_HW;
  213. m[1] = HW_NCPU;
  214. len = sizeof(int);
  215. sysctl(m, 2, &nums, &len, NULL, 0);
  216. }
  217. return nums;
  218. }
  219. #endif
  220. #if defined(OS_DARWIN)
  221. int get_num_procs(void) {
  222. static int nums = 0;
  223. size_t len;
  224. if (nums == 0){
  225. len = sizeof(int);
  226. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  227. }
  228. return nums;
  229. }
  230. /*
  231. void set_stack_limit(int limitMB){
  232. int result=0;
  233. struct rlimit rl;
  234. rlim_t StackSize;
  235. StackSize=limitMB*1024*1024;
  236. result=getrlimit(RLIMIT_STACK, &rl);
  237. if(result==0){
  238. if(rl.rlim_cur < StackSize){
  239. rl.rlim_cur=StackSize;
  240. result=setrlimit(RLIMIT_STACK, &rl);
  241. if(result !=0){
  242. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  243. }
  244. }
  245. }
  246. }
  247. */
  248. #endif
  249. /*
  250. OpenBLAS uses the numbers of CPU cores in multithreading.
  251. It can be set by openblas_set_num_threads(int num_threads);
  252. */
  253. int blas_cpu_number = 0;
  254. /*
  255. The numbers of threads in the thread pool.
  256. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  257. */
  258. int blas_num_threads = 0;
  259. int goto_get_num_procs (void) {
  260. return blas_cpu_number;
  261. }
  262. void openblas_fork_handler()
  263. {
  264. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  265. // built with "make USE_OPENMP=0".
  266. // Hanging can still happen when OpenBLAS is built against the libgomp
  267. // implementation of OpenMP. The problem is tracked at:
  268. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  269. // In the mean time build with USE_OPENMP=0 or link against another
  270. // implementation of OpenMP.
  271. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  272. int err;
  273. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
  274. if(err != 0)
  275. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  276. #endif
  277. }
  278. extern int openblas_num_threads_env();
  279. extern int openblas_goto_num_threads_env();
  280. extern int openblas_omp_num_threads_env();
  281. int blas_get_cpu_number(void){
  282. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  283. int max_num;
  284. #endif
  285. int blas_goto_num = 0;
  286. int blas_omp_num = 0;
  287. if (blas_num_threads) return blas_num_threads;
  288. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  289. max_num = get_num_procs();
  290. #endif
  291. // blas_goto_num = 0;
  292. #ifndef USE_OPENMP
  293. blas_goto_num=openblas_num_threads_env();
  294. if (blas_goto_num < 0) blas_goto_num = 0;
  295. if (blas_goto_num == 0) {
  296. blas_goto_num=openblas_goto_num_threads_env();
  297. if (blas_goto_num < 0) blas_goto_num = 0;
  298. }
  299. #endif
  300. // blas_omp_num = 0;
  301. blas_omp_num=openblas_omp_num_threads_env();
  302. if (blas_omp_num < 0) blas_omp_num = 0;
  303. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  304. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  305. else blas_num_threads = MAX_CPU_NUMBER;
  306. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_DARWIN) || defined(OS_ANDROID)
  307. if (blas_num_threads > max_num) blas_num_threads = max_num;
  308. #endif
  309. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  310. #ifdef DEBUG
  311. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  312. #endif
  313. blas_cpu_number = blas_num_threads;
  314. return blas_num_threads;
  315. }
  316. #endif
  317. int openblas_get_num_procs(void) {
  318. #ifndef SMP
  319. return 1;
  320. #else
  321. return get_num_procs();
  322. #endif
  323. }
  324. int openblas_get_num_threads(void) {
  325. #ifndef SMP
  326. return 1;
  327. #else
  328. // init blas_cpu_number if needed
  329. blas_get_cpu_number();
  330. return blas_cpu_number;
  331. #endif
  332. }
  333. struct release_t {
  334. void *address;
  335. void (*func)(struct release_t *);
  336. long attr;
  337. };
  338. int hugetlb_allocated = 0;
  339. static struct release_t release_info[NUM_BUFFERS];
  340. static int release_pos = 0;
  341. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  342. static int hot_alloc = 0;
  343. #endif
  344. /* Global lock for memory allocation */
  345. #if defined(USE_PTHREAD_LOCK)
  346. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  347. #elif defined(USE_PTHREAD_SPINLOCK)
  348. static pthread_spinlock_t alloc_lock = 0;
  349. #else
  350. static BLASULONG alloc_lock = 0UL;
  351. #endif
  352. #ifdef ALLOC_MMAP
  353. static void alloc_mmap_free(struct release_t *release){
  354. if (munmap(release -> address, BUFFER_SIZE)) {
  355. printf("OpenBLAS : munmap failed\n");
  356. }
  357. }
  358. #ifdef NO_WARMUP
  359. static void *alloc_mmap(void *address){
  360. void *map_address;
  361. if (address){
  362. map_address = mmap(address,
  363. BUFFER_SIZE,
  364. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  365. } else {
  366. map_address = mmap(address,
  367. BUFFER_SIZE,
  368. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  369. }
  370. if (map_address != (void *)-1) {
  371. #if defined(SMP) && !defined(USE_OPENMP)
  372. LOCK_COMMAND(&alloc_lock);
  373. #endif
  374. release_info[release_pos].address = map_address;
  375. release_info[release_pos].func = alloc_mmap_free;
  376. release_pos ++;
  377. #if defined(SMP) && !defined(USE_OPENMP)
  378. UNLOCK_COMMAND(&alloc_lock);
  379. #endif
  380. }
  381. #ifdef OS_LINUX
  382. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  383. #endif
  384. return map_address;
  385. }
  386. #else
  387. #define BENCH_ITERATION 4
  388. #define SCALING 2
  389. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  390. BLASULONG original, *p;
  391. BLASULONG start, stop, min;
  392. int iter, i, count;
  393. min = (BLASULONG)-1;
  394. original = *(BLASULONG *)(address + size - PAGESIZE);
  395. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  396. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  397. p = (BLASULONG *)address;
  398. count = size / PAGESIZE;
  399. start = rpcc();
  400. for (i = 0; i < count; i ++) {
  401. p = (BLASULONG *)(*p);
  402. }
  403. stop = rpcc();
  404. if (min > stop - start) min = stop - start;
  405. }
  406. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  407. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  408. return min;
  409. }
  410. static void *alloc_mmap(void *address){
  411. void *map_address, *best_address;
  412. BLASULONG best, start, current;
  413. BLASULONG allocsize;
  414. if (address){
  415. /* Just give up use advanced operation */
  416. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  417. #ifdef OS_LINUX
  418. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  419. #endif
  420. } else {
  421. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  422. if (hot_alloc == 0) {
  423. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  424. #ifdef OS_LINUX
  425. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  426. #endif
  427. } else {
  428. #endif
  429. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  430. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  431. if (map_address != (void *)-1) {
  432. #ifdef OS_LINUX
  433. #ifdef DEBUG
  434. int ret=0;
  435. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  436. if(ret==-1){
  437. int errsv=errno;
  438. perror("OpenBLAS alloc_mmap:");
  439. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  440. }
  441. #else
  442. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  443. #endif
  444. #endif
  445. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  446. start = (BLASULONG)map_address;
  447. current = (SCALING - 1) * BUFFER_SIZE;
  448. while(current > 0) {
  449. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  450. start += PAGESIZE;
  451. current -= PAGESIZE;
  452. }
  453. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  454. start = (BLASULONG)map_address;
  455. best = (BLASULONG)-1;
  456. best_address = map_address;
  457. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  458. current = run_bench(start, allocsize);
  459. if (best > current) {
  460. best = current;
  461. best_address = (void *)start;
  462. }
  463. start += PAGESIZE;
  464. }
  465. if ((BLASULONG)best_address > (BLASULONG)map_address)
  466. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  467. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  468. map_address = best_address;
  469. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  470. hot_alloc = 2;
  471. #endif
  472. }
  473. }
  474. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  475. }
  476. #endif
  477. if (map_address != (void *)-1) {
  478. #if defined(SMP) && !defined(USE_OPENMP)
  479. LOCK_COMMAND(&alloc_lock);
  480. #endif
  481. release_info[release_pos].address = map_address;
  482. release_info[release_pos].func = alloc_mmap_free;
  483. release_pos ++;
  484. #if defined(SMP) && !defined(USE_OPENMP)
  485. UNLOCK_COMMAND(&alloc_lock);
  486. #endif
  487. }
  488. return map_address;
  489. }
  490. #endif
  491. #endif
  492. #ifdef ALLOC_MALLOC
  493. static void alloc_malloc_free(struct release_t *release){
  494. free(release -> address);
  495. }
  496. static void *alloc_malloc(void *address){
  497. void *map_address;
  498. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  499. if (map_address == (void *)NULL) map_address = (void *)-1;
  500. if (map_address != (void *)-1) {
  501. release_info[release_pos].address = map_address;
  502. release_info[release_pos].func = alloc_malloc_free;
  503. release_pos ++;
  504. }
  505. return map_address;
  506. }
  507. #endif
  508. #ifdef ALLOC_QALLOC
  509. void *qalloc(int flags, size_t bytes);
  510. void *qfree (void *address);
  511. #define QNONCACHE 0x1
  512. #define QCOMMS 0x2
  513. #define QFAST 0x4
  514. static void alloc_qalloc_free(struct release_t *release){
  515. qfree(release -> address);
  516. }
  517. static void *alloc_qalloc(void *address){
  518. void *map_address;
  519. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  520. if (map_address == (void *)NULL) map_address = (void *)-1;
  521. if (map_address != (void *)-1) {
  522. release_info[release_pos].address = map_address;
  523. release_info[release_pos].func = alloc_qalloc_free;
  524. release_pos ++;
  525. }
  526. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  527. }
  528. #endif
  529. #ifdef ALLOC_WINDOWS
  530. static void alloc_windows_free(struct release_t *release){
  531. VirtualFree(release -> address, BUFFER_SIZE, MEM_DECOMMIT);
  532. }
  533. static void *alloc_windows(void *address){
  534. void *map_address;
  535. map_address = VirtualAlloc(address,
  536. BUFFER_SIZE,
  537. MEM_RESERVE | MEM_COMMIT,
  538. PAGE_READWRITE);
  539. if (map_address == (void *)NULL) map_address = (void *)-1;
  540. if (map_address != (void *)-1) {
  541. release_info[release_pos].address = map_address;
  542. release_info[release_pos].func = alloc_windows_free;
  543. release_pos ++;
  544. }
  545. return map_address;
  546. }
  547. #endif
  548. #ifdef ALLOC_DEVICEDRIVER
  549. #ifndef DEVICEDRIVER_NAME
  550. #define DEVICEDRIVER_NAME "/dev/mapper"
  551. #endif
  552. static void alloc_devicedirver_free(struct release_t *release){
  553. if (munmap(release -> address, BUFFER_SIZE)) {
  554. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  555. }
  556. if (close(release -> attr)) {
  557. printf("OpenBLAS : Bugphysarea close failed.\n");
  558. }
  559. }
  560. static void *alloc_devicedirver(void *address){
  561. int fd;
  562. void *map_address;
  563. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  564. return (void *)-1;
  565. }
  566. map_address = mmap(address, BUFFER_SIZE,
  567. PROT_READ | PROT_WRITE,
  568. MAP_FILE | MAP_SHARED,
  569. fd, 0);
  570. if (map_address != (void *)-1) {
  571. release_info[release_pos].address = map_address;
  572. release_info[release_pos].attr = fd;
  573. release_info[release_pos].func = alloc_devicedirver_free;
  574. release_pos ++;
  575. }
  576. return map_address;
  577. }
  578. #endif
  579. #ifdef ALLOC_SHM
  580. static void alloc_shm_free(struct release_t *release){
  581. if (shmdt(release -> address)) {
  582. printf("OpenBLAS : Shared memory unmap failed.\n");
  583. }
  584. }
  585. static void *alloc_shm(void *address){
  586. void *map_address;
  587. int shmid;
  588. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  589. map_address = (void *)shmat(shmid, address, 0);
  590. if (map_address != (void *)-1){
  591. #ifdef OS_LINUX
  592. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  593. #endif
  594. shmctl(shmid, IPC_RMID, 0);
  595. release_info[release_pos].address = map_address;
  596. release_info[release_pos].attr = shmid;
  597. release_info[release_pos].func = alloc_shm_free;
  598. release_pos ++;
  599. }
  600. return map_address;
  601. }
  602. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  603. static void alloc_hugetlb_free(struct release_t *release){
  604. #if defined(OS_LINUX) || defined(OS_AIX)
  605. if (shmdt(release -> address)) {
  606. printf("OpenBLAS : Hugepage unmap failed.\n");
  607. }
  608. #endif
  609. #ifdef __sun__
  610. munmap(release -> address, BUFFER_SIZE);
  611. #endif
  612. #ifdef OS_WINDOWS
  613. VirtualFree(release -> address, BUFFER_SIZE, MEM_LARGE_PAGES | MEM_DECOMMIT);
  614. #endif
  615. }
  616. static void *alloc_hugetlb(void *address){
  617. void *map_address = (void *)-1;
  618. #if defined(OS_LINUX) || defined(OS_AIX)
  619. int shmid;
  620. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  621. #ifdef OS_LINUX
  622. SHM_HUGETLB |
  623. #endif
  624. #ifdef OS_AIX
  625. SHM_LGPAGE | SHM_PIN |
  626. #endif
  627. IPC_CREAT | SHM_R | SHM_W);
  628. if (shmid != -1) {
  629. map_address = (void *)shmat(shmid, address, SHM_RND);
  630. #ifdef OS_LINUX
  631. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  632. #endif
  633. if (map_address != (void *)-1){
  634. shmctl(shmid, IPC_RMID, 0);
  635. }
  636. }
  637. #endif
  638. #ifdef __sun__
  639. struct memcntl_mha mha;
  640. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  641. mha.mha_flags = 0;
  642. mha.mha_pagesize = HUGE_PAGESIZE;
  643. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  644. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  645. #endif
  646. #ifdef OS_WINDOWS
  647. HANDLE hToken;
  648. TOKEN_PRIVILEGES tp;
  649. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  650. tp.PrivilegeCount = 1;
  651. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  652. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  653. CloseHandle(hToken);
  654. return (void*)-1;
  655. }
  656. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  657. CloseHandle(hToken);
  658. return (void*)-1;
  659. }
  660. map_address = (void *)VirtualAlloc(address,
  661. BUFFER_SIZE,
  662. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  663. PAGE_READWRITE);
  664. tp.Privileges[0].Attributes = 0;
  665. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  666. if (map_address == (void *)NULL) map_address = (void *)-1;
  667. #endif
  668. if (map_address != (void *)-1){
  669. release_info[release_pos].address = map_address;
  670. release_info[release_pos].func = alloc_hugetlb_free;
  671. release_pos ++;
  672. }
  673. return map_address;
  674. }
  675. #endif
  676. #endif
  677. #ifdef ALLOC_HUGETLBFILE
  678. static int hugetlb_pid = 0;
  679. static void alloc_hugetlbfile_free(struct release_t *release){
  680. if (munmap(release -> address, BUFFER_SIZE)) {
  681. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  682. }
  683. if (close(release -> attr)) {
  684. printf("OpenBLAS : HugeTLBfs close failed.\n");
  685. }
  686. }
  687. static void *alloc_hugetlbfile(void *address){
  688. void *map_address = (void *)-1;
  689. int fd;
  690. char filename[64];
  691. if (!hugetlb_pid) hugetlb_pid = getpid();
  692. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  693. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  694. return (void *)-1;
  695. }
  696. unlink(filename);
  697. map_address = mmap(address, BUFFER_SIZE,
  698. PROT_READ | PROT_WRITE,
  699. MAP_SHARED,
  700. fd, 0);
  701. if (map_address != (void *)-1) {
  702. release_info[release_pos].address = map_address;
  703. release_info[release_pos].attr = fd;
  704. release_info[release_pos].func = alloc_hugetlbfile_free;
  705. release_pos ++;
  706. }
  707. return map_address;
  708. }
  709. #endif
  710. #ifdef SEEK_ADDRESS
  711. static BLASULONG base_address = 0UL;
  712. #else
  713. static BLASULONG base_address = BASE_ADDRESS;
  714. #endif
  715. static volatile struct {
  716. BLASULONG lock;
  717. void *addr;
  718. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  719. int pos;
  720. #endif
  721. int used;
  722. #ifndef __64BIT__
  723. char dummy[48];
  724. #else
  725. char dummy[40];
  726. #endif
  727. } memory[NUM_BUFFERS];
  728. static int memory_initialized = 0;
  729. /* Memory allocation routine */
  730. /* procpos ... indicates where it comes from */
  731. /* 0 : Level 3 functions */
  732. /* 1 : Level 2 functions */
  733. /* 2 : Thread */
  734. void *blas_memory_alloc(int procpos){
  735. int position;
  736. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  737. int mypos;
  738. #endif
  739. void *map_address;
  740. void *(*memoryalloc[])(void *address) = {
  741. #ifdef ALLOC_DEVICEDRIVER
  742. alloc_devicedirver,
  743. #endif
  744. /* Hugetlb implicitly assumes ALLOC_SHM */
  745. #ifdef ALLOC_SHM
  746. alloc_shm,
  747. #endif
  748. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  749. alloc_hugetlb,
  750. #endif
  751. #ifdef ALLOC_MMAP
  752. alloc_mmap,
  753. #endif
  754. #ifdef ALLOC_QALLOC
  755. alloc_qalloc,
  756. #endif
  757. #ifdef ALLOC_WINDOWS
  758. alloc_windows,
  759. #endif
  760. #ifdef ALLOC_MALLOC
  761. alloc_malloc,
  762. #endif
  763. NULL,
  764. };
  765. void *(**func)(void *address);
  766. #if defined(SMP) && !defined(USE_OPENMP)
  767. LOCK_COMMAND(&alloc_lock);
  768. #endif
  769. if (!memory_initialized) {
  770. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  771. for (position = 0; position < NUM_BUFFERS; position ++){
  772. memory[position].addr = (void *)0;
  773. memory[position].pos = -1;
  774. memory[position].used = 0;
  775. memory[position].lock = 0;
  776. }
  777. #endif
  778. #ifdef DYNAMIC_ARCH
  779. gotoblas_dynamic_init();
  780. #endif
  781. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  782. gotoblas_affinity_init();
  783. #endif
  784. #ifdef SMP
  785. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  786. #endif
  787. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  788. #ifndef DYNAMIC_ARCH
  789. blas_set_parameter();
  790. #endif
  791. #endif
  792. memory_initialized = 1;
  793. }
  794. #if defined(SMP) && !defined(USE_OPENMP)
  795. UNLOCK_COMMAND(&alloc_lock);
  796. #endif
  797. #ifdef DEBUG
  798. printf("Alloc Start ...\n");
  799. #endif
  800. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  801. mypos = WhereAmI();
  802. position = mypos;
  803. while (position >= NUM_BUFFERS) position >>= 1;
  804. do {
  805. if (!memory[position].used && (memory[position].pos == mypos)) {
  806. #if defined(SMP) && !defined(USE_OPENMP)
  807. LOCK_COMMAND(&alloc_lock);
  808. #endif
  809. /* blas_lock(&memory[position].lock);*/
  810. if (!memory[position].used) goto allocation;
  811. #if defined(SMP) && !defined(USE_OPENMP)
  812. UNLOCK_COMMAND(&alloc_lock);
  813. #endif
  814. /* blas_unlock(&memory[position].lock);*/
  815. }
  816. position ++;
  817. } while (position < NUM_BUFFERS);
  818. #endif
  819. position = 0;
  820. do {
  821. /* if (!memory[position].used) { */
  822. #if defined(SMP) && !defined(USE_OPENMP)
  823. LOCK_COMMAND(&alloc_lock);
  824. #endif
  825. /* blas_lock(&memory[position].lock);*/
  826. if (!memory[position].used) goto allocation;
  827. #if defined(SMP) && !defined(USE_OPENMP)
  828. UNLOCK_COMMAND(&alloc_lock);
  829. #endif
  830. /* blas_unlock(&memory[position].lock);*/
  831. /* } */
  832. position ++;
  833. } while (position < NUM_BUFFERS);
  834. goto error;
  835. allocation :
  836. #ifdef DEBUG
  837. printf(" Position -> %d\n", position);
  838. #endif
  839. memory[position].used = 1;
  840. #if defined(SMP) && !defined(USE_OPENMP)
  841. UNLOCK_COMMAND(&alloc_lock);
  842. #endif
  843. /* blas_unlock(&memory[position].lock);*/
  844. if (!memory[position].addr) {
  845. do {
  846. #ifdef DEBUG
  847. printf("Allocation Start : %lx\n", base_address);
  848. #endif
  849. map_address = (void *)-1;
  850. func = &memoryalloc[0];
  851. while ((func != NULL) && (map_address == (void *) -1)) {
  852. map_address = (*func)((void *)base_address);
  853. #ifdef ALLOC_DEVICEDRIVER
  854. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  855. fprintf(stderr, "OpenBLAS Warning ... Physically contigous allocation was failed.\n");
  856. }
  857. #endif
  858. #ifdef ALLOC_HUGETLBFILE
  859. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  860. #ifndef OS_WINDOWS
  861. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  862. #endif
  863. }
  864. #endif
  865. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  866. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  867. #endif
  868. func ++;
  869. }
  870. #ifdef DEBUG
  871. printf(" Success -> %08lx\n", map_address);
  872. #endif
  873. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  874. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  875. } while ((BLASLONG)map_address == -1);
  876. #if defined(SMP) && !defined(USE_OPENMP)
  877. LOCK_COMMAND(&alloc_lock);
  878. #endif
  879. memory[position].addr = map_address;
  880. #if defined(SMP) && !defined(USE_OPENMP)
  881. UNLOCK_COMMAND(&alloc_lock);
  882. #endif
  883. #ifdef DEBUG
  884. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  885. #endif
  886. }
  887. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  888. if (memory[position].pos == -1) memory[position].pos = mypos;
  889. #endif
  890. #ifdef DYNAMIC_ARCH
  891. if (memory_initialized == 1) {
  892. #if defined(SMP) && !defined(USE_OPENMP)
  893. LOCK_COMMAND(&alloc_lock);
  894. #endif
  895. if (memory_initialized == 1) {
  896. if (!gotoblas) gotoblas_dynamic_init();
  897. memory_initialized = 2;
  898. }
  899. #if defined(SMP) && !defined(USE_OPENMP)
  900. UNLOCK_COMMAND(&alloc_lock);
  901. #endif
  902. }
  903. #endif
  904. #ifdef DEBUG
  905. printf("Mapped : %p %3d\n\n",
  906. (void *)memory[position].addr, position);
  907. #endif
  908. return (void *)memory[position].addr;
  909. error:
  910. printf("BLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  911. return NULL;
  912. }
  913. void blas_memory_free(void *free_area){
  914. int position;
  915. #ifdef DEBUG
  916. printf("Unmapped Start : %p ...\n", free_area);
  917. #endif
  918. position = 0;
  919. #if defined(SMP) && !defined(USE_OPENMP)
  920. LOCK_COMMAND(&alloc_lock);
  921. #endif
  922. while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
  923. position++;
  924. if (memory[position].addr != free_area) goto error;
  925. #ifdef DEBUG
  926. printf(" Position : %d\n", position);
  927. #endif
  928. // arm: ensure all writes are finished before other thread takes this memory
  929. WMB;
  930. memory[position].used = 0;
  931. #if defined(SMP) && !defined(USE_OPENMP)
  932. UNLOCK_COMMAND(&alloc_lock);
  933. #endif
  934. #ifdef DEBUG
  935. printf("Unmap Succeeded.\n\n");
  936. #endif
  937. return;
  938. error:
  939. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  940. #ifdef DEBUG
  941. for (position = 0; position < NUM_BUFFERS; position++)
  942. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  943. #endif
  944. #if defined(SMP) && !defined(USE_OPENMP)
  945. UNLOCK_COMMAND(&alloc_lock);
  946. #endif
  947. return;
  948. }
  949. void *blas_memory_alloc_nolock(int unused) {
  950. void *map_address;
  951. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  952. return map_address;
  953. }
  954. void blas_memory_free_nolock(void * map_address) {
  955. free(map_address);
  956. }
  957. void blas_shutdown(void){
  958. int pos;
  959. #ifdef SMP
  960. BLASFUNC(blas_thread_shutdown)();
  961. #endif
  962. LOCK_COMMAND(&alloc_lock);
  963. for (pos = 0; pos < release_pos; pos ++) {
  964. release_info[pos].func(&release_info[pos]);
  965. }
  966. #ifdef SEEK_ADDRESS
  967. base_address = 0UL;
  968. #else
  969. base_address = BASE_ADDRESS;
  970. #endif
  971. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  972. memory[pos].addr = (void *)0;
  973. memory[pos].used = 0;
  974. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  975. memory[pos].pos = -1;
  976. #endif
  977. memory[pos].lock = 0;
  978. }
  979. UNLOCK_COMMAND(&alloc_lock);
  980. return;
  981. }
  982. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  983. #ifdef SMP
  984. #if defined(USE_PTHREAD_LOCK)
  985. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  986. #elif defined(USE_PTHREAD_SPINLOCK)
  987. static pthread_spinlock_t init_lock = 0;
  988. #else
  989. static BLASULONG init_lock = 0UL;
  990. #endif
  991. #endif
  992. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  993. void *sa, void *sb, BLASLONG pos) {
  994. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  995. size_t size;
  996. BLASULONG buffer;
  997. size = BUFFER_SIZE - PAGESIZE;
  998. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  999. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1000. if (hot_alloc != 2) {
  1001. #endif
  1002. #ifdef SMP
  1003. LOCK_COMMAND(&init_lock);
  1004. #endif
  1005. while (size > 0) {
  1006. *(int *)buffer = size;
  1007. buffer += PAGESIZE;
  1008. size -= PAGESIZE;
  1009. }
  1010. #ifdef SMP
  1011. UNLOCK_COMMAND(&init_lock);
  1012. #endif
  1013. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  1014. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1015. while (size > 0) {
  1016. *(int *)buffer = size;
  1017. buffer += 64;
  1018. size -= 64;
  1019. }
  1020. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1021. }
  1022. #endif
  1023. #endif
  1024. }
  1025. #ifdef SMP
  1026. static void _init_thread_memory(void *buffer) {
  1027. blas_queue_t queue[MAX_CPU_NUMBER];
  1028. int num_cpu;
  1029. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  1030. blas_queue_init(&queue[num_cpu]);
  1031. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  1032. queue[num_cpu].routine = &_touch_memory;
  1033. queue[num_cpu].args = NULL;
  1034. queue[num_cpu].next = &queue[num_cpu + 1];
  1035. }
  1036. queue[num_cpu - 1].next = NULL;
  1037. queue[0].sa = buffer;
  1038. exec_blas(num_cpu, queue);
  1039. }
  1040. #endif
  1041. static void gotoblas_memory_init(void) {
  1042. void *buffer;
  1043. hot_alloc = 1;
  1044. buffer = (void *)blas_memory_alloc(0);
  1045. #ifdef SMP
  1046. if (blas_cpu_number == 0) blas_get_cpu_number();
  1047. #ifdef SMP_SERVER
  1048. if (blas_server_avail == 0) blas_thread_init();
  1049. #endif
  1050. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  1051. #else
  1052. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  1053. #endif
  1054. blas_memory_free(buffer);
  1055. }
  1056. #endif
  1057. /* Initialization for all function; this function should be called before main */
  1058. static int gotoblas_initialized = 0;
  1059. extern void openblas_read_env();
  1060. void CONSTRUCTOR gotoblas_init(void) {
  1061. if (gotoblas_initialized) return;
  1062. #ifdef SMP
  1063. openblas_fork_handler();
  1064. #endif
  1065. openblas_read_env();
  1066. #ifdef PROFILE
  1067. moncontrol (0);
  1068. #endif
  1069. #ifdef DYNAMIC_ARCH
  1070. gotoblas_dynamic_init();
  1071. #endif
  1072. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1073. gotoblas_affinity_init();
  1074. #endif
  1075. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1076. gotoblas_memory_init();
  1077. #endif
  1078. //#if defined(OS_LINUX)
  1079. #if 0
  1080. struct rlimit curlimit;
  1081. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  1082. {
  1083. if ( curlimit.rlim_cur != curlimit.rlim_max )
  1084. {
  1085. curlimit.rlim_cur = curlimit.rlim_max;
  1086. setrlimit(RLIMIT_STACK, &curlimit);
  1087. }
  1088. }
  1089. #endif
  1090. #ifdef SMP
  1091. if (blas_cpu_number == 0) blas_get_cpu_number();
  1092. #ifdef SMP_SERVER
  1093. if (blas_server_avail == 0) blas_thread_init();
  1094. #endif
  1095. #endif
  1096. #ifdef FUNCTION_PROFILE
  1097. gotoblas_profile_init();
  1098. #endif
  1099. gotoblas_initialized = 1;
  1100. #ifdef PROFILE
  1101. moncontrol (1);
  1102. #endif
  1103. }
  1104. void DESTRUCTOR gotoblas_quit(void) {
  1105. if (gotoblas_initialized == 0) return;
  1106. blas_shutdown();
  1107. #ifdef PROFILE
  1108. moncontrol (0);
  1109. #endif
  1110. #ifdef FUNCTION_PROFILE
  1111. gotoblas_profile_quit();
  1112. #endif
  1113. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1114. gotoblas_affinity_quit();
  1115. #endif
  1116. #ifdef DYNAMIC_ARCH
  1117. gotoblas_dynamic_quit();
  1118. #endif
  1119. gotoblas_initialized = 0;
  1120. #ifdef PROFILE
  1121. moncontrol (1);
  1122. #endif
  1123. }
  1124. #if defined(_MSC_VER) && !defined(__clang__)
  1125. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  1126. {
  1127. switch (ul_reason_for_call)
  1128. {
  1129. case DLL_PROCESS_ATTACH:
  1130. gotoblas_init();
  1131. break;
  1132. case DLL_THREAD_ATTACH:
  1133. break;
  1134. case DLL_THREAD_DETACH:
  1135. break;
  1136. case DLL_PROCESS_DETACH:
  1137. gotoblas_quit();
  1138. break;
  1139. default:
  1140. break;
  1141. }
  1142. return TRUE;
  1143. }
  1144. /*
  1145. This is to allow static linking.
  1146. Code adapted from Google performance tools:
  1147. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  1148. Reference:
  1149. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  1150. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  1151. */
  1152. static int on_process_term(void)
  1153. {
  1154. gotoblas_quit();
  1155. return 0;
  1156. }
  1157. #ifdef _WIN64
  1158. #pragma comment(linker, "/INCLUDE:_tls_used")
  1159. #else
  1160. #pragma comment(linker, "/INCLUDE:__tls_used")
  1161. #endif
  1162. #ifdef _WIN64
  1163. #pragma const_seg(".CRT$XLB")
  1164. #else
  1165. #pragma data_seg(".CRT$XLB")
  1166. #endif
  1167. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  1168. #ifdef _WIN64
  1169. #pragma const_seg()
  1170. #else
  1171. #pragma data_seg()
  1172. #endif
  1173. #ifdef _WIN64
  1174. #pragma const_seg(".CRT$XTU")
  1175. #else
  1176. #pragma data_seg(".CRT$XTU")
  1177. #endif
  1178. static int(*p_process_term)(void) = on_process_term;
  1179. #ifdef _WIN64
  1180. #pragma const_seg()
  1181. #else
  1182. #pragma data_seg()
  1183. #endif
  1184. #endif
  1185. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  1186. /* Don't call me; this is just work around for PGI / Sun bug */
  1187. void gotoblas_dummy_for_PGI(void) {
  1188. gotoblas_init();
  1189. gotoblas_quit();
  1190. #if 0
  1191. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  1192. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  1193. #else
  1194. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  1195. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  1196. #endif
  1197. }
  1198. #endif