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

Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
7 years ago
7 years ago
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
9 years ago
9 years ago
9 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  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. #if defined(USE_TLS) && defined(SMP)
  68. #define COMPILE_TLS
  69. #if USE_TLS != 1
  70. #undef COMPILE_TLS
  71. #endif
  72. #if defined(__GLIBC_PREREQ)
  73. #if !__GLIBC_PREREQ(2,20)
  74. #undef COMPILE_TLS
  75. #endif
  76. #endif
  77. #endif
  78. /* Memory buffer must fit two matrix subblocks of maximal size */
  79. #define XSTR(x) STR(x)
  80. #define STR(x) #x
  81. #if BUFFER_SIZE < (SGEMM_DEFAULT_P * SGEMM_DEFAULT_Q * 4 * 2) || \
  82. BUFFER_SIZE < (SGEMM_DEFAULT_P * SGEMM_DEFAULT_R * 4 * 2) || \
  83. BUFFER_SIZE < (SGEMM_DEFAULT_R * SGEMM_DEFAULT_Q * 4 * 2)
  84. #warning BUFFER_SIZE is too small for P, Q, and R of SGEMM - large calculations may crash !
  85. #endif
  86. #if BUFFER_SIZE < (DGEMM_DEFAULT_P * DGEMM_DEFAULT_Q * 8 * 2) || \
  87. BUFFER_SIZE < (DGEMM_DEFAULT_P * DGEMM_DEFAULT_R * 8 * 2) || \
  88. BUFFER_SIZE < (DGEMM_DEFAULT_R * DGEMM_DEFAULT_Q * 8 * 2)
  89. #warning BUFFER_SIZE is too small for P, Q, and R of DGEMM - large calculations may crash !
  90. #endif
  91. #if BUFFER_SIZE < (CGEMM_DEFAULT_P * CGEMM_DEFAULT_Q * 8 * 2) || \
  92. BUFFER_SIZE < (CGEMM_DEFAULT_P * CGEMM_DEFAULT_R * 8 * 2) || \
  93. BUFFER_SIZE < (CGEMM_DEFAULT_R * CGEMM_DEFAULT_Q * 8 * 2)
  94. #warning BUFFER_SIZE is too small for P, Q, and R of CGEMM - large calculations may crash !
  95. #endif
  96. #if BUFFER_SIZE < (ZGEMM_DEFAULT_P * ZGEMM_DEFAULT_Q * 16 * 2) || \
  97. BUFFER_SIZE < (ZGEMM_DEFAULT_P * ZGEMM_DEFAULT_R * 16 * 2) || \
  98. BUFFER_SIZE < (ZGEMM_DEFAULT_R * ZGEMM_DEFAULT_Q * 16 * 2)
  99. #warning BUFFER_SIZE is too small for P, Q, and R of ZGEMM - large calculations may crash !
  100. #endif
  101. #if defined(COMPILE_TLS)
  102. #include <errno.h>
  103. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  104. #define ALLOC_WINDOWS
  105. #ifndef MEM_LARGE_PAGES
  106. #define MEM_LARGE_PAGES 0x20000000
  107. #endif
  108. #else
  109. #define ALLOC_MMAP
  110. #define ALLOC_MALLOC
  111. #endif
  112. #include <stdlib.h>
  113. #include <stdio.h>
  114. #include <fcntl.h>
  115. #if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
  116. #include <sys/mman.h>
  117. #ifndef NO_SYSV_IPC
  118. #include <sys/shm.h>
  119. #endif
  120. #include <sys/ipc.h>
  121. #endif
  122. #include <sys/types.h>
  123. #ifdef OS_LINUX
  124. #include <sys/sysinfo.h>
  125. #include <sched.h>
  126. #include <errno.h>
  127. #include <linux/unistd.h>
  128. #include <sys/syscall.h>
  129. #include <sys/time.h>
  130. #include <sys/resource.h>
  131. #endif
  132. #ifdef OS_HAIKU
  133. #include <unistd.h>
  134. #endif
  135. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
  136. #include <sys/sysctl.h>
  137. #include <sys/resource.h>
  138. #endif
  139. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  140. #include <conio.h>
  141. #undef printf
  142. #define printf _cprintf
  143. #endif
  144. #ifdef OS_LINUX
  145. #ifndef MPOL_PREFERRED
  146. #define MPOL_PREFERRED 1
  147. #endif
  148. #endif
  149. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  150. #define NO_WARMUP
  151. #endif
  152. #ifndef SHM_HUGETLB
  153. #define SHM_HUGETLB 04000
  154. #endif
  155. #ifndef FIXED_PAGESIZE
  156. #define FIXED_PAGESIZE 4096
  157. #endif
  158. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  159. #if defined(_MSC_VER) && !defined(__clang__)
  160. #define CONSTRUCTOR __cdecl
  161. #define DESTRUCTOR __cdecl
  162. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  163. #define CONSTRUCTOR __attribute__ ((constructor))
  164. #define DESTRUCTOR __attribute__ ((destructor))
  165. #elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
  166. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  167. #define DESTRUCTOR __attribute__ ((destructor(101)))
  168. #else
  169. #define CONSTRUCTOR __attribute__ ((constructor))
  170. #define DESTRUCTOR __attribute__ ((destructor))
  171. #endif
  172. #ifdef DYNAMIC_ARCH
  173. gotoblas_t *gotoblas = NULL;
  174. #endif
  175. extern void openblas_warning(int verbose, const char * msg);
  176. #ifndef SMP
  177. #define blas_cpu_number 1
  178. #define blas_num_threads 1
  179. /* Dummy Function */
  180. int goto_get_num_procs (void) { return 1;};
  181. void goto_set_num_threads(int num_threads) {};
  182. #else
  183. #if defined(OS_LINUX) || defined(OS_SUNOS)
  184. #ifndef NO_AFFINITY
  185. int get_num_procs(void);
  186. #else
  187. int get_num_procs(void) {
  188. static int nums = 0;
  189. cpu_set_t cpuset,*cpusetp;
  190. size_t size;
  191. int ret;
  192. #if defined(__GLIBC_PREREQ)
  193. #if !__GLIBC_PREREQ(2, 7)
  194. int i;
  195. #if !__GLIBC_PREREQ(2, 6)
  196. int n;
  197. #endif
  198. #endif
  199. #endif
  200. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  201. #if !defined(OS_LINUX)
  202. return nums;
  203. #endif
  204. #if !defined(__GLIBC_PREREQ)
  205. return nums;
  206. #else
  207. #if !__GLIBC_PREREQ(2, 3)
  208. return nums;
  209. #endif
  210. #if !__GLIBC_PREREQ(2, 7)
  211. ret = sched_getaffinity(0,sizeof(cpuset), &cpuset);
  212. if (ret!=0) return nums;
  213. n=0;
  214. #if !__GLIBC_PREREQ(2, 6)
  215. for (i=0;i<nums;i++)
  216. if (CPU_ISSET(i,&cpuset)) n++;
  217. nums=n;
  218. #else
  219. nums = CPU_COUNT(sizeof(cpuset),&cpuset);
  220. #endif
  221. return nums;
  222. #else
  223. if (nums >= CPU_SETSIZE) {
  224. cpusetp = CPU_ALLOC(nums);
  225. if (cpusetp == NULL) {
  226. return nums;
  227. }
  228. size = CPU_ALLOC_SIZE(nums);
  229. ret = sched_getaffinity(0,size,cpusetp);
  230. if (ret!=0) {
  231. CPU_FREE(cpusetp);
  232. return nums;
  233. }
  234. ret = CPU_COUNT_S(size,cpusetp);
  235. if (ret > 0 && ret < nums) nums = ret;
  236. CPU_FREE(cpusetp);
  237. return nums;
  238. } else {
  239. ret = sched_getaffinity(0,sizeof(cpuset),&cpuset);
  240. if (ret!=0) {
  241. return nums;
  242. }
  243. ret = CPU_COUNT(&cpuset);
  244. if (ret > 0 && ret < nums) nums = ret;
  245. return nums;
  246. }
  247. #endif
  248. #endif
  249. }
  250. #endif
  251. #endif
  252. #ifdef OS_ANDROID
  253. int get_num_procs(void) {
  254. static int nums = 0;
  255. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  256. return nums;
  257. }
  258. #endif
  259. #ifdef OS_HAIKU
  260. int get_num_procs(void) {
  261. static int nums = 0;
  262. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  263. return nums;
  264. }
  265. #endif
  266. #ifdef OS_AIX
  267. int get_num_procs(void) {
  268. static int nums = 0;
  269. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  270. return nums;
  271. }
  272. #endif
  273. #ifdef OS_WINDOWS
  274. int get_num_procs(void) {
  275. static int nums = 0;
  276. if (nums == 0) {
  277. SYSTEM_INFO sysinfo;
  278. GetSystemInfo(&sysinfo);
  279. nums = sysinfo.dwNumberOfProcessors;
  280. }
  281. return nums;
  282. }
  283. #endif
  284. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY)
  285. int get_num_procs(void) {
  286. static int nums = 0;
  287. int m[2];
  288. size_t len;
  289. if (nums == 0) {
  290. m[0] = CTL_HW;
  291. m[1] = HW_NCPU;
  292. len = sizeof(int);
  293. sysctl(m, 2, &nums, &len, NULL, 0);
  294. }
  295. return nums;
  296. }
  297. #endif
  298. #if defined(OS_DARWIN)
  299. int get_num_procs(void) {
  300. static int nums = 0;
  301. size_t len;
  302. if (nums == 0){
  303. len = sizeof(int);
  304. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  305. }
  306. return nums;
  307. }
  308. /*
  309. void set_stack_limit(int limitMB){
  310. int result=0;
  311. struct rlimit rl;
  312. rlim_t StackSize;
  313. StackSize=limitMB*1024*1024;
  314. result=getrlimit(RLIMIT_STACK, &rl);
  315. if(result==0){
  316. if(rl.rlim_cur < StackSize){
  317. rl.rlim_cur=StackSize;
  318. result=setrlimit(RLIMIT_STACK, &rl);
  319. if(result !=0){
  320. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  321. }
  322. }
  323. }
  324. }
  325. */
  326. #endif
  327. /*
  328. OpenBLAS uses the numbers of CPU cores in multithreading.
  329. It can be set by openblas_set_num_threads(int num_threads);
  330. */
  331. int blas_cpu_number = 0;
  332. /*
  333. The numbers of threads in the thread pool.
  334. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  335. */
  336. int blas_num_threads = 0;
  337. int goto_get_num_procs (void) {
  338. return blas_cpu_number;
  339. }
  340. static void blas_memory_init();
  341. void openblas_fork_handler()
  342. {
  343. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  344. // built with "make USE_OPENMP=0".
  345. // Hanging can still happen when OpenBLAS is built against the libgomp
  346. // implementation of OpenMP. The problem is tracked at:
  347. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  348. // In the mean time build with USE_OPENMP=0 or link against another
  349. // implementation of OpenMP.
  350. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  351. int err;
  352. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, blas_memory_init);
  353. if(err != 0)
  354. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  355. #endif
  356. }
  357. extern int openblas_num_threads_env();
  358. extern int openblas_goto_num_threads_env();
  359. extern int openblas_omp_num_threads_env();
  360. int blas_get_cpu_number(void){
  361. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
  362. int max_num;
  363. #endif
  364. int blas_goto_num = 0;
  365. int blas_omp_num = 0;
  366. if (blas_num_threads) return blas_num_threads;
  367. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
  368. max_num = get_num_procs();
  369. #endif
  370. // blas_goto_num = 0;
  371. #ifndef USE_OPENMP_UNUSED
  372. blas_goto_num=openblas_num_threads_env();
  373. if (blas_goto_num < 0) blas_goto_num = 0;
  374. if (blas_goto_num == 0) {
  375. blas_goto_num=openblas_goto_num_threads_env();
  376. if (blas_goto_num < 0) blas_goto_num = 0;
  377. }
  378. #endif
  379. // blas_omp_num = 0;
  380. blas_omp_num=openblas_omp_num_threads_env();
  381. if (blas_omp_num < 0) blas_omp_num = 0;
  382. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  383. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  384. else blas_num_threads = MAX_CPU_NUMBER;
  385. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
  386. if (blas_num_threads > max_num) blas_num_threads = max_num;
  387. #endif
  388. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  389. #ifdef DEBUG
  390. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  391. #endif
  392. blas_cpu_number = blas_num_threads;
  393. return blas_num_threads;
  394. }
  395. #endif
  396. int openblas_get_num_procs(void) {
  397. #ifndef SMP
  398. return 1;
  399. #else
  400. return get_num_procs();
  401. #endif
  402. }
  403. int openblas_get_num_threads(void) {
  404. #ifndef SMP
  405. return 1;
  406. #else
  407. // init blas_cpu_number if needed
  408. blas_get_cpu_number();
  409. return blas_cpu_number;
  410. #endif
  411. }
  412. int hugetlb_allocated = 0;
  413. #if defined(OS_WINDOWS)
  414. #define LIKELY_ONE(x) (x)
  415. #else
  416. #define LIKELY_ONE(x) (__builtin_expect(x, 1))
  417. #endif
  418. /* Stores information about the allocation and how to release it */
  419. struct alloc_t {
  420. /* Whether this allocation is being used */
  421. int used;
  422. /* Any special attributes needed when releasing this allocation */
  423. int attr;
  424. /* Function that can properly release this memory */
  425. void (*release_func)(struct alloc_t *);
  426. /* Pad to 64-byte alignment */
  427. char pad[64 - 2 * sizeof(int) - sizeof(void(*))];
  428. };
  429. /* Convenience macros for storing release funcs */
  430. #define STORE_RELEASE_FUNC(address, func) \
  431. if (address != (void *)-1) { \
  432. struct alloc_t *alloc_info = (struct alloc_t *)address; \
  433. alloc_info->release_func = func; \
  434. }
  435. #define STORE_RELEASE_FUNC_WITH_ATTR(address, func, attr) \
  436. if (address != (void *)-1) { \
  437. struct alloc_t *alloc_info = (struct alloc_t *)address; \
  438. alloc_info->release_func = func; \
  439. alloc_info->attr = attr; \
  440. }
  441. /* The number of bytes that will be allocated for each buffer. When allocating
  442. memory, we store an alloc_t followed by the actual buffer memory. This means
  443. that each allocation always has its associated alloc_t, without the need
  444. for an auxiliary tracking structure. */
  445. static const int allocation_block_size = BUFFER_SIZE + sizeof(struct alloc_t);
  446. #if defined(SMP)
  447. # if defined(OS_WINDOWS)
  448. static DWORD local_storage_key = 0;
  449. DWORD lsk;
  450. # else
  451. static pthread_key_t local_storage_key = 0;
  452. pthread_key_t lsk;
  453. # endif /* defined(OS_WINDOWS) */
  454. #endif /* defined(SMP) */
  455. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  456. static int hot_alloc = 0;
  457. #endif
  458. /* Global lock for memory allocation */
  459. #if defined(USE_PTHREAD_LOCK)
  460. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  461. #elif defined(USE_PTHREAD_SPINLOCK)
  462. static pthread_spinlock_t alloc_lock = 0;
  463. #else
  464. static BLASULONG alloc_lock = 0UL;
  465. #endif
  466. #if defined(USE_PTHREAD_LOCK)
  467. static pthread_mutex_t key_lock = PTHREAD_MUTEX_INITIALIZER;
  468. #elif defined(USE_PTHREAD_SPINLOCK)
  469. static pthread_spinlock_t key_lock = 0;
  470. #else
  471. static BLASULONG key_lock = 0UL;
  472. #endif
  473. /* Returns a pointer to the start of the per-thread memory allocation data */
  474. static __inline struct alloc_t ** get_memory_table() {
  475. #if defined(SMP)
  476. LOCK_COMMAND(&key_lock);
  477. lsk=local_storage_key;
  478. UNLOCK_COMMAND(&key_lock);
  479. if (!lsk) {
  480. blas_memory_init();
  481. }
  482. # if defined(OS_WINDOWS)
  483. struct alloc_t ** local_memory_table = (struct alloc_t **)TlsGetValue(local_storage_key);
  484. # else
  485. struct alloc_t ** local_memory_table = (struct alloc_t **)pthread_getspecific(local_storage_key);
  486. # endif /* defined(OS_WINDOWS) */
  487. #else
  488. static struct alloc_t ** local_memory_table = NULL;
  489. #endif /* defined(SMP) */
  490. #if defined (SMP)
  491. LOCK_COMMAND(&key_lock);
  492. lsk=local_storage_key;
  493. UNLOCK_COMMAND(&key_lock);
  494. if (lsk && !local_memory_table) {
  495. #else
  496. if (!local_memory_table) {
  497. #endif /* defined(SMP) */
  498. local_memory_table = (struct alloc_t **)malloc(sizeof(struct alloc_t *) * NUM_BUFFERS);
  499. memset(local_memory_table, 0, sizeof(struct alloc_t *) * NUM_BUFFERS);
  500. #if defined(SMP)
  501. # if defined(OS_WINDOWS)
  502. LOCK_COMMAND(&key_lock);
  503. TlsSetValue(local_storage_key, (void*)local_memory_table);
  504. UNLOCK_COMMAND(&key_lock);
  505. # else
  506. LOCK_COMMAND(&key_lock);
  507. pthread_setspecific(local_storage_key, (void*)local_memory_table);
  508. UNLOCK_COMMAND(&key_lock);
  509. # endif /* defined(OS_WINDOWS) */
  510. #endif /* defined(SMP) */
  511. }
  512. return local_memory_table;
  513. }
  514. #ifdef ALLOC_MMAP
  515. static void alloc_mmap_free(struct alloc_t *alloc_info){
  516. if (munmap(alloc_info, allocation_block_size)) {
  517. printf("OpenBLAS : munmap failed\n");
  518. }
  519. }
  520. #ifdef NO_WARMUP
  521. static void *alloc_mmap(void *address){
  522. void *map_address;
  523. if (address){
  524. map_address = mmap(address,
  525. allocation_block_size,
  526. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  527. } else {
  528. map_address = mmap(address,
  529. allocation_block_size,
  530. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  531. }
  532. STORE_RELEASE_FUNC(map_address, alloc_mmap_free);
  533. #ifdef OS_LINUX
  534. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  535. #endif
  536. return map_address;
  537. }
  538. #else
  539. #define BENCH_ITERATION 4
  540. #define SCALING 2
  541. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  542. BLASULONG original, *p;
  543. BLASULONG start, stop, min;
  544. int iter, i, count;
  545. min = (BLASULONG)-1;
  546. original = *(BLASULONG *)(address + size - PAGESIZE);
  547. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  548. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  549. p = (BLASULONG *)address;
  550. count = size / PAGESIZE;
  551. start = rpcc();
  552. for (i = 0; i < count; i ++) {
  553. p = (BLASULONG *)(*p);
  554. }
  555. stop = rpcc();
  556. if (min > stop - start) min = stop - start;
  557. }
  558. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  559. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  560. return min;
  561. }
  562. static void *alloc_mmap(void *address){
  563. void *map_address, *best_address;
  564. BLASULONG best, start, current, original;
  565. BLASULONG allocsize;
  566. if (address){
  567. /* Just give up use advanced operation */
  568. map_address = mmap(address, allocation_block_size, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  569. #ifdef OS_LINUX
  570. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  571. #endif
  572. } else {
  573. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  574. if (hot_alloc == 0) {
  575. map_address = mmap(NULL, allocation_block_size, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  576. #ifdef OS_LINUX
  577. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  578. #endif
  579. } else {
  580. #endif
  581. map_address = mmap(NULL, allocation_block_size * SCALING,
  582. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  583. if (map_address != (void *)-1) {
  584. #ifdef OS_LINUX
  585. #ifdef DEBUG
  586. int ret=0;
  587. ret=my_mbind(map_address, allocation_block_size * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  588. if(ret==-1){
  589. int errsv=errno;
  590. perror("OpenBLAS alloc_mmap:");
  591. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  592. }
  593. #else
  594. my_mbind(map_address, allocation_block_size * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  595. #endif
  596. #endif
  597. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  598. start = (BLASULONG)map_address;
  599. current = (SCALING - 1) * allocation_block_size;
  600. original = current;
  601. while(current > 0 && current <= original) {
  602. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  603. start += PAGESIZE;
  604. current -= PAGESIZE;
  605. }
  606. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  607. start = (BLASULONG)map_address;
  608. best = (BLASULONG)-1;
  609. best_address = map_address;
  610. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * allocation_block_size)) {
  611. current = run_bench(start, allocsize);
  612. if (best > current) {
  613. best = current;
  614. best_address = (void *)start;
  615. }
  616. start += PAGESIZE;
  617. }
  618. if ((BLASULONG)best_address > (BLASULONG)map_address)
  619. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  620. munmap((void *)((BLASULONG)best_address + allocation_block_size), (SCALING - 1) * allocation_block_size + (BLASULONG)map_address - (BLASULONG)best_address);
  621. map_address = best_address;
  622. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  623. hot_alloc = 2;
  624. #endif
  625. }
  626. }
  627. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  628. }
  629. #endif
  630. STORE_RELEASE_FUNC(map_address, alloc_mmap_free);
  631. return map_address;
  632. }
  633. #endif
  634. #endif
  635. #ifdef ALLOC_MALLOC
  636. static void alloc_malloc_free(struct alloc_t *alloc_info){
  637. free(alloc_info);
  638. }
  639. static void *alloc_malloc(void *address){
  640. void *map_address;
  641. map_address = (void *)malloc(allocation_block_size + FIXED_PAGESIZE);
  642. if (map_address == (void *)NULL) map_address = (void *)-1;
  643. STORE_RELEASE_FUNC(map_address, alloc_malloc_free);
  644. return map_address;
  645. }
  646. #endif
  647. #ifdef ALLOC_QALLOC
  648. void *qalloc(int flags, size_t bytes);
  649. void *qfree (void *address);
  650. #define QNONCACHE 0x1
  651. #define QCOMMS 0x2
  652. #define QFAST 0x4
  653. static void alloc_qalloc_free(struct alloc_t *alloc_info){
  654. qfree(alloc_info);
  655. }
  656. static void *alloc_qalloc(void *address){
  657. void *map_address;
  658. map_address = (void *)qalloc(QCOMMS | QFAST, allocation_block_size + FIXED_PAGESIZE);
  659. if (map_address == (void *)NULL) map_address = (void *)-1;
  660. STORE_RELEASE_FUNC(map_address, alloc_qalloc_free);
  661. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  662. }
  663. #endif
  664. #ifdef ALLOC_WINDOWS
  665. static void alloc_windows_free(struct alloc_t *alloc_info){
  666. VirtualFree(alloc_info, 0, MEM_RELEASE);
  667. }
  668. static void *alloc_windows(void *address){
  669. void *map_address;
  670. map_address = VirtualAlloc(address,
  671. allocation_block_size,
  672. MEM_RESERVE | MEM_COMMIT,
  673. PAGE_READWRITE);
  674. if (map_address == (void *)NULL) map_address = (void *)-1;
  675. STORE_RELEASE_FUNC(map_address, alloc_windows_free);
  676. return map_address;
  677. }
  678. #endif
  679. #ifdef ALLOC_DEVICEDRIVER
  680. #ifndef DEVICEDRIVER_NAME
  681. #define DEVICEDRIVER_NAME "/dev/mapper"
  682. #endif
  683. static void alloc_devicedirver_free(struct alloc_t *alloc_info){
  684. int attr = alloc_info -> attr;
  685. if (munmap(address, allocation_block_size)) {
  686. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  687. }
  688. if (close(attr)) {
  689. printf("OpenBLAS : Bugphysarea close failed.\n");
  690. }
  691. }
  692. static void *alloc_devicedirver(void *address){
  693. int fd;
  694. void *map_address;
  695. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  696. return (void *)-1;
  697. }
  698. map_address = mmap(address, allocation_block_size,
  699. PROT_READ | PROT_WRITE,
  700. MAP_FILE | MAP_SHARED,
  701. fd, 0);
  702. STORE_RELEASE_FUNC_WITH_ATTR(map_address, alloc_devicedirver_free, fd);
  703. return map_address;
  704. }
  705. #endif
  706. #ifdef ALLOC_SHM
  707. static void alloc_shm_free(struct alloc_t *alloc_info){
  708. if (shmdt(alloc_info)) {
  709. printf("OpenBLAS : Shared memory unmap failed.\n");
  710. }
  711. }
  712. static void *alloc_shm(void *address){
  713. void *map_address;
  714. int shmid;
  715. shmid = shmget(IPC_PRIVATE, allocation_block_size,IPC_CREAT | 0600);
  716. map_address = (void *)shmat(shmid, address, 0);
  717. if (map_address != (void *)-1){
  718. #ifdef OS_LINUX
  719. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  720. #endif
  721. shmctl(shmid, IPC_RMID, 0);
  722. struct alloc_t *alloc_info = (struct alloc_t *)map_address;
  723. alloc_info->release_func = alloc_shm_free;
  724. alloc_info->attr = shmid;
  725. }
  726. return map_address;
  727. }
  728. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  729. static void alloc_hugetlb_free(struct alloc_t *alloc_info){
  730. #if defined(OS_LINUX) || defined(OS_AIX)
  731. if (shmdt(alloc_info)) {
  732. printf("OpenBLAS : Hugepage unmap failed.\n");
  733. }
  734. #endif
  735. #ifdef __sun__
  736. munmap(alloc_info, allocation_block_size);
  737. #endif
  738. #ifdef OS_WINDOWS
  739. VirtualFree(alloc_info, 0, MEM_LARGE_PAGES | MEM_RELEASE);
  740. #endif
  741. }
  742. static void *alloc_hugetlb(void *address){
  743. void *map_address = (void *)-1;
  744. #if defined(OS_LINUX) || defined(OS_AIX)
  745. int shmid;
  746. shmid = shmget(IPC_PRIVATE, allocation_block_size,
  747. #ifdef OS_LINUX
  748. SHM_HUGETLB |
  749. #endif
  750. #ifdef OS_AIX
  751. SHM_LGPAGE | SHM_PIN |
  752. #endif
  753. IPC_CREAT | SHM_R | SHM_W);
  754. if (shmid != -1) {
  755. map_address = (void *)shmat(shmid, address, SHM_RND);
  756. #ifdef OS_LINUX
  757. my_mbind(map_address, allocation_block_size, MPOL_PREFERRED, NULL, 0, 0);
  758. #endif
  759. if (map_address != (void *)-1){
  760. shmctl(shmid, IPC_RMID, 0);
  761. }
  762. }
  763. #endif
  764. #ifdef __sun__
  765. struct memcntl_mha mha;
  766. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  767. mha.mha_flags = 0;
  768. mha.mha_pagesize = HUGE_PAGESIZE;
  769. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  770. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, allocation_block_size);
  771. #endif
  772. #ifdef OS_WINDOWS
  773. HANDLE hToken;
  774. TOKEN_PRIVILEGES tp;
  775. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  776. tp.PrivilegeCount = 1;
  777. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  778. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  779. CloseHandle(hToken);
  780. return (void*)-1;
  781. }
  782. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  783. CloseHandle(hToken);
  784. return (void*)-1;
  785. }
  786. map_address = (void *)VirtualAlloc(address,
  787. allocation_block_size,
  788. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  789. PAGE_READWRITE);
  790. tp.Privileges[0].Attributes = 0;
  791. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  792. if (map_address == (void *)NULL) map_address = (void *)-1;
  793. #endif
  794. STORE_RELEASE_FUNC(map_address, alloc_hugetlb_free);
  795. return map_address;
  796. }
  797. #endif
  798. #endif
  799. #ifdef ALLOC_HUGETLBFILE
  800. static int hugetlb_pid = 0;
  801. static void alloc_hugetlbfile_free(struct alloc_t *alloc_info){
  802. int attr = alloc_info -> attr;
  803. if (munmap(alloc_info, allocation_block_size)) {
  804. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  805. }
  806. if (close(attr)) {
  807. printf("OpenBLAS : HugeTLBfs close failed.\n");
  808. }
  809. }
  810. static void *alloc_hugetlbfile(void *address){
  811. void *map_address = (void *)-1;
  812. int fd;
  813. char filename[64];
  814. if (!hugetlb_pid) hugetlb_pid = getpid();
  815. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  816. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  817. return (void *)-1;
  818. }
  819. unlink(filename);
  820. map_address = mmap(address, allocation_block_size,
  821. PROT_READ | PROT_WRITE,
  822. MAP_SHARED,
  823. fd, 0);
  824. STORE_RELEASE_FUNC_WITH_ATTR(map_address, alloc_hugetlbfile_free, fd);
  825. return map_address;
  826. }
  827. #endif
  828. #ifdef SEEK_ADDRESS
  829. static BLASULONG base_address = 0UL;
  830. #else
  831. static BLASULONG base_address = BASE_ADDRESS;
  832. #endif
  833. #if __STDC_VERSION__ >= 201112L
  834. static _Atomic int memory_initialized = 0;
  835. #else
  836. static volatile int memory_initialized = 0;
  837. #endif
  838. /* Memory allocation routine */
  839. /* procpos ... indicates where it comes from */
  840. /* 0 : Level 3 functions */
  841. /* 1 : Level 2 functions */
  842. /* 2 : Thread */
  843. static void blas_memory_cleanup(void* ptr){
  844. if (ptr) {
  845. struct alloc_t ** table = (struct alloc_t **)ptr;
  846. int pos;
  847. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  848. struct alloc_t *alloc_info = table[pos];
  849. if (alloc_info) {
  850. alloc_info->release_func(alloc_info);
  851. table[pos] = (void *)0;
  852. }
  853. }
  854. free(table);
  855. }
  856. }
  857. static void blas_memory_init(){
  858. #if defined(SMP)
  859. # if defined(OS_WINDOWS)
  860. local_storage_key = TlsAlloc();
  861. # else
  862. pthread_key_create(&local_storage_key, blas_memory_cleanup);
  863. # endif /* defined(OS_WINDOWS) */
  864. #endif /* defined(SMP) */
  865. }
  866. void *blas_memory_alloc(int procpos){
  867. int position;
  868. void *map_address;
  869. void *(*memoryalloc[])(void *address) = {
  870. #ifdef ALLOC_DEVICEDRIVER
  871. alloc_devicedirver,
  872. #endif
  873. /* Hugetlb implicitly assumes ALLOC_SHM */
  874. #ifdef ALLOC_SHM
  875. alloc_shm,
  876. #endif
  877. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  878. alloc_hugetlb,
  879. #endif
  880. #ifdef ALLOC_MMAP
  881. alloc_mmap,
  882. #endif
  883. #ifdef ALLOC_QALLOC
  884. alloc_qalloc,
  885. #endif
  886. #ifdef ALLOC_WINDOWS
  887. alloc_windows,
  888. #endif
  889. #ifdef ALLOC_MALLOC
  890. alloc_malloc,
  891. #endif
  892. NULL,
  893. };
  894. void *(**func)(void *address);
  895. struct alloc_t * alloc_info;
  896. struct alloc_t ** alloc_table;
  897. #if defined(SMP) && !defined(USE_OPENMP)
  898. int mi;
  899. LOCK_COMMAND(&alloc_lock);
  900. mi=memory_initialized;
  901. UNLOCK_COMMAND(&alloc_lock);
  902. if (!LIKELY_ONE(mi)) {
  903. #else
  904. if (!LIKELY_ONE(memory_initialized)) {
  905. #endif
  906. #if defined(SMP) && !defined(USE_OPENMP)
  907. /* Only allow a single thread to initialize memory system */
  908. LOCK_COMMAND(&alloc_lock);
  909. if (!memory_initialized) {
  910. #endif
  911. blas_memory_init();
  912. #ifdef DYNAMIC_ARCH
  913. gotoblas_dynamic_init();
  914. #endif
  915. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  916. gotoblas_affinity_init();
  917. #endif
  918. #ifdef SMP
  919. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  920. #endif
  921. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  922. #ifndef DYNAMIC_ARCH
  923. blas_set_parameter();
  924. #endif
  925. #endif
  926. memory_initialized = 1;
  927. #if defined(SMP) && !defined(USE_OPENMP)
  928. }
  929. UNLOCK_COMMAND(&alloc_lock);
  930. #endif
  931. }
  932. #ifdef DEBUG
  933. printf("Alloc Start ...\n");
  934. #endif
  935. position = 0;
  936. alloc_table = get_memory_table();
  937. do {
  938. if (!alloc_table[position] || !alloc_table[position]->used) goto allocation;
  939. position ++;
  940. } while (position < NUM_BUFFERS);
  941. goto error;
  942. allocation :
  943. #ifdef DEBUG
  944. printf(" Position -> %d\n", position);
  945. #endif
  946. alloc_info = alloc_table[position];
  947. if (!alloc_info) {
  948. do {
  949. #ifdef DEBUG
  950. printf("Allocation Start : %lx\n", base_address);
  951. #endif
  952. map_address = (void *)-1;
  953. func = &memoryalloc[0];
  954. while ((func != NULL) && (map_address == (void *) -1)) {
  955. map_address = (*func)((void *)base_address);
  956. #ifdef ALLOC_DEVICEDRIVER
  957. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  958. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation failed.\n");
  959. }
  960. #endif
  961. #ifdef ALLOC_HUGETLBFILE
  962. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  963. #ifndef OS_WINDOWS
  964. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation failed.\n");
  965. #endif
  966. }
  967. #endif
  968. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  969. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  970. #endif
  971. func ++;
  972. }
  973. #ifdef DEBUG
  974. printf(" Success -> %08lx\n", map_address);
  975. #endif
  976. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  977. if (base_address) base_address += allocation_block_size + FIXED_PAGESIZE;
  978. } while ((BLASLONG)map_address == -1);
  979. alloc_table[position] = alloc_info = map_address;
  980. #ifdef DEBUG
  981. printf(" Mapping Succeeded. %p(%d)\n", (void *)alloc_info, position);
  982. #endif
  983. }
  984. #ifdef DEBUG
  985. printf("Mapped : %p %3d\n\n", (void *)alloc_info, position);
  986. #endif
  987. alloc_info->used = 1;
  988. return (void *)(((char *)alloc_info) + sizeof(struct alloc_t));
  989. error:
  990. printf("OpenBLAS : Program will terminate because you tried to allocate too many memory regions.\n");
  991. return NULL;
  992. }
  993. void blas_memory_free(void *buffer){
  994. #ifdef DEBUG
  995. int position;
  996. struct alloc_t ** alloc_table;
  997. #endif
  998. /* Since we passed an offset pointer to the caller, get back to the actual allocation */
  999. struct alloc_t *alloc_info = (void *)(((char *)buffer) - sizeof(struct alloc_t));
  1000. #ifdef DEBUG
  1001. printf("Unmapped Start : %p ...\n", alloc_info);
  1002. #endif
  1003. alloc_info->used = 0;
  1004. #ifdef DEBUG
  1005. printf("Unmap Succeeded.\n\n");
  1006. #endif
  1007. return;
  1008. #ifdef DEBUG
  1009. alloc_table = get_memory_table();
  1010. for (position = 0; position < NUM_BUFFERS; position++){
  1011. if (alloc_table[position]) {
  1012. printf("%4ld %p : %d\n", position, alloc_table[position], alloc_table[position]->used);
  1013. }
  1014. }
  1015. #endif
  1016. return;
  1017. }
  1018. void *blas_memory_alloc_nolock(int unused) {
  1019. void *map_address;
  1020. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1021. return map_address;
  1022. }
  1023. void blas_memory_free_nolock(void * map_address) {
  1024. free(map_address);
  1025. }
  1026. #ifdef SMP
  1027. void blas_thread_memory_cleanup(void) {
  1028. blas_memory_cleanup((void*)get_memory_table());
  1029. }
  1030. #endif
  1031. void blas_shutdown(void){
  1032. #ifdef SMP
  1033. BLASFUNC(blas_thread_shutdown)();
  1034. #endif
  1035. #ifdef SMP
  1036. /* Only cleanupIf we were built for threading and TLS was initialized */
  1037. if (local_storage_key)
  1038. #endif
  1039. blas_thread_memory_cleanup();
  1040. #ifdef SEEK_ADDRESS
  1041. base_address = 0UL;
  1042. #else
  1043. base_address = BASE_ADDRESS;
  1044. #endif
  1045. return;
  1046. }
  1047. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1048. #ifdef SMP
  1049. #if defined(USE_PTHREAD_LOCK)
  1050. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  1051. #elif defined(USE_PTHREAD_SPINLOCK)
  1052. static pthread_spinlock_t init_lock = 0;
  1053. #else
  1054. static BLASULONG init_lock = 0UL;
  1055. #endif
  1056. #endif
  1057. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  1058. void *sa, void *sb, BLASLONG pos) {
  1059. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  1060. size_t size;
  1061. BLASULONG buffer;
  1062. size = allocation_block_size - PAGESIZE;
  1063. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1064. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1065. if (hot_alloc != 2) {
  1066. #endif
  1067. #ifdef SMP
  1068. LOCK_COMMAND(&init_lock);
  1069. #endif
  1070. while (size > 0) {
  1071. *(int *)buffer = size;
  1072. buffer += PAGESIZE;
  1073. size -= PAGESIZE;
  1074. }
  1075. #ifdef SMP
  1076. UNLOCK_COMMAND(&init_lock);
  1077. #endif
  1078. size = MIN((allocation_block_size - PAGESIZE), L2_SIZE);
  1079. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  1080. while (size > 0) {
  1081. *(int *)buffer = size;
  1082. buffer += 64;
  1083. size -= 64;
  1084. }
  1085. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1086. }
  1087. #endif
  1088. #endif
  1089. }
  1090. #ifdef SMP
  1091. static void _init_thread_memory(void *buffer) {
  1092. blas_queue_t queue[MAX_CPU_NUMBER];
  1093. int num_cpu;
  1094. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  1095. blas_queue_init(&queue[num_cpu]);
  1096. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  1097. queue[num_cpu].routine = &_touch_memory;
  1098. queue[num_cpu].args = NULL;
  1099. queue[num_cpu].next = &queue[num_cpu + 1];
  1100. }
  1101. queue[num_cpu - 1].next = NULL;
  1102. queue[0].sa = buffer;
  1103. exec_blas(num_cpu, queue);
  1104. }
  1105. #endif
  1106. static void gotoblas_memory_init(void) {
  1107. void *buffer;
  1108. hot_alloc = 1;
  1109. buffer = (void *)blas_memory_alloc(0);
  1110. #ifdef SMP
  1111. if (blas_cpu_number == 0) blas_get_cpu_number();
  1112. #ifdef SMP_SERVER
  1113. if (blas_server_avail == 0) blas_thread_init();
  1114. #endif
  1115. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  1116. #else
  1117. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  1118. #endif
  1119. blas_memory_free(buffer);
  1120. }
  1121. #endif
  1122. /* Initialization for all function; this function should be called before main */
  1123. static int gotoblas_initialized = 0;
  1124. extern void openblas_read_env();
  1125. void CONSTRUCTOR gotoblas_init(void) {
  1126. if (gotoblas_initialized) return;
  1127. #ifdef SMP
  1128. openblas_fork_handler();
  1129. #endif
  1130. openblas_read_env();
  1131. #ifdef PROFILE
  1132. moncontrol (0);
  1133. #endif
  1134. #ifdef DYNAMIC_ARCH
  1135. gotoblas_dynamic_init();
  1136. #endif
  1137. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1138. gotoblas_affinity_init();
  1139. #endif
  1140. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1141. gotoblas_memory_init();
  1142. #endif
  1143. //#if defined(OS_LINUX)
  1144. #if 0
  1145. struct rlimit curlimit;
  1146. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  1147. {
  1148. if ( curlimit.rlim_cur != curlimit.rlim_max )
  1149. {
  1150. curlimit.rlim_cur = curlimit.rlim_max;
  1151. setrlimit(RLIMIT_STACK, &curlimit);
  1152. }
  1153. }
  1154. #endif
  1155. #ifdef SMP
  1156. if (blas_cpu_number == 0) blas_get_cpu_number();
  1157. #ifdef SMP_SERVER
  1158. if (blas_server_avail == 0) blas_thread_init();
  1159. #endif
  1160. #endif
  1161. #ifdef FUNCTION_PROFILE
  1162. gotoblas_profile_init();
  1163. #endif
  1164. gotoblas_initialized = 1;
  1165. #ifdef PROFILE
  1166. moncontrol (1);
  1167. #endif
  1168. }
  1169. void DESTRUCTOR gotoblas_quit(void) {
  1170. if (gotoblas_initialized == 0) return;
  1171. blas_shutdown();
  1172. #if defined(SMP)
  1173. #if defined(OS_WINDOWS)
  1174. TlsFree(local_storage_key);
  1175. #else
  1176. pthread_key_delete(local_storage_key);
  1177. #endif
  1178. #endif
  1179. #ifdef PROFILE
  1180. moncontrol (0);
  1181. #endif
  1182. #ifdef FUNCTION_PROFILE
  1183. gotoblas_profile_quit();
  1184. #endif
  1185. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  1186. gotoblas_affinity_quit();
  1187. #endif
  1188. #ifdef DYNAMIC_ARCH
  1189. gotoblas_dynamic_quit();
  1190. #endif
  1191. gotoblas_initialized = 0;
  1192. #ifdef PROFILE
  1193. moncontrol (1);
  1194. #endif
  1195. }
  1196. #if defined(_MSC_VER) && !defined(__clang__)
  1197. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  1198. {
  1199. switch (ul_reason_for_call)
  1200. {
  1201. case DLL_PROCESS_ATTACH:
  1202. gotoblas_init();
  1203. break;
  1204. case DLL_THREAD_ATTACH:
  1205. break;
  1206. case DLL_THREAD_DETACH:
  1207. #if defined(SMP)
  1208. blas_thread_memory_cleanup();
  1209. #endif
  1210. break;
  1211. case DLL_PROCESS_DETACH:
  1212. gotoblas_quit();
  1213. break;
  1214. default:
  1215. break;
  1216. }
  1217. return TRUE;
  1218. }
  1219. /*
  1220. This is to allow static linking.
  1221. Code adapted from Google performance tools:
  1222. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  1223. Reference:
  1224. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  1225. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  1226. */
  1227. static int on_process_term(void)
  1228. {
  1229. gotoblas_quit();
  1230. return 0;
  1231. }
  1232. #ifdef _WIN64
  1233. #pragma comment(linker, "/INCLUDE:_tls_used")
  1234. #else
  1235. #pragma comment(linker, "/INCLUDE:__tls_used")
  1236. #endif
  1237. #ifdef _WIN64
  1238. #pragma const_seg(".CRT$XLB")
  1239. #else
  1240. #pragma data_seg(".CRT$XLB")
  1241. #endif
  1242. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  1243. #ifdef _WIN64
  1244. #pragma const_seg()
  1245. #else
  1246. #pragma data_seg()
  1247. #endif
  1248. #ifdef _WIN64
  1249. #pragma const_seg(".CRT$XTU")
  1250. #else
  1251. #pragma data_seg(".CRT$XTU")
  1252. #endif
  1253. static int(*p_process_term)(void) = on_process_term;
  1254. #ifdef _WIN64
  1255. #pragma const_seg()
  1256. #else
  1257. #pragma data_seg()
  1258. #endif
  1259. #endif
  1260. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  1261. /* Don't call me; this is just work around for PGI / Sun bug */
  1262. void gotoblas_dummy_for_PGI(void) {
  1263. gotoblas_init();
  1264. gotoblas_quit();
  1265. #if __PGIC__ < 19
  1266. #if 0
  1267. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  1268. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  1269. #else
  1270. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  1271. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  1272. #endif
  1273. #endif
  1274. }
  1275. #endif
  1276. #else
  1277. /* USE_TLS / COMPILE_TLS not set */
  1278. #include <errno.h>
  1279. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  1280. #define ALLOC_WINDOWS
  1281. #ifndef MEM_LARGE_PAGES
  1282. #define MEM_LARGE_PAGES 0x20000000
  1283. #endif
  1284. #else
  1285. #define ALLOC_MMAP
  1286. #define ALLOC_MALLOC
  1287. #endif
  1288. #include <stdlib.h>
  1289. #include <stdio.h>
  1290. #include <fcntl.h>
  1291. #if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
  1292. #include <sys/mman.h>
  1293. #ifndef NO_SYSV_IPC
  1294. #include <sys/shm.h>
  1295. #endif
  1296. #include <sys/ipc.h>
  1297. #endif
  1298. #include <sys/types.h>
  1299. #ifdef OS_LINUX
  1300. #include <sys/sysinfo.h>
  1301. #include <sched.h>
  1302. #include <errno.h>
  1303. #include <linux/unistd.h>
  1304. #include <sys/syscall.h>
  1305. #include <sys/time.h>
  1306. #include <sys/resource.h>
  1307. #endif
  1308. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
  1309. #include <sys/sysctl.h>
  1310. #include <sys/resource.h>
  1311. #endif
  1312. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  1313. #include <conio.h>
  1314. #undef printf
  1315. #define printf _cprintf
  1316. #endif
  1317. #ifdef OS_LINUX
  1318. #ifndef MPOL_PREFERRED
  1319. #define MPOL_PREFERRED 1
  1320. #endif
  1321. #endif
  1322. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  1323. #define NO_WARMUP
  1324. #endif
  1325. #ifndef SHM_HUGETLB
  1326. #define SHM_HUGETLB 04000
  1327. #endif
  1328. #ifndef FIXED_PAGESIZE
  1329. #define FIXED_PAGESIZE 4096
  1330. #endif
  1331. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  1332. #if defined(_MSC_VER) && !defined(__clang__)
  1333. #define CONSTRUCTOR __cdecl
  1334. #define DESTRUCTOR __cdecl
  1335. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  1336. #define CONSTRUCTOR __attribute__ ((constructor))
  1337. #define DESTRUCTOR __attribute__ ((destructor))
  1338. #elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
  1339. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  1340. #define DESTRUCTOR __attribute__ ((destructor(101)))
  1341. #else
  1342. #define CONSTRUCTOR __attribute__ ((constructor))
  1343. #define DESTRUCTOR __attribute__ ((destructor))
  1344. #endif
  1345. #ifdef DYNAMIC_ARCH
  1346. gotoblas_t *gotoblas = NULL;
  1347. #endif
  1348. extern void openblas_warning(int verbose, const char * msg);
  1349. #ifndef SMP
  1350. #define blas_cpu_number 1
  1351. #define blas_num_threads 1
  1352. /* Dummy Function */
  1353. int goto_get_num_procs (void) { return 1;};
  1354. void goto_set_num_threads(int num_threads) {};
  1355. #else
  1356. #if defined(OS_LINUX) || defined(OS_SUNOS)
  1357. #ifndef NO_AFFINITY
  1358. int get_num_procs(void);
  1359. #else
  1360. int get_num_procs(void) {
  1361. static int nums = 0;
  1362. cpu_set_t cpuset,*cpusetp;
  1363. size_t size;
  1364. int ret;
  1365. #if defined(__GLIBC_PREREQ)
  1366. #if !__GLIBC_PREREQ(2, 7)
  1367. int i;
  1368. #if !__GLIBC_PREREQ(2, 6)
  1369. int n;
  1370. #endif
  1371. #endif
  1372. #endif
  1373. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1374. #if !defined(OS_LINUX)
  1375. return nums;
  1376. #endif
  1377. #if !defined(__GLIBC_PREREQ)
  1378. return nums;
  1379. #else
  1380. #if !__GLIBC_PREREQ(2, 3)
  1381. return nums;
  1382. #endif
  1383. #if !__GLIBC_PREREQ(2, 7)
  1384. ret = sched_getaffinity(0,sizeof(cpuset), &cpuset);
  1385. if (ret!=0) return nums;
  1386. n=0;
  1387. #if !__GLIBC_PREREQ(2, 6)
  1388. for (i=0;i<nums;i++)
  1389. if (CPU_ISSET(i,&cpuset)) n++;
  1390. nums=n;
  1391. #else
  1392. nums = CPU_COUNT(sizeof(cpuset),&cpuset);
  1393. #endif
  1394. return nums;
  1395. #else
  1396. if (nums >= CPU_SETSIZE) {
  1397. cpusetp = CPU_ALLOC(nums);
  1398. if (cpusetp == NULL) {
  1399. return nums;
  1400. }
  1401. size = CPU_ALLOC_SIZE(nums);
  1402. ret = sched_getaffinity(0,size,cpusetp);
  1403. if (ret!=0) {
  1404. CPU_FREE(cpusetp);
  1405. return nums;
  1406. }
  1407. ret = CPU_COUNT_S(size,cpusetp);
  1408. if (ret > 0 && ret < nums) nums = ret;
  1409. CPU_FREE(cpusetp);
  1410. return nums;
  1411. } else {
  1412. ret = sched_getaffinity(0,sizeof(cpuset),&cpuset);
  1413. if (ret!=0) {
  1414. return nums;
  1415. }
  1416. ret = CPU_COUNT(&cpuset);
  1417. if (ret > 0 && ret < nums) nums = ret;
  1418. return nums;
  1419. }
  1420. #endif
  1421. #endif
  1422. }
  1423. #endif
  1424. #endif
  1425. #ifdef OS_ANDROID
  1426. int get_num_procs(void) {
  1427. static int nums = 0;
  1428. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1429. return nums;
  1430. }
  1431. #endif
  1432. #ifdef OS_HAIKU
  1433. int get_num_procs(void) {
  1434. static int nums = 0;
  1435. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1436. return nums;
  1437. }
  1438. #endif
  1439. #ifdef OS_AIX
  1440. int get_num_procs(void) {
  1441. static int nums = 0;
  1442. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  1443. return nums;
  1444. }
  1445. #endif
  1446. #ifdef OS_WINDOWS
  1447. int get_num_procs(void) {
  1448. static int nums = 0;
  1449. if (nums == 0) {
  1450. SYSTEM_INFO sysinfo;
  1451. GetSystemInfo(&sysinfo);
  1452. nums = sysinfo.dwNumberOfProcessors;
  1453. }
  1454. return nums;
  1455. }
  1456. #endif
  1457. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY)
  1458. int get_num_procs(void) {
  1459. static int nums = 0;
  1460. int m[2];
  1461. size_t len;
  1462. if (nums == 0) {
  1463. m[0] = CTL_HW;
  1464. m[1] = HW_NCPU;
  1465. len = sizeof(int);
  1466. sysctl(m, 2, &nums, &len, NULL, 0);
  1467. }
  1468. return nums;
  1469. }
  1470. #endif
  1471. #if defined(OS_DARWIN)
  1472. int get_num_procs(void) {
  1473. static int nums = 0;
  1474. size_t len;
  1475. if (nums == 0){
  1476. len = sizeof(int);
  1477. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  1478. }
  1479. return nums;
  1480. }
  1481. /*
  1482. void set_stack_limit(int limitMB){
  1483. int result=0;
  1484. struct rlimit rl;
  1485. rlim_t StackSize;
  1486. StackSize=limitMB*1024*1024;
  1487. result=getrlimit(RLIMIT_STACK, &rl);
  1488. if(result==0){
  1489. if(rl.rlim_cur < StackSize){
  1490. rl.rlim_cur=StackSize;
  1491. result=setrlimit(RLIMIT_STACK, &rl);
  1492. if(result !=0){
  1493. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  1494. }
  1495. }
  1496. }
  1497. }
  1498. */
  1499. #endif
  1500. /*
  1501. OpenBLAS uses the numbers of CPU cores in multithreading.
  1502. It can be set by openblas_set_num_threads(int num_threads);
  1503. */
  1504. int blas_cpu_number = 0;
  1505. /*
  1506. The numbers of threads in the thread pool.
  1507. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  1508. */
  1509. int blas_num_threads = 0;
  1510. int goto_get_num_procs (void) {
  1511. return blas_cpu_number;
  1512. }
  1513. void openblas_fork_handler()
  1514. {
  1515. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  1516. // built with "make USE_OPENMP=0".
  1517. // Hanging can still happen when OpenBLAS is built against the libgomp
  1518. // implementation of OpenMP. The problem is tracked at:
  1519. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  1520. // In the mean time build with USE_OPENMP=0 or link against another
  1521. // implementation of OpenMP.
  1522. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  1523. int err;
  1524. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
  1525. if(err != 0)
  1526. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  1527. #endif
  1528. }
  1529. extern int openblas_num_threads_env();
  1530. extern int openblas_goto_num_threads_env();
  1531. extern int openblas_omp_num_threads_env();
  1532. int blas_get_cpu_number(void){
  1533. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
  1534. int max_num;
  1535. #endif
  1536. int blas_goto_num = 0;
  1537. int blas_omp_num = 0;
  1538. if (blas_num_threads) return blas_num_threads;
  1539. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
  1540. max_num = get_num_procs();
  1541. #endif
  1542. // blas_goto_num = 0;
  1543. #ifndef USE_OPENMP
  1544. blas_goto_num=openblas_num_threads_env();
  1545. if (blas_goto_num < 0) blas_goto_num = 0;
  1546. if (blas_goto_num == 0) {
  1547. blas_goto_num=openblas_goto_num_threads_env();
  1548. if (blas_goto_num < 0) blas_goto_num = 0;
  1549. }
  1550. #endif
  1551. // blas_omp_num = 0;
  1552. blas_omp_num=openblas_omp_num_threads_env();
  1553. if (blas_omp_num < 0) blas_omp_num = 0;
  1554. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  1555. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  1556. else blas_num_threads = MAX_CPU_NUMBER;
  1557. #if defined(OS_LINUX) || defined(OS_WINDOWS) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN) || defined(OS_ANDROID)
  1558. if (blas_num_threads > max_num) blas_num_threads = max_num;
  1559. #endif
  1560. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  1561. #ifdef DEBUG
  1562. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  1563. #endif
  1564. blas_cpu_number = blas_num_threads;
  1565. return blas_num_threads;
  1566. }
  1567. #endif
  1568. int openblas_get_num_procs(void) {
  1569. #ifndef SMP
  1570. return 1;
  1571. #else
  1572. return get_num_procs();
  1573. #endif
  1574. }
  1575. int openblas_get_num_threads(void) {
  1576. #ifndef SMP
  1577. return 1;
  1578. #else
  1579. // init blas_cpu_number if needed
  1580. blas_get_cpu_number();
  1581. return blas_cpu_number;
  1582. #endif
  1583. }
  1584. struct release_t {
  1585. void *address;
  1586. void (*func)(struct release_t *);
  1587. long attr;
  1588. };
  1589. int hugetlb_allocated = 0;
  1590. static struct release_t release_info[NUM_BUFFERS];
  1591. static int release_pos = 0;
  1592. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1593. static int hot_alloc = 0;
  1594. #endif
  1595. /* Global lock for memory allocation */
  1596. #if defined(USE_PTHREAD_LOCK)
  1597. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  1598. #elif defined(USE_PTHREAD_SPINLOCK)
  1599. static pthread_spinlock_t alloc_lock = 0;
  1600. #else
  1601. static BLASULONG alloc_lock = 0UL;
  1602. #endif
  1603. #ifdef ALLOC_MMAP
  1604. static void alloc_mmap_free(struct release_t *release){
  1605. if (!release->address) return;
  1606. if (munmap(release -> address, BUFFER_SIZE)) {
  1607. int errsv=errno;
  1608. perror("OpenBLAS : munmap failed:");
  1609. printf("error code=%d,\trelease->address=%lx\n",errsv,release->address);
  1610. }
  1611. }
  1612. #ifdef NO_WARMUP
  1613. static void *alloc_mmap(void *address){
  1614. void *map_address;
  1615. if (address){
  1616. map_address = mmap(address,
  1617. BUFFER_SIZE,
  1618. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1619. } else {
  1620. map_address = mmap(address,
  1621. BUFFER_SIZE,
  1622. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1623. }
  1624. if (map_address != (void *)-1) {
  1625. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1626. LOCK_COMMAND(&alloc_lock);
  1627. #endif
  1628. release_info[release_pos].address = map_address;
  1629. release_info[release_pos].func = alloc_mmap_free;
  1630. release_pos ++;
  1631. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1632. UNLOCK_COMMAND(&alloc_lock);
  1633. #endif
  1634. } else {
  1635. #ifdef DEBUG
  1636. int errsv=errno;
  1637. perror("OpenBLAS : mmap failed:");
  1638. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1639. #endif
  1640. }
  1641. #ifdef OS_LINUX
  1642. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1643. #endif
  1644. return map_address;
  1645. }
  1646. #else
  1647. #define BENCH_ITERATION 4
  1648. #define SCALING 2
  1649. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  1650. BLASULONG original, *p;
  1651. BLASULONG start, stop, min;
  1652. int iter, i, count;
  1653. min = (BLASULONG)-1;
  1654. original = *(BLASULONG *)(address + size - PAGESIZE);
  1655. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  1656. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  1657. p = (BLASULONG *)address;
  1658. count = size / PAGESIZE;
  1659. start = rpcc();
  1660. for (i = 0; i < count; i ++) {
  1661. p = (BLASULONG *)(*p);
  1662. }
  1663. stop = rpcc();
  1664. if (min > stop - start) min = stop - start;
  1665. }
  1666. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  1667. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  1668. return min;
  1669. }
  1670. static void *alloc_mmap(void *address){
  1671. void *map_address, *best_address;
  1672. BLASULONG best, start, current;
  1673. BLASULONG allocsize;
  1674. if (address){
  1675. /* Just give up use advanced operation */
  1676. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1677. #ifdef OS_LINUX
  1678. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1679. #endif
  1680. } else {
  1681. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1682. if (hot_alloc == 0) {
  1683. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1684. #ifdef OS_LINUX
  1685. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1686. #endif
  1687. } else {
  1688. #endif
  1689. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  1690. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1691. if (map_address != (void *)-1) {
  1692. #ifdef OS_LINUX
  1693. #ifdef DEBUG
  1694. int ret=0;
  1695. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1696. if(ret==-1){
  1697. int errsv=errno;
  1698. perror("OpenBLAS alloc_mmap:");
  1699. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1700. }
  1701. #else
  1702. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1703. #endif
  1704. #endif
  1705. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  1706. start = (BLASULONG)map_address;
  1707. current = (SCALING - 1) * BUFFER_SIZE;
  1708. while(current > 0) {
  1709. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  1710. start += PAGESIZE;
  1711. current -= PAGESIZE;
  1712. }
  1713. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  1714. start = (BLASULONG)map_address;
  1715. best = (BLASULONG)-1;
  1716. best_address = map_address;
  1717. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  1718. current = run_bench(start, allocsize);
  1719. if (best > current) {
  1720. best = current;
  1721. best_address = (void *)start;
  1722. }
  1723. start += PAGESIZE;
  1724. }
  1725. if ((BLASULONG)best_address > (BLASULONG)map_address)
  1726. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  1727. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  1728. map_address = best_address;
  1729. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1730. hot_alloc = 2;
  1731. #endif
  1732. }
  1733. }
  1734. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1735. }
  1736. #endif
  1737. if (map_address != (void *)-1) {
  1738. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1739. LOCK_COMMAND(&alloc_lock);
  1740. #endif
  1741. release_info[release_pos].address = map_address;
  1742. release_info[release_pos].func = alloc_mmap_free;
  1743. release_pos ++;
  1744. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1745. UNLOCK_COMMAND(&alloc_lock);
  1746. #endif
  1747. }
  1748. return map_address;
  1749. }
  1750. #endif
  1751. #endif
  1752. #ifdef ALLOC_MALLOC
  1753. static void alloc_malloc_free(struct release_t *release){
  1754. free(release -> address);
  1755. }
  1756. static void *alloc_malloc(void *address){
  1757. void *map_address;
  1758. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1759. if (map_address == (void *)NULL) map_address = (void *)-1;
  1760. if (map_address != (void *)-1) {
  1761. release_info[release_pos].address = map_address;
  1762. release_info[release_pos].func = alloc_malloc_free;
  1763. release_pos ++;
  1764. }
  1765. return map_address;
  1766. }
  1767. #endif
  1768. #ifdef ALLOC_QALLOC
  1769. void *qalloc(int flags, size_t bytes);
  1770. void *qfree (void *address);
  1771. #define QNONCACHE 0x1
  1772. #define QCOMMS 0x2
  1773. #define QFAST 0x4
  1774. static void alloc_qalloc_free(struct release_t *release){
  1775. qfree(release -> address);
  1776. }
  1777. static void *alloc_qalloc(void *address){
  1778. void *map_address;
  1779. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  1780. if (map_address == (void *)NULL) map_address = (void *)-1;
  1781. if (map_address != (void *)-1) {
  1782. release_info[release_pos].address = map_address;
  1783. release_info[release_pos].func = alloc_qalloc_free;
  1784. release_pos ++;
  1785. }
  1786. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  1787. }
  1788. #endif
  1789. #ifdef ALLOC_WINDOWS
  1790. static void alloc_windows_free(struct release_t *release){
  1791. VirtualFree(release -> address, 0, MEM_RELEASE);
  1792. }
  1793. static void *alloc_windows(void *address){
  1794. void *map_address;
  1795. map_address = VirtualAlloc(address,
  1796. BUFFER_SIZE,
  1797. MEM_RESERVE | MEM_COMMIT,
  1798. PAGE_READWRITE);
  1799. if (map_address == (void *)NULL) map_address = (void *)-1;
  1800. if (map_address != (void *)-1) {
  1801. release_info[release_pos].address = map_address;
  1802. release_info[release_pos].func = alloc_windows_free;
  1803. release_pos ++;
  1804. }
  1805. return map_address;
  1806. }
  1807. #endif
  1808. #ifdef ALLOC_DEVICEDRIVER
  1809. #ifndef DEVICEDRIVER_NAME
  1810. #define DEVICEDRIVER_NAME "/dev/mapper"
  1811. #endif
  1812. static void alloc_devicedirver_free(struct release_t *release){
  1813. if (munmap(release -> address, BUFFER_SIZE)) {
  1814. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  1815. }
  1816. if (close(release -> attr)) {
  1817. printf("OpenBLAS : Bugphysarea close failed.\n");
  1818. }
  1819. }
  1820. static void *alloc_devicedirver(void *address){
  1821. int fd;
  1822. void *map_address;
  1823. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  1824. return (void *)-1;
  1825. }
  1826. map_address = mmap(address, BUFFER_SIZE,
  1827. PROT_READ | PROT_WRITE,
  1828. MAP_FILE | MAP_SHARED,
  1829. fd, 0);
  1830. if (map_address != (void *)-1) {
  1831. release_info[release_pos].address = map_address;
  1832. release_info[release_pos].attr = fd;
  1833. release_info[release_pos].func = alloc_devicedirver_free;
  1834. release_pos ++;
  1835. }
  1836. return map_address;
  1837. }
  1838. #endif
  1839. #ifdef ALLOC_SHM
  1840. static void alloc_shm_free(struct release_t *release){
  1841. if (shmdt(release -> address)) {
  1842. printf("OpenBLAS : Shared memory unmap failed.\n");
  1843. }
  1844. }
  1845. static void *alloc_shm(void *address){
  1846. void *map_address;
  1847. int shmid;
  1848. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  1849. map_address = (void *)shmat(shmid, address, 0);
  1850. if (map_address != (void *)-1){
  1851. #ifdef OS_LINUX
  1852. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1853. #endif
  1854. shmctl(shmid, IPC_RMID, 0);
  1855. release_info[release_pos].address = map_address;
  1856. release_info[release_pos].attr = shmid;
  1857. release_info[release_pos].func = alloc_shm_free;
  1858. release_pos ++;
  1859. }
  1860. return map_address;
  1861. }
  1862. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  1863. static void alloc_hugetlb_free(struct release_t *release){
  1864. #if defined(OS_LINUX) || defined(OS_AIX)
  1865. if (shmdt(release -> address)) {
  1866. printf("OpenBLAS : Hugepage unmap failed.\n");
  1867. }
  1868. #endif
  1869. #ifdef __sun__
  1870. munmap(release -> address, BUFFER_SIZE);
  1871. #endif
  1872. #ifdef OS_WINDOWS
  1873. VirtualFree(release -> address, 0, MEM_LARGE_PAGES | MEM_RELEASE);
  1874. #endif
  1875. }
  1876. static void *alloc_hugetlb(void *address){
  1877. void *map_address = (void *)-1;
  1878. #if defined(OS_LINUX) || defined(OS_AIX)
  1879. int shmid;
  1880. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  1881. #ifdef OS_LINUX
  1882. SHM_HUGETLB |
  1883. #endif
  1884. #ifdef OS_AIX
  1885. SHM_LGPAGE | SHM_PIN |
  1886. #endif
  1887. IPC_CREAT | SHM_R | SHM_W);
  1888. if (shmid != -1) {
  1889. map_address = (void *)shmat(shmid, address, SHM_RND);
  1890. #ifdef OS_LINUX
  1891. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1892. #endif
  1893. if (map_address != (void *)-1){
  1894. shmctl(shmid, IPC_RMID, 0);
  1895. }
  1896. }
  1897. #endif
  1898. #ifdef __sun__
  1899. struct memcntl_mha mha;
  1900. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  1901. mha.mha_flags = 0;
  1902. mha.mha_pagesize = HUGE_PAGESIZE;
  1903. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  1904. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  1905. #endif
  1906. #ifdef OS_WINDOWS
  1907. HANDLE hToken;
  1908. TOKEN_PRIVILEGES tp;
  1909. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  1910. tp.PrivilegeCount = 1;
  1911. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1912. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  1913. CloseHandle(hToken);
  1914. return (void*)-1;
  1915. }
  1916. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  1917. CloseHandle(hToken);
  1918. return (void*)-1;
  1919. }
  1920. map_address = (void *)VirtualAlloc(address,
  1921. BUFFER_SIZE,
  1922. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  1923. PAGE_READWRITE);
  1924. tp.Privileges[0].Attributes = 0;
  1925. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  1926. if (map_address == (void *)NULL) map_address = (void *)-1;
  1927. #endif
  1928. if (map_address != (void *)-1){
  1929. release_info[release_pos].address = map_address;
  1930. release_info[release_pos].func = alloc_hugetlb_free;
  1931. release_pos ++;
  1932. }
  1933. return map_address;
  1934. }
  1935. #endif
  1936. #endif
  1937. #ifdef ALLOC_HUGETLBFILE
  1938. static int hugetlb_pid = 0;
  1939. static void alloc_hugetlbfile_free(struct release_t *release){
  1940. if (munmap(release -> address, BUFFER_SIZE)) {
  1941. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  1942. }
  1943. if (close(release -> attr)) {
  1944. printf("OpenBLAS : HugeTLBfs close failed.\n");
  1945. }
  1946. }
  1947. static void *alloc_hugetlbfile(void *address){
  1948. void *map_address = (void *)-1;
  1949. int fd;
  1950. char filename[64];
  1951. if (!hugetlb_pid) hugetlb_pid = getpid();
  1952. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  1953. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  1954. return (void *)-1;
  1955. }
  1956. unlink(filename);
  1957. map_address = mmap(address, BUFFER_SIZE,
  1958. PROT_READ | PROT_WRITE,
  1959. MAP_SHARED,
  1960. fd, 0);
  1961. if (map_address != (void *)-1) {
  1962. release_info[release_pos].address = map_address;
  1963. release_info[release_pos].attr = fd;
  1964. release_info[release_pos].func = alloc_hugetlbfile_free;
  1965. release_pos ++;
  1966. }
  1967. return map_address;
  1968. }
  1969. #endif
  1970. #ifdef SEEK_ADDRESS
  1971. static BLASULONG base_address = 0UL;
  1972. #else
  1973. static BLASULONG base_address = BASE_ADDRESS;
  1974. #endif
  1975. static volatile struct {
  1976. BLASULONG lock;
  1977. void *addr;
  1978. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  1979. int pos;
  1980. #endif
  1981. int used;
  1982. #ifndef __64BIT__
  1983. char dummy[48];
  1984. #else
  1985. char dummy[40];
  1986. #endif
  1987. } memory[NUM_BUFFERS];
  1988. static int memory_initialized = 0;
  1989. /* Memory allocation routine */
  1990. /* procpos ... indicates where it comes from */
  1991. /* 0 : Level 3 functions */
  1992. /* 1 : Level 2 functions */
  1993. /* 2 : Thread */
  1994. void *blas_memory_alloc(int procpos){
  1995. int position;
  1996. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  1997. int mypos = 0;
  1998. #endif
  1999. void *map_address;
  2000. void *(*memoryalloc[])(void *address) = {
  2001. #ifdef ALLOC_DEVICEDRIVER
  2002. alloc_devicedirver,
  2003. #endif
  2004. /* Hugetlb implicitly assumes ALLOC_SHM */
  2005. #ifdef ALLOC_SHM
  2006. alloc_shm,
  2007. #endif
  2008. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  2009. alloc_hugetlb,
  2010. #endif
  2011. #ifdef ALLOC_MMAP
  2012. alloc_mmap,
  2013. #endif
  2014. #ifdef ALLOC_QALLOC
  2015. alloc_qalloc,
  2016. #endif
  2017. #ifdef ALLOC_WINDOWS
  2018. alloc_windows,
  2019. #endif
  2020. #ifdef ALLOC_MALLOC
  2021. alloc_malloc,
  2022. #endif
  2023. NULL,
  2024. };
  2025. void *(**func)(void *address);
  2026. #if defined(USE_OPENMP)
  2027. if (!memory_initialized) {
  2028. #endif
  2029. LOCK_COMMAND(&alloc_lock);
  2030. if (!memory_initialized) {
  2031. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2032. for (position = 0; position < NUM_BUFFERS; position ++){
  2033. memory[position].addr = (void *)0;
  2034. memory[position].pos = -1;
  2035. memory[position].used = 0;
  2036. memory[position].lock = 0;
  2037. }
  2038. #endif
  2039. #ifdef DYNAMIC_ARCH
  2040. gotoblas_dynamic_init();
  2041. #endif
  2042. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2043. gotoblas_affinity_init();
  2044. #endif
  2045. #ifdef SMP
  2046. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  2047. #endif
  2048. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  2049. #ifndef DYNAMIC_ARCH
  2050. blas_set_parameter();
  2051. #endif
  2052. #endif
  2053. memory_initialized = 1;
  2054. }
  2055. UNLOCK_COMMAND(&alloc_lock);
  2056. #if defined(USE_OPENMP)
  2057. }
  2058. #endif
  2059. #ifdef DEBUG
  2060. printf("Alloc Start ...\n");
  2061. #endif
  2062. /* #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2063. mypos = WhereAmI();
  2064. position = mypos;
  2065. while (position >= NUM_BUFFERS) position >>= 1;
  2066. do {
  2067. if (!memory[position].used && (memory[position].pos == mypos)) {
  2068. #if defined(SMP) && !defined(USE_OPENMP)
  2069. LOCK_COMMAND(&alloc_lock);
  2070. #else
  2071. blas_lock(&memory[position].lock);
  2072. #endif
  2073. if (!memory[position].used) goto allocation;
  2074. #if defined(SMP) && !defined(USE_OPENMP)
  2075. UNLOCK_COMMAND(&alloc_lock);
  2076. #else
  2077. blas_unlock(&memory[position].lock);
  2078. #endif
  2079. }
  2080. position ++;
  2081. } while (position < NUM_BUFFERS);
  2082. #endif */
  2083. position = 0;
  2084. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2085. LOCK_COMMAND(&alloc_lock);
  2086. #endif
  2087. do {
  2088. RMB;
  2089. #if defined(USE_OPENMP)
  2090. if (!memory[position].used) {
  2091. blas_lock(&memory[position].lock);
  2092. #endif
  2093. if (!memory[position].used) goto allocation;
  2094. #if defined(USE_OPENMP)
  2095. blas_unlock(&memory[position].lock);
  2096. }
  2097. #endif
  2098. position ++;
  2099. } while (position < NUM_BUFFERS);
  2100. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2101. UNLOCK_COMMAND(&alloc_lock);
  2102. #endif
  2103. goto error;
  2104. allocation :
  2105. #ifdef DEBUG
  2106. printf(" Position -> %d\n", position);
  2107. #endif
  2108. memory[position].used = 1;
  2109. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2110. UNLOCK_COMMAND(&alloc_lock);
  2111. #else
  2112. blas_unlock(&memory[position].lock);
  2113. #endif
  2114. if (!memory[position].addr) {
  2115. do {
  2116. #ifdef DEBUG
  2117. printf("Allocation Start : %lx\n", base_address);
  2118. #endif
  2119. map_address = (void *)-1;
  2120. func = &memoryalloc[0];
  2121. while ((func != NULL) && (map_address == (void *) -1)) {
  2122. map_address = (*func)((void *)base_address);
  2123. #ifdef ALLOC_DEVICEDRIVER
  2124. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  2125. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation was failed.\n");
  2126. }
  2127. #endif
  2128. #ifdef ALLOC_HUGETLBFILE
  2129. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  2130. #ifndef OS_WINDOWS
  2131. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  2132. #endif
  2133. }
  2134. #endif
  2135. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  2136. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  2137. #endif
  2138. func ++;
  2139. }
  2140. #ifdef DEBUG
  2141. printf(" Success -> %08lx\n", map_address);
  2142. #endif
  2143. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  2144. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  2145. } while ((BLASLONG)map_address == -1);
  2146. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2147. LOCK_COMMAND(&alloc_lock);
  2148. #endif
  2149. memory[position].addr = map_address;
  2150. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2151. UNLOCK_COMMAND(&alloc_lock);
  2152. #endif
  2153. #ifdef DEBUG
  2154. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  2155. #endif
  2156. }
  2157. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2158. if (memory[position].pos == -1) memory[position].pos = mypos;
  2159. #endif
  2160. #ifdef DYNAMIC_ARCH
  2161. if (memory_initialized == 1) {
  2162. LOCK_COMMAND(&alloc_lock);
  2163. if (memory_initialized == 1) {
  2164. if (!gotoblas) gotoblas_dynamic_init();
  2165. memory_initialized = 2;
  2166. }
  2167. UNLOCK_COMMAND(&alloc_lock);
  2168. }
  2169. #endif
  2170. #ifdef DEBUG
  2171. printf("Mapped : %p %3d\n\n",
  2172. (void *)memory[position].addr, position);
  2173. #endif
  2174. return (void *)memory[position].addr;
  2175. error:
  2176. printf("BLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  2177. return NULL;
  2178. }
  2179. void blas_memory_free(void *free_area){
  2180. int position;
  2181. #ifdef DEBUG
  2182. printf("Unmapped Start : %p ...\n", free_area);
  2183. #endif
  2184. position = 0;
  2185. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2186. LOCK_COMMAND(&alloc_lock);
  2187. #endif
  2188. while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
  2189. position++;
  2190. if (memory[position].addr != free_area) goto error;
  2191. #ifdef DEBUG
  2192. printf(" Position : %d\n", position);
  2193. #endif
  2194. // arm: ensure all writes are finished before other thread takes this memory
  2195. WMB;
  2196. memory[position].used = 0;
  2197. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2198. UNLOCK_COMMAND(&alloc_lock);
  2199. #endif
  2200. #ifdef DEBUG
  2201. printf("Unmap Succeeded.\n\n");
  2202. #endif
  2203. return;
  2204. error:
  2205. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  2206. #ifdef DEBUG
  2207. for (position = 0; position < NUM_BUFFERS; position++)
  2208. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  2209. #endif
  2210. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2211. UNLOCK_COMMAND(&alloc_lock);
  2212. #endif
  2213. return;
  2214. }
  2215. void *blas_memory_alloc_nolock(int unused) {
  2216. void *map_address;
  2217. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  2218. return map_address;
  2219. }
  2220. void blas_memory_free_nolock(void * map_address) {
  2221. free(map_address);
  2222. }
  2223. void blas_shutdown(void){
  2224. int pos;
  2225. #ifdef SMP
  2226. BLASFUNC(blas_thread_shutdown)();
  2227. #endif
  2228. LOCK_COMMAND(&alloc_lock);
  2229. for (pos = 0; pos < release_pos; pos ++) {
  2230. release_info[pos].func(&release_info[pos]);
  2231. }
  2232. #ifdef SEEK_ADDRESS
  2233. base_address = 0UL;
  2234. #else
  2235. base_address = BASE_ADDRESS;
  2236. #endif
  2237. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  2238. memory[pos].addr = (void *)0;
  2239. memory[pos].used = 0;
  2240. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2241. memory[pos].pos = -1;
  2242. #endif
  2243. memory[pos].lock = 0;
  2244. }
  2245. UNLOCK_COMMAND(&alloc_lock);
  2246. return;
  2247. }
  2248. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2249. #if defined(SMP) || defined(USE_LOCKING)
  2250. #if defined(USE_PTHREAD_LOCK)
  2251. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  2252. #elif defined(USE_PTHREAD_SPINLOCK)
  2253. static pthread_spinlock_t init_lock = 0;
  2254. #else
  2255. static BLASULONG init_lock = 0UL;
  2256. #endif
  2257. #endif
  2258. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  2259. void *sa, void *sb, BLASLONG pos) {
  2260. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  2261. size_t size;
  2262. BLASULONG buffer;
  2263. size = BUFFER_SIZE - PAGESIZE;
  2264. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2265. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2266. if (hot_alloc != 2) {
  2267. #endif
  2268. #if defined(SMP) || defined(USE_LOCKING)
  2269. LOCK_COMMAND(&init_lock);
  2270. #endif
  2271. while (size > 0) {
  2272. *(int *)buffer = size;
  2273. buffer += PAGESIZE;
  2274. size -= PAGESIZE;
  2275. }
  2276. #if defined(SMP) || defined(USE_LOCKING)
  2277. UNLOCK_COMMAND(&init_lock);
  2278. #endif
  2279. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  2280. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2281. while (size > 0) {
  2282. *(int *)buffer = size;
  2283. buffer += 64;
  2284. size -= 64;
  2285. }
  2286. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2287. }
  2288. #endif
  2289. #endif
  2290. }
  2291. #ifdef SMP
  2292. static void _init_thread_memory(void *buffer) {
  2293. blas_queue_t queue[MAX_CPU_NUMBER];
  2294. int num_cpu;
  2295. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  2296. blas_queue_init(&queue[num_cpu]);
  2297. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  2298. queue[num_cpu].routine = &_touch_memory;
  2299. queue[num_cpu].args = NULL;
  2300. queue[num_cpu].next = &queue[num_cpu + 1];
  2301. }
  2302. queue[num_cpu - 1].next = NULL;
  2303. queue[0].sa = buffer;
  2304. exec_blas(num_cpu, queue);
  2305. }
  2306. #endif
  2307. static void gotoblas_memory_init(void) {
  2308. void *buffer;
  2309. hot_alloc = 1;
  2310. buffer = (void *)blas_memory_alloc(0);
  2311. #ifdef SMP
  2312. if (blas_cpu_number == 0) blas_get_cpu_number();
  2313. #ifdef SMP_SERVER
  2314. if (blas_server_avail == 0) blas_thread_init();
  2315. #endif
  2316. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  2317. #else
  2318. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  2319. #endif
  2320. blas_memory_free(buffer);
  2321. }
  2322. #endif
  2323. /* Initialization for all function; this function should be called before main */
  2324. static int gotoblas_initialized = 0;
  2325. extern void openblas_read_env();
  2326. void CONSTRUCTOR gotoblas_init(void) {
  2327. if (gotoblas_initialized) return;
  2328. #ifdef SMP
  2329. openblas_fork_handler();
  2330. #endif
  2331. openblas_read_env();
  2332. #ifdef PROFILE
  2333. moncontrol (0);
  2334. #endif
  2335. #ifdef DYNAMIC_ARCH
  2336. gotoblas_dynamic_init();
  2337. #endif
  2338. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2339. gotoblas_affinity_init();
  2340. #endif
  2341. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2342. gotoblas_memory_init();
  2343. #endif
  2344. //#if defined(OS_LINUX)
  2345. #if 0
  2346. struct rlimit curlimit;
  2347. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  2348. {
  2349. if ( curlimit.rlim_cur != curlimit.rlim_max )
  2350. {
  2351. curlimit.rlim_cur = curlimit.rlim_max;
  2352. setrlimit(RLIMIT_STACK, &curlimit);
  2353. }
  2354. }
  2355. #endif
  2356. #ifdef SMP
  2357. if (blas_cpu_number == 0) blas_get_cpu_number();
  2358. #ifdef SMP_SERVER
  2359. if (blas_server_avail == 0) blas_thread_init();
  2360. #endif
  2361. #endif
  2362. #ifdef FUNCTION_PROFILE
  2363. gotoblas_profile_init();
  2364. #endif
  2365. gotoblas_initialized = 1;
  2366. #ifdef PROFILE
  2367. moncontrol (1);
  2368. #endif
  2369. }
  2370. void DESTRUCTOR gotoblas_quit(void) {
  2371. if (gotoblas_initialized == 0) return;
  2372. blas_shutdown();
  2373. #ifdef PROFILE
  2374. moncontrol (0);
  2375. #endif
  2376. #ifdef FUNCTION_PROFILE
  2377. gotoblas_profile_quit();
  2378. #endif
  2379. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2380. gotoblas_affinity_quit();
  2381. #endif
  2382. #ifdef DYNAMIC_ARCH
  2383. gotoblas_dynamic_quit();
  2384. #endif
  2385. gotoblas_initialized = 0;
  2386. #ifdef PROFILE
  2387. moncontrol (1);
  2388. #endif
  2389. }
  2390. #if defined(_MSC_VER) && !defined(__clang__)
  2391. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  2392. {
  2393. switch (ul_reason_for_call)
  2394. {
  2395. case DLL_PROCESS_ATTACH:
  2396. gotoblas_init();
  2397. break;
  2398. case DLL_THREAD_ATTACH:
  2399. break;
  2400. case DLL_THREAD_DETACH:
  2401. break;
  2402. case DLL_PROCESS_DETACH:
  2403. gotoblas_quit();
  2404. break;
  2405. default:
  2406. break;
  2407. }
  2408. return TRUE;
  2409. }
  2410. /*
  2411. This is to allow static linking.
  2412. Code adapted from Google performance tools:
  2413. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  2414. Reference:
  2415. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  2416. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  2417. */
  2418. static int on_process_term(void)
  2419. {
  2420. gotoblas_quit();
  2421. return 0;
  2422. }
  2423. #ifdef _WIN64
  2424. #pragma comment(linker, "/INCLUDE:_tls_used")
  2425. #else
  2426. #pragma comment(linker, "/INCLUDE:__tls_used")
  2427. #endif
  2428. #ifdef _WIN64
  2429. #pragma const_seg(".CRT$XLB")
  2430. #else
  2431. #pragma data_seg(".CRT$XLB")
  2432. #endif
  2433. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  2434. #ifdef _WIN64
  2435. #pragma const_seg()
  2436. #else
  2437. #pragma data_seg()
  2438. #endif
  2439. #ifdef _WIN64
  2440. #pragma const_seg(".CRT$XTU")
  2441. #else
  2442. #pragma data_seg(".CRT$XTU")
  2443. #endif
  2444. static int(*p_process_term)(void) = on_process_term;
  2445. #ifdef _WIN64
  2446. #pragma const_seg()
  2447. #else
  2448. #pragma data_seg()
  2449. #endif
  2450. #endif
  2451. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  2452. /* Don't call me; this is just work around for PGI / Sun bug */
  2453. void gotoblas_dummy_for_PGI(void) {
  2454. gotoblas_init();
  2455. gotoblas_quit();
  2456. #if __PGIC__ < 19
  2457. #if 0
  2458. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  2459. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  2460. #else
  2461. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  2462. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  2463. #endif
  2464. #endif
  2465. }
  2466. #endif
  2467. #endif