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

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