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

Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
4 years ago
4 years ago
7 years ago
7 years ago
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
Remove the need for most locking in memory.c. Using thread local storage for tracking memory allocations means that threads no longer have to lock at all when doing memory allocations / frees. This particularly helps the gemm driver since it does an allocation per invocation. Even without threading at all, this helps, since even calling a lock with no contention has a cost: Before this change, no threading: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 102 ns 102 ns 13504412 BM_SGEMM/6 175 ns 175 ns 7997580 BM_SGEMM/8 205 ns 205 ns 6842073 BM_SGEMM/10 266 ns 266 ns 5294919 BM_SGEMM/16 478 ns 478 ns 2963441 BM_SGEMM/20 690 ns 690 ns 2144755 BM_SGEMM/32 1906 ns 1906 ns 716981 BM_SGEMM/40 2983 ns 2983 ns 473218 BM_SGEMM/64 9421 ns 9422 ns 148450 BM_SGEMM/72 12630 ns 12631 ns 112105 BM_SGEMM/80 15845 ns 15846 ns 89118 BM_SGEMM/90 25675 ns 25676 ns 54332 BM_SGEMM/100 29864 ns 29865 ns 47120 BM_SGEMM/112 37841 ns 37842 ns 36717 BM_SGEMM/128 56531 ns 56532 ns 25361 BM_SGEMM/140 75886 ns 75888 ns 18143 BM_SGEMM/150 98493 ns 98496 ns 14299 BM_SGEMM/160 102620 ns 102622 ns 13381 BM_SGEMM/170 135169 ns 135173 ns 10231 BM_SGEMM/180 146170 ns 146172 ns 9535 BM_SGEMM/189 190226 ns 190231 ns 7397 BM_SGEMM/200 194513 ns 194519 ns 7210 BM_SGEMM/256 396561 ns 396573 ns 3531 ``` with this change: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 14500387 BM_SGEMM/6 166 ns 166 ns 8381763 BM_SGEMM/8 196 ns 196 ns 7277044 BM_SGEMM/10 256 ns 256 ns 5515721 BM_SGEMM/16 463 ns 463 ns 3025197 BM_SGEMM/20 636 ns 636 ns 2070213 BM_SGEMM/32 1885 ns 1885 ns 739444 BM_SGEMM/40 2969 ns 2969 ns 472152 BM_SGEMM/64 9371 ns 9372 ns 148932 BM_SGEMM/72 12431 ns 12431 ns 112919 BM_SGEMM/80 15615 ns 15616 ns 89978 BM_SGEMM/90 25397 ns 25398 ns 55041 BM_SGEMM/100 29445 ns 29446 ns 47540 BM_SGEMM/112 37530 ns 37531 ns 37286 BM_SGEMM/128 55373 ns 55375 ns 25277 BM_SGEMM/140 76241 ns 76241 ns 18259 BM_SGEMM/150 102196 ns 102200 ns 13736 BM_SGEMM/160 101521 ns 101525 ns 13556 BM_SGEMM/170 136182 ns 136184 ns 10567 BM_SGEMM/180 146861 ns 146864 ns 9035 BM_SGEMM/189 192632 ns 192632 ns 7231 BM_SGEMM/200 198547 ns 198555 ns 6995 BM_SGEMM/256 392316 ns 392330 ns 3539 ``` Before, when built with USE_THREAD=1, GEMM_MULTITHREAD_THRESHOLD = 4, the cost of small matrix operations was overshadowed by thread locking (look smaller than 32) even when not explicitly spawning threads: ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 328 ns 328 ns 4170562 BM_SGEMM/6 396 ns 396 ns 3536400 BM_SGEMM/8 418 ns 418 ns 3330102 BM_SGEMM/10 491 ns 491 ns 2863047 BM_SGEMM/16 710 ns 710 ns 2028314 BM_SGEMM/20 871 ns 871 ns 1581546 BM_SGEMM/32 2132 ns 2132 ns 657089 BM_SGEMM/40 3197 ns 3196 ns 437969 BM_SGEMM/64 9645 ns 9645 ns 144987 BM_SGEMM/72 35064 ns 32881 ns 50264 BM_SGEMM/80 37661 ns 35787 ns 42080 BM_SGEMM/90 36507 ns 36077 ns 40091 BM_SGEMM/100 32513 ns 31850 ns 48607 BM_SGEMM/112 41742 ns 41207 ns 37273 BM_SGEMM/128 67211 ns 65095 ns 21933 BM_SGEMM/140 68263 ns 67943 ns 19245 BM_SGEMM/150 121854 ns 115439 ns 10660 BM_SGEMM/160 116826 ns 115539 ns 10000 BM_SGEMM/170 126566 ns 122798 ns 11960 BM_SGEMM/180 130088 ns 127292 ns 11503 BM_SGEMM/189 120309 ns 116634 ns 13162 BM_SGEMM/200 114559 ns 110993 ns 10000 BM_SGEMM/256 217063 ns 207806 ns 6417 ``` and after, it's gone (note this includes my other change which reduces calls to num_cpu_avail): ``` ---------------------------------------------------- Benchmark Time CPU Iterations ---------------------------------------------------- BM_SGEMM/4 95 ns 95 ns 12347650 BM_SGEMM/6 166 ns 166 ns 8259683 BM_SGEMM/8 193 ns 193 ns 7162210 BM_SGEMM/10 258 ns 258 ns 5415657 BM_SGEMM/16 471 ns 471 ns 2981009 BM_SGEMM/20 666 ns 666 ns 2148002 BM_SGEMM/32 1903 ns 1903 ns 738245 BM_SGEMM/40 2969 ns 2969 ns 473239 BM_SGEMM/64 9440 ns 9440 ns 148442 BM_SGEMM/72 37239 ns 33330 ns 46813 BM_SGEMM/80 57350 ns 55949 ns 32251 BM_SGEMM/90 36275 ns 36249 ns 42259 BM_SGEMM/100 31111 ns 31008 ns 45270 BM_SGEMM/112 43782 ns 40912 ns 34749 BM_SGEMM/128 67375 ns 64406 ns 22443 BM_SGEMM/140 76389 ns 67003 ns 21430 BM_SGEMM/150 72952 ns 71830 ns 19793 BM_SGEMM/160 97039 ns 96858 ns 11498 BM_SGEMM/170 123272 ns 122007 ns 11855 BM_SGEMM/180 126828 ns 126505 ns 11567 BM_SGEMM/189 115179 ns 114665 ns 11044 BM_SGEMM/200 89289 ns 87259 ns 16147 BM_SGEMM/256 226252 ns 222677 ns 7375 ``` I've also tested this with ThreadSanitizer and found no data races during execution. I'm not sure why 200 is always faster than it's neighbors, we must be hitting some optimal cache size or something.
7 years ago
9 years ago
9 years ago
9 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. #define NEW_BUFFERS 512
  68. #ifndef likely
  69. #ifdef __GNUC__
  70. #define likely(x) __builtin_expect(!!(x), 1)
  71. #define unlikely(x) __builtin_expect(!!(x), 0)
  72. #else
  73. #define likely(x) (x)
  74. #define unlikely(x) (x)
  75. #endif
  76. #endif
  77. #if defined(USE_TLS) && defined(SMP)
  78. #define COMPILE_TLS
  79. #if USE_TLS != 1
  80. #undef COMPILE_TLS
  81. #endif
  82. #if defined(__GLIBC_PREREQ)
  83. #if !__GLIBC_PREREQ(2,20)
  84. #undef COMPILE_TLS
  85. #endif
  86. #endif
  87. #endif
  88. /* Memory buffer must fit two matrix subblocks of maximal size */
  89. #define XSTR(x) STR(x)
  90. #define STR(x) #x
  91. #if BUFFER_SIZE < (SGEMM_DEFAULT_P * SGEMM_DEFAULT_Q * 4 * 2) || \
  92. BUFFER_SIZE < (SGEMM_DEFAULT_P * SGEMM_DEFAULT_R * 4 * 2) || \
  93. BUFFER_SIZE < (SGEMM_DEFAULT_R * SGEMM_DEFAULT_Q * 4 * 2)
  94. #warning BUFFER_SIZE is too small for P, Q, and R of SGEMM - large calculations may crash !
  95. #endif
  96. #if BUFFER_SIZE < (DGEMM_DEFAULT_P * DGEMM_DEFAULT_Q * 8 * 2) || \
  97. BUFFER_SIZE < (DGEMM_DEFAULT_P * DGEMM_DEFAULT_R * 8 * 2) || \
  98. BUFFER_SIZE < (DGEMM_DEFAULT_R * DGEMM_DEFAULT_Q * 8 * 2)
  99. #warning BUFFER_SIZE is too small for P, Q, and R of DGEMM - large calculations may crash !
  100. #endif
  101. #if BUFFER_SIZE < (CGEMM_DEFAULT_P * CGEMM_DEFAULT_Q * 8 * 2) || \
  102. BUFFER_SIZE < (CGEMM_DEFAULT_P * CGEMM_DEFAULT_R * 8 * 2) || \
  103. BUFFER_SIZE < (CGEMM_DEFAULT_R * CGEMM_DEFAULT_Q * 8 * 2)
  104. #warning BUFFER_SIZE is too small for P, Q, and R of CGEMM - large calculations may crash !
  105. #endif
  106. #if BUFFER_SIZE < (ZGEMM_DEFAULT_P * ZGEMM_DEFAULT_Q * 16 * 2) || \
  107. BUFFER_SIZE < (ZGEMM_DEFAULT_P * ZGEMM_DEFAULT_R * 16 * 2) || \
  108. BUFFER_SIZE < (ZGEMM_DEFAULT_R * ZGEMM_DEFAULT_Q * 16 * 2)
  109. #warning BUFFER_SIZE is too small for P, Q, and R of ZGEMM - large calculations may crash !
  110. #endif
  111. #if defined(COMPILE_TLS)
  112. #include <errno.h>
  113. #if defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)
  114. #define ALLOC_WINDOWS
  115. #ifndef MEM_LARGE_PAGES
  116. #define MEM_LARGE_PAGES 0x20000000
  117. #endif
  118. #else
  119. #define ALLOC_MMAP
  120. #define ALLOC_MALLOC
  121. #endif
  122. #include <stdlib.h>
  123. #include <stdio.h>
  124. #include <fcntl.h>
  125. #if !defined(OS_WINDOWS) || defined(OS_CYGWIN_NT)
  126. #include <sys/mman.h>
  127. #ifndef NO_SYSV_IPC
  128. #include <sys/shm.h>
  129. #endif
  130. #include <sys/ipc.h>
  131. #endif
  132. #include <sys/types.h>
  133. #ifdef OS_LINUX
  134. #include <sys/sysinfo.h>
  135. #include <sched.h>
  136. #include <errno.h>
  137. #include <linux/unistd.h>
  138. #include <sys/syscall.h>
  139. #include <sys/time.h>
  140. #include <sys/resource.h>
  141. #endif
  142. #ifdef OS_HAIKU
  143. #include <unistd.h>
  144. #endif
  145. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY) || defined(OS_DARWIN)
  146. #include <sys/sysctl.h>
  147. #include <sys/resource.h>
  148. #endif
  149. #if defined(OS_WINDOWS) && (defined(__MINGW32__) || defined(__MINGW64__))
  150. #include <conio.h>
  151. #undef printf
  152. #define printf _cprintf
  153. #endif
  154. #ifdef OS_LINUX
  155. #ifndef MPOL_PREFERRED
  156. #define MPOL_PREFERRED 1
  157. #endif
  158. #endif
  159. #if (defined(PPC440) || !defined(OS_LINUX) || defined(HPL)) && !defined(NO_WARMUP)
  160. #define NO_WARMUP
  161. #endif
  162. #ifndef SHM_HUGETLB
  163. #define SHM_HUGETLB 04000
  164. #endif
  165. #ifndef FIXED_PAGESIZE
  166. #define FIXED_PAGESIZE 4096
  167. #endif
  168. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  169. #if defined(_MSC_VER) && !defined(__clang__)
  170. #define CONSTRUCTOR __cdecl
  171. #define DESTRUCTOR __cdecl
  172. #elif (defined(OS_DARWIN) || defined(OS_SUNOS)) && defined(C_GCC)
  173. #define CONSTRUCTOR __attribute__ ((constructor))
  174. #define DESTRUCTOR __attribute__ ((destructor))
  175. #elif __GNUC__ && INIT_PRIORITY && ((GCC_VERSION >= 40300) || (CLANG_VERSION >= 20900))
  176. #define CONSTRUCTOR __attribute__ ((constructor(101)))
  177. #define DESTRUCTOR __attribute__ ((destructor(101)))
  178. #else
  179. #define CONSTRUCTOR __attribute__ ((constructor))
  180. #define DESTRUCTOR __attribute__ ((destructor))
  181. #endif
  182. #ifdef DYNAMIC_ARCH
  183. gotoblas_t *gotoblas = NULL;
  184. #endif
  185. extern void openblas_warning(int verbose, const char * msg);
  186. #ifndef SMP
  187. #define blas_cpu_number 1
  188. #define blas_num_threads 1
  189. /* Dummy Function */
  190. int goto_get_num_procs (void) { return 1;};
  191. void goto_set_num_threads(int num_threads) {};
  192. #else
  193. #if defined(OS_LINUX) || defined(OS_SUNOS)
  194. #ifndef NO_AFFINITY
  195. int get_num_procs(void);
  196. #else
  197. int get_num_procs(void) {
  198. static int nums = 0;
  199. int ret;
  200. #if defined(__GLIBC_PREREQ)
  201. cpu_set_t cpuset,*cpusetp;
  202. size_t size;
  203. #if !__GLIBC_PREREQ(2, 7)
  204. int i;
  205. #if !__GLIBC_PREREQ(2, 6)
  206. int n;
  207. #endif
  208. #endif
  209. #endif
  210. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  211. #if defined(USE_OPENMP)
  212. #if _OPENMP >= 201511
  213. int i,n;
  214. n = 0;
  215. ret = omp_get_num_places();
  216. if (ret > 0) for (i=0; i<ret;i++) n+= omp_get_place_num_procs(i);
  217. if (n > 0) nums = n;
  218. #endif
  219. return (nums > 0 ? nums : 2);
  220. #endif
  221. #if !defined(OS_LINUX)
  222. return (nums > 0 ? nums : 2);
  223. #endif
  224. #if !defined(__GLIBC_PREREQ)
  225. return (nums > 0 ? nums :2);
  226. #else
  227. #if !__GLIBC_PREREQ(2, 3)
  228. return (nums > 0 ? nums :2);
  229. #endif
  230. #if !__GLIBC_PREREQ(2, 7)
  231. ret = sched_getaffinity(0,sizeof(cpuset), &cpuset);
  232. if (ret!=0) return (nums > 0 ? nums :2);
  233. n=0;
  234. #if !__GLIBC_PREREQ(2, 6)
  235. for (i=0;i<nums;i++)
  236. if (CPU_ISSET(i,&cpuset)) n++;
  237. nums=n;
  238. #else
  239. nums = CPU_COUNT(sizeof(cpuset),&cpuset);
  240. #endif
  241. return (nums > 0 ? nums :2);
  242. #else
  243. if (nums >= CPU_SETSIZE) {
  244. cpusetp = CPU_ALLOC(nums);
  245. if (cpusetp == NULL) {
  246. return (nums > 0 ? nums :2);
  247. }
  248. size = CPU_ALLOC_SIZE(nums);
  249. ret = sched_getaffinity(0,size,cpusetp);
  250. if (ret!=0) {
  251. CPU_FREE(cpusetp);
  252. return (nums > 0 ? nums :2);
  253. }
  254. ret = CPU_COUNT_S(size,cpusetp);
  255. if (ret > 0 && ret < nums) nums = ret;
  256. CPU_FREE(cpusetp);
  257. return (nums > 0 ? nums :2);
  258. } else {
  259. ret = sched_getaffinity(0,sizeof(cpuset),&cpuset);
  260. if (ret!=0) {
  261. return (nums > 0 ? nums :2);
  262. }
  263. ret = CPU_COUNT(&cpuset);
  264. if (ret > 0 && ret < nums) nums = ret;
  265. return (nums > 0 ? nums :2);
  266. }
  267. #endif
  268. #endif
  269. }
  270. #endif
  271. #endif
  272. #ifdef OS_ANDROID
  273. int get_num_procs(void) {
  274. static int nums = 0;
  275. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  276. return nums;
  277. }
  278. #endif
  279. #ifdef OS_HAIKU
  280. int get_num_procs(void) {
  281. static int nums = 0;
  282. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  283. return nums;
  284. }
  285. #endif
  286. #ifdef OS_AIX
  287. int get_num_procs(void) {
  288. static int nums = 0;
  289. if (!nums) nums = sysconf(_SC_NPROCESSORS_CONF);
  290. return nums;
  291. }
  292. #endif
  293. #ifdef OS_WINDOWS
  294. int get_num_procs(void) {
  295. static int nums = 0;
  296. if (nums == 0) {
  297. SYSTEM_INFO sysinfo;
  298. GetSystemInfo(&sysinfo);
  299. nums = sysinfo.dwNumberOfProcessors;
  300. }
  301. return nums;
  302. }
  303. #endif
  304. #if defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_NETBSD) || defined(OS_DRAGONFLY)
  305. int get_num_procs(void) {
  306. static int nums = 0;
  307. int m[2];
  308. size_t len;
  309. if (nums == 0) {
  310. m[0] = CTL_HW;
  311. m[1] = HW_NCPU;
  312. len = sizeof(int);
  313. sysctl(m, 2, &nums, &len, NULL, 0);
  314. }
  315. return nums;
  316. }
  317. #endif
  318. #if defined(OS_DARWIN)
  319. int get_num_procs(void) {
  320. static int nums = 0;
  321. size_t len;
  322. if (nums == 0){
  323. len = sizeof(int);
  324. sysctlbyname("hw.physicalcpu", &nums, &len, NULL, 0);
  325. }
  326. return nums;
  327. }
  328. /*
  329. void set_stack_limit(int limitMB){
  330. int result=0;
  331. struct rlimit rl;
  332. rlim_t StackSize;
  333. StackSize=limitMB*1024*1024;
  334. result=getrlimit(RLIMIT_STACK, &rl);
  335. if(result==0){
  336. if(rl.rlim_cur < StackSize){
  337. rl.rlim_cur=StackSize;
  338. result=setrlimit(RLIMIT_STACK, &rl);
  339. if(result !=0){
  340. fprintf(stderr, "OpenBLAS: set stack limit error =%d\n", result);
  341. }
  342. }
  343. }
  344. }
  345. */
  346. #endif
  347. /*
  348. OpenBLAS uses the numbers of CPU cores in multithreading.
  349. It can be set by openblas_set_num_threads(int num_threads);
  350. */
  351. int blas_cpu_number = 0;
  352. /*
  353. The numbers of threads in the thread pool.
  354. This value is equal or large than blas_cpu_number. This means some threads are sleep.
  355. */
  356. int blas_num_threads = 0;
  357. int goto_get_num_procs (void) {
  358. return blas_cpu_number;
  359. }
  360. static void blas_memory_init(void);
  361. void openblas_fork_handler(void)
  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(void);
  378. extern int openblas_goto_num_threads_env(void);
  379. extern int openblas_omp_num_threads_env(void);
  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(void) {
  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(void){
  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(void);
  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 goto_get_num_procs (void) {
  1554. return blas_cpu_number;
  1555. }
  1556. void openblas_fork_handler(void)
  1557. {
  1558. // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
  1559. // built with "make USE_OPENMP=0".
  1560. // Hanging can still happen when OpenBLAS is built against the libgomp
  1561. // implementation of OpenMP. The problem is tracked at:
  1562. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
  1563. // In the mean time build with USE_OPENMP=0 or link against another
  1564. // implementation of OpenMP.
  1565. #if !((defined(OS_WINDOWS) && !defined(OS_CYGWIN_NT)) || defined(OS_ANDROID)) && defined(SMP_SERVER)
  1566. int err;
  1567. err = pthread_atfork ((void (*)(void)) BLASFUNC(blas_thread_shutdown), NULL, NULL);
  1568. if(err != 0)
  1569. openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
  1570. #endif
  1571. }
  1572. extern int openblas_num_threads_env(void);
  1573. extern int openblas_goto_num_threads_env(void);
  1574. extern int openblas_omp_num_threads_env(void);
  1575. int blas_get_cpu_number(void){
  1576. #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)
  1577. int max_num;
  1578. #endif
  1579. int blas_goto_num = 0;
  1580. int blas_omp_num = 0;
  1581. if (blas_num_threads) return blas_num_threads;
  1582. #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)
  1583. max_num = get_num_procs();
  1584. #endif
  1585. // blas_goto_num = 0;
  1586. #ifndef USE_OPENMP
  1587. blas_goto_num=openblas_num_threads_env();
  1588. if (blas_goto_num < 0) blas_goto_num = 0;
  1589. if (blas_goto_num == 0) {
  1590. blas_goto_num=openblas_goto_num_threads_env();
  1591. if (blas_goto_num < 0) blas_goto_num = 0;
  1592. }
  1593. #endif
  1594. // blas_omp_num = 0;
  1595. blas_omp_num=openblas_omp_num_threads_env();
  1596. if (blas_omp_num < 0) blas_omp_num = 0;
  1597. if (blas_goto_num > 0) blas_num_threads = blas_goto_num;
  1598. else if (blas_omp_num > 0) blas_num_threads = blas_omp_num;
  1599. else blas_num_threads = MAX_CPU_NUMBER;
  1600. #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)
  1601. if (blas_num_threads > max_num) blas_num_threads = max_num;
  1602. #endif
  1603. if (blas_num_threads > MAX_CPU_NUMBER) blas_num_threads = MAX_CPU_NUMBER;
  1604. #ifdef DEBUG
  1605. printf( "Adjusted number of threads : %3d\n", blas_num_threads);
  1606. #endif
  1607. blas_cpu_number = blas_num_threads;
  1608. return blas_num_threads;
  1609. }
  1610. #endif
  1611. int openblas_get_num_procs(void) {
  1612. #ifndef SMP
  1613. return 1;
  1614. #else
  1615. return get_num_procs();
  1616. #endif
  1617. }
  1618. int openblas_get_num_threads(void) {
  1619. #ifndef SMP
  1620. return 1;
  1621. #else
  1622. // init blas_cpu_number if needed
  1623. blas_get_cpu_number();
  1624. return blas_cpu_number;
  1625. #endif
  1626. }
  1627. struct release_t {
  1628. void *address;
  1629. void (*func)(struct release_t *);
  1630. long attr;
  1631. };
  1632. int hugetlb_allocated = 0;
  1633. static struct release_t release_info[NUM_BUFFERS];
  1634. static struct release_t *new_release_info;
  1635. static int release_pos = 0;
  1636. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1637. static int hot_alloc = 0;
  1638. #endif
  1639. /* Global lock for memory allocation */
  1640. #if defined(USE_PTHREAD_LOCK)
  1641. static pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
  1642. #elif defined(USE_PTHREAD_SPINLOCK)
  1643. static pthread_spinlock_t alloc_lock = 0;
  1644. #else
  1645. static BLASULONG alloc_lock = 0UL;
  1646. #endif
  1647. #ifdef ALLOC_MMAP
  1648. static void alloc_mmap_free(struct release_t *release){
  1649. if (!release->address) return;
  1650. if (munmap(release -> address, BUFFER_SIZE)) {
  1651. int errsv=errno;
  1652. perror("OpenBLAS : munmap failed:");
  1653. printf("error code=%d,\trelease->address=%p\n",errsv,release->address);
  1654. }
  1655. }
  1656. #ifdef NO_WARMUP
  1657. static void *alloc_mmap(void *address){
  1658. void *map_address;
  1659. if (address){
  1660. map_address = mmap(address,
  1661. BUFFER_SIZE,
  1662. MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1663. } else {
  1664. map_address = mmap(address,
  1665. BUFFER_SIZE,
  1666. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1667. }
  1668. if (map_address != (void *)-1) {
  1669. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1670. LOCK_COMMAND(&alloc_lock);
  1671. #endif
  1672. if (likely(release_pos < NUM_BUFFERS)) {
  1673. release_info[release_pos].address = map_address;
  1674. release_info[release_pos].func = alloc_mmap_free;
  1675. } else {
  1676. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1677. new_release_info[release_pos-NUM_BUFFERS].func = alloc_mmap_free;
  1678. }
  1679. release_pos ++;
  1680. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1681. UNLOCK_COMMAND(&alloc_lock);
  1682. #endif
  1683. } else {
  1684. #ifdef DEBUG
  1685. int errsv=errno;
  1686. perror("OpenBLAS : mmap failed:");
  1687. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1688. #endif
  1689. }
  1690. #ifdef OS_LINUX
  1691. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1692. #endif
  1693. return map_address;
  1694. }
  1695. #else
  1696. #define BENCH_ITERATION 4
  1697. #define SCALING 2
  1698. static inline BLASULONG run_bench(BLASULONG address, BLASULONG size) {
  1699. BLASULONG original, *p;
  1700. BLASULONG start, stop, min;
  1701. int iter, i, count;
  1702. min = (BLASULONG)-1;
  1703. original = *(BLASULONG *)(address + size - PAGESIZE);
  1704. *(BLASULONG *)(address + size - PAGESIZE) = (BLASULONG)address;
  1705. for (iter = 0; iter < BENCH_ITERATION; iter ++ ) {
  1706. p = (BLASULONG *)address;
  1707. count = size / PAGESIZE;
  1708. start = rpcc();
  1709. for (i = 0; i < count; i ++) {
  1710. p = (BLASULONG *)(*p);
  1711. }
  1712. stop = rpcc();
  1713. if (min > stop - start) min = stop - start;
  1714. }
  1715. *(BLASULONG *)(address + size - PAGESIZE + 0) = original;
  1716. *(BLASULONG *)(address + size - PAGESIZE + 8) = (BLASULONG)p;
  1717. return min;
  1718. }
  1719. static void *alloc_mmap(void *address){
  1720. void *map_address, *best_address;
  1721. BLASULONG best, start, current;
  1722. BLASULONG allocsize;
  1723. if (address){
  1724. /* Just give up use advanced operation */
  1725. map_address = mmap(address, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY | MAP_FIXED, -1, 0);
  1726. #ifdef OS_LINUX
  1727. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1728. #endif
  1729. } else {
  1730. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1731. if (hot_alloc == 0) {
  1732. map_address = mmap(NULL, BUFFER_SIZE, MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1733. #ifdef OS_LINUX
  1734. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1735. #endif
  1736. } else {
  1737. #endif
  1738. map_address = mmap(NULL, BUFFER_SIZE * SCALING,
  1739. MMAP_ACCESS, MMAP_POLICY, -1, 0);
  1740. if (map_address != (void *)-1) {
  1741. #ifdef OS_LINUX
  1742. #ifdef DEBUG
  1743. int ret=0;
  1744. ret=my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1745. if(ret==-1){
  1746. int errsv=errno;
  1747. perror("OpenBLAS alloc_mmap:");
  1748. printf("error code=%d,\tmap_address=%lx\n",errsv,map_address);
  1749. }
  1750. #else
  1751. my_mbind(map_address, BUFFER_SIZE * SCALING, MPOL_PREFERRED, NULL, 0, 0);
  1752. #endif
  1753. #endif
  1754. #ifdef BUILD_DOUBLE
  1755. allocsize = DGEMM_P * DGEMM_Q * sizeof(double);
  1756. #elif defined(BUILD_COMPLEX16)
  1757. allocsize = ZGEMM_P * ZGEMM_Q * sizeof(double);
  1758. #elif defined(BUILD_COMPLEX)
  1759. allocsize = CGEMM_P * CGEMM_Q * sizeof(double);
  1760. #else
  1761. allocsize = SGEMM_P * SGEMM_Q * sizeof(double);
  1762. #endif
  1763. start = (BLASULONG)map_address;
  1764. current = (SCALING - 1) * BUFFER_SIZE;
  1765. while(current > 0) {
  1766. *(BLASLONG *)start = (BLASLONG)start + PAGESIZE;
  1767. start += PAGESIZE;
  1768. current -= PAGESIZE;
  1769. }
  1770. *(BLASLONG *)(start - PAGESIZE) = (BLASULONG)map_address;
  1771. start = (BLASULONG)map_address;
  1772. best = (BLASULONG)-1;
  1773. best_address = map_address;
  1774. while ((start + allocsize < (BLASULONG)map_address + (SCALING - 1) * BUFFER_SIZE)) {
  1775. current = run_bench(start, allocsize);
  1776. if (best > current) {
  1777. best = current;
  1778. best_address = (void *)start;
  1779. }
  1780. start += PAGESIZE;
  1781. }
  1782. if ((BLASULONG)best_address > (BLASULONG)map_address)
  1783. munmap(map_address, (BLASULONG)best_address - (BLASULONG)map_address);
  1784. munmap((void *)((BLASULONG)best_address + BUFFER_SIZE), (SCALING - 1) * BUFFER_SIZE + (BLASULONG)map_address - (BLASULONG)best_address);
  1785. map_address = best_address;
  1786. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1787. hot_alloc = 2;
  1788. #endif
  1789. }
  1790. }
  1791. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  1792. }
  1793. #endif
  1794. if (map_address != (void *)-1) {
  1795. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1796. LOCK_COMMAND(&alloc_lock);
  1797. #endif
  1798. if (likely(release_pos < NUM_BUFFERS)) {
  1799. release_info[release_pos].address = map_address;
  1800. release_info[release_pos].func = alloc_mmap_free;
  1801. } else {
  1802. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1803. new_release_info[release_pos-NUM_BUFFERS].func = alloc_mmap_free;
  1804. }
  1805. release_pos ++;
  1806. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  1807. UNLOCK_COMMAND(&alloc_lock);
  1808. #endif
  1809. }
  1810. return map_address;
  1811. }
  1812. #endif
  1813. #endif
  1814. #ifdef ALLOC_MALLOC
  1815. static void alloc_malloc_free(struct release_t *release){
  1816. free(release -> address);
  1817. }
  1818. static void *alloc_malloc(void *address){
  1819. void *map_address;
  1820. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  1821. if (map_address == (void *)NULL) map_address = (void *)-1;
  1822. if (map_address != (void *)-1) {
  1823. if (likely(release_pos < NUM_BUFFERS)) {
  1824. release_info[release_pos].address = map_address;
  1825. release_info[release_pos].func = alloc_malloc_free;
  1826. } else {
  1827. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1828. new_release_info[release_pos-NUM_BUFFERS].func = alloc_malloc_free;
  1829. }
  1830. release_pos ++;
  1831. }
  1832. return map_address;
  1833. }
  1834. #endif
  1835. #ifdef ALLOC_QALLOC
  1836. void *qalloc(int flags, size_t bytes);
  1837. void *qfree (void *address);
  1838. #define QNONCACHE 0x1
  1839. #define QCOMMS 0x2
  1840. #define QFAST 0x4
  1841. static void alloc_qalloc_free(struct release_t *release){
  1842. qfree(release -> address);
  1843. }
  1844. static void *alloc_qalloc(void *address){
  1845. void *map_address;
  1846. map_address = (void *)qalloc(QCOMMS | QFAST, BUFFER_SIZE + FIXED_PAGESIZE);
  1847. if (map_address == (void *)NULL) map_address = (void *)-1;
  1848. if (map_address != (void *)-1) {
  1849. if (likely(release_pos < NUM_BUFFERS)) {
  1850. release_info[release_pos].address = map_address;
  1851. release_info[release_pos].func = alloc_qalloc_free;
  1852. } else {
  1853. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1854. new_release_info[release_pos-NUM_BUFFERS].func = alloc_qalloc_free;
  1855. }
  1856. release_pos ++;
  1857. }
  1858. return (void *)(((BLASULONG)map_address + FIXED_PAGESIZE - 1) & ~(FIXED_PAGESIZE - 1));
  1859. }
  1860. #endif
  1861. #ifdef ALLOC_WINDOWS
  1862. static void alloc_windows_free(struct release_t *release){
  1863. VirtualFree(release -> address, 0, MEM_RELEASE);
  1864. }
  1865. static void *alloc_windows(void *address){
  1866. void *map_address;
  1867. map_address = VirtualAlloc(address,
  1868. BUFFER_SIZE,
  1869. MEM_RESERVE | MEM_COMMIT,
  1870. PAGE_READWRITE);
  1871. if (map_address == (void *)NULL) map_address = (void *)-1;
  1872. if (map_address != (void *)-1) {
  1873. if (likely(release_pos < NUM_BUFFERS)) {
  1874. release_info[release_pos].address = map_address;
  1875. release_info[release_pos].func = alloc_windows_free;
  1876. } else {
  1877. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1878. new_release_info[release_pos-NUM_BUFFERS].func = alloc_windows_free;
  1879. }
  1880. release_pos ++;
  1881. }
  1882. return map_address;
  1883. }
  1884. #endif
  1885. #ifdef ALLOC_DEVICEDRIVER
  1886. #ifndef DEVICEDRIVER_NAME
  1887. #define DEVICEDRIVER_NAME "/dev/mapper"
  1888. #endif
  1889. static void alloc_devicedirver_free(struct release_t *release){
  1890. if (munmap(release -> address, BUFFER_SIZE)) {
  1891. printf("OpenBLAS : Bugphysarea unmap failed.\n");
  1892. }
  1893. if (close(release -> attr)) {
  1894. printf("OpenBLAS : Bugphysarea close failed.\n");
  1895. }
  1896. }
  1897. static void *alloc_devicedirver(void *address){
  1898. int fd;
  1899. void *map_address;
  1900. if ((fd = open(DEVICEDRIVER_NAME, O_RDWR | O_SYNC)) < 0) {
  1901. return (void *)-1;
  1902. }
  1903. map_address = mmap(address, BUFFER_SIZE,
  1904. PROT_READ | PROT_WRITE,
  1905. MAP_FILE | MAP_SHARED,
  1906. fd, 0);
  1907. if (map_address != (void *)-1) {
  1908. if (likely(release_pos < NUM_BUFFERS)) {
  1909. release_info[release_pos].address = map_address;
  1910. release_info[release_pos].attr = fd;
  1911. release_info[release_pos].func = alloc_devicedirver_free;
  1912. } else {
  1913. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1914. new_release_info[release_pos-NUM_BUFFERS].attr = fd;
  1915. new_release_info[release_pos-NUM_BUFFERS].func = alloc_devicedirver_free;
  1916. }
  1917. release_pos ++;
  1918. }
  1919. return map_address;
  1920. }
  1921. #endif
  1922. #ifdef ALLOC_SHM
  1923. static void alloc_shm_free(struct release_t *release){
  1924. if (shmdt(release -> address)) {
  1925. printf("OpenBLAS : Shared memory unmap failed.\n");
  1926. }
  1927. }
  1928. static void *alloc_shm(void *address){
  1929. void *map_address;
  1930. int shmid;
  1931. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,IPC_CREAT | 0600);
  1932. map_address = (void *)shmat(shmid, address, 0);
  1933. if (map_address != (void *)-1){
  1934. #ifdef OS_LINUX
  1935. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1936. #endif
  1937. shmctl(shmid, IPC_RMID, 0);
  1938. if (likely(release_pos < NUM_BUFFERS)) {
  1939. release_info[release_pos].address = map_address;
  1940. release_info[release_pos].attr = shmid;
  1941. release_info[release_pos].func = alloc_shm_free;
  1942. } else {
  1943. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  1944. new_release_info[release_pos-NUM_BUFFERS].attr = shmid;
  1945. new_release_info[release_pos-NUM_BUFFERS].func = alloc_shm_free;
  1946. }
  1947. release_pos ++;
  1948. }
  1949. return map_address;
  1950. }
  1951. #if defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS
  1952. static void alloc_hugetlb_free(struct release_t *release){
  1953. #if defined(OS_LINUX) || defined(OS_AIX)
  1954. if (shmdt(release -> address)) {
  1955. printf("OpenBLAS : Hugepage unmap failed.\n");
  1956. }
  1957. #endif
  1958. #ifdef __sun__
  1959. munmap(release -> address, BUFFER_SIZE);
  1960. #endif
  1961. #ifdef OS_WINDOWS
  1962. VirtualFree(release -> address, 0, MEM_LARGE_PAGES | MEM_RELEASE);
  1963. #endif
  1964. }
  1965. static void *alloc_hugetlb(void *address){
  1966. void *map_address = (void *)-1;
  1967. #if defined(OS_LINUX) || defined(OS_AIX)
  1968. int shmid;
  1969. shmid = shmget(IPC_PRIVATE, BUFFER_SIZE,
  1970. #ifdef OS_LINUX
  1971. SHM_HUGETLB |
  1972. #endif
  1973. #ifdef OS_AIX
  1974. SHM_LGPAGE | SHM_PIN |
  1975. #endif
  1976. IPC_CREAT | SHM_R | SHM_W);
  1977. if (shmid != -1) {
  1978. map_address = (void *)shmat(shmid, address, SHM_RND);
  1979. #ifdef OS_LINUX
  1980. my_mbind(map_address, BUFFER_SIZE, MPOL_PREFERRED, NULL, 0, 0);
  1981. #endif
  1982. if (map_address != (void *)-1){
  1983. shmctl(shmid, IPC_RMID, 0);
  1984. }
  1985. }
  1986. #endif
  1987. #ifdef __sun__
  1988. struct memcntl_mha mha;
  1989. mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
  1990. mha.mha_flags = 0;
  1991. mha.mha_pagesize = HUGE_PAGESIZE;
  1992. memcntl(NULL, 0, MC_HAT_ADVISE, (char *)&mha, 0, 0);
  1993. map_address = (BLASULONG)memalign(HUGE_PAGESIZE, BUFFER_SIZE);
  1994. #endif
  1995. #ifdef OS_WINDOWS
  1996. HANDLE hToken;
  1997. TOKEN_PRIVILEGES tp;
  1998. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != TRUE) return (void *) -1;
  1999. tp.PrivilegeCount = 1;
  2000. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  2001. if (LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid) != TRUE) {
  2002. CloseHandle(hToken);
  2003. return (void*)-1;
  2004. }
  2005. if (AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) != TRUE) {
  2006. CloseHandle(hToken);
  2007. return (void*)-1;
  2008. }
  2009. map_address = (void *)VirtualAlloc(address,
  2010. BUFFER_SIZE,
  2011. MEM_LARGE_PAGES | MEM_RESERVE | MEM_COMMIT,
  2012. PAGE_READWRITE);
  2013. tp.Privileges[0].Attributes = 0;
  2014. AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
  2015. if (map_address == (void *)NULL) map_address = (void *)-1;
  2016. #endif
  2017. if (map_address != (void *)-1){
  2018. if (likely(release_pos < NUM_BUFFERS)) {
  2019. release_info[release_pos].address = map_address;
  2020. release_info[release_pos].func = alloc_hugetlb_free;
  2021. } else {
  2022. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  2023. new_release_info[release_pos-NUM_BUFFERS].func = alloc_hugetlb_free;
  2024. }
  2025. release_pos ++;
  2026. }
  2027. return map_address;
  2028. }
  2029. #endif
  2030. #endif
  2031. #ifdef ALLOC_HUGETLBFILE
  2032. static int hugetlb_pid = 0;
  2033. static void alloc_hugetlbfile_free(struct release_t *release){
  2034. if (munmap(release -> address, BUFFER_SIZE)) {
  2035. printf("OpenBLAS : HugeTLBfs unmap failed.\n");
  2036. }
  2037. if (close(release -> attr)) {
  2038. printf("OpenBLAS : HugeTLBfs close failed.\n");
  2039. }
  2040. }
  2041. static void *alloc_hugetlbfile(void *address){
  2042. void *map_address = (void *)-1;
  2043. int fd;
  2044. char filename[64];
  2045. if (!hugetlb_pid) hugetlb_pid = getpid();
  2046. sprintf(filename, "%s/gotoblas.%d", HUGETLB_FILE_NAME, hugetlb_pid);
  2047. if ((fd = open(filename, O_RDWR | O_CREAT, 0700)) < 0) {
  2048. return (void *)-1;
  2049. }
  2050. unlink(filename);
  2051. map_address = mmap(address, BUFFER_SIZE,
  2052. PROT_READ | PROT_WRITE,
  2053. MAP_SHARED,
  2054. fd, 0);
  2055. if (map_address != (void *)-1) {
  2056. if (likely(release_pos < NUM_BUFFERS)) {
  2057. release_info[release_pos].address = map_address;
  2058. release_info[release_pos].attr = fd;
  2059. release_info[release_pos].func = alloc_hugetlbfile_free;
  2060. } else {
  2061. new_release_info[release_pos-NUM_BUFFERS].address = map_address;
  2062. new_release_info[release_pos-NUM_BUFFERS].attr = fd;
  2063. new_release_info[release_pos-NUM_BUFFERS].func = alloc_hugetlbfile_free;
  2064. }
  2065. release_pos ++;
  2066. }
  2067. return map_address;
  2068. }
  2069. #endif
  2070. #ifdef SEEK_ADDRESS
  2071. static BLASULONG base_address = 0UL;
  2072. #else
  2073. static BLASULONG base_address = BASE_ADDRESS;
  2074. #endif
  2075. static volatile struct {
  2076. BLASULONG lock;
  2077. void *addr;
  2078. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2079. int pos;
  2080. #endif
  2081. int used;
  2082. #ifndef __64BIT__
  2083. char dummy[48];
  2084. #else
  2085. char dummy[40];
  2086. #endif
  2087. } memory[NUM_BUFFERS];
  2088. struct newmemstruct
  2089. {
  2090. BLASULONG lock;
  2091. void *addr;
  2092. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2093. int pos;
  2094. #endif
  2095. int used;
  2096. #ifndef __64BIT__
  2097. char dummy[48];
  2098. #else
  2099. char dummy[40];
  2100. #endif
  2101. };
  2102. static volatile struct newmemstruct *newmemory;
  2103. static int memory_initialized = 0;
  2104. static int memory_overflowed = 0;
  2105. /* Memory allocation routine */
  2106. /* procpos ... indicates where it comes from */
  2107. /* 0 : Level 3 functions */
  2108. /* 1 : Level 2 functions */
  2109. /* 2 : Thread */
  2110. void *blas_memory_alloc(int procpos){
  2111. int i;
  2112. int position;
  2113. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2114. int mypos = 0;
  2115. #endif
  2116. void *map_address;
  2117. void *(*memoryalloc[])(void *address) = {
  2118. #ifdef ALLOC_DEVICEDRIVER
  2119. alloc_devicedirver,
  2120. #endif
  2121. /* Hugetlb implicitly assumes ALLOC_SHM */
  2122. #ifdef ALLOC_SHM
  2123. alloc_shm,
  2124. #endif
  2125. #if ((defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS))
  2126. alloc_hugetlb,
  2127. #endif
  2128. #ifdef ALLOC_MMAP
  2129. alloc_mmap,
  2130. #endif
  2131. #ifdef ALLOC_QALLOC
  2132. alloc_qalloc,
  2133. #endif
  2134. #ifdef ALLOC_WINDOWS
  2135. alloc_windows,
  2136. #endif
  2137. #ifdef ALLOC_MALLOC
  2138. alloc_malloc,
  2139. #endif
  2140. NULL,
  2141. };
  2142. void *(**func)(void *address);
  2143. #if defined(USE_OPENMP)
  2144. if (!memory_initialized) {
  2145. #endif
  2146. LOCK_COMMAND(&alloc_lock);
  2147. if (!memory_initialized) {
  2148. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2149. for (position = 0; position < NUM_BUFFERS; position ++){
  2150. memory[position].addr = (void *)0;
  2151. memory[position].pos = -1;
  2152. memory[position].used = 0;
  2153. memory[position].lock = 0;
  2154. }
  2155. #endif
  2156. #ifdef DYNAMIC_ARCH
  2157. gotoblas_dynamic_init();
  2158. #endif
  2159. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2160. gotoblas_affinity_init();
  2161. #endif
  2162. #ifdef SMP
  2163. if (!blas_num_threads) blas_cpu_number = blas_get_cpu_number();
  2164. #endif
  2165. #if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_IA64) || defined(ARCH_MIPS64) || defined(ARCH_ARM64)
  2166. #ifndef DYNAMIC_ARCH
  2167. blas_set_parameter();
  2168. #endif
  2169. #endif
  2170. memory_initialized = 1;
  2171. }
  2172. UNLOCK_COMMAND(&alloc_lock);
  2173. #if defined(USE_OPENMP)
  2174. }
  2175. #endif
  2176. #ifdef DEBUG
  2177. printf("Alloc Start ...\n");
  2178. #endif
  2179. /* #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2180. mypos = WhereAmI();
  2181. position = mypos;
  2182. while (position >= NUM_BUFFERS) position >>= 1;
  2183. do {
  2184. if (!memory[position].used && (memory[position].pos == mypos)) {
  2185. #if defined(SMP) && !defined(USE_OPENMP)
  2186. LOCK_COMMAND(&alloc_lock);
  2187. #else
  2188. blas_lock(&memory[position].lock);
  2189. #endif
  2190. if (!memory[position].used) goto allocation;
  2191. #if defined(SMP) && !defined(USE_OPENMP)
  2192. UNLOCK_COMMAND(&alloc_lock);
  2193. #else
  2194. blas_unlock(&memory[position].lock);
  2195. #endif
  2196. }
  2197. position ++;
  2198. } while (position < NUM_BUFFERS);
  2199. #endif */
  2200. position = 0;
  2201. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2202. LOCK_COMMAND(&alloc_lock);
  2203. #endif
  2204. do {
  2205. RMB;
  2206. #if defined(USE_OPENMP)
  2207. if (!memory[position].used) {
  2208. blas_lock(&memory[position].lock);
  2209. #endif
  2210. if (!memory[position].used) goto allocation;
  2211. #if defined(USE_OPENMP)
  2212. blas_unlock(&memory[position].lock);
  2213. }
  2214. #endif
  2215. position ++;
  2216. } while (position < NUM_BUFFERS);
  2217. if (memory_overflowed) {
  2218. do {
  2219. RMB;
  2220. #if defined(USE_OPENMP)
  2221. if (!newmemory[position-NUM_BUFFERS].used) {
  2222. blas_lock(&newmemory[position-NUM_BUFFERS].lock);
  2223. #endif
  2224. if (!newmemory[position-NUM_BUFFERS].used) goto allocation2;
  2225. #if defined(USE_OPENMP)
  2226. blas_unlock(&newmemory[position-NUM_BUFFERS].lock);
  2227. }
  2228. #endif
  2229. position ++;
  2230. } while (position < NEW_BUFFERS + NUM_BUFFERS);
  2231. }
  2232. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2233. UNLOCK_COMMAND(&alloc_lock);
  2234. #endif
  2235. goto error;
  2236. allocation :
  2237. #ifdef DEBUG
  2238. printf(" Position -> %d\n", position);
  2239. #endif
  2240. memory[position].used = 1;
  2241. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2242. UNLOCK_COMMAND(&alloc_lock);
  2243. #else
  2244. blas_unlock(&memory[position].lock);
  2245. #endif
  2246. if (!memory[position].addr) {
  2247. do {
  2248. #ifdef DEBUG
  2249. printf("Allocation Start : %lx\n", base_address);
  2250. #endif
  2251. map_address = (void *)-1;
  2252. func = &memoryalloc[0];
  2253. while ((*func != NULL) && (map_address == (void *) -1)) {
  2254. map_address = (*func)((void *)base_address);
  2255. #ifdef ALLOC_DEVICEDRIVER
  2256. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  2257. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation was failed.\n");
  2258. }
  2259. #endif
  2260. #ifdef ALLOC_HUGETLBFILE
  2261. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  2262. #ifndef OS_WINDOWS
  2263. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  2264. #endif
  2265. }
  2266. #endif
  2267. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  2268. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  2269. #endif
  2270. func ++;
  2271. }
  2272. #ifdef DEBUG
  2273. printf(" Success -> %08lx\n", map_address);
  2274. #endif
  2275. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  2276. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  2277. } while ((BLASLONG)map_address == -1);
  2278. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2279. LOCK_COMMAND(&alloc_lock);
  2280. #endif
  2281. memory[position].addr = map_address;
  2282. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2283. UNLOCK_COMMAND(&alloc_lock);
  2284. #endif
  2285. #ifdef DEBUG
  2286. printf(" Mapping Succeeded. %p(%d)\n", (void *)memory[position].addr, position);
  2287. #endif
  2288. }
  2289. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2290. if (memory[position].pos == -1) memory[position].pos = mypos;
  2291. #endif
  2292. #ifdef DYNAMIC_ARCH
  2293. if (memory_initialized == 1) {
  2294. LOCK_COMMAND(&alloc_lock);
  2295. if (memory_initialized == 1) {
  2296. if (!gotoblas) gotoblas_dynamic_init();
  2297. memory_initialized = 2;
  2298. }
  2299. UNLOCK_COMMAND(&alloc_lock);
  2300. }
  2301. #endif
  2302. #ifdef DEBUG
  2303. printf("Mapped : %p %3d\n\n",
  2304. (void *)memory[position].addr, position);
  2305. #endif
  2306. return (void *)memory[position].addr;
  2307. error:
  2308. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2309. LOCK_COMMAND(&alloc_lock);
  2310. #endif
  2311. if (memory_overflowed) goto terminate;
  2312. fprintf(stderr,"OpenBLAS warning: precompiled NUM_THREADS exceeded, adding auxiliary array for thread metadata.\n");
  2313. fprintf(stderr,"To avoid this warning, please rebuild your copy of OpenBLAS with a larger NUM_THREADS setting\n");
  2314. fprintf(stderr,"or set the environment variable OPENBLAS_NUM_THREADS to %d or lower\n", MAX_CPU_NUMBER);
  2315. memory_overflowed=1;
  2316. MB;
  2317. new_release_info = (struct release_t*) malloc(NEW_BUFFERS * sizeof(struct release_t));
  2318. newmemory = (struct newmemstruct*) malloc(NEW_BUFFERS * sizeof(struct newmemstruct));
  2319. for (i = 0; i < NEW_BUFFERS; i++) {
  2320. newmemory[i].addr = (void *)0;
  2321. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2322. newmemory[i].pos = -1;
  2323. #endif
  2324. newmemory[i].used = 0;
  2325. newmemory[i].lock = 0;
  2326. }
  2327. allocation2:
  2328. newmemory[position-NUM_BUFFERS].used = 1;
  2329. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2330. UNLOCK_COMMAND(&alloc_lock);
  2331. #else
  2332. blas_unlock(&newmemory[position-NUM_BUFFERS].lock);
  2333. #endif
  2334. do {
  2335. #ifdef DEBUG
  2336. printf("Allocation Start : %lx\n", base_address);
  2337. #endif
  2338. map_address = (void *)-1;
  2339. func = &memoryalloc[0];
  2340. while ((*func != NULL) && (map_address == (void *) -1)) {
  2341. map_address = (*func)((void *)base_address);
  2342. #ifdef ALLOC_DEVICEDRIVER
  2343. if ((*func == alloc_devicedirver) && (map_address == (void *)-1)) {
  2344. fprintf(stderr, "OpenBLAS Warning ... Physically contiguous allocation was failed.\n");
  2345. }
  2346. #endif
  2347. #ifdef ALLOC_HUGETLBFILE
  2348. if ((*func == alloc_hugetlbfile) && (map_address == (void *)-1)) {
  2349. #ifndef OS_WINDOWS
  2350. fprintf(stderr, "OpenBLAS Warning ... HugeTLB(File) allocation was failed.\n");
  2351. #endif
  2352. }
  2353. #endif
  2354. #if (defined ALLOC_SHM) && (defined OS_LINUX || defined OS_AIX || defined __sun__ || defined OS_WINDOWS)
  2355. if ((*func == alloc_hugetlb) && (map_address != (void *)-1)) hugetlb_allocated = 1;
  2356. #endif
  2357. func ++;
  2358. }
  2359. #ifdef DEBUG
  2360. printf(" Success -> %08lx\n", map_address);
  2361. #endif
  2362. if (((BLASLONG) map_address) == -1) base_address = 0UL;
  2363. if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
  2364. } while ((BLASLONG)map_address == -1);
  2365. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2366. LOCK_COMMAND(&alloc_lock);
  2367. #endif
  2368. newmemory[position-NUM_BUFFERS].addr = map_address;
  2369. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2370. UNLOCK_COMMAND(&alloc_lock);
  2371. #endif
  2372. #ifdef DEBUG
  2373. printf(" Mapping Succeeded. %p(%d)\n", (void *)newmemory[position-NUM_BUFFERS].addr, position);
  2374. #endif
  2375. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2376. if (newmemory[position-NUM_BUFFERS].pos == -1) newmemory[position-NUM_BUFFERS].pos = mypos;
  2377. #endif
  2378. return (void *)newmemory[position-NUM_BUFFERS].addr;
  2379. terminate:
  2380. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2381. UNLOCK_COMMAND(&alloc_lock);
  2382. #endif
  2383. printf("OpenBLAS : Program is Terminated. Because you tried to allocate too many memory regions.\n");
  2384. printf("This library was built to support a maximum of %d threads - either rebuild OpenBLAS\n", NUM_BUFFERS);
  2385. printf("with a larger NUM_THREADS value or set the environment variable OPENBLAS_NUM_THREADS to\n");
  2386. printf("a sufficiently small number. This error typically occurs when the software that relies on\n");
  2387. printf("OpenBLAS calls BLAS functions from many threads in parallel, or when your computer has more\n");
  2388. printf("cpu cores than what OpenBLAS was configured to handle.\n");
  2389. return NULL;
  2390. }
  2391. void blas_memory_free(void *free_area){
  2392. int position;
  2393. #ifdef DEBUG
  2394. printf("Unmapped Start : %p ...\n", free_area);
  2395. #endif
  2396. position = 0;
  2397. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2398. LOCK_COMMAND(&alloc_lock);
  2399. #endif
  2400. while ((position < NUM_BUFFERS) && (memory[position].addr != free_area))
  2401. position++;
  2402. if (position >= NUM_BUFFERS && !memory_overflowed) goto error;
  2403. #ifdef DEBUG
  2404. if (memory[position].addr != free_area) goto error;
  2405. printf(" Position : %d\n", position);
  2406. #endif
  2407. if (unlikely(memory_overflowed && position >= NUM_BUFFERS)) {
  2408. while ((position < NUM_BUFFERS+NEW_BUFFERS) && (newmemory[position-NUM_BUFFERS].addr != free_area))
  2409. position++;
  2410. // arm: ensure all writes are finished before other thread takes this memory
  2411. WMB;
  2412. if (position - NUM_BUFFERS >= NEW_BUFFERS) goto error;
  2413. newmemory[position-NUM_BUFFERS].used = 0;
  2414. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2415. UNLOCK_COMMAND(&alloc_lock);
  2416. #endif
  2417. #ifdef DEBUG
  2418. printf("Unmap from overflow area succeeded.\n\n");
  2419. #endif
  2420. return;
  2421. } else {
  2422. // arm: ensure all writes are finished before other thread takes this memory
  2423. WMB;
  2424. memory[position].used = 0;
  2425. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2426. UNLOCK_COMMAND(&alloc_lock);
  2427. #endif
  2428. #ifdef DEBUG
  2429. printf("Unmap Succeeded.\n\n");
  2430. #endif
  2431. return;
  2432. }
  2433. error:
  2434. printf("BLAS : Bad memory unallocation! : %4d %p\n", position, free_area);
  2435. #ifdef DEBUG
  2436. for (position = 0; position < NUM_BUFFERS; position++)
  2437. printf("%4ld %p : %d\n", position, memory[position].addr, memory[position].used);
  2438. #endif
  2439. #if (defined(SMP) || defined(USE_LOCKING)) && !defined(USE_OPENMP)
  2440. UNLOCK_COMMAND(&alloc_lock);
  2441. #endif
  2442. return;
  2443. }
  2444. void *blas_memory_alloc_nolock(int unused) {
  2445. void *map_address;
  2446. map_address = (void *)malloc(BUFFER_SIZE + FIXED_PAGESIZE);
  2447. return map_address;
  2448. }
  2449. void blas_memory_free_nolock(void * map_address) {
  2450. free(map_address);
  2451. }
  2452. void blas_shutdown(void){
  2453. int pos;
  2454. #ifdef SMP
  2455. BLASFUNC(blas_thread_shutdown)();
  2456. #endif
  2457. LOCK_COMMAND(&alloc_lock);
  2458. for (pos = 0; pos < release_pos; pos ++) {
  2459. if (likely(pos < NUM_BUFFERS))
  2460. release_info[pos].func(&release_info[pos]);
  2461. else
  2462. new_release_info[pos-NUM_BUFFERS].func(&new_release_info[pos-NUM_BUFFERS]);
  2463. }
  2464. #ifdef SEEK_ADDRESS
  2465. base_address = 0UL;
  2466. #else
  2467. base_address = BASE_ADDRESS;
  2468. #endif
  2469. for (pos = 0; pos < NUM_BUFFERS; pos ++){
  2470. memory[pos].addr = (void *)0;
  2471. memory[pos].used = 0;
  2472. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2473. memory[pos].pos = -1;
  2474. #endif
  2475. memory[pos].lock = 0;
  2476. }
  2477. if (memory_overflowed)
  2478. for (pos = 0; pos < NEW_BUFFERS; pos ++){
  2479. newmemory[pos].addr = (void *)0;
  2480. newmemory[pos].used = 0;
  2481. #if defined(WHEREAMI) && !defined(USE_OPENMP)
  2482. newmemory[pos].pos = -1;
  2483. #endif
  2484. newmemory[pos].lock = 0;
  2485. }
  2486. UNLOCK_COMMAND(&alloc_lock);
  2487. return;
  2488. }
  2489. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2490. #if defined(SMP) || defined(USE_LOCKING)
  2491. #if defined(USE_PTHREAD_LOCK)
  2492. static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER;
  2493. #elif defined(USE_PTHREAD_SPINLOCK)
  2494. static pthread_spinlock_t init_lock = 0;
  2495. #else
  2496. static BLASULONG init_lock = 0UL;
  2497. #endif
  2498. #endif
  2499. static void _touch_memory(blas_arg_t *arg, BLASLONG *range_m, BLASLONG *range_n,
  2500. void *sa, void *sb, BLASLONG pos) {
  2501. #if !defined(ARCH_POWER) && !defined(ARCH_SPARC)
  2502. size_t size;
  2503. BLASULONG buffer;
  2504. size = BUFFER_SIZE - PAGESIZE;
  2505. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2506. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2507. if (hot_alloc != 2) {
  2508. #endif
  2509. #if defined(SMP) || defined(USE_LOCKING)
  2510. LOCK_COMMAND(&init_lock);
  2511. #endif
  2512. while (size > 0) {
  2513. *(int *)buffer = size;
  2514. buffer += PAGESIZE;
  2515. size -= PAGESIZE;
  2516. }
  2517. #if defined(SMP) || defined(USE_LOCKING)
  2518. UNLOCK_COMMAND(&init_lock);
  2519. #endif
  2520. size = MIN((BUFFER_SIZE - PAGESIZE), L2_SIZE);
  2521. buffer = (BLASULONG)sa + GEMM_OFFSET_A;
  2522. while (size > 0) {
  2523. *(int *)buffer = size;
  2524. buffer += 64;
  2525. size -= 64;
  2526. }
  2527. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2528. }
  2529. #endif
  2530. #endif
  2531. }
  2532. #ifdef SMP
  2533. static void _init_thread_memory(void *buffer) {
  2534. blas_queue_t queue[MAX_CPU_NUMBER];
  2535. int num_cpu;
  2536. for (num_cpu = 0; num_cpu < blas_num_threads; num_cpu++) {
  2537. blas_queue_init(&queue[num_cpu]);
  2538. queue[num_cpu].mode = BLAS_DOUBLE | BLAS_REAL;
  2539. queue[num_cpu].routine = &_touch_memory;
  2540. queue[num_cpu].args = NULL;
  2541. queue[num_cpu].next = &queue[num_cpu + 1];
  2542. }
  2543. queue[num_cpu - 1].next = NULL;
  2544. queue[0].sa = buffer;
  2545. exec_blas(num_cpu, queue);
  2546. }
  2547. #endif
  2548. static void gotoblas_memory_init(void) {
  2549. void *buffer;
  2550. hot_alloc = 1;
  2551. buffer = (void *)blas_memory_alloc(0);
  2552. #ifdef SMP
  2553. if (blas_cpu_number == 0) blas_get_cpu_number();
  2554. #ifdef SMP_SERVER
  2555. if (blas_server_avail == 0) blas_thread_init();
  2556. #endif
  2557. _init_thread_memory((void *)((BLASULONG)buffer + GEMM_OFFSET_A));
  2558. #else
  2559. _touch_memory(NULL, NULL, NULL, (void *)((BLASULONG)buffer + GEMM_OFFSET_A), NULL, 0);
  2560. #endif
  2561. blas_memory_free(buffer);
  2562. }
  2563. #endif
  2564. /* Initialization for all function; this function should be called before main */
  2565. static int gotoblas_initialized = 0;
  2566. extern void openblas_read_env(void);
  2567. void CONSTRUCTOR gotoblas_init(void) {
  2568. if (gotoblas_initialized) return;
  2569. #ifdef SMP
  2570. openblas_fork_handler();
  2571. #endif
  2572. openblas_read_env();
  2573. #ifdef PROFILE
  2574. moncontrol (0);
  2575. #endif
  2576. #ifdef DYNAMIC_ARCH
  2577. gotoblas_dynamic_init();
  2578. #endif
  2579. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2580. gotoblas_affinity_init();
  2581. #endif
  2582. #if defined(OS_LINUX) && !defined(NO_WARMUP)
  2583. gotoblas_memory_init();
  2584. #endif
  2585. //#if defined(OS_LINUX)
  2586. #if 0
  2587. struct rlimit curlimit;
  2588. if ( getrlimit(RLIMIT_STACK, &curlimit ) == 0 )
  2589. {
  2590. if ( curlimit.rlim_cur != curlimit.rlim_max )
  2591. {
  2592. curlimit.rlim_cur = curlimit.rlim_max;
  2593. setrlimit(RLIMIT_STACK, &curlimit);
  2594. }
  2595. }
  2596. #endif
  2597. #ifdef SMP
  2598. if (blas_cpu_number == 0) blas_get_cpu_number();
  2599. #ifdef SMP_SERVER
  2600. if (blas_server_avail == 0) blas_thread_init();
  2601. #endif
  2602. #endif
  2603. #ifdef FUNCTION_PROFILE
  2604. gotoblas_profile_init();
  2605. #endif
  2606. gotoblas_initialized = 1;
  2607. #ifdef PROFILE
  2608. moncontrol (1);
  2609. #endif
  2610. }
  2611. void DESTRUCTOR gotoblas_quit(void) {
  2612. if (gotoblas_initialized == 0) return;
  2613. blas_shutdown();
  2614. #ifdef PROFILE
  2615. moncontrol (0);
  2616. #endif
  2617. #ifdef FUNCTION_PROFILE
  2618. gotoblas_profile_quit();
  2619. #endif
  2620. #if defined(SMP) && defined(OS_LINUX) && !defined(NO_AFFINITY)
  2621. gotoblas_affinity_quit();
  2622. #endif
  2623. #ifdef DYNAMIC_ARCH
  2624. gotoblas_dynamic_quit();
  2625. #endif
  2626. gotoblas_initialized = 0;
  2627. #ifdef PROFILE
  2628. moncontrol (1);
  2629. #endif
  2630. }
  2631. #if defined(_MSC_VER) && !defined(__clang__)
  2632. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  2633. {
  2634. switch (ul_reason_for_call)
  2635. {
  2636. case DLL_PROCESS_ATTACH:
  2637. gotoblas_init();
  2638. break;
  2639. case DLL_THREAD_ATTACH:
  2640. break;
  2641. case DLL_THREAD_DETACH:
  2642. break;
  2643. case DLL_PROCESS_DETACH:
  2644. gotoblas_quit();
  2645. break;
  2646. default:
  2647. break;
  2648. }
  2649. return TRUE;
  2650. }
  2651. /*
  2652. This is to allow static linking.
  2653. Code adapted from Google performance tools:
  2654. https://gperftools.googlecode.com/git-history/perftools-1.0/src/windows/port.cc
  2655. Reference:
  2656. https://sourceware.org/ml/pthreads-win32/2008/msg00028.html
  2657. http://ci.boost.org/svn-trac/browser/trunk/libs/thread/src/win32/tss_pe.cpp
  2658. */
  2659. static int on_process_term(void)
  2660. {
  2661. gotoblas_quit();
  2662. return 0;
  2663. }
  2664. #ifdef _WIN64
  2665. #pragma comment(linker, "/INCLUDE:_tls_used")
  2666. #else
  2667. #pragma comment(linker, "/INCLUDE:__tls_used")
  2668. #endif
  2669. #ifdef _WIN64
  2670. #pragma const_seg(".CRT$XLB")
  2671. #else
  2672. #pragma data_seg(".CRT$XLB")
  2673. #endif
  2674. static void (APIENTRY *dll_callback)(HINSTANCE h, DWORD ul_reason_for_call, PVOID pv) = DllMain;
  2675. #ifdef _WIN64
  2676. #pragma const_seg()
  2677. #else
  2678. #pragma data_seg()
  2679. #endif
  2680. #ifdef _WIN64
  2681. #pragma const_seg(".CRT$XTU")
  2682. #else
  2683. #pragma data_seg(".CRT$XTU")
  2684. #endif
  2685. static int(*p_process_term)(void) = on_process_term;
  2686. #ifdef _WIN64
  2687. #pragma const_seg()
  2688. #else
  2689. #pragma data_seg()
  2690. #endif
  2691. #endif
  2692. #if (defined(C_PGI) || (!defined(C_SUN) && defined(F_INTERFACE_SUN))) && (defined(ARCH_X86) || defined(ARCH_X86_64))
  2693. /* Don't call me; this is just work around for PGI / Sun bug */
  2694. void gotoblas_dummy_for_PGI(void) {
  2695. gotoblas_init();
  2696. gotoblas_quit();
  2697. #if __PGIC__ < 19
  2698. #if 0
  2699. asm ("\t.section\t.ctors,\"aw\",@progbits; .align 8; .quad gotoblas_init; .section .text");
  2700. asm ("\t.section\t.dtors,\"aw\",@progbits; .align 8; .quad gotoblas_quit; .section .text");
  2701. #else
  2702. asm (".section .init,\"ax\"; call gotoblas_init@PLT; .section .text");
  2703. asm (".section .fini,\"ax\"; call gotoblas_quit@PLT; .section .text");
  2704. #endif
  2705. #endif
  2706. }
  2707. #endif
  2708. #endif