You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

memory.c 85 kB

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