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

memory.c 85 kB

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