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