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