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

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